Debian Codegraph @

tpm2/errors.go

raw
1 package tpm2
2
3 import (
4 "fmt"
5 )
6
7 type errorDesc struct {
8 name string
9 description string
10 }
11
12 var fmt0Descs = map[TPMRC]errorDesc{
13 TPMRCInitialize: {
14 name: "TPM_RC_INITIALIZE",
15 description: "TPM not initialized by TPM2_Startup or already initialized",
16 },
17 TPMRCFailure: {
18 name: "TPM_RC_FAILURE",
19 description: "commands not being accepted because of a TPM failure",
20 },
21 TPMRCSequence: {
22 name: "TPM_RC_SEQUENCE",
23 description: "improper use of a sequence handle",
24 },
25 TPMRCPrivate: {
26 name: "TPM_RC_PRIVATE",
27 description: "not currently used",
28 },
29 TPMRCHMAC: {
30 name: "TPM_RC_HMAC",
31 description: "not currently used",
32 },
33 TPMRCDisabled: {
34 name: "TPM_RC_DISABLED",
35 description: "the command is disabled",
36 },
37 TPMRCExclusive: {
38 name: "TPM_RC_EXCLUSIVE",
39 description: "command failed because audit sequence required exclusivity",
40 },
41 TPMRCAuthType: {
42 name: "TPM_RC_AUTH_TYPE",
43 description: "authorization handle is not correct for command",
44 },
45 TPMRCAuthMissing: {
46 name: "TPM_RC_AUTH_MISSING",
47 description: "command requires an authorization session for handle and it is not present.",
48 },
49 TPMRCPolicy: {
50 name: "TPM_RC_POLICY",
51 description: "policy failure in math operation or an invalid authPolicy value",
52 },
53 TPMRCPCR: {
54 name: "TPM_RC_PCR",
55 description: "PCR check fail",
56 },
57 TPMRCPCRChanged: {
58 name: "TPM_RC_PCR_CHANGED",
59 description: "PCR have changed since checked.",
60 },
61 TPMRCUpgrade: {
62 name: "TPM_RC_UPGRADE",
63 description: "for all commands other than TPM2_FieldUpgradeData(), this code indicates that the TPM is in field upgrade mode; for TPM2_FieldUpgradeData(), this code indicates that the TPM is not in field upgrade mode",
64 },
65 TPMRCTooManyContexts: {
66 name: "TPM_RC_TOO_MANY_CONTEXTS",
67 description: "context ID counter is at maximum.",
68 },
69 TPMRCAuthUnavailable: {
70 name: "TPM_RC_AUTH_UNAVAILABLE",
71 description: "authValue or authPolicy is not available for selected entity.",
72 },
73 TPMRCReboot: {
74 name: "TPM_RC_REBOOT",
75 description: "a _TPM_Init and Startup(CLEAR) is required before the TPM can resume operation.",
76 },
77 TPMRCUnbalanced: {
78 name: "TPM_RC_UNBALANCED",
79 description: "the protection algorithms (hash and symmetric) are not reasonably balanced. The digest size of the hash must be larger than the key size of the symmetric algorithm.",
80 },
81 TPMRCCommandSize: {
82 name: "TPM_RC_COMMAND_SIZE",
83 description: "command commandSize value is inconsistent with contents of the command buffer; either the size is not the same as the octets loaded by the hardware interface layer or the value is not large enough to hold a command header",
84 },
85 TPMRCCommandCode: {
86 name: "TPM_RC_COMMAND_CODE",
87 description: "command code not supported",
88 },
89 TPMRCAuthSize: {
90 name: "TPM_RC_AUTHSIZE",
91 description: "the value of authorizationSize is out of range or the number of octets in the Authorization Area is greater than required",
92 },
93 TPMRCAuthContext: {
94 name: "TPM_RC_AUTH_CONTEXT",
95 description: "use of an authorization session with a context command or another command that cannot have an authorization session.",
96 },
97 TPMRCNVRange: {
98 name: "TPM_RC_NV_RANGE",
99 description: "NV offset+size is out of range.",
100 },
101 TPMRCNVSize: {
102 name: "TPM_RC_NV_SIZE",
103 description: "Requested allocation size is larger than allowed.",
104 },
105 TPMRCNVLocked: {
106 name: "TPM_RC_NV_LOCKED",
107 description: "NV access locked.",
108 },
109 TPMRCNVAuthorization: {
110 name: "TPM_RC_NV_AUTHORIZATION",
111 description: "NV access authorization fails in command actions (this failure does not affect lockout.action)",
112 },
113 TPMRCNVUninitialized: {
114 name: "TPM_RC_NV_UNINITIALIZED",
115 description: "an NV Index is used before being initialized or the state saved by TPM2_Shutdown(STATE) could not be restored",
116 },
117 TPMRCNVSpace: {
118 name: "TPM_RC_NV_SPACE",
119 description: "insufficient space for NV allocation",
120 },
121 TPMRCNVDefined: {
122 name: "TPM_RC_NV_DEFINED",
123 description: "NV Index or persistent object already defined",
124 },
125 TPMRCBadContext: {
126 name: "TPM_RC_BAD_CONTEXT",
127 description: "context in TPM2_ContextLoad() is not valid",
128 },
129 TPMRCCPHash: {
130 name: "TPM_RC_CPHASH",
131 description: "cpHash value already set or not correct for use",
132 },
133 TPMRCParent: {
134 name: "TPM_RC_PARENT",
135 description: "handle for parent is not a valid parent",
136 },
137 TPMRCNeedsTest: {
138 name: "TPM_RC_NEEDS_TEST",
139 description: "some function needs testing.",
140 },
141 TPMRCNoResult: {
142 name: "TPM_RC_NO_RESULT",
143 description: "an internal function cannot process a request due to an unspecified problem. This code is usually related to invalid parameters that are not properly filtered by the input unmarshaling code.",
144 },
145 TPMRCSensitive: {
146 name: "TPM_RC_SENSITIVE",
147 description: "the sensitive area did not unmarshal correctly after decryption – this code is used in lieu of the other unmarshaling errors so that an attacker cannot determine where the unmarshaling error occurred",
148 },
149 }
150
151 var fmt1Descs = map[TPMRC]errorDesc{
152 TPMRCAsymmetric: {
153 name: "TPM_RC_ASYMMETRIC RC_FMT1",
154 description: "asymmetric algorithm not supported or not correct",
155 },
156 TPMRCAttributes: {
157 name: "TPM_RC_ATTRIBUTES",
158 description: "inconsistent attributes",
159 },
160 TPMRCHash: {
161 name: "TPM_RC_HASH",
162 description: "hash algorithm not supported or not appropriate",
163 },
164 TPMRCValue: {
165 name: "TPM_RC_VALUE",
166 description: "value is out of range or is not correct for the context",
167 },
168 TPMRCHierarchy: {
169 name: "TPM_RC_HIERARCHY",
170 description: "hierarchy is not enabled or is not correct for the use",
171 },
172 TPMRCKeySize: {
173 name: "TPM_RC_KEY_SIZE",
174 description: "key size is not supported",
175 },
176 TPMRCMGF: {
177 name: "TPM_RC_MGF",
178 description: "mask generation function not supported",
179 },
180 TPMRCMode: {
181 name: "TPM_RC_MODE",
182 description: "mode of operation not supported",
183 },
184 TPMRCType: {
185 name: "TPM_RC_TYPE",
186 description: "the type of the value is not appropriate for the use",
187 },
188 TPMRCHandle: {
189 name: "TPM_RC_HANDLE",
190 description: "the handle is not correct for the use",
191 },
192 TPMRCKDF: {
193 name: "TPM_RC_KDF",
194 description: "unsupported key derivation function or function not appropriate for use",
195 },
196 TPMRCRange: {
197 name: "TPM_RC_RANGE",
198 description: "value was out of allowed range.",
199 },
200 TPMRCAuthFail: {
201 name: "TPM_RC_AUTH_FAIL",
202 description: "the authorization HMAC check failed and DA counter incremented",
203 },
204 TPMRCNonce: {
205 name: "TPM_RC_NONCE",
206 description: "invalid nonce size or nonce value mismatch",
207 },
208 TPMRCPP: {
209 name: "TPM_RC_PP",
210 description: "authorization requires assertion of PP",
211 },
212 TPMRCScheme: {
213 name: "TPM_RC_SCHEME",
214 description: "unsupported or incompatible scheme",
215 },
216 TPMRCSize: {
217 name: "TPM_RC_SIZE",
218 description: "structure is the wrong size",
219 },
220 TPMRCSymmetric: {
221 name: "TPM_RC_SYMMETRIC",
222 description: "unsupported symmetric algorithm or key size, or not appropriate for instance",
223 },
224 TPMRCTag: {
225 name: "TPM_RC_TAG",
226 description: "incorrect structure tag",
227 },
228 TPMRCSelector: {
229 name: "TPM_RC_SELECTOR",
230 description: "union selector is incorrect",
231 },
232 TPMRCInsufficient: {
233 name: "TPM_RC_INSUFFICIENT",
234 description: "the TPM was unable to unmarshal a value because there were not enough octets in the input buffer",
235 },
236 TPMRCSignature: {
237 name: "TPM_RC_SIGNATURE",
238 description: "the signature is not valid",
239 },
240 TPMRCKey: {
241 name: "TPM_RC_KEY",
242 description: "key fields are not compatible with the selected use",
243 },
244 TPMRCPolicyFail: {
245 name: "TPM_RC_POLICY_FAIL",
246 description: "a policy check failed",
247 },
248 TPMRCIntegrity: {
249 name: "TPM_RC_INTEGRITY",
250 description: "integrity check failed",
251 },
252 TPMRCTicket: {
253 name: "TPM_RC_TICKET",
254 description: "invalid ticket",
255 },
256 TPMRCReservedBits: {
257 name: "TPM_RC_RESERVED_BITS",
258 description: "reserved bits not set to zero as required",
259 },
260 TPMRCBadAuth: {
261 name: "TPM_RC_BAD_AUTH",
262 description: "authorization failure without DA implications",
263 },
264 TPMRCExpired: {
265 name: "TPM_RC_EXPIRED",
266 description: "the policy has expired",
267 },
268 TPMRCPolicyCC: {
269 name: "TPM_RC_POLICY_CC",
270 description: "the commandCode in the policy is not the commandCode of the command or the command code in a policy command references a command that is not implemented",
271 },
272 TPMRCBinding: {
273 name: "TPM_RC_BINDING",
274 description: "public and sensitive portions of an object are not cryptographically bound",
275 },
276 TPMRCCurve: {
277 name: "TPM_RC_CURVE",
278 description: "curve not supported",
279 },
280 TPMRCECCPoint: {
281 name: "TPM_RC_ECC_POINT",
282 description: "point is not on the required curve.",
283 },
284 }
285
286 var warnDescs = map[TPMRC]errorDesc{
287 TPMRCContextGap: {
288 name: "TPM_RC_CONTEXT_GAP",
289 description: "gap for context ID is too large",
290 },
291 TPMRCObjectMemory: {
292 name: "TPM_RC_OBJECT_MEMORY",
293 description: "out of memory for object contexts",
294 },
295 TPMRCSessionMemory: {
296 name: "TPM_RC_SESSION_MEMORY",
297 description: "out of memory for session contexts",
298 },
299 TPMRCMemory: {
300 name: "TPM_RC_MEMORY",
301 description: "out of shared object/session memory or need space for internal operations",
302 },
303 TPMRCSessionHandles: {
304 name: "TPM_RC_SESSION_HANDLES",
305 description: "out of session handles – a session must be flushed before a new session may be created",
306 },
307 TPMRCObjectHandles: {
308 name: "TPM_RC_OBJECT_HANDLES",
309 description: "out of object handles – the handle space for objects is depleted and a reboot is required",
310 },
311 TPMRCLocality: {
312 name: "TPM_RC_LOCALITY",
313 description: "bad locality",
314 },
315 TPMRCYielded: {
316 name: "TPM_RC_YIELDED",
317 description: "the TPM has suspended operation on the command; forward progress was made and the command may be retried",
318 },
319 TPMRCCanceled: {
320 name: "TPM_RC_CANCELED",
321 description: "the command was canceled",
322 },
323 TPMRCTesting: {
324 name: "TPM_RC_TESTING",
325 description: "TPM is performing self-tests",
326 },
327 TPMRCReferenceH0: {
328 name: "TPM_RC_REFERENCE_H0",
329 description: "the 1st handle in the handle area references a transient object or session that is not loaded",
330 },
331 TPMRCReferenceH1: {
332 name: "TPM_RC_REFERENCE_H1",
333 description: "the 2nd handle in the handle area references a transient object or session that is not loaded",
334 },
335 TPMRCReferenceH2: {
336 name: "TPM_RC_REFERENCE_H2",
337 description: "the 3rd handle in the handle area references a transient object or session that is not loaded",
338 },
339 TPMRCReferenceH3: {
340 name: "TPM_RC_REFERENCE_H3",
341 description: "the 4th handle in the handle area references a transient object or session that is not loaded",
342 },
343 TPMRCReferenceH4: {
344 name: "TPM_RC_REFERENCE_H4",
345 description: "the 5th handle in the handle area references a transient object or session that is not loaded",
346 },
347 TPMRCReferenceH5: {
348 name: "TPM_RC_REFERENCE_H5",
349 description: "the 6th handle in the handle area references a transient object or session that is not loaded",
350 },
351 TPMRCReferenceH6: {
352 name: "TPM_RC_REFERENCE_H6",
353 description: "the 7th handle in the handle area references a transient object or session that is not loaded",
354 },
355 TPMRCReferenceS0: {
356 name: "TPM_RC_REFERENCE_S0",
357 description: "the 1st authorization session handle references a session that is not loaded",
358 },
359 TPMRCReferenceS1: {
360 name: "TPM_RC_REFERENCE_S1",
361 description: "the 2nd authorization session handle references a session that is not loaded",
362 },
363 TPMRCReferenceS2: {
364 name: "TPM_RC_REFERENCE_S2",
365 description: "the 3rd authorization session handle references a session that is not loaded",
366 },
367 TPMRCReferenceS3: {
368 name: "TPM_RC_REFERENCE_S3",
369 description: "the 4th authorization session handle references a session that is not loaded",
370 },
371 TPMRCReferenceS4: {
372 name: "TPM_RC_REFERENCE_S4",
373 description: "the 5th session handle references a session that is not loaded",
374 },
375 TPMRCReferenceS5: {
376 name: "TPM_RC_REFERENCE_S5",
377 description: "the 6th session handle references a session that is not loaded",
378 },
379 TPMRCReferenceS6: {
380 name: "TPM_RC_REFERENCE_S6",
381 description: "the 7th authorization session handle references a session that is not loaded",
382 },
383 TPMRCNVRate: {
384 name: "TPM_RC_NV_RATE",
385 description: "the TPM is rate-limiting accesses to prevent wearout of NV",
386 },
387 TPMRCLockout: {
388 name: "TPM_RC_LOCKOUT",
389 description: "authorizations for objects subject to DA protection are not allowed at this time because the TPM is in DA lockout mode",
390 },
391 TPMRCRetry: {
392 name: "TPM_RC_RETRY",
393 description: "the TPM was not able to start the command",
394 },
395 TPMRCNVUnavailable: {
396 name: "TPM_RC_NV_UNAVAILABLE",
397 description: "the command may require writing of NV and NV is not current accessible",
398 },
399 }
400
401 // subject represents a subject of a TPM error code with additional details
402 // (i.e., FMT1 codes)
403 type subject int
404
405 const (
406 handleRelated subject = iota + 1
407 parameterRelated
408 sessionRelated
409 )
410
411 // String returns the string representation of the ErrorSubject.
412 func (s subject) String() string {
413 switch s {
414 case handleRelated:
415 return "handle"
416 case parameterRelated:
417 return "parameter"
418 case sessionRelated:
419 return "session"
420 default:
421 return "unknown subject"
422 }
423 }
424
425 // TPMFmt1Error represents a TPM 2.0 format-1 error, with additional information.
426 type TPMFmt1Error struct {
427 // The canonical TPM error code, with handle/parameter/session info
428 // stripped out.
429 canonical TPMRC
430 // Whether this was a handle, parameter, or session error.
431 subject subject
432 // Which handle, parameter, or session was in error
433 index int
434 }
435
436 // Error returns the string representation of the error.
437 func (e TPMFmt1Error) Error() string {
438 desc, ok := fmt1Descs[e.canonical]
439 if !ok {
440 return fmt.Sprintf("unknown format-1 error: %s %d (%x)", e.subject, e.index, uint32(e.canonical))
441 }
442 return fmt.Sprintf("%s (%v %d): %s", desc.name, e.subject, e.index, desc.description)
443 }
444
445 // Handle returns whether the error is handle-related and if so, which handle is
446 // in error.
447 func (e TPMFmt1Error) Handle() (bool, int) {
448 if e.subject != handleRelated {
449 return false, 0
450 }
451 return true, e.index
452 }
453
454 // Parameter returns whether the error is handle-related and if so, which handle
455 // is in error.
456 func (e TPMFmt1Error) Parameter() (bool, int) {
457 if e.subject != parameterRelated {
458 return false, 0
459 }
460 return true, e.index
461 }
462
463 // Session returns whether the error is handle-related and if so, which handle
464 // is in error.
465 func (e TPMFmt1Error) Session() (bool, int) {
466 if e.subject != sessionRelated {
467 return false, 0
468 }
469 return true, e.index
470 }
471
472 // isFmt0Error returns true if the result is a format-0 error.
473 func (r TPMRC) isFmt0Error() bool {
474 return (r&rcVer1) == rcVer1 && (r&rcWarn) != rcWarn
475 }
476
477 // isFmt1Error returns true and a format-1 error structure if the error is a
478 // format-1 error.
479 func (r TPMRC) isFmt1Error() (bool, TPMFmt1Error) {
480 if (r & rcFmt1) != rcFmt1 {
481 return false, TPMFmt1Error{}
482 }
483 subj := handleRelated
484 if (r & rcP) == rcP {
485 subj = parameterRelated
486 r ^= rcP
487 } else if (r & rcS) == rcS {
488 subj = sessionRelated
489 r ^= rcS
490 }
491 idx := int((r & 0xF00) >> 8)
492 r &= 0xFFFFF0FF
493 return true, TPMFmt1Error{
494 canonical: r,
495 subject: subj,
496 index: idx,
497 }
498 }
499
500 // IsWarning returns true if the error is a warning code.
501 // This usually indicates a problem with the TPM state, and not the command.
502 // Retrying the command later may succeed.
503 func (r TPMRC) IsWarning() bool {
504 if isFmt1, _ := r.isFmt1Error(); isFmt1 {
505 // There aren't any format-1 warnings.
506 return false
507 }
508 return (r&rcVer1) == rcVer1 && (r&rcWarn) == rcWarn
509 }
510
511 // Error produces a nice human-readable representation of the error, parsing TPM
512 // FMT1 errors as needed.
513 func (r TPMRC) Error() string {
514 if isFmt1, fmt1 := r.isFmt1Error(); isFmt1 {
515 return fmt1.Error()
516 }
517 if r.isFmt0Error() {
518 desc, ok := fmt0Descs[r]
519 if !ok {
520 return fmt.Sprintf("unknown format-0 error code (0x%x)", uint32(r))
521 }
522 return fmt.Sprintf("%s: %s", desc.name, desc.description)
523 }
524 if r.IsWarning() {
525 desc, ok := warnDescs[r]
526 if !ok {
527 return fmt.Sprintf("unknown warning (0x%x)", uint32(r))
528 }
529 return fmt.Sprintf("%s: %s", desc.name, desc.description)
530 }
531 return fmt.Sprintf("unrecognized error code (0x%x)", uint32(r))
532 }
533
534 // Is returns whether the TPMRC (which may be a FMT1 error) is equal to the
535 // given canonical error.
536 func (r TPMRC) Is(target error) bool {
537 targetTPMRC, ok := target.(TPMRC)
538 if !ok {
539 return false
540 }
541 if isFmt1, fmt1 := r.isFmt1Error(); isFmt1 {
542 return fmt1.canonical == targetTPMRC
543 }
544 return r == targetTPMRC
545 }
546
547 // As returns whether the error can be assigned to the given interface type.
548 // If supported, it updates the value pointed at by target.
549 // Supports the Fmt1Error type.
550 func (r TPMRC) As(target interface{}) bool {
551 pFmt1, ok := target.(*TPMFmt1Error)
552 if !ok {
553 return false
554 }
555 isFmt1, fmt1 := r.isFmt1Error()
556 if !isFmt1 {
557 return false
558 }
559 *pFmt1 = fmt1
560 return true
561 }