TianoCore EDK2 master
Loading...
Searching...
No Matches
Tpm2Object.c
Go to the documentation of this file.
1
13#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
15
16#pragma pack(1)
17
18typedef struct {
20 TPMI_DH_OBJECT ObjectHandle;
22
23typedef struct {
25 TPM2B_PUBLIC OutPublic;
26 TPM2B_NAME Name;
27 TPM2B_NAME QualifiedName;
29
30#pragma pack()
31
44EFIAPI
46 IN TPMI_DH_OBJECT ObjectHandle,
47 OUT TPM2B_PUBLIC *OutPublic,
48 OUT TPM2B_NAME *Name,
49 OUT TPM2B_NAME *QualifiedName
50 )
51{
52 EFI_STATUS Status;
53 TPM2_READ_PUBLIC_COMMAND SendBuffer;
55 UINT32 SendBufferSize;
56 UINT32 RecvBufferSize;
57 TPM_RC ResponseCode;
58 UINT8 *Buffer;
59 UINT16 OutPublicSize;
60 UINT16 NameSize;
61 UINT16 QualifiedNameSize;
62
63 //
64 // Construct command
65 //
66 SendBuffer.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
67 SendBuffer.Header.commandCode = SwapBytes32 (TPM_CC_ReadPublic);
68
69 SendBuffer.ObjectHandle = SwapBytes32 (ObjectHandle);
70
71 SendBufferSize = (UINT32)sizeof (SendBuffer);
72 SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
73
74 //
75 // send Tpm command
76 //
77 RecvBufferSize = sizeof (RecvBuffer);
78 Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
79 if (EFI_ERROR (Status)) {
80 return Status;
81 }
82
83 if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
84 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - RecvBufferSize Error - %x\n", RecvBufferSize));
85 return EFI_DEVICE_ERROR;
86 }
87
88 ResponseCode = SwapBytes32 (RecvBuffer.Header.responseCode);
89 if (ResponseCode != TPM_RC_SUCCESS) {
90 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - responseCode - %x\n", SwapBytes32 (RecvBuffer.Header.responseCode)));
91 }
92
93 switch (ResponseCode) {
94 case TPM_RC_SUCCESS:
95 // return data
96 break;
97 case TPM_RC_SEQUENCE:
98 // objectHandle references a sequence object
99 return EFI_INVALID_PARAMETER;
100 default:
101 return EFI_DEVICE_ERROR;
102 }
103
104 //
105 // Basic check
106 //
107 OutPublicSize = SwapBytes16 (RecvBuffer.OutPublic.size);
108 if (OutPublicSize > sizeof (TPMT_PUBLIC)) {
109 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - OutPublicSize error %x\n", OutPublicSize));
110 return EFI_DEVICE_ERROR;
111 }
112
113 NameSize = SwapBytes16 (
115 (UINT16 *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) +
116 sizeof (UINT16) + OutPublicSize)
117 )
118 );
119 if (NameSize > sizeof (TPMU_NAME)) {
120 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - NameSize error %x\n", NameSize));
121 return EFI_DEVICE_ERROR;
122 }
123
124 QualifiedNameSize = SwapBytes16 (
126 (UINT16 *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) +
127 sizeof (UINT16) + OutPublicSize +
128 sizeof (UINT16) + NameSize)
129 )
130 );
131 if (QualifiedNameSize > sizeof (TPMU_NAME)) {
132 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - QualifiedNameSize error %x\n", QualifiedNameSize));
133 return EFI_DEVICE_ERROR;
134 }
135
136 if (RecvBufferSize != sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + OutPublicSize + sizeof (UINT16) + NameSize + sizeof (UINT16) + QualifiedNameSize) {
137 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - RecvBufferSize %x Error - OutPublicSize %x, NameSize %x, QualifiedNameSize %x\n", RecvBufferSize, OutPublicSize, NameSize, QualifiedNameSize));
138 return EFI_DEVICE_ERROR;
139 }
140
141 //
142 // Return the response
143 //
144 Buffer = (UINT8 *)&RecvBuffer.OutPublic;
145 CopyMem (OutPublic, &RecvBuffer.OutPublic, sizeof (UINT16) + OutPublicSize);
146 OutPublic->size = OutPublicSize;
147 OutPublic->publicArea.type = SwapBytes16 (OutPublic->publicArea.type);
148 OutPublic->publicArea.nameAlg = SwapBytes16 (OutPublic->publicArea.nameAlg);
149 WriteUnaligned32 ((UINT32 *)&OutPublic->publicArea.objectAttributes, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&OutPublic->publicArea.objectAttributes)));
150 Buffer = (UINT8 *)&RecvBuffer.OutPublic.publicArea.authPolicy;
151 OutPublic->publicArea.authPolicy.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
152 Buffer += sizeof (UINT16);
153 if (OutPublic->publicArea.authPolicy.size > sizeof (TPMU_HA)) {
154 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - authPolicy.size error %x\n", OutPublic->publicArea.authPolicy.size));
155 return EFI_DEVICE_ERROR;
156 }
157
158 CopyMem (OutPublic->publicArea.authPolicy.buffer, Buffer, OutPublic->publicArea.authPolicy.size);
159 Buffer += OutPublic->publicArea.authPolicy.size;
160
161 // TPMU_PUBLIC_PARMS
162 switch (OutPublic->publicArea.type) {
163 case TPM_ALG_KEYEDHASH:
164 OutPublic->publicArea.parameters.keyedHashDetail.scheme.scheme = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
165 Buffer += sizeof (UINT16);
166 switch (OutPublic->publicArea.parameters.keyedHashDetail.scheme.scheme) {
167 case TPM_ALG_HMAC:
168 OutPublic->publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
169 Buffer += sizeof (UINT16);
170 break;
171 case TPM_ALG_XOR:
172 OutPublic->publicArea.parameters.keyedHashDetail.scheme.details.xor.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
173 Buffer += sizeof (UINT16);
174 OutPublic->publicArea.parameters.keyedHashDetail.scheme.details.xor.kdf = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
175 Buffer += sizeof (UINT16);
176 break;
177 default:
178 return EFI_UNSUPPORTED;
179 }
180
181 break;
182 case TPM_ALG_SYMCIPHER:
183 OutPublic->publicArea.parameters.symDetail.algorithm = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
184 Buffer += sizeof (UINT16);
185 switch (OutPublic->publicArea.parameters.symDetail.algorithm) {
186 case TPM_ALG_AES:
187 OutPublic->publicArea.parameters.symDetail.keyBits.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
188 Buffer += sizeof (UINT16);
189 OutPublic->publicArea.parameters.symDetail.mode.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
190 Buffer += sizeof (UINT16);
191 break;
192 case TPM_ALG_SM4:
193 OutPublic->publicArea.parameters.symDetail.keyBits.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
194 Buffer += sizeof (UINT16);
195 OutPublic->publicArea.parameters.symDetail.mode.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
196 Buffer += sizeof (UINT16);
197 break;
198 case TPM_ALG_XOR:
199 OutPublic->publicArea.parameters.symDetail.keyBits.xor = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
200 Buffer += sizeof (UINT16);
201 break;
202 case TPM_ALG_NULL:
203 break;
204 default:
205 return EFI_UNSUPPORTED;
206 }
207
208 break;
209 case TPM_ALG_RSA:
210 OutPublic->publicArea.parameters.rsaDetail.symmetric.algorithm = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
211 Buffer += sizeof (UINT16);
212 switch (OutPublic->publicArea.parameters.rsaDetail.symmetric.algorithm) {
213 case TPM_ALG_AES:
214 OutPublic->publicArea.parameters.rsaDetail.symmetric.keyBits.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
215 Buffer += sizeof (UINT16);
216 OutPublic->publicArea.parameters.rsaDetail.symmetric.mode.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
217 Buffer += sizeof (UINT16);
218 break;
219 case TPM_ALG_SM4:
220 OutPublic->publicArea.parameters.rsaDetail.symmetric.keyBits.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
221 Buffer += sizeof (UINT16);
222 OutPublic->publicArea.parameters.rsaDetail.symmetric.mode.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
223 Buffer += sizeof (UINT16);
224 break;
225 case TPM_ALG_NULL:
226 break;
227 default:
228 return EFI_UNSUPPORTED;
229 }
230
231 OutPublic->publicArea.parameters.rsaDetail.scheme.scheme = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
232 Buffer += sizeof (UINT16);
233 switch (OutPublic->publicArea.parameters.rsaDetail.scheme.scheme) {
234 case TPM_ALG_RSASSA:
235 OutPublic->publicArea.parameters.rsaDetail.scheme.details.rsassa.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
236 Buffer += sizeof (UINT16);
237 break;
238 case TPM_ALG_RSAPSS:
239 OutPublic->publicArea.parameters.rsaDetail.scheme.details.rsapss.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
240 Buffer += sizeof (UINT16);
241 break;
242 case TPM_ALG_RSAES:
243 break;
244 case TPM_ALG_OAEP:
245 OutPublic->publicArea.parameters.rsaDetail.scheme.details.oaep.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
246 Buffer += sizeof (UINT16);
247 break;
248 case TPM_ALG_NULL:
249 break;
250 default:
251 return EFI_UNSUPPORTED;
252 }
253
254 OutPublic->publicArea.parameters.rsaDetail.keyBits = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
255 Buffer += sizeof (UINT16);
256 OutPublic->publicArea.parameters.rsaDetail.exponent = SwapBytes32 (ReadUnaligned32 ((UINT32 *)Buffer));
257 Buffer += sizeof (UINT32);
258 break;
259 case TPM_ALG_ECC:
260 OutPublic->publicArea.parameters.eccDetail.symmetric.algorithm = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
261 Buffer += sizeof (UINT16);
262 switch (OutPublic->publicArea.parameters.eccDetail.symmetric.algorithm) {
263 case TPM_ALG_AES:
264 OutPublic->publicArea.parameters.eccDetail.symmetric.keyBits.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
265 Buffer += sizeof (UINT16);
266 OutPublic->publicArea.parameters.eccDetail.symmetric.mode.aes = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
267 Buffer += sizeof (UINT16);
268 break;
269 case TPM_ALG_SM4:
270 OutPublic->publicArea.parameters.eccDetail.symmetric.keyBits.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
271 Buffer += sizeof (UINT16);
272 OutPublic->publicArea.parameters.eccDetail.symmetric.mode.SM4 = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
273 Buffer += sizeof (UINT16);
274 break;
275 case TPM_ALG_NULL:
276 break;
277 default:
278 return EFI_UNSUPPORTED;
279 }
280
281 OutPublic->publicArea.parameters.eccDetail.scheme.scheme = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
282 Buffer += sizeof (UINT16);
283 switch (OutPublic->publicArea.parameters.eccDetail.scheme.scheme) {
284 case TPM_ALG_ECDSA:
285 OutPublic->publicArea.parameters.eccDetail.scheme.details.ecdsa.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
286 Buffer += sizeof (UINT16);
287 break;
288 case TPM_ALG_ECDAA:
289 OutPublic->publicArea.parameters.eccDetail.scheme.details.ecdaa.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
290 Buffer += sizeof (UINT16);
291 break;
292 case TPM_ALG_ECSCHNORR:
293 OutPublic->publicArea.parameters.eccDetail.scheme.details.ecSchnorr.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
294 Buffer += sizeof (UINT16);
295 break;
296 case TPM_ALG_ECDH:
297 break;
298 case TPM_ALG_NULL:
299 break;
300 default:
301 return EFI_UNSUPPORTED;
302 }
303
304 OutPublic->publicArea.parameters.eccDetail.curveID = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
305 Buffer += sizeof (UINT16);
306 OutPublic->publicArea.parameters.eccDetail.kdf.scheme = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
307 Buffer += sizeof (UINT16);
308 switch (OutPublic->publicArea.parameters.eccDetail.kdf.scheme) {
309 case TPM_ALG_MGF1:
310 OutPublic->publicArea.parameters.eccDetail.kdf.details.mgf1.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
311 Buffer += sizeof (UINT16);
312 break;
313 case TPM_ALG_KDF1_SP800_108:
314 OutPublic->publicArea.parameters.eccDetail.kdf.details.kdf1_sp800_108.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
315 Buffer += sizeof (UINT16);
316 break;
317 case TPM_ALG_KDF1_SP800_56a:
318 OutPublic->publicArea.parameters.eccDetail.kdf.details.kdf1_SP800_56a.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
319 Buffer += sizeof (UINT16);
320 break;
321 case TPM_ALG_KDF2:
322 OutPublic->publicArea.parameters.eccDetail.kdf.details.kdf2.hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
323 Buffer += sizeof (UINT16);
324 break;
325 case TPM_ALG_NULL:
326 break;
327 default:
328 return EFI_UNSUPPORTED;
329 }
330
331 break;
332 default:
333 return EFI_UNSUPPORTED;
334 }
335
336 // TPMU_PUBLIC_ID
337 switch (OutPublic->publicArea.type) {
338 case TPM_ALG_KEYEDHASH:
339 OutPublic->publicArea.unique.keyedHash.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
340 Buffer += sizeof (UINT16);
341 if (OutPublic->publicArea.unique.keyedHash.size > sizeof (TPMU_HA)) {
342 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - keyedHash.size error %x\n", OutPublic->publicArea.unique.keyedHash.size));
343 return EFI_DEVICE_ERROR;
344 }
345
346 CopyMem (OutPublic->publicArea.unique.keyedHash.buffer, Buffer, OutPublic->publicArea.unique.keyedHash.size);
347 Buffer += OutPublic->publicArea.unique.keyedHash.size;
348 break;
349 case TPM_ALG_SYMCIPHER:
350 OutPublic->publicArea.unique.sym.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
351 Buffer += sizeof (UINT16);
352 if (OutPublic->publicArea.unique.sym.size > sizeof (TPMU_HA)) {
353 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - sym.size error %x\n", OutPublic->publicArea.unique.sym.size));
354 return EFI_DEVICE_ERROR;
355 }
356
357 CopyMem (OutPublic->publicArea.unique.sym.buffer, Buffer, OutPublic->publicArea.unique.sym.size);
358 Buffer += OutPublic->publicArea.unique.sym.size;
359 break;
360 case TPM_ALG_RSA:
361 OutPublic->publicArea.unique.rsa.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
362 Buffer += sizeof (UINT16);
363 if (OutPublic->publicArea.unique.rsa.size > MAX_RSA_KEY_BYTES) {
364 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - rsa.size error %x\n", OutPublic->publicArea.unique.rsa.size));
365 return EFI_DEVICE_ERROR;
366 }
367
368 CopyMem (OutPublic->publicArea.unique.rsa.buffer, Buffer, OutPublic->publicArea.unique.rsa.size);
369 Buffer += OutPublic->publicArea.unique.rsa.size;
370 break;
371 case TPM_ALG_ECC:
372 OutPublic->publicArea.unique.ecc.x.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
373 Buffer += sizeof (UINT16);
374 if (OutPublic->publicArea.unique.ecc.x.size > MAX_ECC_KEY_BYTES) {
375 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - ecc.x.size error %x\n", OutPublic->publicArea.unique.ecc.x.size));
376 return EFI_DEVICE_ERROR;
377 }
378
379 CopyMem (OutPublic->publicArea.unique.ecc.x.buffer, Buffer, OutPublic->publicArea.unique.ecc.x.size);
380 Buffer += OutPublic->publicArea.unique.ecc.x.size;
381 OutPublic->publicArea.unique.ecc.y.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));
382 Buffer += sizeof (UINT16);
383 if (OutPublic->publicArea.unique.ecc.y.size > MAX_ECC_KEY_BYTES) {
384 DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - ecc.y.size error %x\n", OutPublic->publicArea.unique.ecc.y.size));
385 return EFI_DEVICE_ERROR;
386 }
387
388 CopyMem (OutPublic->publicArea.unique.ecc.y.buffer, Buffer, OutPublic->publicArea.unique.ecc.y.size);
389 Buffer += OutPublic->publicArea.unique.ecc.y.size;
390 break;
391 default:
392 return EFI_UNSUPPORTED;
393 }
394
395 CopyMem (Name->name, (UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + OutPublicSize + sizeof (UINT16), NameSize);
396 Name->size = NameSize;
397
398 CopyMem (QualifiedName->name, (UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof (UINT16) + OutPublicSize + sizeof (UINT16) + NameSize + sizeof (UINT16), QualifiedNameSize);
399 QualifiedName->size = QualifiedNameSize;
400
401 return EFI_SUCCESS;
402}
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
Definition: SwapBytes16.c:25
UINT16 EFIAPI ReadUnaligned16(IN CONST UINT16 *Buffer)
Definition: Unaligned.c:29
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
UINT32 EFIAPI WriteUnaligned32(OUT UINT32 *Buffer, IN UINT32 Value)
Definition: Unaligned.c:177
UINT32 EFIAPI ReadUnaligned32(IN CONST UINT32 *Buffer)
Definition: Unaligned.c:145
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define TPM_ALG_RSA
The RSA algorithm.
Definition: Tpm12.h:362
#define TPM_ALG_XOR
XOR using the rolling nonces.
Definition: Tpm12.h:371
#define TPM_ALG_MGF1
The XOR algorithm using MGF1 to create a string the size of the encrypted block.
Definition: Tpm12.h:368
#define TPM_ALG_HMAC
The RFC 2104 HMAC algorithm.
Definition: Tpm12.h:366
EFI_STATUS EFIAPI Tpm2SubmitCommand(IN UINT32 InputParameterBlockSize, IN UINT8 *InputParameterBlock, IN OUT UINT32 *OutputParameterBlockSize, IN UINT8 *OutputParameterBlock)
EFI_STATUS EFIAPI Tpm2ReadPublic(IN TPMI_DH_OBJECT ObjectHandle, OUT TPM2B_PUBLIC *OutPublic, OUT TPM2B_NAME *Name, OUT TPM2B_NAME *QualifiedName)
Definition: Tpm2Object.c:45
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Tpm20.h:905