LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - finish.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.7 % 1730 1569
Test Date: 2025-08-24 08:11:14 Functions: 100.0 % 29 29

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2025 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include "spdm_unit_test.h"
       8              : #include "internal/libspdm_requester_lib.h"
       9              : #include "internal/libspdm_secured_message_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
      12              : 
      13              : static size_t m_libspdm_local_buffer_size;
      14              : static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE];
      15              : 
      16              : static uint8_t m_libspdm_dummy_buffer[LIBSPDM_MAX_HASH_SIZE];
      17              : static const uint8_t m_libspdm_zero_buffer[LIBSPDM_MAX_HASH_SIZE] = {0};
      18              : 
      19              : static libspdm_th_managed_buffer_t th_curr;
      20              : 
      21           41 : void libspdm_secured_message_set_response_finished_key(
      22              :     void *spdm_secured_message_context, const void *key, size_t key_size)
      23              : {
      24              :     libspdm_secured_message_context_t *secured_message_context;
      25              : 
      26           41 :     secured_message_context = spdm_secured_message_context;
      27           41 :     LIBSPDM_ASSERT(key_size == secured_message_context->hash_size);
      28           41 :     libspdm_copy_mem(secured_message_context->handshake_secret.response_finished_key,
      29              :                      sizeof(secured_message_context->handshake_secret.response_finished_key),
      30              :                      key, secured_message_context->hash_size);
      31           41 : }
      32              : 
      33           40 : libspdm_return_t libspdm_requester_finish_test_send_message(void *spdm_context,
      34              :                                                             size_t request_size,
      35              :                                                             const void *request,
      36              :                                                             uint64_t timeout)
      37              : {
      38              :     libspdm_test_context_t *spdm_test_context;
      39              :     const uint8_t *ptr;
      40              : 
      41           40 :     spdm_test_context = libspdm_get_test_context();
      42           40 :     ptr = (const uint8_t *)request;
      43           40 :     switch (spdm_test_context->case_id) {
      44            1 :     case 0x1:
      45            1 :         return LIBSPDM_STATUS_SEND_FAIL;
      46            1 :     case 0x2:
      47            1 :         m_libspdm_local_buffer_size = 0;
      48            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      49              :                          request_size - 1);
      50            1 :         m_libspdm_local_buffer_size += (request_size - 1);
      51            1 :         return LIBSPDM_STATUS_SUCCESS;
      52            0 :     case 0x3:
      53            0 :         m_libspdm_local_buffer_size = 0;
      54            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      55              :                          request_size - 1);
      56            0 :         m_libspdm_local_buffer_size += (request_size - 1);
      57            0 :         return LIBSPDM_STATUS_SUCCESS;
      58            1 :     case 0x4:
      59            1 :         m_libspdm_local_buffer_size = 0;
      60            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      61              :                          request_size - 1);
      62            1 :         m_libspdm_local_buffer_size += (request_size - 1);
      63            1 :         return LIBSPDM_STATUS_SUCCESS;
      64            1 :     case 0x5:
      65            1 :         m_libspdm_local_buffer_size = 0;
      66            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      67              :                          request_size - 1);
      68            1 :         m_libspdm_local_buffer_size += (request_size - 1);
      69            1 :         return LIBSPDM_STATUS_SUCCESS;
      70            2 :     case 0x6:
      71            2 :         m_libspdm_local_buffer_size = 0;
      72            2 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      73              :                          request_size - 1);
      74            2 :         m_libspdm_local_buffer_size += (request_size - 1);
      75            2 :         return LIBSPDM_STATUS_SUCCESS;
      76            1 :     case 0x7:
      77            1 :         m_libspdm_local_buffer_size = 0;
      78            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      79              :                          request_size - 1);
      80            1 :         m_libspdm_local_buffer_size += (request_size - 1);
      81            1 :         return LIBSPDM_STATUS_SUCCESS;
      82            2 :     case 0x8:
      83            2 :         m_libspdm_local_buffer_size = 0;
      84            2 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      85              :                          request_size - 1);
      86            2 :         m_libspdm_local_buffer_size += (request_size - 1);
      87            2 :         return LIBSPDM_STATUS_SUCCESS;
      88            2 :     case 0x9: {
      89              :         static size_t sub_index = 0;
      90            2 :         if (sub_index == 0) {
      91            1 :             m_libspdm_local_buffer_size = 0;
      92            1 :             libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
      93              :                              request_size - 1);
      94            1 :             m_libspdm_local_buffer_size += (request_size - 1);
      95            1 :             sub_index++;
      96              :         }
      97              :     }
      98            2 :         return LIBSPDM_STATUS_SUCCESS;
      99           18 :     case 0xA:
     100           18 :         m_libspdm_local_buffer_size = 0;
     101           18 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     102              :                          request_size - 1);
     103           18 :         m_libspdm_local_buffer_size += (request_size - 1);
     104           18 :         return LIBSPDM_STATUS_SUCCESS;
     105            1 :     case 0xB:
     106            1 :         m_libspdm_local_buffer_size = 0;
     107            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     108              :                          request_size - 1);
     109            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     110            1 :         return LIBSPDM_STATUS_SUCCESS;
     111            0 :     case 0xC:
     112            0 :         m_libspdm_local_buffer_size = 0;
     113            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     114              :                          request_size - 1);
     115            0 :         m_libspdm_local_buffer_size += (request_size - 1);
     116            0 :         return LIBSPDM_STATUS_SUCCESS;
     117            0 :     case 0xD:
     118            0 :         m_libspdm_local_buffer_size = 0;
     119            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     120              :                          request_size - 1);
     121            0 :         m_libspdm_local_buffer_size += (request_size - 1);
     122            0 :         return LIBSPDM_STATUS_SUCCESS;
     123            1 :     case 0xE:
     124            1 :         m_libspdm_local_buffer_size = 0;
     125            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     126              :                          request_size - 1);
     127            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     128            1 :         return LIBSPDM_STATUS_SUCCESS;
     129            0 :     case 0xF:
     130            0 :         m_libspdm_local_buffer_size = 0;
     131            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     132              :                          request_size - 1);
     133            0 :         m_libspdm_local_buffer_size += (request_size - 1);
     134            0 :         return LIBSPDM_STATUS_SUCCESS;
     135            1 :     case 0x10:
     136            1 :         m_libspdm_local_buffer_size = 0;
     137            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     138              :                          request_size - 1);
     139            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     140            1 :         return LIBSPDM_STATUS_SUCCESS;
     141            1 :     case 0x11:
     142            1 :         m_libspdm_local_buffer_size = 0;
     143            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     144              :                          request_size - 1);
     145            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     146            1 :         return LIBSPDM_STATUS_SUCCESS;
     147            1 :     case 0x12:
     148            1 :         m_libspdm_local_buffer_size = 0;
     149            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     150              :                          request_size - 1);
     151            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     152            1 :         return LIBSPDM_STATUS_SUCCESS;
     153            0 :     case 0x13:
     154            0 :         m_libspdm_local_buffer_size = 0;
     155            0 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     156              :                          request_size - 1);
     157            0 :         m_libspdm_local_buffer_size += (request_size - 1);
     158            0 :         return LIBSPDM_STATUS_SUCCESS;
     159            1 :     case 0x14:
     160            1 :         m_libspdm_local_buffer_size = 0;
     161            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     162              :                          request_size - 1);
     163            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     164            1 :         return LIBSPDM_STATUS_SUCCESS;
     165            1 :     case 0x15:
     166            1 :         m_libspdm_local_buffer_size = 0;
     167            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     168              :                          request_size - 1);
     169            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     170            1 :         return LIBSPDM_STATUS_SUCCESS;
     171            1 :     case 0x16:
     172            1 :         m_libspdm_local_buffer_size = 0;
     173            1 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     174              :                          request_size - 1);
     175            1 :         m_libspdm_local_buffer_size += (request_size - 1);
     176            1 :         return LIBSPDM_STATUS_SUCCESS;
     177            3 :     case 0x17:
     178              :     case 0x18:
     179              :     case 0x19:
     180            3 :         m_libspdm_local_buffer_size = 0;
     181            3 :         libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer), &ptr[1],
     182              :                          request_size - 1);
     183            3 :         m_libspdm_local_buffer_size += (request_size - 1);
     184            3 :         return LIBSPDM_STATUS_SUCCESS;
     185            0 :     default:
     186            0 :         return LIBSPDM_STATUS_SEND_FAIL;
     187              :     }
     188              : }
     189              : 
     190           39 : libspdm_return_t libspdm_requester_finish_test_receive_message(
     191              :     void *spdm_context, size_t *response_size,
     192              :     void **response, uint64_t timeout)
     193              : {
     194              :     libspdm_test_context_t *spdm_test_context;
     195              : 
     196           39 :     spdm_test_context = libspdm_get_test_context();
     197           39 :     switch (spdm_test_context->case_id) {
     198            0 :     case 0x1:
     199            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     200              : 
     201            1 :     case 0x2: {
     202              :         spdm_finish_response_t *spdm_response;
     203              :         uint32_t hash_size;
     204              :         uint32_t hmac_size;
     205              :         uint8_t *ptr;
     206              :         void *data;
     207              :         size_t data_size;
     208              :         uint8_t *cert_buffer;
     209              :         size_t cert_buffer_size;
     210              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     211              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     212              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     213              :         size_t spdm_response_size;
     214              :         size_t transport_header_size;
     215              : 
     216              :         ((libspdm_context_t *)spdm_context)
     217            1 :         ->connection_info.algorithm.base_asym_algo =
     218              :             m_libspdm_use_asym_algo;
     219              :         ((libspdm_context_t *)spdm_context)
     220            1 :         ->connection_info.algorithm.base_hash_algo =
     221              :             m_libspdm_use_hash_algo;
     222              :         ((libspdm_context_t *)spdm_context)
     223            1 :         ->connection_info.algorithm.dhe_named_group =
     224              :             m_libspdm_use_dhe_algo;
     225              :         ((libspdm_context_t *)spdm_context)
     226            1 :         ->connection_info.algorithm.measurement_hash_algo =
     227              :             m_libspdm_use_measurement_hash_algo;
     228            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     229            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     230            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     231            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     232            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     233              : 
     234            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     235            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
     236            1 :         spdm_response->header.param1 = 0;
     237            1 :         spdm_response->header.param2 = 0;
     238            1 :         ptr = (void *)(spdm_response + 1);
     239            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     240              :                          sizeof(m_libspdm_local_buffer)
     241            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     242              :                             m_libspdm_local_buffer),
     243              :                          spdm_response, sizeof(spdm_finish_response_t));
     244            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     245            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     246              :                                                         m_libspdm_use_asym_algo, &data,
     247              :                                                         &data_size, NULL, NULL);
     248            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     249            1 :         cert_buffer = (uint8_t *)data;
     250            1 :         cert_buffer_size = data_size;
     251            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     252              :                          cert_buffer_hash);
     253              :         /* transcript.message_a size is 0*/
     254            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     255              :         /* session_transcript.message_k is 0*/
     256            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     257              :                                       m_libspdm_local_buffer_size);
     258            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     259            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     260              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     261            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     262              :                          response_finished_key, hash_size, ptr);
     263            1 :         ptr += hmac_size;
     264            1 :         free(data);
     265              : 
     266            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     267              :                                               false, spdm_response_size,
     268              :                                               spdm_response, response_size,
     269              :                                               response);
     270              :     }
     271            1 :         return LIBSPDM_STATUS_SUCCESS;
     272              : 
     273            0 :     case 0x3: {
     274              :         spdm_finish_response_t *spdm_response;
     275              :         uint32_t hash_size;
     276              :         uint32_t hmac_size;
     277              :         uint8_t *ptr;
     278              :         void *data;
     279              :         size_t data_size;
     280              :         uint8_t *cert_buffer;
     281              :         size_t cert_buffer_size;
     282              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     283              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     284              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     285              :         size_t spdm_response_size;
     286              :         size_t transport_header_size;
     287              : 
     288              :         ((libspdm_context_t *)spdm_context)
     289            0 :         ->connection_info.algorithm.base_asym_algo =
     290              :             m_libspdm_use_asym_algo;
     291              :         ((libspdm_context_t *)spdm_context)
     292            0 :         ->connection_info.algorithm.base_hash_algo =
     293              :             m_libspdm_use_hash_algo;
     294              :         ((libspdm_context_t *)spdm_context)
     295            0 :         ->connection_info.algorithm.dhe_named_group =
     296              :             m_libspdm_use_dhe_algo;
     297              :         ((libspdm_context_t *)spdm_context)
     298            0 :         ->connection_info.algorithm.measurement_hash_algo =
     299              :             m_libspdm_use_measurement_hash_algo;
     300            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     301            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     302            0 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     303            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     304            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     305              : 
     306            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     307            0 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
     308            0 :         spdm_response->header.param1 = 0;
     309            0 :         spdm_response->header.param2 = 0;
     310            0 :         ptr = (void *)(spdm_response + 1);
     311            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     312              :                          sizeof(m_libspdm_local_buffer)
     313            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     314              :                             m_libspdm_local_buffer),
     315              :                          spdm_response, sizeof(spdm_finish_response_t));
     316            0 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     317            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     318              :                                                         m_libspdm_use_asym_algo, &data,
     319              :                                                         &data_size, NULL, NULL);
     320            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     321            0 :         cert_buffer = (uint8_t *)data;
     322            0 :         cert_buffer_size = data_size;
     323            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     324              :                          cert_buffer_hash);
     325              :         /* transcript.message_a size is 0*/
     326            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     327              :         /* session_transcript.message_k is 0*/
     328            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     329              :                                       m_libspdm_local_buffer_size);
     330            0 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     331            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     332              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     333            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     334              :                          response_finished_key, hash_size, ptr);
     335            0 :         ptr += hmac_size;
     336            0 :         free(data);
     337              : 
     338            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     339              :                                               false, spdm_response_size,
     340              :                                               spdm_response, response_size,
     341              :                                               response);
     342              :     }
     343            0 :         return LIBSPDM_STATUS_SUCCESS;
     344              : 
     345            1 :     case 0x4: {
     346              :         spdm_error_response_t *spdm_response;
     347              :         size_t spdm_response_size;
     348              :         size_t transport_header_size;
     349              : 
     350            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     351            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     352            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     353              : 
     354            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     355            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     356            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
     357            1 :         spdm_response->header.param2 = 0;
     358              : 
     359            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     360              :                                               false, spdm_response_size,
     361              :                                               spdm_response,
     362              :                                               response_size, response);
     363              :     }
     364            1 :         return LIBSPDM_STATUS_SUCCESS;
     365              : 
     366            1 :     case 0x5: {
     367              :         spdm_error_response_t *spdm_response;
     368              :         size_t spdm_response_size;
     369              :         size_t transport_header_size;
     370              : 
     371            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     372            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     373            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     374              : 
     375            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     376            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     377            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     378            1 :         spdm_response->header.param2 = 0;
     379              : 
     380            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     381              :                                               false, spdm_response_size,
     382              :                                               spdm_response,
     383              :                                               response_size, response);
     384              :     }
     385            1 :         return LIBSPDM_STATUS_SUCCESS;
     386              : 
     387            2 :     case 0x6: {
     388              :         static size_t sub_index1 = 0;
     389            2 :         if (sub_index1 == 0) {
     390              :             spdm_error_response_t *spdm_response;
     391              :             size_t spdm_response_size;
     392              :             size_t transport_header_size;
     393              : 
     394            1 :             spdm_response_size = sizeof(spdm_error_response_t);
     395            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     396            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     397              : 
     398            1 :             spdm_response->header.spdm_version =
     399              :                 SPDM_MESSAGE_VERSION_11;
     400            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     401            1 :             spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     402            1 :             spdm_response->header.param2 = 0;
     403              : 
     404            1 :             libspdm_transport_test_encode_message(
     405              :                 spdm_context, NULL, false, false,
     406              :                 spdm_response_size, spdm_response,
     407              :                 response_size, response);
     408            1 :             sub_index1++;
     409            1 :         } else if (sub_index1 == 1) {
     410              :             spdm_finish_response_t *spdm_response;
     411              :             uint32_t hash_size;
     412              :             uint32_t hmac_size;
     413              :             uint8_t *ptr;
     414              :             void *data;
     415              :             size_t data_size;
     416              :             uint8_t *cert_buffer;
     417              :             size_t cert_buffer_size;
     418              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     419              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     420              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     421              :             size_t spdm_response_size;
     422              :             size_t transport_header_size;
     423              : 
     424              :             ((libspdm_context_t *)spdm_context)
     425            1 :             ->connection_info.algorithm.base_asym_algo =
     426              :                 m_libspdm_use_asym_algo;
     427              :             ((libspdm_context_t *)spdm_context)
     428            1 :             ->connection_info.algorithm.base_hash_algo =
     429              :                 m_libspdm_use_hash_algo;
     430              :             ((libspdm_context_t *)spdm_context)
     431            1 :             ->connection_info.algorithm.dhe_named_group =
     432              :                 m_libspdm_use_dhe_algo;
     433              :             ((libspdm_context_t *)spdm_context)
     434              :             ->connection_info.algorithm
     435            1 :             .measurement_hash_algo =
     436              :                 m_libspdm_use_measurement_hash_algo;
     437            1 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     438            1 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     439            1 :             spdm_response_size =
     440            1 :                 sizeof(spdm_finish_response_t) + hmac_size;
     441            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     442            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     443              : 
     444            1 :             spdm_response->header.spdm_version =
     445              :                 SPDM_MESSAGE_VERSION_11;
     446            1 :             spdm_response->header.request_response_code =
     447              :                 SPDM_FINISH_RSP;
     448            1 :             spdm_response->header.param1 = 0;
     449            1 :             spdm_response->header.param2 = 0;
     450            1 :             ptr = (void *)(spdm_response + 1);
     451            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     452              :                              sizeof(m_libspdm_local_buffer)
     453            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     454              :                                 m_libspdm_local_buffer),
     455              :                              spdm_response, sizeof(spdm_finish_response_t));
     456            1 :             m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     457            1 :             libspdm_read_responder_public_certificate_chain(
     458              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
     459              :                 &data_size, NULL, NULL);
     460            1 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     461            1 :             cert_buffer = (uint8_t *)data;
     462            1 :             cert_buffer_size = data_size;
     463            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
     464              :                              cert_buffer_size, cert_buffer_hash);
     465              :             /* transcript.message_a size is 0*/
     466            1 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
     467              :                                           hash_size);
     468              :             /* session_transcript.message_k is 0*/
     469            1 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     470              :                                           m_libspdm_local_buffer_size);
     471            1 :             libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE,
     472              :                             (uint8_t)(0xFF));
     473            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     474              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
     475            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     476              :                              response_finished_key, hash_size, ptr);
     477            1 :             ptr += hmac_size;
     478            1 :             free(data);
     479              : 
     480            1 :             libspdm_transport_test_encode_message(
     481              :                 spdm_context, NULL, false, false, spdm_response_size,
     482              :                 spdm_response, response_size, response);
     483              :         }
     484              :     }
     485            2 :         return LIBSPDM_STATUS_SUCCESS;
     486              : 
     487            1 :     case 0x7: {
     488              :         spdm_error_response_t *spdm_response;
     489              :         size_t spdm_response_size;
     490              :         size_t transport_header_size;
     491              : 
     492            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     493            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     494            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     495              : 
     496            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
     497            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     498            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
     499            1 :         spdm_response->header.param2 = 0;
     500              : 
     501            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     502              :                                               false, spdm_response_size,
     503              :                                               spdm_response,
     504              :                                               response_size, response);
     505              :     }
     506            1 :         return LIBSPDM_STATUS_SUCCESS;
     507              : 
     508            2 :     case 0x8: {
     509              :         spdm_error_response_data_response_not_ready_t *spdm_response;
     510              :         size_t spdm_response_size;
     511              :         size_t transport_header_size;
     512              : 
     513            2 :         spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     514            2 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     515            2 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     516              : 
     517            2 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     518            2 :         spdm_response->header.request_response_code = SPDM_ERROR;
     519            2 :         spdm_response->header.param1 =
     520              :             SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     521            2 :         spdm_response->header.param2 = 0;
     522            2 :         spdm_response->extend_error_data.rd_exponent = 1;
     523            2 :         spdm_response->extend_error_data.rd_tm = 2;
     524            2 :         spdm_response->extend_error_data.request_code = SPDM_FINISH;
     525            2 :         spdm_response->extend_error_data.token = 0;
     526              : 
     527            2 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     528              :                                               false, spdm_response_size,
     529              :                                               spdm_response,
     530              :                                               response_size, response);
     531              :     }
     532            2 :         return LIBSPDM_STATUS_SUCCESS;
     533              : 
     534            2 :     case 0x9: {
     535              :         static size_t sub_index2 = 0;
     536            2 :         if (sub_index2 == 0) {
     537              :             spdm_error_response_data_response_not_ready_t
     538              :             *spdm_response;
     539              :             size_t spdm_response_size;
     540              :             size_t transport_header_size;
     541              : 
     542            1 :             spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     543            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     544            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     545              : 
     546            1 :             spdm_response->header.spdm_version =
     547              :                 SPDM_MESSAGE_VERSION_11;
     548            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     549            1 :             spdm_response->header.param1 =
     550              :                 SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     551            1 :             spdm_response->header.param2 = 0;
     552            1 :             spdm_response->extend_error_data.rd_exponent = 1;
     553            1 :             spdm_response->extend_error_data.rd_tm = 2;
     554            1 :             spdm_response->extend_error_data.request_code =
     555              :                 SPDM_FINISH;
     556            1 :             spdm_response->extend_error_data.token = 1;
     557              : 
     558            1 :             libspdm_transport_test_encode_message(
     559              :                 spdm_context, NULL, false, false,
     560              :                 spdm_response_size, spdm_response,
     561              :                 response_size, response);
     562            1 :             sub_index2++;
     563            1 :         } else if (sub_index2 == 1) {
     564              :             spdm_finish_response_t *spdm_response;
     565              :             uint32_t hash_size;
     566              :             uint32_t hmac_size;
     567              :             uint8_t *ptr;
     568              :             void *data;
     569              :             size_t data_size;
     570              :             uint8_t *cert_buffer;
     571              :             size_t cert_buffer_size;
     572              :             uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     573              :             uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     574              :             uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     575              :             size_t spdm_response_size;
     576              :             size_t transport_header_size;
     577              : 
     578              :             ((libspdm_context_t *)spdm_context)
     579            1 :             ->connection_info.algorithm.base_asym_algo =
     580              :                 m_libspdm_use_asym_algo;
     581              :             ((libspdm_context_t *)spdm_context)
     582            1 :             ->connection_info.algorithm.base_hash_algo =
     583              :                 m_libspdm_use_hash_algo;
     584              :             ((libspdm_context_t *)spdm_context)
     585            1 :             ->connection_info.algorithm.dhe_named_group =
     586              :                 m_libspdm_use_dhe_algo;
     587              :             ((libspdm_context_t *)spdm_context)
     588              :             ->connection_info.algorithm
     589            1 :             .measurement_hash_algo =
     590              :                 m_libspdm_use_measurement_hash_algo;
     591            1 :             hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     592            1 :             hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     593            1 :             spdm_response_size =
     594            1 :                 sizeof(spdm_finish_response_t) + hmac_size;
     595            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     596            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     597              : 
     598            1 :             spdm_response->header.spdm_version =
     599              :                 SPDM_MESSAGE_VERSION_11;
     600            1 :             spdm_response->header.request_response_code =
     601              :                 SPDM_FINISH_RSP;
     602            1 :             spdm_response->header.param1 = 0;
     603            1 :             spdm_response->header.param2 = 0;
     604            1 :             ptr = (void *)(spdm_response + 1);
     605            1 :             libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     606              :                              sizeof(m_libspdm_local_buffer)
     607            1 :                              - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     608              :                                 m_libspdm_local_buffer),
     609              :                              spdm_response, sizeof(spdm_finish_response_t));
     610            1 :             m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     611            1 :             libspdm_read_responder_public_certificate_chain(
     612              :                 m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
     613              :                 &data_size, NULL, NULL);
     614            1 :             libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     615            1 :             cert_buffer = (uint8_t *)data;
     616            1 :             cert_buffer_size = data_size;
     617            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
     618              :                              cert_buffer_size, cert_buffer_hash);
     619              :             /* transcript.message_a size is 0*/
     620            1 :             libspdm_append_managed_buffer(&th_curr, cert_buffer_hash,
     621              :                                           hash_size);
     622              :             /* session_transcript.message_k is 0*/
     623            1 :             libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     624              :                                           m_libspdm_local_buffer_size);
     625            1 :             libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE,
     626              :                             (uint8_t)(0xFF));
     627            1 :             libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     628              :                              libspdm_get_managed_buffer_size(&th_curr), hash_data);
     629            1 :             libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     630              :                              response_finished_key, hash_size, ptr);
     631            1 :             ptr += hmac_size;
     632            1 :             free(data);
     633              : 
     634            1 :             libspdm_transport_test_encode_message(
     635              :                 spdm_context, NULL, false, false, spdm_response_size,
     636              :                 spdm_response, response_size, response);
     637              :         }
     638              :     }
     639            2 :         return LIBSPDM_STATUS_SUCCESS;
     640              : 
     641           18 :     case 0xA:
     642              :     {
     643              :         static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
     644              : 
     645              :         spdm_error_response_t *spdm_response;
     646              :         size_t spdm_response_size;
     647              :         size_t transport_header_size;
     648              : 
     649           18 :         spdm_response_size = sizeof(spdm_error_response_t);
     650           18 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     651           18 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     652              : 
     653           18 :         if(error_code <= 0xff) {
     654           18 :             libspdm_zero_mem (spdm_response, spdm_response_size);
     655           18 :             spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     656           18 :             spdm_response->header.request_response_code = SPDM_ERROR;
     657           18 :             spdm_response->header.param1 = (uint8_t) error_code;
     658           18 :             spdm_response->header.param2 = 0;
     659              : 
     660           18 :             libspdm_transport_test_encode_message (spdm_context, NULL, false, false,
     661              :                                                    spdm_response_size, spdm_response,
     662              :                                                    response_size, response);
     663              :         }
     664              : 
     665           18 :         error_code++;
     666           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
     667            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
     668              :         }
     669           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
     670            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
     671              :         }
     672           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
     673            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
     674              :         }
     675              :     }
     676           18 :         return LIBSPDM_STATUS_SUCCESS;
     677            1 :     case 0xB:
     678              :     {
     679              :         spdm_finish_response_t *spdm_response;
     680              :         uint32_t hash_size;
     681              :         uint32_t hmac_size;
     682              :         uint8_t *ptr;
     683              :         void *data;
     684              :         size_t data_size;
     685              :         uint8_t *cert_buffer;
     686              :         size_t cert_buffer_size;
     687              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     688              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     689              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     690              :         size_t spdm_response_size;
     691              :         size_t transport_header_size;
     692              : 
     693              :         ((libspdm_context_t *)spdm_context)
     694            1 :         ->connection_info.algorithm.base_asym_algo =
     695              :             m_libspdm_use_asym_algo;
     696              :         ((libspdm_context_t *)spdm_context)
     697            1 :         ->connection_info.algorithm.base_hash_algo =
     698              :             m_libspdm_use_hash_algo;
     699              :         ((libspdm_context_t *)spdm_context)
     700            1 :         ->connection_info.algorithm.dhe_named_group =
     701              :             m_libspdm_use_dhe_algo;
     702              :         ((libspdm_context_t *)spdm_context)
     703            1 :         ->connection_info.algorithm.measurement_hash_algo =
     704              :             m_libspdm_use_measurement_hash_algo;
     705            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     706            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     707            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     708            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     709            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     710              : 
     711            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     712            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
     713            1 :         spdm_response->header.param1 = 0;
     714            1 :         spdm_response->header.param2 = 0;
     715            1 :         ptr = (void *)(spdm_response + 1);
     716            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     717              :                          sizeof(m_libspdm_local_buffer)
     718            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     719              :                             m_libspdm_local_buffer),
     720              :                          spdm_response, sizeof(spdm_finish_response_t));
     721            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     722            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     723              :                                                         m_libspdm_use_asym_algo, &data,
     724              :                                                         &data_size, NULL, NULL);
     725            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     726            1 :         cert_buffer = (uint8_t *)data;
     727            1 :         cert_buffer_size = data_size;
     728            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     729              :                          cert_buffer_hash);
     730              :         /* transcript.message_a size is 0*/
     731            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     732              :         /* session_transcript.message_k is 0*/
     733            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     734              :                                       m_libspdm_local_buffer_size);
     735            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     736            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     737              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     738            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     739              :                          response_finished_key, hash_size, ptr);
     740            1 :         ptr += hmac_size;
     741            1 :         free(data);
     742              : 
     743            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     744              :                                               false, spdm_response_size,
     745              :                                               spdm_response, response_size,
     746              :                                               response);
     747              :     }
     748            1 :         return LIBSPDM_STATUS_SUCCESS;
     749              : 
     750            0 :     case 0xC: {
     751              :         spdm_finish_response_t *spdm_response;
     752              :         uint32_t hash_size;
     753              :         uint32_t hmac_size;
     754              :         uint8_t *ptr;
     755              :         void *data;
     756              :         size_t data_size;
     757              :         uint8_t *cert_buffer;
     758              :         size_t cert_buffer_size;
     759              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     760              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     761              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     762              :         size_t spdm_response_size;
     763              :         size_t transport_header_size;
     764              : 
     765              :         ((libspdm_context_t *)spdm_context)
     766            0 :         ->connection_info.algorithm.base_asym_algo =
     767              :             m_libspdm_use_asym_algo;
     768              :         ((libspdm_context_t *)spdm_context)
     769            0 :         ->connection_info.algorithm.base_hash_algo =
     770              :             m_libspdm_use_hash_algo;
     771              :         ((libspdm_context_t *)spdm_context)
     772            0 :         ->connection_info.algorithm.dhe_named_group =
     773              :             m_libspdm_use_dhe_algo;
     774              :         ((libspdm_context_t *)spdm_context)
     775            0 :         ->connection_info.algorithm.measurement_hash_algo =
     776              :             m_libspdm_use_measurement_hash_algo;
     777            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     778            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     779            0 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     780            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     781            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     782              : 
     783            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     784            0 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
     785            0 :         spdm_response->header.param1 = 0;
     786            0 :         spdm_response->header.param2 = 0;
     787            0 :         ptr = (void *)(spdm_response + 1);
     788            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     789              :                          sizeof(m_libspdm_local_buffer)
     790            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     791              :                             m_libspdm_local_buffer),
     792              :                          spdm_response, sizeof(spdm_finish_response_t));
     793            0 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     794            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     795              :                                                         m_libspdm_use_asym_algo, &data,
     796              :                                                         &data_size, NULL, NULL);
     797            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     798            0 :         cert_buffer = (uint8_t *)data;
     799            0 :         cert_buffer_size = data_size;
     800            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     801              :                          cert_buffer_hash);
     802              :         /* transcript.message_a size is 0*/
     803            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     804              :         /* session_transcript.message_k is 0*/
     805            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     806              :                                       m_libspdm_local_buffer_size);
     807            0 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     808            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     809              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     810            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     811              :                          response_finished_key, hash_size, ptr);
     812            0 :         ptr += hmac_size;
     813            0 :         free(data);
     814              : 
     815            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     816              :                                               false, spdm_response_size,
     817              :                                               spdm_response, response_size,
     818              :                                               response);
     819              :     }
     820            0 :         return LIBSPDM_STATUS_SUCCESS;
     821              : 
     822            0 :     case 0xD: {
     823              :         spdm_finish_response_t *spdm_response;
     824              :         uint32_t hash_size;
     825              :         uint32_t hmac_size;
     826              :         uint8_t *ptr;
     827              :         void *data;
     828              :         size_t data_size;
     829              :         uint8_t *cert_buffer;
     830              :         size_t cert_buffer_size;
     831              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     832              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     833              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     834              :         size_t spdm_response_size;
     835              :         size_t transport_header_size;
     836              : 
     837              :         ((libspdm_context_t *)spdm_context)
     838            0 :         ->connection_info.algorithm.base_asym_algo =
     839              :             m_libspdm_use_asym_algo;
     840              :         ((libspdm_context_t *)spdm_context)
     841            0 :         ->connection_info.algorithm.base_hash_algo =
     842              :             m_libspdm_use_hash_algo;
     843              :         ((libspdm_context_t *)spdm_context)
     844            0 :         ->connection_info.algorithm.dhe_named_group =
     845              :             m_libspdm_use_dhe_algo;
     846              :         ((libspdm_context_t *)spdm_context)
     847            0 :         ->connection_info.algorithm.measurement_hash_algo =
     848              :             m_libspdm_use_measurement_hash_algo;
     849            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     850            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     851            0 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     852            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     853            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     854              : 
     855            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     856            0 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
     857            0 :         spdm_response->header.param1 = 0;
     858            0 :         spdm_response->header.param2 = 0;
     859            0 :         ptr = (void *)(spdm_response + 1);
     860            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     861              :                          sizeof(m_libspdm_local_buffer)
     862            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     863              :                             m_libspdm_local_buffer),
     864              :                          spdm_response, sizeof(spdm_finish_response_t));
     865            0 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     866            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     867              :                                                         m_libspdm_use_asym_algo, &data,
     868              :                                                         &data_size, NULL, NULL);
     869            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     870            0 :         cert_buffer = (uint8_t *)data;
     871            0 :         cert_buffer_size = data_size;
     872            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     873              :                          cert_buffer_hash);
     874              :         /* transcript.message_a size is 0*/
     875            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     876              :         /* session_transcript.message_k is 0*/
     877            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     878              :                                       m_libspdm_local_buffer_size);
     879            0 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     880            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     881              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     882            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     883              :                          response_finished_key, hash_size, ptr);
     884            0 :         ptr += hmac_size;
     885            0 :         free(data);
     886              : 
     887            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     888              :                                               false, spdm_response_size,
     889              :                                               spdm_response, response_size,
     890              :                                               response);
     891              :     }
     892            0 :         return LIBSPDM_STATUS_SUCCESS;
     893              : 
     894            1 :     case 0xE: {
     895              :         spdm_finish_response_t *spdm_response;
     896              :         uint32_t hash_size;
     897              :         uint32_t hmac_size;
     898              :         uint8_t *ptr;
     899              :         void *data;
     900              :         size_t data_size;
     901              :         uint8_t *cert_buffer;
     902              :         size_t cert_buffer_size;
     903              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     904              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     905              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     906              :         size_t spdm_response_size;
     907              :         size_t transport_header_size;
     908              : 
     909              :         ((libspdm_context_t *)spdm_context)
     910            1 :         ->connection_info.algorithm.base_asym_algo =
     911              :             m_libspdm_use_asym_algo;
     912              :         ((libspdm_context_t *)spdm_context)
     913            1 :         ->connection_info.algorithm.base_hash_algo =
     914              :             m_libspdm_use_hash_algo;
     915              :         ((libspdm_context_t *)spdm_context)
     916            1 :         ->connection_info.algorithm.dhe_named_group =
     917              :             m_libspdm_use_dhe_algo;
     918              :         ((libspdm_context_t *)spdm_context)
     919            1 :         ->connection_info.algorithm.measurement_hash_algo =
     920              :             m_libspdm_use_measurement_hash_algo;
     921            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     922            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     923            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     924            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     925            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     926              : 
     927            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     928              :         /*wrong response code*/
     929            1 :         spdm_response->header.request_response_code = SPDM_FINISH;
     930            1 :         spdm_response->header.param1 = 0;
     931            1 :         spdm_response->header.param2 = 0;
     932            1 :         ptr = (void *)(spdm_response + 1);
     933            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     934              :                          sizeof(m_libspdm_local_buffer)
     935            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
     936              :                             m_libspdm_local_buffer),
     937              :                          spdm_response, sizeof(spdm_finish_response_t));
     938            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
     939            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     940              :                                                         m_libspdm_use_asym_algo, &data,
     941              :                                                         &data_size, NULL, NULL);
     942            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
     943            1 :         cert_buffer = (uint8_t *)data;
     944            1 :         cert_buffer_size = data_size;
     945            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
     946              :                          cert_buffer_hash);
     947              :         /* transcript.message_a size is 0*/
     948            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
     949              :         /* session_transcript.message_k is 0*/
     950            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
     951              :                                       m_libspdm_local_buffer_size);
     952            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
     953            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
     954              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
     955            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
     956              :                          response_finished_key, hash_size, ptr);
     957            1 :         ptr += hmac_size;
     958            1 :         free(data);
     959              : 
     960            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     961              :                                               false, spdm_response_size,
     962              :                                               spdm_response, response_size,
     963              :                                               response);
     964              :     }
     965            1 :         return LIBSPDM_STATUS_SUCCESS;
     966              : 
     967            0 :     case 0xF: {
     968              :         spdm_finish_response_t *spdm_response;
     969              :         uint32_t hash_size;
     970              :         uint32_t hmac_size;
     971              :         uint8_t *ptr;
     972              :         void *data;
     973              :         size_t data_size;
     974              :         uint8_t *cert_buffer;
     975              :         size_t cert_buffer_size;
     976              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
     977              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
     978              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
     979              :         size_t spdm_response_size;
     980              :         size_t transport_header_size;
     981              : 
     982              :         ((libspdm_context_t *)spdm_context)
     983            0 :         ->connection_info.algorithm.base_asym_algo =
     984              :             m_libspdm_use_asym_algo;
     985              :         ((libspdm_context_t *)spdm_context)
     986            0 :         ->connection_info.algorithm.base_hash_algo =
     987              :             m_libspdm_use_hash_algo;
     988              :         ((libspdm_context_t *)spdm_context)
     989            0 :         ->connection_info.algorithm.dhe_named_group =
     990              :             m_libspdm_use_dhe_algo;
     991              :         ((libspdm_context_t *)spdm_context)
     992            0 :         ->connection_info.algorithm.measurement_hash_algo =
     993              :             m_libspdm_use_measurement_hash_algo;
     994            0 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     995            0 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
     996            0 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
     997            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     998            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     999              : 
    1000            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1001            0 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1002            0 :         spdm_response->header.param1 = 0;
    1003            0 :         spdm_response->header.param2 = 0;
    1004            0 :         ptr = (void *)(spdm_response + 1);
    1005            0 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1006              :                          sizeof(m_libspdm_local_buffer)
    1007            0 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1008              :                             m_libspdm_local_buffer),
    1009              :                          spdm_response, sizeof(spdm_finish_response_t));
    1010            0 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
    1011            0 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1012              :                                                         m_libspdm_use_asym_algo, &data,
    1013              :                                                         &data_size, NULL, NULL);
    1014            0 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1015            0 :         cert_buffer = (uint8_t *)data;
    1016            0 :         cert_buffer_size = data_size;
    1017            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1018              :                          cert_buffer_hash);
    1019              :         /* transcript.message_a size is 0*/
    1020            0 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1021              :         /* session_transcript.message_k is 0*/
    1022            0 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1023              :                                       m_libspdm_local_buffer_size);
    1024            0 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1025            0 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1026              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1027            0 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1028              :                          response_finished_key, hash_size, ptr);
    1029            0 :         ptr += hmac_size;
    1030            0 :         free(data);
    1031              : 
    1032            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1033              :                                               false, spdm_response_size,
    1034              :                                               spdm_response, response_size,
    1035              :                                               response);
    1036              :     }
    1037            0 :         return LIBSPDM_STATUS_SUCCESS;
    1038              : 
    1039            1 :     case 0x10: {
    1040              :         spdm_finish_response_t *spdm_response;
    1041              :         uint32_t hash_size;
    1042              :         uint32_t hmac_size;
    1043              :         uint8_t *ptr;
    1044              :         void *data;
    1045              :         size_t data_size;
    1046              :         uint8_t *cert_buffer;
    1047              :         size_t cert_buffer_size;
    1048              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1049              :         uint8_t req_cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1050              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1051              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1052              :         size_t spdm_response_size;
    1053              :         size_t transport_header_size;
    1054              : 
    1055              :         ((libspdm_context_t *)spdm_context)
    1056            1 :         ->connection_info.algorithm.base_asym_algo =
    1057              :             m_libspdm_use_asym_algo;
    1058              :         ((libspdm_context_t *)spdm_context)
    1059            1 :         ->connection_info.algorithm.req_base_asym_alg =
    1060              :             m_libspdm_use_req_asym_algo;
    1061              :         ((libspdm_context_t *)spdm_context)
    1062            1 :         ->connection_info.algorithm.base_hash_algo =
    1063              :             m_libspdm_use_hash_algo;
    1064              :         ((libspdm_context_t *)spdm_context)
    1065            1 :         ->connection_info.algorithm.dhe_named_group =
    1066              :             m_libspdm_use_dhe_algo;
    1067              :         ((libspdm_context_t *)spdm_context)
    1068            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1069              :             m_libspdm_use_measurement_hash_algo;
    1070            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1071            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1072            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
    1073            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1074            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1075              : 
    1076            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1077            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1078            1 :         spdm_response->header.param1 = 0;
    1079            1 :         spdm_response->header.param2 = 0;
    1080            1 :         ptr = (void *)(spdm_response + 1);
    1081            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1082              :                          sizeof(m_libspdm_local_buffer)
    1083            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1084              :                             m_libspdm_local_buffer),
    1085              :                          spdm_response, sizeof(spdm_finish_response_t));
    1086            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
    1087            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1088              :                                                         m_libspdm_use_asym_algo, &data,
    1089              :                                                         &data_size, NULL, NULL);
    1090            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1091            1 :         cert_buffer = (uint8_t *)data;
    1092            1 :         cert_buffer_size = data_size;
    1093            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1094              :                          cert_buffer_hash);
    1095            1 :         libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    1096              :                                                         m_libspdm_use_req_asym_algo, &data,
    1097              :                                                         &data_size, NULL, NULL);
    1098            1 :         cert_buffer = (uint8_t *)data;
    1099            1 :         cert_buffer_size = data_size;
    1100            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1101              :                          req_cert_buffer_hash);
    1102              :         /* transcript.message_a size is 0*/
    1103            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1104              :         /* session_transcript.message_k is 0*/
    1105            1 :         libspdm_append_managed_buffer(&th_curr, req_cert_buffer_hash,
    1106              :                                       hash_size);
    1107            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1108              :                                       m_libspdm_local_buffer_size);
    1109            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1110            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1111              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1112            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1113              :                          response_finished_key, hash_size, ptr);
    1114            1 :         ptr += hmac_size;
    1115            1 :         free(data);
    1116              : 
    1117            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1118              :                                               false, spdm_response_size,
    1119              :                                               spdm_response, response_size,
    1120              :                                               response);
    1121              :     }
    1122            1 :         return LIBSPDM_STATUS_SUCCESS;
    1123              : 
    1124            1 :     case 0x11: {
    1125              :         spdm_finish_response_t *spdm_response;
    1126              :         uint32_t hmac_size;
    1127              :         uint8_t *ptr;
    1128              :         size_t spdm_response_size;
    1129              :         size_t transport_header_size;
    1130              : 
    1131              :         ((libspdm_context_t *)spdm_context)
    1132            1 :         ->connection_info.algorithm.base_asym_algo =
    1133              :             m_libspdm_use_asym_algo;
    1134              :         ((libspdm_context_t *)spdm_context)
    1135            1 :         ->connection_info.algorithm.req_base_asym_alg =
    1136              :             m_libspdm_use_req_asym_algo;
    1137              :         ((libspdm_context_t *)spdm_context)
    1138            1 :         ->connection_info.algorithm.base_hash_algo =
    1139              :             m_libspdm_use_hash_algo;
    1140              :         ((libspdm_context_t *)spdm_context)
    1141            1 :         ->connection_info.algorithm.dhe_named_group =
    1142              :             m_libspdm_use_dhe_algo;
    1143              :         ((libspdm_context_t *)spdm_context)
    1144            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1145              :             m_libspdm_use_measurement_hash_algo;
    1146            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1147            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
    1148            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1149            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1150              : 
    1151            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1152            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1153            1 :         spdm_response->header.param1 = 0;
    1154            1 :         spdm_response->header.param2 = 0;
    1155            1 :         ptr = (void *)(spdm_response + 1);
    1156            1 :         libspdm_set_mem(ptr, hmac_size, (uint8_t)(0x00)); /*all-zero MAC*/
    1157            1 :         ptr += hmac_size;
    1158              : 
    1159            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1160              :                                               false, spdm_response_size,
    1161              :                                               spdm_response, response_size,
    1162              :                                               response);
    1163              :     }
    1164            1 :         return LIBSPDM_STATUS_SUCCESS;
    1165              : 
    1166            1 :     case 0x12: {
    1167              :         spdm_finish_response_t *spdm_response;
    1168              :         uint32_t hash_size;
    1169              :         uint32_t hmac_size;
    1170              :         uint8_t *ptr;
    1171              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1172              :         uint8_t zero_data[LIBSPDM_MAX_HASH_SIZE];
    1173              :         size_t spdm_response_size;
    1174              :         size_t transport_header_size;
    1175              : 
    1176              :         ((libspdm_context_t *)spdm_context)
    1177            1 :         ->connection_info.algorithm.base_asym_algo =
    1178              :             m_libspdm_use_asym_algo;
    1179              :         ((libspdm_context_t *)spdm_context)
    1180            1 :         ->connection_info.algorithm.req_base_asym_alg =
    1181              :             m_libspdm_use_req_asym_algo;
    1182              :         ((libspdm_context_t *)spdm_context)
    1183            1 :         ->connection_info.algorithm.base_hash_algo =
    1184              :             m_libspdm_use_hash_algo;
    1185              :         ((libspdm_context_t *)spdm_context)
    1186            1 :         ->connection_info.algorithm.dhe_named_group =
    1187              :             m_libspdm_use_dhe_algo;
    1188              :         ((libspdm_context_t *)spdm_context)
    1189            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1190              :             m_libspdm_use_measurement_hash_algo;
    1191            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1192            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1193            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
    1194            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1195            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1196              : 
    1197            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1198            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1199            1 :         spdm_response->header.param1 = 0;
    1200            1 :         spdm_response->header.param2 = 0;
    1201            1 :         ptr = (void *)(spdm_response + 1);
    1202            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1203            1 :         libspdm_set_mem(zero_data, hash_size, (uint8_t)(0x00));
    1204            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, zero_data, hash_size,
    1205              :                          response_finished_key, hash_size, ptr);
    1206            1 :         ptr += hmac_size;
    1207              : 
    1208            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1209              :                                               false, spdm_response_size,
    1210              :                                               spdm_response, response_size,
    1211              :                                               response);
    1212              :     }
    1213            1 :         return LIBSPDM_STATUS_SUCCESS;
    1214              : 
    1215            0 :     case 0x13: {
    1216              :     }
    1217            0 :         return LIBSPDM_STATUS_SUCCESS;
    1218              : 
    1219            1 :     case 0x14: {
    1220              :         spdm_finish_response_t *spdm_response;
    1221              :         uint32_t hash_size;
    1222              :         uint32_t hmac_size;
    1223              :         uint8_t *ptr;
    1224              :         void *data;
    1225              :         size_t data_size;
    1226              :         uint8_t *cert_buffer;
    1227              :         size_t cert_buffer_size;
    1228              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1229              :         uint8_t req_cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1230              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1231              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1232              :         size_t spdm_response_size;
    1233              :         size_t transport_header_size;
    1234              : 
    1235              :         ((libspdm_context_t *)spdm_context)
    1236            1 :         ->connection_info.algorithm.base_asym_algo =
    1237              :             m_libspdm_use_asym_algo;
    1238              :         ((libspdm_context_t *)spdm_context)
    1239            1 :         ->connection_info.algorithm.req_base_asym_alg =
    1240              :             m_libspdm_use_req_asym_algo;
    1241              :         ((libspdm_context_t *)spdm_context)
    1242            1 :         ->connection_info.algorithm.base_hash_algo =
    1243              :             m_libspdm_use_hash_algo;
    1244              :         ((libspdm_context_t *)spdm_context)
    1245            1 :         ->connection_info.algorithm.dhe_named_group =
    1246              :             m_libspdm_use_dhe_algo;
    1247              :         ((libspdm_context_t *)spdm_context)
    1248            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1249              :             m_libspdm_use_measurement_hash_algo;
    1250            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1251            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1252            1 :         spdm_response_size = sizeof(spdm_finish_response_t) +
    1253            1 :                              hmac_size/2;/* half HMAC size*/
    1254            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1255            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1256              : 
    1257            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1258            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1259            1 :         spdm_response->header.param1 = 0;
    1260            1 :         spdm_response->header.param2 = 0;
    1261            1 :         ptr = (void *)(spdm_response + 1);
    1262            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1263              :                          sizeof(m_libspdm_local_buffer)
    1264            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1265              :                             m_libspdm_local_buffer),
    1266              :                          spdm_response, sizeof(spdm_finish_response_t));
    1267            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
    1268            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1269              :                                                         m_libspdm_use_asym_algo, &data,
    1270              :                                                         &data_size, NULL, NULL);
    1271            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1272            1 :         cert_buffer = (uint8_t *)data;
    1273            1 :         cert_buffer_size = data_size;
    1274            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1275              :                          cert_buffer_hash);
    1276            1 :         libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    1277              :                                                         m_libspdm_use_req_asym_algo, &data,
    1278              :                                                         &data_size, NULL, NULL);
    1279            1 :         cert_buffer = (uint8_t *)data;
    1280            1 :         cert_buffer_size = data_size;
    1281            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1282              :                          req_cert_buffer_hash);
    1283              :         /* transcript.message_a size is 0*/
    1284            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1285              :         /* session_transcript.message_k is 0*/
    1286            1 :         libspdm_append_managed_buffer(&th_curr, req_cert_buffer_hash,
    1287              :                                       hash_size);
    1288            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1289              :                                       m_libspdm_local_buffer_size);
    1290            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1291            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1292              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1293            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1294              :                          response_finished_key, hash_size, ptr);
    1295            1 :         ptr += hmac_size/2; /* half HMAC size*/
    1296            1 :         libspdm_set_mem(ptr, hmac_size/2, (uint8_t) 0x00);
    1297            1 :         free(data);
    1298              : 
    1299            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1300              :                                               false, spdm_response_size,
    1301              :                                               spdm_response, response_size,
    1302              :                                               response);
    1303              :     }
    1304            1 :         return LIBSPDM_STATUS_SUCCESS;
    1305              : 
    1306            1 :     case 0x15: {
    1307              :         spdm_error_response_t *spdm_response;
    1308              :         size_t spdm_response_size;
    1309              :         size_t transport_header_size;
    1310              : 
    1311            1 :         spdm_response_size = sizeof(spdm_error_response_t);
    1312            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1313            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1314              : 
    1315            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1316            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
    1317            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_DECRYPT_ERROR;
    1318            1 :         spdm_response->header.param2 = 0;
    1319              : 
    1320            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1321              :                                               false, spdm_response_size,
    1322              :                                               spdm_response,
    1323              :                                               response_size, response);
    1324              :     }
    1325            1 :         return LIBSPDM_STATUS_SUCCESS;
    1326              : 
    1327            1 :     case 0x16: {
    1328              :         spdm_finish_response_t *spdm_response;
    1329              :         uint32_t hash_size;
    1330              :         uint32_t hmac_size;
    1331              :         uint8_t *ptr;
    1332              :         void *data;
    1333              :         size_t data_size;
    1334              :         uint8_t *cert_buffer;
    1335              :         size_t cert_buffer_size;
    1336              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1337              :         uint8_t req_cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1338              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1339              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1340              :         size_t spdm_response_size;
    1341              :         size_t transport_header_size;
    1342              : 
    1343            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_asym_algo =
    1344              :             m_libspdm_use_asym_algo;
    1345            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.req_base_asym_alg =
    1346              :             m_libspdm_use_req_asym_algo;
    1347            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
    1348              :             m_libspdm_use_hash_algo;
    1349            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.dhe_named_group =
    1350              :             m_libspdm_use_dhe_algo;
    1351            1 :         ((libspdm_context_t *)spdm_context)->connection_info.algorithm.measurement_hash_algo =
    1352              :             m_libspdm_use_measurement_hash_algo;
    1353            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1354            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1355            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
    1356            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1357            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1358              : 
    1359            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1360            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1361            1 :         spdm_response->header.param1 = 0;
    1362            1 :         spdm_response->header.param2 = 0;
    1363            1 :         ptr = (void *)(spdm_response + 1);
    1364            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1365              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    1366              :                          spdm_response, sizeof(spdm_finish_response_t));
    1367            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
    1368            1 :         libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1369              :                                                         m_libspdm_use_asym_algo, &data,
    1370              :                                                         &data_size, NULL, NULL);
    1371            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1372            1 :         cert_buffer = (uint8_t *)data;
    1373            1 :         cert_buffer_size = data_size;
    1374            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
    1375            1 :         libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    1376              :                                                         m_libspdm_use_req_asym_algo, &data,
    1377              :                                                         &data_size, NULL, NULL);
    1378            1 :         cert_buffer = (uint8_t *)data;
    1379            1 :         cert_buffer_size = data_size;
    1380            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1381              :                          req_cert_buffer_hash);
    1382              :         /* transcript.message_a size is 0*/
    1383            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1384              :         /* session_transcript.message_k is 0*/
    1385            1 :         libspdm_append_managed_buffer(&th_curr, req_cert_buffer_hash, hash_size);
    1386            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1387              :                                       m_libspdm_local_buffer_size);
    1388            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1389            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1390              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1391            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1392              :                          response_finished_key, hash_size, ptr);
    1393            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1394              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
    1395              :                          ptr, hmac_size);
    1396            1 :         m_libspdm_local_buffer_size += hmac_size;
    1397            1 :         ptr += hmac_size;
    1398            1 :         free(data);
    1399              : 
    1400            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1401              :                                               false, spdm_response_size,
    1402              :                                               spdm_response, response_size,
    1403              :                                               response);
    1404              :     }
    1405            1 :         return LIBSPDM_STATUS_SUCCESS;
    1406            1 :     case 0x17: {
    1407              :         spdm_finish_response_t *spdm_response;
    1408              :         uint32_t hash_size;
    1409              :         uint32_t hmac_size;
    1410              :         uint8_t *ptr;
    1411              :         void *data;
    1412              :         size_t data_size;
    1413              :         uint8_t *cert_buffer;
    1414              :         size_t cert_buffer_size;
    1415              :         uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
    1416              :         uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
    1417              :         uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
    1418              :         size_t spdm_response_size;
    1419              :         size_t transport_header_size;
    1420              : 
    1421              :         ((libspdm_context_t *)spdm_context)
    1422            1 :         ->connection_info.algorithm.base_asym_algo =
    1423              :             m_libspdm_use_asym_algo;
    1424              :         ((libspdm_context_t *)spdm_context)
    1425            1 :         ->connection_info.algorithm.base_hash_algo =
    1426              :             m_libspdm_use_hash_algo;
    1427              :         ((libspdm_context_t *)spdm_context)
    1428            1 :         ->connection_info.algorithm.dhe_named_group =
    1429              :             m_libspdm_use_dhe_algo;
    1430              :         ((libspdm_context_t *)spdm_context)
    1431            1 :         ->connection_info.algorithm.measurement_hash_algo =
    1432              :             m_libspdm_use_measurement_hash_algo;
    1433            1 :         hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1434            1 :         hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1435            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + hmac_size;
    1436            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1437            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1438              : 
    1439            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1440            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1441            1 :         spdm_response->header.param1 = 0;
    1442            1 :         spdm_response->header.param2 = 0;
    1443            1 :         ptr = (void *)(spdm_response + 1);
    1444            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
    1445              :                          sizeof(m_libspdm_local_buffer)
    1446            1 :                          - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
    1447              :                             m_libspdm_local_buffer),
    1448              :                          spdm_response, sizeof(spdm_finish_response_t));
    1449            1 :         m_libspdm_local_buffer_size += sizeof(spdm_finish_response_t);
    1450            1 :         libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data, &data_size);
    1451            1 :         libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
    1452            1 :         cert_buffer = (uint8_t *)data;
    1453            1 :         cert_buffer_size = data_size;
    1454            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size,
    1455              :                          cert_buffer_hash);
    1456              :         /* transcript.message_a size is 0*/
    1457            1 :         libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
    1458              :         /* session_transcript.message_k is 0*/
    1459            1 :         libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
    1460              :                                       m_libspdm_local_buffer_size);
    1461            1 :         libspdm_set_mem(response_finished_key, LIBSPDM_MAX_HASH_SIZE, (uint8_t)(0xFF));
    1462            1 :         libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
    1463              :                          libspdm_get_managed_buffer_size(&th_curr), hash_data);
    1464            1 :         libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
    1465              :                          response_finished_key, hash_size, ptr);
    1466            1 :         ptr += hmac_size;
    1467            1 :         free(data);
    1468              : 
    1469            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
    1470              :                                               false, spdm_response_size,
    1471              :                                               spdm_response, response_size,
    1472              :                                               response);
    1473              :     }
    1474            1 :         return LIBSPDM_STATUS_SUCCESS;
    1475            1 :     case 0x18: {
    1476              :         spdm_finish_response_t *spdm_response;
    1477              :         libspdm_session_info_t *session_info;
    1478              :         size_t spdm_response_size;
    1479              :         size_t transport_header_size;
    1480              :         uint32_t session_id;
    1481              :         uint8_t *scratch_buffer;
    1482              :         size_t scratch_buffer_size;
    1483              : 
    1484            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1485              : 
    1486              :         /* The ResponderVerifyData field does absent.*/
    1487            1 :         spdm_response_size = sizeof(spdm_finish_response_t);
    1488              : 
    1489            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1490            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
    1491            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1492            1 :         spdm_response->header.param1 = 0;
    1493            1 :         spdm_response->header.param2 = 0;
    1494              : 
    1495            1 :         session_id = 0xFFFFFFFF;
    1496              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
    1497              :          * transport_message is always in sender buffer. */
    1498            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
    1499            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
    1500              :                           scratch_buffer_size - transport_header_size,
    1501              :                           spdm_response, spdm_response_size);
    1502            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
    1503            1 :         libspdm_transport_test_encode_message (spdm_context, &session_id, false, false,
    1504              :                                                spdm_response_size, spdm_response,
    1505              :                                                response_size, response);
    1506              : 
    1507            1 :         session_info = libspdm_get_session_info_via_session_id (spdm_context, session_id);
    1508            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
    1509            1 :         handshake_secret.response_handshake_sequence_number--;
    1510              :     }
    1511            1 :         return LIBSPDM_STATUS_SUCCESS;
    1512            1 :     case 0x19: {
    1513              :         spdm_finish_response_t *spdm_response;
    1514              :         libspdm_session_info_t *session_info;
    1515              :         size_t spdm_response_size;
    1516              :         size_t transport_header_size;
    1517              :         uint32_t session_id;
    1518              :         uint8_t *scratch_buffer;
    1519              :         size_t scratch_buffer_size;
    1520              :         uint16_t opaque_data_size;
    1521              :         uint8_t *ptr;
    1522              : 
    1523            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
    1524              : 
    1525            1 :         opaque_data_size = 8;
    1526              :         /* The ResponderVerifyData field does absent.*/
    1527            1 :         spdm_response_size = sizeof(spdm_finish_response_t) + sizeof(uint16_t) + opaque_data_size;
    1528              : 
    1529            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
    1530            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_14;
    1531            1 :         spdm_response->header.request_response_code = SPDM_FINISH_RSP;
    1532            1 :         spdm_response->header.param1 = 0;
    1533            1 :         spdm_response->header.param2 = 0;
    1534            1 :         ptr = (uint8_t *)spdm_response + sizeof(spdm_finish_response_t);
    1535            1 :         libspdm_write_uint16(ptr, opaque_data_size);
    1536              : 
    1537            1 :         session_id = 0xFFFFFFFF;
    1538              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
    1539              :          * transport_message is always in sender buffer. */
    1540            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
    1541            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
    1542              :                           scratch_buffer_size - transport_header_size,
    1543              :                           spdm_response, spdm_response_size);
    1544            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
    1545            1 :         libspdm_transport_test_encode_message (spdm_context, &session_id, false, false,
    1546              :                                                spdm_response_size, spdm_response,
    1547              :                                                response_size, response);
    1548              : 
    1549            1 :         session_info = libspdm_get_session_info_via_session_id (spdm_context, session_id);
    1550            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
    1551            1 :         handshake_secret.response_handshake_sequence_number--;
    1552              :     }
    1553            1 :         return LIBSPDM_STATUS_SUCCESS;
    1554              : 
    1555            0 :     default:
    1556            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
    1557              :     }
    1558              : }
    1559              : 
    1560              : /**
    1561              :  * Test 1: when no FINISH_RSP message is received, and the client returns a
    1562              :  * device error.
    1563              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1564              :  **/
    1565            1 : void libspdm_test_requester_finish_case1(void **state)
    1566              : {
    1567              :     libspdm_return_t status;
    1568              :     libspdm_test_context_t *spdm_test_context;
    1569              :     libspdm_context_t *spdm_context;
    1570              :     uint32_t session_id;
    1571              :     uint8_t req_slot_id_param;
    1572              :     void *data;
    1573              :     size_t data_size;
    1574              :     void *hash;
    1575              :     size_t hash_size;
    1576              :     libspdm_session_info_t *session_info;
    1577              : 
    1578            1 :     spdm_test_context = *state;
    1579            1 :     spdm_context = spdm_test_context->spdm_context;
    1580            1 :     spdm_test_context->case_id = 0x1;
    1581            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1582              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1583            1 :     spdm_context->connection_info.connection_state =
    1584              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1585            1 :     spdm_context->connection_info.capability.flags |=
    1586              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1587            1 :     spdm_context->connection_info.capability.flags |=
    1588              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1589            1 :     spdm_context->connection_info.capability.flags |=
    1590              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1591            1 :     spdm_context->local_context.capability.flags |=
    1592              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1593            1 :     spdm_context->local_context.capability.flags |=
    1594              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1595            1 :     spdm_context->local_context.capability.flags |=
    1596              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1597            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1598              :                                                     m_libspdm_use_asym_algo, &data,
    1599              :                                                     &data_size, &hash, &hash_size);
    1600            1 :     libspdm_reset_message_a(spdm_context);
    1601            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1602              :         m_libspdm_use_hash_algo;
    1603            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1604              :         m_libspdm_use_asym_algo;
    1605            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1606              :         m_libspdm_use_dhe_algo;
    1607            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1608              :         m_libspdm_use_aead_algo;
    1609              : 
    1610              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1611              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1612              :         data_size;
    1613              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1614              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1615              :                      data, data_size);
    1616              : #else
    1617            1 :     libspdm_hash_all(
    1618              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1619              :         data, data_size,
    1620            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1621            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1622            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1623            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1624              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1625              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1626              :         data, data_size,
    1627              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1628              : #endif
    1629            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1630              : 
    1631            1 :     session_id = 0xFFFFFFFF;
    1632            1 :     session_info = &spdm_context->session_info[0];
    1633            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1634            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1635            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1636            1 :     libspdm_secured_message_set_response_finished_key(
    1637              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1638              :         hash_size);
    1639            1 :     libspdm_secured_message_set_session_state(
    1640              :         session_info->secured_message_context,
    1641              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1642              : 
    1643            1 :     spdm_context->connection_info.capability.flags |=
    1644              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1645            1 :     spdm_context->local_context.capability.flags |=
    1646              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1647            1 :     req_slot_id_param = 0;
    1648            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1649              :                                          req_slot_id_param);
    1650            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
    1651            1 :     free(data);
    1652            1 : }
    1653              : 
    1654              : /**
    1655              :  * Test 2: receiving a correct FINISH_RSP message with only MAC (no
    1656              :  * mutual authentication) and 'handshake in the clear'.
    1657              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    1658              :  * session is established.
    1659              :  **/
    1660            1 : void libspdm_test_requester_finish_case2(void **state)
    1661              : {
    1662              :     libspdm_return_t status;
    1663              :     libspdm_test_context_t *spdm_test_context;
    1664              :     libspdm_context_t *spdm_context;
    1665              :     uint32_t session_id;
    1666              :     uint8_t req_slot_id_param;
    1667              :     void *data;
    1668              :     size_t data_size;
    1669              :     void *hash;
    1670              :     size_t hash_size;
    1671              :     libspdm_session_info_t *session_info;
    1672              :     libspdm_secured_message_context_t *secured_message_context;
    1673              : 
    1674            1 :     spdm_test_context = *state;
    1675            1 :     spdm_context = spdm_test_context->spdm_context;
    1676            1 :     spdm_test_context->case_id = 0x2;
    1677            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1678              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1679            1 :     spdm_context->connection_info.connection_state =
    1680              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1681            1 :     spdm_context->connection_info.capability.flags |=
    1682              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1683            1 :     spdm_context->connection_info.capability.flags |=
    1684              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1685            1 :     spdm_context->connection_info.capability.flags |=
    1686              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1687            1 :     spdm_context->local_context.capability.flags |=
    1688              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1689            1 :     spdm_context->local_context.capability.flags |=
    1690              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1691            1 :     spdm_context->local_context.capability.flags |=
    1692              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1693            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1694              :                                                     m_libspdm_use_asym_algo, &data,
    1695              :                                                     &data_size, &hash, &hash_size);
    1696            1 :     libspdm_reset_message_a(spdm_context);
    1697            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1698              :         m_libspdm_use_hash_algo;
    1699            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1700              :         m_libspdm_use_asym_algo;
    1701            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1702              :         m_libspdm_use_dhe_algo;
    1703            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1704              :         m_libspdm_use_aead_algo;
    1705              : 
    1706              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1707              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1708              :         data_size;
    1709              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1710              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1711              :                      data, data_size);
    1712              : #else
    1713            1 :     libspdm_hash_all(
    1714              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1715              :         data, data_size,
    1716            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1717            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1718            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1719            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1720              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1721              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1722              :         data, data_size,
    1723              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1724              : #endif
    1725            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1726              : 
    1727            1 :     session_id = 0xFFFFFFFF;
    1728            1 :     session_info = &spdm_context->session_info[0];
    1729            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1730            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1731            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1732            1 :     libspdm_secured_message_set_response_finished_key(
    1733              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1734              :         hash_size);
    1735            1 :     libspdm_secured_message_set_session_state(
    1736              :         session_info->secured_message_context,
    1737              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1738              : 
    1739            1 :     spdm_context->connection_info.capability.flags |=
    1740              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1741            1 :     spdm_context->local_context.capability.flags |=
    1742              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1743            1 :     req_slot_id_param = 0;
    1744            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    1745            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1746            1 :     assert_int_equal(
    1747              :         libspdm_secured_message_get_session_state(
    1748              :             spdm_context->session_info[0].secured_message_context),
    1749              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1750              : 
    1751            1 :     secured_message_context = session_info->secured_message_context;
    1752              : 
    1753            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    1754              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    1755            1 :     free(data);
    1756            1 : }
    1757              : 
    1758              : /**
    1759              :  * Test 3: requester state has not been negotiated, as if GET_VERSION, GET_CAPABILITIES and
    1760              :  * NEGOTIATE_ALGORITHMS had not been exchanged.
    1761              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    1762              :  **/
    1763            1 : void libspdm_test_requester_finish_case3(void **state)
    1764              : {
    1765              :     libspdm_return_t status;
    1766              :     libspdm_test_context_t *spdm_test_context;
    1767              :     libspdm_context_t *spdm_context;
    1768              :     uint32_t session_id;
    1769              :     uint8_t req_slot_id_param;
    1770              :     void *data;
    1771              :     size_t data_size;
    1772              :     void *hash;
    1773              :     size_t hash_size;
    1774              :     libspdm_session_info_t *session_info;
    1775              : 
    1776            1 :     spdm_test_context = *state;
    1777            1 :     spdm_context = spdm_test_context->spdm_context;
    1778            1 :     spdm_test_context->case_id = 0x3;
    1779            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1780              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1781            1 :     spdm_context->connection_info.connection_state =
    1782              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
    1783            1 :     spdm_context->connection_info.capability.flags |=
    1784              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1785            1 :     spdm_context->connection_info.capability.flags |=
    1786              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1787            1 :     spdm_context->connection_info.capability.flags |=
    1788              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1789            1 :     spdm_context->local_context.capability.flags |=
    1790              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1791            1 :     spdm_context->local_context.capability.flags |=
    1792              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1793            1 :     spdm_context->local_context.capability.flags |=
    1794              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1795            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1796              :                                                     m_libspdm_use_asym_algo, &data,
    1797              :                                                     &data_size, &hash, &hash_size);
    1798            1 :     libspdm_reset_message_a(spdm_context);
    1799            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1800              :         m_libspdm_use_hash_algo;
    1801            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1802              :         m_libspdm_use_asym_algo;
    1803            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1804              :         m_libspdm_use_dhe_algo;
    1805            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1806              :         m_libspdm_use_aead_algo;
    1807              : 
    1808              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1809              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1810              :         data_size;
    1811              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1812              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1813              :                      data, data_size);
    1814              : #else
    1815            1 :     libspdm_hash_all(
    1816              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1817              :         data, data_size,
    1818            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1819            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1820            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1821            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1822              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1823              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1824              :         data, data_size,
    1825              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1826              : #endif
    1827            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1828              : 
    1829            1 :     session_id = 0xFFFFFFFF;
    1830            1 :     session_info = &spdm_context->session_info[0];
    1831            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1832            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1833            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1834            1 :     libspdm_secured_message_set_response_finished_key(
    1835              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1836              :         hash_size);
    1837            1 :     libspdm_secured_message_set_session_state(
    1838              :         session_info->secured_message_context,
    1839              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1840              : 
    1841            1 :     spdm_context->connection_info.capability.flags |=
    1842              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1843            1 :     spdm_context->local_context.capability.flags |=
    1844              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1845            1 :     req_slot_id_param = 0;
    1846            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1847              :                                          req_slot_id_param);
    1848            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    1849            1 :     free(data);
    1850            1 : }
    1851              : 
    1852              : /**
    1853              :  * Test 4: the requester is setup correctly (see Test 2), but receives an ERROR
    1854              :  * message indicating InvalidParameters.
    1855              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1856              :  **/
    1857            1 : void libspdm_test_requester_finish_case4(void **state)
    1858              : {
    1859              :     libspdm_return_t status;
    1860              :     libspdm_test_context_t *spdm_test_context;
    1861              :     libspdm_context_t *spdm_context;
    1862              :     uint32_t session_id;
    1863              :     uint8_t req_slot_id_param;
    1864              :     void *data;
    1865              :     size_t data_size;
    1866              :     void *hash;
    1867              :     size_t hash_size;
    1868              :     libspdm_session_info_t *session_info;
    1869              : 
    1870            1 :     spdm_test_context = *state;
    1871            1 :     spdm_context = spdm_test_context->spdm_context;
    1872            1 :     spdm_test_context->case_id = 0x4;
    1873            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1874              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1875            1 :     spdm_context->connection_info.connection_state =
    1876              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1877            1 :     spdm_context->connection_info.capability.flags |=
    1878              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1879            1 :     spdm_context->connection_info.capability.flags |=
    1880              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1881            1 :     spdm_context->connection_info.capability.flags |=
    1882              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1883            1 :     spdm_context->local_context.capability.flags |=
    1884              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1885            1 :     spdm_context->local_context.capability.flags |=
    1886              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1887            1 :     spdm_context->local_context.capability.flags |=
    1888              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1889            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1890              :                                                     m_libspdm_use_asym_algo, &data,
    1891              :                                                     &data_size, &hash, &hash_size);
    1892            1 :     libspdm_reset_message_a(spdm_context);
    1893            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1894              :         m_libspdm_use_hash_algo;
    1895            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1896              :         m_libspdm_use_asym_algo;
    1897            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1898              :         m_libspdm_use_dhe_algo;
    1899            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1900              :         m_libspdm_use_aead_algo;
    1901              : 
    1902              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1903              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1904              :         data_size;
    1905              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1906              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1907              :                      data, data_size);
    1908              : #else
    1909            1 :     libspdm_hash_all(
    1910              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1911              :         data, data_size,
    1912            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1913            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1914            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1915            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1916              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1917              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1918              :         data, data_size,
    1919              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1920              : #endif
    1921            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1922              : 
    1923            1 :     session_id = 0xFFFFFFFF;
    1924            1 :     session_info = &spdm_context->session_info[0];
    1925            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1926            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1927            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1928            1 :     libspdm_secured_message_set_response_finished_key(
    1929              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1930              :         hash_size);
    1931            1 :     libspdm_secured_message_set_session_state(
    1932              :         session_info->secured_message_context,
    1933              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1934              : 
    1935            1 :     spdm_context->connection_info.capability.flags |=
    1936              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1937            1 :     spdm_context->local_context.capability.flags |=
    1938              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1939            1 :     req_slot_id_param = 0;
    1940            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1941              :                                          req_slot_id_param);
    1942            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
    1943            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    1944            1 :     free(data);
    1945            1 : }
    1946              : 
    1947              : /**
    1948              :  * Test 5: the requester is setup correctly (see Test 2), but receives an ERROR
    1949              :  * message indicating the Busy status of the responder.
    1950              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1951              :  **/
    1952            1 : void libspdm_test_requester_finish_case5(void **state)
    1953              : {
    1954              :     libspdm_return_t status;
    1955              :     libspdm_test_context_t *spdm_test_context;
    1956              :     libspdm_context_t *spdm_context;
    1957              :     uint32_t session_id;
    1958              :     uint8_t req_slot_id_param;
    1959              :     void *data;
    1960              :     size_t data_size;
    1961              :     void *hash;
    1962              :     size_t hash_size;
    1963              :     libspdm_session_info_t *session_info;
    1964              : 
    1965            1 :     spdm_test_context = *state;
    1966            1 :     spdm_context = spdm_test_context->spdm_context;
    1967            1 :     spdm_test_context->case_id = 0x5;
    1968            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1969              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1970            1 :     spdm_context->connection_info.connection_state =
    1971              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1972            1 :     spdm_context->connection_info.capability.flags |=
    1973              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1974            1 :     spdm_context->connection_info.capability.flags |=
    1975              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1976            1 :     spdm_context->connection_info.capability.flags |=
    1977              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1978            1 :     spdm_context->local_context.capability.flags |=
    1979              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1980            1 :     spdm_context->local_context.capability.flags |=
    1981              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1982            1 :     spdm_context->local_context.capability.flags |=
    1983              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1984            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1985              :                                                     m_libspdm_use_asym_algo, &data,
    1986              :                                                     &data_size, &hash, &hash_size);
    1987            1 :     libspdm_reset_message_a(spdm_context);
    1988            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1989              :         m_libspdm_use_hash_algo;
    1990            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1991              :         m_libspdm_use_asym_algo;
    1992            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1993              :         m_libspdm_use_dhe_algo;
    1994            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1995              :         m_libspdm_use_aead_algo;
    1996              : 
    1997              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1998              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1999              :         data_size;
    2000              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2001              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2002              :                      data, data_size);
    2003              : #else
    2004            1 :     libspdm_hash_all(
    2005              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2006              :         data, data_size,
    2007            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2008            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2009            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2010            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2011              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2012              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2013              :         data, data_size,
    2014              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2015              : #endif
    2016            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2017              : 
    2018            1 :     session_id = 0xFFFFFFFF;
    2019            1 :     session_info = &spdm_context->session_info[0];
    2020            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2021            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2022            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2023            1 :     libspdm_secured_message_set_response_finished_key(
    2024              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2025              :         hash_size);
    2026            1 :     libspdm_secured_message_set_session_state(
    2027              :         session_info->secured_message_context,
    2028              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2029              : 
    2030            1 :     spdm_context->connection_info.capability.flags |=
    2031              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2032            1 :     spdm_context->local_context.capability.flags |=
    2033              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2034            1 :     req_slot_id_param = 0;
    2035            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2036              :                                          req_slot_id_param);
    2037            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    2038            1 :     free(data);
    2039            1 : }
    2040              : 
    2041              : /**
    2042              :  * Test 6: the requester is setup correctly (see Test 2), but, on the first try,
    2043              :  * receiving a Busy ERROR message, and on retry, receiving a correct FINISH_RSP
    2044              :  * message with only MAC (no mutual authentication).
    2045              :  * Expected behavior: client returns a Status of RETURN_SUCCESS.
    2046              :  **/
    2047            1 : void libspdm_test_requester_finish_case6(void **state)
    2048              : {
    2049              :     libspdm_return_t status;
    2050              :     libspdm_test_context_t *spdm_test_context;
    2051              :     libspdm_context_t *spdm_context;
    2052              :     uint32_t session_id;
    2053              :     uint8_t req_slot_id_param;
    2054              :     void *data;
    2055              :     size_t data_size;
    2056              :     void *hash;
    2057              :     size_t hash_size;
    2058              :     libspdm_session_info_t *session_info;
    2059              : 
    2060            1 :     spdm_test_context = *state;
    2061            1 :     spdm_context = spdm_test_context->spdm_context;
    2062            1 :     spdm_test_context->case_id = 0x6;
    2063            1 :     spdm_context->retry_times = 3;
    2064            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2065              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2066            1 :     spdm_context->connection_info.connection_state =
    2067              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2068            1 :     spdm_context->connection_info.capability.flags |=
    2069              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2070            1 :     spdm_context->connection_info.capability.flags |=
    2071              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2072            1 :     spdm_context->connection_info.capability.flags |=
    2073              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2074            1 :     spdm_context->local_context.capability.flags |=
    2075              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2076            1 :     spdm_context->local_context.capability.flags |=
    2077              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2078            1 :     spdm_context->local_context.capability.flags |=
    2079              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2080            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2081              :                                                     m_libspdm_use_asym_algo, &data,
    2082              :                                                     &data_size, &hash, &hash_size);
    2083            1 :     libspdm_reset_message_a(spdm_context);
    2084            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2085              :         m_libspdm_use_hash_algo;
    2086            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2087              :         m_libspdm_use_asym_algo;
    2088            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2089              :         m_libspdm_use_dhe_algo;
    2090            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2091              :         m_libspdm_use_aead_algo;
    2092              : 
    2093              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2094              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2095              :         data_size;
    2096              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2097              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2098              :                      data, data_size);
    2099              : #else
    2100            1 :     libspdm_hash_all(
    2101              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2102              :         data, data_size,
    2103            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2104            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2105            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2106            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2107              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2108              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2109              :         data, data_size,
    2110              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2111              : #endif
    2112            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2113              : 
    2114            1 :     session_id = 0xFFFFFFFF;
    2115            1 :     session_info = &spdm_context->session_info[0];
    2116            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2117            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2118            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2119            1 :     libspdm_secured_message_set_response_finished_key(
    2120              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2121              :         hash_size);
    2122            1 :     libspdm_secured_message_set_session_state(
    2123              :         session_info->secured_message_context,
    2124              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2125              : 
    2126            1 :     spdm_context->connection_info.capability.flags |=
    2127              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2128            1 :     spdm_context->local_context.capability.flags |=
    2129              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2130            1 :     req_slot_id_param = 0;
    2131            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2132              :                                          req_slot_id_param);
    2133            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2134            1 :     assert_int_equal(
    2135              :         libspdm_secured_message_get_session_state(
    2136              :             spdm_context->session_info[0].secured_message_context),
    2137              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2138            1 :     free(data);
    2139            1 : }
    2140              : 
    2141              : /**
    2142              :  * Test 7: the requester is setup correctly (see Test 2), but receives an ERROR
    2143              :  * message indicating the RequestResynch status of the responder.
    2144              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR, and the
    2145              :  * communication is reset to expect a new GET_VERSION message.
    2146              :  **/
    2147            1 : void libspdm_test_requester_finish_case7(void **state)
    2148              : {
    2149              :     libspdm_return_t status;
    2150              :     libspdm_test_context_t *spdm_test_context;
    2151              :     libspdm_context_t *spdm_context;
    2152              :     uint32_t session_id;
    2153              :     uint8_t req_slot_id_param;
    2154              :     void *data;
    2155              :     size_t data_size;
    2156              :     void *hash;
    2157              :     size_t hash_size;
    2158              :     libspdm_session_info_t *session_info;
    2159              : 
    2160            1 :     spdm_test_context = *state;
    2161            1 :     spdm_context = spdm_test_context->spdm_context;
    2162            1 :     spdm_test_context->case_id = 0x7;
    2163            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2164              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2165            1 :     spdm_context->connection_info.connection_state =
    2166              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2167            1 :     spdm_context->connection_info.capability.flags |=
    2168              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2169            1 :     spdm_context->connection_info.capability.flags |=
    2170              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2171            1 :     spdm_context->connection_info.capability.flags |=
    2172              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2173            1 :     spdm_context->local_context.capability.flags |=
    2174              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2175            1 :     spdm_context->local_context.capability.flags |=
    2176              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2177            1 :     spdm_context->local_context.capability.flags |=
    2178              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2179            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2180              :                                                     m_libspdm_use_asym_algo, &data,
    2181              :                                                     &data_size, &hash, &hash_size);
    2182            1 :     libspdm_reset_message_a(spdm_context);
    2183            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2184              :         m_libspdm_use_hash_algo;
    2185            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2186              :         m_libspdm_use_asym_algo;
    2187            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2188              :         m_libspdm_use_dhe_algo;
    2189            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2190              :         m_libspdm_use_aead_algo;
    2191              : 
    2192              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2193              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2194              :         data_size;
    2195              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2196              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2197              :                      data, data_size);
    2198              : #else
    2199            1 :     libspdm_hash_all(
    2200              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2201              :         data, data_size,
    2202            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2203            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2204            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2205            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2206              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2207              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2208              :         data, data_size,
    2209              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2210              : #endif
    2211            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2212              : 
    2213            1 :     session_id = 0xFFFFFFFF;
    2214            1 :     session_info = &spdm_context->session_info[0];
    2215            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2216            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2217            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2218            1 :     libspdm_secured_message_set_response_finished_key(
    2219              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2220              :         hash_size);
    2221            1 :     libspdm_secured_message_set_session_state(
    2222              :         session_info->secured_message_context,
    2223              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2224              : 
    2225            1 :     spdm_context->connection_info.capability.flags |=
    2226              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2227            1 :     spdm_context->local_context.capability.flags |=
    2228              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2229            1 :     req_slot_id_param = 0;
    2230            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2231              :                                          req_slot_id_param);
    2232            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    2233            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    2234              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    2235            1 :     free(data);
    2236            1 : }
    2237              : 
    2238              : /**
    2239              :  * Test 8: the requester is setup correctly (see Test 2), but receives an ERROR
    2240              :  * message indicating the ResponseNotReady status of the responder.
    2241              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR,.
    2242              :  **/
    2243            1 : void libspdm_test_requester_finish_case8(void **state)
    2244              : {
    2245              :     libspdm_return_t status;
    2246              :     libspdm_test_context_t *spdm_test_context;
    2247              :     libspdm_context_t *spdm_context;
    2248              :     uint32_t session_id;
    2249              :     uint8_t req_slot_id_param;
    2250              :     void *data;
    2251              :     size_t data_size;
    2252              :     void *hash;
    2253              :     size_t hash_size;
    2254              :     libspdm_session_info_t *session_info;
    2255              : 
    2256            1 :     spdm_test_context = *state;
    2257            1 :     spdm_context = spdm_test_context->spdm_context;
    2258            1 :     spdm_test_context->case_id = 0x8;
    2259            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2260              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2261            1 :     spdm_context->connection_info.connection_state =
    2262              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2263            1 :     spdm_context->connection_info.capability.flags |=
    2264              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2265            1 :     spdm_context->connection_info.capability.flags |=
    2266              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2267            1 :     spdm_context->connection_info.capability.flags |=
    2268              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2269            1 :     spdm_context->local_context.capability.flags |=
    2270              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2271            1 :     spdm_context->local_context.capability.flags |=
    2272              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2273            1 :     spdm_context->local_context.capability.flags |=
    2274              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2275            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2276              :                                                     m_libspdm_use_asym_algo, &data,
    2277              :                                                     &data_size, &hash, &hash_size);
    2278            1 :     libspdm_reset_message_a(spdm_context);
    2279            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2280              :         m_libspdm_use_hash_algo;
    2281            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2282              :         m_libspdm_use_asym_algo;
    2283            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2284              :         m_libspdm_use_dhe_algo;
    2285            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2286              :         m_libspdm_use_aead_algo;
    2287              : 
    2288              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2289              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2290              :         data_size;
    2291              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2292              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2293              :                      data, data_size);
    2294              : #else
    2295            1 :     libspdm_hash_all(
    2296              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2297              :         data, data_size,
    2298            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2299            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2300            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2301            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2302              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2303              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2304              :         data, data_size,
    2305              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2306              : #endif
    2307            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2308              : 
    2309            1 :     session_id = 0xFFFFFFFF;
    2310            1 :     session_info = &spdm_context->session_info[0];
    2311            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2312            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2313            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2314            1 :     libspdm_secured_message_set_response_finished_key(
    2315              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2316              :         hash_size);
    2317            1 :     libspdm_secured_message_set_session_state(
    2318              :         session_info->secured_message_context,
    2319              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2320              : 
    2321            1 :     spdm_context->connection_info.capability.flags |=
    2322              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2323            1 :     spdm_context->local_context.capability.flags |=
    2324              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2325            1 :     req_slot_id_param = 0;
    2326            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2327              :                                          req_slot_id_param);
    2328            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    2329            1 :     free(data);
    2330            1 : }
    2331              : 
    2332              : /**
    2333              :  * Test 9: the requester is setup correctly (see Test 2), but, on the first try,
    2334              :  * receiving a ResponseNotReady ERROR message, and on retry, receiving a correct
    2335              :  * FINISH_RSP message with only MAC (no mutual authentication).
    2336              :  * Expected behavior: client returns a Status of RETURN_SUCCESS.
    2337              :  **/
    2338            1 : void libspdm_test_requester_finish_case9(void **state)
    2339              : {
    2340              :     libspdm_return_t status;
    2341              :     libspdm_test_context_t *spdm_test_context;
    2342              :     libspdm_context_t *spdm_context;
    2343              :     uint32_t session_id;
    2344              :     uint8_t req_slot_id_param;
    2345              :     void *data;
    2346              :     size_t data_size;
    2347              :     void *hash;
    2348              :     size_t hash_size;
    2349              :     libspdm_session_info_t *session_info;
    2350              : 
    2351            1 :     spdm_test_context = *state;
    2352            1 :     spdm_context = spdm_test_context->spdm_context;
    2353            1 :     spdm_test_context->case_id = 0x9;
    2354            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2355              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2356            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2357            1 :     spdm_context->connection_info.capability.flags |=
    2358              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2359            1 :     spdm_context->connection_info.capability.flags |=
    2360              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2361            1 :     spdm_context->connection_info.capability.flags |=
    2362              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2363            1 :     spdm_context->local_context.capability.flags |=
    2364              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2365            1 :     spdm_context->local_context.capability.flags |=
    2366              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2367            1 :     spdm_context->local_context.capability.flags |=
    2368              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2369            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2370              :                                                     m_libspdm_use_asym_algo, &data,
    2371              :                                                     &data_size, &hash, &hash_size);
    2372            1 :     libspdm_reset_message_a(spdm_context);
    2373            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2374            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2375            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2376            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2377              : 
    2378              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2379              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
    2380              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2381              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2382              :                      data, data_size);
    2383              : #else
    2384            1 :     libspdm_hash_all(
    2385              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2386              :         data, data_size,
    2387            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2388            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2389            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2390            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2391              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2392              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2393              :         data, data_size,
    2394              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2395              : #endif
    2396            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2397              : 
    2398            1 :     session_id = 0xFFFFFFFF;
    2399            1 :     session_info = &spdm_context->session_info[0];
    2400            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2401            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2402            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2403            1 :     libspdm_secured_message_set_response_finished_key(
    2404              :         session_info->secured_message_context, m_libspdm_dummy_buffer, hash_size);
    2405            1 :     libspdm_secured_message_set_session_state(
    2406              :         session_info->secured_message_context,
    2407              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2408              : 
    2409            1 :     spdm_context->connection_info.capability.flags |=
    2410              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2411            1 :     spdm_context->local_context.capability.flags |=
    2412              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2413            1 :     req_slot_id_param = 0;
    2414            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    2415              :     if (LIBSPDM_RESPOND_IF_READY_SUPPORT) {
    2416            1 :         assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2417            1 :         assert_int_equal(
    2418              :             libspdm_secured_message_get_session_state(
    2419              :                 spdm_context->session_info[0].secured_message_context),
    2420              :             LIBSPDM_SESSION_STATE_ESTABLISHED);
    2421              :     } else {
    2422              :         assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    2423              :     }
    2424              : 
    2425            1 :     free(data);
    2426            1 : }
    2427              : 
    2428              : /**
    2429              :  * Test 10: receiving an unexpected ERROR message from the responder.
    2430              :  * There are tests for all named codes, including some reserved ones
    2431              :  * (namely, 0x00, 0x0b, 0x0c, 0x3f, 0xfd, 0xfe).
    2432              :  * However, for having specific test cases, it is excluded from this case:
    2433              :  * Busy (0x03), ResponseNotReady (0x42), and RequestResync (0x43).
    2434              :  * Expected behavior: client returns a status of RETURN_DEVICE_ERROR.
    2435              :  **/
    2436            1 : void libspdm_test_requester_finish_case10(void **state) {
    2437              :     libspdm_return_t status;
    2438              :     libspdm_test_context_t    *spdm_test_context;
    2439              :     libspdm_context_t  *spdm_context;
    2440              :     uint32_t session_id;
    2441              :     uint8_t req_slot_id_param;
    2442              :     void                 *data;
    2443              :     size_t data_size;
    2444              :     void                 *hash;
    2445              :     size_t hash_size;
    2446              :     libspdm_session_info_t    *session_info;
    2447              :     uint16_t error_code;
    2448              : 
    2449            1 :     spdm_test_context = *state;
    2450            1 :     spdm_context = spdm_test_context->spdm_context;
    2451            1 :     spdm_test_context->case_id = 0xA;
    2452            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2453              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2454            1 :     spdm_context->connection_info.capability.flags |=
    2455              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2456            1 :     spdm_context->connection_info.capability.flags |=
    2457              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2458            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2459            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2460            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2461            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2462            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    2463              :                                                      m_libspdm_use_asym_algo,
    2464              :                                                      &data, &data_size,
    2465              :                                                      &hash, &hash_size);
    2466            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2467            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2468            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2469            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2470              : 
    2471              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2472              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2473              :         data_size;
    2474              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2475              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2476              :                      data, data_size);
    2477              : #else
    2478            1 :     libspdm_hash_all(
    2479              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2480              :         data, data_size,
    2481            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2482            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2483            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2484            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2485              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2486              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2487              :         data, data_size,
    2488              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2489              : #endif
    2490            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2491              : 
    2492            1 :     session_id = 0xFFFFFFFF;
    2493            1 :     spdm_context->connection_info.capability.flags |=
    2494              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2495            1 :     spdm_context->local_context.capability.flags |=
    2496              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2497            1 :     req_slot_id_param = 0;
    2498              : 
    2499            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    2500           19 :     while(error_code <= 0xff) {
    2501           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2502           18 :         libspdm_reset_message_a(spdm_context);
    2503              : 
    2504           18 :         session_info = &spdm_context->session_info[0];
    2505           18 :         libspdm_session_info_init (spdm_context, session_info, session_id, false);
    2506           18 :         hash_size = libspdm_get_hash_size (m_libspdm_use_hash_algo);
    2507           18 :         libspdm_set_mem (m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2508           18 :         libspdm_secured_message_set_response_finished_key (session_info->secured_message_context,
    2509              :                                                            m_libspdm_dummy_buffer, hash_size);
    2510           18 :         libspdm_secured_message_set_session_state (session_info->secured_message_context,
    2511              :                                                    LIBSPDM_SESSION_STATE_HANDSHAKING);
    2512              : 
    2513           18 :         status = libspdm_send_receive_finish (spdm_context, session_id, req_slot_id_param);
    2514           18 :         if(error_code != SPDM_ERROR_CODE_DECRYPT_ERROR) {
    2515           17 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    2516              :         } else {
    2517            1 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_SESSION_MSG_ERROR, error_code);
    2518              :         }
    2519              : 
    2520           18 :         error_code++;
    2521           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    2522            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    2523              :         }
    2524           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    2525            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    2526              :         }
    2527           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    2528            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    2529              :         }
    2530              :     }
    2531              : 
    2532            1 :     free(data);
    2533            1 : }
    2534              : 
    2535            1 : void libspdm_test_requester_finish_case11(void **state)
    2536              : {
    2537              :     libspdm_return_t status;
    2538              :     libspdm_test_context_t *spdm_test_context;
    2539              :     libspdm_context_t *spdm_context;
    2540              :     uint32_t session_id;
    2541              :     uint8_t req_slot_id_param;
    2542              :     void *data;
    2543              :     size_t data_size;
    2544              :     void *hash;
    2545              :     size_t hash_size;
    2546              :     libspdm_session_info_t *session_info;
    2547              : 
    2548            1 :     spdm_test_context = *state;
    2549            1 :     spdm_context = spdm_test_context->spdm_context;
    2550            1 :     spdm_test_context->case_id = 0xB;
    2551            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2552              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2553            1 :     spdm_context->connection_info.connection_state =
    2554              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2555            1 :     spdm_context->connection_info.capability.flags |=
    2556              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2557            1 :     spdm_context->connection_info.capability.flags |=
    2558              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2559            1 :     spdm_context->connection_info.capability.flags |=
    2560              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2561            1 :     spdm_context->local_context.capability.flags |=
    2562              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2563            1 :     spdm_context->local_context.capability.flags |=
    2564              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2565            1 :     spdm_context->local_context.capability.flags |=
    2566              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2567            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2568              :                                                     m_libspdm_use_asym_algo, &data,
    2569              :                                                     &data_size, &hash, &hash_size);
    2570            1 :     libspdm_reset_message_a(spdm_context);
    2571            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2572              :         m_libspdm_use_hash_algo;
    2573            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2574              :         m_libspdm_use_asym_algo;
    2575            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2576              :         m_libspdm_use_dhe_algo;
    2577            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2578              :         m_libspdm_use_aead_algo;
    2579              : 
    2580              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2581              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2582              :         data_size;
    2583              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2584              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2585              :                      data, data_size);
    2586              : #else
    2587            1 :     libspdm_hash_all(
    2588              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2589              :         data, data_size,
    2590            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2591            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2592            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2593            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2594              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2595              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2596              :         data, data_size,
    2597              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2598              : #endif
    2599            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2600              : 
    2601            1 :     session_id = 0xFFFFFFFF;
    2602            1 :     session_info = &spdm_context->session_info[0];
    2603            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2604            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2605            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2606            1 :     libspdm_secured_message_set_response_finished_key(
    2607              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2608              :         hash_size);
    2609            1 :     libspdm_secured_message_set_session_state(
    2610              :         session_info->secured_message_context,
    2611              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2612              : 
    2613            1 :     spdm_context->connection_info.capability.flags |=
    2614              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2615            1 :     spdm_context->local_context.capability.flags |=
    2616              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2617            1 :     req_slot_id_param = 0;
    2618              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2619              :     session_info->session_transcript.message_m.buffer_size =
    2620              :         session_info->session_transcript.message_m.max_buffer_size;
    2621              :     spdm_context->transcript.message_b.buffer_size =
    2622              :         spdm_context->transcript.message_b.max_buffer_size;
    2623              :     spdm_context->transcript.message_c.buffer_size =
    2624              :         spdm_context->transcript.message_c.max_buffer_size;
    2625              :     spdm_context->transcript.message_mut_b.buffer_size =
    2626              :         spdm_context->transcript.message_mut_b.max_buffer_size;
    2627              :     spdm_context->transcript.message_mut_c.buffer_size =
    2628              :         spdm_context->transcript.message_mut_c.max_buffer_size;
    2629              : #endif
    2630              : 
    2631            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2632              :                                          req_slot_id_param);
    2633            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2634            1 :     assert_int_equal(
    2635              :         libspdm_secured_message_get_session_state(
    2636              :             spdm_context->session_info[0].secured_message_context),
    2637              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2638              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2639              :     assert_int_equal(session_info->session_transcript.message_m.buffer_size,
    2640              :                      0);
    2641              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
    2642              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
    2643              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
    2644              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
    2645              : #endif
    2646            1 :     free(data);
    2647            1 : }
    2648              : 
    2649              : /**
    2650              :  * Test 12: requester is not setup correctly to support key exchange
    2651              :  * (no capabilities). The responder would attempt to return a correct
    2652              :  * FINISH_RSP message.
    2653              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2654              :  **/
    2655            1 : void libspdm_test_requester_finish_case12(void **state)
    2656              : {
    2657              :     libspdm_return_t status;
    2658              :     libspdm_test_context_t *spdm_test_context;
    2659              :     libspdm_context_t *spdm_context;
    2660              :     uint32_t session_id;
    2661              :     uint8_t req_slot_id_param;
    2662              :     void *data;
    2663              :     size_t data_size;
    2664              :     void *hash;
    2665              :     size_t hash_size;
    2666              :     libspdm_session_info_t *session_info;
    2667              : 
    2668            1 :     spdm_test_context = *state;
    2669            1 :     spdm_context = spdm_test_context->spdm_context;
    2670            1 :     spdm_test_context->case_id = 0xC;
    2671            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2672              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2673            1 :     spdm_context->connection_info.connection_state =
    2674              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2675            1 :     spdm_context->connection_info.capability.flags = 0;
    2676            1 :     spdm_context->connection_info.capability.flags |=
    2677              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2678            1 :     spdm_context->connection_info.capability.flags |=
    2679              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2680            1 :     spdm_context->connection_info.capability.flags |=
    2681              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2682            1 :     spdm_context->local_context.capability.flags = 0;
    2683              :     /* no key exchange capabilities (requester)*/
    2684            1 :     spdm_context->local_context.capability.flags |=
    2685              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2686            1 :     spdm_context->local_context.capability.flags |=
    2687              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2688            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2689              :                                                     m_libspdm_use_asym_algo, &data,
    2690              :                                                     &data_size, &hash, &hash_size);
    2691            1 :     libspdm_reset_message_a(spdm_context);
    2692            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2693              :         m_libspdm_use_hash_algo;
    2694            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2695              :         m_libspdm_use_asym_algo;
    2696            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2697              :         m_libspdm_use_dhe_algo;
    2698            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2699              :         m_libspdm_use_aead_algo;
    2700              : 
    2701              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2702              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2703              :         data_size;
    2704              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2705              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2706              :                      data, data_size);
    2707              : #else
    2708            1 :     libspdm_hash_all(
    2709              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2710              :         data, data_size,
    2711            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2712            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2713            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2714            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2715              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2716              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2717              :         data, data_size,
    2718              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2719              : #endif
    2720            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2721              : 
    2722            1 :     session_id = 0xFFFFFFFF;
    2723            1 :     session_info = &spdm_context->session_info[0];
    2724            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2725            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2726            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2727            1 :     libspdm_secured_message_set_response_finished_key(
    2728              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2729              :         hash_size);
    2730            1 :     libspdm_secured_message_set_session_state(
    2731              :         session_info->secured_message_context,
    2732              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2733              : 
    2734            1 :     spdm_context->connection_info.capability.flags |=
    2735              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2736            1 :     spdm_context->local_context.capability.flags |=
    2737              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2738            1 :     req_slot_id_param = 0;
    2739            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2740              :                                          req_slot_id_param);
    2741            1 :     assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
    2742            1 :     free(data);
    2743            1 : }
    2744              : 
    2745              : /**
    2746              :  * Test 13: requester is not setup correctly to accept key exchange and
    2747              :  * finish at this point (at least NEGOTIATE_ALGORITHMS is required, if
    2748              :  * the public key was provisioned before the key exchange). The
    2749              :  * responder would attempt to return a correct FINISH_RSP message.
    2750              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2751              :  **/
    2752            1 : void libspdm_test_requester_finish_case13(void **state)
    2753              : {
    2754              :     libspdm_return_t status;
    2755              :     libspdm_test_context_t *spdm_test_context;
    2756              :     libspdm_context_t *spdm_context;
    2757              :     uint32_t session_id;
    2758              :     uint8_t req_slot_id_param;
    2759              :     void *data;
    2760              :     size_t data_size;
    2761              :     void *hash;
    2762              :     size_t hash_size;
    2763              :     libspdm_session_info_t *session_info;
    2764              : 
    2765            1 :     spdm_test_context = *state;
    2766            1 :     spdm_context = spdm_test_context->spdm_context;
    2767            1 :     spdm_test_context->case_id = 0xD;
    2768            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2769              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2770            1 :     spdm_context->connection_info.connection_state =
    2771              :         LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES;
    2772            1 :     spdm_context->connection_info.capability.flags |=
    2773              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2774            1 :     spdm_context->connection_info.capability.flags |=
    2775              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2776            1 :     spdm_context->connection_info.capability.flags |=
    2777              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2778            1 :     spdm_context->local_context.capability.flags |=
    2779              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2780            1 :     spdm_context->local_context.capability.flags |=
    2781              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2782            1 :     spdm_context->local_context.capability.flags |=
    2783              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2784            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2785              :                                                     m_libspdm_use_asym_algo, &data,
    2786              :                                                     &data_size, &hash, &hash_size);
    2787            1 :     libspdm_reset_message_a(spdm_context);
    2788            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2789              :         m_libspdm_use_hash_algo;
    2790            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2791              :         m_libspdm_use_asym_algo;
    2792            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2793              :         m_libspdm_use_dhe_algo;
    2794            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2795              :         m_libspdm_use_aead_algo;
    2796              : 
    2797              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2798              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2799              :         data_size;
    2800              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2801              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2802              :                      data, data_size);
    2803              : #else
    2804            1 :     libspdm_hash_all(
    2805              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2806              :         data, data_size,
    2807            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2808            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2809            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2810            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2811              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2812              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2813              :         data, data_size,
    2814              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2815              : #endif
    2816            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2817              : 
    2818            1 :     session_id = 0xFFFFFFFF;
    2819            1 :     session_info = &spdm_context->session_info[0];
    2820            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2821            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2822            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2823            1 :     libspdm_secured_message_set_response_finished_key(
    2824              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2825              :         hash_size);
    2826            1 :     libspdm_secured_message_set_session_state(
    2827              :         session_info->secured_message_context,
    2828              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2829              : 
    2830            1 :     spdm_context->connection_info.capability.flags |=
    2831              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2832            1 :     spdm_context->local_context.capability.flags |=
    2833              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2834            1 :     req_slot_id_param = 0;
    2835            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2836              :                                          req_slot_id_param);
    2837            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    2838            1 :     free(data);
    2839            1 : }
    2840              : 
    2841              : /**
    2842              :  * Test 14: receiving an incorrect FINISH_RSP message, with wrong response
    2843              :  * code, but all other field correct.
    2844              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    2845              :  **/
    2846            1 : void libspdm_test_requester_finish_case14(void **state)
    2847              : {
    2848              :     libspdm_return_t status;
    2849              :     libspdm_test_context_t *spdm_test_context;
    2850              :     libspdm_context_t *spdm_context;
    2851              :     uint32_t session_id;
    2852              :     uint8_t req_slot_id_param;
    2853              :     void *data;
    2854              :     size_t data_size;
    2855              :     void *hash;
    2856              :     size_t hash_size;
    2857              :     libspdm_session_info_t *session_info;
    2858              : 
    2859            1 :     spdm_test_context = *state;
    2860            1 :     spdm_context = spdm_test_context->spdm_context;
    2861            1 :     spdm_test_context->case_id = 0xE;
    2862            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2863              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2864            1 :     spdm_context->connection_info.connection_state =
    2865              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2866            1 :     spdm_context->connection_info.capability.flags |=
    2867              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2868            1 :     spdm_context->connection_info.capability.flags |=
    2869              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2870            1 :     spdm_context->connection_info.capability.flags |=
    2871              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2872            1 :     spdm_context->local_context.capability.flags |=
    2873              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2874            1 :     spdm_context->local_context.capability.flags |=
    2875              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2876            1 :     spdm_context->local_context.capability.flags |=
    2877              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2878            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2879              :                                                     m_libspdm_use_asym_algo, &data,
    2880              :                                                     &data_size, &hash, &hash_size);
    2881            1 :     libspdm_reset_message_a(spdm_context);
    2882            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2883              :         m_libspdm_use_hash_algo;
    2884            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2885              :         m_libspdm_use_asym_algo;
    2886            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2887              :         m_libspdm_use_dhe_algo;
    2888            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2889              :         m_libspdm_use_aead_algo;
    2890              : 
    2891              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2892              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2893              :         data_size;
    2894              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2895              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2896              :                      data, data_size);
    2897              : #else
    2898            1 :     libspdm_hash_all(
    2899              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2900              :         data, data_size,
    2901            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2902            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2903            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2904            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2905              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2906              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2907              :         data, data_size,
    2908              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2909              : #endif
    2910            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2911              : 
    2912            1 :     session_id = 0xFFFFFFFF;
    2913            1 :     session_info = &spdm_context->session_info[0];
    2914            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2915            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2916            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2917            1 :     libspdm_secured_message_set_response_finished_key(
    2918              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2919              :         hash_size);
    2920            1 :     libspdm_secured_message_set_session_state(
    2921              :         session_info->secured_message_context,
    2922              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2923              : 
    2924            1 :     spdm_context->connection_info.capability.flags |=
    2925              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2926            1 :     spdm_context->local_context.capability.flags |=
    2927              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2928            1 :     req_slot_id_param = 0;
    2929            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2930              :                                          req_slot_id_param);
    2931            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    2932            1 :     free(data);
    2933            1 : }
    2934              : 
    2935              : /**
    2936              :  * Test 15: requester is not setup correctly by not initializing a
    2937              :  * session during KEY_EXCHANGE. The responder would attempt to
    2938              :  * return a correct FINISH_RSP message.
    2939              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2940              :  **/
    2941            1 : void libspdm_test_requester_finish_case15(void **state)
    2942              : {
    2943              :     libspdm_return_t status;
    2944              :     libspdm_test_context_t *spdm_test_context;
    2945              :     libspdm_context_t *spdm_context;
    2946              :     uint32_t session_id;
    2947              :     uint8_t req_slot_id_param;
    2948              :     void *data;
    2949              :     size_t data_size;
    2950              :     void *hash;
    2951              :     size_t hash_size;
    2952              :     libspdm_session_info_t *session_info;
    2953              : 
    2954            1 :     spdm_test_context = *state;
    2955            1 :     spdm_context = spdm_test_context->spdm_context;
    2956            1 :     spdm_test_context->case_id = 0xF;
    2957            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2958              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2959            1 :     spdm_context->connection_info.connection_state =
    2960              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2961            1 :     spdm_context->connection_info.capability.flags |=
    2962              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2963            1 :     spdm_context->connection_info.capability.flags |=
    2964              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2965            1 :     spdm_context->connection_info.capability.flags |=
    2966              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2967            1 :     spdm_context->local_context.capability.flags |=
    2968              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2969            1 :     spdm_context->local_context.capability.flags |=
    2970              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2971            1 :     spdm_context->local_context.capability.flags |=
    2972              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2973            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2974              :                                                     m_libspdm_use_asym_algo, &data,
    2975              :                                                     &data_size, &hash, &hash_size);
    2976            1 :     libspdm_reset_message_a(spdm_context);
    2977            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2978              :         m_libspdm_use_hash_algo;
    2979            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2980              :         m_libspdm_use_asym_algo;
    2981            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2982              :         m_libspdm_use_dhe_algo;
    2983            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2984              :         m_libspdm_use_aead_algo;
    2985              : 
    2986              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2987              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2988              :         data_size;
    2989              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2990              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2991              :                      data, data_size);
    2992              : #else
    2993            1 :     libspdm_hash_all(
    2994              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2995              :         data, data_size,
    2996            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2997            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2998            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2999            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3000              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3001              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3002              :         data, data_size,
    3003              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3004              : #endif
    3005            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3006              : 
    3007            1 :     session_id = 0xFFFFFFFF;
    3008            1 :     session_info = &spdm_context->session_info[0];
    3009            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3010            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3011            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3012            1 :     libspdm_secured_message_set_response_finished_key(
    3013              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3014              :         hash_size);
    3015            1 :     libspdm_secured_message_set_session_state(
    3016              :         session_info->secured_message_context,
    3017              :         LIBSPDM_SESSION_STATE_NOT_STARTED);
    3018              : 
    3019            1 :     spdm_context->connection_info.capability.flags |=
    3020              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3021            1 :     spdm_context->local_context.capability.flags |=
    3022              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3023            1 :     req_slot_id_param = 0;
    3024            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3025              :                                          req_slot_id_param);
    3026            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    3027            1 :     free(data);
    3028            1 : }
    3029              : 
    3030              : /**
    3031              :  * Test 16: receiving a correct FINISH_RSP message with a correct MAC,
    3032              :  * mutual authentication and 'handshake in the clear'.
    3033              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3034              :  * session is established.
    3035              :  **/
    3036            1 : void libspdm_test_requester_finish_case16(void **state)
    3037              : {
    3038              :     libspdm_return_t status;
    3039              :     libspdm_test_context_t *spdm_test_context;
    3040              :     libspdm_context_t *spdm_context;
    3041              :     uint32_t session_id;
    3042              :     uint8_t req_slot_id_param;
    3043              :     void *data;
    3044              :     size_t data_size;
    3045              :     void *hash;
    3046              :     size_t hash_size;
    3047              :     libspdm_session_info_t *session_info;
    3048              : 
    3049            1 :     spdm_test_context = *state;
    3050            1 :     spdm_context = spdm_test_context->spdm_context;
    3051            1 :     spdm_test_context->case_id = 0x10;
    3052            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3053              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3054            1 :     spdm_context->connection_info.connection_state =
    3055              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3056            1 :     spdm_context->connection_info.capability.flags |=
    3057              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3058            1 :     spdm_context->connection_info.capability.flags |=
    3059              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3060            1 :     spdm_context->connection_info.capability.flags |=
    3061              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3062            1 :     spdm_context->local_context.capability.flags |=
    3063              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3064            1 :     spdm_context->local_context.capability.flags |=
    3065              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3066            1 :     spdm_context->local_context.capability.flags |=
    3067              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3068            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3069              :                                                     m_libspdm_use_asym_algo, &data,
    3070              :                                                     &data_size, &hash, &hash_size);
    3071            1 :     libspdm_reset_message_a(spdm_context);
    3072            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3073              :         m_libspdm_use_hash_algo;
    3074            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3075              :         m_libspdm_use_asym_algo;
    3076            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3077              :         m_libspdm_use_req_asym_algo;
    3078            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3079              :         m_libspdm_use_dhe_algo;
    3080            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3081              :         m_libspdm_use_aead_algo;
    3082              : 
    3083              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3084              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3085              :         data_size;
    3086              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3087              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3088              :                      data, data_size);
    3089              : #else
    3090            1 :     libspdm_hash_all(
    3091              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3092              :         data, data_size,
    3093            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3094            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3095            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3096            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3097              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3098              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3099              :         data, data_size,
    3100              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3101              : #endif
    3102            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3103              : 
    3104            1 :     req_slot_id_param = 0;
    3105            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3106              :                                                     m_libspdm_use_req_asym_algo, &data,
    3107              :                                                     &data_size, &hash, &hash_size);
    3108              :     spdm_context->local_context.
    3109            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3110              :     spdm_context->local_context.
    3111            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3112              : 
    3113            1 :     session_id = 0xFFFFFFFF;
    3114            1 :     session_info = &spdm_context->session_info[0];
    3115            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3116            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3117            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3118            1 :     libspdm_secured_message_set_response_finished_key(
    3119              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3120              :         hash_size);
    3121            1 :     libspdm_secured_message_set_session_state(
    3122              :         session_info->secured_message_context,
    3123              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3124            1 :     session_info->mut_auth_requested = 1;
    3125              : 
    3126            1 :     spdm_context->connection_info.capability.flags |=
    3127              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3128            1 :     spdm_context->local_context.capability.flags |=
    3129              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3130              : 
    3131            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3132              :                                          req_slot_id_param);
    3133            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3134            1 :     assert_int_equal(
    3135              :         libspdm_secured_message_get_session_state(
    3136              :             spdm_context->session_info[0].secured_message_context),
    3137              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3138            1 :     free(data);
    3139            1 : }
    3140              : 
    3141              : /**
    3142              :  * Test 17: receiving a FINISH_RSP message with an incorrect MAC
    3143              :  * (all-zero), mutual authentication, and 'handshake in the clear'.
    3144              :  * Expected behavior: client returns a Status of RETURN_SECURITY_VIOLATION.
    3145              :  **/
    3146            1 : void libspdm_test_requester_finish_case17(void **state)
    3147              : {
    3148              :     libspdm_return_t status;
    3149              :     libspdm_test_context_t *spdm_test_context;
    3150              :     libspdm_context_t *spdm_context;
    3151              :     uint32_t session_id;
    3152              :     uint8_t req_slot_id_param;
    3153              :     void *data;
    3154              :     size_t data_size;
    3155              :     void *hash;
    3156              :     size_t hash_size;
    3157              :     libspdm_session_info_t *session_info;
    3158              : 
    3159            1 :     spdm_test_context = *state;
    3160            1 :     spdm_context = spdm_test_context->spdm_context;
    3161            1 :     spdm_test_context->case_id = 0x11;
    3162            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3163              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3164            1 :     spdm_context->connection_info.connection_state =
    3165              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3166            1 :     spdm_context->connection_info.capability.flags |=
    3167              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3168            1 :     spdm_context->connection_info.capability.flags |=
    3169              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3170            1 :     spdm_context->connection_info.capability.flags |=
    3171              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3172            1 :     spdm_context->local_context.capability.flags |=
    3173              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3174            1 :     spdm_context->local_context.capability.flags |=
    3175              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3176            1 :     spdm_context->local_context.capability.flags |=
    3177              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3178            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3179              :                                                     m_libspdm_use_asym_algo, &data,
    3180              :                                                     &data_size, &hash, &hash_size);
    3181            1 :     libspdm_reset_message_a(spdm_context);
    3182            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3183              :         m_libspdm_use_hash_algo;
    3184            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3185              :         m_libspdm_use_asym_algo;
    3186            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3187              :         m_libspdm_use_req_asym_algo;
    3188            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3189              :         m_libspdm_use_dhe_algo;
    3190            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3191              :         m_libspdm_use_aead_algo;
    3192              : 
    3193              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3194              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3195              :         data_size;
    3196              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3197              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3198              :                      data, data_size);
    3199              : #else
    3200            1 :     libspdm_hash_all(
    3201              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3202              :         data, data_size,
    3203            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3204            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3205            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3206            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3207              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3208              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3209              :         data, data_size,
    3210              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3211              : #endif
    3212            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3213              : 
    3214            1 :     req_slot_id_param = 0;
    3215            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3216              :                                                     m_libspdm_use_req_asym_algo, &data,
    3217              :                                                     &data_size, &hash, &hash_size);
    3218              :     spdm_context->local_context.
    3219            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3220              :     spdm_context->local_context.
    3221            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3222              : 
    3223            1 :     session_id = 0xFFFFFFFF;
    3224            1 :     session_info = &spdm_context->session_info[0];
    3225            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3226            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3227            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3228            1 :     libspdm_secured_message_set_response_finished_key(
    3229              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3230              :         hash_size);
    3231            1 :     libspdm_secured_message_set_session_state(
    3232              :         session_info->secured_message_context,
    3233              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3234            1 :     session_info->mut_auth_requested = 1;
    3235              : 
    3236            1 :     spdm_context->connection_info.capability.flags |=
    3237              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3238            1 :     spdm_context->local_context.capability.flags |=
    3239              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3240              : 
    3241            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3242              :                                          req_slot_id_param);
    3243            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    3244            1 :     free(data);
    3245            1 : }
    3246              : 
    3247              : /**
    3248              :  * Test 18: receiving a FINISH_RSP message with an incorrect MAC
    3249              :  * (arbitrary), mutual authentication, and 'handshake in the clear'.
    3250              :  * Expected behavior: client returns a Status of RETURN_SECURITY_VIOLATION.
    3251              :  **/
    3252            1 : void libspdm_test_requester_finish_case18(void **state)
    3253              : {
    3254              :     libspdm_return_t status;
    3255              :     libspdm_test_context_t *spdm_test_context;
    3256              :     libspdm_context_t *spdm_context;
    3257              :     uint32_t session_id;
    3258              :     uint8_t req_slot_id_param;
    3259              :     void *data;
    3260              :     size_t data_size;
    3261              :     void *hash;
    3262              :     size_t hash_size;
    3263              :     libspdm_session_info_t *session_info;
    3264              : 
    3265            1 :     spdm_test_context = *state;
    3266            1 :     spdm_context = spdm_test_context->spdm_context;
    3267            1 :     spdm_test_context->case_id = 0x12;
    3268            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3269              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3270            1 :     spdm_context->connection_info.connection_state =
    3271              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3272            1 :     spdm_context->connection_info.capability.flags |=
    3273              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3274            1 :     spdm_context->connection_info.capability.flags |=
    3275              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3276            1 :     spdm_context->connection_info.capability.flags |=
    3277              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3278            1 :     spdm_context->local_context.capability.flags |=
    3279              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3280            1 :     spdm_context->local_context.capability.flags |=
    3281              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3282            1 :     spdm_context->local_context.capability.flags |=
    3283              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3284            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3285              :                                                     m_libspdm_use_asym_algo, &data,
    3286              :                                                     &data_size, &hash, &hash_size);
    3287            1 :     libspdm_reset_message_a(spdm_context);
    3288            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3289              :         m_libspdm_use_hash_algo;
    3290            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3291              :         m_libspdm_use_asym_algo;
    3292            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3293              :         m_libspdm_use_req_asym_algo;
    3294            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3295              :         m_libspdm_use_dhe_algo;
    3296            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3297              :         m_libspdm_use_aead_algo;
    3298              : 
    3299              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3300              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3301              :         data_size;
    3302              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3303              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3304              :                      data, data_size);
    3305              : #else
    3306            1 :     libspdm_hash_all(
    3307              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3308              :         data, data_size,
    3309            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3310            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3311            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3312            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3313              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3314              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3315              :         data, data_size,
    3316              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3317              : #endif
    3318            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3319              : 
    3320            1 :     req_slot_id_param = 0;
    3321            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3322              :                                                     m_libspdm_use_req_asym_algo, &data,
    3323              :                                                     &data_size, &hash, &hash_size);
    3324              :     spdm_context->local_context.
    3325            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3326              :     spdm_context->local_context.
    3327            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3328              : 
    3329            1 :     session_id = 0xFFFFFFFF;
    3330            1 :     session_info = &spdm_context->session_info[0];
    3331            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3332            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3333            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3334            1 :     libspdm_secured_message_set_response_finished_key(
    3335              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3336              :         hash_size);
    3337            1 :     libspdm_secured_message_set_session_state(
    3338              :         session_info->secured_message_context,
    3339              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3340            1 :     session_info->mut_auth_requested = 1;
    3341              : 
    3342            1 :     spdm_context->connection_info.capability.flags |=
    3343              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3344            1 :     spdm_context->local_context.capability.flags |=
    3345              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3346              : 
    3347            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3348              :                                          req_slot_id_param);
    3349            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    3350            1 :     free(data);
    3351            1 : }
    3352              : 
    3353              : /**
    3354              :  * Test 19:
    3355              :  * Expected behavior:
    3356              :  **/
    3357            1 : void libspdm_test_requester_finish_case19(void **state)
    3358              : {
    3359            1 : }
    3360              : 
    3361              : /**
    3362              :  * Test 20: receiving a FINISH_RSP message an incorrect MAC size (only the
    3363              :  * correct first half of the MAC), mutual authentication, and 'handshake
    3364              :  * in the clear'.
    3365              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    3366              :  **/
    3367            1 : void libspdm_test_requester_finish_case20(void **state)
    3368              : {
    3369              :     libspdm_return_t status;
    3370              :     libspdm_test_context_t *spdm_test_context;
    3371              :     libspdm_context_t *spdm_context;
    3372              :     uint32_t session_id;
    3373              :     uint8_t req_slot_id_param;
    3374              :     void *data;
    3375              :     size_t data_size;
    3376              :     void *hash;
    3377              :     size_t hash_size;
    3378              :     libspdm_session_info_t *session_info;
    3379              : 
    3380            1 :     spdm_test_context = *state;
    3381            1 :     spdm_context = spdm_test_context->spdm_context;
    3382            1 :     spdm_test_context->case_id = 0x14;
    3383            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3384              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3385            1 :     spdm_context->connection_info.connection_state =
    3386              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3387            1 :     spdm_context->connection_info.capability.flags |=
    3388              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3389            1 :     spdm_context->connection_info.capability.flags |=
    3390              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3391            1 :     spdm_context->connection_info.capability.flags |=
    3392              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3393            1 :     spdm_context->local_context.capability.flags |=
    3394              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3395            1 :     spdm_context->local_context.capability.flags |=
    3396              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3397            1 :     spdm_context->local_context.capability.flags |=
    3398              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3399            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3400              :                                                     m_libspdm_use_asym_algo, &data,
    3401              :                                                     &data_size, &hash, &hash_size);
    3402            1 :     libspdm_reset_message_a(spdm_context);
    3403            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3404              :         m_libspdm_use_hash_algo;
    3405            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3406              :         m_libspdm_use_asym_algo;
    3407            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3408              :         m_libspdm_use_req_asym_algo;
    3409            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3410              :         m_libspdm_use_dhe_algo;
    3411            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3412              :         m_libspdm_use_aead_algo;
    3413              : 
    3414              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3415              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3416              :         data_size;
    3417              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3418              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3419              :                      data, data_size);
    3420              : #else
    3421            1 :     libspdm_hash_all(
    3422              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3423              :         data, data_size,
    3424            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3425            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3426            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3427            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3428              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3429              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3430              :         data, data_size,
    3431              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3432              : #endif
    3433            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3434              : 
    3435            1 :     req_slot_id_param = 0;
    3436            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3437              :                                                     m_libspdm_use_req_asym_algo, &data,
    3438              :                                                     &data_size, &hash, &hash_size);
    3439              :     spdm_context->local_context.
    3440            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3441              :     spdm_context->local_context.
    3442            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3443              : 
    3444            1 :     session_id = 0xFFFFFFFF;
    3445            1 :     session_info = &spdm_context->session_info[0];
    3446            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3447            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3448            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3449            1 :     libspdm_secured_message_set_response_finished_key(
    3450              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3451              :         hash_size);
    3452            1 :     libspdm_secured_message_set_session_state(
    3453              :         session_info->secured_message_context,
    3454              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3455            1 :     session_info->mut_auth_requested = 1;
    3456              : 
    3457            1 :     spdm_context->connection_info.capability.flags |=
    3458              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3459            1 :     spdm_context->local_context.capability.flags |=
    3460              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3461              : 
    3462            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3463              :                                          req_slot_id_param);
    3464            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    3465            1 :     free(data);
    3466            1 : }
    3467              : 
    3468              : /**
    3469              :  * Test 21: the requester is setup correctly, but receives an ERROR with SPDM_ERROR_CODE_DECRYPT_ERROR.
    3470              :  * Expected behavior: client returns a Status of INVALID_SESSION_ID  and free the session ID.
    3471              :  **/
    3472            1 : void libspdm_test_requester_finish_case21(void **state)
    3473              : {
    3474              :     libspdm_return_t status;
    3475              :     libspdm_test_context_t *spdm_test_context;
    3476              :     libspdm_context_t *spdm_context;
    3477              :     uint32_t session_id;
    3478              :     uint8_t req_slot_id_param;
    3479              :     void *data;
    3480              :     size_t data_size;
    3481              :     void *hash;
    3482              :     size_t hash_size;
    3483              :     libspdm_session_info_t *session_info;
    3484              : 
    3485            1 :     spdm_test_context = *state;
    3486            1 :     spdm_context = spdm_test_context->spdm_context;
    3487            1 :     spdm_test_context->case_id = 0x15;
    3488            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3489              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3490            1 :     spdm_context->connection_info.connection_state =
    3491              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3492            1 :     spdm_context->connection_info.capability.flags |=
    3493              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3494            1 :     spdm_context->connection_info.capability.flags |=
    3495              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3496            1 :     spdm_context->connection_info.capability.flags |=
    3497              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3498            1 :     spdm_context->local_context.capability.flags |=
    3499              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3500            1 :     spdm_context->local_context.capability.flags |=
    3501              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3502            1 :     spdm_context->local_context.capability.flags |=
    3503              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3504            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3505              :                                                     m_libspdm_use_asym_algo, &data,
    3506              :                                                     &data_size, &hash, &hash_size);
    3507            1 :     libspdm_reset_message_a(spdm_context);
    3508            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3509              :         m_libspdm_use_hash_algo;
    3510            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3511              :         m_libspdm_use_asym_algo;
    3512            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3513              :         m_libspdm_use_dhe_algo;
    3514            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3515              :         m_libspdm_use_aead_algo;
    3516              : 
    3517              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3518              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3519              :         data_size;
    3520              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3521              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3522              :                      data, data_size);
    3523              : #else
    3524            1 :     libspdm_hash_all(
    3525              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3526              :         data, data_size,
    3527            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3528            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3529            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3530            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3531              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3532              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3533              :         data, data_size,
    3534              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3535              : #endif
    3536            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3537              : 
    3538            1 :     session_id = 0xFFFFFFFF;
    3539            1 :     session_info = &spdm_context->session_info[0];
    3540            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3541            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3542            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3543            1 :     libspdm_secured_message_set_response_finished_key(
    3544              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3545              :         hash_size);
    3546            1 :     libspdm_secured_message_set_session_state(
    3547              :         session_info->secured_message_context,
    3548              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3549              : 
    3550            1 :     spdm_context->connection_info.capability.flags |=
    3551              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3552            1 :     spdm_context->local_context.capability.flags |=
    3553              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3554            1 :     req_slot_id_param = 0;
    3555            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3556              :                                          req_slot_id_param);
    3557            1 :     assert_int_equal(status, LIBSPDM_STATUS_SESSION_MSG_ERROR);
    3558            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    3559            1 :     free(data);
    3560            1 : }
    3561              : 
    3562              : /**
    3563              :  * Test 22: a FINISH request message is successfully sent and a FINISH_RSP response message is
    3564              :  * successfully received.
    3565              :  * Expected Behavior: requester returns the status RETURN_SUCCESS and a FINISH_RSP message is
    3566              :  * received, buffer F appends the exchanged FINISH and FINISH_RSP
    3567              :  **/
    3568            1 : void libspdm_test_requester_finish_case22(void **state)
    3569              : {
    3570              :     libspdm_return_t status;
    3571              :     libspdm_test_context_t *spdm_test_context;
    3572              :     libspdm_context_t *spdm_context;
    3573              :     uint32_t session_id;
    3574              :     uint8_t req_slot_id_param;
    3575              :     void *data;
    3576              :     size_t data_size;
    3577              :     void *hash;
    3578              :     size_t hash_size;
    3579              :     libspdm_session_info_t *session_info;
    3580              : 
    3581            1 :     spdm_test_context = *state;
    3582            1 :     spdm_context = spdm_test_context->spdm_context;
    3583            1 :     spdm_test_context->case_id = 0x16;
    3584            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3585              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3586            1 :     spdm_context->connection_info.connection_state =
    3587              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3588            1 :     spdm_context->connection_info.capability.flags |=
    3589              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3590            1 :     spdm_context->connection_info.capability.flags |=
    3591              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3592            1 :     spdm_context->connection_info.capability.flags |=
    3593              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3594            1 :     spdm_context->local_context.capability.flags |=
    3595              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3596            1 :     spdm_context->local_context.capability.flags |=
    3597              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3598            1 :     spdm_context->local_context.capability.flags |=
    3599              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3600            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3601              :                                                     m_libspdm_use_asym_algo, &data,
    3602              :                                                     &data_size, &hash, &hash_size);
    3603            1 :     libspdm_reset_message_a(spdm_context);
    3604            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    3605            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    3606            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
    3607            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    3608            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    3609              : 
    3610              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3611              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3612              :         data_size;
    3613              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3614              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3615              :                      data, data_size);
    3616              : #else
    3617            1 :     libspdm_hash_all(
    3618              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3619              :         data, data_size,
    3620            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3621            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3622            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3623            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3624              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3625              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3626              :         data, data_size,
    3627              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3628              : #endif
    3629            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3630              : 
    3631            1 :     req_slot_id_param = 0;
    3632            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3633              :                                                     m_libspdm_use_req_asym_algo, &data,
    3634              :                                                     &data_size, &hash, &hash_size);
    3635              :     spdm_context->local_context.
    3636            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3637              :     spdm_context->local_context.
    3638            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3639              : 
    3640            1 :     session_id = 0xFFFFFFFF;
    3641            1 :     session_info = &spdm_context->session_info[0];
    3642            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3643            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3644            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3645            1 :     libspdm_secured_message_set_response_finished_key(
    3646              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3647              :         hash_size);
    3648            1 :     libspdm_secured_message_set_session_state(
    3649              :         session_info->secured_message_context,
    3650              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3651            1 :     session_info->mut_auth_requested = 1;
    3652              : 
    3653            1 :     spdm_context->connection_info.capability.flags |=
    3654              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3655            1 :     spdm_context->local_context.capability.flags |=
    3656              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3657              : 
    3658            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3659              :                                          req_slot_id_param);
    3660            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3661            1 :     assert_int_equal(
    3662              :         libspdm_secured_message_get_session_state(
    3663              :             spdm_context->session_info[0].secured_message_context),
    3664              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3665              : 
    3666              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3667              :     assert_int_equal(spdm_context->session_info[0].session_transcript.message_f.buffer_size,
    3668              :                      m_libspdm_local_buffer_size);
    3669              :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%x):\n",
    3670              :                    m_libspdm_local_buffer_size));
    3671              :     libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3672              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_f.buffer,
    3673              :                         m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3674              : #endif
    3675            1 :     free(data);
    3676            1 : }
    3677              : 
    3678              : /**
    3679              :  * Test 23: receiving a correct FINISH_RSP message using slot_id 0xFF
    3680              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3681              :  * session is established.
    3682              :  **/
    3683            1 : void libspdm_test_requester_finish_case23(void **state)
    3684              : {
    3685              :     libspdm_return_t status;
    3686              :     libspdm_test_context_t *spdm_test_context;
    3687              :     libspdm_context_t *spdm_context;
    3688              :     uint32_t session_id;
    3689              :     uint8_t req_slot_id_param;
    3690              :     void *data;
    3691              :     size_t data_size;
    3692              :     size_t hash_size;
    3693              :     libspdm_session_info_t *session_info;
    3694              :     libspdm_secured_message_context_t *secured_message_context;
    3695              : 
    3696            1 :     spdm_test_context = *state;
    3697            1 :     spdm_context = spdm_test_context->spdm_context;
    3698            1 :     spdm_test_context->case_id = 0x17;
    3699            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3700              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3701            1 :     spdm_context->connection_info.connection_state =
    3702              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3703            1 :     spdm_context->connection_info.capability.flags |=
    3704              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3705            1 :     spdm_context->connection_info.capability.flags |=
    3706              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3707            1 :     spdm_context->connection_info.capability.flags |=
    3708              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3709            1 :     spdm_context->connection_info.capability.flags |=
    3710              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PUB_KEY_ID_CAP;
    3711            1 :     spdm_context->local_context.capability.flags |=
    3712              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3713            1 :     spdm_context->local_context.capability.flags |=
    3714              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3715            1 :     spdm_context->local_context.capability.flags |=
    3716              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3717            1 :     spdm_context->local_context.capability.flags |=
    3718              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
    3719            1 :     libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data, &data_size);
    3720            1 :     spdm_context->local_context.peer_public_key_provision = data;
    3721            1 :     spdm_context->local_context.peer_public_key_provision_size = data_size;
    3722              : 
    3723            1 :     libspdm_reset_message_a(spdm_context);
    3724            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3725              :         m_libspdm_use_hash_algo;
    3726            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3727              :         m_libspdm_use_asym_algo;
    3728            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3729              :         m_libspdm_use_dhe_algo;
    3730            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3731              :         m_libspdm_use_aead_algo;
    3732              : 
    3733            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0xFF;
    3734              : 
    3735            1 :     session_id = 0xFFFFFFFF;
    3736            1 :     session_info = &spdm_context->session_info[0];
    3737            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3738            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3739            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3740            1 :     libspdm_secured_message_set_response_finished_key(
    3741              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3742              :         hash_size);
    3743            1 :     libspdm_secured_message_set_session_state(
    3744              :         session_info->secured_message_context,
    3745              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3746              : 
    3747            1 :     spdm_context->connection_info.capability.flags |=
    3748              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3749            1 :     spdm_context->local_context.capability.flags |=
    3750              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3751            1 :     req_slot_id_param = 0;
    3752            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    3753            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3754            1 :     assert_int_equal(
    3755              :         libspdm_secured_message_get_session_state(
    3756              :             spdm_context->session_info[0].secured_message_context),
    3757              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3758              : 
    3759            1 :     secured_message_context = session_info->secured_message_context;
    3760              : 
    3761            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    3762              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    3763            1 :     free(data);
    3764            1 : }
    3765              : 
    3766              : /**
    3767              :  * Test 24: Set HANDSHAKE_IN_THE_CLEAR_CAP to 0 , The ResponderVerifyData field is absent.
    3768              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3769              :  * session is established.
    3770              :  **/
    3771            1 : void libspdm_test_requester_finish_case24(void **state)
    3772              : {
    3773              :     libspdm_return_t status;
    3774              :     libspdm_test_context_t *spdm_test_context;
    3775              :     libspdm_context_t *spdm_context;
    3776              :     uint32_t session_id;
    3777              :     uint8_t req_slot_id_param;
    3778              :     void *data;
    3779              :     size_t data_size;
    3780              :     void *hash;
    3781              :     size_t hash_size;
    3782              :     libspdm_session_info_t *session_info;
    3783              :     libspdm_secured_message_context_t *secured_message_context;
    3784              : 
    3785            1 :     spdm_test_context = *state;
    3786            1 :     spdm_context = spdm_test_context->spdm_context;
    3787            1 :     spdm_test_context->case_id = 0x18;
    3788            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3789              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3790            1 :     spdm_context->connection_info.connection_state =
    3791              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3792            1 :     spdm_context->connection_info.capability.flags |=
    3793              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3794            1 :     spdm_context->connection_info.capability.flags |=
    3795              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3796            1 :     spdm_context->connection_info.capability.flags |=
    3797              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3798            1 :     spdm_context->local_context.capability.flags |=
    3799              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3800            1 :     spdm_context->local_context.capability.flags |=
    3801              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3802            1 :     spdm_context->local_context.capability.flags |=
    3803              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3804            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3805              :                                                     m_libspdm_use_asym_algo, &data,
    3806              :                                                     &data_size, &hash, &hash_size);
    3807            1 :     libspdm_reset_message_a(spdm_context);
    3808            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3809              :         m_libspdm_use_hash_algo;
    3810            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3811              :         m_libspdm_use_asym_algo;
    3812            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3813              :         m_libspdm_use_dhe_algo;
    3814            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3815              :         m_libspdm_use_aead_algo;
    3816              : 
    3817              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3818              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3819              :         data_size;
    3820              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3821              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3822              :                      data, data_size);
    3823              : #else
    3824            1 :     libspdm_hash_all(
    3825              :         m_libspdm_use_hash_algo,
    3826              :         data, data_size,
    3827            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3828            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3829            1 :         libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3830            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3831              :         m_libspdm_use_hash_algo,
    3832              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3833              :         data, data_size,
    3834              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3835              : #endif
    3836            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3837              : 
    3838              :     /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0*/
    3839            1 :     spdm_context->connection_info.capability.flags &=
    3840              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3841            1 :     spdm_context->local_context.capability.flags &=
    3842              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3843              : 
    3844            1 :     session_id = 0xFFFFFFFF;
    3845            1 :     session_info = &spdm_context->session_info[0];
    3846            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    3847            1 :     spdm_context->last_spdm_request_session_id = session_id;
    3848            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3849            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3850            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3851            1 :     libspdm_secured_message_set_response_finished_key(
    3852              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3853              :         hash_size);
    3854            1 :     libspdm_secured_message_set_session_state(
    3855              :         session_info->secured_message_context,
    3856              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3857              : 
    3858            1 :     req_slot_id_param = 0;
    3859            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    3860            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3861            1 :     assert_int_equal(
    3862              :         libspdm_secured_message_get_session_state(
    3863              :             spdm_context->session_info[0].secured_message_context),
    3864              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3865              : 
    3866            1 :     secured_message_context = session_info->secured_message_context;
    3867              : 
    3868            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    3869              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    3870            1 :     free(data);
    3871            1 : }
    3872              : 
    3873              : /**
    3874              :  * Test 25: SPDM version 1.4, with OpaqueData
    3875              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3876              :  * session is established.
    3877              :  **/
    3878            1 : void libspdm_test_requester_finish_case25(void **state)
    3879              : {
    3880              :     libspdm_return_t status;
    3881              :     libspdm_test_context_t *spdm_test_context;
    3882              :     libspdm_context_t *spdm_context;
    3883              :     uint32_t session_id;
    3884              :     uint8_t req_slot_id_param;
    3885              :     void *data;
    3886              :     size_t data_size;
    3887              :     void *hash;
    3888              :     size_t hash_size;
    3889              :     libspdm_session_info_t *session_info;
    3890              :     libspdm_secured_message_context_t *secured_message_context;
    3891              : 
    3892            1 :     spdm_test_context = *state;
    3893            1 :     spdm_context = spdm_test_context->spdm_context;
    3894            1 :     spdm_test_context->case_id = 0x19;
    3895            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
    3896              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3897            1 :     spdm_context->connection_info.connection_state =
    3898              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3899            1 :     spdm_context->connection_info.capability.flags |=
    3900              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3901            1 :     spdm_context->connection_info.capability.flags |=
    3902              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3903            1 :     spdm_context->connection_info.capability.flags |=
    3904              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3905            1 :     spdm_context->local_context.capability.flags |=
    3906              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3907            1 :     spdm_context->local_context.capability.flags |=
    3908              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3909            1 :     spdm_context->local_context.capability.flags |=
    3910              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3911            1 :     libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3912              :                                                     m_libspdm_use_asym_algo, &data,
    3913              :                                                     &data_size, &hash, &hash_size);
    3914            1 :     libspdm_reset_message_a(spdm_context);
    3915            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3916              :         m_libspdm_use_hash_algo;
    3917            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3918              :         m_libspdm_use_asym_algo;
    3919            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3920              :         m_libspdm_use_dhe_algo;
    3921            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3922              :         m_libspdm_use_aead_algo;
    3923              : 
    3924              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3925              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3926              :         data_size;
    3927              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3928              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3929              :                      data, data_size);
    3930              : #else
    3931            1 :     libspdm_hash_all(
    3932              :         m_libspdm_use_hash_algo,
    3933              :         data, data_size,
    3934            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3935            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3936            1 :         libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3937            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3938              :         m_libspdm_use_hash_algo,
    3939              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3940              :         data, data_size,
    3941              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3942              : #endif
    3943            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3944              : 
    3945              :     /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0*/
    3946            1 :     spdm_context->connection_info.capability.flags &=
    3947              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3948            1 :     spdm_context->local_context.capability.flags &=
    3949              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3950              : 
    3951            1 :     session_id = 0xFFFFFFFF;
    3952            1 :     session_info = &spdm_context->session_info[0];
    3953            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    3954            1 :     spdm_context->last_spdm_request_session_id = session_id;
    3955            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3956            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3957            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3958            1 :     libspdm_secured_message_set_response_finished_key(
    3959              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3960              :         hash_size);
    3961            1 :     libspdm_secured_message_set_session_state(
    3962              :         session_info->secured_message_context,
    3963              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3964              : 
    3965            1 :     req_slot_id_param = 0;
    3966            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    3967            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3968            1 :     assert_int_equal(
    3969              :         libspdm_secured_message_get_session_state(
    3970              :             spdm_context->session_info[0].secured_message_context),
    3971              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3972              : 
    3973            1 :     secured_message_context = session_info->secured_message_context;
    3974              : 
    3975            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    3976              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    3977            1 :     free(data);
    3978            1 : }
    3979              : 
    3980            1 : int libspdm_requester_finish_test_main(void)
    3981              : {
    3982            1 :     const struct CMUnitTest spdm_requester_finish_tests[] = {
    3983              :         /* SendRequest failed*/
    3984              :         cmocka_unit_test(libspdm_test_requester_finish_case1),
    3985              :         /* Successful response*/
    3986              :         cmocka_unit_test(libspdm_test_requester_finish_case2),
    3987              :         /* connection_state check failed*/
    3988              :         cmocka_unit_test(libspdm_test_requester_finish_case3),
    3989              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    3990              :         cmocka_unit_test(libspdm_test_requester_finish_case4),
    3991              :         /* Always SPDM_ERROR_CODE_BUSY*/
    3992              :         cmocka_unit_test(libspdm_test_requester_finish_case5),
    3993              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    3994              :         cmocka_unit_test(libspdm_test_requester_finish_case6),
    3995              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    3996              :         cmocka_unit_test(libspdm_test_requester_finish_case7),
    3997              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    3998              :         cmocka_unit_test(libspdm_test_requester_finish_case8),
    3999              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    4000              :         cmocka_unit_test(libspdm_test_requester_finish_case9),
    4001              :         /* Unexpected errors*/
    4002              :         cmocka_unit_test(libspdm_test_requester_finish_case10),
    4003              :         /* Buffer reset*/
    4004              :         cmocka_unit_test(libspdm_test_requester_finish_case11),
    4005              :         /* No correct setup*/
    4006              :         cmocka_unit_test(libspdm_test_requester_finish_case12),
    4007              :         cmocka_unit_test(libspdm_test_requester_finish_case13),
    4008              :         cmocka_unit_test(libspdm_test_requester_finish_case14),
    4009              :         cmocka_unit_test(libspdm_test_requester_finish_case15),
    4010              :         /* Successful response*/
    4011              :         cmocka_unit_test(libspdm_test_requester_finish_case16),
    4012              :         /* Response with invalid MAC*/
    4013              :         cmocka_unit_test(libspdm_test_requester_finish_case17),
    4014              :         cmocka_unit_test(libspdm_test_requester_finish_case18),
    4015              :         /* Can be populated with new test.*/
    4016              :         cmocka_unit_test(libspdm_test_requester_finish_case19),
    4017              :         cmocka_unit_test(libspdm_test_requester_finish_case20),
    4018              :         /* Error response: SPDM_ERROR_CODE_DECRYPT_ERROR*/
    4019              :         cmocka_unit_test(libspdm_test_requester_finish_case21),
    4020              :         /* Buffer verification*/
    4021              :         cmocka_unit_test(libspdm_test_requester_finish_case22),
    4022              :         /* Successful response using provisioned public key (slot_id 0xFF) */
    4023              :         cmocka_unit_test(libspdm_test_requester_finish_case23),
    4024              :         /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0 , The ResponderVerifyData field is absent.*/
    4025              :         cmocka_unit_test(libspdm_test_requester_finish_case24),
    4026              :         /* SPDM 1.4 with OpaqueData */
    4027              :         cmocka_unit_test(libspdm_test_requester_finish_case25),
    4028              :     };
    4029              : 
    4030            1 :     libspdm_test_context_t test_context = {
    4031              :         LIBSPDM_TEST_CONTEXT_VERSION,
    4032              :         true,
    4033              :         libspdm_requester_finish_test_send_message,
    4034              :         libspdm_requester_finish_test_receive_message,
    4035              :     };
    4036              : 
    4037            1 :     libspdm_setup_test_context(&test_context);
    4038              : 
    4039            1 :     return cmocka_run_group_tests(spdm_requester_finish_tests,
    4040              :                                   libspdm_unit_test_group_setup,
    4041              :                                   libspdm_unit_test_group_teardown);
    4042              : }
    4043              : 
    4044              : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
        

Generated by: LCOV version 2.0-1