14#define CONNECTUIN_FAILURE_GET_SPDM_UID_FAILED "Fail to get Spdm Uid"
15#define CONNECTUIN_FAILURE_STGNATURE_DB_FUL_STRING "The Signature database devdb is full"
31 SpdmDeviceContextList = &mSpdmDeviceContextList;
34 if (NewSpdmDeviceContext ==
NULL) {
35 ASSERT (NewSpdmDeviceContext !=
NULL);
39 NewSpdmDeviceContext->Signature = SPDM_DEVICE_CONTEXT_INSTANCE_SIGNATURE;
40 NewSpdmDeviceContext->SpdmDeviceContext = SpdmDeviceContext;
42 InsertTailList (SpdmDeviceContextList, &NewSpdmDeviceContext->Link);
63 SpdmDeviceContextList = &mSpdmDeviceContextList;
66 while (!
IsNull (SpdmDeviceContextList, Link)) {
67 CurrentSpdmDeviceContext = SPDM_DEVICE_CONTEXT_INSTANCE_FROM_LINK (Link);
69 if (CurrentSpdmDeviceContext->SpdmDeviceContext->SpdmContext == SpdmContext) {
70 return CurrentSpdmDeviceContext->SpdmDeviceContext->SpdmIoProtocol;
98 VarSize =
sizeof (*SpdmUid);
99 Status =
gRT->GetVariable (
101 &gEfiDeviceSecuritySpdmUidGuid,
106 if (Status == EFI_NOT_FOUND) {
108 }
else if (EFI_ERROR (Status)) {
113 Status =
gRT->SetVariable (
115 &gEfiDeviceSecuritySpdmUidGuid,
116 EFI_VARIABLE_BOOTSERVICE_ACCESS,
137 IN CHAR8 *FailureString,
145 EV_PLATFORM_CONFIG_FLAGS,
151 DEBUG ((DEBUG_INFO,
"RecordConnectionFailureStatus %r\n", Status));
174 UINTN SpdmContextSize;
176 UINTN ScratchBufferSize;
178 SPDM_RETURN SpdmReturn;
183 UINTN SiglistHeaderSize;
187 SPDM_DATA_PARAMETER Parameter;
194 if (SpdmDeviceContext ==
NULL) {
195 ASSERT (SpdmDeviceContext !=
NULL);
199 SpdmDeviceContext->Signature = SPDM_DEVICE_CONTEXT_SIGNATURE;
201 SpdmDeviceContext->IsEmbeddedDevice = SpdmDeviceInfo->IsEmbeddedDevice;
203 SpdmContextSize = SpdmGetContextSize ();
205 if (SpdmContext ==
NULL) {
206 ASSERT (SpdmContext !=
NULL);
210 SpdmReturn = SpdmInitContext (SpdmContext);
211 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
215 SpdmRegisterDeviceIoFunc (
217 SpdmDeviceInfo->SendMessage,
218 SpdmDeviceInfo->ReceiveMessage
220 SpdmRegisterTransportLayerFunc (
222 SpdmDeviceInfo->MaxSpdmMsgSize,
223 SpdmDeviceInfo->TransportHeaderSize,
224 SpdmDeviceInfo->TransportTailSize,
225 SpdmDeviceInfo->TransportEncodeMessage,
226 SpdmDeviceInfo->TransportDecodeMessage
229 SpdmRegisterDeviceBufferFunc (
231 SpdmDeviceInfo->SenderBufferSize,
232 SpdmDeviceInfo->ReceiverBufferSize,
233 SpdmDeviceInfo->AcquireSenderBuffer,
234 SpdmDeviceInfo->ReleaseSenderBuffer,
235 SpdmDeviceInfo->AcquireReceiverBuffer,
236 SpdmDeviceInfo->ReleaseReceiverBuffer
239 ScratchBufferSize = SpdmGetSizeofRequiredScratchBuffer (SpdmContext);
241 if (ScratchBuffer ==
NULL) {
242 ASSERT (ScratchBuffer !=
NULL);
246 SpdmSetScratchBuffer (SpdmContext, ScratchBuffer, ScratchBufferSize);
248 SpdmDeviceContext->SpdmContextSize = SpdmContextSize;
249 SpdmDeviceContext->SpdmContext = SpdmContext;
250 SpdmDeviceContext->ScratchBufferSize = ScratchBufferSize;
251 SpdmDeviceContext->ScratchBuffer = ScratchBuffer;
253 Status =
gBS->HandleProtocol (
255 &gEfiDevicePathProtocolGuid,
256 (VOID **)&SpdmDeviceContext->DevicePath
258 if (EFI_ERROR (Status)) {
259 DEBUG ((DEBUG_ERROR,
"Locate - DevicePath - %r\n", Status));
263 Status =
gBS->HandleProtocol (
266 (VOID **)&SpdmDeviceContext->DeviceIo
268 if (EFI_ERROR (Status)) {
269 DEBUG ((DEBUG_ERROR,
"Locate - DeviceIo - %r\n", Status));
273 if (SpdmDeviceInfo->SpdmIoProtocolGuid !=
NULL) {
274 Status =
gBS->HandleProtocol (
276 SpdmDeviceInfo->SpdmIoProtocolGuid,
277 (VOID **)&SpdmDeviceContext->SpdmIoProtocol
279 if (EFI_ERROR (Status)) {
280 DEBUG ((DEBUG_ERROR,
"Locate - SpdmIoProtocol - %r\n", Status));
286 if (SpdmDeviceContext->DeviceIo ==
NULL) {
287 DEBUG ((DEBUG_ERROR,
"Locate - PciIo - %r\n", Status));
292 Status =
GetSpdmUid (&SpdmDeviceContext->DeviceUID);
293 if (EFI_ERROR (Status)) {
295 CONNECTUIN_FAILURE_GET_SPDM_UID_FAILED,
296 sizeof (CONNECTUIN_FAILURE_GET_SPDM_UID_FAILED)
298 if (EFI_ERROR (Status)) {
303 DEBUG ((DEBUG_ERROR,
"Fail to get UID - %r\n", Status));
310 EFI_DEVICE_SECURITY_DATABASE,
311 &gEfiDeviceSignatureDatabaseGuid,
312 (VOID **)&SpdmDeviceContext->SignatureList,
313 &SpdmDeviceContext->SignatureListSize
315 if ((!EFI_ERROR (Status)) && (SpdmDeviceContext->SignatureList !=
NULL)) {
316 DbList = SpdmDeviceContext->SignatureList;
317 DbSize = SpdmDeviceContext->SignatureListSize;
318 while ((DbSize > 0) && (SpdmDeviceContext->SignatureListSize >= DbList->
SignatureListSize)) {
336 for (Index = 0; Index < CertCount; Index++) {
340 ZeroMem (&Parameter,
sizeof (Parameter));
341 Parameter.location = SpdmDataLocationLocal;
342 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataPeerPublicRootCert, &Parameter, Data, DataSize);
343 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
344 if (SpdmReturn == LIBSPDM_STATUS_BUFFER_FULL) {
346 CONNECTUIN_FAILURE_STGNATURE_DB_FUL_STRING,
347 sizeof (CONNECTUIN_FAILURE_STGNATURE_DB_FUL_STRING)
349 if (EFI_ERROR (Status)) {
368 ZeroMem (&Parameter,
sizeof (Parameter));
369 Parameter.location = SpdmDataLocationLocal;
370 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataCapabilityCTExponent, &Parameter, &Data8,
sizeof (Data8));
371 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
377 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataCapabilityFlags, &Parameter, &Data32,
sizeof (Data32));
378 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
383 Data8 = SPDM_MEASUREMENT_SPECIFICATION_DMTF;
384 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataMeasurementSpec, &Parameter, &Data8,
sizeof (Data8));
385 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
390 if (SpdmDeviceInfo->BaseAsymAlgo != 0) {
391 Data32 = SpdmDeviceInfo->BaseAsymAlgo;
394 SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072 |
395 SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096 |
396 SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256 |
397 SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384 |
398 SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521;
401 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataBaseAsymAlgo, &Parameter, &Data32,
sizeof (Data32));
402 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
407 if (SpdmDeviceInfo->BaseHashAlgo != 0) {
408 Data32 = SpdmDeviceInfo->BaseHashAlgo;
411 SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_384 |
412 SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_512;
415 SpdmReturn = SpdmSetData (SpdmContext, SpdmDataBaseHashAlgo, &Parameter, &Data32,
sizeof (Data32));
416 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
421 SpdmReturn = SpdmInitConnection (SpdmContext,
FALSE);
422 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
423 DEBUG ((DEBUG_ERROR,
"SpdmInitConnection - %p\n", SpdmReturn));
425 AuthState = TCG_DEVICE_SECURITY_EVENT_DATA_DEVICE_AUTH_STATE_NO_SPDM;
426 SecurityState->AuthenticationState = EDKII_DEVICE_SECURITY_STATE_ERROR_DEVICE_NO_CAPABILITIES;
429 DEBUG ((DEBUG_ERROR,
"ExtendCertificate AUTH_STATE_NO_SPDM failed\n"));
435 ZeroMem (&Parameter,
sizeof (Parameter));
436 Parameter.location = SpdmDataLocationConnection;
437 DataSize =
sizeof (Data16);
438 SpdmReturn = SpdmGetData (SpdmContext, SpdmDataSpdmVersion, &Parameter, &Data16, &DataSize);
439 if (LIBSPDM_STATUS_IS_ERROR (SpdmReturn)) {
440 DEBUG ((DEBUG_ERROR,
"SpdmGetData - %p\n", SpdmReturn));
444 SpdmDeviceContext->SpdmVersion = (Data16 >> SPDM_VERSION_NUMBER_SHIFT_BIT);
446 return SpdmDeviceContext;
465 if (SpdmDeviceContext->SpdmContext !=
NULL) {
466 ZeroMem (SpdmDeviceContext->SpdmContext, SpdmDeviceContext->SpdmContextSize);
467 FreePool (SpdmDeviceContext->SpdmContext);
470 if (SpdmDeviceContext->ScratchBuffer !=
NULL) {
471 ZeroMem (SpdmDeviceContext->ScratchBuffer, SpdmDeviceContext->ScratchBufferSize);
472 FreePool (SpdmDeviceContext->ScratchBuffer);
475 if (SpdmDeviceContext->SignatureList !=
NULL) {
476 ZeroMem (SpdmDeviceContext->SignatureList, SpdmDeviceContext->SignatureListSize);
477 FreePool (SpdmDeviceContext->SignatureList);
BOOLEAN EFIAPI IsNull(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
LIST_ENTRY *EFIAPI GetNextNode(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
LIST_ENTRY *EFIAPI GetFirstNode(IN CONST LIST_ENTRY *List)
#define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)
LIST_ENTRY *EFIAPI InsertTailList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_GUID gEdkiiDeviceIdentifierTypePciGuid
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_RUNTIME_SERVICES * gRT
#define DEBUG(Expression)
#define SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048
#define SPDM_ALGORITHMS_BASE_HASH_ALGO_TPM_ALG_SHA_256
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)
VOID *EFIAPI GetSpdmIoProtocolViaSpdmContext(IN VOID *SpdmContext)
SPDM_DEVICE_CONTEXT *EFIAPI CreateSpdmDeviceContext(IN EDKII_SPDM_DEVICE_INFO *SpdmDeviceInfo, OUT EDKII_DEVICE_SECURITY_STATE *SecurityState)
VOID EFIAPI DestroySpdmDeviceContext(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext)
VOID RecordSpdmDeviceContextInList(IN SPDM_DEVICE_CONTEXT *SpdmDeviceContext)
EFI_STATUS RecordConnectionFailureStatus(IN CHAR8 *FailureString, IN UINT32 StringLen)
EFI_STATUS GetSpdmUid(UINT64 *SpdmUid)
EFI_STATUS EFIAPI TpmMeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
EFI_STATUS EFIAPI GetVariable2(IN CONST CHAR16 *Name, IN CONST EFI_GUID *Guid, OUT VOID **Value, OUT UINTN *Size OPTIONAL)
UINT32 SignatureHeaderSize