LCOV - code coverage report
Current view: top level - library/spdm_requester_lib - libspdm_req_send_receive.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.0 % 337 283
Test Date: 2025-07-27 08:10:33 Functions: 100.0 % 5 5

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

Generated by: LCOV version 2.0-1