Code coverage report for DMTF/libspdm
Current view: top level - library/spdm_responder_lib - libspdm_rsp_encap_get_certificate.c (source / functions) Coverage Total Hit
Test: b3f7d2e00db5b07cf17ccc0075121f9c08f2965b Lines: 89.0 % 219 195
Test Date: 2026-11-29 08:33:56 Functions: 100.0 % 5 5
Branches: 80.3 % 142 114

             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_responder_lib.h"
       8                 :             : 
       9                 :             : #if (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_SEND_GET_CERTIFICATE_SUPPORT)
      10                 :             : 
      11                 :          70 : libspdm_return_t libspdm_get_encap_request_get_certificate(void *context,
      12                 :             :                                                            const uint32_t *session_id,
      13                 :             :                                                            uint8_t req_slot_id,
      14                 :             :                                                            size_t cert_chain_size,
      15                 :             :                                                            void *cert_chain,
      16                 :             :                                                            size_t *encap_request_size,
      17                 :             :                                                            void *encap_request)
      18                 :             : {
      19                 :             :     libspdm_context_t *spdm_context;
      20                 :             :     libspdm_encap_context_t *encap_context;
      21                 :             : 
      22                 :          70 :     spdm_context = context;
      23                 :             : 
      24   [ +  +  +  + ]:          70 :     if ((cert_chain == NULL) || (cert_chain_size == 0)) {
      25                 :           4 :         return LIBSPDM_STATUS_INVALID_PARAMETER;
      26                 :             :     }
      27                 :             : 
      28         [ +  + ]:          66 :     if (req_slot_id >= SPDM_MAX_SLOT_COUNT) {
      29                 :             :         /* The slot indexes per-slot state of SPDM_MAX_SLOT_COUNT entries, in which the
      30                 :             :          * Requester's certificate chain is recorded once retrieved. GET_CERTIFICATE has no slot
      31                 :             :          * value that designates a provisioned public key. */
      32                 :           6 :         return LIBSPDM_STATUS_INVALID_PARAMETER;
      33                 :             :     }
      34                 :             : 
      35                 :          60 :     encap_context = libspdm_get_encap_context(spdm_context, session_id);
      36         [ -  + ]:          60 :     if (encap_context == NULL) {
      37                 :             :         /* session_id does not refer to an existing session. */
      38                 :           0 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      39                 :             :     }
      40                 :             : 
      41                 :             :     /* This starts a fresh retrieval of the Requester's certificate chain. libspdm issues the
      42                 :             :      * remaining requests itself, accumulating into this buffer. */
      43                 :          60 :     encap_context->payload_buffer = cert_chain;
      44                 :          60 :     encap_context->payload_buffer_max_size = cert_chain_size;
      45                 :          60 :     encap_context->payload_buffer_size = 0;
      46                 :             : 
      47                 :          60 :     return libspdm_get_encap_request_get_certificate_continue(
      48                 :             :         spdm_context, session_id, req_slot_id, encap_request_size, encap_request);
      49                 :             : }
      50                 :             : 
      51                 :         112 : libspdm_return_t libspdm_get_encap_request_get_certificate_continue(
      52                 :             :     libspdm_context_t *context,
      53                 :             :     const uint32_t *session_id,
      54                 :             :     uint8_t req_slot_id,
      55                 :             :     size_t *encap_request_size,
      56                 :             :     void *encap_request)
      57                 :             : {
      58                 :             :     libspdm_encap_context_t *encap_context;
      59                 :             :     libspdm_context_t *spdm_context;
      60                 :             :     spdm_get_certificate_large_request_t *spdm_request;
      61                 :             :     libspdm_return_t status;
      62                 :             :     uint32_t req_msg_length;
      63                 :             :     uint32_t req_msg_offset;
      64                 :             :     bool use_large_cert_chain;
      65                 :             :     uint32_t req_msg_header_size;
      66                 :             :     uint32_t rsp_msg_header_size;
      67                 :             : 
      68                 :         112 :     spdm_context = context;
      69                 :             : 
      70                 :         112 :     encap_context = libspdm_get_encap_context(spdm_context, session_id);
      71         [ -  + ]:         112 :     if (encap_context == NULL) {
      72                 :             :         /* session_id does not refer to an existing session. */
      73                 :           0 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      74                 :             :     }
      75                 :             : 
      76                 :         112 :     encap_context->has_last_encap_request = false;
      77                 :             : 
      78         [ +  + ]:         112 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_11) {
      79                 :           2 :         return LIBSPDM_STATUS_UNSUPPORTED_CAP;
      80                 :             :     }
      81                 :             : 
      82         [ -  + ]:         110 :     if (!libspdm_is_capabilities_flag_supported(
      83                 :             :             spdm_context, false,
      84                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP, 0)) {
      85                 :           0 :         return LIBSPDM_STATUS_UNSUPPORTED_CAP;
      86                 :             :     }
      87                 :             : 
      88   [ +  +  +  - ]:         114 :     if ((libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_14) &&
      89                 :           4 :         libspdm_is_capabilities_flag_supported(
      90                 :             :             spdm_context, false,
      91                 :             :             SPDM_GET_CAPABILITIES_REQUEST_FLAGS_LARGE_RESP_CAP, 0)) {
      92                 :           4 :         use_large_cert_chain = true;
      93                 :           4 :         encap_context->use_large_cert_chain = true;
      94                 :             :     } else {
      95                 :         106 :         use_large_cert_chain = false;
      96                 :         106 :         encap_context->use_large_cert_chain = false;
      97                 :             :     }
      98                 :             : 
      99         [ +  + ]:         110 :     if (use_large_cert_chain) {
     100                 :           4 :         req_msg_header_size = sizeof(spdm_get_certificate_large_request_t);
     101                 :           4 :         rsp_msg_header_size = sizeof(spdm_certificate_large_response_t);
     102                 :             :     } else {
     103                 :         106 :         req_msg_header_size = sizeof(spdm_get_certificate_request_t);
     104                 :         106 :         rsp_msg_header_size = sizeof(spdm_certificate_response_t);
     105                 :             :     }
     106                 :             : 
     107                 :         110 :     LIBSPDM_ASSERT(*encap_request_size >= req_msg_header_size);
     108                 :         110 :     *encap_request_size = req_msg_header_size;
     109                 :             : 
     110                 :         110 :     spdm_request = encap_request;
     111                 :             : 
     112                 :         110 :     spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
     113                 :         110 :     spdm_request->header.request_response_code = SPDM_GET_CERTIFICATE;
     114                 :         110 :     spdm_request->header.param1 = req_slot_id;
     115                 :         110 :     spdm_request->header.param2 = 0;
     116                 :         110 :     req_msg_offset = (uint32_t)encap_context->payload_buffer_size;
     117                 :             : 
     118                 :             :     /* certificate response is encapsulate in deliver encapsulated response msg */
     119                 :         110 :     req_msg_length = spdm_context->local_context.capability.max_spdm_msg_size -
     120                 :         110 :                      sizeof(spdm_deliver_encapsulated_response_request_t) -
     121                 :             :                      rsp_msg_header_size;
     122                 :             : 
     123         [ +  + ]:         110 :     if (use_large_cert_chain) {
     124                 :           4 :         spdm_request->header.param1 |= SPDM_GET_CERTIFICATE_REQUEST_LARGE_CERT_CHAIN;
     125                 :           4 :         spdm_request->offset = 0;
     126                 :           4 :         spdm_request->length = 0;
     127                 :           4 :         spdm_request->large_offset = req_msg_offset;
     128                 :           4 :         spdm_request->large_length = req_msg_length;
     129                 :             :     } else {
     130                 :         106 :         req_msg_length = LIBSPDM_MIN(req_msg_length, SPDM_MAX_CERTIFICATE_CHAIN_SIZE);
     131                 :         106 :         spdm_request->offset = (uint16_t)req_msg_offset;
     132                 :         106 :         spdm_request->length = (uint16_t)req_msg_length;
     133                 :             :     }
     134                 :         110 :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "request (offset 0x%x, size 0x%x):\n",
     135                 :             :                    req_msg_offset, req_msg_length));
     136                 :             : 
     137                 :             :     /* Cache data*/
     138                 :         110 :     status = libspdm_append_message_mut_b(spdm_context, spdm_request, *encap_request_size);
     139         [ -  + ]:         110 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     140                 :           0 :         return LIBSPDM_STATUS_BUFFER_FULL;
     141                 :             :     }
     142                 :             : 
     143                 :         110 :     encap_context->req_slot_id = req_slot_id;
     144                 :         110 :     libspdm_copy_mem(&encap_context->last_encap_request_header,
     145                 :             :                      sizeof(encap_context->last_encap_request_header),
     146                 :         110 :                      &spdm_request->header, sizeof(spdm_message_header_t));
     147                 :         110 :     encap_context->has_last_encap_request = true;
     148                 :             : 
     149                 :         110 :     return LIBSPDM_STATUS_SUCCESS;
     150                 :             : }
     151                 :             : 
     152                 :             : /**
     153                 :             :  * Determines whether a mutual authentication other than the one that encap_context belongs to is
     154                 :             :  * in progress. Its CHALLENGE_AUTH or FINISH is verified against the certificate chains already
     155                 :             :  * recorded, so no other flow can replace one until it ends.
     156                 :             :  *
     157                 :             :  * @param  spdm_context   A pointer to the SPDM context.
     158                 :             :  * @param  encap_context  The encapsulated context of the flow that retrieved a certificate chain.
     159                 :             :  *
     160                 :             :  * @retval true   Another mutual authentication is in progress.
     161                 :             :  * @retval false  No other mutual authentication is in progress.
     162                 :             :  **/
     163                 :          54 : static bool libspdm_is_other_mut_auth_in_progress(libspdm_context_t *spdm_context,
     164                 :             :                                                   const libspdm_encap_context_t *encap_context)
     165                 :             : {
     166                 :             :     libspdm_session_info_t *session_info;
     167                 :             :     size_t index;
     168                 :             : 
     169         [ +  + ]:          54 :     if (encap_context != &spdm_context->encap_context) {
     170                 :             :         /* A basic flow is in progress until it ends, including while ResponseNotReady has
     171                 :             :          * suspended it. */
     172         [ +  + ]:          22 :         if (spdm_context->encap_context.flow_type == LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH) {
     173                 :           4 :             return true;
     174                 :             :         }
     175                 :             :         #if LIBSPDM_RESPOND_IF_READY_SUPPORT
     176         [ +  + ]:          18 :         if (spdm_context->encap_context.response_not_ready &&
     177         [ +  - ]:           2 :             (spdm_context->encap_context.response_not_ready_flow_type ==
     178                 :             :              LIBSPDM_ENCAP_FLOW_BASIC_MUT_AUTH)) {
     179                 :           2 :             return true;
     180                 :             :         }
     181                 :             :         #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
     182                 :             :     }
     183                 :             : 
     184                 :             :     /* A session that requested mutual authentication is in progress until FINISH completes its
     185                 :             :      * handshake. Only its own session-based flow can record a certificate chain meanwhile. */
     186         [ +  + ]:         196 :     for (index = 0; index < LIBSPDM_MAX_SESSION_COUNT; index++) {
     187                 :         160 :         session_info = &spdm_context->session_info[index];
     188         [ +  + ]:         160 :         if ((session_info->session_id == INVALID_SESSION_ID) ||
     189   [ +  +  +  + ]:          90 :             (session_info->mut_auth_requested == 0) ||
     190                 :          28 :             (libspdm_secured_message_get_session_state(session_info->secured_message_context) !=
     191                 :             :              LIBSPDM_SESSION_STATE_HANDSHAKING)) {
     192                 :         134 :             continue;
     193                 :             :         }
     194         [ +  + ]:          26 :         if ((encap_context == &session_info->encap_context) &&
     195         [ +  + ]:          16 :             (encap_context->flow_type == LIBSPDM_ENCAP_FLOW_SESS_MUT_AUTH)) {
     196                 :          14 :             continue;
     197                 :             :         }
     198                 :          12 :         return true;
     199                 :             :     }
     200                 :             : 
     201                 :          36 :     return false;
     202                 :             : }
     203                 :             : 
     204                 :             : /**
     205                 :             :  * Determines whether recording a certificate chain would replace a different certificate chain
     206                 :             :  * that is already recorded for the slot.
     207                 :             :  *
     208                 :             :  * @param  spdm_context     A pointer to the SPDM context.
     209                 :             :  * @param  slot_id          The slot that the certificate chain would be recorded for.
     210                 :             :  * @param  cert_chain       The certificate chain.
     211                 :             :  * @param  cert_chain_size  The size, in bytes, of cert_chain.
     212                 :             :  *
     213                 :             :  * @retval true   A different certificate chain is recorded for the slot.
     214                 :             :  * @retval false  No certificate chain, or the same certificate chain, is recorded for the slot.
     215                 :             :  **/
     216                 :          18 : static bool libspdm_replaces_recorded_cert_chain(libspdm_context_t *spdm_context,
     217                 :             :                                                  uint8_t slot_id,
     218                 :             :                                                  const uint8_t *cert_chain,
     219                 :             :                                                  size_t cert_chain_size)
     220                 :             : {
     221                 :             :     const libspdm_peer_used_cert_chain_t *peer_used_cert_chain;
     222                 :             : #if !LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     223                 :             :     uint8_t cert_chain_hash[LIBSPDM_MAX_HASH_SIZE];
     224                 :             : #endif /* !LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
     225                 :             : 
     226                 :          18 :     peer_used_cert_chain = &spdm_context->connection_info.peer_used_cert_chain[slot_id];
     227                 :             : 
     228                 :             : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     229                 :             :     if (peer_used_cert_chain->buffer_size == 0) {
     230                 :             :         return false;
     231                 :             :     }
     232                 :             :     return (peer_used_cert_chain->buffer_size != cert_chain_size) ||
     233                 :             :            !libspdm_consttime_is_mem_equal(peer_used_cert_chain->buffer, cert_chain,
     234                 :             :                                            cert_chain_size);
     235                 :             : #else
     236         [ +  + ]:          18 :     if (peer_used_cert_chain->buffer_hash_size == 0) {
     237                 :           2 :         return false;
     238                 :             :     }
     239         [ -  + ]:          16 :     if (!libspdm_hash_all(spdm_context->connection_info.algorithm.base_hash_algo,
     240                 :             :                           cert_chain, cert_chain_size, cert_chain_hash)) {
     241                 :             :         /* The certificate chains cannot be compared, so they are treated as different. */
     242                 :           0 :         return true;
     243                 :             :     }
     244                 :          32 :     return !libspdm_consttime_is_mem_equal(peer_used_cert_chain->buffer_hash, cert_chain_hash,
     245                 :          16 :                                            peer_used_cert_chain->buffer_hash_size);
     246                 :             : #endif /* LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT */
     247                 :             : }
     248                 :             : 
     249                 :         148 : libspdm_return_t libspdm_process_encap_response_certificate(
     250                 :             :     libspdm_context_t *spdm_context, size_t encap_response_size,
     251                 :             :     const void *encap_response, bool *need_continue)
     252                 :             : {
     253                 :             :     libspdm_encap_context_t *encap_context;
     254                 :             :     const spdm_certificate_large_response_t *spdm_response;
     255                 :             :     size_t spdm_response_size;
     256                 :             :     bool result;
     257                 :             :     libspdm_return_t status;
     258                 :             :     uint32_t request_offset;
     259                 :             :     uint8_t slot_id;
     260                 :             :     uint8_t *cert_chain_buffer;
     261                 :             :     size_t cert_chain_buffer_size;
     262                 :             :     size_t cert_chain_buffer_max_size;
     263                 :             :     uint8_t cert_model;
     264                 :             :     uint32_t rsp_msg_portion_length;
     265                 :             :     uint32_t rsp_msg_remainder_length;
     266                 :             :     bool use_large_cert_chain;
     267                 :             :     uint32_t rsp_msg_header_size;
     268                 :             :     uint32_t max_cert_chain_size;
     269                 :             :     uint32_t req_msg_length;
     270                 :             : 
     271                 :         148 :     spdm_response = encap_response;
     272                 :         148 :     spdm_response_size = encap_response_size;
     273                 :             : 
     274         [ -  + ]:         148 :     if (spdm_response_size < sizeof(spdm_message_header_t)) {
     275                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_SIZE;
     276                 :             :     }
     277         [ -  + ]:         148 :     if (spdm_response->header.spdm_version != libspdm_get_connection_version (spdm_context)) {
     278                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     279                 :             :     }
     280         [ +  + ]:         148 :     if (spdm_response->header.request_response_code == SPDM_ERROR) {
     281                 :           4 :         status = libspdm_handle_encap_error_response_main(spdm_response->header.param1);
     282         [ +  - ]:           4 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     283                 :           4 :             return status;
     284                 :             :         }
     285         [ -  + ]:         144 :     } else if (spdm_response->header.request_response_code != SPDM_CERTIFICATE) {
     286                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     287                 :             :     }
     288                 :         144 :     encap_context = libspdm_get_encap_context_via_last_request(spdm_context);
     289                 :             : 
     290                 :         144 :     cert_chain_buffer = (uint8_t *)encap_context->payload_buffer;
     291                 :         144 :     cert_chain_buffer_size = encap_context->payload_buffer_size;
     292                 :         144 :     cert_chain_buffer_max_size = encap_context->payload_buffer_max_size;
     293                 :             : 
     294                 :         144 :     use_large_cert_chain = encap_context->use_large_cert_chain;
     295         [ +  + ]:         144 :     if (use_large_cert_chain) {
     296         [ +  + ]:           6 :         if ((spdm_response->header.param1 & SPDM_CERTIFICATE_RESPONSE_LARGE_CERT_CHAIN) == 0) {
     297                 :           2 :             return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     298                 :             :         }
     299                 :             :     } else {
     300         [ +  + ]:         138 :         if ((spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_14) &&
     301         [ +  - ]:           2 :             ((spdm_response->header.param1 & SPDM_CERTIFICATE_RESPONSE_LARGE_CERT_CHAIN) != 0)) {
     302                 :           2 :             return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     303                 :             :         }
     304                 :             :     }
     305                 :             : 
     306         [ +  + ]:         140 :     if (use_large_cert_chain) {
     307                 :           4 :         max_cert_chain_size = SPDM_MAX_CERTIFICATE_CHAIN_SIZE_14;
     308                 :           4 :         rsp_msg_header_size = sizeof(spdm_certificate_large_response_t);
     309                 :             :     } else {
     310                 :         136 :         max_cert_chain_size = SPDM_MAX_CERTIFICATE_CHAIN_SIZE;
     311                 :         136 :         rsp_msg_header_size = sizeof(spdm_certificate_response_t);
     312                 :             :     }
     313                 :             : 
     314                 :             :     /* certificate response is encapsulate in deliver encapsulated response msg */
     315                 :         140 :     req_msg_length = spdm_context->local_context.capability.max_spdm_msg_size -
     316                 :         140 :                      sizeof(spdm_deliver_encapsulated_response_request_t) -
     317                 :             :                      rsp_msg_header_size;
     318                 :             : 
     319         [ +  + ]:         140 :     if (!use_large_cert_chain) {
     320                 :         136 :         req_msg_length = LIBSPDM_MIN(req_msg_length, SPDM_MAX_CERTIFICATE_CHAIN_SIZE);
     321                 :             :     }
     322                 :             : 
     323         [ -  + ]:         140 :     if (encap_response_size < rsp_msg_header_size) {
     324                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_SIZE;
     325                 :             :     }
     326         [ +  + ]:         140 :     if (use_large_cert_chain) {
     327                 :           4 :         rsp_msg_portion_length = spdm_response->large_portion_length;
     328                 :           4 :         rsp_msg_remainder_length = spdm_response->large_remainder_length;
     329                 :             :     } else {
     330                 :         136 :         rsp_msg_portion_length = spdm_response->portion_length;
     331                 :         136 :         rsp_msg_remainder_length = spdm_response->remainder_length;
     332                 :             :     }
     333                 :             : 
     334   [ +  +  +  + ]:         140 :     if ((rsp_msg_portion_length > req_msg_length) ||
     335                 :             :         (rsp_msg_portion_length == 0)) {
     336                 :           6 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     337                 :             :     }
     338                 :             : 
     339                 :         134 :     request_offset = (uint32_t)cert_chain_buffer_size;
     340                 :             : 
     341         [ -  + ]:         134 :     if (rsp_msg_portion_length > max_cert_chain_size - request_offset) {
     342                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     343                 :             :     }
     344         [ -  + ]:         134 :     if (rsp_msg_remainder_length > max_cert_chain_size - request_offset - rsp_msg_portion_length) {
     345                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     346                 :             :     }
     347         [ +  + ]:         134 :     if (request_offset == 0) {
     348                 :          74 :         encap_context->cert_chain_total_len = rsp_msg_portion_length + rsp_msg_remainder_length;
     349                 :          60 :     } else if (encap_context->cert_chain_total_len !=
     350         [ +  + ]:          60 :                request_offset + rsp_msg_portion_length + rsp_msg_remainder_length) {
     351                 :           2 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     352                 :             :     }
     353                 :         132 :     slot_id = encap_context->req_slot_id;
     354         [ -  + ]:         132 :     if ((spdm_response->header.param1 & SPDM_CERTIFICATE_RESPONSE_SLOT_ID_MASK) != slot_id) {
     355                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     356                 :             :     }
     357         [ +  + ]:         132 :     if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
     358                 :          26 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "cert_info - 0x%02x\n",
     359                 :             :                        spdm_response->header.param2));
     360                 :          26 :         cert_model = spdm_response->header.param2 &
     361                 :             :                      SPDM_CERTIFICATE_RESPONSE_ATTRIBUTES_CERTIFICATE_INFO_MASK;
     362         [ +  + ]:          26 :         if (spdm_context->connection_info.multi_key_conn_req) {
     363         [ -  + ]:          18 :             if (cert_model > SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT) {
     364                 :           0 :                 return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     365                 :             :             }
     366   [ +  +  +  + ]:          18 :             if ((slot_id == 0) &&
     367                 :             :                 (cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
     368                 :           2 :                 return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     369                 :             :             }
     370         [ +  + ]:          16 :             if ((cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_NONE) &&
     371         [ +  - ]:           2 :                 (spdm_response->portion_length != 0)) {
     372                 :           2 :                 return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     373                 :             :             }
     374                 :             :         } else {
     375         [ -  + ]:           8 :             if (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_NONE) {
     376                 :           0 :                 return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     377                 :             :             }
     378                 :             :         }
     379                 :             :         /* The certificate model is recorded with the certificate chain, so it must be the same in
     380                 :             :          * every CERTIFICATE response that carries the chain. */
     381         [ +  + ]:          22 :         if (request_offset == 0) {
     382                 :          16 :             encap_context->cert_model = cert_model;
     383         [ +  + ]:           6 :         } else if (encap_context->cert_model != cert_model) {
     384                 :           2 :             return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     385                 :             :         }
     386         [ -  + ]:          20 :         if ((spdm_context->connection_info.peer_cert_info[slot_id] !=
     387                 :           0 :              SPDM_CERTIFICATE_INFO_CERT_MODEL_NONE) &&
     388         [ #  # ]:           0 :             (spdm_context->connection_info.peer_cert_info[slot_id] != cert_model)) {
     389                 :           0 :             return LIBSPDM_STATUS_INVALID_MSG_FIELD;
     390                 :             :         }
     391                 :             :     }
     392                 :             : 
     393         [ -  + ]:         126 :     if (spdm_response_size < rsp_msg_header_size + rsp_msg_portion_length) {
     394                 :           0 :         return LIBSPDM_STATUS_INVALID_MSG_SIZE;
     395                 :             :     }
     396                 :         126 :     spdm_response_size = rsp_msg_header_size + rsp_msg_portion_length;
     397                 :             : 
     398                 :             :     /* Cache data*/
     399                 :             : 
     400                 :         126 :     status = libspdm_append_message_mut_b(spdm_context, spdm_response, spdm_response_size);
     401         [ -  + ]:         126 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     402                 :           0 :         return LIBSPDM_STATUS_BUFFER_FULL;
     403                 :             :     }
     404                 :             : 
     405                 :         126 :     LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Certificate (offset 0x%x, size 0x%x):\n",
     406                 :             :                    request_offset, rsp_msg_portion_length));
     407                 :         126 :     LIBSPDM_INTERNAL_DUMP_HEX((const uint8_t *)spdm_response + rsp_msg_header_size,
     408                 :             :                               rsp_msg_portion_length);
     409                 :             : 
     410         [ +  + ]:         126 :     if (cert_chain_buffer_size + rsp_msg_portion_length > cert_chain_buffer_max_size) {
     411                 :           4 :         LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "cert_chain buffer too small\n"));
     412                 :           4 :         return LIBSPDM_STATUS_BUFFER_TOO_SMALL;
     413                 :             :     }
     414                 :             : 
     415                 :         122 :     libspdm_copy_mem(cert_chain_buffer + cert_chain_buffer_size,
     416                 :             :                      cert_chain_buffer_max_size - cert_chain_buffer_size,
     417                 :             :                      (const uint8_t *)spdm_response + rsp_msg_header_size,
     418                 :             :                      rsp_msg_portion_length);
     419                 :             : 
     420                 :         122 :     cert_chain_buffer_size += rsp_msg_portion_length;
     421                 :         122 :     encap_context->payload_buffer_size = cert_chain_buffer_size;
     422                 :             : 
     423         [ +  + ]:         122 :     if (rsp_msg_remainder_length != 0) {
     424                 :          68 :         *need_continue = true;
     425                 :             : 
     426                 :          68 :         return LIBSPDM_STATUS_SUCCESS;
     427                 :             :     }
     428                 :             : 
     429                 :          54 :     *need_continue = false;
     430                 :             : 
     431   [ +  +  +  + ]:          72 :     if (libspdm_is_other_mut_auth_in_progress(spdm_context, encap_context) &&
     432                 :          18 :         libspdm_replaces_recorded_cert_chain(spdm_context, slot_id, cert_chain_buffer,
     433                 :             :                                              cert_chain_buffer_size)) {
     434                 :          14 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     435                 :             :     }
     436                 :             : 
     437         [ +  + ]:          40 :     if (spdm_context->local_context.verify_peer_spdm_cert_chain != NULL) {
     438                 :           2 :         result = spdm_context->local_context.verify_peer_spdm_cert_chain (
     439                 :           2 :             spdm_context, encap_context->req_slot_id,
     440                 :             :             cert_chain_buffer_size, cert_chain_buffer, NULL, NULL);
     441         [ -  + ]:           2 :         if (!result) {
     442                 :           0 :             return LIBSPDM_STATUS_VERIF_FAIL;
     443                 :             :         }
     444                 :             :     } else {
     445                 :          38 :         result = libspdm_verify_peer_cert_chain_buffer_integrity(
     446                 :             :             spdm_context, cert_chain_buffer, cert_chain_buffer_size);
     447         [ +  + ]:          38 :         if (!result) {
     448                 :           4 :             return LIBSPDM_STATUS_VERIF_FAIL;
     449                 :             :         }
     450                 :             : 
     451                 :             :         /*verify peer cert chain authority*/
     452                 :          34 :         result = libspdm_verify_peer_cert_chain_buffer_authority(
     453                 :             :             spdm_context, cert_chain_buffer, cert_chain_buffer_size, NULL, NULL);
     454         [ +  + ]:          34 :         if (!result) {
     455                 :           4 :             status = LIBSPDM_STATUS_VERIF_NO_AUTHORITY;
     456                 :             :         }
     457                 :             :     }
     458                 :             : 
     459                 :          36 :     slot_id = encap_context->req_slot_id;
     460                 :          36 :     LIBSPDM_ASSERT(slot_id < SPDM_MAX_SLOT_COUNT);
     461                 :          36 :     encap_context->recorded_cert_chain_slot_mask |= (uint8_t)(1 << slot_id);
     462         [ +  + ]:          36 :     if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
     463                 :           4 :         spdm_context->connection_info.peer_cert_info[slot_id] = encap_context->cert_model;
     464                 :             :     }
     465                 :             : 
     466                 :             : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
     467                 :             :     spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_size =
     468                 :             :         cert_chain_buffer_size;
     469                 :             : 
     470                 :             :     libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer,
     471                 :             :                      sizeof(spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer),
     472                 :             :                      cert_chain_buffer, cert_chain_buffer_size);
     473                 :             : #else
     474                 :          36 :     result = libspdm_hash_all(
     475                 :             :         spdm_context->connection_info.algorithm.base_hash_algo,
     476                 :             :         cert_chain_buffer, cert_chain_buffer_size,
     477                 :          36 :         spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_hash);
     478         [ -  + ]:          36 :     if (!result) {
     479                 :           0 :         return LIBSPDM_STATUS_CRYPTO_ERROR;
     480                 :             :     }
     481                 :          72 :     spdm_context->connection_info.peer_used_cert_chain[slot_id].buffer_hash_size =
     482                 :          36 :         libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
     483                 :             : 
     484                 :          36 :     libspdm_free_peer_leaf_cert_public_key(spdm_context, slot_id);
     485         [ -  + ]:          36 :     if (spdm_context->connection_info.algorithm.req_pqc_asym_alg != 0) {
     486                 :           0 :         result = libspdm_get_pqc_leaf_cert_public_key_from_cert_chain(
     487                 :             :             spdm_context->connection_info.algorithm.base_hash_algo,
     488                 :             :             spdm_context->connection_info.algorithm.req_pqc_asym_alg,
     489                 :             :             cert_chain_buffer, cert_chain_buffer_size,
     490                 :           0 :             &spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key);
     491                 :             :     } else {
     492                 :          36 :         result = libspdm_get_leaf_cert_public_key_from_cert_chain(
     493                 :             :             spdm_context->connection_info.algorithm.base_hash_algo,
     494                 :          36 :             spdm_context->connection_info.algorithm.req_base_asym_alg,
     495                 :             :             cert_chain_buffer, cert_chain_buffer_size,
     496                 :          36 :             &spdm_context->connection_info.peer_used_cert_chain[slot_id].leaf_cert_public_key);
     497                 :             :     }
     498         [ -  + ]:          36 :     if (!result) {
     499                 :           0 :         return LIBSPDM_STATUS_INVALID_CERT;
     500                 :             :     }
     501                 :             : #endif
     502         [ +  + ]:          36 :     if (status != LIBSPDM_STATUS_VERIF_NO_AUTHORITY) {
     503                 :          32 :         return LIBSPDM_STATUS_SUCCESS;
     504                 :             :     } else {
     505                 :           4 :         return LIBSPDM_STATUS_VERIF_NO_AUTHORITY;
     506                 :             :     }
     507                 :             : }
     508                 :             : 
     509                 :             : #endif /* (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (...) */
        

Generated by: LCOV version 2.0-1