Debian Codegraph @

legacy/tpm2/error.go

raw
1 package tpm2
2
3 import (
4 "fmt"
5
6 "github.com/google/go-tpm/tpmutil"
7 )
8
9 type (
10 // RCFmt0 holds Format 0 error codes
11 RCFmt0 uint8
12
13 // RCFmt1 holds Format 1 error codes
14 RCFmt1 uint8
15
16 // RCWarn holds error codes used in warnings
17 RCWarn uint8
18
19 // RCIndex is used to reference arguments, handles and sessions in errors
20 RCIndex uint8
21 )
22
23 // Format 0 error codes.
24 const (
25 RCInitialize RCFmt0 = 0x00
26 RCFailure RCFmt0 = 0x01
27 RCSequence RCFmt0 = 0x03
28 RCPrivate RCFmt0 = 0x0B
29 RCHMAC RCFmt0 = 0x19
30 RCDisabled RCFmt0 = 0x20
31 RCExclusive RCFmt0 = 0x21
32 RCAuthType RCFmt0 = 0x24
33 RCAuthMissing RCFmt0 = 0x25
34 RCPolicy RCFmt0 = 0x26
35 RCPCR RCFmt0 = 0x27
36 RCPCRChanged RCFmt0 = 0x28
37 RCUpgrade RCFmt0 = 0x2D
38 RCTooManyContexts RCFmt0 = 0x2E
39 RCAuthUnavailable RCFmt0 = 0x2F
40 RCReboot RCFmt0 = 0x30
41 RCUnbalanced RCFmt0 = 0x31
42 RCCommandSize RCFmt0 = 0x42
43 RCCommandCode RCFmt0 = 0x43
44 RCAuthSize RCFmt0 = 0x44
45 RCAuthContext RCFmt0 = 0x45
46 RCNVRange RCFmt0 = 0x46
47 RCNVSize RCFmt0 = 0x47
48 RCNVLocked RCFmt0 = 0x48
49 RCNVAuthorization RCFmt0 = 0x49
50 RCNVUninitialized RCFmt0 = 0x4A
51 RCNVSpace RCFmt0 = 0x4B
52 RCNVDefined RCFmt0 = 0x4C
53 RCBadContext RCFmt0 = 0x50
54 RCCPHash RCFmt0 = 0x51
55 RCParent RCFmt0 = 0x52
56 RCNeedsTest RCFmt0 = 0x53
57 RCNoResult RCFmt0 = 0x54
58 RCSensitive RCFmt0 = 0x55
59 )
60
61 var fmt0Msg = map[RCFmt0]string{
62 RCInitialize: "TPM not initialized by TPM2_Startup or already initialized",
63 RCFailure: "commands not being accepted because of a TPM failure",
64 RCSequence: "improper use of a sequence handle",
65 RCPrivate: "not currently used",
66 RCHMAC: "not currently used",
67 RCDisabled: "the command is disabled",
68 RCExclusive: "command failed because audit sequence required exclusivity",
69 RCAuthType: "authorization handle is not correct for command",
70 RCAuthMissing: "5 command requires an authorization session for handle and it is not present",
71 RCPolicy: "policy failure in math operation or an invalid authPolicy value",
72 RCPCR: "PCR check fail",
73 RCPCRChanged: "PCR have changed since checked",
74 RCUpgrade: "TPM is in field upgrade mode unless called via TPM2_FieldUpgradeData(), then it is not in field upgrade mode",
75 RCTooManyContexts: "context ID counter is at maximum",
76 RCAuthUnavailable: "authValue or authPolicy is not available for selected entity",
77 RCReboot: "a _TPM_Init and Startup(CLEAR) is required before the TPM can resume operation",
78 RCUnbalanced: "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",
79 RCCommandSize: "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",
80 RCCommandCode: "command code not supported",
81 RCAuthSize: "the value of authorizationSize is out of range or the number of octets in the Authorization Area is greater than required",
82 RCAuthContext: "use of an authorization session with a context command or another command that cannot have an authorization session",
83 RCNVRange: "NV offset+size is out of range",
84 RCNVSize: "Requested allocation size is larger than allowed",
85 RCNVLocked: "NV access locked",
86 RCNVAuthorization: "NV access authorization fails in command actions",
87 RCNVUninitialized: "an NV Index is used before being initialized or the state saved by TPM2_Shutdown(STATE) could not be restored",
88 RCNVSpace: "insufficient space for NV allocation",
89 RCNVDefined: "NV Index or persistent object already defined",
90 RCBadContext: "context in TPM2_ContextLoad() is not valid",
91 RCCPHash: "cpHash value already set or not correct for use",
92 RCParent: "handle for parent is not a valid parent",
93 RCNeedsTest: "some function needs testing",
94 RCNoResult: "returned when 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",
95 RCSensitive: "the sensitive area did not unmarshal correctly after decryption",
96 }
97
98 // Format 1 error codes.
99 const (
100 RCAsymmetric = 0x01
101 RCAttributes = 0x02
102 RCHash = 0x03
103 RCValue = 0x04
104 RCHierarchy = 0x05
105 RCKeySize = 0x07
106 RCMGF = 0x08
107 RCMode = 0x09
108 RCType = 0x0A
109 RCHandle = 0x0B
110 RCKDF = 0x0C
111 RCRange = 0x0D
112 RCAuthFail = 0x0E
113 RCNonce = 0x0F
114 RCPP = 0x10
115 RCScheme = 0x12
116 RCSize = 0x15
117 RCSymmetric = 0x16
118 RCTag = 0x17
119 RCSelector = 0x18
120 RCInsufficient = 0x1A
121 RCSignature = 0x1B
122 RCKey = 0x1C
123 RCPolicyFail = 0x1D
124 RCIntegrity = 0x1F
125 RCTicket = 0x20
126 RCReservedBits = 0x21
127 RCBadAuth = 0x22
128 RCExpired = 0x23
129 RCPolicyCC = 0x24
130 RCBinding = 0x25
131 RCCurve = 0x26
132 RCECCPoint = 0x27
133 )
134
135 var fmt1Msg = map[RCFmt1]string{
136 RCAsymmetric: "asymmetric algorithm not supported or not correct",
137 RCAttributes: "inconsistent attributes",
138 RCHash: "hash algorithm not supported or not appropriate",
139 RCValue: "value is out of range or is not correct for the context",
140 RCHierarchy: "hierarchy is not enabled or is not correct for the use",
141 RCKeySize: "key size is not supported",
142 RCMGF: "mask generation function not supported",
143 RCMode: "mode of operation not supported",
144 RCType: "the type of the value is not appropriate for the use",
145 RCHandle: "the handle is not correct for the use",
146 RCKDF: "unsupported key derivation function or function not appropriate for use",
147 RCRange: "value was out of allowed range",
148 RCAuthFail: "the authorization HMAC check failed and DA counter incremented",
149 RCNonce: "invalid nonce size or nonce value mismatch",
150 RCPP: "authorization requires assertion of PP",
151 RCScheme: "unsupported or incompatible scheme",
152 RCSize: "structure is the wrong size",
153 RCSymmetric: "unsupported symmetric algorithm or key size, or not appropriate for instance",
154 RCTag: "incorrect structure tag",
155 RCSelector: "union selector is incorrect",
156 RCInsufficient: "the TPM was unable to unmarshal a value because there were not enough octets in the input buffer",
157 RCSignature: "the signature is not valid",
158 RCKey: "key fields are not compatible with the selected use",
159 RCPolicyFail: "a policy check failed",
160 RCIntegrity: "integrity check failed",
161 RCTicket: "invalid ticket",
162 RCReservedBits: "reserved bits not set to zero as required",
163 RCBadAuth: "authorization failure without DA implications",
164 RCExpired: "the policy has expired",
165 RCPolicyCC: "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",
166 RCBinding: "public and sensitive portions of an object are not cryptographically bound",
167 RCCurve: "curve not supported",
168 RCECCPoint: "point is not on the required curve",
169 }
170
171 // Warning codes.
172 const (
173 RCContextGap RCWarn = 0x01
174 RCObjectMemory RCWarn = 0x02
175 RCSessionMemory RCWarn = 0x03
176 RCMemory RCWarn = 0x04
177 RCSessionHandles RCWarn = 0x05
178 RCObjectHandles RCWarn = 0x06
179 RCLocality RCWarn = 0x07
180 RCYielded RCWarn = 0x08
181 RCCanceled RCWarn = 0x09
182 RCTesting RCWarn = 0x0A
183 RCReferenceH0 RCWarn = 0x10
184 RCReferenceH1 RCWarn = 0x11
185 RCReferenceH2 RCWarn = 0x12
186 RCReferenceH3 RCWarn = 0x13
187 RCReferenceH4 RCWarn = 0x14
188 RCReferenceH5 RCWarn = 0x15
189 RCReferenceH6 RCWarn = 0x16
190 RCReferenceS0 RCWarn = 0x18
191 RCReferenceS1 RCWarn = 0x19
192 RCReferenceS2 RCWarn = 0x1A
193 RCReferenceS3 RCWarn = 0x1B
194 RCReferenceS4 RCWarn = 0x1C
195 RCReferenceS5 RCWarn = 0x1D
196 RCReferenceS6 RCWarn = 0x1E
197 RCNVRate RCWarn = 0x20
198 RCLockout RCWarn = 0x21
199 RCRetry RCWarn = 0x22
200 RCNVUnavailable RCWarn = 0x23
201 )
202
203 var warnMsg = map[RCWarn]string{
204 RCContextGap: "gap for context ID is too large",
205 RCObjectMemory: "out of memory for object contexts",
206 RCSessionMemory: "out of memory for session contexts",
207 RCMemory: "out of shared object/session memory or need space for internal operations",
208 RCSessionHandles: "out of session handles",
209 RCObjectHandles: "out of object handles",
210 RCLocality: "bad locality",
211 RCYielded: "the TPM has suspended operation on the command; forward progress was made and the command may be retried",
212 RCCanceled: "the command was canceled",
213 RCTesting: "TPM is performing self-tests",
214 RCReferenceH0: "the 1st handle in the handle area references a transient object or session that is not loaded",
215 RCReferenceH1: "the 2nd handle in the handle area references a transient object or session that is not loaded",
216 RCReferenceH2: "the 3rd handle in the handle area references a transient object or session that is not loaded",
217 RCReferenceH3: "the 4th handle in the handle area references a transient object or session that is not loaded",
218 RCReferenceH4: "the 5th handle in the handle area references a transient object or session that is not loaded",
219 RCReferenceH5: "the 6th handle in the handle area references a transient object or session that is not loaded",
220 RCReferenceH6: "the 7th handle in the handle area references a transient object or session that is not loaded",
221 RCReferenceS0: "the 1st authorization session handle references a session that is not loaded",
222 RCReferenceS1: "the 2nd authorization session handle references a session that is not loaded",
223 RCReferenceS2: "the 3rd authorization session handle references a session that is not loaded",
224 RCReferenceS3: "the 4th authorization session handle references a session that is not loaded",
225 RCReferenceS4: "the 5th authorization session handle references a session that is not loaded",
226 RCReferenceS5: "the 6th authorization session handle references a session that is not loaded",
227 RCReferenceS6: "the 7th authorization session handle references a session that is not loaded",
228 RCNVRate: "the TPM is rate-limiting accesses to prevent wearout of NV",
229 RCLockout: "authorizations for objects subject to DA protection are not allowed at this time because the TPM is in DA lockout mode",
230 RCRetry: "the TPM was not able to start the command",
231 RCNVUnavailable: "the command may require writing of NV and NV is not current accessible",
232 }
233
234 // Indexes for arguments, handles and sessions.
235 const (
236 RC1 RCIndex = iota + 1
237 RC2
238 RC3
239 RC4
240 RC5
241 RC6
242 RC7
243 RC8
244 RC9
245 RCA
246 RCB
247 RCC
248 RCD
249 RCE
250 RCF
251 )
252
253 const unknownCode = "unknown error code"
254
255 // Error is returned for all Format 0 errors from the TPM. It is used for general
256 // errors not specific to a parameter, handle or session.
257 type Error struct {
258 Code RCFmt0
259 }
260
261 func (e Error) Error() string {
262 msg := fmt0Msg[e.Code]
263 if msg == "" {
264 msg = unknownCode
265 }
266 return fmt.Sprintf("error code 0x%x : %s", e.Code, msg)
267 }
268
269 // VendorError represents a vendor-specific error response. These types of responses
270 // are not decoded and Code contains the complete response code.
271 type VendorError struct {
272 Code uint32
273 }
274
275 func (e VendorError) Error() string {
276 return fmt.Sprintf("vendor error code 0x%x", e.Code)
277 }
278
279 // Warning is typically used to report transient errors.
280 type Warning struct {
281 Code RCWarn
282 }
283
284 func (w Warning) Error() string {
285 msg := warnMsg[w.Code]
286 if msg == "" {
287 msg = unknownCode
288 }
289 return fmt.Sprintf("warning code 0x%x : %s", w.Code, msg)
290 }
291
292 // ParameterError describes an error related to a parameter, and the parameter number.
293 type ParameterError struct {
294 Code RCFmt1
295 Parameter RCIndex
296 }
297
298 func (e ParameterError) Error() string {
299 msg := fmt1Msg[e.Code]
300 if msg == "" {
301 msg = unknownCode
302 }
303 return fmt.Sprintf("parameter %d, error code 0x%x : %s", e.Parameter, e.Code, msg)
304 }
305
306 // HandleError describes an error related to a handle, and the handle number.
307 type HandleError struct {
308 Code RCFmt1
309 Handle RCIndex
310 }
311
312 func (e HandleError) Error() string {
313 msg := fmt1Msg[e.Code]
314 if msg == "" {
315 msg = unknownCode
316 }
317 return fmt.Sprintf("handle %d, error code 0x%x : %s", e.Handle, e.Code, msg)
318 }
319
320 // SessionError describes an error related to a session, and the session number.
321 type SessionError struct {
322 Code RCFmt1
323 Session RCIndex
324 }
325
326 func (e SessionError) Error() string {
327 msg := fmt1Msg[e.Code]
328 if msg == "" {
329 msg = unknownCode
330 }
331 return fmt.Sprintf("session %d, error code 0x%x : %s", e.Session, e.Code, msg)
332 }
333
334 // Decode a TPM2 response code and return the appropriate error. Logic
335 // according to the "Response Code Evaluation" chart in Part 1 of the TPM 2.0
336 // spec.
337 func decodeResponse(code tpmutil.ResponseCode) error {
338 if code == tpmutil.RCSuccess {
339 return nil
340 }
341 if code&0x180 == 0 { // Bits 7:8 == 0 is a TPM1 error
342 return fmt.Errorf("response status 0x%x", code)
343 }
344 if code&0x80 == 0 { // Bit 7 unset
345 if code&0x400 > 0 { // Bit 10 set, vendor specific code
346 return VendorError{uint32(code)}
347 }
348 if code&0x800 > 0 { // Bit 11 set, warning with code in bit 0:6
349 return Warning{RCWarn(code & 0x7f)}
350 }
351 // error with code in bit 0:6
352 return Error{RCFmt0(code & 0x7f)}
353 }
354 if code&0x40 > 0 { // Bit 6 set, code in 0:5, parameter number in 8:11
355 return ParameterError{RCFmt1(code & 0x3f), RCIndex((code & 0xf00) >> 8)}
356 }
357 if code&0x800 == 0 { // Bit 11 unset, code in 0:5, handle in 8:10
358 return HandleError{RCFmt1(code & 0x3f), RCIndex((code & 0x700) >> 8)}
359 }
360 // Code in 0:5, Session in 8:10
361 return SessionError{RCFmt1(code & 0x3f), RCIndex((code & 0x700) >> 8)}
362 }