LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - finish.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.6 % 1752 1569
Test Date: 2025-09-14 08:11:04 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 :     if (!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            0 :         assert(false);
    1601              :     }
    1602            1 :     libspdm_reset_message_a(spdm_context);
    1603            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1604              :         m_libspdm_use_hash_algo;
    1605            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1606              :         m_libspdm_use_asym_algo;
    1607            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1608              :         m_libspdm_use_dhe_algo;
    1609            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1610              :         m_libspdm_use_aead_algo;
    1611              : 
    1612              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1613              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1614              :         data_size;
    1615              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1616              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1617              :                      data, data_size);
    1618              : #else
    1619            1 :     libspdm_hash_all(
    1620              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1621              :         data, data_size,
    1622            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1623            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1624            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1625            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1626              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1627              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1628              :         data, data_size,
    1629              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1630              : #endif
    1631            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1632              : 
    1633            1 :     session_id = 0xFFFFFFFF;
    1634            1 :     session_info = &spdm_context->session_info[0];
    1635            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1636            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1637            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1638            1 :     libspdm_secured_message_set_response_finished_key(
    1639              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1640              :         hash_size);
    1641            1 :     libspdm_secured_message_set_session_state(
    1642              :         session_info->secured_message_context,
    1643              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1644              : 
    1645            1 :     spdm_context->connection_info.capability.flags |=
    1646              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1647            1 :     spdm_context->local_context.capability.flags |=
    1648              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1649            1 :     req_slot_id_param = 0;
    1650            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1651              :                                          req_slot_id_param);
    1652            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
    1653            1 :     free(data);
    1654            1 : }
    1655              : 
    1656              : /**
    1657              :  * Test 2: receiving a correct FINISH_RSP message with only MAC (no
    1658              :  * mutual authentication) and 'handshake in the clear'.
    1659              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    1660              :  * session is established.
    1661              :  **/
    1662            1 : void libspdm_test_requester_finish_case2(void **state)
    1663              : {
    1664              :     libspdm_return_t status;
    1665              :     libspdm_test_context_t *spdm_test_context;
    1666              :     libspdm_context_t *spdm_context;
    1667              :     uint32_t session_id;
    1668              :     uint8_t req_slot_id_param;
    1669              :     void *data;
    1670              :     size_t data_size;
    1671              :     void *hash;
    1672              :     size_t hash_size;
    1673              :     libspdm_session_info_t *session_info;
    1674              :     libspdm_secured_message_context_t *secured_message_context;
    1675              : 
    1676            1 :     spdm_test_context = *state;
    1677            1 :     spdm_context = spdm_test_context->spdm_context;
    1678            1 :     spdm_test_context->case_id = 0x2;
    1679            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1680              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1681            1 :     spdm_context->connection_info.connection_state =
    1682              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1683            1 :     spdm_context->connection_info.capability.flags |=
    1684              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1685            1 :     spdm_context->connection_info.capability.flags |=
    1686              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1687            1 :     spdm_context->connection_info.capability.flags |=
    1688              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1689            1 :     spdm_context->local_context.capability.flags |=
    1690              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1691            1 :     spdm_context->local_context.capability.flags |=
    1692              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1693            1 :     spdm_context->local_context.capability.flags |=
    1694              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1695            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1696              :                                                          m_libspdm_use_asym_algo, &data,
    1697              :                                                          &data_size, &hash, &hash_size)) {
    1698            0 :         assert(false);
    1699              :     }
    1700            1 :     libspdm_reset_message_a(spdm_context);
    1701            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1702              :         m_libspdm_use_hash_algo;
    1703            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1704              :         m_libspdm_use_asym_algo;
    1705            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1706              :         m_libspdm_use_dhe_algo;
    1707            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1708              :         m_libspdm_use_aead_algo;
    1709              : 
    1710              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1711              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1712              :         data_size;
    1713              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1714              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1715              :                      data, data_size);
    1716              : #else
    1717            1 :     libspdm_hash_all(
    1718              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1719              :         data, data_size,
    1720            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1721            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1722            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1723            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1724              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1725              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1726              :         data, data_size,
    1727              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1728              : #endif
    1729            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1730              : 
    1731            1 :     session_id = 0xFFFFFFFF;
    1732            1 :     session_info = &spdm_context->session_info[0];
    1733            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1734            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1735            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1736            1 :     libspdm_secured_message_set_response_finished_key(
    1737              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1738              :         hash_size);
    1739            1 :     libspdm_secured_message_set_session_state(
    1740              :         session_info->secured_message_context,
    1741              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1742              : 
    1743            1 :     spdm_context->connection_info.capability.flags |=
    1744              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1745            1 :     spdm_context->local_context.capability.flags |=
    1746              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1747            1 :     req_slot_id_param = 0;
    1748            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    1749            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1750            1 :     assert_int_equal(
    1751              :         libspdm_secured_message_get_session_state(
    1752              :             spdm_context->session_info[0].secured_message_context),
    1753              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1754              : 
    1755            1 :     secured_message_context = session_info->secured_message_context;
    1756              : 
    1757            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    1758              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    1759            1 :     free(data);
    1760            1 : }
    1761              : 
    1762              : /**
    1763              :  * Test 3: requester state has not been negotiated, as if GET_VERSION, GET_CAPABILITIES and
    1764              :  * NEGOTIATE_ALGORITHMS had not been exchanged.
    1765              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    1766              :  **/
    1767            1 : void libspdm_test_requester_finish_case3(void **state)
    1768              : {
    1769              :     libspdm_return_t status;
    1770              :     libspdm_test_context_t *spdm_test_context;
    1771              :     libspdm_context_t *spdm_context;
    1772              :     uint32_t session_id;
    1773              :     uint8_t req_slot_id_param;
    1774              :     void *data;
    1775              :     size_t data_size;
    1776              :     void *hash;
    1777              :     size_t hash_size;
    1778              :     libspdm_session_info_t *session_info;
    1779              : 
    1780            1 :     spdm_test_context = *state;
    1781            1 :     spdm_context = spdm_test_context->spdm_context;
    1782            1 :     spdm_test_context->case_id = 0x3;
    1783            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1784              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1785            1 :     spdm_context->connection_info.connection_state =
    1786              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
    1787            1 :     spdm_context->connection_info.capability.flags |=
    1788              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1789            1 :     spdm_context->connection_info.capability.flags |=
    1790              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1791            1 :     spdm_context->connection_info.capability.flags |=
    1792              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1793            1 :     spdm_context->local_context.capability.flags |=
    1794              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1795            1 :     spdm_context->local_context.capability.flags |=
    1796              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1797            1 :     spdm_context->local_context.capability.flags |=
    1798              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1799            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1800              :                                                          m_libspdm_use_asym_algo, &data,
    1801              :                                                          &data_size, &hash, &hash_size)) {
    1802            0 :         assert(false);
    1803              :     }
    1804            1 :     libspdm_reset_message_a(spdm_context);
    1805            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1806              :         m_libspdm_use_hash_algo;
    1807            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1808              :         m_libspdm_use_asym_algo;
    1809            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1810              :         m_libspdm_use_dhe_algo;
    1811            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1812              :         m_libspdm_use_aead_algo;
    1813              : 
    1814              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1815              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1816              :         data_size;
    1817              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1818              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1819              :                      data, data_size);
    1820              : #else
    1821            1 :     libspdm_hash_all(
    1822              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1823              :         data, data_size,
    1824            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1825            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1826            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1827            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1828              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1829              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1830              :         data, data_size,
    1831              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1832              : #endif
    1833            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1834              : 
    1835            1 :     session_id = 0xFFFFFFFF;
    1836            1 :     session_info = &spdm_context->session_info[0];
    1837            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1838            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1839            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1840            1 :     libspdm_secured_message_set_response_finished_key(
    1841              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1842              :         hash_size);
    1843            1 :     libspdm_secured_message_set_session_state(
    1844              :         session_info->secured_message_context,
    1845              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1846              : 
    1847            1 :     spdm_context->connection_info.capability.flags |=
    1848              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1849            1 :     spdm_context->local_context.capability.flags |=
    1850              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1851            1 :     req_slot_id_param = 0;
    1852            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1853              :                                          req_slot_id_param);
    1854            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    1855            1 :     free(data);
    1856            1 : }
    1857              : 
    1858              : /**
    1859              :  * Test 4: the requester is setup correctly (see Test 2), but receives an ERROR
    1860              :  * message indicating InvalidParameters.
    1861              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1862              :  **/
    1863            1 : void libspdm_test_requester_finish_case4(void **state)
    1864              : {
    1865              :     libspdm_return_t status;
    1866              :     libspdm_test_context_t *spdm_test_context;
    1867              :     libspdm_context_t *spdm_context;
    1868              :     uint32_t session_id;
    1869              :     uint8_t req_slot_id_param;
    1870              :     void *data;
    1871              :     size_t data_size;
    1872              :     void *hash;
    1873              :     size_t hash_size;
    1874              :     libspdm_session_info_t *session_info;
    1875              : 
    1876            1 :     spdm_test_context = *state;
    1877            1 :     spdm_context = spdm_test_context->spdm_context;
    1878            1 :     spdm_test_context->case_id = 0x4;
    1879            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1880              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1881            1 :     spdm_context->connection_info.connection_state =
    1882              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1883            1 :     spdm_context->connection_info.capability.flags |=
    1884              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1885            1 :     spdm_context->connection_info.capability.flags |=
    1886              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1887            1 :     spdm_context->connection_info.capability.flags |=
    1888              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1889            1 :     spdm_context->local_context.capability.flags |=
    1890              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1891            1 :     spdm_context->local_context.capability.flags |=
    1892              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1893            1 :     spdm_context->local_context.capability.flags |=
    1894              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1895            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1896              :                                                          m_libspdm_use_asym_algo, &data,
    1897              :                                                          &data_size, &hash, &hash_size)) {
    1898            0 :         assert(false);
    1899              :     }
    1900            1 :     libspdm_reset_message_a(spdm_context);
    1901            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1902              :         m_libspdm_use_hash_algo;
    1903            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1904              :         m_libspdm_use_asym_algo;
    1905            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1906              :         m_libspdm_use_dhe_algo;
    1907            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1908              :         m_libspdm_use_aead_algo;
    1909              : 
    1910              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1911              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1912              :         data_size;
    1913              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1914              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1915              :                      data, data_size);
    1916              : #else
    1917            1 :     libspdm_hash_all(
    1918              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1919              :         data, data_size,
    1920            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    1921            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    1922            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    1923            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    1924              :         spdm_context->connection_info.algorithm.base_hash_algo,
    1925              :         spdm_context->connection_info.algorithm.base_asym_algo,
    1926              :         data, data_size,
    1927              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    1928              : #endif
    1929            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    1930              : 
    1931            1 :     session_id = 0xFFFFFFFF;
    1932            1 :     session_info = &spdm_context->session_info[0];
    1933            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    1934            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    1935            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    1936            1 :     libspdm_secured_message_set_response_finished_key(
    1937              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    1938              :         hash_size);
    1939            1 :     libspdm_secured_message_set_session_state(
    1940              :         session_info->secured_message_context,
    1941              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1942              : 
    1943            1 :     spdm_context->connection_info.capability.flags |=
    1944              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1945            1 :     spdm_context->local_context.capability.flags |=
    1946              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    1947            1 :     req_slot_id_param = 0;
    1948            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    1949              :                                          req_slot_id_param);
    1950            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
    1951            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    1952            1 :     free(data);
    1953            1 : }
    1954              : 
    1955              : /**
    1956              :  * Test 5: the requester is setup correctly (see Test 2), but receives an ERROR
    1957              :  * message indicating the Busy status of the responder.
    1958              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1959              :  **/
    1960            1 : void libspdm_test_requester_finish_case5(void **state)
    1961              : {
    1962              :     libspdm_return_t status;
    1963              :     libspdm_test_context_t *spdm_test_context;
    1964              :     libspdm_context_t *spdm_context;
    1965              :     uint32_t session_id;
    1966              :     uint8_t req_slot_id_param;
    1967              :     void *data;
    1968              :     size_t data_size;
    1969              :     void *hash;
    1970              :     size_t hash_size;
    1971              :     libspdm_session_info_t *session_info;
    1972              : 
    1973            1 :     spdm_test_context = *state;
    1974            1 :     spdm_context = spdm_test_context->spdm_context;
    1975            1 :     spdm_test_context->case_id = 0x5;
    1976            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1977              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1978            1 :     spdm_context->connection_info.connection_state =
    1979              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1980            1 :     spdm_context->connection_info.capability.flags |=
    1981              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    1982            1 :     spdm_context->connection_info.capability.flags |=
    1983              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1984            1 :     spdm_context->connection_info.capability.flags |=
    1985              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1986            1 :     spdm_context->local_context.capability.flags |=
    1987              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    1988            1 :     spdm_context->local_context.capability.flags |=
    1989              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1990            1 :     spdm_context->local_context.capability.flags |=
    1991              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1992            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1993              :                                                          m_libspdm_use_asym_algo, &data,
    1994              :                                                          &data_size, &hash, &hash_size)) {
    1995            0 :         assert(false);
    1996              :     }
    1997            1 :     libspdm_reset_message_a(spdm_context);
    1998            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1999              :         m_libspdm_use_hash_algo;
    2000            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2001              :         m_libspdm_use_asym_algo;
    2002            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2003              :         m_libspdm_use_dhe_algo;
    2004            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2005              :         m_libspdm_use_aead_algo;
    2006              : 
    2007              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2008              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2009              :         data_size;
    2010              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2011              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2012              :                      data, data_size);
    2013              : #else
    2014            1 :     libspdm_hash_all(
    2015              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2016              :         data, data_size,
    2017            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2018            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2019            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2020            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2021              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2022              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2023              :         data, data_size,
    2024              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2025              : #endif
    2026            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2027              : 
    2028            1 :     session_id = 0xFFFFFFFF;
    2029            1 :     session_info = &spdm_context->session_info[0];
    2030            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2031            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2032            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2033            1 :     libspdm_secured_message_set_response_finished_key(
    2034              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2035              :         hash_size);
    2036            1 :     libspdm_secured_message_set_session_state(
    2037              :         session_info->secured_message_context,
    2038              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2039              : 
    2040            1 :     spdm_context->connection_info.capability.flags |=
    2041              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2042            1 :     spdm_context->local_context.capability.flags |=
    2043              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2044            1 :     req_slot_id_param = 0;
    2045            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2046              :                                          req_slot_id_param);
    2047            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    2048            1 :     free(data);
    2049            1 : }
    2050              : 
    2051              : /**
    2052              :  * Test 6: the requester is setup correctly (see Test 2), but, on the first try,
    2053              :  * receiving a Busy ERROR message, and on retry, receiving a correct FINISH_RSP
    2054              :  * message with only MAC (no mutual authentication).
    2055              :  * Expected behavior: client returns a Status of RETURN_SUCCESS.
    2056              :  **/
    2057            1 : void libspdm_test_requester_finish_case6(void **state)
    2058              : {
    2059              :     libspdm_return_t status;
    2060              :     libspdm_test_context_t *spdm_test_context;
    2061              :     libspdm_context_t *spdm_context;
    2062              :     uint32_t session_id;
    2063              :     uint8_t req_slot_id_param;
    2064              :     void *data;
    2065              :     size_t data_size;
    2066              :     void *hash;
    2067              :     size_t hash_size;
    2068              :     libspdm_session_info_t *session_info;
    2069              : 
    2070            1 :     spdm_test_context = *state;
    2071            1 :     spdm_context = spdm_test_context->spdm_context;
    2072            1 :     spdm_test_context->case_id = 0x6;
    2073            1 :     spdm_context->retry_times = 3;
    2074            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2075              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2076            1 :     spdm_context->connection_info.connection_state =
    2077              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2078            1 :     spdm_context->connection_info.capability.flags |=
    2079              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2080            1 :     spdm_context->connection_info.capability.flags |=
    2081              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2082            1 :     spdm_context->connection_info.capability.flags |=
    2083              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2084            1 :     spdm_context->local_context.capability.flags |=
    2085              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2086            1 :     spdm_context->local_context.capability.flags |=
    2087              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2088            1 :     spdm_context->local_context.capability.flags |=
    2089              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2090            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2091              :                                                          m_libspdm_use_asym_algo, &data,
    2092              :                                                          &data_size, &hash, &hash_size)) {
    2093            0 :         assert(false);
    2094              :     }
    2095            1 :     libspdm_reset_message_a(spdm_context);
    2096            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2097              :         m_libspdm_use_hash_algo;
    2098            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2099              :         m_libspdm_use_asym_algo;
    2100            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2101              :         m_libspdm_use_dhe_algo;
    2102            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2103              :         m_libspdm_use_aead_algo;
    2104              : 
    2105              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2106              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2107              :         data_size;
    2108              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2109              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2110              :                      data, data_size);
    2111              : #else
    2112            1 :     libspdm_hash_all(
    2113              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2114              :         data, data_size,
    2115            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2116            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2117            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2118            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2119              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2120              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2121              :         data, data_size,
    2122              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2123              : #endif
    2124            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2125              : 
    2126            1 :     session_id = 0xFFFFFFFF;
    2127            1 :     session_info = &spdm_context->session_info[0];
    2128            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2129            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2130            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2131            1 :     libspdm_secured_message_set_response_finished_key(
    2132              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2133              :         hash_size);
    2134            1 :     libspdm_secured_message_set_session_state(
    2135              :         session_info->secured_message_context,
    2136              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2137              : 
    2138            1 :     spdm_context->connection_info.capability.flags |=
    2139              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2140            1 :     spdm_context->local_context.capability.flags |=
    2141              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2142            1 :     req_slot_id_param = 0;
    2143            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2144              :                                          req_slot_id_param);
    2145            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2146            1 :     assert_int_equal(
    2147              :         libspdm_secured_message_get_session_state(
    2148              :             spdm_context->session_info[0].secured_message_context),
    2149              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2150            1 :     free(data);
    2151            1 : }
    2152              : 
    2153              : /**
    2154              :  * Test 7: the requester is setup correctly (see Test 2), but receives an ERROR
    2155              :  * message indicating the RequestResynch status of the responder.
    2156              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR, and the
    2157              :  * communication is reset to expect a new GET_VERSION message.
    2158              :  **/
    2159            1 : void libspdm_test_requester_finish_case7(void **state)
    2160              : {
    2161              :     libspdm_return_t status;
    2162              :     libspdm_test_context_t *spdm_test_context;
    2163              :     libspdm_context_t *spdm_context;
    2164              :     uint32_t session_id;
    2165              :     uint8_t req_slot_id_param;
    2166              :     void *data;
    2167              :     size_t data_size;
    2168              :     void *hash;
    2169              :     size_t hash_size;
    2170              :     libspdm_session_info_t *session_info;
    2171              : 
    2172            1 :     spdm_test_context = *state;
    2173            1 :     spdm_context = spdm_test_context->spdm_context;
    2174            1 :     spdm_test_context->case_id = 0x7;
    2175            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2176              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2177            1 :     spdm_context->connection_info.connection_state =
    2178              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2179            1 :     spdm_context->connection_info.capability.flags |=
    2180              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2181            1 :     spdm_context->connection_info.capability.flags |=
    2182              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2183            1 :     spdm_context->connection_info.capability.flags |=
    2184              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2185            1 :     spdm_context->local_context.capability.flags |=
    2186              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2187            1 :     spdm_context->local_context.capability.flags |=
    2188              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2189            1 :     spdm_context->local_context.capability.flags |=
    2190              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2191            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2192              :                                                          m_libspdm_use_asym_algo, &data,
    2193              :                                                          &data_size, &hash, &hash_size)) {
    2194            0 :         assert(false);
    2195              :     }
    2196            1 :     libspdm_reset_message_a(spdm_context);
    2197            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2198              :         m_libspdm_use_hash_algo;
    2199            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2200              :         m_libspdm_use_asym_algo;
    2201            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2202              :         m_libspdm_use_dhe_algo;
    2203            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2204              :         m_libspdm_use_aead_algo;
    2205              : 
    2206              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2207              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2208              :         data_size;
    2209              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2210              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2211              :                      data, data_size);
    2212              : #else
    2213            1 :     libspdm_hash_all(
    2214              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2215              :         data, data_size,
    2216            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2217            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2218            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2219            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2220              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2221              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2222              :         data, data_size,
    2223              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2224              : #endif
    2225            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2226              : 
    2227            1 :     session_id = 0xFFFFFFFF;
    2228            1 :     session_info = &spdm_context->session_info[0];
    2229            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2230            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2231            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2232            1 :     libspdm_secured_message_set_response_finished_key(
    2233              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2234              :         hash_size);
    2235            1 :     libspdm_secured_message_set_session_state(
    2236              :         session_info->secured_message_context,
    2237              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2238              : 
    2239            1 :     spdm_context->connection_info.capability.flags |=
    2240              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2241            1 :     spdm_context->local_context.capability.flags |=
    2242              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2243            1 :     req_slot_id_param = 0;
    2244            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2245              :                                          req_slot_id_param);
    2246            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    2247            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    2248              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    2249            1 :     free(data);
    2250            1 : }
    2251              : 
    2252              : /**
    2253              :  * Test 8: the requester is setup correctly (see Test 2), but receives an ERROR
    2254              :  * message indicating the ResponseNotReady status of the responder.
    2255              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR,.
    2256              :  **/
    2257            1 : void libspdm_test_requester_finish_case8(void **state)
    2258              : {
    2259              :     libspdm_return_t status;
    2260              :     libspdm_test_context_t *spdm_test_context;
    2261              :     libspdm_context_t *spdm_context;
    2262              :     uint32_t session_id;
    2263              :     uint8_t req_slot_id_param;
    2264              :     void *data;
    2265              :     size_t data_size;
    2266              :     void *hash;
    2267              :     size_t hash_size;
    2268              :     libspdm_session_info_t *session_info;
    2269              : 
    2270            1 :     spdm_test_context = *state;
    2271            1 :     spdm_context = spdm_test_context->spdm_context;
    2272            1 :     spdm_test_context->case_id = 0x8;
    2273            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2274              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2275            1 :     spdm_context->connection_info.connection_state =
    2276              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2277            1 :     spdm_context->connection_info.capability.flags |=
    2278              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2279            1 :     spdm_context->connection_info.capability.flags |=
    2280              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2281            1 :     spdm_context->connection_info.capability.flags |=
    2282              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2283            1 :     spdm_context->local_context.capability.flags |=
    2284              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2285            1 :     spdm_context->local_context.capability.flags |=
    2286              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2287            1 :     spdm_context->local_context.capability.flags |=
    2288              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2289            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2290              :                                                          m_libspdm_use_asym_algo, &data,
    2291              :                                                          &data_size, &hash, &hash_size)) {
    2292            0 :         assert(false);
    2293              :     }
    2294            1 :     libspdm_reset_message_a(spdm_context);
    2295            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2296              :         m_libspdm_use_hash_algo;
    2297            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2298              :         m_libspdm_use_asym_algo;
    2299            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2300              :         m_libspdm_use_dhe_algo;
    2301            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2302              :         m_libspdm_use_aead_algo;
    2303              : 
    2304              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2305              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2306              :         data_size;
    2307              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2308              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2309              :                      data, data_size);
    2310              : #else
    2311            1 :     libspdm_hash_all(
    2312              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2313              :         data, data_size,
    2314            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2315            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2316            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2317            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2318              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2319              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2320              :         data, data_size,
    2321              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2322              : #endif
    2323            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2324              : 
    2325            1 :     session_id = 0xFFFFFFFF;
    2326            1 :     session_info = &spdm_context->session_info[0];
    2327            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2328            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2329            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2330            1 :     libspdm_secured_message_set_response_finished_key(
    2331              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2332              :         hash_size);
    2333            1 :     libspdm_secured_message_set_session_state(
    2334              :         session_info->secured_message_context,
    2335              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2336              : 
    2337            1 :     spdm_context->connection_info.capability.flags |=
    2338              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2339            1 :     spdm_context->local_context.capability.flags |=
    2340              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2341            1 :     req_slot_id_param = 0;
    2342            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2343              :                                          req_slot_id_param);
    2344            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    2345            1 :     free(data);
    2346            1 : }
    2347              : 
    2348              : /**
    2349              :  * Test 9: the requester is setup correctly (see Test 2), but, on the first try,
    2350              :  * receiving a ResponseNotReady ERROR message, and on retry, receiving a correct
    2351              :  * FINISH_RSP message with only MAC (no mutual authentication).
    2352              :  * Expected behavior: client returns a Status of RETURN_SUCCESS.
    2353              :  **/
    2354            1 : void libspdm_test_requester_finish_case9(void **state)
    2355              : {
    2356              :     libspdm_return_t status;
    2357              :     libspdm_test_context_t *spdm_test_context;
    2358              :     libspdm_context_t *spdm_context;
    2359              :     uint32_t session_id;
    2360              :     uint8_t req_slot_id_param;
    2361              :     void *data;
    2362              :     size_t data_size;
    2363              :     void *hash;
    2364              :     size_t hash_size;
    2365              :     libspdm_session_info_t *session_info;
    2366              : 
    2367            1 :     spdm_test_context = *state;
    2368            1 :     spdm_context = spdm_test_context->spdm_context;
    2369            1 :     spdm_test_context->case_id = 0x9;
    2370            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2371              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2372            1 :     spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2373            1 :     spdm_context->connection_info.capability.flags |=
    2374              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2375            1 :     spdm_context->connection_info.capability.flags |=
    2376              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2377            1 :     spdm_context->connection_info.capability.flags |=
    2378              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2379            1 :     spdm_context->local_context.capability.flags |=
    2380              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2381            1 :     spdm_context->local_context.capability.flags |=
    2382              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2383            1 :     spdm_context->local_context.capability.flags |=
    2384              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2385            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2386              :                                                          m_libspdm_use_asym_algo, &data,
    2387              :                                                          &data_size, &hash, &hash_size)) {
    2388            0 :         assert(false);
    2389              :     }
    2390            1 :     libspdm_reset_message_a(spdm_context);
    2391            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2392            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2393            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2394            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2395              : 
    2396              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2397              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
    2398              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2399              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2400              :                      data, data_size);
    2401              : #else
    2402            1 :     libspdm_hash_all(
    2403              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2404              :         data, data_size,
    2405            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2406            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2407            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2408            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2409              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2410              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2411              :         data, data_size,
    2412              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2413              : #endif
    2414            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2415              : 
    2416            1 :     session_id = 0xFFFFFFFF;
    2417            1 :     session_info = &spdm_context->session_info[0];
    2418            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2419            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2420            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2421            1 :     libspdm_secured_message_set_response_finished_key(
    2422              :         session_info->secured_message_context, m_libspdm_dummy_buffer, hash_size);
    2423            1 :     libspdm_secured_message_set_session_state(
    2424              :         session_info->secured_message_context,
    2425              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2426              : 
    2427            1 :     spdm_context->connection_info.capability.flags |=
    2428              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2429            1 :     spdm_context->local_context.capability.flags |=
    2430              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2431            1 :     req_slot_id_param = 0;
    2432            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    2433              :     if (LIBSPDM_RESPOND_IF_READY_SUPPORT) {
    2434            1 :         assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2435            1 :         assert_int_equal(
    2436              :             libspdm_secured_message_get_session_state(
    2437              :                 spdm_context->session_info[0].secured_message_context),
    2438              :             LIBSPDM_SESSION_STATE_ESTABLISHED);
    2439              :     } else {
    2440              :         assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    2441              :     }
    2442              : 
    2443            1 :     free(data);
    2444            1 : }
    2445              : 
    2446              : /**
    2447              :  * Test 10: receiving an unexpected ERROR message from the responder.
    2448              :  * There are tests for all named codes, including some reserved ones
    2449              :  * (namely, 0x00, 0x0b, 0x0c, 0x3f, 0xfd, 0xfe).
    2450              :  * However, for having specific test cases, it is excluded from this case:
    2451              :  * Busy (0x03), ResponseNotReady (0x42), and RequestResync (0x43).
    2452              :  * Expected behavior: client returns a status of RETURN_DEVICE_ERROR.
    2453              :  **/
    2454            1 : void libspdm_test_requester_finish_case10(void **state) {
    2455              :     libspdm_return_t status;
    2456              :     libspdm_test_context_t    *spdm_test_context;
    2457              :     libspdm_context_t  *spdm_context;
    2458              :     uint32_t session_id;
    2459              :     uint8_t req_slot_id_param;
    2460              :     void                 *data;
    2461              :     size_t data_size;
    2462              :     void                 *hash;
    2463              :     size_t hash_size;
    2464              :     libspdm_session_info_t    *session_info;
    2465              :     uint16_t error_code;
    2466              : 
    2467            1 :     spdm_test_context = *state;
    2468            1 :     spdm_context = spdm_test_context->spdm_context;
    2469            1 :     spdm_test_context->case_id = 0xA;
    2470            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2471              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2472            1 :     spdm_context->connection_info.capability.flags |=
    2473              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2474            1 :     spdm_context->connection_info.capability.flags |=
    2475              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2476            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2477            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2478            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2479            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2480            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    2481              :                                                      m_libspdm_use_asym_algo,
    2482              :                                                      &data, &data_size,
    2483              :                                                      &hash, &hash_size);
    2484            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2485            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2486            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2487            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2488              : 
    2489              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2490              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2491              :         data_size;
    2492              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2493              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2494              :                      data, data_size);
    2495              : #else
    2496            1 :     libspdm_hash_all(
    2497              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2498              :         data, data_size,
    2499            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2500            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2501            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2502            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2503              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2504              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2505              :         data, data_size,
    2506              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2507              : #endif
    2508            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2509              : 
    2510            1 :     session_id = 0xFFFFFFFF;
    2511            1 :     spdm_context->connection_info.capability.flags |=
    2512              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2513            1 :     spdm_context->local_context.capability.flags |=
    2514              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2515            1 :     req_slot_id_param = 0;
    2516              : 
    2517            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    2518           19 :     while(error_code <= 0xff) {
    2519           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2520           18 :         libspdm_reset_message_a(spdm_context);
    2521              : 
    2522           18 :         session_info = &spdm_context->session_info[0];
    2523           18 :         libspdm_session_info_init (spdm_context, session_info, session_id, false);
    2524           18 :         hash_size = libspdm_get_hash_size (m_libspdm_use_hash_algo);
    2525           18 :         libspdm_set_mem (m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2526           18 :         libspdm_secured_message_set_response_finished_key (session_info->secured_message_context,
    2527              :                                                            m_libspdm_dummy_buffer, hash_size);
    2528           18 :         libspdm_secured_message_set_session_state (session_info->secured_message_context,
    2529              :                                                    LIBSPDM_SESSION_STATE_HANDSHAKING);
    2530              : 
    2531           18 :         status = libspdm_send_receive_finish (spdm_context, session_id, req_slot_id_param);
    2532           18 :         if(error_code != SPDM_ERROR_CODE_DECRYPT_ERROR) {
    2533           17 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    2534              :         } else {
    2535            1 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_SESSION_MSG_ERROR, error_code);
    2536              :         }
    2537              : 
    2538           18 :         error_code++;
    2539           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    2540            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    2541              :         }
    2542           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    2543            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    2544              :         }
    2545           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    2546            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    2547              :         }
    2548              :     }
    2549              : 
    2550            1 :     free(data);
    2551            1 : }
    2552              : 
    2553            1 : void libspdm_test_requester_finish_case11(void **state)
    2554              : {
    2555              :     libspdm_return_t status;
    2556              :     libspdm_test_context_t *spdm_test_context;
    2557              :     libspdm_context_t *spdm_context;
    2558              :     uint32_t session_id;
    2559              :     uint8_t req_slot_id_param;
    2560              :     void *data;
    2561              :     size_t data_size;
    2562              :     void *hash;
    2563              :     size_t hash_size;
    2564              :     libspdm_session_info_t *session_info;
    2565              : 
    2566            1 :     spdm_test_context = *state;
    2567            1 :     spdm_context = spdm_test_context->spdm_context;
    2568            1 :     spdm_test_context->case_id = 0xB;
    2569            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2570              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2571            1 :     spdm_context->connection_info.connection_state =
    2572              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2573            1 :     spdm_context->connection_info.capability.flags |=
    2574              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2575            1 :     spdm_context->connection_info.capability.flags |=
    2576              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2577            1 :     spdm_context->connection_info.capability.flags |=
    2578              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2579            1 :     spdm_context->local_context.capability.flags |=
    2580              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2581            1 :     spdm_context->local_context.capability.flags |=
    2582              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2583            1 :     spdm_context->local_context.capability.flags |=
    2584              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2585            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2586              :                                                          m_libspdm_use_asym_algo, &data,
    2587              :                                                          &data_size, &hash, &hash_size)) {
    2588            0 :         assert(false);
    2589              :     }
    2590            1 :     libspdm_reset_message_a(spdm_context);
    2591            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2592              :         m_libspdm_use_hash_algo;
    2593            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2594              :         m_libspdm_use_asym_algo;
    2595            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2596              :         m_libspdm_use_dhe_algo;
    2597            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2598              :         m_libspdm_use_aead_algo;
    2599              : 
    2600              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2601              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2602              :         data_size;
    2603              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2604              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2605              :                      data, data_size);
    2606              : #else
    2607            1 :     libspdm_hash_all(
    2608              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2609              :         data, data_size,
    2610            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2611            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2612            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2613            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2614              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2615              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2616              :         data, data_size,
    2617              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2618              : #endif
    2619            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2620              : 
    2621            1 :     session_id = 0xFFFFFFFF;
    2622            1 :     session_info = &spdm_context->session_info[0];
    2623            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2624            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2625            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2626            1 :     libspdm_secured_message_set_response_finished_key(
    2627              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2628              :         hash_size);
    2629            1 :     libspdm_secured_message_set_session_state(
    2630              :         session_info->secured_message_context,
    2631              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2632              : 
    2633            1 :     spdm_context->connection_info.capability.flags |=
    2634              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2635            1 :     spdm_context->local_context.capability.flags |=
    2636              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2637            1 :     req_slot_id_param = 0;
    2638              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2639              :     session_info->session_transcript.message_m.buffer_size =
    2640              :         session_info->session_transcript.message_m.max_buffer_size;
    2641              :     spdm_context->transcript.message_b.buffer_size =
    2642              :         spdm_context->transcript.message_b.max_buffer_size;
    2643              :     spdm_context->transcript.message_c.buffer_size =
    2644              :         spdm_context->transcript.message_c.max_buffer_size;
    2645              :     spdm_context->transcript.message_mut_b.buffer_size =
    2646              :         spdm_context->transcript.message_mut_b.max_buffer_size;
    2647              :     spdm_context->transcript.message_mut_c.buffer_size =
    2648              :         spdm_context->transcript.message_mut_c.max_buffer_size;
    2649              : #endif
    2650              : 
    2651            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2652              :                                          req_slot_id_param);
    2653            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2654            1 :     assert_int_equal(
    2655              :         libspdm_secured_message_get_session_state(
    2656              :             spdm_context->session_info[0].secured_message_context),
    2657              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2658              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2659              :     assert_int_equal(session_info->session_transcript.message_m.buffer_size,
    2660              :                      0);
    2661              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
    2662              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
    2663              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
    2664              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
    2665              : #endif
    2666            1 :     free(data);
    2667            1 : }
    2668              : 
    2669              : /**
    2670              :  * Test 12: requester is not setup correctly to support key exchange
    2671              :  * (no capabilities). The responder would attempt to return a correct
    2672              :  * FINISH_RSP message.
    2673              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2674              :  **/
    2675            1 : void libspdm_test_requester_finish_case12(void **state)
    2676              : {
    2677              :     libspdm_return_t status;
    2678              :     libspdm_test_context_t *spdm_test_context;
    2679              :     libspdm_context_t *spdm_context;
    2680              :     uint32_t session_id;
    2681              :     uint8_t req_slot_id_param;
    2682              :     void *data;
    2683              :     size_t data_size;
    2684              :     void *hash;
    2685              :     size_t hash_size;
    2686              :     libspdm_session_info_t *session_info;
    2687              : 
    2688            1 :     spdm_test_context = *state;
    2689            1 :     spdm_context = spdm_test_context->spdm_context;
    2690            1 :     spdm_test_context->case_id = 0xC;
    2691            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2692              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2693            1 :     spdm_context->connection_info.connection_state =
    2694              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2695            1 :     spdm_context->connection_info.capability.flags = 0;
    2696            1 :     spdm_context->connection_info.capability.flags |=
    2697              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2698            1 :     spdm_context->connection_info.capability.flags |=
    2699              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2700            1 :     spdm_context->connection_info.capability.flags |=
    2701              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2702            1 :     spdm_context->local_context.capability.flags = 0;
    2703              :     /* no key exchange capabilities (requester)*/
    2704            1 :     spdm_context->local_context.capability.flags |=
    2705              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2706            1 :     spdm_context->local_context.capability.flags |=
    2707              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2708            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2709              :                                                          m_libspdm_use_asym_algo, &data,
    2710              :                                                          &data_size, &hash, &hash_size)) {
    2711            0 :         assert(false);
    2712              :     }
    2713            1 :     libspdm_reset_message_a(spdm_context);
    2714            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2715              :         m_libspdm_use_hash_algo;
    2716            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2717              :         m_libspdm_use_asym_algo;
    2718            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2719              :         m_libspdm_use_dhe_algo;
    2720            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2721              :         m_libspdm_use_aead_algo;
    2722              : 
    2723              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2724              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2725              :         data_size;
    2726              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2727              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2728              :                      data, data_size);
    2729              : #else
    2730            1 :     libspdm_hash_all(
    2731              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2732              :         data, data_size,
    2733            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2734            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2735            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2736            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2737              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2738              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2739              :         data, data_size,
    2740              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2741              : #endif
    2742            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2743              : 
    2744            1 :     session_id = 0xFFFFFFFF;
    2745            1 :     session_info = &spdm_context->session_info[0];
    2746            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2747            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2748            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2749            1 :     libspdm_secured_message_set_response_finished_key(
    2750              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2751              :         hash_size);
    2752            1 :     libspdm_secured_message_set_session_state(
    2753              :         session_info->secured_message_context,
    2754              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2755              : 
    2756            1 :     spdm_context->connection_info.capability.flags |=
    2757              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2758            1 :     spdm_context->local_context.capability.flags |=
    2759              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2760            1 :     req_slot_id_param = 0;
    2761            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2762              :                                          req_slot_id_param);
    2763            1 :     assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
    2764            1 :     free(data);
    2765            1 : }
    2766              : 
    2767              : /**
    2768              :  * Test 13: requester is not setup correctly to accept key exchange and
    2769              :  * finish at this point (at least NEGOTIATE_ALGORITHMS is required, if
    2770              :  * the public key was provisioned before the key exchange). The
    2771              :  * responder would attempt to return a correct FINISH_RSP message.
    2772              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2773              :  **/
    2774            1 : void libspdm_test_requester_finish_case13(void **state)
    2775              : {
    2776              :     libspdm_return_t status;
    2777              :     libspdm_test_context_t *spdm_test_context;
    2778              :     libspdm_context_t *spdm_context;
    2779              :     uint32_t session_id;
    2780              :     uint8_t req_slot_id_param;
    2781              :     void *data;
    2782              :     size_t data_size;
    2783              :     void *hash;
    2784              :     size_t hash_size;
    2785              :     libspdm_session_info_t *session_info;
    2786              : 
    2787            1 :     spdm_test_context = *state;
    2788            1 :     spdm_context = spdm_test_context->spdm_context;
    2789            1 :     spdm_test_context->case_id = 0xD;
    2790            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2791              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2792            1 :     spdm_context->connection_info.connection_state =
    2793              :         LIBSPDM_CONNECTION_STATE_AFTER_CAPABILITIES;
    2794            1 :     spdm_context->connection_info.capability.flags |=
    2795              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2796            1 :     spdm_context->connection_info.capability.flags |=
    2797              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2798            1 :     spdm_context->connection_info.capability.flags |=
    2799              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2800            1 :     spdm_context->local_context.capability.flags |=
    2801              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2802            1 :     spdm_context->local_context.capability.flags |=
    2803              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2804            1 :     spdm_context->local_context.capability.flags |=
    2805              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2806            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2807              :                                                          m_libspdm_use_asym_algo, &data,
    2808              :                                                          &data_size, &hash, &hash_size)) {
    2809            0 :         assert(false);
    2810              :     }
    2811            1 :     libspdm_reset_message_a(spdm_context);
    2812            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2813              :         m_libspdm_use_hash_algo;
    2814            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2815              :         m_libspdm_use_asym_algo;
    2816            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2817              :         m_libspdm_use_dhe_algo;
    2818            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2819              :         m_libspdm_use_aead_algo;
    2820              : 
    2821              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2822              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2823              :         data_size;
    2824              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2825              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2826              :                      data, data_size);
    2827              : #else
    2828            1 :     libspdm_hash_all(
    2829              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2830              :         data, data_size,
    2831            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2832            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2833            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2834            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2835              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2836              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2837              :         data, data_size,
    2838              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2839              : #endif
    2840            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2841              : 
    2842            1 :     session_id = 0xFFFFFFFF;
    2843            1 :     session_info = &spdm_context->session_info[0];
    2844            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2845            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2846            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2847            1 :     libspdm_secured_message_set_response_finished_key(
    2848              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2849              :         hash_size);
    2850            1 :     libspdm_secured_message_set_session_state(
    2851              :         session_info->secured_message_context,
    2852              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2853              : 
    2854            1 :     spdm_context->connection_info.capability.flags |=
    2855              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2856            1 :     spdm_context->local_context.capability.flags |=
    2857              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2858            1 :     req_slot_id_param = 0;
    2859            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2860              :                                          req_slot_id_param);
    2861            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    2862            1 :     free(data);
    2863            1 : }
    2864              : 
    2865              : /**
    2866              :  * Test 14: receiving an incorrect FINISH_RSP message, with wrong response
    2867              :  * code, but all other field correct.
    2868              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    2869              :  **/
    2870            1 : void libspdm_test_requester_finish_case14(void **state)
    2871              : {
    2872              :     libspdm_return_t status;
    2873              :     libspdm_test_context_t *spdm_test_context;
    2874              :     libspdm_context_t *spdm_context;
    2875              :     uint32_t session_id;
    2876              :     uint8_t req_slot_id_param;
    2877              :     void *data;
    2878              :     size_t data_size;
    2879              :     void *hash;
    2880              :     size_t hash_size;
    2881              :     libspdm_session_info_t *session_info;
    2882              : 
    2883            1 :     spdm_test_context = *state;
    2884            1 :     spdm_context = spdm_test_context->spdm_context;
    2885            1 :     spdm_test_context->case_id = 0xE;
    2886            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2887              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2888            1 :     spdm_context->connection_info.connection_state =
    2889              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2890            1 :     spdm_context->connection_info.capability.flags |=
    2891              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2892            1 :     spdm_context->connection_info.capability.flags |=
    2893              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2894            1 :     spdm_context->connection_info.capability.flags |=
    2895              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2896            1 :     spdm_context->local_context.capability.flags |=
    2897              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2898            1 :     spdm_context->local_context.capability.flags |=
    2899              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2900            1 :     spdm_context->local_context.capability.flags |=
    2901              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2902            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2903              :                                                          m_libspdm_use_asym_algo, &data,
    2904              :                                                          &data_size, &hash, &hash_size)) {
    2905            0 :         assert(false);
    2906              :     }
    2907            1 :     libspdm_reset_message_a(spdm_context);
    2908            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2909              :         m_libspdm_use_hash_algo;
    2910            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2911              :         m_libspdm_use_asym_algo;
    2912            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2913              :         m_libspdm_use_dhe_algo;
    2914            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2915              :         m_libspdm_use_aead_algo;
    2916              : 
    2917              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2918              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2919              :         data_size;
    2920              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2921              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2922              :                      data, data_size);
    2923              : #else
    2924            1 :     libspdm_hash_all(
    2925              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2926              :         data, data_size,
    2927            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    2928            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    2929            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    2930            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    2931              :         spdm_context->connection_info.algorithm.base_hash_algo,
    2932              :         spdm_context->connection_info.algorithm.base_asym_algo,
    2933              :         data, data_size,
    2934              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    2935              : #endif
    2936            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    2937              : 
    2938            1 :     session_id = 0xFFFFFFFF;
    2939            1 :     session_info = &spdm_context->session_info[0];
    2940            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    2941            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    2942            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    2943            1 :     libspdm_secured_message_set_response_finished_key(
    2944              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    2945              :         hash_size);
    2946            1 :     libspdm_secured_message_set_session_state(
    2947              :         session_info->secured_message_context,
    2948              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2949              : 
    2950            1 :     spdm_context->connection_info.capability.flags |=
    2951              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2952            1 :     spdm_context->local_context.capability.flags |=
    2953              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    2954            1 :     req_slot_id_param = 0;
    2955            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    2956              :                                          req_slot_id_param);
    2957            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    2958            1 :     free(data);
    2959            1 : }
    2960              : 
    2961              : /**
    2962              :  * Test 15: requester is not setup correctly by not initializing a
    2963              :  * session during KEY_EXCHANGE. The responder would attempt to
    2964              :  * return a correct FINISH_RSP message.
    2965              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2966              :  **/
    2967            1 : void libspdm_test_requester_finish_case15(void **state)
    2968              : {
    2969              :     libspdm_return_t status;
    2970              :     libspdm_test_context_t *spdm_test_context;
    2971              :     libspdm_context_t *spdm_context;
    2972              :     uint32_t session_id;
    2973              :     uint8_t req_slot_id_param;
    2974              :     void *data;
    2975              :     size_t data_size;
    2976              :     void *hash;
    2977              :     size_t hash_size;
    2978              :     libspdm_session_info_t *session_info;
    2979              : 
    2980            1 :     spdm_test_context = *state;
    2981            1 :     spdm_context = spdm_test_context->spdm_context;
    2982            1 :     spdm_test_context->case_id = 0xF;
    2983            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2984              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2985            1 :     spdm_context->connection_info.connection_state =
    2986              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2987            1 :     spdm_context->connection_info.capability.flags |=
    2988              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    2989            1 :     spdm_context->connection_info.capability.flags |=
    2990              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2991            1 :     spdm_context->connection_info.capability.flags |=
    2992              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2993            1 :     spdm_context->local_context.capability.flags |=
    2994              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    2995            1 :     spdm_context->local_context.capability.flags |=
    2996              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2997            1 :     spdm_context->local_context.capability.flags |=
    2998              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2999            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3000              :                                                          m_libspdm_use_asym_algo, &data,
    3001              :                                                          &data_size, &hash, &hash_size)) {
    3002            0 :         assert(false);
    3003              :     }
    3004            1 :     libspdm_reset_message_a(spdm_context);
    3005            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3006              :         m_libspdm_use_hash_algo;
    3007            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3008              :         m_libspdm_use_asym_algo;
    3009            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3010              :         m_libspdm_use_dhe_algo;
    3011            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3012              :         m_libspdm_use_aead_algo;
    3013              : 
    3014              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3015              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3016              :         data_size;
    3017              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3018              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3019              :                      data, data_size);
    3020              : #else
    3021            1 :     libspdm_hash_all(
    3022              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3023              :         data, data_size,
    3024            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3025            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3026            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3027            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3028              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3029              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3030              :         data, data_size,
    3031              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3032              : #endif
    3033            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3034              : 
    3035            1 :     session_id = 0xFFFFFFFF;
    3036            1 :     session_info = &spdm_context->session_info[0];
    3037            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3038            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3039            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3040            1 :     libspdm_secured_message_set_response_finished_key(
    3041              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3042              :         hash_size);
    3043            1 :     libspdm_secured_message_set_session_state(
    3044              :         session_info->secured_message_context,
    3045              :         LIBSPDM_SESSION_STATE_NOT_STARTED);
    3046              : 
    3047            1 :     spdm_context->connection_info.capability.flags |=
    3048              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3049            1 :     spdm_context->local_context.capability.flags |=
    3050              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3051            1 :     req_slot_id_param = 0;
    3052            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3053              :                                          req_slot_id_param);
    3054            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    3055            1 :     free(data);
    3056            1 : }
    3057              : 
    3058              : /**
    3059              :  * Test 16: receiving a correct FINISH_RSP message with a correct MAC,
    3060              :  * mutual authentication and 'handshake in the clear'.
    3061              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3062              :  * session is established.
    3063              :  **/
    3064            1 : void libspdm_test_requester_finish_case16(void **state)
    3065              : {
    3066              :     libspdm_return_t status;
    3067              :     libspdm_test_context_t *spdm_test_context;
    3068              :     libspdm_context_t *spdm_context;
    3069              :     uint32_t session_id;
    3070              :     uint8_t req_slot_id_param;
    3071              :     void *data;
    3072              :     size_t data_size;
    3073              :     void *hash;
    3074              :     size_t hash_size;
    3075              :     libspdm_session_info_t *session_info;
    3076              : 
    3077            1 :     spdm_test_context = *state;
    3078            1 :     spdm_context = spdm_test_context->spdm_context;
    3079            1 :     spdm_test_context->case_id = 0x10;
    3080            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3081              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3082            1 :     spdm_context->connection_info.connection_state =
    3083              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3084            1 :     spdm_context->connection_info.capability.flags |=
    3085              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3086            1 :     spdm_context->connection_info.capability.flags |=
    3087              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3088            1 :     spdm_context->connection_info.capability.flags |=
    3089              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3090            1 :     spdm_context->local_context.capability.flags |=
    3091              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3092            1 :     spdm_context->local_context.capability.flags |=
    3093              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3094            1 :     spdm_context->local_context.capability.flags |=
    3095              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3096            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3097              :                                                          m_libspdm_use_asym_algo, &data,
    3098              :                                                          &data_size, &hash, &hash_size)) {
    3099            0 :         assert(false);
    3100              :     }
    3101            1 :     libspdm_reset_message_a(spdm_context);
    3102            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3103              :         m_libspdm_use_hash_algo;
    3104            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3105              :         m_libspdm_use_asym_algo;
    3106            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3107              :         m_libspdm_use_req_asym_algo;
    3108            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3109              :         m_libspdm_use_dhe_algo;
    3110            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3111              :         m_libspdm_use_aead_algo;
    3112              : 
    3113              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3114              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3115              :         data_size;
    3116              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3117              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3118              :                      data, data_size);
    3119              : #else
    3120            1 :     libspdm_hash_all(
    3121              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3122              :         data, data_size,
    3123            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3124            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3125            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3126            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3127              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3128              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3129              :         data, data_size,
    3130              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3131              : #endif
    3132            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3133              : 
    3134            1 :     req_slot_id_param = 0;
    3135            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3136              :                                                     m_libspdm_use_req_asym_algo, &data,
    3137              :                                                     &data_size, &hash, &hash_size);
    3138              :     spdm_context->local_context.
    3139            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3140              :     spdm_context->local_context.
    3141            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3142              : 
    3143            1 :     session_id = 0xFFFFFFFF;
    3144            1 :     session_info = &spdm_context->session_info[0];
    3145            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3146            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3147            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3148            1 :     libspdm_secured_message_set_response_finished_key(
    3149              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3150              :         hash_size);
    3151            1 :     libspdm_secured_message_set_session_state(
    3152              :         session_info->secured_message_context,
    3153              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3154            1 :     session_info->mut_auth_requested = 1;
    3155              : 
    3156            1 :     spdm_context->connection_info.capability.flags |=
    3157              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3158            1 :     spdm_context->local_context.capability.flags |=
    3159              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3160              : 
    3161            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3162              :                                          req_slot_id_param);
    3163            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3164            1 :     assert_int_equal(
    3165              :         libspdm_secured_message_get_session_state(
    3166              :             spdm_context->session_info[0].secured_message_context),
    3167              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3168            1 :     free(data);
    3169            1 : }
    3170              : 
    3171              : /**
    3172              :  * Test 17: receiving a FINISH_RSP message with an incorrect MAC
    3173              :  * (all-zero), mutual authentication, and 'handshake in the clear'.
    3174              :  * Expected behavior: client returns a Status of RETURN_SECURITY_VIOLATION.
    3175              :  **/
    3176            1 : void libspdm_test_requester_finish_case17(void **state)
    3177              : {
    3178              :     libspdm_return_t status;
    3179              :     libspdm_test_context_t *spdm_test_context;
    3180              :     libspdm_context_t *spdm_context;
    3181              :     uint32_t session_id;
    3182              :     uint8_t req_slot_id_param;
    3183              :     void *data;
    3184              :     size_t data_size;
    3185              :     void *hash;
    3186              :     size_t hash_size;
    3187              :     libspdm_session_info_t *session_info;
    3188              : 
    3189            1 :     spdm_test_context = *state;
    3190            1 :     spdm_context = spdm_test_context->spdm_context;
    3191            1 :     spdm_test_context->case_id = 0x11;
    3192            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3193              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3194            1 :     spdm_context->connection_info.connection_state =
    3195              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3196            1 :     spdm_context->connection_info.capability.flags |=
    3197              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3198            1 :     spdm_context->connection_info.capability.flags |=
    3199              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3200            1 :     spdm_context->connection_info.capability.flags |=
    3201              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3202            1 :     spdm_context->local_context.capability.flags |=
    3203              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3204            1 :     spdm_context->local_context.capability.flags |=
    3205              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3206            1 :     spdm_context->local_context.capability.flags |=
    3207              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3208            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3209              :                                                          m_libspdm_use_asym_algo, &data,
    3210              :                                                          &data_size, &hash, &hash_size)) {
    3211            0 :         assert(false);
    3212              :     }
    3213            1 :     libspdm_reset_message_a(spdm_context);
    3214            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3215              :         m_libspdm_use_hash_algo;
    3216            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3217              :         m_libspdm_use_asym_algo;
    3218            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3219              :         m_libspdm_use_req_asym_algo;
    3220            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3221              :         m_libspdm_use_dhe_algo;
    3222            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3223              :         m_libspdm_use_aead_algo;
    3224              : 
    3225              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3226              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3227              :         data_size;
    3228              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3229              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3230              :                      data, data_size);
    3231              : #else
    3232            1 :     libspdm_hash_all(
    3233              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3234              :         data, data_size,
    3235            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3236            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3237            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3238            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3239              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3240              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3241              :         data, data_size,
    3242              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3243              : #endif
    3244            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3245              : 
    3246            1 :     req_slot_id_param = 0;
    3247            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3248              :                                                     m_libspdm_use_req_asym_algo, &data,
    3249              :                                                     &data_size, &hash, &hash_size);
    3250              :     spdm_context->local_context.
    3251            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3252              :     spdm_context->local_context.
    3253            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3254              : 
    3255            1 :     session_id = 0xFFFFFFFF;
    3256            1 :     session_info = &spdm_context->session_info[0];
    3257            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3258            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3259            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3260            1 :     libspdm_secured_message_set_response_finished_key(
    3261              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3262              :         hash_size);
    3263            1 :     libspdm_secured_message_set_session_state(
    3264              :         session_info->secured_message_context,
    3265              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3266            1 :     session_info->mut_auth_requested = 1;
    3267              : 
    3268            1 :     spdm_context->connection_info.capability.flags |=
    3269              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3270            1 :     spdm_context->local_context.capability.flags |=
    3271              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3272              : 
    3273            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3274              :                                          req_slot_id_param);
    3275            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    3276            1 :     free(data);
    3277            1 : }
    3278              : 
    3279              : /**
    3280              :  * Test 18: receiving a FINISH_RSP message with an incorrect MAC
    3281              :  * (arbitrary), mutual authentication, and 'handshake in the clear'.
    3282              :  * Expected behavior: client returns a Status of RETURN_SECURITY_VIOLATION.
    3283              :  **/
    3284            1 : void libspdm_test_requester_finish_case18(void **state)
    3285              : {
    3286              :     libspdm_return_t status;
    3287              :     libspdm_test_context_t *spdm_test_context;
    3288              :     libspdm_context_t *spdm_context;
    3289              :     uint32_t session_id;
    3290              :     uint8_t req_slot_id_param;
    3291              :     void *data;
    3292              :     size_t data_size;
    3293              :     void *hash;
    3294              :     size_t hash_size;
    3295              :     libspdm_session_info_t *session_info;
    3296              : 
    3297            1 :     spdm_test_context = *state;
    3298            1 :     spdm_context = spdm_test_context->spdm_context;
    3299            1 :     spdm_test_context->case_id = 0x12;
    3300            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3301              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3302            1 :     spdm_context->connection_info.connection_state =
    3303              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3304            1 :     spdm_context->connection_info.capability.flags |=
    3305              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3306            1 :     spdm_context->connection_info.capability.flags |=
    3307              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3308            1 :     spdm_context->connection_info.capability.flags |=
    3309              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3310            1 :     spdm_context->local_context.capability.flags |=
    3311              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3312            1 :     spdm_context->local_context.capability.flags |=
    3313              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3314            1 :     spdm_context->local_context.capability.flags |=
    3315              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3316            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3317              :                                                          m_libspdm_use_asym_algo, &data,
    3318              :                                                          &data_size, &hash, &hash_size)) {
    3319            0 :         assert(false);
    3320              :     }
    3321            1 :     libspdm_reset_message_a(spdm_context);
    3322            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3323              :         m_libspdm_use_hash_algo;
    3324            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3325              :         m_libspdm_use_asym_algo;
    3326            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3327              :         m_libspdm_use_req_asym_algo;
    3328            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3329              :         m_libspdm_use_dhe_algo;
    3330            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3331              :         m_libspdm_use_aead_algo;
    3332              : 
    3333              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3334              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3335              :         data_size;
    3336              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3337              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3338              :                      data, data_size);
    3339              : #else
    3340            1 :     libspdm_hash_all(
    3341              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3342              :         data, data_size,
    3343            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3344            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3345            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3346            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3347              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3348              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3349              :         data, data_size,
    3350              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3351              : #endif
    3352            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3353              : 
    3354            1 :     req_slot_id_param = 0;
    3355            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3356              :                                                     m_libspdm_use_req_asym_algo, &data,
    3357              :                                                     &data_size, &hash, &hash_size);
    3358              :     spdm_context->local_context.
    3359            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3360              :     spdm_context->local_context.
    3361            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3362              : 
    3363            1 :     session_id = 0xFFFFFFFF;
    3364            1 :     session_info = &spdm_context->session_info[0];
    3365            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3366            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3367            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3368            1 :     libspdm_secured_message_set_response_finished_key(
    3369              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3370              :         hash_size);
    3371            1 :     libspdm_secured_message_set_session_state(
    3372              :         session_info->secured_message_context,
    3373              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3374            1 :     session_info->mut_auth_requested = 1;
    3375              : 
    3376            1 :     spdm_context->connection_info.capability.flags |=
    3377              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3378            1 :     spdm_context->local_context.capability.flags |=
    3379              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3380              : 
    3381            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3382              :                                          req_slot_id_param);
    3383            1 :     assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
    3384            1 :     free(data);
    3385            1 : }
    3386              : 
    3387              : /**
    3388              :  * Test 19:
    3389              :  * Expected behavior:
    3390              :  **/
    3391            1 : void libspdm_test_requester_finish_case19(void **state)
    3392              : {
    3393            1 : }
    3394              : 
    3395              : /**
    3396              :  * Test 20: receiving a FINISH_RSP message an incorrect MAC size (only the
    3397              :  * correct first half of the MAC), mutual authentication, and 'handshake
    3398              :  * in the clear'.
    3399              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    3400              :  **/
    3401            1 : void libspdm_test_requester_finish_case20(void **state)
    3402              : {
    3403              :     libspdm_return_t status;
    3404              :     libspdm_test_context_t *spdm_test_context;
    3405              :     libspdm_context_t *spdm_context;
    3406              :     uint32_t session_id;
    3407              :     uint8_t req_slot_id_param;
    3408              :     void *data;
    3409              :     size_t data_size;
    3410              :     void *hash;
    3411              :     size_t hash_size;
    3412              :     libspdm_session_info_t *session_info;
    3413              : 
    3414            1 :     spdm_test_context = *state;
    3415            1 :     spdm_context = spdm_test_context->spdm_context;
    3416            1 :     spdm_test_context->case_id = 0x14;
    3417            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3418              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3419            1 :     spdm_context->connection_info.connection_state =
    3420              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3421            1 :     spdm_context->connection_info.capability.flags |=
    3422              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3423            1 :     spdm_context->connection_info.capability.flags |=
    3424              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3425            1 :     spdm_context->connection_info.capability.flags |=
    3426              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3427            1 :     spdm_context->local_context.capability.flags |=
    3428              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3429            1 :     spdm_context->local_context.capability.flags |=
    3430              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3431            1 :     spdm_context->local_context.capability.flags |=
    3432              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3433            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3434              :                                                          m_libspdm_use_asym_algo, &data,
    3435              :                                                          &data_size, &hash, &hash_size)) {
    3436            0 :         assert(false);
    3437              :     }
    3438            1 :     libspdm_reset_message_a(spdm_context);
    3439            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3440              :         m_libspdm_use_hash_algo;
    3441            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3442              :         m_libspdm_use_asym_algo;
    3443            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg =
    3444              :         m_libspdm_use_req_asym_algo;
    3445            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3446              :         m_libspdm_use_dhe_algo;
    3447            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3448              :         m_libspdm_use_aead_algo;
    3449              : 
    3450              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3451              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3452              :         data_size;
    3453              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3454              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3455              :                      data, data_size);
    3456              : #else
    3457            1 :     libspdm_hash_all(
    3458              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3459              :         data, data_size,
    3460            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3461            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3462            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3463            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3464              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3465              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3466              :         data, data_size,
    3467              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3468              : #endif
    3469            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3470              : 
    3471            1 :     req_slot_id_param = 0;
    3472            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3473              :                                                     m_libspdm_use_req_asym_algo, &data,
    3474              :                                                     &data_size, &hash, &hash_size);
    3475              :     spdm_context->local_context.
    3476            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3477              :     spdm_context->local_context.
    3478            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3479              : 
    3480            1 :     session_id = 0xFFFFFFFF;
    3481            1 :     session_info = &spdm_context->session_info[0];
    3482            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3483            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3484            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3485            1 :     libspdm_secured_message_set_response_finished_key(
    3486              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3487              :         hash_size);
    3488            1 :     libspdm_secured_message_set_session_state(
    3489              :         session_info->secured_message_context,
    3490              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3491            1 :     session_info->mut_auth_requested = 1;
    3492              : 
    3493            1 :     spdm_context->connection_info.capability.flags |=
    3494              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3495            1 :     spdm_context->local_context.capability.flags |=
    3496              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3497              : 
    3498            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3499              :                                          req_slot_id_param);
    3500            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
    3501            1 :     free(data);
    3502            1 : }
    3503              : 
    3504              : /**
    3505              :  * Test 21: the requester is setup correctly, but receives an ERROR with SPDM_ERROR_CODE_DECRYPT_ERROR.
    3506              :  * Expected behavior: client returns a Status of INVALID_SESSION_ID  and free the session ID.
    3507              :  **/
    3508            1 : void libspdm_test_requester_finish_case21(void **state)
    3509              : {
    3510              :     libspdm_return_t status;
    3511              :     libspdm_test_context_t *spdm_test_context;
    3512              :     libspdm_context_t *spdm_context;
    3513              :     uint32_t session_id;
    3514              :     uint8_t req_slot_id_param;
    3515              :     void *data;
    3516              :     size_t data_size;
    3517              :     void *hash;
    3518              :     size_t hash_size;
    3519              :     libspdm_session_info_t *session_info;
    3520              : 
    3521            1 :     spdm_test_context = *state;
    3522            1 :     spdm_context = spdm_test_context->spdm_context;
    3523            1 :     spdm_test_context->case_id = 0x15;
    3524            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3525              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3526            1 :     spdm_context->connection_info.connection_state =
    3527              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3528            1 :     spdm_context->connection_info.capability.flags |=
    3529              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3530            1 :     spdm_context->connection_info.capability.flags |=
    3531              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3532            1 :     spdm_context->connection_info.capability.flags |=
    3533              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3534            1 :     spdm_context->local_context.capability.flags |=
    3535              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3536            1 :     spdm_context->local_context.capability.flags |=
    3537              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3538            1 :     spdm_context->local_context.capability.flags |=
    3539              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3540            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3541              :                                                          m_libspdm_use_asym_algo, &data,
    3542              :                                                          &data_size, &hash, &hash_size)) {
    3543            0 :         assert(false);
    3544              :     }
    3545            1 :     libspdm_reset_message_a(spdm_context);
    3546            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3547              :         m_libspdm_use_hash_algo;
    3548            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3549              :         m_libspdm_use_asym_algo;
    3550            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3551              :         m_libspdm_use_dhe_algo;
    3552            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3553              :         m_libspdm_use_aead_algo;
    3554              : 
    3555              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3556              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3557              :         data_size;
    3558              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3559              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3560              :                      data, data_size);
    3561              : #else
    3562            1 :     libspdm_hash_all(
    3563              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3564              :         data, data_size,
    3565            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3566            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3567            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3568            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3569              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3570              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3571              :         data, data_size,
    3572              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3573              : #endif
    3574            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3575              : 
    3576            1 :     session_id = 0xFFFFFFFF;
    3577            1 :     session_info = &spdm_context->session_info[0];
    3578            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3579            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3580            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3581            1 :     libspdm_secured_message_set_response_finished_key(
    3582              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3583              :         hash_size);
    3584            1 :     libspdm_secured_message_set_session_state(
    3585              :         session_info->secured_message_context,
    3586              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3587              : 
    3588            1 :     spdm_context->connection_info.capability.flags |=
    3589              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3590            1 :     spdm_context->local_context.capability.flags |=
    3591              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3592            1 :     req_slot_id_param = 0;
    3593            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3594              :                                          req_slot_id_param);
    3595            1 :     assert_int_equal(status, LIBSPDM_STATUS_SESSION_MSG_ERROR);
    3596            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    3597            1 :     free(data);
    3598            1 : }
    3599              : 
    3600              : /**
    3601              :  * Test 22: a FINISH request message is successfully sent and a FINISH_RSP response message is
    3602              :  * successfully received.
    3603              :  * Expected Behavior: requester returns the status RETURN_SUCCESS and a FINISH_RSP message is
    3604              :  * received, buffer F appends the exchanged FINISH and FINISH_RSP
    3605              :  **/
    3606            1 : void libspdm_test_requester_finish_case22(void **state)
    3607              : {
    3608              :     libspdm_return_t status;
    3609              :     libspdm_test_context_t *spdm_test_context;
    3610              :     libspdm_context_t *spdm_context;
    3611              :     uint32_t session_id;
    3612              :     uint8_t req_slot_id_param;
    3613              :     void *data;
    3614              :     size_t data_size;
    3615              :     void *hash;
    3616              :     size_t hash_size;
    3617              :     libspdm_session_info_t *session_info;
    3618              : 
    3619            1 :     spdm_test_context = *state;
    3620            1 :     spdm_context = spdm_test_context->spdm_context;
    3621            1 :     spdm_test_context->case_id = 0x16;
    3622            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3623              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3624            1 :     spdm_context->connection_info.connection_state =
    3625              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3626            1 :     spdm_context->connection_info.capability.flags |=
    3627              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3628            1 :     spdm_context->connection_info.capability.flags |=
    3629              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3630            1 :     spdm_context->connection_info.capability.flags |=
    3631              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3632            1 :     spdm_context->local_context.capability.flags |=
    3633              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3634            1 :     spdm_context->local_context.capability.flags |=
    3635              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3636            1 :     spdm_context->local_context.capability.flags |=
    3637              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3638            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3639              :                                                          m_libspdm_use_asym_algo, &data,
    3640              :                                                          &data_size, &hash, &hash_size)) {
    3641            0 :         assert(false);
    3642              :     }
    3643            1 :     libspdm_reset_message_a(spdm_context);
    3644            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    3645            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    3646            1 :     spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
    3647            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    3648            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    3649              : 
    3650              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3651              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3652              :         data_size;
    3653              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3654              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3655              :                      data, data_size);
    3656              : #else
    3657            1 :     libspdm_hash_all(
    3658              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3659              :         data, data_size,
    3660            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3661            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3662            1 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
    3663            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3664              :         spdm_context->connection_info.algorithm.base_hash_algo,
    3665              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3666              :         data, data_size,
    3667              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3668              : #endif
    3669            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3670              : 
    3671            1 :     req_slot_id_param = 0;
    3672            1 :     libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
    3673              :                                                     m_libspdm_use_req_asym_algo, &data,
    3674              :                                                     &data_size, &hash, &hash_size);
    3675              :     spdm_context->local_context.
    3676            1 :     local_cert_chain_provision_size[req_slot_id_param] = data_size;
    3677              :     spdm_context->local_context.
    3678            1 :     local_cert_chain_provision[req_slot_id_param] = data;
    3679              : 
    3680            1 :     session_id = 0xFFFFFFFF;
    3681            1 :     session_info = &spdm_context->session_info[0];
    3682            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3683            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3684            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3685            1 :     libspdm_secured_message_set_response_finished_key(
    3686              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3687              :         hash_size);
    3688            1 :     libspdm_secured_message_set_session_state(
    3689              :         session_info->secured_message_context,
    3690              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3691            1 :     session_info->mut_auth_requested = 1;
    3692              : 
    3693            1 :     spdm_context->connection_info.capability.flags |=
    3694              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3695            1 :     spdm_context->local_context.capability.flags |=
    3696              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3697              : 
    3698            1 :     status = libspdm_send_receive_finish(spdm_context, session_id,
    3699              :                                          req_slot_id_param);
    3700            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3701            1 :     assert_int_equal(
    3702              :         libspdm_secured_message_get_session_state(
    3703              :             spdm_context->session_info[0].secured_message_context),
    3704              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3705              : 
    3706              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3707              :     assert_int_equal(spdm_context->session_info[0].session_transcript.message_f.buffer_size,
    3708              :                      m_libspdm_local_buffer_size);
    3709              :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%x):\n",
    3710              :                    m_libspdm_local_buffer_size));
    3711              :     libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3712              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_f.buffer,
    3713              :                         m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    3714              : #endif
    3715            1 :     free(data);
    3716            1 : }
    3717              : 
    3718              : /**
    3719              :  * Test 23: receiving a correct FINISH_RSP message using slot_id 0xFF
    3720              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3721              :  * session is established.
    3722              :  **/
    3723            1 : void libspdm_test_requester_finish_case23(void **state)
    3724              : {
    3725              :     libspdm_return_t status;
    3726              :     libspdm_test_context_t *spdm_test_context;
    3727              :     libspdm_context_t *spdm_context;
    3728              :     uint32_t session_id;
    3729              :     uint8_t req_slot_id_param;
    3730              :     void *data;
    3731              :     size_t data_size;
    3732              :     size_t hash_size;
    3733              :     libspdm_session_info_t *session_info;
    3734              :     libspdm_secured_message_context_t *secured_message_context;
    3735              : 
    3736            1 :     spdm_test_context = *state;
    3737            1 :     spdm_context = spdm_test_context->spdm_context;
    3738            1 :     spdm_test_context->case_id = 0x17;
    3739            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3740              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3741            1 :     spdm_context->connection_info.connection_state =
    3742              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3743            1 :     spdm_context->connection_info.capability.flags |=
    3744              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3745            1 :     spdm_context->connection_info.capability.flags |=
    3746              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3747            1 :     spdm_context->connection_info.capability.flags |=
    3748              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3749            1 :     spdm_context->connection_info.capability.flags |=
    3750              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PUB_KEY_ID_CAP;
    3751            1 :     spdm_context->local_context.capability.flags |=
    3752              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3753            1 :     spdm_context->local_context.capability.flags |=
    3754              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3755            1 :     spdm_context->local_context.capability.flags |=
    3756              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3757            1 :     spdm_context->local_context.capability.flags |=
    3758              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
    3759            1 :     libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data, &data_size);
    3760            1 :     spdm_context->local_context.peer_public_key_provision = data;
    3761            1 :     spdm_context->local_context.peer_public_key_provision_size = data_size;
    3762              : 
    3763            1 :     libspdm_reset_message_a(spdm_context);
    3764            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3765              :         m_libspdm_use_hash_algo;
    3766            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3767              :         m_libspdm_use_asym_algo;
    3768            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3769              :         m_libspdm_use_dhe_algo;
    3770            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3771              :         m_libspdm_use_aead_algo;
    3772              : 
    3773            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0xFF;
    3774              : 
    3775            1 :     session_id = 0xFFFFFFFF;
    3776            1 :     session_info = &spdm_context->session_info[0];
    3777            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3778            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3779            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3780            1 :     libspdm_secured_message_set_response_finished_key(
    3781              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3782              :         hash_size);
    3783            1 :     libspdm_secured_message_set_session_state(
    3784              :         session_info->secured_message_context,
    3785              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3786              : 
    3787            1 :     spdm_context->connection_info.capability.flags |=
    3788              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3789            1 :     spdm_context->local_context.capability.flags |=
    3790              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3791            1 :     req_slot_id_param = 0;
    3792            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    3793            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3794            1 :     assert_int_equal(
    3795              :         libspdm_secured_message_get_session_state(
    3796              :             spdm_context->session_info[0].secured_message_context),
    3797              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3798              : 
    3799            1 :     secured_message_context = session_info->secured_message_context;
    3800              : 
    3801            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    3802              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    3803            1 :     free(data);
    3804            1 : }
    3805              : 
    3806              : /**
    3807              :  * Test 24: Set HANDSHAKE_IN_THE_CLEAR_CAP to 0 , The ResponderVerifyData field is absent.
    3808              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3809              :  * session is established.
    3810              :  **/
    3811            1 : void libspdm_test_requester_finish_case24(void **state)
    3812              : {
    3813              :     libspdm_return_t status;
    3814              :     libspdm_test_context_t *spdm_test_context;
    3815              :     libspdm_context_t *spdm_context;
    3816              :     uint32_t session_id;
    3817              :     uint8_t req_slot_id_param;
    3818              :     void *data;
    3819              :     size_t data_size;
    3820              :     void *hash;
    3821              :     size_t hash_size;
    3822              :     libspdm_session_info_t *session_info;
    3823              :     libspdm_secured_message_context_t *secured_message_context;
    3824              : 
    3825            1 :     spdm_test_context = *state;
    3826            1 :     spdm_context = spdm_test_context->spdm_context;
    3827            1 :     spdm_test_context->case_id = 0x18;
    3828            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    3829              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3830            1 :     spdm_context->connection_info.connection_state =
    3831              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3832            1 :     spdm_context->connection_info.capability.flags |=
    3833              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3834            1 :     spdm_context->connection_info.capability.flags |=
    3835              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3836            1 :     spdm_context->connection_info.capability.flags |=
    3837              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3838            1 :     spdm_context->local_context.capability.flags |=
    3839              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3840            1 :     spdm_context->local_context.capability.flags |=
    3841              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3842            1 :     spdm_context->local_context.capability.flags |=
    3843              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3844            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3845              :                                                          m_libspdm_use_asym_algo, &data,
    3846              :                                                          &data_size, &hash, &hash_size)) {
    3847            0 :         assert(false);
    3848              :     }
    3849            1 :     libspdm_reset_message_a(spdm_context);
    3850            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3851              :         m_libspdm_use_hash_algo;
    3852            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3853              :         m_libspdm_use_asym_algo;
    3854            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3855              :         m_libspdm_use_dhe_algo;
    3856            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3857              :         m_libspdm_use_aead_algo;
    3858              : 
    3859              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3860              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3861              :         data_size;
    3862              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3863              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3864              :                      data, data_size);
    3865              : #else
    3866            1 :     libspdm_hash_all(
    3867              :         m_libspdm_use_hash_algo,
    3868              :         data, data_size,
    3869            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3870            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3871            1 :         libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3872            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3873              :         m_libspdm_use_hash_algo,
    3874              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3875              :         data, data_size,
    3876              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3877              : #endif
    3878            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3879              : 
    3880              :     /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0*/
    3881            1 :     spdm_context->connection_info.capability.flags &=
    3882              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3883            1 :     spdm_context->local_context.capability.flags &=
    3884              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3885              : 
    3886            1 :     session_id = 0xFFFFFFFF;
    3887            1 :     session_info = &spdm_context->session_info[0];
    3888            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    3889            1 :     spdm_context->last_spdm_request_session_id = session_id;
    3890            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    3891            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3892            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    3893            1 :     libspdm_secured_message_set_response_finished_key(
    3894              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    3895              :         hash_size);
    3896            1 :     libspdm_secured_message_set_session_state(
    3897              :         session_info->secured_message_context,
    3898              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    3899              : 
    3900            1 :     req_slot_id_param = 0;
    3901            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    3902            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    3903            1 :     assert_int_equal(
    3904              :         libspdm_secured_message_get_session_state(
    3905              :             spdm_context->session_info[0].secured_message_context),
    3906              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    3907              : 
    3908            1 :     secured_message_context = session_info->secured_message_context;
    3909              : 
    3910            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    3911              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    3912            1 :     free(data);
    3913            1 : }
    3914              : 
    3915              : /**
    3916              :  * Test 25: SPDM version 1.4, with OpaqueData
    3917              :  * Expected behavior: client returns a Status of RETURN_SUCCESS and
    3918              :  * session is established.
    3919              :  **/
    3920            1 : void libspdm_test_requester_finish_case25(void **state)
    3921              : {
    3922              :     libspdm_return_t status;
    3923              :     libspdm_test_context_t *spdm_test_context;
    3924              :     libspdm_context_t *spdm_context;
    3925              :     uint32_t session_id;
    3926              :     uint8_t req_slot_id_param;
    3927              :     void *data;
    3928              :     size_t data_size;
    3929              :     void *hash;
    3930              :     size_t hash_size;
    3931              :     libspdm_session_info_t *session_info;
    3932              :     libspdm_secured_message_context_t *secured_message_context;
    3933              : 
    3934            1 :     spdm_test_context = *state;
    3935            1 :     spdm_context = spdm_test_context->spdm_context;
    3936            1 :     spdm_test_context->case_id = 0x19;
    3937            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
    3938              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    3939            1 :     spdm_context->connection_info.connection_state =
    3940              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    3941            1 :     spdm_context->connection_info.capability.flags |=
    3942              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
    3943            1 :     spdm_context->connection_info.capability.flags |=
    3944              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    3945            1 :     spdm_context->connection_info.capability.flags |=
    3946              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    3947            1 :     spdm_context->local_context.capability.flags |=
    3948              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
    3949            1 :     spdm_context->local_context.capability.flags |=
    3950              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    3951            1 :     spdm_context->local_context.capability.flags |=
    3952              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    3953            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    3954              :                                                          m_libspdm_use_asym_algo, &data,
    3955              :                                                          &data_size, &hash, &hash_size)) {
    3956            0 :         assert(false);
    3957              :     }
    3958            1 :     libspdm_reset_message_a(spdm_context);
    3959            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    3960              :         m_libspdm_use_hash_algo;
    3961            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    3962              :         m_libspdm_use_asym_algo;
    3963            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    3964              :         m_libspdm_use_dhe_algo;
    3965            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    3966              :         m_libspdm_use_aead_algo;
    3967              : 
    3968              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    3969              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    3970              :         data_size;
    3971              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    3972              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    3973              :                      data, data_size);
    3974              : #else
    3975            1 :     libspdm_hash_all(
    3976              :         m_libspdm_use_hash_algo,
    3977              :         data, data_size,
    3978            1 :         spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
    3979            1 :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
    3980            1 :         libspdm_get_hash_size(m_libspdm_use_hash_algo);
    3981            1 :     libspdm_get_leaf_cert_public_key_from_cert_chain(
    3982              :         m_libspdm_use_hash_algo,
    3983              :         spdm_context->connection_info.algorithm.base_asym_algo,
    3984              :         data, data_size,
    3985              :         &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
    3986              : #endif
    3987            1 :     spdm_context->connection_info.peer_used_cert_chain_slot_id = 0;
    3988              : 
    3989              :     /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0*/
    3990            1 :     spdm_context->connection_info.capability.flags &=
    3991              :         ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3992            1 :     spdm_context->local_context.capability.flags &=
    3993              :         ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
    3994              : 
    3995            1 :     session_id = 0xFFFFFFFF;
    3996            1 :     session_info = &spdm_context->session_info[0];
    3997            1 :     spdm_context->last_spdm_request_session_id_valid = true;
    3998            1 :     spdm_context->last_spdm_request_session_id = session_id;
    3999            1 :     libspdm_session_info_init(spdm_context, session_info, session_id, false);
    4000            1 :     hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
    4001            1 :     libspdm_set_mem(m_libspdm_dummy_buffer, hash_size, (uint8_t)(0xFF));
    4002            1 :     libspdm_secured_message_set_response_finished_key(
    4003              :         session_info->secured_message_context, m_libspdm_dummy_buffer,
    4004              :         hash_size);
    4005            1 :     libspdm_secured_message_set_session_state(
    4006              :         session_info->secured_message_context,
    4007              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    4008              : 
    4009            1 :     req_slot_id_param = 0;
    4010            1 :     status = libspdm_send_receive_finish(spdm_context, session_id, req_slot_id_param);
    4011            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    4012            1 :     assert_int_equal(
    4013              :         libspdm_secured_message_get_session_state(
    4014              :             spdm_context->session_info[0].secured_message_context),
    4015              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    4016              : 
    4017            1 :     secured_message_context = session_info->secured_message_context;
    4018              : 
    4019            1 :     assert_memory_equal((const void *)secured_message_context->master_secret.master_secret,
    4020              :                         (const void *)m_libspdm_zero_buffer, sizeof(m_libspdm_zero_buffer));
    4021            1 :     free(data);
    4022            1 : }
    4023              : 
    4024            1 : int libspdm_requester_finish_test_main(void)
    4025              : {
    4026            1 :     const struct CMUnitTest spdm_requester_finish_tests[] = {
    4027              :         /* SendRequest failed*/
    4028              :         cmocka_unit_test(libspdm_test_requester_finish_case1),
    4029              :         /* Successful response*/
    4030              :         cmocka_unit_test(libspdm_test_requester_finish_case2),
    4031              :         /* connection_state check failed*/
    4032              :         cmocka_unit_test(libspdm_test_requester_finish_case3),
    4033              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    4034              :         cmocka_unit_test(libspdm_test_requester_finish_case4),
    4035              :         /* Always SPDM_ERROR_CODE_BUSY*/
    4036              :         cmocka_unit_test(libspdm_test_requester_finish_case5),
    4037              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    4038              :         cmocka_unit_test(libspdm_test_requester_finish_case6),
    4039              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    4040              :         cmocka_unit_test(libspdm_test_requester_finish_case7),
    4041              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    4042              :         cmocka_unit_test(libspdm_test_requester_finish_case8),
    4043              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    4044              :         cmocka_unit_test(libspdm_test_requester_finish_case9),
    4045              :         /* Unexpected errors*/
    4046              :         cmocka_unit_test(libspdm_test_requester_finish_case10),
    4047              :         /* Buffer reset*/
    4048              :         cmocka_unit_test(libspdm_test_requester_finish_case11),
    4049              :         /* No correct setup*/
    4050              :         cmocka_unit_test(libspdm_test_requester_finish_case12),
    4051              :         cmocka_unit_test(libspdm_test_requester_finish_case13),
    4052              :         cmocka_unit_test(libspdm_test_requester_finish_case14),
    4053              :         cmocka_unit_test(libspdm_test_requester_finish_case15),
    4054              :         /* Successful response*/
    4055              :         cmocka_unit_test(libspdm_test_requester_finish_case16),
    4056              :         /* Response with invalid MAC*/
    4057              :         cmocka_unit_test(libspdm_test_requester_finish_case17),
    4058              :         cmocka_unit_test(libspdm_test_requester_finish_case18),
    4059              :         /* Can be populated with new test.*/
    4060              :         cmocka_unit_test(libspdm_test_requester_finish_case19),
    4061              :         cmocka_unit_test(libspdm_test_requester_finish_case20),
    4062              :         /* Error response: SPDM_ERROR_CODE_DECRYPT_ERROR*/
    4063              :         cmocka_unit_test(libspdm_test_requester_finish_case21),
    4064              :         /* Buffer verification*/
    4065              :         cmocka_unit_test(libspdm_test_requester_finish_case22),
    4066              :         /* Successful response using provisioned public key (slot_id 0xFF) */
    4067              :         cmocka_unit_test(libspdm_test_requester_finish_case23),
    4068              :         /* Set HANDSHAKE_IN_THE_CLEAR_CAP to 0 , The ResponderVerifyData field is absent.*/
    4069              :         cmocka_unit_test(libspdm_test_requester_finish_case24),
    4070              :         /* SPDM 1.4 with OpaqueData */
    4071              :         cmocka_unit_test(libspdm_test_requester_finish_case25),
    4072              :     };
    4073              : 
    4074            1 :     libspdm_test_context_t test_context = {
    4075              :         LIBSPDM_TEST_CONTEXT_VERSION,
    4076              :         true,
    4077              :         libspdm_requester_finish_test_send_message,
    4078              :         libspdm_requester_finish_test_receive_message,
    4079              :     };
    4080              : 
    4081            1 :     libspdm_setup_test_context(&test_context);
    4082              : 
    4083            1 :     return cmocka_run_group_tests(spdm_requester_finish_tests,
    4084              :                                   libspdm_unit_test_group_setup,
    4085              :                                   libspdm_unit_test_group_teardown);
    4086              : }
    4087              : 
    4088              : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
        

Generated by: LCOV version 2.0-1