Code coverage report for DMTF/libspdm
Current view: top level - library/spdm_requester_lib - libspdm_req_send_receive.c (source / functions) Coverage Total Hit
Test: b3f7d2e00db5b07cf17ccc0075121f9c08f2965b Lines: 86.6 % 372 322
Test Date: 2026-11-29 08:33:56 Functions: 100.0 % 5 5
Branches: 72.4 % 250 181

             Branch data     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 "internal/libspdm_requester_lib.h"
       8                 :             : #include "internal/libspdm_secured_message_lib.h"
       9                 :             : 
      10                 :        6292 : libspdm_return_t libspdm_send_request(void *spdm_context, const uint32_t *session_id,
      11                 :             :                                       bool is_app_message,
      12                 :             :                                       size_t request_size, void *request)
      13                 :             : {
      14                 :             :     libspdm_context_t *context;
      15                 :             :     libspdm_return_t status;
      16                 :             :     uint8_t *message;
      17                 :             :     size_t message_size;
      18                 :             :     uint64_t timeout;
      19                 :             :     uint8_t *scratch_buffer;
      20                 :             :     size_t scratch_buffer_size;
      21                 :             :     size_t transport_header_size;
      22                 :             :     uint8_t *sender_buffer;
      23                 :             :     size_t sender_buffer_size;
      24                 :             : 
      25                 :        6292 :     context = spdm_context;
      26                 :             : 
      27         [ +  + ]:        6292 :     if (is_app_message) {
      28         [ +  - ]:          10 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
      29                 :             :                        "libspdm_send_spdm_request[%x] app msg, size (0x%zx): \n",
      30                 :             :                        (session_id != NULL) ? *session_id : 0x0, request_size));
      31                 :             :     } else {
      32         [ +  + ]:        6282 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
      33                 :             :                        "libspdm_send_spdm_request[%x] msg %s(0x%x), size (0x%zx): \n",
      34                 :             :                        (session_id != NULL) ? *session_id : 0x0,
      35                 :             :                        libspdm_get_code_str(((spdm_message_header_t *)request)->
      36                 :             :                                             request_response_code),
      37                 :             :                        ((spdm_message_header_t *)request)->request_response_code, request_size));
      38                 :             :     }
      39                 :        6292 :     LIBSPDM_INTERNAL_DUMP_HEX(request, request_size);
      40                 :             : 
      41                 :        6292 :     transport_header_size = context->local_context.capability.transport_header_size;
      42                 :        6292 :     libspdm_get_scratch_buffer(context, (void **)&scratch_buffer, &scratch_buffer_size);
      43                 :        6292 :     libspdm_get_sender_buffer(context, (void **)&sender_buffer, &sender_buffer_size);
      44                 :             : 
      45                 :             :     /* This is a problem because original code assumes request is in the sender buffer,
      46                 :             :      * when it can really be using the scratch space for chunking.
      47                 :             :      * Did not want to modify all request handlers to pass this information,
      48                 :             :      * so just making the determination here by examining scratch/sender buffers.
      49                 :             :      * This may be something that should be refactored in the future. */
      50                 :             :     #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
      51                 :             :     /* If message is in the scratch buffer then sender_buffer can/will be NULL, so first check for
      52                 :             :      * NULLness before performing pointer arithmetic with it. */
      53   [ +  +  +  - ]:        6292 :     if ((sender_buffer != NULL) && ((uint8_t *)request >= sender_buffer) &&
      54         [ -  + ]:        6094 :         ((uint8_t *)request < (sender_buffer + sender_buffer_size))) {
      55                 :           0 :         message = sender_buffer;
      56                 :           0 :         message_size = sender_buffer_size;
      57                 :             :     } else {
      58                 :        6292 :         if ((uint8_t *)request >=
      59         [ +  - ]:        6292 :             scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context)
      60                 :        6292 :             && (uint8_t *)request <
      61                 :       12584 :             scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context)
      62         [ +  + ]:        6292 :             + libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context)) {
      63                 :         468 :             message = scratch_buffer +
      64                 :         234 :                       libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context);
      65                 :         234 :             message_size = libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
      66                 :        6058 :         } else if ((uint8_t *)request >=
      67                 :       12116 :                    scratch_buffer +
      68         [ +  - ]:        6058 :                    libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context)
      69                 :        6058 :                    && (uint8_t *)request <
      70                 :             :                    scratch_buffer +
      71                 :        6058 :                    libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context) +
      72         [ +  - ]:        6058 :                    libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context)) {
      73                 :       12116 :             message = scratch_buffer +
      74                 :        6058 :                       libspdm_get_scratch_buffer_large_sender_receiver_offset(spdm_context);
      75                 :        6058 :             message_size = libspdm_get_scratch_buffer_large_sender_receiver_capacity(spdm_context);
      76                 :             :         }
      77                 :             :     }
      78                 :             :     #else /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
      79                 :             :     message = sender_buffer;
      80                 :             :     message_size = sender_buffer_size;
      81                 :             :     #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
      82                 :             : 
      83         [ +  + ]:        6292 :     if (session_id != NULL) {
      84                 :             :         /* For a secured message, copy the message from the sender buffer to the scratch buffer.
      85                 :             :          * transport_message is always in sender buffer. */
      86                 :             : 
      87                 :         612 :         libspdm_copy_mem (scratch_buffer + transport_header_size,
      88                 :             :                           scratch_buffer_size - transport_header_size,
      89                 :             :                           request, request_size);
      90                 :         612 :         request = scratch_buffer + transport_header_size;
      91                 :             :     }
      92                 :             : 
      93                 :             :     /* backup it to last_spdm_request, because the caller wants to compare it with response */
      94         [ +  + ]:        6292 :     if (((const spdm_message_header_t *)request)->request_response_code != SPDM_RESPOND_IF_READY
      95         [ +  + ]:        6222 :         && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_GET
      96         [ +  + ]:        6028 :         && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_SEND) {
      97                 :        5988 :         libspdm_copy_mem (context->last_spdm_request,
      98                 :        5988 :                           libspdm_get_scratch_buffer_last_spdm_request_capacity(context),
      99                 :             :                           request,
     100                 :             :                           request_size);
     101                 :        5988 :         context->last_spdm_request_size = request_size;
     102                 :             :     }
     103                 :             : 
     104                 :        6292 :     status = context->transport_encode_message(
     105                 :             :         context, session_id, is_app_message, true, request_size,
     106                 :             :         request, &message_size, (void **)&message);
     107         [ +  + ]:        6292 :     if (session_id != NULL) {
     108                 :             :         /* clean up secure message which was copied to scratch buffer */
     109                 :         612 :         libspdm_zero_mem(request, request_size);
     110                 :             :     }
     111         [ -  + ]:        6292 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     112                 :           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "transport_encode_message status - %x\n", status));
     113   [ #  #  #  # ]:           0 :         if ((session_id != NULL) &&
     114         [ #  # ]:           0 :             ((status == LIBSPDM_STATUS_SEQUENCE_NUMBER_OVERFLOW) ||
     115                 :             :              (status == LIBSPDM_STATUS_CRYPTO_ERROR))) {
     116                 :           0 :             libspdm_free_session_id(context, *session_id);
     117                 :             :         }
     118                 :           0 :         return status;
     119                 :             :     }
     120                 :             : 
     121                 :        6292 :     timeout = context->local_context.capability.rtt;
     122                 :        6292 :     status = context->send_message(context, message_size, message, timeout);
     123                 :             : 
     124         [ +  + ]:        6292 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     125         [ +  + ]:          62 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "libspdm_send_spdm_request[%x] status - %x\n",
     126                 :             :                        (session_id != NULL) ? *session_id : 0x0, status));
     127                 :             :     }
     128                 :             : 
     129                 :        6292 :     return status;
     130                 :             : }
     131                 :             : 
     132                 :        6196 : libspdm_return_t libspdm_receive_response(void *spdm_context, const uint32_t *session_id,
     133                 :             :                                           bool is_app_message,
     134                 :             :                                           size_t *response_size,
     135                 :             :                                           void **response)
     136                 :             : {
     137                 :             :     libspdm_context_t *context;
     138                 :             :     void *temp_session_context;
     139                 :             :     libspdm_return_t status;
     140                 :             :     uint8_t *message;
     141                 :             :     size_t message_size;
     142                 :             :     uint32_t *message_session_id;
     143                 :             :     uint32_t message_id;
     144                 :             :     bool is_message_app_message;
     145                 :             :     uint64_t timeout;
     146                 :             :     size_t transport_header_size;
     147                 :             :     uint8_t *scratch_buffer;
     148                 :             :     size_t scratch_buffer_size;
     149                 :             :     void *backup_response;
     150                 :             :     size_t backup_response_size;
     151                 :             :     bool reset_key_update;
     152                 :             :     bool result;
     153                 :             : 
     154                 :        6196 :     context = spdm_context;
     155                 :             : 
     156         [ +  + ]:        6196 :     if (context->crypto_request) {
     157                 :        5466 :         timeout = context->local_context.capability.rtt +
     158                 :        5466 :                   ((uint64_t)1 << context->connection_info.capability.ct_exponent);
     159                 :             :     } else {
     160                 :         730 :         timeout = context->local_context.capability.rtt + context->local_context.capability.st1;
     161                 :             :     }
     162                 :             : 
     163                 :        6196 :     message = *response;
     164                 :        6196 :     message_size = *response_size;
     165                 :        6196 :     status = context->receive_message(context, &message_size, (void **)&message, timeout);
     166         [ +  + ]:        6196 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     167         [ +  + ]:          56 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     168                 :             :                        "libspdm_receive_spdm_response[%x] status - %x\n",
     169                 :             :                        (session_id != NULL) ? *session_id : 0x0, status));
     170                 :          56 :         return status;
     171                 :             :     }
     172                 :             : 
     173                 :             :     /*
     174                 :             :      * The storage transport encoding, defined by DSP0286, does not indicate
     175                 :             :      * in the transport data whether the message is in a secure session. This is
     176                 :             :      * different to most other transport encodings, which includes session
     177                 :             :      * information in the encoding.
     178                 :             :      *
     179                 :             :      * As such, if the message is in a secure session (session_id != NULL), set
     180                 :             :      * message_session_id to be non-NULL to indicate to the lower layer
     181                 :             :      * that the message is in a secure session.
     182                 :             :      */
     183         [ +  + ]:        6140 :     if (session_id != NULL) {
     184                 :         582 :         message_session_id = &message_id;
     185                 :         582 :         message_id = *session_id;
     186                 :             :     } else {
     187                 :        5558 :         message_session_id = NULL;
     188                 :             :     }
     189                 :        6140 :     is_message_app_message = false;
     190                 :             : 
     191                 :             :     /* always use scratch buffer for the response.
     192                 :             :      * if it is secured message, this scratch buffer will be used.
     193                 :             :      * if it is normal message, the response ptr will point to receiver buffer. */
     194                 :        6140 :     transport_header_size = context->local_context.capability.transport_header_size;
     195                 :        6140 :     libspdm_get_scratch_buffer (context, (void **)&scratch_buffer, &scratch_buffer_size);
     196                 :             :     #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
     197                 :        6140 :     *response = scratch_buffer + libspdm_get_scratch_buffer_secure_message_offset() +
     198                 :             :                 transport_header_size;
     199                 :        6140 :     *response_size = libspdm_get_scratch_buffer_secure_message_capacity(context) -
     200                 :             :                      transport_header_size;
     201                 :             :     #else
     202                 :             :     *response = scratch_buffer + transport_header_size;
     203                 :             :     *response_size = scratch_buffer_size - transport_header_size;
     204                 :             :     #endif
     205                 :             : 
     206                 :        6140 :     backup_response = *response;
     207                 :        6140 :     backup_response_size = *response_size;
     208                 :             : 
     209                 :        6140 :     status = context->transport_decode_message(
     210                 :             :         context, &message_session_id, &is_message_app_message,
     211                 :             :         false, message_size, message, response_size, response);
     212                 :             : 
     213                 :        6140 :     reset_key_update = false;
     214                 :        6140 :     temp_session_context = NULL;
     215                 :             : 
     216         [ +  + ]:        6140 :     if (status == LIBSPDM_STATUS_SESSION_TRY_DISCARD_KEY_UPDATE) {
     217                 :             :         /* Failed to decode, but have backup keys. Try rolling back before aborting.
     218                 :             :          * message_session_id must be valid for libspdm to have attempted decryption. */
     219         [ -  + ]:          54 :         if (message_session_id == NULL) {
     220                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     221                 :             :         }
     222                 :          54 :         temp_session_context = libspdm_get_secured_message_context_via_session_id(
     223                 :             :             context, libspdm_read_uint32((const uint8_t *)message_session_id));
     224         [ -  + ]:          54 :         if (temp_session_context == NULL) {
     225                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     226                 :             :         }
     227                 :             : 
     228                 :          54 :         result = libspdm_activate_update_session_data_key(
     229                 :             :             temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_RESPONDER, false);
     230         [ -  + ]:          54 :         if (!result) {
     231                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     232                 :             :         }
     233                 :             : 
     234                 :             :         /* Retry decoding message with backup Requester key.
     235                 :             :          * Must reset some of the parameters in case they were modified */
     236         [ +  - ]:          54 :         if (session_id != NULL) {
     237                 :          54 :             libspdm_write_uint32((uint8_t *)message_session_id, *session_id);
     238                 :             :         } else {
     239                 :           0 :             message_session_id = NULL;
     240                 :             :         }
     241                 :          54 :         is_message_app_message = false;
     242                 :          54 :         *response = backup_response;
     243                 :          54 :         *response_size = backup_response_size;
     244                 :          54 :         status = context->transport_decode_message(
     245                 :             :             context, &message_session_id, &is_message_app_message,
     246                 :             :             false, message_size, message, response_size, response);
     247                 :             : 
     248                 :          54 :         reset_key_update = true;
     249                 :             :     }
     250                 :             : 
     251                 :             :     /*
     252                 :             :      * decoded_message may contain padding zeros due to transport layer alignment requirements.
     253                 :             :      * trim the decoded_message size to the maximum data_transfer_size.
     254                 :             :      */
     255                 :        6140 :     *response_size = LIBSPDM_MIN(*response_size, context->local_context.capability.data_transfer_size);
     256                 :             : 
     257         [ +  + ]:        6140 :     if (session_id != NULL) {
     258         [ +  + ]:         582 :         if (message_session_id == NULL) {
     259         [ +  - ]:           4 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     260                 :             :                            "libspdm_receive_spdm_response[%x] GetSessionId - NULL\n",
     261                 :             :                            (session_id != NULL) ? *session_id : 0x0));
     262                 :           4 :             goto error;
     263                 :             :         }
     264         [ -  + ]:         578 :         if (libspdm_read_uint32((const uint8_t *)message_session_id) != *session_id) {
     265         [ #  # ]:           0 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     266                 :             :                            "libspdm_receive_spdm_response[%x] GetSessionId - %x\n",
     267                 :             :                            (session_id != NULL) ? *session_id : 0x0,
     268                 :             :                            libspdm_read_uint32((const uint8_t *)message_session_id)));
     269                 :           0 :             goto error;
     270                 :             :         }
     271                 :             :     } else {
     272         [ -  + ]:        5558 :         if (message_session_id != NULL) {
     273         [ #  # ]:           0 :             LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     274                 :             :                            "libspdm_receive_spdm_response[%x] GetSessionId - %x\n",
     275                 :             :                            (session_id != NULL) ? *session_id : 0x0,
     276                 :             :                            libspdm_read_uint32((const uint8_t *)message_session_id)));
     277                 :           0 :             goto error;
     278                 :             :         }
     279                 :             :     }
     280                 :             : 
     281   [ +  +  +  - ]:        6136 :     if ((is_app_message && !is_message_app_message) ||
     282   [ +  +  -  + ]:        6136 :         (!is_app_message && is_message_app_message)) {
     283         [ #  # ]:           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     284                 :             :                        "libspdm_receive_spdm_response[%x] app_message mismatch\n",
     285                 :             :                        (session_id != NULL) ? *session_id : 0x0));
     286                 :           0 :         goto error;
     287                 :             :     }
     288                 :             : 
     289         [ -  + ]:        6136 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     290         [ #  # ]:           0 :         if ((session_id != NULL) &&
     291         [ #  # ]:           0 :             (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR)) {
     292                 :           0 :             libspdm_free_session_id(context, *session_id);
     293                 :             :         }
     294         [ #  # ]:           0 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     295                 :             :                        "libspdm_receive_spdm_response[%x] status - %x\n",
     296                 :             :                        (session_id != NULL) ? *session_id : 0x0, status));
     297         [ +  + ]:        6136 :     } else if (is_app_message) {
     298         [ +  - ]:           6 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     299                 :             :                        "libspdm_receive_spdm_response[%x] app msg, size (0x%zx): \n",
     300                 :             :                        (session_id != NULL) ? *session_id : 0x0, *response_size));
     301                 :           6 :         LIBSPDM_INTERNAL_DUMP_HEX(*response, *response_size);
     302                 :             :     } else {
     303         [ +  + ]:        6130 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
     304                 :             :                        "libspdm_receive_spdm_response[%x] msg %s(0x%x), size (0x%zx): \n",
     305                 :             :                        (session_id != NULL) ? *session_id : 0x0,
     306                 :             :                        libspdm_get_code_str(((spdm_message_header_t *)*response)->
     307                 :             :                                             request_response_code),
     308                 :             :                        ((spdm_message_header_t *)*response)->request_response_code,
     309                 :             :                        *response_size));
     310                 :        6130 :         LIBSPDM_INTERNAL_DUMP_HEX(*response, *response_size);
     311                 :             :     }
     312                 :             : 
     313                 :             :     /* Handle special case:
     314                 :             :      * If the Responder returns RESPONSE_NOT_READY error to KEY_UPDATE, the Requester needs
     315                 :             :      * to activate backup key to parse the error. Then later the Responder will return SUCCESS,
     316                 :             :      * the Requester needs new key. So restore the environment by
     317                 :             :      * libspdm_create_update_session_data_key() again.*/
     318         [ +  + ]:        6136 :     if (reset_key_update) {
     319                 :             :         /* temp_session_context and message_session_id must necessarily
     320                 :             :          * be valid for the code to reach here. */
     321   [ +  -  -  + ]:          54 :         if (temp_session_context == NULL || message_session_id == NULL) {
     322                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     323                 :             :         }
     324                 :          54 :         result = libspdm_create_update_session_data_key(
     325                 :             :             temp_session_context, LIBSPDM_KEY_UPDATE_ACTION_RESPONDER);
     326         [ -  + ]:          54 :         if (!result) {
     327                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     328                 :             :         }
     329                 :             :     }
     330                 :             : 
     331                 :        6136 :     return status;
     332                 :             : 
     333                 :           4 : error:
     334         [ -  + ]:           4 :     if (context->last_spdm_error.error_code == SPDM_ERROR_CODE_DECRYPT_ERROR) {
     335                 :           0 :         return LIBSPDM_STATUS_SESSION_MSG_ERROR;
     336                 :             :     } else {
     337                 :           4 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     338                 :             :     }
     339                 :             : }
     340                 :             : 
     341                 :             : #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
     342                 :          32 : static libspdm_return_t libspdm_handle_large_request(
     343                 :             :     libspdm_context_t *spdm_context,
     344                 :             :     const uint32_t *session_id,
     345                 :             :     size_t request_size, void *request)
     346                 :             : {
     347                 :             :     libspdm_return_t status;
     348                 :             : 
     349                 :             :     spdm_chunk_send_request_t *spdm_request;
     350                 :             :     spdm_chunk_send_request_14_t *spdm_request_14;
     351                 :             :     size_t spdm_request_size;
     352                 :             :     spdm_chunk_send_ack_response_t *spdm_response;
     353                 :             :     spdm_chunk_send_ack_response_14_t *spdm_response_14;
     354                 :             :     size_t response_header_size;
     355                 :             :     uint8_t *message;
     356                 :             :     size_t message_size;
     357                 :             :     void *response;
     358                 :             :     size_t response_size;
     359                 :             :     size_t transport_header_size;
     360                 :             : 
     361                 :             :     uint8_t *scratch_buffer;
     362                 :             :     size_t scratch_buffer_size;
     363                 :             : 
     364                 :             :     uint8_t *chunk_ptr;
     365                 :             :     size_t copy_size;
     366                 :             :     libspdm_chunk_info_t *send_info;
     367                 :             :     uint32_t min_data_transfer_size;
     368                 :             :     uint64_t max_chunk_data_transfer_size;
     369                 :             :     spdm_error_response_t *spdm_error;
     370                 :             : 
     371         [ -  + ]:          32 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_12) {
     372                 :           0 :         return LIBSPDM_STATUS_UNSUPPORTED_CAP;
     373                 :             :     }
     374                 :             : 
     375                 :             :     /* Fail if requester or responder does not support chunk cap */
     376         [ -  + ]:          32 :     if (!libspdm_is_capabilities_flag_supported(
     377                 :             :             spdm_context, true,
     378                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
     379                 :             :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
     380                 :           0 :         return LIBSPDM_STATUS_ERROR_PEER;
     381                 :             :     }
     382                 :             : 
     383                 :             :     /* Fail if exceed max chunks */
     384                 :          32 :     min_data_transfer_size = LIBSPDM_MIN(
     385                 :             :         spdm_context->connection_info.capability.data_transfer_size,
     386                 :             :         spdm_context->local_context.capability.sender_data_transfer_size);
     387                 :             : 
     388         [ +  + ]:          32 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
     389                 :             :         /* chunk seq no wrap not considered in spdm 1.4+ */
     390                 :             : 
     391                 :          28 :         max_chunk_data_transfer_size =
     392                 :          28 :             ((uint64_t) min_data_transfer_size - sizeof(spdm_chunk_send_request_t)) * 65536 -
     393                 :             :             sizeof(uint32_t);
     394                 :             :         /* max_spdm_msg_size is already checked in caller */
     395                 :             : 
     396         [ +  + ]:          28 :         if (request_size > max_chunk_data_transfer_size) {
     397                 :           2 :             return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
     398                 :             :         }
     399                 :             :     }
     400                 :             :     /* now acquire the sender buffer */
     401                 :          30 :     transport_header_size = spdm_context->local_context.capability.transport_header_size;
     402                 :             : 
     403                 :          30 :     libspdm_get_scratch_buffer(spdm_context, (void **)&scratch_buffer, &scratch_buffer_size);
     404                 :             : 
     405                 :             :     /* Temporary send/receive buffers for chunking are in the scratch space */
     406                 :          30 :     message = scratch_buffer + libspdm_get_scratch_buffer_sender_receiver_offset(spdm_context);
     407                 :          30 :     message_size = libspdm_get_scratch_buffer_sender_receiver_capacity(spdm_context);
     408                 :             : 
     409                 :          30 :     send_info = &spdm_context->chunk_context.send;
     410                 :          30 :     send_info->chunk_in_use = true;
     411                 :             : 
     412                 :             :     /* The first section of the scratch
     413                 :             :      * buffer may be used for other purposes. Use only after that section. */
     414                 :          60 :     send_info->large_message = scratch_buffer +
     415                 :          30 :                                libspdm_get_scratch_buffer_large_message_offset(spdm_context);
     416                 :          30 :     send_info->large_message_capacity =
     417                 :          30 :         libspdm_get_scratch_buffer_large_message_capacity(spdm_context);
     418                 :             : 
     419                 :          30 :     libspdm_zero_mem(send_info->large_message, send_info->large_message_capacity);
     420                 :          30 :     libspdm_copy_mem(send_info->large_message, send_info->large_message_capacity,
     421                 :             :                      request, request_size);
     422                 :             : 
     423                 :          30 :     send_info->large_message_size = request_size;
     424                 :          30 :     send_info->chunk_bytes_transferred = 0;
     425                 :          30 :     send_info->chunk_seq_no = 0;
     426                 :          30 :     request = NULL; /* Invalidate to prevent accidental use. */
     427                 :          30 :     request_size = 0;
     428                 :             : 
     429                 :             :     do {
     430                 :          40 :         LIBSPDM_ASSERT(send_info->large_message_capacity >= transport_header_size);
     431                 :          40 :         spdm_request = (spdm_chunk_send_request_t *)((uint8_t *)message + transport_header_size);
     432                 :          40 :         spdm_request_size = message_size - transport_header_size;
     433                 :             : 
     434                 :          40 :         spdm_request->header.spdm_version = libspdm_get_connection_version(spdm_context);
     435                 :          40 :         spdm_request->header.request_response_code = SPDM_CHUNK_SEND;
     436                 :          40 :         spdm_request->header.param1 = 0;
     437                 :          40 :         spdm_request->header.param2 = send_info->chunk_handle;
     438                 :             : 
     439         [ +  + ]:          40 :         if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
     440                 :          32 :             spdm_request->chunk_seq_no = (uint16_t) send_info->chunk_seq_no;
     441                 :          32 :             spdm_request->reserved = 0;
     442                 :          32 :             chunk_ptr = (uint8_t *)(spdm_request + 1);
     443                 :             :         } else {
     444                 :           8 :             spdm_request_14 = (spdm_chunk_send_request_14_t *)spdm_request;
     445                 :           8 :             spdm_request_14->chunk_seq_no = send_info->chunk_seq_no;
     446                 :           8 :             chunk_ptr = (uint8_t *)(spdm_request_14 + 1);
     447                 :             :         }
     448                 :             : 
     449                 :             :         LIBSPDM_ASSERT(sizeof(spdm_chunk_send_request_t) == sizeof(spdm_chunk_send_request_14_t));
     450                 :          40 :         if ((min_data_transfer_size - sizeof(spdm_chunk_send_request_t)) <
     451         [ +  + ]:          40 :             (send_info->large_message_size - send_info->chunk_bytes_transferred)) {
     452                 :          30 :             copy_size = min_data_transfer_size - sizeof(spdm_chunk_send_request_t);
     453                 :             :         } else {
     454                 :          10 :             copy_size = (send_info->large_message_size - send_info->chunk_bytes_transferred);
     455                 :             :         }
     456                 :             : 
     457         [ +  + ]:          40 :         if (send_info->chunk_seq_no == 0) {
     458                 :          30 :             libspdm_write_uint32((uint8_t *)(spdm_request + 1),
     459                 :          30 :                                  (uint32_t)send_info->large_message_size);
     460                 :          30 :             chunk_ptr += sizeof(uint32_t);
     461                 :          30 :             copy_size -= sizeof(uint32_t);
     462                 :             :         }
     463                 :             : 
     464                 :          40 :         spdm_request->chunk_size = (uint32_t)copy_size;
     465                 :             : 
     466                 :          40 :         libspdm_copy_mem(
     467                 :          40 :             chunk_ptr, spdm_request_size - ((uint8_t *)spdm_request - (uint8_t *)message),
     468                 :          40 :             (uint8_t *)send_info->large_message + send_info->chunk_bytes_transferred, copy_size);
     469                 :             : 
     470                 :          40 :         send_info->chunk_bytes_transferred += copy_size;
     471         [ +  + ]:          40 :         if (send_info->chunk_bytes_transferred >= send_info->large_message_size) {
     472                 :          10 :             spdm_request->header.param1 |= SPDM_CHUNK_SEND_REQUEST_ATTRIBUTE_LAST_CHUNK;
     473                 :             :         }
     474                 :             : 
     475                 :          40 :         spdm_request_size = (chunk_ptr + copy_size) - (uint8_t *)spdm_request;
     476                 :          40 :         status = libspdm_send_request(
     477                 :             :             spdm_context, session_id, false,
     478                 :             :             spdm_request_size, spdm_request);
     479                 :             : 
     480         [ +  + ]:          40 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     481                 :           2 :             break;
     482                 :             :         }
     483                 :             : 
     484                 :          38 :         response = message;
     485                 :          38 :         response_size = message_size;
     486                 :             : 
     487                 :          38 :         libspdm_zero_mem(response, response_size);
     488                 :             : 
     489                 :          38 :         status = libspdm_receive_response(
     490                 :             :             spdm_context, session_id, false,
     491                 :             :             &response_size, &response);
     492                 :             : 
     493         [ +  + ]:          38 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     494                 :           2 :             break;
     495                 :             :         }
     496                 :          36 :         spdm_response = (void *)(response);
     497                 :             : 
     498         [ -  + ]:          36 :         if (response_size < sizeof(spdm_message_header_t)) {
     499                 :           0 :             status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     500                 :           0 :             break;
     501                 :             :         }
     502                 :             : 
     503         [ +  + ]:          36 :         if (spdm_response->header.request_response_code == SPDM_ERROR
     504         [ +  + ]:           6 :             && spdm_response->header.param1 != SPDM_ERROR_CODE_LARGE_RESPONSE) {
     505                 :           4 :             status = libspdm_handle_simple_error_response(spdm_context,
     506                 :           4 :                                                           spdm_response->header.param1);
     507                 :           4 :             break;
     508                 :             :         }
     509                 :             : 
     510         [ +  + ]:          32 :         if (spdm_response->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
     511                 :           2 :             status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     512                 :           2 :             break;
     513                 :             :         }
     514                 :             : 
     515         [ +  + ]:          30 :         if (spdm_response->header.request_response_code == SPDM_ERROR
     516         [ +  - ]:           2 :             && spdm_response->header.param1 == SPDM_ERROR_CODE_LARGE_RESPONSE) {
     517                 :             : 
     518                 :             :             /* It is possible that the CHUNK_SEND_ACK + chunk response is larger
     519                 :             :              * than the DATA_TRANSFER_SIZE. In this case an ERROR_LARGE_RESPONSE
     520                 :             :              * is returned directly in the response buffer rather than part of
     521                 :             :              * the CHUNK_SEND_ACK. Store this error response in scratch buffer
     522                 :             :              * to be handled when reading response. Also note that in this case
     523                 :             :              * of large response, the CHUNK_SEND_ACK portion is not sent.
     524                 :             :              * Only the response portion that requires the CHUNK_GET is sent */
     525         [ +  - ]:           2 :             if (response_size < send_info->large_message_capacity) {
     526                 :           2 :                 libspdm_copy_mem(
     527                 :             :                     send_info->large_message, send_info->large_message_capacity,
     528                 :             :                     spdm_response, response_size);
     529                 :           2 :                 send_info->large_message_size = response_size;
     530                 :           2 :                 break;
     531                 :             :             } else {
     532                 :           0 :                 status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     533                 :           0 :                 break;
     534                 :             :             }
     535                 :             :         } else {
     536         [ -  + ]:          28 :             if (spdm_response->header.request_response_code != SPDM_CHUNK_SEND_ACK) {
     537                 :           0 :                 status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     538                 :           0 :                 break;
     539                 :             :             }
     540                 :             : 
     541         [ +  + ]:          28 :             if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
     542                 :          22 :                 response_header_size = sizeof(spdm_chunk_send_ack_response_t);
     543                 :             :             } else {
     544                 :           6 :                 response_header_size = sizeof(spdm_chunk_send_ack_response_14_t);
     545                 :           6 :                 spdm_response_14 = (spdm_chunk_send_ack_response_14_t *)spdm_response;
     546                 :             :             }
     547                 :             : 
     548         [ +  + ]:          28 :             if (response_size < response_header_size) {
     549                 :           2 :                 status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     550                 :           2 :                 break;
     551                 :             :             }
     552                 :          26 :             if (spdm_response->header.param1
     553         [ +  + ]:          26 :                 & SPDM_CHUNK_SEND_ACK_RESPONSE_ATTRIBUTE_EARLY_ERROR_DETECTED) {
     554                 :             : 
     555                 :           4 :                 spdm_error = (spdm_error_response_t *)((uint8_t *)spdm_response + response_header_size);
     556         [ -  + ]:           4 :                 if (response_size < (response_header_size +
     557                 :             :                                      sizeof(spdm_error_response_t))) {
     558                 :           0 :                     status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     559                 :           0 :                     break;
     560                 :             :                 }
     561         [ +  - ]:           4 :                 if ((spdm_error->header.spdm_version !=
     562                 :           4 :                      libspdm_get_connection_version(spdm_context)) ||
     563         [ -  + ]:           4 :                     (spdm_error->header.request_response_code != SPDM_ERROR)) {
     564                 :           0 :                     status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     565                 :           0 :                     break;
     566                 :             :                 }
     567         [ +  + ]:           4 :                 if (spdm_error->header.param1 == SPDM_ERROR_CODE_LARGE_RESPONSE) {
     568                 :           2 :                     status = LIBSPDM_STATUS_ERROR_PEER;
     569                 :           2 :                     break;
     570                 :             :                 }
     571                 :             : 
     572                 :             :                 /* Store the error response in scratch buffer to be read by
     573                 :             :                  * libspdm_receive_spdm_response and returned to its caller
     574                 :             :                  * and handled in the error response handling flow */
     575                 :           2 :                 libspdm_copy_mem(
     576                 :             :                     send_info->large_message,
     577                 :             :                     send_info->large_message_capacity,
     578                 :             :                     (uint8_t *)spdm_response + response_header_size,
     579                 :             :                     response_size - response_header_size);
     580                 :             : 
     581                 :           2 :                 send_info->large_message_size =
     582                 :           2 :                     (response_size - response_header_size);
     583                 :           2 :                 status = LIBSPDM_STATUS_SUCCESS;
     584                 :           2 :                 break;
     585                 :             :             }
     586         [ +  + ]:          22 :             if (spdm_response->header.param2 != send_info->chunk_handle) {
     587                 :           2 :                 status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     588                 :           2 :                 break;
     589                 :             :             }
     590         [ +  + ]:          20 :             if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_14) {
     591         [ +  + ]:          14 :                 if (send_info->chunk_seq_no != spdm_response->chunk_seq_no) {
     592                 :           2 :                     status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     593                 :           2 :                     break;
     594                 :             :                 }
     595                 :             :             } else {
     596         [ -  + ]:           6 :                 if (send_info->chunk_seq_no != spdm_response_14->chunk_seq_no) {
     597                 :           0 :                     status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     598                 :           0 :                     break;
     599                 :             :                 }
     600                 :             :             }
     601                 :             : 
     602                 :          18 :             chunk_ptr = (uint8_t *)spdm_response + response_header_size;
     603                 :          18 :             send_info->chunk_seq_no++;
     604                 :             : 
     605         [ +  + ]:          18 :             if (send_info->chunk_bytes_transferred >= send_info->large_message_size) {
     606                 :             :                 /* All bytes have been transferred. Store response in scratch buffer
     607                 :             :                  * to be read by libspdm_receive_spdm_response */
     608                 :           8 :                 libspdm_copy_mem(
     609                 :             :                     send_info->large_message, send_info->large_message_capacity,
     610                 :             :                     chunk_ptr, response_size - response_header_size);
     611                 :           8 :                 send_info->large_message_size =
     612                 :           8 :                     (response_size - response_header_size);
     613                 :           8 :                 break;
     614                 :             :             }
     615                 :             :         }
     616                 :             : 
     617                 :          10 :     } while (LIBSPDM_STATUS_IS_SUCCESS(status)
     618   [ +  -  +  - ]:          10 :              && send_info->chunk_bytes_transferred < send_info->large_message_size);
     619                 :             : 
     620         [ +  + ]:          30 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     621                 :          18 :         libspdm_zero_mem(send_info->large_message, send_info->large_message_capacity);
     622                 :          18 :         send_info->chunk_in_use = false;
     623                 :          18 :         send_info->chunk_handle++; /* Implicit wrap-around*/
     624                 :          18 :         send_info->chunk_seq_no = 0;
     625                 :          18 :         send_info->chunk_bytes_transferred = 0;
     626                 :          18 :         send_info->large_message = NULL;
     627                 :          18 :         send_info->large_message_size = 0;
     628                 :          18 :         send_info->large_message_capacity = 0;
     629                 :             :     }
     630                 :             : 
     631                 :          30 :     return status;
     632                 :             : }
     633                 :             : #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
     634                 :             : 
     635                 :        6276 : libspdm_return_t libspdm_send_spdm_request(libspdm_context_t *spdm_context,
     636                 :             :                                            const uint32_t *session_id,
     637                 :             :                                            size_t request_size, void *request)
     638                 :             : {
     639                 :             :     libspdm_session_info_t *session_info;
     640                 :             :     libspdm_session_state_t session_state;
     641                 :             :     libspdm_return_t status;
     642                 :             :     #if LIBSPDM_ENABLE_MSG_LOG
     643                 :             :     size_t msg_log_size;
     644                 :             :     #endif /* LIBSPDM_ENABLE_MSG_LOG */
     645                 :             : 
     646                 :             :     /* If chunking is not supported then message must fit in both the send buffer and the receive
     647                 :             :      * buffer. */
     648         [ +  + ]:        6276 :     if (!libspdm_is_capabilities_flag_supported(
     649                 :             :             spdm_context, true,
     650                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CHUNK_CAP,
     651                 :             :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHUNK_CAP)) {
     652         [ +  + ]:        5990 :         if ((spdm_context->connection_info.capability.data_transfer_size != 0) &&
     653         [ -  + ]:          36 :             (request_size > spdm_context->connection_info.capability.data_transfer_size)) {
     654                 :           0 :             return LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL;
     655                 :             :         }
     656         [ +  - ]:        5990 :         if ((spdm_context->local_context.capability.sender_data_transfer_size != 0) &&
     657         [ -  + ]:        5990 :             (request_size > spdm_context->local_context.capability.sender_data_transfer_size)) {
     658                 :           0 :             return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
     659                 :             :         }
     660                 :             :     }
     661                 :             : 
     662   [ +  +  +  + ]:        6960 :     if ((session_id != NULL) &&
     663                 :         684 :         libspdm_is_capabilities_flag_supported(
     664                 :             :             spdm_context, true,
     665                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
     666                 :             :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
     667                 :          82 :         session_info = libspdm_get_session_info_via_session_id(spdm_context, *session_id);
     668                 :          82 :         LIBSPDM_ASSERT(session_info != NULL);
     669         [ -  + ]:          82 :         if (session_info == NULL) {
     670                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     671                 :             :         }
     672                 :          82 :         session_state = libspdm_secured_message_get_session_state(
     673                 :             :             session_info->secured_message_context);
     674   [ +  -  +  - ]:          82 :         if ((session_state == LIBSPDM_SESSION_STATE_HANDSHAKING) && !session_info->use_psk) {
     675                 :          82 :             session_id = NULL;
     676                 :             :         }
     677                 :             :     }
     678                 :             : 
     679         [ +  + ]:        6276 :     if ((spdm_context->connection_info.capability.max_spdm_msg_size != 0) &&
     680         [ +  + ]:         168 :         (request_size > spdm_context->connection_info.capability.max_spdm_msg_size)) {
     681                 :           2 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "request_size > rsp max_spdm_msg_size\n"));
     682                 :           2 :         return LIBSPDM_STATUS_PEER_BUFFER_TOO_SMALL;
     683                 :             :     }
     684                 :        6274 :     LIBSPDM_ASSERT (request_size <= spdm_context->local_context.capability.max_spdm_msg_size);
     685                 :             : 
     686                 :             :     #if LIBSPDM_ENABLE_MSG_LOG
     687                 :             :     /* First save the size of the message log buffer. If there is an error it will be reverted. */
     688                 :        6274 :     msg_log_size = libspdm_get_msg_log_size(spdm_context);
     689                 :        6274 :     libspdm_append_msg_log(spdm_context, request, request_size);
     690                 :             :     #endif /* LIBSPDM_ENABLE_MSG_LOG */
     691                 :             : 
     692                 :             :     /* large SPDM message is the SPDM message whose size is greater than the DataTransferSize of the receiving
     693                 :             :      * SPDM endpoint or greater than the transmit buffer size of the sending SPDM endpoint */
     694         [ +  + ]:        6274 :     if (((const spdm_message_header_t *)request)->request_response_code != SPDM_GET_VERSION
     695         [ +  + ]:        6100 :         && ((const spdm_message_header_t *)request)->request_response_code != SPDM_GET_CAPABILITIES
     696         [ +  + ]:        5840 :         && ((spdm_context->connection_info.capability.data_transfer_size != 0 &&
     697         [ +  + ]:         122 :              request_size > spdm_context->connection_info.capability.data_transfer_size) ||
     698         [ +  - ]:        5810 :             (spdm_context->local_context.capability.sender_data_transfer_size != 0 &&
     699         [ +  + ]:        5810 :              request_size > spdm_context->local_context.capability.sender_data_transfer_size))) {
     700                 :             : 
     701                 :             :         #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
     702                 :             :         /* libspdm_send_request is not called with the original request in this flow.
     703                 :             :          * This leads to the last_spdm_request field not having the original request value.
     704                 :             :          * The caller assumes the request has been copied to last_spdm_request,
     705                 :             :          * so that it can compare last_spdm_request's fields with response fields
     706                 :             :          * Therefore the request must be copied to last_spdm_request here. */
     707                 :             : 
     708         [ +  - ]:          32 :         if (((const spdm_message_header_t *)request)->request_response_code != SPDM_RESPOND_IF_READY
     709         [ +  - ]:          32 :             && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_GET
     710         [ +  - ]:          32 :             && ((const spdm_message_header_t *)request)->request_response_code != SPDM_CHUNK_SEND) {
     711                 :          32 :             libspdm_copy_mem(
     712                 :             :                 spdm_context->last_spdm_request,
     713                 :          32 :                 libspdm_get_scratch_buffer_last_spdm_request_capacity(spdm_context),
     714                 :             :                 request, request_size);
     715                 :          32 :             spdm_context->last_spdm_request_size = request_size;
     716                 :             :         }
     717                 :             : 
     718                 :          32 :         status = libspdm_handle_large_request(
     719                 :             :             spdm_context, session_id, request_size, request);
     720                 :             :         #else  /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP*/
     721                 :             :         status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
     722                 :             :         #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP*/
     723                 :             :     } else {
     724                 :        6242 :         status = libspdm_send_request(spdm_context, session_id, false, request_size, request);
     725                 :             :     }
     726                 :             : 
     727                 :             :     #if LIBSPDM_ENABLE_MSG_LOG
     728                 :             :     /* If there is an error in sending the request then revert the request in the message log. */
     729         [ +  + ]:        6274 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     730                 :          80 :         spdm_context->msg_log.buffer_size = msg_log_size;
     731                 :             :     }
     732                 :             :     #endif /* LIBSPDM_ENABLE_MSG_LOG */
     733                 :             : 
     734                 :        6274 :     return status;
     735                 :             : }
     736                 :             : 
     737                 :        6162 : libspdm_return_t libspdm_receive_spdm_response(libspdm_context_t *spdm_context,
     738                 :             :                                                const uint32_t *session_id,
     739                 :             :                                                size_t *response_size,
     740                 :             :                                                void **response)
     741                 :             : {
     742                 :             :     libspdm_return_t status;
     743                 :             :     libspdm_session_info_t *session_info;
     744                 :             :     libspdm_session_state_t session_state;
     745                 :             : 
     746                 :             :     #if LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP
     747                 :             :     spdm_message_header_t *spdm_response;
     748                 :             :     size_t response_capacity;
     749                 :             :     libspdm_chunk_info_t *send_info;
     750                 :        6162 :     bool response_from_chunk = false;
     751                 :             :     #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
     752                 :             : 
     753   [ +  +  +  + ]:        6824 :     if ((session_id != NULL) &&
     754                 :         662 :         libspdm_is_capabilities_flag_supported(
     755                 :             :             spdm_context, true,
     756                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP,
     757                 :             :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP)) {
     758                 :          80 :         session_info = libspdm_get_session_info_via_session_id(spdm_context, *session_id);
     759                 :          80 :         LIBSPDM_ASSERT(session_info != NULL);
     760         [ -  + ]:          80 :         if (session_info == NULL) {
     761                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     762                 :             :         }
     763                 :          80 :         session_state = libspdm_secured_message_get_session_state(
     764                 :             :             session_info->secured_message_context);
     765   [ +  -  +  - ]:          80 :         if ((session_state == LIBSPDM_SESSION_STATE_HANDSHAKING) && !session_info->use_psk) {
     766                 :          80 :             session_id = NULL;
     767                 :             :         }
     768                 :             :     }
     769                 :             : 
     770                 :             :     #if !(LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP)
     771                 :             :     status = libspdm_receive_response(spdm_context, session_id, false, response_size, response);
     772                 :             :     #else /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
     773                 :        6162 :     send_info = &spdm_context->chunk_context.send;
     774         [ +  + ]:        6162 :     if (send_info->chunk_in_use) {
     775                 :          12 :         libspdm_copy_mem(*response, *response_size,
     776                 :          12 :                          send_info->large_message, send_info->large_message_size);
     777                 :          12 :         *response_size = send_info->large_message_size;
     778                 :          12 :         response_capacity = send_info->large_message_capacity;
     779                 :          12 :         response_from_chunk = true;
     780                 :             : 
     781                 :             :         /* This response may either be an actual response or ERROR_LARGE_RESPONSE,
     782                 :             :          * the latter which should be handled in the large response handler. */
     783                 :          12 :         libspdm_zero_mem(send_info->large_message, send_info->large_message_capacity);
     784                 :          12 :         send_info->chunk_in_use = false;
     785                 :          12 :         send_info->chunk_handle++; /* Implicit wrap-around*/
     786                 :          12 :         send_info->chunk_seq_no = 0;
     787                 :          12 :         send_info->chunk_bytes_transferred = 0;
     788                 :          12 :         send_info->large_message = NULL;
     789                 :          12 :         send_info->large_message_size = 0;
     790                 :          12 :         send_info->large_message_capacity = 0;
     791                 :          12 :         status = LIBSPDM_STATUS_SUCCESS;
     792                 :             :     } else {
     793                 :        6150 :         response_capacity = *response_size;
     794                 :        6150 :         status = libspdm_receive_response(spdm_context, session_id, false, response_size, response);
     795         [ +  + ]:        6150 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     796                 :          56 :             goto receive_done;
     797                 :             :         }
     798                 :             :     }
     799                 :             : 
     800                 :        6106 :     spdm_response = (spdm_message_header_t *)(*response);
     801                 :             : 
     802         [ -  + ]:        6106 :     if (*response_size < sizeof(spdm_message_header_t)) {
     803                 :           0 :         status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     804                 :           0 :         goto receive_done;
     805                 :             :     }
     806                 :             : 
     807         [ +  + ]:        6106 :     if (spdm_response->request_response_code == SPDM_ERROR
     808         [ +  + ]:        1094 :         && spdm_response->param1 == SPDM_ERROR_CODE_LARGE_RESPONSE
     809         [ +  + ]:          26 :         && !spdm_context->chunk_context.get.chunk_in_use) {
     810                 :          24 :         status = libspdm_handle_error_large_response(
     811                 :             :             spdm_context, session_id,
     812                 :             :             response_size, (void *)spdm_response, response_capacity, response_from_chunk);
     813                 :             : 
     814         [ +  + ]:          24 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     815                 :          10 :             goto receive_done;
     816                 :             :         }
     817                 :             : 
     818         [ -  + ]:          14 :         if (*response_size < sizeof(spdm_message_header_t)) {
     819                 :           0 :             status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     820                 :           0 :             goto receive_done;
     821                 :             :         }
     822                 :             : 
     823                 :             :         /* Per the spec, SPDM_VERSION shall not be chunked
     824                 :             :          * and should be an unexpected error. */
     825         [ -  + ]:          14 :         if (spdm_response->request_response_code == SPDM_VERSION) {
     826                 :           0 :             status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     827                 :           0 :             goto receive_done;
     828                 :             :         }
     829                 :             : 
     830                 :             :         /* Per the spec, SPDM_CAPABILITIES shall not be chunked unless
     831                 :             :          * the response includes Supported Algorithms. */
     832         [ +  + ]:          14 :         if (spdm_response->request_response_code == SPDM_CAPABILITIES) {
     833         [ +  - ]:           2 :             if ((spdm_response->param1 &
     834                 :             :                  SPDM_CAPABILITIES_RESPONSE_PARAM1_SUPPORTED_ALGORITHMS) == 0) {
     835                 :           0 :                 status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     836                 :           0 :                 goto receive_done;
     837                 :             :             }
     838                 :             :         }
     839                 :             :     }
     840                 :             : 
     841                 :        6096 : receive_done:
     842                 :             :     #endif /* LIBSPDM_ENABLE_CAPABILITY_CHUNK_CAP */
     843                 :             : 
     844                 :        6162 :     return status;
     845                 :             : }
        

Generated by: LCOV version 2.0-1