UnitTest/TestSpdmResponder/TestSpdmResponderCertificate.c
/**
  Test 12: Request a whole certificate chain byte by byte
  Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
**/
Test 12: Request a whole certificate chain byte by byte
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 12 : Request a whole certificate chain byte by byte Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
void TestSpdmResponderCertificateCase12(void **state) {
  RETURN_STATUS        Status;
  SPDM_TEST_CONTEXT    *SpdmTestContext;
  SPDM_DEVICE_CONTEXT  *SpdmContext;
  UINTN                ResponseSize;
  UINT8                Response[MAX_SPDM_MESSAGE_BUFFER_SIZE];
  SPDM_CERTIFICATE_RESPONSE *SpdmResponse;
  VOID                 *Data;
  UINTN                DataSize;
  UINTN                Count;
  UINT16               ExpectedChunkSize;
  SpdmTestContext = *state;
  SpdmContext = SpdmTestContext->SpdmContext;
  SpdmTestContext->CaseId = 0xC;
  SpdmContext->ConnectionInfo.ConnectionState = SpdmConnectionStateAfterDigests;
  SpdmContext->LocalContext.Capability.Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
  ReadResponderPublicCertificateChain (mUseHashAlgo, mUseAsymAlgo, &Data, &DataSize, NULL, NULL);
  SpdmContext->LocalContext.LocalCertChainProvision[0] = Data;
  SpdmContext->LocalContext.LocalCertChainProvisionSize[0] = DataSize;
  SpdmContext->LocalContext.SlotCount = 1;
  mSpdmGetCertificateRequest3.Length = 1;
  ExpectedChunkSize = 1;
  Count = (DataSize + mSpdmGetCertificateRequest3.Length - 1) / mSpdmGetCertificateRequest3.Length;
  ResetManagedBuffer (&SpdmContext->Transcript.MessageB);
  SpdmResponse = NULL;
  for(UINTN offset=0; offset<DataSize; offset++) {
    TEST_DEBUG_PRINT("offset:%u \n", offset);
    mSpdmGetCertificateRequest3.Offset = (UINT16)offset;
    ResponseSize = sizeof(Response);
    Status = SpdmGetResponseCertificate (SpdmContext, mSpdmGetCertificateRequest3Size, &mSpdmGetCertificateRequest3, &ResponseSize, Response);
    assert_int_equal (Status, RETURN_SUCCESS);
    SpdmResponse = (VOID *)Response;
    if (SpdmResponse->Header.RequestResponseCode == SPDM_CERTIFICATE) {
      assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_CERTIFICATE);
      assert_int_equal (ResponseSize, sizeof(SPDM_CERTIFICATE_RESPONSE) + ExpectedChunkSize);
      assert_int_equal (SpdmResponse->Header.Param1, 0);
      assert_int_equal (SpdmResponse->PortionLength, ExpectedChunkSize);
      assert_int_equal (SpdmResponse->RemainderLength, DataSize - offset - ExpectedChunkSize);
      assert_int_equal ( ((UINT8*) Data)[offset], (Response + sizeof(SPDM_CERTIFICATE_RESPONSE))[0]);
    } else {
      assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_ERROR);
      break;
    }
  }
  if (SpdmResponse != NULL) {
    if (SpdmResponse->Header.RequestResponseCode == SPDM_CERTIFICATE) {
      assert_int_equal (SpdmContext->Transcript.MessageB.BufferSize, sizeof(SPDM_GET_CERTIFICATE_REQUEST)*Count + sizeof(SPDM_CERTIFICATE_RESPONSE)*Count + DataSize);
    }
  }
  free(Data);
}
void TestSpdmResponderCertificateCase12 ( void * * state ) { RETURN_STATUS Status ; SPDM_TEST_CONTEXT * SpdmTestContext ; SPDM_DEVICE_CONTEXT * SpdmContext ; UINTN ResponseSize ; UINT8 Response [ MAX_SPDM_MESSAGE_BUFFER_SIZE ] ; SPDM_CERTIFICATE_RESPONSE * SpdmResponse ; VOID * Data ; UINTN DataSize ; UINTN Count ; UINT16 ExpectedChunkSize ; SpdmTestContext = * state ; SpdmContext = SpdmTestContext -> SpdmContext ; SpdmTestContext -> CaseId = 0xC ; SpdmContext -> ConnectionInfo . ConnectionState = SpdmConnectionStateAfterDigests ; SpdmContext -> LocalContext . Capability . Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP ; ReadResponderPublicCertificateChain ( mUseHashAlgo , mUseAsymAlgo , & Data , & DataSize , NULL , NULL ) ; SpdmContext -> LocalContext . LocalCertChainProvision [ 0 ] = Data ; SpdmContext -> LocalContext . LocalCertChainProvisionSize [ 0 ] = DataSize ; SpdmContext -> LocalContext . SlotCount = 1 ; mSpdmGetCertificateRequest3 . Length = 1 ; ExpectedChunkSize = 1 ; Count = ( DataSize + mSpdmGetCertificateRequest3 . Length - 1 ) / mSpdmGetCertificateRequest3 . Length ; ResetManagedBuffer ( & SpdmContext -> Transcript . MessageB ) ; SpdmResponse = NULL ; for ( UINTN offset = 0 ; offset < DataSize ; offset ++ ) { TEST_DEBUG_PRINT ( " \n " , offset ) ; mSpdmGetCertificateRequest3 . Offset = ( UINT16 ) offset ; ResponseSize = sizeof ( Response ) ; Status = SpdmGetResponseCertificate ( SpdmContext , mSpdmGetCertificateRequest3Size , & mSpdmGetCertificateRequest3 , & ResponseSize , Response ) ; assert_int_equal ( Status , RETURN_SUCCESS ) ; SpdmResponse = ( VOID * ) Response ; if ( SpdmResponse -> Header . RequestResponseCode == SPDM_CERTIFICATE ) { assert_int_equal ( SpdmResponse -> Header . RequestResponseCode , SPDM_CERTIFICATE ) ; assert_int_equal ( ResponseSize , sizeof ( SPDM_CERTIFICATE_RESPONSE ) + ExpectedChunkSize ) ; assert_int_equal ( SpdmResponse -> Header . Param1 , 0 ) ; assert_int_equal ( SpdmResponse -> PortionLength , ExpectedChunkSize ) ; assert_int_equal ( SpdmResponse -> RemainderLength , DataSize - offset - ExpectedChunkSize ) ; assert_int_equal ( ( ( UINT8 * ) Data ) [ offset ] , ( Response + sizeof ( SPDM_CERTIFICATE_RESPONSE ) ) [ 0 ] ) ; } else { assert_int_equal ( SpdmResponse -> Header . RequestResponseCode , SPDM_ERROR ) ; break ; } } if ( SpdmResponse != NULL ) { if ( SpdmResponse -> Header . RequestResponseCode == SPDM_CERTIFICATE ) { assert_int_equal ( SpdmContext -> Transcript . MessageB . BufferSize , sizeof ( SPDM_GET_CERTIFICATE_REQUEST ) * Count + sizeof ( SPDM_CERTIFICATE_RESPONSE ) * Count + DataSize ) ; } } free ( Data ) ; }
Test 12: Request a whole certificate chain byte by byte
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 12 : Request a whole certificate chain byte by byte Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
// Setting up the SpdmContext and loading a sample certificate chain // This tests considers only Length = 1 // reseting an internal buffer to avoid overflow and prevent tests to succeed // It may fail because the spdm does not support too many messages. // assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_CERTIFICATE);