TianoCore EDK2 master
Loading...
Searching...
No Matches
SpdmAuthentication.c
Go to the documentation of this file.
1
11
28EFIAPI
30 IN UINT32 PcrIndex,
31 IN UINT32 EventType,
32 IN CHAR16 *VarName,
33 IN EFI_GUID *VendorGuid,
34 IN VOID *VarData,
35 IN UINTN VarSize
36 )
37{
38 EFI_STATUS Status;
39 UINTN VarNameLength;
40 UEFI_VARIABLE_DATA *VarLog;
41 UINT32 VarLogSize;
42
43 if (!(((VarSize == 0) && (VarData == NULL)) || ((VarSize != 0) && (VarData != NULL)))) {
44 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
45 return EFI_INVALID_PARAMETER;
46 }
47
48 VarNameLength = StrLen (VarName);
49 VarLogSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
50 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
51
52 VarLog = (UEFI_VARIABLE_DATA *)AllocateZeroPool (VarLogSize);
53 if (VarLog == NULL) {
54 return EFI_OUT_OF_RESOURCES;
55 }
56
57 CopyMem (&VarLog->VariableName, VendorGuid, sizeof (VarLog->VariableName));
58 VarLog->UnicodeNameLength = VarNameLength;
59 VarLog->VariableDataLength = VarSize;
60 CopyMem (
61 VarLog->UnicodeName,
62 VarName,
63 VarNameLength * sizeof (*VarName)
64 );
65 if (VarSize != 0) {
66 CopyMem (
67 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
68 VarData,
69 VarSize
70 );
71 }
72
73 DEBUG ((DEBUG_INFO, "VariableDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PcrIndex, (UINTN)EventType));
74 DEBUG ((DEBUG_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
75
76 Status = TpmMeasureAndLogData (
77 PcrIndex,
78 EventType,
79 VarLog,
80 VarLogSize,
81 VarLog,
82 VarLogSize
83 );
84 FreePool (VarLog);
85 return Status;
86}
87
108 IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext,
109 IN UINT8 AuthState,
110 IN UINTN CertChainSize,
111 IN UINT8 *CertChain,
112 IN VOID *TrustAnchor,
113 IN UINTN TrustAnchorSize,
114 IN UINT8 SlotId,
115 OUT EDKII_DEVICE_SECURITY_STATE *SecurityState
116 )
117{
118 VOID *EventLog;
119 UINT32 EventLogSize;
120 UINT8 *EventLogPtr;
124 VOID *DeviceContext;
125 UINTN DeviceContextSize;
126 EFI_STATUS Status;
127 UINTN DevicePathSize;
128 UINT32 BaseHashAlgo;
129 UINTN DataSize;
130 VOID *SpdmContext;
131 SPDM_DATA_PARAMETER Parameter;
132 EFI_SIGNATURE_DATA *SignatureData;
133 UINTN SignatureDataSize;
134
135 SpdmContext = SpdmDeviceContext->SpdmContext;
136
137 EventLog = NULL;
138 ZeroMem (&Parameter, sizeof (Parameter));
139 Parameter.location = SpdmDataLocationConnection;
140 DataSize = sizeof (BaseHashAlgo);
141 Status = SpdmGetData (SpdmContext, SpdmDataBaseHashAlgo, &Parameter, &BaseHashAlgo, &DataSize);
142 ASSERT_EFI_ERROR (Status);
143
144 DeviceContextSize = GetDeviceMeasurementContextSize (SpdmDeviceContext);
145 DevicePathSize = GetDevicePathSize (SpdmDeviceContext->DevicePath);
146
147 switch (AuthState) {
148 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_SUCCESS:
149 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_AUTH:
150 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_BINDING:
151 EventLogSize = (UINT32)(sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT) +
153 sizeof (UINT64) + DevicePathSize +
155 CertChainSize +
156 DeviceContextSize);
157 EventLog = AllocatePool (EventLogSize);
158 if (EventLog == NULL) {
159 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_UEFI_OUT_OF_RESOURCE;
160 return EFI_OUT_OF_RESOURCES;
161 }
162
163 EventLogPtr = EventLog;
164
165 NvIndexInstance = (VOID *)EventLogPtr;
166 CopyMem (NvIndexInstance->Signature, TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE, sizeof (TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE));
167 NvIndexInstance->Version = TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT_VERSION;
168 ZeroMem (NvIndexInstance->Reserved, sizeof (NvIndexInstance->Reserved));
169 EventLogPtr += sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT);
170
171 EventData2 = (VOID *)EventLogPtr;
172 CopyMem (EventData2->Signature, TCG_DEVICE_SECURITY_EVENT_DATA_SIGNATURE_2, sizeof (EventData2->Signature));
173 EventData2->Version = TCG_DEVICE_SECURITY_EVENT_DATA_VERSION_2;
174 EventData2->AuthState = AuthState;
175 EventData2->Reserved = 0;
176 EventData2->Length = (UINT32)EventLogSize;
177 EventData2->DeviceType = GetSpdmDeviceType (SpdmDeviceContext);
178
179 EventData2->SubHeaderType = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_SUB_HEADER_TYPE_SPDM_CERT_CHAIN;
180 EventData2->SubHeaderLength = (UINT32)(sizeof (TCG_DEVICE_SECURITY_EVENT_DATA_SUB_HEADER_SPDM_CERT_CHAIN) + CertChainSize);
181 EventData2->SubHeaderUID = SpdmDeviceContext->DeviceUID;
182
183 EventLogPtr = (VOID *)(EventData2 + 1);
184
185 *(UINT64 *)EventLogPtr = (UINT64)DevicePathSize;
186 EventLogPtr += sizeof (UINT64);
187 CopyMem (EventLogPtr, SpdmDeviceContext->DevicePath, DevicePathSize);
188 EventLogPtr += DevicePathSize;
189
190 TcgSpdmCertChain = (VOID *)EventLogPtr;
191 TcgSpdmCertChain->SpdmVersion = SpdmDeviceContext->SpdmVersion;
192 TcgSpdmCertChain->SpdmSlotId = SlotId;
193 TcgSpdmCertChain->Reserved = 0;
194 TcgSpdmCertChain->SpdmHashAlgo = BaseHashAlgo;
196
197 CopyMem (EventLogPtr, CertChain, CertChainSize);
198 EventLogPtr += CertChainSize;
199
200 if (DeviceContextSize != 0) {
201 DeviceContext = (VOID *)EventLogPtr;
202 Status = CreateDeviceMeasurementContext (SpdmDeviceContext, DeviceContext, DeviceContextSize);
203 if (Status != EFI_SUCCESS) {
204 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
205 Status = EFI_DEVICE_ERROR;
206 goto Exit;
207 }
208 }
209
210 Status = TpmMeasureAndLogData (
211 TCG_NV_EXTEND_INDEX_FOR_INSTANCE,
212 EV_NO_ACTION,
213 EventLog,
214 EventLogSize,
215 EventLog,
216 EventLogSize
217 );
218 if (EFI_ERROR (Status)) {
219 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_TCG_EXTEND_TPM_PCR;
220 }
221
222 DEBUG ((DEBUG_INFO, "TpmMeasureAndLogData (Instance) - %r\n", Status));
223
224 break;
225 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_INVALID:
226 EventLogSize = (UINT32)(sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT) +
228 sizeof (UINT64) + DevicePathSize +
230 DeviceContextSize);
231 EventLog = AllocatePool (EventLogSize);
232 if (EventLog == NULL) {
233 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_UEFI_OUT_OF_RESOURCE;
234 return EFI_OUT_OF_RESOURCES;
235 }
236
237 EventLogPtr = EventLog;
238
239 NvIndexInstance = (VOID *)EventLogPtr;
240 CopyMem (NvIndexInstance->Signature, TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE, sizeof (TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE));
241 NvIndexInstance->Version = TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT_VERSION;
242 ZeroMem (NvIndexInstance->Reserved, sizeof (NvIndexInstance->Reserved));
243 EventLogPtr += sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT);
244
245 EventData2 = (VOID *)EventLogPtr;
246 CopyMem (EventData2->Signature, TCG_DEVICE_SECURITY_EVENT_DATA_SIGNATURE_2, sizeof (EventData2->Signature));
247 EventData2->Version = TCG_DEVICE_SECURITY_EVENT_DATA_VERSION_2;
248 EventData2->AuthState = AuthState;
249 EventData2->Reserved = 0;
250 EventData2->Length = (UINT32)EventLogSize;
251 EventData2->DeviceType = GetSpdmDeviceType (SpdmDeviceContext);
252
253 EventData2->SubHeaderType = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_SUB_HEADER_TYPE_SPDM_CERT_CHAIN;
254 EventData2->SubHeaderLength = sizeof (TCG_DEVICE_SECURITY_EVENT_DATA_SUB_HEADER_SPDM_CERT_CHAIN);
255 EventData2->SubHeaderUID = SpdmDeviceContext->DeviceUID;
256
257 EventLogPtr = (VOID *)(EventData2 + 1);
258
259 *(UINT64 *)EventLogPtr = (UINT64)DevicePathSize;
260 EventLogPtr += sizeof (UINT64);
261 CopyMem (EventLogPtr, SpdmDeviceContext->DevicePath, DevicePathSize);
262 EventLogPtr += DevicePathSize;
263
264 TcgSpdmCertChain = (VOID *)EventLogPtr;
265 TcgSpdmCertChain->SpdmVersion = SpdmDeviceContext->SpdmVersion;
266 TcgSpdmCertChain->SpdmSlotId = SlotId;
267 TcgSpdmCertChain->Reserved = 0;
268 TcgSpdmCertChain->SpdmHashAlgo = BaseHashAlgo;
270
271 if (DeviceContextSize != 0) {
272 DeviceContext = (VOID *)EventLogPtr;
273 Status = CreateDeviceMeasurementContext (SpdmDeviceContext, DeviceContext, DeviceContextSize);
274 if (Status != EFI_SUCCESS) {
275 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
276 Status = EFI_DEVICE_ERROR;
277 goto Exit;
278 }
279 }
280
281 Status = TpmMeasureAndLogData (
282 TCG_NV_EXTEND_INDEX_FOR_INSTANCE,
283 EV_NO_ACTION,
284 EventLog,
285 EventLogSize,
286 EventLog,
287 EventLogSize
288 );
289 if (EFI_ERROR (Status)) {
290 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_TCG_EXTEND_TPM_PCR;
291 }
292
293 DEBUG ((DEBUG_INFO, "TpmMeasureAndLogData (Instance) - %r\n", Status));
294
295 goto Exit;
296 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_NO_SIG:
297 case TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_SPDM:
298 EventLogSize = (UINT32)(sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT) +
300 sizeof (UINT64) + DevicePathSize +
301 DeviceContextSize);
302 EventLog = AllocatePool (EventLogSize);
303 if (EventLog == NULL) {
304 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_UEFI_OUT_OF_RESOURCE;
305 return EFI_OUT_OF_RESOURCES;
306 }
307
308 EventLogPtr = EventLog;
309
310 NvIndexInstance = (VOID *)EventLogPtr;
311 CopyMem (NvIndexInstance->Signature, TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE, sizeof (TCG_NV_EXTEND_INDEX_FOR_INSTANCE_SIGNATURE));
312 NvIndexInstance->Version = TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT_VERSION;
313 ZeroMem (NvIndexInstance->Reserved, sizeof (NvIndexInstance->Reserved));
314 EventLogPtr += sizeof (TCG_NV_INDEX_INSTANCE_EVENT_LOG_STRUCT);
315
316 EventData2 = (VOID *)EventLogPtr;
317 CopyMem (EventData2->Signature, TCG_DEVICE_SECURITY_EVENT_DATA_SIGNATURE_2, sizeof (EventData2->Signature));
318 EventData2->Version = TCG_DEVICE_SECURITY_EVENT_DATA_VERSION_2;
319 EventData2->AuthState = AuthState;
320 EventData2->Reserved = 0;
321 EventData2->Length = (UINT32)EventLogSize;
322 EventData2->DeviceType = GetSpdmDeviceType (SpdmDeviceContext);
323
324 EventData2->SubHeaderType = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_SUB_HEADER_TYPE_SPDM_CERT_CHAIN;
325 EventData2->SubHeaderLength = 0;
326 EventData2->SubHeaderUID = SpdmDeviceContext->DeviceUID;
327
328 EventLogPtr = (VOID *)(EventData2 + 1);
329
330 *(UINT64 *)EventLogPtr = (UINT64)DevicePathSize;
331 EventLogPtr += sizeof (UINT64);
332 CopyMem (EventLogPtr, SpdmDeviceContext->DevicePath, DevicePathSize);
333 EventLogPtr += DevicePathSize;
334
335 if (DeviceContextSize != 0) {
336 DeviceContext = (VOID *)EventLogPtr;
337 Status = CreateDeviceMeasurementContext (SpdmDeviceContext, DeviceContext, DeviceContextSize);
338 if (Status != EFI_SUCCESS) {
339 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
340 Status = EFI_DEVICE_ERROR;
341 goto Exit;
342 }
343 }
344
345 Status = TpmMeasureAndLogData (
346 TCG_NV_EXTEND_INDEX_FOR_INSTANCE,
347 EV_NO_ACTION,
348 EventLog,
349 EventLogSize,
350 EventLog,
351 EventLogSize
352 );
353 if (EFI_ERROR (Status)) {
354 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_TCG_EXTEND_TPM_PCR;
355 }
356
357 DEBUG ((DEBUG_INFO, "TpmMeasureAndLogData (Instance) - %r\n", Status));
358
359 goto Exit;
360 default:
361 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_UEFI_UNSUPPORTED;
362 return EFI_UNSUPPORTED;
363 }
364
365 if ((TrustAnchor != NULL) && (TrustAnchorSize != 0)) {
366 SignatureDataSize = sizeof (EFI_GUID) + TrustAnchorSize;
367 SignatureData = AllocateZeroPool (SignatureDataSize);
368 if (SignatureData == NULL) {
369 ASSERT (SignatureData != NULL);
370 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_UEFI_OUT_OF_RESOURCE;
371 Status = EFI_OUT_OF_RESOURCES;
372 goto Exit;
373 }
374
375 CopyGuid (&SignatureData->SignatureOwner, &gEfiCallerIdGuid);
376 CopyMem (
377 (UINT8 *)SignatureData + sizeof (EFI_GUID),
378 TrustAnchor,
379 TrustAnchorSize
380 );
381
383 PCR_INDEX_FOR_SIGNATURE_DB,
384 EV_EFI_SPDM_DEVICE_AUTHORITY,
385 EFI_DEVICE_SECURITY_DATABASE,
386 &gEfiDeviceSignatureDatabaseGuid,
387 SignatureData,
388 SignatureDataSize
389 );
390 FreePool (SignatureData);
391 }
392
393Exit:
394 if (EventLog != NULL) {
395 FreePool (EventLog);
396 }
397
398 return Status;
399}
400
417 IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext,
418 IN UINT8 AuthState,
419 IN UINT8 *RequesterNonce,
420 IN UINT8 *ResponderNonce,
421 OUT EDKII_DEVICE_SECURITY_STATE *SecurityState
422 )
423{
424 EFI_STATUS Status;
425
426 {
427 TCG_NV_INDEX_DYNAMIC_EVENT_LOG_STRUCT_SPDM_CHALLENGE DynamicEventLogSpdmChallengeEvent;
428 TCG_NV_INDEX_DYNAMIC_EVENT_LOG_STRUCT_SPDM_CHALLENGE_AUTH DynamicEventLogSpdmChallengeAuthEvent;
429
430 CopyMem (DynamicEventLogSpdmChallengeEvent.Header.Signature, TCG_NV_EXTEND_INDEX_FOR_DYNAMIC_SIGNATURE, sizeof (TCG_NV_EXTEND_INDEX_FOR_DYNAMIC_SIGNATURE));
431 DynamicEventLogSpdmChallengeEvent.Header.Version = TCG_NV_INDEX_DYNAMIC_EVENT_LOG_STRUCT_VERSION;
432 ZeroMem (DynamicEventLogSpdmChallengeEvent.Header.Reserved, sizeof (DynamicEventLogSpdmChallengeEvent.Header.Reserved));
433 DynamicEventLogSpdmChallengeEvent.Header.Uid = SpdmDeviceContext->DeviceUID;
434 DynamicEventLogSpdmChallengeEvent.DescriptionSize = sizeof (TCG_SPDM_CHALLENGE_DESCRIPTION);
435 CopyMem (DynamicEventLogSpdmChallengeEvent.Description, TCG_SPDM_CHALLENGE_DESCRIPTION, sizeof (TCG_SPDM_CHALLENGE_DESCRIPTION));
436 DynamicEventLogSpdmChallengeEvent.DataSize = SPDM_NONCE_SIZE;
437 CopyMem (DynamicEventLogSpdmChallengeEvent.Data, RequesterNonce, SPDM_NONCE_SIZE);
438
439 Status = TpmMeasureAndLogData (
440 TCG_NV_EXTEND_INDEX_FOR_DYNAMIC,
441 EV_NO_ACTION,
442 &DynamicEventLogSpdmChallengeEvent,
443 sizeof (DynamicEventLogSpdmChallengeEvent),
444 &DynamicEventLogSpdmChallengeEvent,
445 sizeof (DynamicEventLogSpdmChallengeEvent)
446 );
447 if (EFI_ERROR (Status)) {
448 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_TCG_EXTEND_TPM_PCR;
449 }
450
451 DEBUG ((DEBUG_INFO, "TpmMeasureAndLogData (Dynamic) - %r\n", Status));
452
453 CopyMem (DynamicEventLogSpdmChallengeAuthEvent.Header.Signature, TCG_NV_EXTEND_INDEX_FOR_DYNAMIC_SIGNATURE, sizeof (TCG_NV_EXTEND_INDEX_FOR_DYNAMIC_SIGNATURE));
454 DynamicEventLogSpdmChallengeAuthEvent.Header.Version = TCG_NV_INDEX_DYNAMIC_EVENT_LOG_STRUCT_VERSION;
455 ZeroMem (DynamicEventLogSpdmChallengeAuthEvent.Header.Reserved, sizeof (DynamicEventLogSpdmChallengeAuthEvent.Header.Reserved));
456 DynamicEventLogSpdmChallengeAuthEvent.Header.Uid = SpdmDeviceContext->DeviceUID;
457 DynamicEventLogSpdmChallengeAuthEvent.DescriptionSize = sizeof (TCG_SPDM_CHALLENGE_AUTH_DESCRIPTION);
458 CopyMem (DynamicEventLogSpdmChallengeAuthEvent.Description, TCG_SPDM_CHALLENGE_AUTH_DESCRIPTION, sizeof (TCG_SPDM_CHALLENGE_AUTH_DESCRIPTION));
459 DynamicEventLogSpdmChallengeAuthEvent.DataSize = SPDM_NONCE_SIZE;
460 CopyMem (DynamicEventLogSpdmChallengeAuthEvent.Data, ResponderNonce, SPDM_NONCE_SIZE);
461
462 Status = TpmMeasureAndLogData (
463 TCG_NV_EXTEND_INDEX_FOR_DYNAMIC,
464 EV_NO_ACTION,
465 &DynamicEventLogSpdmChallengeAuthEvent,
466 sizeof (DynamicEventLogSpdmChallengeAuthEvent),
467 &DynamicEventLogSpdmChallengeAuthEvent,
468 sizeof (DynamicEventLogSpdmChallengeAuthEvent)
469 );
470 if (EFI_ERROR (Status)) {
471 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_TCG_EXTEND_TPM_PCR;
472 }
473
474 DEBUG ((DEBUG_INFO, "TpmMeasureAndLogData (Dynamic) - %r\n", Status));
475 }
476
477 return Status;
478}
479
496EFIAPI
498 IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext,
499 OUT UINT8 *AuthState,
500 OUT UINT8 *ValidSlotId,
501 OUT EDKII_DEVICE_SECURITY_STATE *SecurityState,
502 OUT BOOLEAN *IsValidCertChain,
503 OUT BOOLEAN *RootCertMatch
504 )
505{
506 EFI_STATUS Status;
507 SPDM_RETURN SpdmReturn;
508 VOID *SpdmContext;
509 UINT32 CapabilityFlags;
510 UINTN DataSize;
511 SPDM_DATA_PARAMETER Parameter;
512 UINT8 SlotMask;
513 UINT8 TotalDigestBuffer[LIBSPDM_MAX_HASH_SIZE * SPDM_MAX_SLOT_COUNT];
514 UINTN CertChainSize;
515 UINT8 CertChain[LIBSPDM_MAX_CERT_CHAIN_SIZE];
516 VOID *TrustAnchor;
517 UINTN TrustAnchorSize;
518 UINT8 SlotId;
519
520 SpdmContext = SpdmDeviceContext->SpdmContext;
521
522 ZeroMem (&Parameter, sizeof (Parameter));
523 Parameter.location = SpdmDataLocationConnection;
524 DataSize = sizeof (CapabilityFlags);
525 SpdmReturn = SpdmGetData (SpdmContext, SpdmDataCapabilityFlags, &Parameter, &CapabilityFlags, &DataSize);
526 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
527 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
528 return EFI_DEVICE_ERROR;
529 }
530
531 *IsValidCertChain = FALSE;
532 *RootCertMatch = FALSE;
533 CertChainSize = sizeof (CertChain);
534 ZeroMem (CertChain, sizeof (CertChain));
535 TrustAnchor = NULL;
536 TrustAnchorSize = 0;
537
538 //
539 // Init *ValidSlotId to invalid slot_id
540 //
541 *ValidSlotId = SPDM_MAX_SLOT_COUNT;
542
543 if ((CapabilityFlags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP) == 0) {
544 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_NO_SIG;
545 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_NO_CAPABILITIES;
546 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, 0, NULL, NULL, 0, 0, SecurityState);
547 return Status;
548 } else {
549 ZeroMem (TotalDigestBuffer, sizeof (TotalDigestBuffer));
550 SpdmReturn = SpdmGetDigest (SpdmContext, NULL, &SlotMask, TotalDigestBuffer);
551 if ((LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) || ((SlotMask & 0x01) == 0)) {
552 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_INVALID;
553 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_CERTIFIACTE_FAILURE;
554 SlotId = 0;
555 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, 0, NULL, NULL, 0, SlotId, SecurityState);
556 return Status;
557 }
558
559 for (SlotId = 0; SlotId < SPDM_MAX_SLOT_COUNT; SlotId++) {
560 if (((SlotMask >> SlotId) & 0x01) == 0) {
561 continue;
562 }
563
564 CertChainSize = sizeof (CertChain);
565 ZeroMem (CertChain, sizeof (CertChain));
566 SpdmReturn = SpdmGetCertificateEx (SpdmContext, NULL, SlotId, &CertChainSize, CertChain, (CONST VOID **)&TrustAnchor, &TrustAnchorSize);
567 if (LIBSPDM_STATUS_IS_SUCCESS (SpdmReturn)) {
568 *IsValidCertChain = TRUE;
569 break;
570 } else if (SpdmReturn == LIBSPDM_STATUS_VERIF_FAIL) {
571 *IsValidCertChain = FALSE;
572 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_INVALID;
573 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_CERTIFIACTE_FAILURE;
574 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, 0, NULL, NULL, 0, SlotId, SecurityState);
575 } else if (SpdmReturn == LIBSPDM_STATUS_VERIF_NO_AUTHORITY) {
576 *IsValidCertChain = TRUE;
577 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_AUTH;
578 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_CERTIFIACTE_FAILURE;
579 *ValidSlotId = SlotId;
580 }
581 }
582
583 if ((SlotId >= SPDM_MAX_SLOT_COUNT) && (*ValidSlotId == SPDM_MAX_SLOT_COUNT)) {
584 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
585 return EFI_DEVICE_ERROR;
586 }
587
588 if (TrustAnchor != NULL) {
589 *RootCertMatch = TRUE;
590 *ValidSlotId = SlotId;
591 } else {
592 *ValidSlotId = 0;
593 }
594
595 DEBUG ((DEBUG_INFO, "SpdmGetCertificateEx - SpdmReturn %p, TrustAnchorSize 0x%x, RootCertMatch %d\n", SpdmReturn, TrustAnchorSize, *RootCertMatch));
596
597 return EFI_SUCCESS;
598 }
599}
600
617EFIAPI
619 IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext,
620 OUT UINT8 *AuthState,
621 IN UINT8 ValidSlotId,
622 IN BOOLEAN IsValidCertChain,
623 IN BOOLEAN RootCertMatch,
624 OUT EDKII_DEVICE_SECURITY_STATE *SecurityState
625 )
626{
627 EFI_STATUS Status;
628 SPDM_RETURN SpdmReturn;
629 VOID *SpdmContext;
630 UINT32 CapabilityFlags;
631 UINTN DataSize;
632 SPDM_DATA_PARAMETER Parameter;
633 UINTN CertChainSize;
634 UINT8 CertChain[LIBSPDM_MAX_CERT_CHAIN_SIZE];
635 UINT8 RequesterNonce[SPDM_NONCE_SIZE];
636 UINT8 ResponderNonce[SPDM_NONCE_SIZE];
637 VOID *TrustAnchor;
638 UINTN TrustAnchorSize;
639 BOOLEAN IsValidChallengeAuthSig;
640
641 SpdmContext = SpdmDeviceContext->SpdmContext;
642
643 ZeroMem (&Parameter, sizeof (Parameter));
644 Parameter.location = SpdmDataLocationConnection;
645 DataSize = sizeof (CapabilityFlags);
646 SpdmReturn = SpdmGetData (SpdmContext, SpdmDataCapabilityFlags, &Parameter, &CapabilityFlags, &DataSize);
647 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
648 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_ERROR;
649 return EFI_DEVICE_ERROR;
650 }
651
652 IsValidChallengeAuthSig = FALSE;
653
654 // get the valid CertChain
655 CertChainSize = sizeof (CertChain);
656 ZeroMem (CertChain, sizeof (CertChain));
657 SpdmReturn = SpdmGetCertificateEx (SpdmContext, NULL, ValidSlotId, &CertChainSize, CertChain, (CONST VOID **)&TrustAnchor, &TrustAnchorSize);
658 if ((!LIBSPDM_STATUS_IS_SUCCESS (SpdmReturn)) && (!(SpdmReturn == LIBSPDM_STATUS_VERIF_NO_AUTHORITY))) {
659 return EFI_DEVICE_ERROR;
660 }
661
662 if ((CapabilityFlags & SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP) == 0) {
663 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_BINDING;
664 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_NO_CAPABILITIES;
665 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, CertChainSize, CertChain, NULL, 0, ValidSlotId, SecurityState);
666 return Status;
667 } else {
668 ZeroMem (RequesterNonce, sizeof (RequesterNonce));
669 ZeroMem (ResponderNonce, sizeof (ResponderNonce));
670 SpdmReturn = SpdmChallengeEx (SpdmContext, NULL, ValidSlotId, SPDM_CHALLENGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, NULL, NULL, NULL, RequesterNonce, ResponderNonce, NULL, 0);
671 if (SpdmReturn == LIBSPDM_STATUS_SUCCESS) {
672 IsValidChallengeAuthSig = TRUE;
673 } else if ((LIBSPDM_STATUS_IS_ERROR (SpdmReturn))) {
674 IsValidChallengeAuthSig = FALSE;
675 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_FAIL_INVALID;
676 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_CHALLENGE_FAILURE;
677 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, 0, NULL, NULL, 0, ValidSlotId, SecurityState);
678 return Status;
679 } else {
680 return EFI_DEVICE_ERROR;
681 }
682
683 if (IsValidCertChain && IsValidChallengeAuthSig && !RootCertMatch) {
684 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_AUTH;
685 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_NO_CERT_PROVISION;
686 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, CertChainSize, CertChain, NULL, 0, ValidSlotId, SecurityState);
687 } else if (IsValidCertChain && IsValidChallengeAuthSig && RootCertMatch) {
688 *AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_SUCCESS;
689 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_SUCCESS;
690 Status = ExtendCertificate (SpdmDeviceContext, *AuthState, CertChainSize, CertChain, TrustAnchor, TrustAnchorSize, ValidSlotId, SecurityState);
691 }
692
693 Status = ExtendAuthentication (SpdmDeviceContext, *AuthState, RequesterNonce, ResponderNonce, SecurityState);
694 }
695
696 return Status;
697}
UINT64 UINTN
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
UINTN EFIAPI GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
#define EDKII_DEVICE_SECURITY_STATE_SUCCESS
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
#define SPDM_CHALLENGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH
Definition: Spdm.h:577
EFI_STATUS EFIAPI DoDeviceCertificate(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext, OUT UINT8 *AuthState, OUT UINT8 *ValidSlotId, OUT EDKII_DEVICE_SECURITY_STATE *SecurityState, OUT BOOLEAN *IsValidCertChain, OUT BOOLEAN *RootCertMatch)
EFI_STATUS ExtendCertificate(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext, IN UINT8 AuthState, IN UINTN CertChainSize, IN UINT8 *CertChain, IN VOID *TrustAnchor, IN UINTN TrustAnchorSize, IN UINT8 SlotId, OUT EDKII_DEVICE_SECURITY_STATE *SecurityState)
EFI_STATUS EFIAPI DoDeviceAuthentication(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext, OUT UINT8 *AuthState, IN UINT8 ValidSlotId, IN BOOLEAN IsValidCertChain, IN BOOLEAN RootCertMatch, OUT EDKII_DEVICE_SECURITY_STATE *SecurityState)
EFI_STATUS ExtendAuthentication(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext, IN UINT8 AuthState, IN UINT8 *RequesterNonce, IN UINT8 *ResponderNonce, OUT EDKII_DEVICE_SECURITY_STATE *SecurityState)
EFI_STATUS EFIAPI MeasureVariable(IN UINT32 PcrIndex, IN UINT32 EventType, IN CHAR16 *VarName, IN EFI_GUID *VendorGuid, IN VOID *VarData, IN UINTN VarSize)
EFI_STATUS EFIAPI CreateDeviceMeasurementContext(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext, IN OUT VOID *DeviceContext, IN UINTN DeviceContextSize)
UINT32 EFIAPI GetSpdmDeviceType(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext)
UINTN EFIAPI GetDeviceMeasurementContextSize(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext)
EFI_STATUS EFIAPI TpmMeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
VOID EFIAPI Exit(IN EFI_STATUS Status)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
GUID EFI_GUID
Definition: UefiBaseType.h:25
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
Definition: Base.h:213
INT8 VariableData[1]
Driver or platform-specific data.