UnitTest/TestSpdmResponder/TestSpdmResponderCertificate.c
/**
  Test 11: request MAX_SPDM_CERT_CHAIN_BLOCK_LEN bytes of a short certificate chain (fits in 1 message)
  Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
**/
Test 11: request MAX_SPDM_CERT_CHAIN_BLOCK_LEN bytes of a short certificate chain (fits in 1 message)
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 11 : request MAX_SPDM_CERT_CHAIN_BLOCK_LEN bytes of a short certificate chain ( fits in 1 message ) Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
void TestSpdmResponderCertificateCase11(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;
  SPDM_ERROR_RESPONSE  *SpdmResponseError;
  VOID                 *Data;
  UINTN                DataSize;
  UINT16               TestCases[] =   {TEST_CERT_SMALL};
  UINTN                ExpectedChunkSize;
  UINTN                ExpectedRemainder;
  SpdmTestContext = *state;
  SpdmContext = SpdmTestContext->SpdmContext;
  SpdmTestContext->CaseId = 0xB;
  SpdmContext->ConnectionInfo.ConnectionState = SpdmConnectionStateAfterDigests;
  SpdmContext->LocalContext.Capability.Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
  mSpdmGetCertificateRequest3.Length = MAX_SPDM_CERT_CHAIN_BLOCK_LEN;
  mSpdmGetCertificateRequest3.Offset = 0;
  for(int i=0; i<sizeof(TestCases)/sizeof(TestCases[0]); i++) {
    ReadResponderPublicCertificateChainBySize (mUseHashAlgo, mUseAsymAlgo, TestCases[i], &Data, &DataSize, NULL, NULL);
    SpdmContext->LocalContext.LocalCertChainProvision[0] = Data;
    SpdmContext->LocalContext.LocalCertChainProvisionSize[0] = DataSize;
    SpdmContext->LocalContext.SlotCount = 1;
    TEST_DEBUG_PRINT("DataSize: %u\n",DataSize);
    TEST_DEBUG_PRINT("mSpdmGetCertificateRequest3.Offset: %u\n",mSpdmGetCertificateRequest3.Offset);
    TEST_DEBUG_PRINT("mSpdmGetCertificateRequest3.Length: %u\n",mSpdmGetCertificateRequest3.Length);
    TEST_DEBUG_PRINT("Offset + Length: %u\n",mSpdmGetCertificateRequest3.Offset + mSpdmGetCertificateRequest3.Length);
    ResetManagedBuffer (&SpdmContext->Transcript.MessageB);
    ResponseSize = sizeof(Response);
    Status = SpdmGetResponseCertificate (SpdmContext, mSpdmGetCertificateRequest3Size, &mSpdmGetCertificateRequest3, &ResponseSize, Response);
    assert_int_equal (Status, RETURN_SUCCESS);
    ExpectedChunkSize = MIN(mSpdmGetCertificateRequest3.Length, DataSize - mSpdmGetCertificateRequest3.Offset);
    ExpectedChunkSize = MIN(ExpectedChunkSize, MAX_SPDM_CERT_CHAIN_BLOCK_LEN);
    ExpectedRemainder = DataSize - mSpdmGetCertificateRequest3.Offset - ExpectedChunkSize;
    TEST_DEBUG_PRINT("ExpectedChunkSize %u\n",ExpectedChunkSize);
    TEST_DEBUG_PRINT("ExpectedRemainder %u\n",ExpectedRemainder);
    if (ExpectedRemainder > MAX_UINT16 || ExpectedChunkSize > MAX_UINT16) {
      SpdmResponseError = (VOID *)Response;
      assert_int_equal (SpdmResponseError->Header.RequestResponseCode, SPDM_ERROR);
      assert_int_equal (SpdmResponseError->Header.Param1, SPDM_ERROR_CODE_INVALID_REQUEST);
    } else {
      assert_int_equal (ResponseSize, sizeof(SPDM_CERTIFICATE_RESPONSE) + ExpectedChunkSize);
      SpdmResponse = (VOID *)Response;
      assert_int_equal (SpdmResponse->Header.RequestResponseCode, SPDM_CERTIFICATE);
      assert_int_equal (SpdmResponse->Header.Param1, 0);
      assert_int_equal (SpdmResponse->PortionLength, ExpectedChunkSize);
      assert_int_equal (SpdmResponse->RemainderLength, ExpectedRemainder);
    }
    TEST_DEBUG_PRINT("\n");
    free(Data);
  }
}
void TestSpdmResponderCertificateCase11 ( 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 ; SPDM_ERROR_RESPONSE * SpdmResponseError ; VOID * Data ; UINTN DataSize ; UINT16 TestCases [ ] = { TEST_CERT_SMALL } ; UINTN ExpectedChunkSize ; UINTN ExpectedRemainder ; SpdmTestContext = * state ; SpdmContext = SpdmTestContext -> SpdmContext ; SpdmTestContext -> CaseId = 0xB ; SpdmContext -> ConnectionInfo . ConnectionState = SpdmConnectionStateAfterDigests ; SpdmContext -> LocalContext . Capability . Flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP ; mSpdmGetCertificateRequest3 . Length = MAX_SPDM_CERT_CHAIN_BLOCK_LEN ; mSpdmGetCertificateRequest3 . Offset = 0 ; for ( int i = 0 ; i < sizeof ( TestCases ) / sizeof ( TestCases [ 0 ] ) ; i ++ ) { ReadResponderPublicCertificateChainBySize ( mUseHashAlgo , mUseAsymAlgo , TestCases [ i ] , & Data , & DataSize , NULL , NULL ) ; SpdmContext -> LocalContext . LocalCertChainProvision [ 0 ] = Data ; SpdmContext -> LocalContext . LocalCertChainProvisionSize [ 0 ] = DataSize ; SpdmContext -> LocalContext . SlotCount = 1 ; TEST_DEBUG_PRINT ( " \n " , DataSize ) ; TEST_DEBUG_PRINT ( " \n " , mSpdmGetCertificateRequest3 . Offset ) ; TEST_DEBUG_PRINT ( " \n " , mSpdmGetCertificateRequest3 . Length ) ; TEST_DEBUG_PRINT ( " \n " , mSpdmGetCertificateRequest3 . Offset + mSpdmGetCertificateRequest3 . Length ) ; ResetManagedBuffer ( & SpdmContext -> Transcript . MessageB ) ; ResponseSize = sizeof ( Response ) ; Status = SpdmGetResponseCertificate ( SpdmContext , mSpdmGetCertificateRequest3Size , & mSpdmGetCertificateRequest3 , & ResponseSize , Response ) ; assert_int_equal ( Status , RETURN_SUCCESS ) ; ExpectedChunkSize = MIN ( mSpdmGetCertificateRequest3 . Length , DataSize - mSpdmGetCertificateRequest3 . Offset ) ; ExpectedChunkSize = MIN ( ExpectedChunkSize , MAX_SPDM_CERT_CHAIN_BLOCK_LEN ) ; ExpectedRemainder = DataSize - mSpdmGetCertificateRequest3 . Offset - ExpectedChunkSize ; TEST_DEBUG_PRINT ( " \n " , ExpectedChunkSize ) ; TEST_DEBUG_PRINT ( " \n " , ExpectedRemainder ) ; if ( ExpectedRemainder > MAX_UINT16 || ExpectedChunkSize > MAX_UINT16 ) { SpdmResponseError = ( VOID * ) Response ; assert_int_equal ( SpdmResponseError -> Header . RequestResponseCode , SPDM_ERROR ) ; assert_int_equal ( SpdmResponseError -> Header . Param1 , SPDM_ERROR_CODE_INVALID_REQUEST ) ; } else { assert_int_equal ( ResponseSize , sizeof ( SPDM_CERTIFICATE_RESPONSE ) + ExpectedChunkSize ) ; SpdmResponse = ( VOID * ) Response ; assert_int_equal ( SpdmResponse -> Header . RequestResponseCode , SPDM_CERTIFICATE ) ; assert_int_equal ( SpdmResponse -> Header . Param1 , 0 ) ; assert_int_equal ( SpdmResponse -> PortionLength , ExpectedChunkSize ) ; assert_int_equal ( SpdmResponse -> RemainderLength , ExpectedRemainder ) ; } TEST_DEBUG_PRINT ( " \n " ) ; free ( Data ) ; } }
Test 11: request MAX_SPDM_CERT_CHAIN_BLOCK_LEN bytes of a short certificate chain (fits in 1 message)
Expected Behavior: generate correctly formed Certficate messages, including its PortionLength and RemainderLength fields
Test 11 : request MAX_SPDM_CERT_CHAIN_BLOCK_LEN bytes of a short certificate chain ( fits in 1 message ) Expected Behavior : generate correctly formed Certficate messages including its PortionLength and RemainderLength fields
// Setting up the SpdmContext and loading a sample certificate chain // reseting an internal buffer to avoid overflow and prevent tests to succeed // Expected received length is limited by MAX_SPDM_CERT_CHAIN_BLOCK_LEN and by the remaining length // Expected certificate length left