LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - psk_finish.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 91.0 % 1121 1020
Test Date: 2025-10-12 08:10:56 Functions: 100.0 % 23 23

            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_PSK_CAP
      12              : 
      13              : static uint8_t m_libspdm_dummy_key_buffer[LIBSPDM_MAX_AEAD_KEY_SIZE];
      14              : static uint8_t m_libspdm_dummy_salt_buffer[LIBSPDM_MAX_AEAD_IV_SIZE];
      15              : 
      16              : static size_t m_libspdm_local_buffer_size;
      17              : static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE];
      18              : 
      19           34 : static void libspdm_secured_message_set_dummy_finished_key(
      20              :     void *spdm_secured_message_context)
      21              : {
      22           34 : }
      23              : 
      24           33 : void libspdm_secured_message_set_response_handshake_encryption_key(
      25              :     void *spdm_secured_message_context, const void *key, size_t key_size)
      26              : {
      27              :     libspdm_secured_message_context_t *secured_message_context;
      28              : 
      29           33 :     secured_message_context = spdm_secured_message_context;
      30           33 :     LIBSPDM_ASSERT(key_size == secured_message_context->aead_key_size);
      31           33 :     libspdm_copy_mem(secured_message_context->handshake_secret.response_handshake_encryption_key,
      32              :                      sizeof(secured_message_context->handshake_secret.
      33              :                             response_handshake_encryption_key),
      34              :                      key, secured_message_context->aead_key_size);
      35           33 : }
      36              : 
      37           33 : void libspdm_secured_message_set_response_handshake_salt(
      38              :     void *spdm_secured_message_context, const void *salt,
      39              :     size_t salt_size)
      40              : {
      41              :     libspdm_secured_message_context_t *secured_message_context;
      42              : 
      43           33 :     secured_message_context = spdm_secured_message_context;
      44           33 :     LIBSPDM_ASSERT(salt_size == secured_message_context->aead_iv_size);
      45           33 :     libspdm_copy_mem(secured_message_context->handshake_secret.response_handshake_salt,
      46              :                      sizeof(secured_message_context->handshake_secret.response_handshake_salt),
      47              :                      salt, secured_message_context->aead_iv_size);
      48           33 : }
      49              : 
      50           34 : static libspdm_return_t send_message(
      51              :     void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
      52              : {
      53              :     libspdm_test_context_t *spdm_test_context;
      54              : 
      55           34 :     spdm_test_context = libspdm_get_test_context();
      56           34 :     switch (spdm_test_context->case_id) {
      57            1 :     case 0x1:
      58            1 :         return LIBSPDM_STATUS_SEND_FAIL;
      59            1 :     case 0x2:
      60            1 :         return LIBSPDM_STATUS_SUCCESS;
      61            0 :     case 0x3:
      62            0 :         return LIBSPDM_STATUS_SUCCESS;
      63            1 :     case 0x4:
      64            1 :         return LIBSPDM_STATUS_SUCCESS;
      65            1 :     case 0x5:
      66            1 :         return LIBSPDM_STATUS_SUCCESS;
      67            2 :     case 0x6:
      68            2 :         return LIBSPDM_STATUS_SUCCESS;
      69            1 :     case 0x7:
      70            1 :         return LIBSPDM_STATUS_SUCCESS;
      71            2 :     case 0x8:
      72            2 :         return LIBSPDM_STATUS_SUCCESS;
      73            2 :     case 0x9:
      74            2 :         return LIBSPDM_STATUS_SUCCESS;
      75           18 :     case 0xA:
      76           18 :         return LIBSPDM_STATUS_SUCCESS;
      77            1 :     case 0xB:
      78            1 :         return LIBSPDM_STATUS_SUCCESS;
      79            0 :     case 0xC:
      80            0 :         return LIBSPDM_STATUS_SUCCESS;
      81            1 :     case 0xD:
      82            1 :         return LIBSPDM_STATUS_SUCCESS;
      83            0 :     case 0xE:
      84            0 :         return LIBSPDM_STATUS_SUCCESS;
      85            1 :     case 0xF:
      86            1 :         return LIBSPDM_STATUS_SUCCESS;
      87            2 :     case 0x10:
      88              :     case 0x11:
      89              :     {
      90              :         libspdm_return_t status;
      91              :         uint8_t *decoded_message;
      92              :         size_t decoded_message_size;
      93              :         uint32_t session_id;
      94              :         uint32_t *message_session_id;
      95              :         bool is_app_message;
      96              :         libspdm_session_info_t *session_info;
      97              :         uint8_t message_buffer[LIBSPDM_SENDER_BUFFER_SIZE];
      98              : 
      99            2 :         message_session_id = NULL;
     100            2 :         session_id = 0xFFFFFFFF;
     101            2 :         decoded_message = (uint8_t *) &m_libspdm_local_buffer[0];
     102            2 :         decoded_message_size = sizeof(m_libspdm_local_buffer);
     103              : 
     104            2 :         session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
     105            2 :         if (session_info == NULL) {
     106            0 :             return LIBSPDM_STATUS_SEND_FAIL;
     107              :         }
     108              : 
     109            2 :         memcpy(message_buffer, request, request_size);
     110              : 
     111            2 :         ((libspdm_secured_message_context_t *)(session_info->secured_message_context))
     112            2 :         ->handshake_secret.request_handshake_sequence_number--;
     113            2 :         m_libspdm_local_buffer_size = 0;
     114            2 :         libspdm_get_scratch_buffer (spdm_context, (void **)&decoded_message, &decoded_message_size);
     115            2 :         status = libspdm_transport_test_decode_message(
     116              :             spdm_context,
     117              :             &message_session_id, &is_app_message, true, request_size, message_buffer,
     118              :             &decoded_message_size, (void **)&decoded_message);
     119            2 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     120            0 :             return LIBSPDM_STATUS_SEND_FAIL;
     121              :         }
     122            2 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     123              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
     124              :                          decoded_message, decoded_message_size);
     125            2 :         m_libspdm_local_buffer_size += decoded_message_size;
     126              :     }
     127            2 :         return LIBSPDM_STATUS_SUCCESS;
     128            0 :     default:
     129            0 :         return LIBSPDM_STATUS_SEND_FAIL;
     130              :     }
     131              : }
     132              : 
     133           33 : static libspdm_return_t receive_message(
     134              :     void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
     135              : {
     136              :     libspdm_test_context_t *spdm_test_context;
     137              : 
     138           33 :     spdm_test_context = libspdm_get_test_context();
     139           33 :     switch (spdm_test_context->case_id) {
     140            0 :     case 0x1:
     141            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     142              : 
     143            1 :     case 0x2: {
     144              :         spdm_psk_finish_response_t *spdm_response;
     145              :         size_t spdm_response_size;
     146              :         size_t transport_header_size;
     147              :         uint32_t session_id;
     148              :         libspdm_session_info_t *session_info;
     149              :         uint8_t *scratch_buffer;
     150              :         size_t scratch_buffer_size;
     151              : 
     152            1 :         session_id = 0xFFFFFFFF;
     153            1 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     154            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     155            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     156              : 
     157            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     158            1 :         spdm_response->header.request_response_code =
     159              :             SPDM_PSK_FINISH_RSP;
     160            1 :         spdm_response->header.param1 = 0;
     161            1 :         spdm_response->header.param2 = 0;
     162              : 
     163              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     164              :          * transport_message is always in sender buffer. */
     165            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     166            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     167              :                           scratch_buffer_size - transport_header_size,
     168              :                           spdm_response, spdm_response_size);
     169            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     170            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     171              :                                               false, false, spdm_response_size,
     172              :                                               spdm_response, response_size,
     173              :                                               response);
     174            1 :         session_info = libspdm_get_session_info_via_session_id(
     175              :             spdm_context, session_id);
     176            1 :         if (session_info == NULL) {
     177            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     178              :         }
     179              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     180              :         ((libspdm_secured_message_context_t
     181            1 :           *)(session_info->secured_message_context))
     182            1 :         ->handshake_secret.response_handshake_sequence_number--;
     183              :     }
     184            1 :         return LIBSPDM_STATUS_SUCCESS;
     185              : 
     186            0 :     case 0x3: {
     187              :         spdm_psk_finish_response_t *spdm_response;
     188              :         size_t spdm_response_size;
     189              :         size_t transport_header_size;
     190              :         uint32_t session_id;
     191              :         libspdm_session_info_t *session_info;
     192              :         uint8_t *scratch_buffer;
     193              :         size_t scratch_buffer_size;
     194              : 
     195            0 :         session_id = 0xFFFFFFFF;
     196            0 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     197            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     198            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     199              : 
     200            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     201            0 :         spdm_response->header.request_response_code =
     202              :             SPDM_PSK_FINISH_RSP;
     203            0 :         spdm_response->header.param1 = 0;
     204            0 :         spdm_response->header.param2 = 0;
     205              : 
     206              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     207              :          * transport_message is always in sender buffer. */
     208            0 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     209            0 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     210              :                           scratch_buffer_size - transport_header_size,
     211              :                           spdm_response, spdm_response_size);
     212            0 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     213            0 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     214              :                                               false, false, spdm_response_size,
     215              :                                               spdm_response, response_size,
     216              :                                               response);
     217            0 :         session_info = libspdm_get_session_info_via_session_id(
     218              :             spdm_context, session_id);
     219            0 :         if (session_info == NULL) {
     220            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     221              :         }
     222              :         ((libspdm_secured_message_context_t
     223            0 :           *)(session_info->secured_message_context))
     224            0 :         ->handshake_secret.response_handshake_sequence_number--;
     225              :     }
     226            0 :         return LIBSPDM_STATUS_SUCCESS;
     227              : 
     228            1 :     case 0x4: {
     229              :         spdm_error_response_t *spdm_response;
     230              :         size_t spdm_response_size;
     231              :         size_t transport_header_size;
     232              :         uint32_t session_id;
     233              :         libspdm_session_info_t *session_info;
     234              :         uint8_t *scratch_buffer;
     235              :         size_t scratch_buffer_size;
     236              : 
     237            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     238            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     239            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     240              : 
     241            1 :         session_id = 0xFFFFFFFF;
     242            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     243            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     244            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
     245            1 :         spdm_response->header.param2 = 0;
     246              : 
     247              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     248              :          * transport_message is always in sender buffer. */
     249            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     250            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     251              :                           scratch_buffer_size - transport_header_size,
     252              :                           spdm_response, spdm_response_size);
     253            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     254            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     255              :                                               false, false,
     256              :                                               spdm_response_size,
     257              :                                               spdm_response,
     258              :                                               response_size, response);
     259            1 :         session_info = libspdm_get_session_info_via_session_id(
     260              :             spdm_context, session_id);
     261            1 :         if (session_info == NULL) {
     262            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     263              :         }
     264              :         ((libspdm_secured_message_context_t
     265            1 :           *)(session_info->secured_message_context))
     266            1 :         ->handshake_secret.response_handshake_sequence_number--;
     267              :     }
     268            1 :         return LIBSPDM_STATUS_SUCCESS;
     269              : 
     270            1 :     case 0x5: {
     271              :         spdm_error_response_t *spdm_response;
     272              :         size_t spdm_response_size;
     273              :         size_t transport_header_size;
     274              :         uint32_t session_id;
     275              :         libspdm_session_info_t *session_info;
     276              :         uint8_t *scratch_buffer;
     277              :         size_t scratch_buffer_size;
     278              : 
     279            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     280            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     281            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     282              : 
     283            1 :         session_id = 0xFFFFFFFF;
     284            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     285            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     286            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     287            1 :         spdm_response->header.param2 = 0;
     288              : 
     289              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     290              :          * transport_message is always in sender buffer. */
     291            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     292            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     293              :                           scratch_buffer_size - transport_header_size,
     294              :                           spdm_response, spdm_response_size);
     295            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     296            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     297              :                                               false, false,
     298              :                                               spdm_response_size,
     299              :                                               spdm_response,
     300              :                                               response_size, response);
     301            1 :         session_info = libspdm_get_session_info_via_session_id(
     302              :             spdm_context, session_id);
     303            1 :         if (session_info == NULL) {
     304            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     305              :         }
     306              :         ((libspdm_secured_message_context_t
     307            1 :           *)(session_info->secured_message_context))
     308            1 :         ->handshake_secret.response_handshake_sequence_number--;
     309              :     }
     310            1 :         return LIBSPDM_STATUS_SUCCESS;
     311              : 
     312            2 :     case 0x6: {
     313              :         static size_t sub_index1 = 0;
     314            2 :         if (sub_index1 == 0) {
     315              :             spdm_error_response_t *spdm_response;
     316              :             size_t spdm_response_size;
     317              :             size_t transport_header_size;
     318              :             uint32_t session_id;
     319              :             libspdm_session_info_t *session_info;
     320              :             uint8_t *scratch_buffer;
     321              :             size_t scratch_buffer_size;
     322              : 
     323            1 :             spdm_response_size = sizeof(spdm_error_response_t);
     324            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     325            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     326              : 
     327            1 :             session_id = 0xFFFFFFFF;
     328            1 :             spdm_response->header.spdm_version =
     329              :                 SPDM_MESSAGE_VERSION_11;
     330            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     331            1 :             spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
     332            1 :             spdm_response->header.param2 = 0;
     333              : 
     334              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     335              :              * transport_message is always in sender buffer. */
     336            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     337              :                                         &scratch_buffer_size);
     338            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     339              :                               scratch_buffer_size - transport_header_size,
     340              :                               spdm_response, spdm_response_size);
     341            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     342            1 :             libspdm_transport_test_encode_message(
     343              :                 spdm_context, &session_id, false, false,
     344              :                 spdm_response_size, spdm_response,
     345              :                 response_size, response);
     346            1 :             sub_index1++;
     347            1 :             session_info = libspdm_get_session_info_via_session_id(
     348              :                 spdm_context, session_id);
     349            1 :             if (session_info == NULL) {
     350            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     351              :             }
     352              :             ((libspdm_secured_message_context_t
     353            1 :               *)(session_info->secured_message_context))
     354              :             ->handshake_secret
     355            1 :             .response_handshake_sequence_number--;
     356            1 :         } else if (sub_index1 == 1) {
     357              :             spdm_psk_finish_response_t *spdm_response;
     358              :             size_t spdm_response_size;
     359              :             size_t transport_header_size;
     360              :             uint32_t session_id;
     361              :             libspdm_session_info_t *session_info;
     362              :             uint8_t *scratch_buffer;
     363              :             size_t scratch_buffer_size;
     364              : 
     365            1 :             session_id = 0xFFFFFFFF;
     366            1 :             spdm_response_size = sizeof(spdm_psk_finish_response_t);
     367            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     368            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     369              : 
     370            1 :             spdm_response->header.spdm_version =
     371              :                 SPDM_MESSAGE_VERSION_11;
     372            1 :             spdm_response->header.request_response_code =
     373              :                 SPDM_PSK_FINISH_RSP;
     374            1 :             spdm_response->header.param1 = 0;
     375            1 :             spdm_response->header.param2 = 0;
     376              : 
     377              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     378              :              * transport_message is always in sender buffer. */
     379            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     380              :                                         &scratch_buffer_size);
     381            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     382              :                               scratch_buffer_size - transport_header_size,
     383              :                               spdm_response, spdm_response_size);
     384            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     385            1 :             libspdm_transport_test_encode_message(
     386              :                 spdm_context, &session_id, false, false,
     387              :                 spdm_response_size, spdm_response, response_size,
     388              :                 response);
     389            1 :             session_info = libspdm_get_session_info_via_session_id(
     390              :                 spdm_context, session_id);
     391            1 :             if (session_info == NULL) {
     392            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     393              :             }
     394              :             ((libspdm_secured_message_context_t
     395            1 :               *)(session_info->secured_message_context))
     396              :             ->handshake_secret
     397            1 :             .response_handshake_sequence_number--;
     398              :         }
     399              :     }
     400            2 :         return LIBSPDM_STATUS_SUCCESS;
     401              : 
     402            1 :     case 0x7: {
     403              :         spdm_error_response_t *spdm_response;
     404              :         size_t spdm_response_size;
     405              :         size_t transport_header_size;
     406              :         uint32_t session_id;
     407              :         libspdm_session_info_t *session_info;
     408              :         uint8_t *scratch_buffer;
     409              :         size_t scratch_buffer_size;
     410              : 
     411            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     412            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     413            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     414              : 
     415            1 :         session_id = 0xFFFFFFFF;
     416            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
     417            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     418            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
     419            1 :         spdm_response->header.param2 = 0;
     420              : 
     421              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     422              :          * transport_message is always in sender buffer. */
     423            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     424            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     425              :                           scratch_buffer_size - transport_header_size,
     426              :                           spdm_response, spdm_response_size);
     427            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     428            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     429              :                                               false, false,
     430              :                                               spdm_response_size,
     431              :                                               spdm_response,
     432              :                                               response_size, response);
     433            1 :         session_info = libspdm_get_session_info_via_session_id(
     434              :             spdm_context, session_id);
     435            1 :         if (session_info == NULL) {
     436            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     437              :         }
     438              :         ((libspdm_secured_message_context_t
     439            1 :           *)(session_info->secured_message_context))
     440            1 :         ->handshake_secret.response_handshake_sequence_number--;
     441              :     }
     442            1 :         return LIBSPDM_STATUS_SUCCESS;
     443              : 
     444            2 :     case 0x8: {
     445              :         spdm_error_response_data_response_not_ready_t *spdm_response;
     446              :         size_t spdm_response_size;
     447              :         size_t transport_header_size;
     448              :         uint32_t session_id;
     449              :         libspdm_session_info_t *session_info;
     450              :         uint8_t *scratch_buffer;
     451              :         size_t scratch_buffer_size;
     452              : 
     453            2 :         spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     454            2 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     455            2 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     456              : 
     457            2 :         session_id = 0xFFFFFFFF;
     458            2 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     459            2 :         spdm_response->header.request_response_code = SPDM_ERROR;
     460            2 :         spdm_response->header.param1 =
     461              :             SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     462            2 :         spdm_response->header.param2 = 0;
     463            2 :         spdm_response->extend_error_data.rd_exponent = 1;
     464            2 :         spdm_response->extend_error_data.rd_tm = 2;
     465            2 :         spdm_response->extend_error_data.request_code = SPDM_PSK_FINISH;
     466            2 :         spdm_response->extend_error_data.token = 0;
     467              : 
     468              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     469              :          * transport_message is always in sender buffer. */
     470            2 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     471            2 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     472              :                           scratch_buffer_size - transport_header_size,
     473              :                           spdm_response, spdm_response_size);
     474            2 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     475            2 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     476              :                                               false, false,
     477              :                                               spdm_response_size,
     478              :                                               spdm_response,
     479              :                                               response_size, response);
     480            2 :         session_info = libspdm_get_session_info_via_session_id(
     481              :             spdm_context, session_id);
     482            2 :         if (session_info == NULL) {
     483            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     484              :         }
     485              :         ((libspdm_secured_message_context_t
     486            2 :           *)(session_info->secured_message_context))
     487            2 :         ->handshake_secret.response_handshake_sequence_number--;
     488              :     }
     489            2 :         return LIBSPDM_STATUS_SUCCESS;
     490              : 
     491            2 :     case 0x9: {
     492              :         static size_t sub_index2 = 0;
     493            2 :         if (sub_index2 == 0) {
     494              :             spdm_error_response_data_response_not_ready_t
     495              :             *spdm_response;
     496              :             size_t spdm_response_size;
     497              :             size_t transport_header_size;
     498              :             uint32_t session_id;
     499              :             libspdm_session_info_t *session_info;
     500              :             uint8_t *scratch_buffer;
     501              :             size_t scratch_buffer_size;
     502              : 
     503            1 :             spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
     504            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     505            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     506              : 
     507            1 :             session_id = 0xFFFFFFFF;
     508            1 :             spdm_response->header.spdm_version =
     509              :                 SPDM_MESSAGE_VERSION_11;
     510            1 :             spdm_response->header.request_response_code = SPDM_ERROR;
     511            1 :             spdm_response->header.param1 =
     512              :                 SPDM_ERROR_CODE_RESPONSE_NOT_READY;
     513            1 :             spdm_response->header.param2 = 0;
     514            1 :             spdm_response->extend_error_data.rd_exponent = 1;
     515            1 :             spdm_response->extend_error_data.rd_tm = 2;
     516            1 :             spdm_response->extend_error_data.request_code =
     517              :                 SPDM_PSK_FINISH;
     518            1 :             spdm_response->extend_error_data.token = 1;
     519              : 
     520              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     521              :              * transport_message is always in sender buffer. */
     522            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     523              :                                         &scratch_buffer_size);
     524            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     525              :                               scratch_buffer_size - transport_header_size,
     526              :                               spdm_response, spdm_response_size);
     527            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     528            1 :             libspdm_transport_test_encode_message(
     529              :                 spdm_context, &session_id, false, false,
     530              :                 spdm_response_size, spdm_response,
     531              :                 response_size, response);
     532            1 :             sub_index2++;
     533            1 :             session_info = libspdm_get_session_info_via_session_id(
     534              :                 spdm_context, session_id);
     535            1 :             if (session_info == NULL) {
     536            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     537              :             }
     538              :             ((libspdm_secured_message_context_t
     539            1 :               *)(session_info->secured_message_context))
     540              :             ->handshake_secret
     541            1 :             .response_handshake_sequence_number--;
     542            1 :         } else if (sub_index2 == 1) {
     543              :             spdm_psk_finish_response_t *spdm_response;
     544              :             size_t spdm_response_size;
     545              :             size_t transport_header_size;
     546              :             uint32_t session_id;
     547              :             libspdm_session_info_t *session_info;
     548              :             uint8_t *scratch_buffer;
     549              :             size_t scratch_buffer_size;
     550              : 
     551            1 :             session_id = 0xFFFFFFFF;
     552            1 :             spdm_response_size = sizeof(spdm_psk_finish_response_t);
     553            1 :             transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     554            1 :             spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     555              : 
     556            1 :             spdm_response->header.spdm_version =
     557              :                 SPDM_MESSAGE_VERSION_11;
     558            1 :             spdm_response->header.request_response_code =
     559              :                 SPDM_PSK_FINISH_RSP;
     560            1 :             spdm_response->header.param1 = 0;
     561            1 :             spdm_response->header.param2 = 0;
     562              : 
     563              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     564              :              * transport_message is always in sender buffer. */
     565            1 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     566              :                                         &scratch_buffer_size);
     567            1 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     568              :                               scratch_buffer_size - transport_header_size,
     569              :                               spdm_response, spdm_response_size);
     570            1 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     571            1 :             libspdm_transport_test_encode_message(
     572              :                 spdm_context, &session_id, false, false,
     573              :                 spdm_response_size, spdm_response, response_size,
     574              :                 response);
     575            1 :             session_info = libspdm_get_session_info_via_session_id(
     576              :                 spdm_context, session_id);
     577            1 :             if (session_info == NULL) {
     578            0 :                 return LIBSPDM_STATUS_RECEIVE_FAIL;
     579              :             }
     580              :             ((libspdm_secured_message_context_t
     581            1 :               *)(session_info->secured_message_context))
     582              :             ->handshake_secret
     583            1 :             .response_handshake_sequence_number--;
     584              :         }
     585              :     }
     586            2 :         return LIBSPDM_STATUS_SUCCESS;
     587              : 
     588           18 :     case 0xA:
     589              :     {
     590              :         static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
     591              : 
     592              :         spdm_error_response_t *spdm_response;
     593              :         size_t spdm_response_size;
     594              :         size_t transport_header_size;
     595              :         uint32_t session_id;
     596              :         libspdm_session_info_t      *session_info;
     597              :         uint8_t *scratch_buffer;
     598              :         size_t scratch_buffer_size;
     599              : 
     600           18 :         session_id = 0xFFFFFFFF;
     601              : 
     602           18 :         spdm_response_size = sizeof(spdm_error_response_t);
     603           18 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     604           18 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     605              : 
     606           18 :         if(error_code <= 0xff) {
     607           18 :             libspdm_zero_mem (spdm_response, spdm_response_size);
     608           18 :             spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     609           18 :             spdm_response->header.request_response_code = SPDM_ERROR;
     610           18 :             spdm_response->header.param1 = (uint8_t) error_code;
     611           18 :             spdm_response->header.param2 = 0;
     612              : 
     613              :             /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     614              :              * transport_message is always in sender buffer. */
     615           18 :             libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer,
     616              :                                         &scratch_buffer_size);
     617           18 :             libspdm_copy_mem (scratch_buffer + transport_header_size,
     618              :                               scratch_buffer_size - transport_header_size,
     619              :                               spdm_response, spdm_response_size);
     620           18 :             spdm_response = (void *)(scratch_buffer + transport_header_size);
     621           18 :             libspdm_transport_test_encode_message (spdm_context, &session_id, false, false,
     622              :                                                    spdm_response_size, spdm_response,
     623              :                                                    response_size, response);
     624           18 :             session_info = libspdm_get_session_info_via_session_id (spdm_context, session_id);
     625           18 :             ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
     626           18 :             handshake_secret.response_handshake_sequence_number--;
     627              :         }
     628              : 
     629           18 :         error_code++;
     630           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
     631            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
     632              :         }
     633           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
     634            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
     635              :         }
     636           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
     637            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
     638              :         }
     639              :     }
     640           18 :         return LIBSPDM_STATUS_SUCCESS;
     641            1 :     case 0xB: {
     642              :         spdm_psk_finish_response_t *spdm_response;
     643              :         size_t spdm_response_size;
     644              :         size_t transport_header_size;
     645              :         uint32_t session_id;
     646              :         libspdm_session_info_t *session_info;
     647              :         uint8_t *scratch_buffer;
     648              :         size_t scratch_buffer_size;
     649              : 
     650            1 :         session_id = 0xFFFFFFFF;
     651            1 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     652            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     653            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     654              : 
     655            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     656            1 :         spdm_response->header.request_response_code =
     657              :             SPDM_PSK_FINISH_RSP;
     658            1 :         spdm_response->header.param1 = 0;
     659            1 :         spdm_response->header.param2 = 0;
     660              : 
     661              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     662              :          * transport_message is always in sender buffer. */
     663            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     664            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     665              :                           scratch_buffer_size - transport_header_size,
     666              :                           spdm_response, spdm_response_size);
     667            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     668            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     669              :                                               false, false, spdm_response_size,
     670              :                                               spdm_response, response_size,
     671              :                                               response);
     672            1 :         session_info = libspdm_get_session_info_via_session_id(
     673              :             spdm_context, session_id);
     674            1 :         if (session_info == NULL) {
     675            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     676              :         }
     677              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     678              :         ((libspdm_secured_message_context_t
     679            1 :           *)(session_info->secured_message_context))
     680            1 :         ->handshake_secret.response_handshake_sequence_number--;
     681              :     }
     682            1 :         return LIBSPDM_STATUS_SUCCESS;
     683              : 
     684            0 :     case 0xC: {
     685              :         spdm_psk_finish_response_t *spdm_response;
     686              :         size_t spdm_response_size;
     687              :         size_t transport_header_size;
     688              :         uint32_t session_id;
     689              :         libspdm_session_info_t *session_info;
     690              :         uint8_t *scratch_buffer;
     691              :         size_t scratch_buffer_size;
     692              : 
     693            0 :         session_id = 0xFFFFFFFF;
     694            0 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     695            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     696            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     697              : 
     698            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     699            0 :         spdm_response->header.request_response_code =
     700              :             SPDM_PSK_FINISH_RSP;
     701            0 :         spdm_response->header.param1 = 0;
     702            0 :         spdm_response->header.param2 = 0;
     703              : 
     704              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     705              :          * transport_message is always in sender buffer. */
     706            0 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     707            0 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     708              :                           scratch_buffer_size - transport_header_size,
     709              :                           spdm_response, spdm_response_size);
     710            0 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     711            0 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     712              :                                               false, false, spdm_response_size,
     713              :                                               spdm_response, response_size,
     714              :                                               response);
     715            0 :         session_info = libspdm_get_session_info_via_session_id(
     716              :             spdm_context, session_id);
     717            0 :         if (session_info == NULL) {
     718            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     719              :         }
     720              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     721              :         ((libspdm_secured_message_context_t
     722            0 :           *)(session_info->secured_message_context))
     723            0 :         ->handshake_secret.response_handshake_sequence_number--;
     724              :     }
     725            0 :         return LIBSPDM_STATUS_SUCCESS;
     726              : 
     727            1 :     case 0xD: {
     728              :         spdm_psk_finish_response_t *spdm_response;
     729              :         size_t spdm_response_size;
     730              :         size_t transport_header_size;
     731              :         uint32_t session_id;
     732              :         libspdm_session_info_t *session_info;
     733              :         uint8_t *scratch_buffer;
     734              :         size_t scratch_buffer_size;
     735              : 
     736            1 :         session_id = 0xFFFFFFFF;
     737            1 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     738            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     739            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     740              : 
     741            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     742            1 :         spdm_response->header.request_response_code =
     743              :             SPDM_FINISH_RSP; /*wrong response code*/
     744            1 :         spdm_response->header.param1 = 0;
     745            1 :         spdm_response->header.param2 = 0;
     746              : 
     747              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     748              :          * transport_message is always in sender buffer. */
     749            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     750            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     751              :                           scratch_buffer_size - transport_header_size,
     752              :                           spdm_response, spdm_response_size);
     753            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     754            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     755              :                                               false, false, spdm_response_size,
     756              :                                               spdm_response, response_size,
     757              :                                               response);
     758            1 :         session_info = libspdm_get_session_info_via_session_id(
     759              :             spdm_context, session_id);
     760            1 :         if (session_info == NULL) {
     761            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     762              :         }
     763              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     764              :         ((libspdm_secured_message_context_t
     765            1 :           *)(session_info->secured_message_context))
     766            1 :         ->handshake_secret.response_handshake_sequence_number--;
     767              :     }
     768            1 :         return LIBSPDM_STATUS_SUCCESS;
     769              : 
     770            0 :     case 0xE: {
     771              :         spdm_psk_finish_response_t *spdm_response;
     772              :         size_t spdm_response_size;
     773              :         size_t transport_header_size;
     774              :         uint32_t session_id;
     775              :         libspdm_session_info_t *session_info;
     776              :         uint8_t *scratch_buffer;
     777              :         size_t scratch_buffer_size;
     778              : 
     779            0 :         session_id = 0xFFFFFFFF;
     780            0 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     781            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     782            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     783              : 
     784            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     785            0 :         spdm_response->header.request_response_code =
     786              :             SPDM_PSK_FINISH_RSP;
     787            0 :         spdm_response->header.param1 = 0;
     788            0 :         spdm_response->header.param2 = 0;
     789              : 
     790              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     791              :          * transport_message is always in sender buffer. */
     792            0 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     793            0 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     794              :                           scratch_buffer_size - transport_header_size,
     795              :                           spdm_response, spdm_response_size);
     796            0 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     797            0 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     798              :                                               false, false, spdm_response_size,
     799              :                                               spdm_response, response_size,
     800              :                                               response);
     801            0 :         session_info = libspdm_get_session_info_via_session_id(
     802              :             spdm_context, session_id);
     803            0 :         if (session_info == NULL) {
     804            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     805              :         }
     806              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     807              :         ((libspdm_secured_message_context_t
     808            0 :           *)(session_info->secured_message_context))
     809            0 :         ->handshake_secret.response_handshake_sequence_number--;
     810              :     }
     811            0 :         return LIBSPDM_STATUS_SUCCESS;
     812              : 
     813            1 :     case 0xF: {
     814              :         spdm_error_response_t *spdm_response;
     815              :         size_t spdm_response_size;
     816              :         size_t transport_header_size;
     817              :         uint32_t session_id;
     818              :         libspdm_session_info_t *session_info;
     819              :         uint8_t *scratch_buffer;
     820              :         size_t scratch_buffer_size;
     821              : 
     822            1 :         session_id = 0xFFFFFFFF;
     823              : 
     824            1 :         spdm_response_size = sizeof(spdm_error_response_t);
     825            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     826            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     827              : 
     828            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     829            1 :         spdm_response->header.request_response_code = SPDM_ERROR;
     830            1 :         spdm_response->header.param1 = SPDM_ERROR_CODE_DECRYPT_ERROR;
     831            1 :         spdm_response->header.param2 = 0;
     832              : 
     833              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     834              :          * transport_message is always in sender buffer. */
     835            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     836            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     837              :                           scratch_buffer_size - transport_header_size,
     838              :                           spdm_response, spdm_response_size);
     839            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     840            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     841              :                                               false, false,
     842              :                                               spdm_response_size,
     843              :                                               spdm_response,
     844              :                                               response_size, response);
     845            1 :         session_info = libspdm_get_session_info_via_session_id(
     846              :             spdm_context, session_id);
     847            1 :         if (session_info == NULL) {
     848            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     849              :         }
     850              :         ((libspdm_secured_message_context_t
     851            1 :           *)(session_info->secured_message_context))
     852            1 :         ->handshake_secret.response_handshake_sequence_number--;
     853              :     }
     854            1 :         return LIBSPDM_STATUS_SUCCESS;
     855              : 
     856            1 :     case 0x10: {
     857              :         spdm_psk_finish_response_t *spdm_response;
     858              :         size_t spdm_response_size;
     859              :         size_t transport_header_size;
     860              :         uint32_t session_id;
     861              :         libspdm_session_info_t *session_info;
     862              :         uint8_t *scratch_buffer;
     863              :         size_t scratch_buffer_size;
     864              : 
     865            1 :         session_id = 0xFFFFFFFF;
     866            1 :         spdm_response_size = sizeof(spdm_psk_finish_response_t);
     867            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     868            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     869              : 
     870            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
     871            1 :         spdm_response->header.request_response_code =
     872              :             SPDM_PSK_FINISH_RSP;
     873            1 :         spdm_response->header.param1 = 0;
     874            1 :         spdm_response->header.param2 = 0;
     875              : 
     876              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     877              :          * transport_message is always in sender buffer. */
     878            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     879            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     880              :                           scratch_buffer_size - transport_header_size,
     881              :                           spdm_response, spdm_response_size);
     882            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     883              : 
     884            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     885              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
     886              :                          spdm_response, spdm_response_size);
     887            1 :         m_libspdm_local_buffer_size += spdm_response_size;
     888              : 
     889            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     890              :                                               false, false, spdm_response_size,
     891              :                                               spdm_response, response_size,
     892              :                                               response);
     893            1 :         session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
     894            1 :         if (session_info == NULL) {
     895            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     896              :         }
     897              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     898            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
     899            1 :         ->handshake_secret.response_handshake_sequence_number--;
     900              :     }
     901            1 :         return LIBSPDM_STATUS_SUCCESS;
     902              : 
     903            1 :     case 0x11: {
     904              :         spdm_psk_finish_response_t *spdm_response;
     905              :         size_t spdm_response_size;
     906              :         size_t transport_header_size;
     907              :         uint32_t session_id;
     908              :         libspdm_session_info_t *session_info;
     909              :         uint8_t *scratch_buffer;
     910              :         size_t scratch_buffer_size;
     911              :         uint16_t opaque_data_size;
     912              :         uint8_t *ptr;
     913              : 
     914            1 :         session_id = 0xFFFFFFFF;
     915            1 :         opaque_data_size = 8;
     916            1 :         spdm_response_size = sizeof(spdm_psk_finish_response_t) + sizeof(uint16_t) + opaque_data_size;
     917            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     918            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     919              : 
     920            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_14;
     921            1 :         spdm_response->header.request_response_code =
     922              :             SPDM_PSK_FINISH_RSP;
     923            1 :         spdm_response->header.param1 = 0;
     924            1 :         spdm_response->header.param2 = 0;
     925            1 :         ptr = (uint8_t *)spdm_response + sizeof(spdm_psk_finish_response_t);
     926            1 :         libspdm_write_uint16(ptr, opaque_data_size);
     927              : 
     928              :         /* For secure message, message is in sender buffer, we need copy it to scratch buffer.
     929              :          * transport_message is always in sender buffer. */
     930            1 :         libspdm_get_scratch_buffer (spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     931            1 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
     932              :                           scratch_buffer_size - transport_header_size,
     933              :                           spdm_response, spdm_response_size);
     934            1 :         spdm_response = (void *)(scratch_buffer + transport_header_size);
     935              : 
     936            1 :         libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
     937              :                          sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
     938              :                          spdm_response, spdm_response_size);
     939            1 :         m_libspdm_local_buffer_size += spdm_response_size;
     940              : 
     941            1 :         libspdm_transport_test_encode_message(spdm_context, &session_id,
     942              :                                               false, false, spdm_response_size,
     943              :                                               spdm_response, response_size,
     944              :                                               response);
     945            1 :         session_info = libspdm_get_session_info_via_session_id(spdm_context, session_id);
     946            1 :         if (session_info == NULL) {
     947            0 :             return LIBSPDM_STATUS_RECEIVE_FAIL;
     948              :         }
     949              :         /* WALKAROUND: If just use single context to encode message and then decode message */
     950            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
     951            1 :         ->handshake_secret.response_handshake_sequence_number--;
     952              :     }
     953            1 :         return LIBSPDM_STATUS_SUCCESS;
     954              : 
     955            0 :     default:
     956            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     957              :     }
     958              : }
     959              : 
     960              : /**
     961              :  * Test 1: when no PSK_FINISH_RSP message is received, and the client returns
     962              :  * a device error.
     963              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
     964              :  **/
     965            1 : static void req_psk_finish_case1(void **state)
     966              : {
     967              :     libspdm_return_t status;
     968              :     libspdm_test_context_t *spdm_test_context;
     969              :     libspdm_context_t *spdm_context;
     970              :     uint32_t session_id;
     971              :     void *data;
     972              :     size_t data_size;
     973              :     void *hash;
     974              :     size_t hash_size;
     975              :     libspdm_session_info_t *session_info;
     976              : 
     977            1 :     spdm_test_context = *state;
     978            1 :     spdm_context = spdm_test_context->spdm_context;
     979            1 :     spdm_test_context->case_id = 0x1;
     980            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
     981              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     982            1 :     spdm_context->connection_info.connection_state =
     983              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     984            1 :     spdm_context->connection_info.capability.flags |=
     985              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
     986            1 :     spdm_context->connection_info.capability.flags |=
     987              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
     988            1 :     spdm_context->connection_info.capability.flags |=
     989              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
     990            1 :     spdm_context->local_context.capability.flags |=
     991              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
     992            1 :     spdm_context->local_context.capability.flags |=
     993              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
     994            1 :     spdm_context->local_context.capability.flags |=
     995              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
     996            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
     997              :                                                          m_libspdm_use_asym_algo, &data,
     998              :                                                          &data_size, &hash, &hash_size)) {
     999            0 :         assert(false);
    1000              :     }
    1001            1 :     libspdm_reset_message_a(spdm_context);
    1002            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1003              :         m_libspdm_use_hash_algo;
    1004            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1005              :         m_libspdm_use_asym_algo;
    1006            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1007              :         m_libspdm_use_dhe_algo;
    1008            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1009              :         m_libspdm_use_aead_algo;
    1010              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1011              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1012              :         data_size;
    1013              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1014              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1015              :                      data, data_size);
    1016              : #endif
    1017              : 
    1018            1 :     session_id = 0xFFFFFFFF;
    1019            1 :     session_info = &spdm_context->session_info[0];
    1020            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1021              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1022            1 :     libspdm_session_info_set_psk_hint(session_info,
    1023              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1024              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1025            1 :     libspdm_secured_message_set_session_state(
    1026              :         session_info->secured_message_context,
    1027              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1028            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1029              : 
    1030            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1031            1 :     assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
    1032            1 :     free(data);
    1033            1 : }
    1034              : 
    1035              : /**
    1036              :  * Test 2: receiving a correct PSK_FINISH_RSP message.
    1037              :  * Expected behavior: client returns a Status of LIBSPDM_STATUS_SUCCESS and
    1038              :  * session is established.
    1039              :  **/
    1040            1 : static void req_psk_finish_case2(void **state)
    1041              : {
    1042              :     libspdm_return_t status;
    1043              :     libspdm_test_context_t *spdm_test_context;
    1044              :     libspdm_context_t *spdm_context;
    1045              :     uint32_t session_id;
    1046              :     void *data;
    1047              :     size_t data_size;
    1048              :     void *hash;
    1049              :     size_t hash_size;
    1050              :     libspdm_session_info_t *session_info;
    1051              : 
    1052            1 :     spdm_test_context = *state;
    1053            1 :     spdm_context = spdm_test_context->spdm_context;
    1054            1 :     spdm_test_context->case_id = 0x2;
    1055            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1056              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1057            1 :     spdm_context->connection_info.connection_state =
    1058              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1059            1 :     spdm_context->connection_info.capability.flags |=
    1060              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1061            1 :     spdm_context->connection_info.capability.flags |=
    1062              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1063            1 :     spdm_context->connection_info.capability.flags |=
    1064              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1065            1 :     spdm_context->local_context.capability.flags |=
    1066              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1067            1 :     spdm_context->local_context.capability.flags |=
    1068              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1069            1 :     spdm_context->local_context.capability.flags |=
    1070              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1071            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1072              :                                                          m_libspdm_use_asym_algo, &data,
    1073              :                                                          &data_size, &hash, &hash_size)) {
    1074            0 :         assert(false);
    1075              :     }
    1076            1 :     libspdm_reset_message_a(spdm_context);
    1077            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1078              :         m_libspdm_use_hash_algo;
    1079            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1080              :         m_libspdm_use_asym_algo;
    1081            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1082              :         m_libspdm_use_dhe_algo;
    1083            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1084              :         m_libspdm_use_aead_algo;
    1085              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1086              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1087              :         data_size;
    1088              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1089              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1090              :                      data, data_size);
    1091              : #endif
    1092              : 
    1093            1 :     session_id = 0xFFFFFFFF;
    1094            1 :     session_info = &spdm_context->session_info[0];
    1095            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1096              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1097            1 :     libspdm_session_info_set_psk_hint(session_info,
    1098              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1099              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1100            1 :     libspdm_secured_message_set_session_state(
    1101              :         session_info->secured_message_context,
    1102              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1103            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1104              :                     ((libspdm_secured_message_context_t
    1105            1 :                       *)(session_info->secured_message_context))
    1106              :                     ->aead_key_size,
    1107              :                     (uint8_t)(0xFF));
    1108            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1109              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1110              :         ((libspdm_secured_message_context_t
    1111            1 :           *)(session_info->secured_message_context))
    1112              :         ->aead_key_size);
    1113            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1114              :                     ((libspdm_secured_message_context_t
    1115            1 :                       *)(session_info->secured_message_context))
    1116              :                     ->aead_iv_size,
    1117              :                     (uint8_t)(0xFF));
    1118            1 :     libspdm_secured_message_set_response_handshake_salt(
    1119              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1120              :         ((libspdm_secured_message_context_t
    1121            1 :           *)(session_info->secured_message_context))
    1122              :         ->aead_iv_size);
    1123              :     ((libspdm_secured_message_context_t *)(session_info
    1124            1 :                                            ->secured_message_context))
    1125            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1126            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1127              : 
    1128            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1129            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1130            1 :     assert_int_equal(
    1131              :         libspdm_secured_message_get_session_state(
    1132              :             spdm_context->session_info[0].secured_message_context),
    1133              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1134            1 :     free(data);
    1135            1 : }
    1136              : 
    1137              : /**
    1138              :  * Test 3: requester state has not been negotiated, as if GET_VERSION,
    1139              :  * GET_CAPABILITIES and NEGOTIATE_ALGORITHMS had not been exchanged.
    1140              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    1141              :  **/
    1142            1 : static void req_psk_finish_case3(void **state)
    1143              : {
    1144              :     libspdm_return_t status;
    1145              :     libspdm_test_context_t *spdm_test_context;
    1146              :     libspdm_context_t *spdm_context;
    1147              :     uint32_t session_id;
    1148              :     void *data;
    1149              :     size_t data_size;
    1150              :     void *hash;
    1151              :     size_t hash_size;
    1152              :     libspdm_session_info_t *session_info;
    1153              : 
    1154            1 :     spdm_test_context = *state;
    1155            1 :     spdm_context = spdm_test_context->spdm_context;
    1156            1 :     spdm_test_context->case_id = 0x3;
    1157            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1158              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1159            1 :     spdm_context->connection_info.connection_state =
    1160              :         LIBSPDM_CONNECTION_STATE_NOT_STARTED;
    1161            1 :     spdm_context->connection_info.capability.flags |=
    1162              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1163            1 :     spdm_context->connection_info.capability.flags |=
    1164              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1165            1 :     spdm_context->connection_info.capability.flags |=
    1166              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1167            1 :     spdm_context->local_context.capability.flags |=
    1168              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1169            1 :     spdm_context->local_context.capability.flags |=
    1170              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1171            1 :     spdm_context->local_context.capability.flags |=
    1172              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1173            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1174              :                                                          m_libspdm_use_asym_algo, &data,
    1175              :                                                          &data_size, &hash, &hash_size)) {
    1176            0 :         assert(false);
    1177              :     }
    1178            1 :     libspdm_reset_message_a(spdm_context);
    1179            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1180              :         m_libspdm_use_hash_algo;
    1181            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1182              :         m_libspdm_use_asym_algo;
    1183            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1184              :         m_libspdm_use_dhe_algo;
    1185            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1186              :         m_libspdm_use_aead_algo;
    1187              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1188              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1189              :         data_size;
    1190              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1191              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1192              :                      data, data_size);
    1193              : #endif
    1194              : 
    1195            1 :     session_id = 0xFFFFFFFF;
    1196            1 :     session_info = &spdm_context->session_info[0];
    1197            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1198              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1199            1 :     libspdm_session_info_set_psk_hint(session_info,
    1200              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1201              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1202            1 :     libspdm_secured_message_set_session_state(
    1203              :         session_info->secured_message_context,
    1204              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1205            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1206              :                     ((libspdm_secured_message_context_t
    1207            1 :                       *)(session_info->secured_message_context))
    1208              :                     ->aead_key_size,
    1209              :                     (uint8_t)(0xFF));
    1210            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1211              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1212              :         ((libspdm_secured_message_context_t
    1213            1 :           *)(session_info->secured_message_context))
    1214              :         ->aead_key_size);
    1215            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1216              :                     ((libspdm_secured_message_context_t
    1217            1 :                       *)(session_info->secured_message_context))
    1218              :                     ->aead_iv_size,
    1219              :                     (uint8_t)(0xFF));
    1220            1 :     libspdm_secured_message_set_response_handshake_salt(
    1221              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1222              :         ((libspdm_secured_message_context_t
    1223            1 :           *)(session_info->secured_message_context))
    1224              :         ->aead_iv_size);
    1225              :     ((libspdm_secured_message_context_t *)(session_info
    1226            1 :                                            ->secured_message_context))
    1227            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1228            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1229              : 
    1230            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1231            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    1232            1 :     free(data);
    1233            1 : }
    1234              : 
    1235              : /**
    1236              :  * Test 4: the requester is setup correctly, but receives an ERROR message
    1237              :  * indicating InvalidParameters.
    1238              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1239              :  **/
    1240            1 : static void req_psk_finish_case4(void **state)
    1241              : {
    1242              :     libspdm_return_t status;
    1243              :     libspdm_test_context_t *spdm_test_context;
    1244              :     libspdm_context_t *spdm_context;
    1245              :     uint32_t session_id;
    1246              :     void *data;
    1247              :     size_t data_size;
    1248              :     void *hash;
    1249              :     size_t hash_size;
    1250              :     libspdm_session_info_t *session_info;
    1251              : 
    1252            1 :     spdm_test_context = *state;
    1253            1 :     spdm_context = spdm_test_context->spdm_context;
    1254            1 :     spdm_test_context->case_id = 0x4;
    1255            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1256              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1257            1 :     spdm_context->connection_info.connection_state =
    1258              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1259            1 :     spdm_context->connection_info.capability.flags |=
    1260              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1261            1 :     spdm_context->connection_info.capability.flags |=
    1262              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1263            1 :     spdm_context->connection_info.capability.flags |=
    1264              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1265            1 :     spdm_context->local_context.capability.flags |=
    1266              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1267            1 :     spdm_context->local_context.capability.flags |=
    1268              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1269            1 :     spdm_context->local_context.capability.flags |=
    1270              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1271            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1272              :                                                          m_libspdm_use_asym_algo, &data,
    1273              :                                                          &data_size, &hash, &hash_size)) {
    1274            0 :         assert(false);
    1275              :     }
    1276            1 :     libspdm_reset_message_a(spdm_context);
    1277            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1278              :         m_libspdm_use_hash_algo;
    1279            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1280              :         m_libspdm_use_asym_algo;
    1281            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1282              :         m_libspdm_use_dhe_algo;
    1283            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1284              :         m_libspdm_use_aead_algo;
    1285              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1286              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1287              :         data_size;
    1288              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1289              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1290              :                      data, data_size);
    1291              : #endif
    1292              : 
    1293            1 :     session_id = 0xFFFFFFFF;
    1294            1 :     session_info = &spdm_context->session_info[0];
    1295            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1296              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1297            1 :     libspdm_session_info_set_psk_hint(session_info,
    1298              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1299              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1300            1 :     libspdm_secured_message_set_session_state(
    1301              :         session_info->secured_message_context,
    1302              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1303            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1304              :                     ((libspdm_secured_message_context_t
    1305            1 :                       *)(session_info->secured_message_context))
    1306              :                     ->aead_key_size,
    1307              :                     (uint8_t)(0xFF));
    1308            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1309              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1310              :         ((libspdm_secured_message_context_t
    1311            1 :           *)(session_info->secured_message_context))
    1312              :         ->aead_key_size);
    1313            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1314              :                     ((libspdm_secured_message_context_t
    1315            1 :                       *)(session_info->secured_message_context))
    1316              :                     ->aead_iv_size,
    1317              :                     (uint8_t)(0xFF));
    1318            1 :     libspdm_secured_message_set_response_handshake_salt(
    1319              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1320              :         ((libspdm_secured_message_context_t
    1321            1 :           *)(session_info->secured_message_context))
    1322              :         ->aead_iv_size);
    1323              :     ((libspdm_secured_message_context_t *)(session_info
    1324            1 :                                            ->secured_message_context))
    1325            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1326            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1327              : 
    1328            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1329            1 :     assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
    1330            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    1331            1 :     free(data);
    1332            1 : }
    1333              : 
    1334              : /**
    1335              :  * Test 5: the requester is setup correctly, but receives an ERROR message
    1336              :  * indicating the Busy status of the responder.
    1337              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1338              :  **/
    1339            1 : static void req_psk_finish_case5(void **state)
    1340              : {
    1341              :     libspdm_return_t status;
    1342              :     libspdm_test_context_t *spdm_test_context;
    1343              :     libspdm_context_t *spdm_context;
    1344              :     uint32_t session_id;
    1345              :     void *data;
    1346              :     size_t data_size;
    1347              :     void *hash;
    1348              :     size_t hash_size;
    1349              :     libspdm_session_info_t *session_info;
    1350              : 
    1351            1 :     spdm_test_context = *state;
    1352            1 :     spdm_context = spdm_test_context->spdm_context;
    1353            1 :     spdm_test_context->case_id = 0x5;
    1354            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1355              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1356            1 :     spdm_context->connection_info.connection_state =
    1357              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1358            1 :     spdm_context->connection_info.capability.flags |=
    1359              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1360            1 :     spdm_context->connection_info.capability.flags |=
    1361              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1362            1 :     spdm_context->connection_info.capability.flags |=
    1363              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1364            1 :     spdm_context->local_context.capability.flags |=
    1365              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1366            1 :     spdm_context->local_context.capability.flags |=
    1367              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1368            1 :     spdm_context->local_context.capability.flags |=
    1369              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1370            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1371              :                                                          m_libspdm_use_asym_algo, &data,
    1372              :                                                          &data_size, &hash, &hash_size)) {
    1373            0 :         assert(false);
    1374              :     }
    1375            1 :     libspdm_reset_message_a(spdm_context);
    1376            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1377              :         m_libspdm_use_hash_algo;
    1378            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1379              :         m_libspdm_use_asym_algo;
    1380            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1381              :         m_libspdm_use_dhe_algo;
    1382            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1383              :         m_libspdm_use_aead_algo;
    1384              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1385              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1386              :         data_size;
    1387              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1388              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1389              :                      data, data_size);
    1390              : #endif
    1391              : 
    1392            1 :     session_id = 0xFFFFFFFF;
    1393            1 :     session_info = &spdm_context->session_info[0];
    1394            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1395              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1396            1 :     libspdm_session_info_set_psk_hint(session_info,
    1397              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1398              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1399            1 :     libspdm_secured_message_set_session_state(
    1400              :         session_info->secured_message_context,
    1401              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1402            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1403              :                     ((libspdm_secured_message_context_t
    1404            1 :                       *)(session_info->secured_message_context))
    1405              :                     ->aead_key_size,
    1406              :                     (uint8_t)(0xFF));
    1407            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1408              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1409              :         ((libspdm_secured_message_context_t
    1410            1 :           *)(session_info->secured_message_context))
    1411              :         ->aead_key_size);
    1412            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1413              :                     ((libspdm_secured_message_context_t
    1414            1 :                       *)(session_info->secured_message_context))
    1415              :                     ->aead_iv_size,
    1416              :                     (uint8_t)(0xFF));
    1417            1 :     libspdm_secured_message_set_response_handshake_salt(
    1418              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1419              :         ((libspdm_secured_message_context_t
    1420            1 :           *)(session_info->secured_message_context))
    1421              :         ->aead_iv_size);
    1422              :     ((libspdm_secured_message_context_t *)(session_info
    1423            1 :                                            ->secured_message_context))
    1424            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1425            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1426              : 
    1427            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1428            1 :     assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
    1429            1 :     free(data);
    1430            1 : }
    1431              : 
    1432              : /**
    1433              :  * Test 6: the requester is setup correctly, but, on the first try, receiving
    1434              :  * a Busy ERROR message, and, on retry, receiving a correct PSK_FINISH_RSP
    1435              :  * message.
    1436              :  * Expected behavior: client returns a Status of LIBSPDM_STATUS_SUCCESS and session
    1437              :  * is established.
    1438              :  **/
    1439            1 : static void req_psk_finish_case6(void **state)
    1440              : {
    1441              :     libspdm_return_t status;
    1442              :     libspdm_test_context_t *spdm_test_context;
    1443              :     libspdm_context_t *spdm_context;
    1444              :     uint32_t session_id;
    1445              :     void *data;
    1446              :     size_t data_size;
    1447              :     void *hash;
    1448              :     size_t hash_size;
    1449              :     libspdm_session_info_t *session_info;
    1450              : 
    1451            1 :     spdm_test_context = *state;
    1452            1 :     spdm_context = spdm_test_context->spdm_context;
    1453            1 :     spdm_test_context->case_id = 0x6;
    1454            1 :     spdm_context->retry_times = 3;
    1455            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1456              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1457            1 :     spdm_context->connection_info.connection_state =
    1458              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1459            1 :     spdm_context->connection_info.capability.flags |=
    1460              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1461            1 :     spdm_context->connection_info.capability.flags |=
    1462              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1463            1 :     spdm_context->connection_info.capability.flags |=
    1464              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1465            1 :     spdm_context->local_context.capability.flags |=
    1466              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1467            1 :     spdm_context->local_context.capability.flags |=
    1468              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1469            1 :     spdm_context->local_context.capability.flags |=
    1470              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1471            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1472              :                                                          m_libspdm_use_asym_algo, &data,
    1473              :                                                          &data_size, &hash, &hash_size)) {
    1474            0 :         assert(false);
    1475              :     }
    1476            1 :     libspdm_reset_message_a(spdm_context);
    1477            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1478              :         m_libspdm_use_hash_algo;
    1479            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1480              :         m_libspdm_use_asym_algo;
    1481            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1482              :         m_libspdm_use_dhe_algo;
    1483            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1484              :         m_libspdm_use_aead_algo;
    1485              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1486              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1487              :         data_size;
    1488              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1489              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1490              :                      data, data_size);
    1491              : #endif
    1492              : 
    1493            1 :     session_id = 0xFFFFFFFF;
    1494            1 :     session_info = &spdm_context->session_info[0];
    1495            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1496              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1497            1 :     libspdm_session_info_set_psk_hint(session_info,
    1498              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1499              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1500            1 :     libspdm_secured_message_set_session_state(
    1501              :         session_info->secured_message_context,
    1502              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1503            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1504              :                     ((libspdm_secured_message_context_t
    1505            1 :                       *)(session_info->secured_message_context))
    1506              :                     ->aead_key_size,
    1507              :                     (uint8_t)(0xFF));
    1508            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1509              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1510              :         ((libspdm_secured_message_context_t
    1511            1 :           *)(session_info->secured_message_context))
    1512              :         ->aead_key_size);
    1513            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1514              :                     ((libspdm_secured_message_context_t
    1515            1 :                       *)(session_info->secured_message_context))
    1516              :                     ->aead_iv_size,
    1517              :                     (uint8_t)(0xFF));
    1518            1 :     libspdm_secured_message_set_response_handshake_salt(
    1519              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1520              :         ((libspdm_secured_message_context_t
    1521            1 :           *)(session_info->secured_message_context))
    1522              :         ->aead_iv_size);
    1523              :     ((libspdm_secured_message_context_t *)(session_info
    1524            1 :                                            ->secured_message_context))
    1525            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1526            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1527              : 
    1528            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1529            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1530            1 :     assert_int_equal(
    1531              :         libspdm_secured_message_get_session_state(
    1532              :             spdm_context->session_info[0].secured_message_context),
    1533              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    1534            1 :     free(data);
    1535            1 : }
    1536              : 
    1537              : /**
    1538              :  * Test 7: the requester is setup correctly, but receives an ERROR message
    1539              :  * indicating the RequestResynch status of the responder.
    1540              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR, and the
    1541              :  * communication is reset to expect a new GET_VERSION message.
    1542              :  **/
    1543            1 : static void req_psk_finish_case7(void **state)
    1544              : {
    1545              :     libspdm_return_t status;
    1546              :     libspdm_test_context_t *spdm_test_context;
    1547              :     libspdm_context_t *spdm_context;
    1548              :     uint32_t session_id;
    1549              :     void *data;
    1550              :     size_t data_size;
    1551              :     void *hash;
    1552              :     size_t hash_size;
    1553              :     libspdm_session_info_t *session_info;
    1554              : 
    1555            1 :     spdm_test_context = *state;
    1556            1 :     spdm_context = spdm_test_context->spdm_context;
    1557            1 :     spdm_test_context->case_id = 0x7;
    1558            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1559              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1560            1 :     spdm_context->connection_info.connection_state =
    1561              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1562            1 :     spdm_context->connection_info.capability.flags |=
    1563              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1564            1 :     spdm_context->connection_info.capability.flags |=
    1565              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1566            1 :     spdm_context->connection_info.capability.flags |=
    1567              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1568            1 :     spdm_context->local_context.capability.flags |=
    1569              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1570            1 :     spdm_context->local_context.capability.flags |=
    1571              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1572            1 :     spdm_context->local_context.capability.flags |=
    1573              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1574            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1575              :                                                          m_libspdm_use_asym_algo, &data,
    1576              :                                                          &data_size, &hash, &hash_size)) {
    1577            0 :         assert(false);
    1578              :     }
    1579            1 :     libspdm_reset_message_a(spdm_context);
    1580            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1581              :         m_libspdm_use_hash_algo;
    1582            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1583              :         m_libspdm_use_asym_algo;
    1584            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1585              :         m_libspdm_use_dhe_algo;
    1586            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1587              :         m_libspdm_use_aead_algo;
    1588              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1589              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1590              :         data_size;
    1591              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1592              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1593              :                      data, data_size);
    1594              : #endif
    1595              : 
    1596            1 :     session_id = 0xFFFFFFFF;
    1597            1 :     session_info = &spdm_context->session_info[0];
    1598            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1599              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1600            1 :     libspdm_session_info_set_psk_hint(session_info,
    1601              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1602              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1603            1 :     libspdm_secured_message_set_session_state(
    1604              :         session_info->secured_message_context,
    1605              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1606            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1607              :                     ((libspdm_secured_message_context_t
    1608            1 :                       *)(session_info->secured_message_context))
    1609              :                     ->aead_key_size,
    1610              :                     (uint8_t)(0xFF));
    1611            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1612              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1613              :         ((libspdm_secured_message_context_t
    1614            1 :           *)(session_info->secured_message_context))
    1615              :         ->aead_key_size);
    1616            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1617              :                     ((libspdm_secured_message_context_t
    1618            1 :                       *)(session_info->secured_message_context))
    1619              :                     ->aead_iv_size,
    1620              :                     (uint8_t)(0xFF));
    1621            1 :     libspdm_secured_message_set_response_handshake_salt(
    1622              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1623              :         ((libspdm_secured_message_context_t
    1624            1 :           *)(session_info->secured_message_context))
    1625              :         ->aead_iv_size);
    1626              :     ((libspdm_secured_message_context_t *)(session_info
    1627            1 :                                            ->secured_message_context))
    1628            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1629            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1630              : 
    1631            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1632            1 :     assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
    1633            1 :     assert_int_equal(spdm_context->connection_info.connection_state,
    1634              :                      LIBSPDM_CONNECTION_STATE_NOT_STARTED);
    1635            1 :     free(data);
    1636            1 : }
    1637              : 
    1638              : /**
    1639              :  * Test 8: the requester is setup correctly, but receives an ERROR message
    1640              :  * indicating the ResponseNotReady status of the responder.
    1641              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    1642              :  **/
    1643            1 : static void req_psk_finish_case8(void **state)
    1644              : {
    1645              :     libspdm_return_t status;
    1646              :     libspdm_test_context_t *spdm_test_context;
    1647              :     libspdm_context_t *spdm_context;
    1648              :     uint32_t session_id;
    1649              :     void *data;
    1650              :     size_t data_size;
    1651              :     void *hash;
    1652              :     size_t hash_size;
    1653              :     libspdm_session_info_t *session_info;
    1654              : 
    1655            1 :     spdm_test_context = *state;
    1656            1 :     spdm_context = spdm_test_context->spdm_context;
    1657            1 :     spdm_test_context->case_id = 0x8;
    1658            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1659              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1660            1 :     spdm_context->connection_info.connection_state =
    1661              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1662            1 :     spdm_context->connection_info.capability.flags |=
    1663              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1664            1 :     spdm_context->connection_info.capability.flags |=
    1665              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1666            1 :     spdm_context->connection_info.capability.flags |=
    1667              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1668            1 :     spdm_context->local_context.capability.flags |=
    1669              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1670            1 :     spdm_context->local_context.capability.flags |=
    1671              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1672            1 :     spdm_context->local_context.capability.flags |=
    1673              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1674            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1675              :                                                          m_libspdm_use_asym_algo, &data,
    1676              :                                                          &data_size, &hash, &hash_size)) {
    1677            0 :         assert(false);
    1678              :     }
    1679            1 :     libspdm_reset_message_a(spdm_context);
    1680            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1681              :         m_libspdm_use_hash_algo;
    1682            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1683              :         m_libspdm_use_asym_algo;
    1684            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1685              :         m_libspdm_use_dhe_algo;
    1686            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1687              :         m_libspdm_use_aead_algo;
    1688              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1689              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1690              :         data_size;
    1691              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1692              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1693              :                      data, data_size);
    1694              : #endif
    1695              : 
    1696            1 :     session_id = 0xFFFFFFFF;
    1697            1 :     session_info = &spdm_context->session_info[0];
    1698            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1699              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1700            1 :     libspdm_session_info_set_psk_hint(session_info,
    1701              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1702              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1703            1 :     libspdm_secured_message_set_session_state(
    1704              :         session_info->secured_message_context,
    1705              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1706            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1707              :                     ((libspdm_secured_message_context_t
    1708            1 :                       *)(session_info->secured_message_context))
    1709              :                     ->aead_key_size,
    1710              :                     (uint8_t)(0xFF));
    1711            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1712              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1713              :         ((libspdm_secured_message_context_t
    1714            1 :           *)(session_info->secured_message_context))
    1715              :         ->aead_key_size);
    1716            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1717              :                     ((libspdm_secured_message_context_t
    1718            1 :                       *)(session_info->secured_message_context))
    1719              :                     ->aead_iv_size,
    1720              :                     (uint8_t)(0xFF));
    1721            1 :     libspdm_secured_message_set_response_handshake_salt(
    1722              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1723              :         ((libspdm_secured_message_context_t
    1724            1 :           *)(session_info->secured_message_context))
    1725              :         ->aead_iv_size);
    1726              :     ((libspdm_secured_message_context_t *)(session_info
    1727            1 :                                            ->secured_message_context))
    1728            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1729            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1730              : 
    1731            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1732            1 :     assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    1733            1 :     free(data);
    1734            1 : }
    1735              : 
    1736              : /**
    1737              :  * Test 9: the requester is setup correctly, but, on the first try, receiving
    1738              :  * a ResponseNotReady ERROR message, and, on retry, receiving a correct
    1739              :  * PSK_FINISH_RSP message.
    1740              :  * Expected behavior: client returns a Status of LIBSPDM_STATUS_SUCCESS and session
    1741              :  * is established.
    1742              :  **/
    1743            1 : static void req_psk_finish_case9(void **state)
    1744              : {
    1745              :     libspdm_return_t status;
    1746              :     libspdm_test_context_t *spdm_test_context;
    1747              :     libspdm_context_t *spdm_context;
    1748              :     uint32_t session_id;
    1749              :     void *data;
    1750              :     size_t data_size;
    1751              :     void *hash;
    1752              :     size_t hash_size;
    1753              :     libspdm_session_info_t *session_info;
    1754              : 
    1755            1 :     spdm_test_context = *state;
    1756            1 :     spdm_context = spdm_test_context->spdm_context;
    1757            1 :     spdm_test_context->case_id = 0x9;
    1758            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1759              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1760            1 :     spdm_context->connection_info.connection_state =
    1761              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1762            1 :     spdm_context->connection_info.capability.flags |=
    1763              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1764            1 :     spdm_context->connection_info.capability.flags |=
    1765              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1766            1 :     spdm_context->connection_info.capability.flags |=
    1767              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1768            1 :     spdm_context->local_context.capability.flags |=
    1769              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1770            1 :     spdm_context->local_context.capability.flags |=
    1771              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1772            1 :     spdm_context->local_context.capability.flags |=
    1773              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1774            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1775              :                                                          m_libspdm_use_asym_algo, &data,
    1776              :                                                          &data_size, &hash, &hash_size)) {
    1777            0 :         assert(false);
    1778              :     }
    1779            1 :     libspdm_reset_message_a(spdm_context);
    1780            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1781              :         m_libspdm_use_hash_algo;
    1782            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1783              :         m_libspdm_use_asym_algo;
    1784            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1785              :         m_libspdm_use_dhe_algo;
    1786            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1787              :         m_libspdm_use_aead_algo;
    1788              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1789              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1790              :         data_size;
    1791              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1792              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1793              :                      data, data_size);
    1794              : #endif
    1795              : 
    1796            1 :     session_id = 0xFFFFFFFF;
    1797            1 :     session_info = &spdm_context->session_info[0];
    1798            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    1799              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1800            1 :     libspdm_session_info_set_psk_hint(session_info,
    1801              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    1802              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1803            1 :     libspdm_secured_message_set_session_state(
    1804              :         session_info->secured_message_context,
    1805              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    1806            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    1807              :                     ((libspdm_secured_message_context_t
    1808            1 :                       *)(session_info->secured_message_context))
    1809              :                     ->aead_key_size,
    1810              :                     (uint8_t)(0xFF));
    1811            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    1812              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1813              :         ((libspdm_secured_message_context_t
    1814            1 :           *)(session_info->secured_message_context))
    1815              :         ->aead_key_size);
    1816            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    1817              :                     ((libspdm_secured_message_context_t
    1818            1 :                       *)(session_info->secured_message_context))
    1819              :                     ->aead_iv_size,
    1820              :                     (uint8_t)(0xFF));
    1821            1 :     libspdm_secured_message_set_response_handshake_salt(
    1822              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    1823              :         ((libspdm_secured_message_context_t
    1824            1 :           *)(session_info->secured_message_context))
    1825              :         ->aead_iv_size);
    1826              :     ((libspdm_secured_message_context_t *)(session_info
    1827            1 :                                            ->secured_message_context))
    1828            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    1829            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1830              : 
    1831            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    1832              :     if (LIBSPDM_RESPOND_IF_READY_SUPPORT) {
    1833            1 :         assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    1834            1 :         assert_int_equal(
    1835              :             libspdm_secured_message_get_session_state(
    1836              :                 spdm_context->session_info[0].secured_message_context),
    1837              :             LIBSPDM_SESSION_STATE_ESTABLISHED);
    1838              :     } else {
    1839              :         assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
    1840              :     }
    1841              : 
    1842            1 :     free(data);
    1843            1 : }
    1844              : 
    1845              : /**
    1846              :  * Test 10: receiving an unexpected ERROR message from the responder.
    1847              :  * There are tests for all named codes, including some reserved ones
    1848              :  * (namely, 0x00, 0x0b, 0x0c, 0x3f, 0xfd, 0xfe).
    1849              :  * However, for having specific test cases, it is excluded from this case:
    1850              :  * Busy (0x03), ResponseNotReady (0x42), and RequestResync (0x43).
    1851              :  * Expected behavior: client returns a status of RETURN_DEVICE_ERROR.
    1852              :  **/
    1853            1 : static void req_psk_finish_case10(void **state) {
    1854              :     libspdm_return_t status;
    1855              :     libspdm_test_context_t    *spdm_test_context;
    1856              :     libspdm_context_t  *spdm_context;
    1857              :     uint32_t session_id;
    1858              :     void                 *data;
    1859              :     size_t data_size;
    1860              :     void                 *hash;
    1861              :     size_t hash_size;
    1862              :     libspdm_session_info_t    *session_info;
    1863              :     uint16_t error_code;
    1864              : 
    1865            1 :     spdm_test_context = *state;
    1866            1 :     spdm_context = spdm_test_context->spdm_context;
    1867            1 :     spdm_test_context->case_id = 0xA;
    1868            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1869              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1870            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1871            1 :     spdm_context->connection_info.capability.flags |=
    1872              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1873            1 :     spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1874            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1875            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1876            1 :     spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1877            1 :     libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
    1878              :                                                      m_libspdm_use_asym_algo,
    1879              :                                                      &data, &data_size,
    1880              :                                                      &hash, &hash_size);
    1881            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    1882            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    1883            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    1884            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    1885              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1886              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1887              :         data_size;
    1888              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1889              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1890              :                      data, data_size);
    1891              : #endif
    1892              : 
    1893            1 :     error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
    1894           19 :     while(error_code <= 0xff) {
    1895           18 :         spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1896           18 :         libspdm_reset_message_a(spdm_context);
    1897              : 
    1898           18 :         session_id = 0xFFFFFFFF;
    1899           18 :         session_info = &spdm_context->session_info[0];
    1900           18 :         libspdm_session_info_init (spdm_context, session_info, session_id,
    1901              :                                    SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    1902           18 :         libspdm_session_info_set_psk_hint(session_info,
    1903              :                                           LIBSPDM_TEST_PSK_HINT_STRING,
    1904              :                                           sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    1905           18 :         libspdm_secured_message_set_session_state (session_info->secured_message_context,
    1906              :                                                    LIBSPDM_SESSION_STATE_HANDSHAKING);
    1907           18 :         libspdm_set_mem (m_libspdm_dummy_key_buffer,
    1908           18 :                          ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_key_size,
    1909              :                          (uint8_t)(0xFF));
    1910           18 :         libspdm_secured_message_set_response_handshake_encryption_key (
    1911              :             session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    1912           18 :             ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_key_size);
    1913           18 :         libspdm_set_mem (m_libspdm_dummy_salt_buffer,
    1914           18 :                          ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->aead_iv_size,
    1915              :                          (uint8_t)(0xFF));
    1916           18 :         libspdm_secured_message_set_response_handshake_salt (session_info->secured_message_context,
    1917              :                                                              m_libspdm_dummy_salt_buffer,
    1918              :                                                              ((libspdm_secured_message_context_t*)(
    1919           18 :                                                                   session_info->
    1920              :                                                                   secured_message_context))->aead_iv_size);
    1921           18 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))->
    1922              :         handshake_secret
    1923           18 :         .response_handshake_sequence_number = 0;
    1924           18 :         libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    1925              : 
    1926           18 :         status = libspdm_send_receive_psk_finish (spdm_context, session_id);
    1927           18 :         if(error_code != SPDM_ERROR_CODE_DECRYPT_ERROR) {
    1928           17 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
    1929              :         } else {
    1930            1 :             LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_SESSION_MSG_ERROR, error_code);
    1931              :         }
    1932              : 
    1933           18 :         error_code++;
    1934           18 :         if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
    1935            1 :             error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
    1936              :         }
    1937           18 :         if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) { /*skip some reserved error codes (0d to 3e)*/
    1938            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
    1939              :         }
    1940           18 :         if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) { /*skip response not ready, request resync, and some reserved codes (44 to fc)*/
    1941            1 :             error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
    1942              :         }
    1943              :     }
    1944              : 
    1945            1 :     free(data);
    1946            1 : }
    1947              : 
    1948            1 : static void req_psk_finish_case11(void **state)
    1949              : {
    1950              :     libspdm_return_t status;
    1951              :     libspdm_test_context_t *spdm_test_context;
    1952              :     libspdm_context_t *spdm_context;
    1953              :     uint32_t session_id;
    1954              :     void *data;
    1955              :     size_t data_size;
    1956              :     void *hash;
    1957              :     size_t hash_size;
    1958              :     libspdm_session_info_t *session_info;
    1959              : 
    1960            1 :     spdm_test_context = *state;
    1961            1 :     spdm_context = spdm_test_context->spdm_context;
    1962            1 :     spdm_test_context->case_id = 0xB;
    1963            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    1964              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    1965            1 :     spdm_context->connection_info.connection_state =
    1966              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    1967            1 :     spdm_context->connection_info.capability.flags |=
    1968              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    1969            1 :     spdm_context->connection_info.capability.flags |=
    1970              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    1971            1 :     spdm_context->connection_info.capability.flags |=
    1972              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    1973            1 :     spdm_context->local_context.capability.flags |=
    1974              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    1975            1 :     spdm_context->local_context.capability.flags |=
    1976              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    1977            1 :     spdm_context->local_context.capability.flags |=
    1978              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    1979            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    1980              :                                                          m_libspdm_use_asym_algo, &data,
    1981              :                                                          &data_size, &hash, &hash_size)) {
    1982            0 :         assert(false);
    1983              :     }
    1984            1 :     libspdm_reset_message_a(spdm_context);
    1985              : 
    1986            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    1987              :         m_libspdm_use_hash_algo;
    1988            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    1989              :         m_libspdm_use_asym_algo;
    1990            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    1991              :         m_libspdm_use_dhe_algo;
    1992            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    1993              :         m_libspdm_use_aead_algo;
    1994              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    1995              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    1996              :         data_size;
    1997              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    1998              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    1999              :                      data, data_size);
    2000              : #endif
    2001              : 
    2002            1 :     session_id = 0xFFFFFFFF;
    2003            1 :     session_info = &spdm_context->session_info[0];
    2004            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    2005              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2006            1 :     libspdm_session_info_set_psk_hint(session_info,
    2007              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2008              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2009            1 :     libspdm_secured_message_set_session_state(
    2010              :         session_info->secured_message_context,
    2011              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2012            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2013              :                     ((libspdm_secured_message_context_t
    2014            1 :                       *)(session_info->secured_message_context))
    2015              :                     ->aead_key_size,
    2016              :                     (uint8_t)(0xFF));
    2017            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2018              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2019              :         ((libspdm_secured_message_context_t
    2020            1 :           *)(session_info->secured_message_context))
    2021              :         ->aead_key_size);
    2022            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2023              :                     ((libspdm_secured_message_context_t
    2024            1 :                       *)(session_info->secured_message_context))
    2025              :                     ->aead_iv_size,
    2026              :                     (uint8_t)(0xFF));
    2027            1 :     libspdm_secured_message_set_response_handshake_salt(
    2028              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2029              :         ((libspdm_secured_message_context_t
    2030            1 :           *)(session_info->secured_message_context))
    2031              :         ->aead_iv_size);
    2032              :     ((libspdm_secured_message_context_t *)(session_info
    2033            1 :                                            ->secured_message_context))
    2034            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2035            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2036              : 
    2037              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2038              :     session_info->session_transcript.message_m.buffer_size =
    2039              :         session_info->session_transcript.message_m.max_buffer_size;
    2040              :     spdm_context->transcript.message_b.buffer_size =
    2041              :         spdm_context->transcript.message_b.max_buffer_size;
    2042              :     spdm_context->transcript.message_c.buffer_size =
    2043              :         spdm_context->transcript.message_c.max_buffer_size;
    2044              :     spdm_context->transcript.message_mut_b.buffer_size =
    2045              :         spdm_context->transcript.message_mut_b.max_buffer_size;
    2046              :     spdm_context->transcript.message_mut_c.buffer_size =
    2047              :         spdm_context->transcript.message_mut_c.max_buffer_size;
    2048              : #endif
    2049              : 
    2050            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2051            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2052            1 :     assert_int_equal(
    2053              :         libspdm_secured_message_get_session_state(
    2054              :             spdm_context->session_info[0].secured_message_context),
    2055              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2056              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2057              :     assert_int_equal(session_info->session_transcript.message_m.buffer_size, 0);
    2058              :     assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
    2059              :     assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
    2060              :     assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
    2061              :     assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
    2062              : #endif
    2063            1 :     free(data);
    2064            1 : }
    2065              : 
    2066              : /**
    2067              :  * Test 12: requester is not setup correctly to support pre-shared keys
    2068              :  * (no capabilities). The responder would attempt to return a correct
    2069              :  * PSK_FINISH_RSP message.
    2070              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2071              :  **/
    2072            1 : static void req_psk_finish_case12(void **state)
    2073              : {
    2074              :     libspdm_return_t status;
    2075              :     libspdm_test_context_t *spdm_test_context;
    2076              :     libspdm_context_t *spdm_context;
    2077              :     uint32_t session_id;
    2078              :     void *data;
    2079              :     size_t data_size;
    2080              :     void *hash;
    2081              :     size_t hash_size;
    2082              :     libspdm_session_info_t *session_info;
    2083              : 
    2084            1 :     spdm_test_context = *state;
    2085            1 :     spdm_context = spdm_test_context->spdm_context;
    2086            1 :     spdm_test_context->case_id = 0xC;
    2087            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2088              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2089            1 :     spdm_context->connection_info.connection_state =
    2090              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2091              :     /*no PSK capabilities*/
    2092            1 :     spdm_context->connection_info.capability.flags &=
    2093              :         ~(SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP);
    2094            1 :     spdm_context->connection_info.capability.flags |=
    2095              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2096            1 :     spdm_context->connection_info.capability.flags |=
    2097              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2098            1 :     spdm_context->local_context.capability.flags |=
    2099              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2100            1 :     spdm_context->local_context.capability.flags |=
    2101              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2102            1 :     spdm_context->local_context.capability.flags |=
    2103              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2104            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2105              :                                                          m_libspdm_use_asym_algo, &data,
    2106              :                                                          &data_size, &hash, &hash_size)) {
    2107            0 :         assert(false);
    2108              :     }
    2109            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    2110            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2111              :         m_libspdm_use_hash_algo;
    2112            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2113              :         m_libspdm_use_asym_algo;
    2114            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2115              :         m_libspdm_use_dhe_algo;
    2116            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2117              :         m_libspdm_use_aead_algo;
    2118              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2119              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2120              :         data_size;
    2121              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2122              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2123              :                      data, data_size);
    2124              : #endif
    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,
    2129              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2130            1 :     libspdm_session_info_set_psk_hint(session_info,
    2131              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2132              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2133            1 :     libspdm_secured_message_set_session_state(
    2134              :         session_info->secured_message_context,
    2135              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2136            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2137              :                     ((libspdm_secured_message_context_t
    2138            1 :                       *)(session_info->secured_message_context))
    2139              :                     ->aead_key_size,
    2140              :                     (uint8_t)(0xFF));
    2141            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2142              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2143              :         ((libspdm_secured_message_context_t
    2144            1 :           *)(session_info->secured_message_context))
    2145              :         ->aead_key_size);
    2146            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2147              :                     ((libspdm_secured_message_context_t
    2148            1 :                       *)(session_info->secured_message_context))
    2149              :                     ->aead_iv_size,
    2150              :                     (uint8_t)(0xFF));
    2151            1 :     libspdm_secured_message_set_response_handshake_salt(
    2152              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2153              :         ((libspdm_secured_message_context_t
    2154            1 :           *)(session_info->secured_message_context))
    2155              :         ->aead_iv_size);
    2156              :     ((libspdm_secured_message_context_t *)(session_info
    2157            1 :                                            ->secured_message_context))
    2158            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2159            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2160              : 
    2161            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2162            1 :     assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
    2163            1 :     free(data);
    2164            1 : }
    2165              : 
    2166              : /**
    2167              :  * Test 13: receiving an incorrect FINISH_RSP message, with wrong response
    2168              :  * code, but all other field correct.
    2169              :  * Expected behavior: client returns a Status of RETURN_DEVICE_ERROR.
    2170              :  **/
    2171            1 : static void req_psk_finish_case13(void **state)
    2172              : {
    2173              :     libspdm_return_t status;
    2174              :     libspdm_test_context_t *spdm_test_context;
    2175              :     libspdm_context_t *spdm_context;
    2176              :     uint32_t session_id;
    2177              :     void *data;
    2178              :     size_t data_size;
    2179              :     void *hash;
    2180              :     size_t hash_size;
    2181              :     libspdm_session_info_t *session_info;
    2182              : 
    2183            1 :     spdm_test_context = *state;
    2184            1 :     spdm_context = spdm_test_context->spdm_context;
    2185            1 :     spdm_test_context->case_id = 0xD;
    2186            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2187              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2188            1 :     spdm_context->connection_info.connection_state =
    2189              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2190              :     /*no PSK capabilities*/
    2191            1 :     spdm_context->connection_info.capability.flags |=
    2192              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    2193            1 :     spdm_context->connection_info.capability.flags |=
    2194              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2195            1 :     spdm_context->connection_info.capability.flags |=
    2196              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2197            1 :     spdm_context->local_context.capability.flags |=
    2198              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2199            1 :     spdm_context->local_context.capability.flags |=
    2200              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2201            1 :     spdm_context->local_context.capability.flags |=
    2202              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2203            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2204              :                                                          m_libspdm_use_asym_algo, &data,
    2205              :                                                          &data_size, &hash, &hash_size)) {
    2206            0 :         assert(false);
    2207              :     }
    2208            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    2209            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2210              :         m_libspdm_use_hash_algo;
    2211            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2212              :         m_libspdm_use_asym_algo;
    2213            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2214              :         m_libspdm_use_dhe_algo;
    2215            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2216              :         m_libspdm_use_aead_algo;
    2217              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2218              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2219              :         data_size;
    2220              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2221              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2222              :                      data, data_size);
    2223              : #endif
    2224              : 
    2225            1 :     session_id = 0xFFFFFFFF;
    2226            1 :     session_info = &spdm_context->session_info[0];
    2227            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    2228              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2229            1 :     libspdm_session_info_set_psk_hint(session_info,
    2230              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2231              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2232            1 :     libspdm_secured_message_set_session_state(
    2233              :         session_info->secured_message_context,
    2234              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2235            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2236              :                     ((libspdm_secured_message_context_t
    2237            1 :                       *)(session_info->secured_message_context))
    2238              :                     ->aead_key_size,
    2239              :                     (uint8_t)(0xFF));
    2240            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2241              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2242              :         ((libspdm_secured_message_context_t
    2243            1 :           *)(session_info->secured_message_context))
    2244              :         ->aead_key_size);
    2245            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2246              :                     ((libspdm_secured_message_context_t
    2247            1 :                       *)(session_info->secured_message_context))
    2248              :                     ->aead_iv_size,
    2249              :                     (uint8_t)(0xFF));
    2250            1 :     libspdm_secured_message_set_response_handshake_salt(
    2251              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2252              :         ((libspdm_secured_message_context_t
    2253            1 :           *)(session_info->secured_message_context))
    2254              :         ->aead_iv_size);
    2255              :     ((libspdm_secured_message_context_t *)(session_info
    2256            1 :                                            ->secured_message_context))
    2257            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2258            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2259              : 
    2260            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2261            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
    2262            1 :     free(data);
    2263            1 : }
    2264              : 
    2265              : /**
    2266              :  * Test 14: requester is not setup correctly by not initializing a
    2267              :  * session during PSK_EXCHANGE. The responder would attempt to
    2268              :  * return a correct PSK_FINISH_RSP message.
    2269              :  * Expected behavior: client returns a Status of RETURN_UNSUPPORTED.
    2270              :  **/
    2271            1 : static void req_psk_finish_case14(void **state)
    2272              : {
    2273              :     libspdm_return_t status;
    2274              :     libspdm_test_context_t *spdm_test_context;
    2275              :     libspdm_context_t *spdm_context;
    2276              :     uint32_t session_id;
    2277              :     void *data;
    2278              :     size_t data_size;
    2279              :     void *hash;
    2280              :     size_t hash_size;
    2281              :     libspdm_session_info_t *session_info;
    2282              : 
    2283            1 :     spdm_test_context = *state;
    2284            1 :     spdm_context = spdm_test_context->spdm_context;
    2285            1 :     spdm_test_context->case_id = 0xE;
    2286            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2287              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2288            1 :     spdm_context->connection_info.connection_state =
    2289              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2290              :     /*no PSK capabilities*/
    2291            1 :     spdm_context->connection_info.capability.flags |=
    2292              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    2293            1 :     spdm_context->connection_info.capability.flags |=
    2294              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2295            1 :     spdm_context->connection_info.capability.flags |=
    2296              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2297            1 :     spdm_context->local_context.capability.flags |=
    2298              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2299            1 :     spdm_context->local_context.capability.flags |=
    2300              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2301            1 :     spdm_context->local_context.capability.flags |=
    2302              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2303            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2304              :                                                          m_libspdm_use_asym_algo, &data,
    2305              :                                                          &data_size, &hash, &hash_size)) {
    2306            0 :         assert(false);
    2307              :     }
    2308            1 :     spdm_context->transcript.message_a.buffer_size = 0;
    2309            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2310              :         m_libspdm_use_hash_algo;
    2311            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2312              :         m_libspdm_use_asym_algo;
    2313            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2314              :         m_libspdm_use_dhe_algo;
    2315            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2316              :         m_libspdm_use_aead_algo;
    2317              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2318              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2319              :         data_size;
    2320              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2321              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2322              :                      data, data_size);
    2323              : #endif
    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,
    2328              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2329            1 :     libspdm_session_info_set_psk_hint(session_info,
    2330              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2331              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2332            1 :     libspdm_secured_message_set_session_state(
    2333              :         session_info->secured_message_context,
    2334              :         LIBSPDM_SESSION_STATE_NOT_STARTED);
    2335            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2336              :                     ((libspdm_secured_message_context_t
    2337            1 :                       *)(session_info->secured_message_context))
    2338              :                     ->aead_key_size,
    2339              :                     (uint8_t)(0xFF));
    2340            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2341              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2342              :         ((libspdm_secured_message_context_t
    2343            1 :           *)(session_info->secured_message_context))
    2344              :         ->aead_key_size);
    2345            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2346              :                     ((libspdm_secured_message_context_t
    2347            1 :                       *)(session_info->secured_message_context))
    2348              :                     ->aead_iv_size,
    2349              :                     (uint8_t)(0xFF));
    2350            1 :     libspdm_secured_message_set_response_handshake_salt(
    2351              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2352              :         ((libspdm_secured_message_context_t
    2353            1 :           *)(session_info->secured_message_context))
    2354              :         ->aead_iv_size);
    2355              :     ((libspdm_secured_message_context_t *)(session_info
    2356            1 :                                            ->secured_message_context))
    2357            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2358            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2359              : 
    2360            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2361            1 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
    2362            1 :     free(data);
    2363            1 : }
    2364              : 
    2365              : /**
    2366              :  * Test 15 the requester is setup correctly, but receives an ERROR with SPDM_ERROR_CODE_DECRYPT_ERROR.
    2367              :  * Expected behavior: client returns a Status of INVALID_SESSION_ID  and free the session ID.
    2368              :  **/
    2369            1 : static void req_psk_finish_case15(void **state)
    2370              : {
    2371              :     libspdm_return_t status;
    2372              :     libspdm_test_context_t *spdm_test_context;
    2373              :     libspdm_context_t *spdm_context;
    2374              :     uint32_t session_id;
    2375              :     void *data;
    2376              :     size_t data_size;
    2377              :     void *hash;
    2378              :     size_t hash_size;
    2379              :     libspdm_session_info_t *session_info;
    2380              : 
    2381            1 :     spdm_test_context = *state;
    2382            1 :     spdm_context = spdm_test_context->spdm_context;
    2383            1 :     spdm_test_context->case_id = 0xF;
    2384            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2385              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2386            1 :     spdm_context->connection_info.connection_state =
    2387              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2388            1 :     spdm_context->connection_info.capability.flags |=
    2389              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    2390            1 :     spdm_context->connection_info.capability.flags |=
    2391              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2392            1 :     spdm_context->connection_info.capability.flags |=
    2393              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2394            1 :     spdm_context->local_context.capability.flags |=
    2395              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2396            1 :     spdm_context->local_context.capability.flags |=
    2397              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2398            1 :     spdm_context->local_context.capability.flags |=
    2399              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2400            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2401              :                                                          m_libspdm_use_asym_algo, &data,
    2402              :                                                          &data_size, &hash, &hash_size)) {
    2403            0 :         assert(false);
    2404              :     }
    2405            1 :     libspdm_reset_message_a(spdm_context);
    2406            1 :     spdm_context->connection_info.algorithm.base_hash_algo =
    2407              :         m_libspdm_use_hash_algo;
    2408            1 :     spdm_context->connection_info.algorithm.base_asym_algo =
    2409              :         m_libspdm_use_asym_algo;
    2410            1 :     spdm_context->connection_info.algorithm.dhe_named_group =
    2411              :         m_libspdm_use_dhe_algo;
    2412            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite =
    2413              :         m_libspdm_use_aead_algo;
    2414              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2415              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2416              :         data_size;
    2417              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2418              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2419              :                      data, data_size);
    2420              : #endif
    2421              : 
    2422            1 :     session_id = 0xFFFFFFFF;
    2423            1 :     session_info = &spdm_context->session_info[0];
    2424            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    2425              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2426            1 :     libspdm_session_info_set_psk_hint(session_info,
    2427              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2428              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2429            1 :     libspdm_secured_message_set_session_state(
    2430              :         session_info->secured_message_context,
    2431              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2432            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2433              :                     ((libspdm_secured_message_context_t
    2434            1 :                       *)(session_info->secured_message_context))
    2435              :                     ->aead_key_size,
    2436              :                     (uint8_t)(0xFF));
    2437            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2438              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2439              :         ((libspdm_secured_message_context_t
    2440            1 :           *)(session_info->secured_message_context))
    2441              :         ->aead_key_size);
    2442            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2443              :                     ((libspdm_secured_message_context_t
    2444            1 :                       *)(session_info->secured_message_context))
    2445              :                     ->aead_iv_size,
    2446              :                     (uint8_t)(0xFF));
    2447            1 :     libspdm_secured_message_set_response_handshake_salt(
    2448              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2449              :         ((libspdm_secured_message_context_t
    2450            1 :           *)(session_info->secured_message_context))
    2451              :         ->aead_iv_size);
    2452              :     ((libspdm_secured_message_context_t *)(session_info
    2453            1 :                                            ->secured_message_context))
    2454            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2455            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2456              : 
    2457            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2458            1 :     assert_int_equal(status, LIBSPDM_STATUS_SESSION_MSG_ERROR);
    2459            1 :     assert_int_equal(spdm_context->session_info->session_id, INVALID_SESSION_ID);
    2460            1 :     free(data);
    2461            1 : }
    2462              : 
    2463              : /**
    2464              :  * Test 16: a request message is successfully sent and a response message is successfully received.
    2465              :  * Expected Behavior: requester returns the status LIBSPDM_STATUS_SUCCESS and a PSK_FINISH_RSP message is
    2466              :  * received, buffer F appends the exchanged PSK_FINISH and PSK_FINISH_RSP messages.
    2467              :  **/
    2468            1 : static void req_psk_finish_case16(void **state)
    2469              : {
    2470              :     libspdm_return_t status;
    2471              :     libspdm_test_context_t *spdm_test_context;
    2472              :     libspdm_context_t *spdm_context;
    2473              :     uint32_t session_id;
    2474              :     void *data;
    2475              :     size_t data_size;
    2476              :     void *hash;
    2477              :     size_t hash_size;
    2478              :     libspdm_session_info_t *session_info;
    2479              : 
    2480            1 :     spdm_test_context = *state;
    2481            1 :     spdm_context = spdm_test_context->spdm_context;
    2482            1 :     spdm_test_context->case_id = 0x10;
    2483            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
    2484              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2485            1 :     spdm_context->connection_info.connection_state =
    2486              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2487            1 :     spdm_context->connection_info.capability.flags |=
    2488              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    2489            1 :     spdm_context->connection_info.capability.flags |=
    2490              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2491            1 :     spdm_context->connection_info.capability.flags |=
    2492              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2493            1 :     spdm_context->local_context.capability.flags |=
    2494              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2495            1 :     spdm_context->local_context.capability.flags |=
    2496              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2497            1 :     spdm_context->local_context.capability.flags |=
    2498              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2499            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2500              :                                                          m_libspdm_use_asym_algo, &data,
    2501              :                                                          &data_size, &hash, &hash_size)) {
    2502            0 :         assert(false);
    2503              :     }
    2504            1 :     libspdm_reset_message_a(spdm_context);
    2505            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2506            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2507            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2508            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2509              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2510              :     spdm_context->connection_info.peer_used_cert_chain[0].buffer_size =
    2511              :         data_size;
    2512              :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
    2513              :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
    2514              :                      data, data_size);
    2515              : #endif
    2516              : 
    2517              : 
    2518            1 :     session_id = 0xFFFFFFFF;
    2519            1 :     session_info = &spdm_context->session_info[0];
    2520            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    2521              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2522            1 :     libspdm_session_info_set_psk_hint(session_info,
    2523              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2524              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2525            1 :     libspdm_secured_message_set_session_state(
    2526              :         session_info->secured_message_context,
    2527              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2528            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2529            1 :                     ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2530              :                     ->aead_key_size, (uint8_t)(0xFF));
    2531            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2532              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2533            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2534              :         ->aead_key_size);
    2535            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2536            1 :                     ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2537              :                     ->aead_iv_size, (uint8_t)(0xFF));
    2538            1 :     libspdm_secured_message_set_response_handshake_salt(
    2539              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2540            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2541              :         ->aead_iv_size);
    2542            1 :     ((libspdm_secured_message_context_t *)(session_info->secured_message_context))
    2543            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2544            1 :     ((libspdm_secured_message_context_t *)(session_info->secured_message_context))
    2545            1 :     ->handshake_secret.request_handshake_sequence_number = 0;
    2546            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2547              : 
    2548            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2549            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2550            1 :     assert_int_equal(
    2551              :         libspdm_secured_message_get_session_state(
    2552              :             spdm_context->session_info[0].secured_message_context),
    2553              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2554              : 
    2555              : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
    2556              :     assert_int_equal(spdm_context->session_info[0].session_transcript.message_f.buffer_size,
    2557              :                      m_libspdm_local_buffer_size);
    2558              :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%x):\n",
    2559              :                    m_libspdm_local_buffer_size));
    2560              :     libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2561              :     assert_memory_equal(spdm_context->session_info[0].session_transcript.message_f.buffer,
    2562              :                         m_libspdm_local_buffer, m_libspdm_local_buffer_size);
    2563              : #endif
    2564            1 :     free(data);
    2565            1 : }
    2566              : 
    2567              : /**
    2568              :  * Test 17: SPDM version 1.4, with OpaqueData
    2569              :  * Expected Behavior: requester returns the status LIBSPDM_STATUS_SUCCESS and a PSK_FINISH_RSP message is
    2570              :  * received.
    2571              :  **/
    2572            1 : static void req_psk_finish_case17(void **state)
    2573              : {
    2574              :     libspdm_return_t status;
    2575              :     libspdm_test_context_t *spdm_test_context;
    2576              :     libspdm_context_t *spdm_context;
    2577              :     uint32_t session_id;
    2578              :     void *data;
    2579              :     size_t data_size;
    2580              :     void *hash;
    2581              :     size_t hash_size;
    2582              :     libspdm_session_info_t *session_info;
    2583              : 
    2584            1 :     spdm_test_context = *state;
    2585            1 :     spdm_context = spdm_test_context->spdm_context;
    2586            1 :     spdm_test_context->case_id = 0x11;
    2587            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
    2588              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
    2589            1 :     spdm_context->connection_info.connection_state =
    2590              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
    2591            1 :     spdm_context->connection_info.capability.flags |=
    2592              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PSK_CAP;
    2593            1 :     spdm_context->connection_info.capability.flags |=
    2594              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCRYPT_CAP;
    2595            1 :     spdm_context->connection_info.capability.flags |=
    2596              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
    2597            1 :     spdm_context->local_context.capability.flags |=
    2598              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PSK_CAP;
    2599            1 :     spdm_context->local_context.capability.flags |=
    2600              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCRYPT_CAP;
    2601            1 :     spdm_context->local_context.capability.flags |=
    2602              :         SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
    2603            1 :     if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
    2604              :                                                          m_libspdm_use_asym_algo, &data,
    2605              :                                                          &data_size, &hash, &hash_size)) {
    2606            0 :         assert(false);
    2607              :     }
    2608            1 :     libspdm_reset_message_a(spdm_context);
    2609            1 :     spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
    2610            1 :     spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
    2611            1 :     spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
    2612            1 :     spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
    2613              : 
    2614            1 :     session_id = 0xFFFFFFFF;
    2615            1 :     session_info = &spdm_context->session_info[0];
    2616            1 :     libspdm_session_info_init(spdm_context, session_info, session_id,
    2617              :                               SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT, true);
    2618            1 :     libspdm_session_info_set_psk_hint(session_info,
    2619              :                                       LIBSPDM_TEST_PSK_HINT_STRING,
    2620              :                                       sizeof(LIBSPDM_TEST_PSK_HINT_STRING));
    2621            1 :     libspdm_secured_message_set_session_state(
    2622              :         session_info->secured_message_context,
    2623              :         LIBSPDM_SESSION_STATE_HANDSHAKING);
    2624            1 :     libspdm_set_mem(m_libspdm_dummy_key_buffer,
    2625            1 :                     ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2626              :                     ->aead_key_size, (uint8_t)(0xFF));
    2627            1 :     libspdm_secured_message_set_response_handshake_encryption_key(
    2628              :         session_info->secured_message_context, m_libspdm_dummy_key_buffer,
    2629            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2630              :         ->aead_key_size);
    2631            1 :     libspdm_set_mem(m_libspdm_dummy_salt_buffer,
    2632            1 :                     ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2633              :                     ->aead_iv_size, (uint8_t)(0xFF));
    2634            1 :     libspdm_secured_message_set_response_handshake_salt(
    2635              :         session_info->secured_message_context, m_libspdm_dummy_salt_buffer,
    2636            1 :         ((libspdm_secured_message_context_t*)(session_info->secured_message_context))
    2637              :         ->aead_iv_size);
    2638            1 :     ((libspdm_secured_message_context_t *)(session_info->secured_message_context))
    2639            1 :     ->handshake_secret.response_handshake_sequence_number = 0;
    2640            1 :     ((libspdm_secured_message_context_t *)(session_info->secured_message_context))
    2641            1 :     ->handshake_secret.request_handshake_sequence_number = 0;
    2642            1 :     libspdm_secured_message_set_dummy_finished_key (session_info->secured_message_context);
    2643              : 
    2644            1 :     status = libspdm_send_receive_psk_finish(spdm_context, session_id);
    2645            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
    2646            1 :     assert_int_equal(
    2647              :         libspdm_secured_message_get_session_state(
    2648              :             spdm_context->session_info[0].secured_message_context),
    2649              :         LIBSPDM_SESSION_STATE_ESTABLISHED);
    2650              : 
    2651            1 :     free(data);
    2652            1 : }
    2653              : 
    2654            1 : int libspdm_req_psk_finish_test(void)
    2655              : {
    2656            1 :     const struct CMUnitTest test_cases[] = {
    2657              :         /* SendRequest failed*/
    2658              :         cmocka_unit_test(req_psk_finish_case1),
    2659              :         /* Successful response*/
    2660              :         cmocka_unit_test(req_psk_finish_case2),
    2661              :         /* connection_state check failed*/
    2662              :         cmocka_unit_test(req_psk_finish_case3),
    2663              :         /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
    2664              :         cmocka_unit_test(req_psk_finish_case4),
    2665              :         /* Always SPDM_ERROR_CODE_BUSY*/
    2666              :         cmocka_unit_test(req_psk_finish_case5),
    2667              :         /* SPDM_ERROR_CODE_BUSY + Successful response*/
    2668              :         cmocka_unit_test(req_psk_finish_case6),
    2669              :         /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
    2670              :         cmocka_unit_test(req_psk_finish_case7),
    2671              :         /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
    2672              :         cmocka_unit_test(req_psk_finish_case8),
    2673              :         /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
    2674              :         cmocka_unit_test(req_psk_finish_case9),
    2675              :         /* Unexpected errors*/
    2676              :         cmocka_unit_test(req_psk_finish_case10),
    2677              :         /* Buffer reset*/
    2678              :         cmocka_unit_test(req_psk_finish_case11),
    2679              :         /* No correct setup*/
    2680              :         cmocka_unit_test(req_psk_finish_case12),
    2681              :         /* Wrong response code*/
    2682              :         cmocka_unit_test(req_psk_finish_case13),
    2683              :         /* Uninitialized session*/
    2684              :         cmocka_unit_test(req_psk_finish_case14),
    2685              :         /* Error response: SPDM_ERROR_CODE_DECRYPT_ERROR*/
    2686              :         cmocka_unit_test(req_psk_finish_case15),
    2687              :         /* Buffer verification*/
    2688              :         cmocka_unit_test(req_psk_finish_case16),
    2689              :         /* SPDM 1.4 with OpaqueData */
    2690              :         cmocka_unit_test(req_psk_finish_case17),
    2691              :     };
    2692              : 
    2693            1 :     libspdm_test_context_t test_context = {
    2694              :         LIBSPDM_TEST_CONTEXT_VERSION,
    2695              :         true,
    2696              :         send_message,
    2697              :         receive_message,
    2698              :     };
    2699              : 
    2700            1 :     libspdm_setup_test_context(&test_context);
    2701              : 
    2702            1 :     return cmocka_run_group_tests(test_cases,
    2703              :                                   libspdm_unit_test_group_setup,
    2704              :                                   libspdm_unit_test_group_teardown);
    2705              : }
    2706              : 
    2707              : #endif /* LIBSPDM_ENABLE_CAPABILITY_PSK_CAP*/
        

Generated by: LCOV version 2.0-1