]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/TcgStorageCoreLib/TcgStorageUtil.c
48f1e6e7bd090ae1c2c91dca9f5253eae5902049
[mirror_edk2.git] / SecurityPkg / Library / TcgStorageCoreLib / TcgStorageUtil.c
1 /** @file
2 Provide functions to provide tcg storage core spec related functions.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Library/TcgStorageCoreLib.h>
10
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14
15 typedef struct {
16 UINT16 FeatureCode;
17 TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feature;
18 UINTN FeatureSize;
19 } TCG_FIND_FEATURE_CTX;
20
21 /**
22 Returns a human-readable string representing a method status return code.
23
24 @param[in] MethodStatus Method status to translate to a string
25
26
27 @retval return the string info.
28 **/
29 CHAR8 *
30 EFIAPI
31 TcgMethodStatusString (
32 UINT8 MethodStatus
33 )
34 {
35 switch (MethodStatus) {
36 #define C(status) case TCG_METHOD_STATUS_CODE_ ## status: return #status
37 C (SUCCESS);
38 C (NOT_AUTHORIZED);
39 C (OBSOLETE);
40 C (SP_BUSY);
41 C (SP_FAILED);
42 C (SP_DISABLED);
43 C (SP_FROZEN);
44 C (NO_SESSIONS_AVAILABLE);
45 C (UNIQUENESS_CONFLICT);
46 C (INSUFFICIENT_SPACE);
47 C (INSUFFICIENT_ROWS);
48 C (INVALID_PARAMETER);
49 C (OBSOLETE2);
50 C (OBSOLETE3);
51 C (TPER_MALFUNCTION);
52 C (TRANSACTION_FAILURE);
53 C (RESPONSE_OVERFLOW);
54 C (AUTHORITY_LOCKED_OUT);
55 C (FAIL);
56 #undef C
57 }
58
59 return "unknown";
60 }
61
62 /**
63 adds call token and method Header (invoking id, and method id).
64
65 @param CreateStruct The input create structure.
66 @param InvokingId Invoking id.
67 @param MethodId Method id.
68
69 **/
70 TCG_RESULT
71 EFIAPI
72 TcgStartMethodCall (
73 TCG_CREATE_STRUCT *CreateStruct,
74 TCG_UID InvokingId,
75 TCG_UID MethodId
76 )
77 {
78 NULL_CHECK (CreateStruct);
79
80 if ((CreateStruct->ComPacket == NULL) ||
81 (CreateStruct->CurPacket == NULL) ||
82 (CreateStruct->CurSubPacket == NULL)
83 )
84 {
85 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
86 return (TcgResultFailureInvalidAction);
87 }
88
89 ERROR_CHECK (TcgAddCall (CreateStruct));
90 ERROR_CHECK (TcgAddTcgUid (CreateStruct, InvokingId));
91 ERROR_CHECK (TcgAddTcgUid (CreateStruct, MethodId));
92
93 return TcgResultSuccess;
94 }
95
96 /**
97 Adds START LIST token.
98
99 @param CreateStruct The input create structure.
100
101 **/
102 TCG_RESULT
103 EFIAPI
104 TcgStartParameters (
105 TCG_CREATE_STRUCT *CreateStruct
106 )
107 {
108 NULL_CHECK (CreateStruct);
109
110 if ((CreateStruct->ComPacket == NULL) ||
111 (CreateStruct->CurPacket == NULL) ||
112 (CreateStruct->CurSubPacket == NULL)
113 )
114 {
115 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
116 return (TcgResultFailureInvalidAction);
117 }
118
119 return TcgAddStartList (CreateStruct);
120 }
121
122 /**
123 Adds END LIST token.
124
125 @param CreateStruct The input create structure.
126
127 **/
128 TCG_RESULT
129 EFIAPI
130 TcgEndParameters (
131 TCG_CREATE_STRUCT *CreateStruct
132 )
133 {
134 NULL_CHECK (CreateStruct);
135
136 if ((CreateStruct->ComPacket == NULL) ||
137 (CreateStruct->CurPacket == NULL) ||
138 (CreateStruct->CurSubPacket == NULL)
139 )
140 {
141 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
142 return (TcgResultFailureInvalidAction);
143 }
144
145 return TcgAddEndList (CreateStruct);
146 }
147
148 /**
149 Adds END Data token and method list.
150
151 @param CreateStruct The input create structure.
152
153 **/
154 TCG_RESULT
155 EFIAPI
156 TcgEndMethodCall (
157 TCG_CREATE_STRUCT *CreateStruct
158 )
159 {
160 NULL_CHECK (CreateStruct);
161
162 if ((CreateStruct->ComPacket == NULL) ||
163 (CreateStruct->CurPacket == NULL) ||
164 (CreateStruct->CurSubPacket == NULL)
165 )
166 {
167 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", CreateStruct->ComPacket, CreateStruct->CurPacket, CreateStruct->CurSubPacket));
168 return (TcgResultFailureInvalidAction);
169 }
170
171 ERROR_CHECK (TcgAddEndOfData (CreateStruct));
172
173 ERROR_CHECK (TcgAddStartList (CreateStruct));
174 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // expected to complete properly
175 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // reserved
176 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // reserved
177 ERROR_CHECK (TcgAddEndList (CreateStruct));
178
179 return TcgResultSuccess;
180 }
181
182 /**
183 Retrieves the comID and Extended comID of the ComPacket in the Tcg response.
184 It is intended to be used to confirm the received Tcg response is intended for user that received it.
185
186 @param [in] ParseStruct Structure used to parse received TCG response.
187 @param [in/out] ComId comID retrieved from received ComPacket.
188 @param [in/out] ComIdExtension Extended comID retrieved from received ComPacket
189
190 **/
191 TCG_RESULT
192 EFIAPI
193 TcgGetComIds (
194 const TCG_PARSE_STRUCT *ParseStruct,
195 UINT16 *ComId,
196 UINT16 *ComIdExtension
197 )
198 {
199 NULL_CHECK (ParseStruct);
200 NULL_CHECK (ComId);
201 NULL_CHECK (ComIdExtension);
202
203 if (ParseStruct->ComPacket == NULL) {
204 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p\n", ParseStruct->ComPacket));
205 return TcgResultFailureInvalidAction;
206 }
207
208 *ComId = SwapBytes16 (ParseStruct->ComPacket->ComIDBE);
209 *ComIdExtension = SwapBytes16 (ParseStruct->ComPacket->ComIDExtensionBE);
210
211 return TcgResultSuccess;
212 }
213
214 /**
215 Checks if the ComIDs of the response match the expected values.
216
217 @param[in] ParseStruct Structure used to parse received TCG response
218 @param[in] ExpectedComId Expected comID
219 @param[in] ExpectedComIdExtension Expected extended comID
220
221 **/
222 TCG_RESULT
223 EFIAPI
224 TcgCheckComIds (
225 const TCG_PARSE_STRUCT *ParseStruct,
226 UINT16 ExpectedComId,
227 UINT16 ExpectedComIdExtension
228 )
229 {
230 UINT16 ParseComId;
231 UINT16 ParseComIdExtension;
232
233 ERROR_CHECK (TcgGetComIds (ParseStruct, &ParseComId, &ParseComIdExtension));
234 if ((ParseComId != ExpectedComId) || (ParseComIdExtension != ExpectedComIdExtension)) {
235 DEBUG ((DEBUG_INFO, "Com ID: Actual 0x%02X Expected 0x%02X\n", ParseComId, ExpectedComId));
236 DEBUG ((DEBUG_INFO, "Extended Com ID: 0x%02X Expected 0x%02X\n", ParseComIdExtension, ExpectedComIdExtension));
237 return TcgResultFailure;
238 }
239
240 return TcgResultSuccess;
241 }
242
243 /**
244 Returns the method status of the current subpacket. Does not affect the current position
245 in the ComPacket. In other words, it can be called whenever you have a valid SubPacket.
246
247 @param [in/out] ParseStruct Structure used to parse received TCG response
248 @param [in/out] MethodStatus Method status retrieved of the current SubPacket
249
250 **/
251 TCG_RESULT
252 EFIAPI
253 TcgGetMethodStatus (
254 const TCG_PARSE_STRUCT *ParseStruct,
255 UINT8 *MethodStatus
256 )
257 {
258 TCG_PARSE_STRUCT TmpParseStruct;
259 TCG_TOKEN TcgToken;
260 UINT8 Reserved1, Reserved2;
261
262 NULL_CHECK (ParseStruct);
263 NULL_CHECK (MethodStatus);
264
265 if ((ParseStruct->ComPacket == NULL) ||
266 (ParseStruct->CurPacket == NULL) ||
267 (ParseStruct->CurSubPacket == NULL)
268 )
269 {
270 DEBUG ((DEBUG_INFO, "unexpected state: ComPacket=%p CurPacket=%p CurSubPacket=%p\n", ParseStruct->ComPacket, ParseStruct->CurPacket, ParseStruct->CurSubPacket));
271 return TcgResultFailureInvalidAction;
272 }
273
274 // duplicate ParseStruct, then don't need to "reset" location cur ptr
275 CopyMem (&TmpParseStruct, ParseStruct, sizeof (TCG_PARSE_STRUCT));
276
277 // method status list exists after the end method call in the subpacket
278 // skip tokens until ENDDATA is found
279 do {
280 ERROR_CHECK (TcgGetNextToken (&TmpParseStruct, &TcgToken));
281 } while (TcgToken.Type != TcgTokenTypeEndOfData);
282
283 // only reach here if enddata is found
284 // at this point, the curptr is pointing at method status list beginning
285 ERROR_CHECK (TcgGetNextStartList (&TmpParseStruct));
286 ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, MethodStatus));
287 ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, &Reserved1));
288 ERROR_CHECK (TcgGetNextUINT8 (&TmpParseStruct, &Reserved2));
289 ERROR_CHECK (TcgGetNextEndList (&TmpParseStruct));
290
291 if (Reserved1 != 0) {
292 DEBUG ((DEBUG_INFO, "Method status reserved1 = 0x%02X (expected 0)\n", Reserved1));
293 return TcgResultFailure;
294 }
295
296 if (Reserved2 != 0) {
297 DEBUG ((DEBUG_INFO, "Method status reserved2 = 0x%02X (expected 0)\n", Reserved1));
298 return TcgResultFailure;
299 }
300
301 return TcgResultSuccess;
302 }
303
304 /**
305 Return the toke type string info.
306
307 @param Type Input the type info.
308
309 @retval Return the string for this type.
310
311 **/
312 CHAR8 *
313 EFIAPI
314 TcgTokenTypeString (
315 TCG_TOKEN_TYPE Type
316 )
317 {
318 switch (Type) {
319 case TcgTokenTypeReserved: return "Reserved";
320 case TcgTokenTypeTinyAtom: return "Tiny Atom";
321 case TcgTokenTypeShortAtom: return "Short Atom";
322 case TcgTokenTypeMediumAtom: return "Medium Atom";
323 case TcgTokenTypeLongAtom: return "Long Atom";
324 case TcgTokenTypeStartList: return "Start List";
325 case TcgTokenTypeEndList: return "End List";
326 case TcgTokenTypeStartName: return "Start Name";
327 case TcgTokenTypeEndName: return "End Name";
328 case TcgTokenTypeCall: return "Call";
329 case TcgTokenTypeEndOfData: return "End of Data";
330 case TcgTokenTypeEndOfSession: return "End of Session";
331 case TcgTokenTypeStartTransaction: return "Start Transaction";
332 case TcgTokenTypeEndTransaction: return "End Transaction";
333 case TcgTokenTypeEmptyAtom: return "Empty atom";
334 }
335
336 return "Unknown";
337 }
338
339 /**
340
341 Adds Start Session call to the data structure. This creates the entire ComPacket structure and
342 returns the size of the entire compacket in the size parameter.
343
344 @param [in/out] CreateStruct Structure used to add the start session call
345 @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
346 @param [in] ComId ComID for the ComPacket
347 @param [in] ComIdExtension Extended ComID for the ComPacket
348 @param [in] HostSessionId Host Session ID
349 @param [in] SpId Security Provider to start session with
350 @param [in] Write Write option for start session. TRUE = start session requests write access
351 @param [in] HostChallengeLength Length of the host challenge. Length should be 0 if hostChallenge is NULL
352 @param [in] HostChallenge Host challenge for Host Signing Authority. If NULL, then no Host Challenge shall be sent.
353 @param [in] HostSigningAuthority Host Signing Authority used for start session. If NULL, then no Host Signing Authority shall be sent.
354
355 **/
356 TCG_RESULT
357 EFIAPI
358 TcgCreateStartSession (
359 TCG_CREATE_STRUCT *CreateStruct,
360 UINT32 *Size,
361 UINT16 ComId,
362 UINT16 ComIdExtension,
363 UINT32 HostSessionId,
364 TCG_UID SpId,
365 BOOLEAN Write,
366 UINT32 HostChallengeLength,
367 const VOID *HostChallenge,
368 TCG_UID HostSigningAuthority
369 )
370 {
371 ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
372 ERROR_CHECK (TcgStartPacket (CreateStruct, 0x0, 0x0, 0x0, 0x0, 0x0));
373 ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
374 ERROR_CHECK (TcgStartMethodCall (CreateStruct, TCG_UID_SMUID, TCG_UID_SM_START_SESSION));
375 ERROR_CHECK (TcgStartParameters (CreateStruct));
376 ERROR_CHECK (TcgAddUINT32 (CreateStruct, HostSessionId));
377 ERROR_CHECK (TcgAddTcgUid (CreateStruct, SpId));
378 ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, Write));
379
380 // optional parameters
381 if ((HostChallenge != NULL) && (HostChallengeLength != 0)) {
382 ERROR_CHECK (TcgAddStartName (CreateStruct));
383 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x00)); // TODO Create Enum for Method Optional Parameters?
384 ERROR_CHECK (TcgAddByteSequence (CreateStruct, HostChallenge, HostChallengeLength, FALSE));
385 ERROR_CHECK (TcgAddEndName (CreateStruct));
386 }
387
388 // optional parameters
389 if (HostSigningAuthority != 0) {
390 ERROR_CHECK (TcgAddStartName (CreateStruct));
391 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x03)); // TODO Create Enum for Method Optional Parameters?
392 ERROR_CHECK (TcgAddTcgUid (CreateStruct, HostSigningAuthority));
393 ERROR_CHECK (TcgAddEndName (CreateStruct));
394 }
395
396 ERROR_CHECK (TcgEndParameters (CreateStruct));
397 ERROR_CHECK (TcgEndMethodCall (CreateStruct));
398 ERROR_CHECK (TcgEndSubPacket (CreateStruct));
399 ERROR_CHECK (TcgEndPacket (CreateStruct));
400 ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
401
402 return TcgResultSuccess;
403 }
404
405 /**
406 Parses the Sync Session response contained in the parseStruct to retrieve Tper session ID. If the Sync Session response
407 parameters do not match the comID, extended ComID and host session ID then a failure is returned.
408
409 @param[in/out] ParseStruct Structure used to parse received TCG response, contains Sync Session response.
410 @param[in] ComId Expected ComID that is compared to actual ComID of response
411 @param[in] ComIdExtension Expected Extended ComID that is compared to actual Extended ComID of response
412 @param[in] HostSessionId Expected Host Session ID that is compared to actual Host Session ID of response
413 @param[in/out] TperSessionId Tper Session ID retrieved from the Sync Session response.
414
415 **/
416 TCG_RESULT
417 EFIAPI
418 TcgParseSyncSession (
419 const TCG_PARSE_STRUCT *ParseStruct,
420 UINT16 ComId,
421 UINT16 ComIdExtension,
422 UINT32 HostSessionId,
423 UINT32 *TperSessionId
424 )
425 {
426 UINT8 MethodStatus;
427 TCG_PARSE_STRUCT TmpParseStruct;
428 UINT16 ParseComId;
429 UINT16 ParseExtComId;
430 TCG_UID InvokingUID;
431 TCG_UID MethodUID;
432 UINT32 RecvHostSessionId;
433
434 NULL_CHECK (ParseStruct);
435 NULL_CHECK (TperSessionId);
436
437 CopyMem (&TmpParseStruct, ParseStruct, sizeof (TCG_PARSE_STRUCT));
438
439 // verify method status is good
440 ERROR_CHECK (TcgGetMethodStatus (&TmpParseStruct, &MethodStatus));
441 METHOD_STATUS_ERROR_CHECK (MethodStatus, TcgResultFailure);
442
443 // verify comids
444 ERROR_CHECK (TcgGetComIds (&TmpParseStruct, &ParseComId, &ParseExtComId));
445
446 if ((ComId != ParseComId) || (ComIdExtension != ParseExtComId)) {
447 DEBUG ((DEBUG_INFO, "unmatched comid (exp: 0x%X recv: 0x%X) or comid extension (exp: 0x%X recv: 0x%X)\n", ComId, ParseComId, ComIdExtension, ParseExtComId));
448 return TcgResultFailure;
449 }
450
451 ERROR_CHECK (TcgGetNextCall (&TmpParseStruct));
452 ERROR_CHECK (TcgGetNextTcgUid (&TmpParseStruct, &InvokingUID));
453 ERROR_CHECK (TcgGetNextTcgUid (&TmpParseStruct, &MethodUID));
454 ERROR_CHECK (TcgGetNextStartList (&TmpParseStruct));
455 ERROR_CHECK (TcgGetNextUINT32 (&TmpParseStruct, &RecvHostSessionId));
456 ERROR_CHECK (TcgGetNextUINT32 (&TmpParseStruct, TperSessionId));
457 ERROR_CHECK (TcgGetNextEndList (&TmpParseStruct));
458 ERROR_CHECK (TcgGetNextEndOfData (&TmpParseStruct));
459
460 if (InvokingUID != TCG_UID_SMUID) {
461 DEBUG ((DEBUG_INFO, "Invoking UID did not match UID_SMUID\n"));
462 return TcgResultFailure;
463 }
464
465 if (MethodUID != TCG_UID_SM_SYNC_SESSION) {
466 DEBUG ((DEBUG_INFO, "Method UID did not match UID_SM_SYNC_SESSION\n"));
467 return TcgResultFailure;
468 }
469
470 if (HostSessionId != RecvHostSessionId) {
471 DEBUG ((DEBUG_INFO, "unmatched HostSessionId (exp: 0x%X recv: 0x%X)\n", HostSessionId, RecvHostSessionId));
472 return TcgResultFailure;
473 }
474
475 return TcgResultSuccess;
476 }
477
478 /**
479
480 Creates ComPacket with EndSession.
481 This assumes a start session has already been opened.
482
483 @param [in/out] CreateStruct Structure used to add Endsession
484 @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
485 @param [in] ComId ComID for the ComPacket
486 @param [in] ComIdExtension Extended ComID for the ComPacket
487 @param [in] HostSessionId Host Session ID for the Packet
488 @param [in] TpSessionId Tper Session ID for the Packet
489
490 **/
491 TCG_RESULT
492 EFIAPI
493 TcgCreateEndSession (
494 TCG_CREATE_STRUCT *CreateStruct,
495 UINT32 *Size,
496 UINT16 ComId,
497 UINT16 ComIdExtension,
498 UINT32 HostSessionId,
499 UINT32 TpSessionId
500 )
501 {
502 ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
503 ERROR_CHECK (TcgStartPacket (CreateStruct, TpSessionId, HostSessionId, 0x0, 0x0, 0x0));
504 ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
505 ERROR_CHECK (TcgAddEndOfSession (CreateStruct));
506 ERROR_CHECK (TcgEndSubPacket (CreateStruct));
507 ERROR_CHECK (TcgEndPacket (CreateStruct));
508 ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
509
510 return TcgResultSuccess;
511 }
512
513 /**
514 Set start method.
515
516 @param CreateStruct Input create structure.
517 @param Row Input the row info.
518 @param ColumnNumber the column info.
519
520 **/
521 TCG_RESULT
522 EFIAPI
523 TcgStartMethodSet (
524 TCG_CREATE_STRUCT *CreateStruct,
525 TCG_UID Row,
526 UINT32 ColumnNumber
527 )
528 {
529 ERROR_CHECK (TcgStartMethodCall (CreateStruct, Row, TCG_UID_METHOD_SET));
530 ERROR_CHECK (TcgStartParameters (CreateStruct));
531 ERROR_CHECK (TcgAddStartName (CreateStruct));
532 ERROR_CHECK (TcgAddUINT8 (CreateStruct, 0x01)); // "Values"
533 ERROR_CHECK (TcgAddStartList (CreateStruct));
534 ERROR_CHECK (TcgAddStartName (CreateStruct));
535 ERROR_CHECK (TcgAddUINT32 (CreateStruct, ColumnNumber));
536 return TcgResultSuccess;
537 }
538
539 /**
540 Set end method.
541
542 @param CreateStruct Input create structure.
543
544 **/
545 TCG_RESULT
546 EFIAPI
547 TcgEndMethodSet (
548 TCG_CREATE_STRUCT *CreateStruct
549 )
550 {
551 ERROR_CHECK (TcgAddEndName (CreateStruct));
552 ERROR_CHECK (TcgAddEndList (CreateStruct));
553 ERROR_CHECK (TcgAddEndName (CreateStruct));
554 ERROR_CHECK (TcgEndParameters (CreateStruct));
555 ERROR_CHECK (TcgEndMethodCall (CreateStruct));
556 return TcgResultSuccess;
557 }
558
559 /**
560 Creates ComPacket with a Method call that sets the PIN column for the row specified.
561 This assumes a start session has already been opened with the desired SP.
562
563 @param [in/out] CreateStruct Structure used to add method call.
564 @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
565 @param [in] ComId ComID for the ComPacket
566 @param [in] ComIdExtension Extended ComID for the ComPacket
567 @param [in] TperSession Tper Session ID for the Packet
568 @param [in] HostSession Host Session ID for the Packet
569 @param [in] SidRow UID of row of current SP to set PIN column
570 @param [in] Password value of PIN to set
571 @param [in] PasswordSize Size of PIN
572
573 **/
574 TCG_RESULT
575 EFIAPI
576 TcgCreateSetCPin (
577 TCG_CREATE_STRUCT *CreateStruct,
578 UINT32 *Size,
579 UINT16 ComId,
580 UINT16 ComIdExtension,
581 UINT32 TperSession,
582 UINT32 HostSession,
583 TCG_UID SidRow,
584 const VOID *Password,
585 UINT32 PasswordSize
586 )
587 {
588 // set new SID Password
589 ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
590 ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
591 ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
592 ERROR_CHECK (TcgStartMethodSet (CreateStruct, SidRow, 0x03)); // "PIN"
593 ERROR_CHECK (TcgAddByteSequence (CreateStruct, Password, PasswordSize, FALSE));
594 ERROR_CHECK (TcgEndMethodSet (CreateStruct));
595 ERROR_CHECK (TcgEndSubPacket (CreateStruct));
596 ERROR_CHECK (TcgEndPacket (CreateStruct));
597 ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
598 return TcgResultSuccess;
599 }
600
601 /**
602 Creates ComPacket with a Method call that sets the "Enabled" column for the row specified using the value specified.
603 This assumes a start session has already been opened with the desired SP.
604
605 @param [in/out] CreateStruct Structure used to add method call
606 @param [in/out] Size Describes the size of the entire ComPacket (header and payload). Filled out by function.
607 @param [in] ComId ComID for the ComPacket
608 @param [in] ComIdExtension Extended ComID for the ComPacket
609 @param [in] TperSession Tper Session ID for the Packet
610 @param [in] HostSession Host Session ID for the Packet
611 @param [in] AuthorityUid Authority UID to modify the "Enabled" column for
612 @param [in] Enabled Value to set the "Enabled" column to
613
614 **/
615 TCG_RESULT
616 EFIAPI
617 TcgSetAuthorityEnabled (
618 TCG_CREATE_STRUCT *CreateStruct,
619 UINT32 *Size,
620 UINT16 ComId,
621 UINT16 ComIdExtension,
622 UINT32 TperSession,
623 UINT32 HostSession,
624 TCG_UID AuthorityUid,
625 BOOLEAN Enabled
626 )
627 {
628 ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
629 ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
630 ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
631 ERROR_CHECK (TcgStartMethodSet (CreateStruct, AuthorityUid, 0x05)); // "Enabled"
632 ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, Enabled));
633 ERROR_CHECK (TcgEndMethodSet (CreateStruct));
634 ERROR_CHECK (TcgEndSubPacket (CreateStruct));
635 ERROR_CHECK (TcgEndPacket (CreateStruct));
636 ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
637 return TcgResultSuccess;
638 }
639
640 /**
641 Create set ace.
642
643 @param CreateStruct Input create structure.
644 @param Size size info.
645 @param ComId ComId info.
646 @param ComIdExtension ComId extension info.
647 @param TperSession Tper session data.
648 @param HostSession Host session data.
649 @param AceRow Ace row info.
650 @param Authority1 Authority 1 info.
651 @param LogicalOperator Logical operator info.
652 @param Authority2 Authority 2 info.
653
654 @retval Return the action result.
655
656 **/
657 TCG_RESULT
658 EFIAPI
659 TcgCreateSetAce (
660 TCG_CREATE_STRUCT *CreateStruct,
661 UINT32 *Size,
662 UINT16 ComId,
663 UINT16 ComIdExtension,
664 UINT32 TperSession,
665 UINT32 HostSession,
666 TCG_UID AceRow,
667 TCG_UID Authority1,
668 BOOLEAN LogicalOperator,
669 TCG_UID Authority2
670 )
671 {
672 UINT8 HalfUidAuthorityObjectRef[4];
673 UINT8 HalfUidBooleanAce[4];
674
675 HalfUidAuthorityObjectRef[0] = 0x0;
676 HalfUidAuthorityObjectRef[1] = 0x0;
677 HalfUidAuthorityObjectRef[2] = 0xC;
678 HalfUidAuthorityObjectRef[3] = 0x5;
679
680 HalfUidBooleanAce[0] = 0x0;
681 HalfUidBooleanAce[1] = 0x0;
682 HalfUidBooleanAce[2] = 0x4;
683 HalfUidBooleanAce[3] = 0xE;
684
685 ERROR_CHECK (TcgStartComPacket (CreateStruct, ComId, ComIdExtension));
686 ERROR_CHECK (TcgStartPacket (CreateStruct, TperSession, HostSession, 0x0, 0x0, 0x0));
687 ERROR_CHECK (TcgStartSubPacket (CreateStruct, 0x0));
688 ERROR_CHECK (TcgStartMethodSet (CreateStruct, AceRow, 0x03)); // "BooleanExpr"
689 ERROR_CHECK (TcgAddStartList (CreateStruct));
690 ERROR_CHECK (TcgAddStartName (CreateStruct));
691 ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidAuthorityObjectRef, sizeof (HalfUidAuthorityObjectRef), FALSE));
692 ERROR_CHECK (TcgAddTcgUid (CreateStruct, Authority1));
693 ERROR_CHECK (TcgAddEndName (CreateStruct));
694 ERROR_CHECK (TcgAddStartName (CreateStruct));
695 ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidAuthorityObjectRef, sizeof (HalfUidAuthorityObjectRef), FALSE));
696 ERROR_CHECK (TcgAddTcgUid (CreateStruct, Authority2));
697 ERROR_CHECK (TcgAddEndName (CreateStruct));
698
699 ERROR_CHECK (TcgAddStartName (CreateStruct));
700 ERROR_CHECK (TcgAddByteSequence (CreateStruct, HalfUidBooleanAce, sizeof (HalfUidBooleanAce), FALSE));
701 ERROR_CHECK (TcgAddBOOLEAN (CreateStruct, LogicalOperator));
702 ERROR_CHECK (TcgAddEndName (CreateStruct));
703 ERROR_CHECK (TcgAddEndList (CreateStruct));
704 ERROR_CHECK (TcgEndMethodSet (CreateStruct));
705 ERROR_CHECK (TcgEndSubPacket (CreateStruct));
706 ERROR_CHECK (TcgEndPacket (CreateStruct));
707 ERROR_CHECK (TcgEndComPacket (CreateStruct, Size));
708 return TcgResultSuccess;
709 }
710
711 /**
712 Enum level 0 discovery.
713
714 @param DiscoveryHeader Discovery header.
715 @param Callback Callback function.
716 @param Context The context for the function.
717
718 @retval return true if the callback return TRUE, else return FALSE.
719
720 **/
721 BOOLEAN
722 EFIAPI
723 TcgEnumLevel0Discovery (
724 const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
725 TCG_LEVEL0_ENUM_CALLBACK Callback,
726 VOID *Context
727 )
728 {
729 UINT32 BytesLeft;
730 const UINT8 *DiscoveryBufferPtr;
731 UINT32 FeatLength;
732 TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feat;
733
734 //
735 // Total bytes including descriptors but not including the Length field
736 //
737 BytesLeft = SwapBytes32 (DiscoveryHeader->LengthBE);
738
739 //
740 // If discovery Header is not valid, exit
741 //
742 if (BytesLeft == 0) {
743 return FALSE;
744 }
745
746 //
747 // Subtract the Length of the Header, except the Length field, which is not included
748 //
749 BytesLeft -= (sizeof (TCG_LEVEL0_DISCOVERY_HEADER) - sizeof (DiscoveryHeader->LengthBE));
750
751 //
752 // Move ptr to first descriptor
753 //
754 DiscoveryBufferPtr = (const UINT8 *)DiscoveryHeader + sizeof (TCG_LEVEL0_DISCOVERY_HEADER);
755
756 while (BytesLeft > sizeof (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER)) {
757 //
758 // Pointer to beginning of descriptor (including common Header)
759 //
760 Feat = (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *)DiscoveryBufferPtr;
761
762 FeatLength = Feat->Length + sizeof (TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER);
763
764 //
765 // Not enough bytes left for Feature descriptor
766 //
767 if (BytesLeft < FeatLength) {
768 break;
769 }
770
771 //
772 // Report the Feature to the callback
773 //
774 if (Callback (DiscoveryHeader, Feat, FeatLength, Context)) {
775 return TRUE;
776 }
777
778 //
779 // Descriptor Length only describes Data after common Header
780 //
781 BytesLeft -= FeatLength;
782 DiscoveryBufferPtr += FeatLength;
783 }
784
785 return FALSE;
786 }
787
788 /**
789 The callback function for Get Feature function.
790
791 @param DiscoveryHeader Input discovery header.
792 @param Feature Input Feature.
793 @param FeatureSize Input Feature size.
794 @param Context The context.
795
796 **/
797 BOOLEAN
798 EFIAPI
799 TcgFindFeatureCallback (
800 const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
801 TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *Feature,
802 UINTN FeatureSize,
803 VOID *Context
804 )
805 {
806 TCG_FIND_FEATURE_CTX *FindCtx;
807
808 FindCtx = (TCG_FIND_FEATURE_CTX *)Context;
809 if ( SwapBytes16 (Feature->FeatureCode_BE) == FindCtx->FeatureCode ) {
810 FindCtx->Feature = Feature;
811 FindCtx->FeatureSize = FeatureSize;
812 return TRUE; // done enumerating features
813 }
814
815 return FALSE; // continue enumerating
816 }
817
818 /**
819 Get Feature code from the header.
820
821 @param DiscoveryHeader The discovery header.
822 @param FeatureCode return the Feature code.
823 @param FeatureSize return the Feature size.
824
825 @retval return the Feature code data.
826 **/
827 TCG_LEVEL0_FEATURE_DESCRIPTOR_HEADER *
828 EFIAPI
829 TcgGetFeature (
830 const TCG_LEVEL0_DISCOVERY_HEADER *DiscoveryHeader,
831 UINT16 FeatureCode,
832 UINTN *FeatureSize
833 )
834 {
835 TCG_FIND_FEATURE_CTX FindCtx;
836
837 FindCtx.FeatureCode = FeatureCode;
838 FindCtx.Feature = NULL;
839 FindCtx.FeatureSize = 0;
840
841 TcgEnumLevel0Discovery (DiscoveryHeader, TcgFindFeatureCallback, &FindCtx);
842 if (FeatureSize != NULL) {
843 *FeatureSize = FindCtx.FeatureSize;
844 }
845
846 return FindCtx.Feature;
847 }
848
849 /**
850 Determines if the protocol provided is part of the provided supported protocol list.
851
852 @param[in] ProtocolList Supported protocol list to investigate
853 @param[in] Protocol Protocol value to determine if supported
854
855 @return TRUE = protocol is supported, FALSE = protocol is not supported
856 **/
857 BOOLEAN
858 EFIAPI
859 TcgIsProtocolSupported (
860 const TCG_SUPPORTED_SECURITY_PROTOCOLS *ProtocolList,
861 UINT16 Protocol
862 )
863 {
864 UINT16 Index;
865 UINT16 ListLength;
866
867 ListLength = SwapBytes16 (ProtocolList->ListLength_BE);
868
869 if (ListLength > sizeof (ProtocolList->List)) {
870 DEBUG ((DEBUG_INFO, "WARNING: list Length is larger than max allowed Value; truncating\n"));
871 ListLength = sizeof (ProtocolList->List);
872 }
873
874 for (Index = 0; Index < ListLength; Index++) {
875 if (ProtocolList->List[Index] == Protocol) {
876 return TRUE;
877 }
878 }
879
880 return FALSE;
881 }
882
883 /**
884 Check whether lock or not.
885
886 @param Discovery
887
888 @retval TRUE if lock, FALSE if not lock.
889 **/
890 BOOLEAN
891 EFIAPI
892 TcgIsLocked (
893 const TCG_LEVEL0_DISCOVERY_HEADER *Discovery
894 )
895 {
896 UINTN Size;
897 TCG_LOCKING_FEATURE_DESCRIPTOR *LockDescriptor;
898
899 Size = 0;
900 LockDescriptor = (TCG_LOCKING_FEATURE_DESCRIPTOR *)TcgGetFeature (Discovery, TCG_FEATURE_LOCKING, &Size);
901
902 if ((LockDescriptor != NULL) && (Size >= sizeof (*LockDescriptor))) {
903 DEBUG ((DEBUG_INFO, "locked: %d\n", LockDescriptor->Locked));
904 return LockDescriptor->Locked;
905 }
906
907 //
908 // Descriptor was not found
909 //
910 return FALSE;
911 }