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: 2026-02-22 08:11:49 Functions: 100.0 % 23 23

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

Generated by: LCOV version 2.0-1