Code coverage report for DMTF/libspdm
Current view: top level - library/spdm_requester_lib - libspdm_req_set_certificate.c (source / functions) Coverage Total Hit
Test: b3f7d2e00db5b07cf17ccc0075121f9c08f2965b Lines: 76.7 % 120 92
Test Date: 2026-11-29 08:33:56 Functions: 100.0 % 3 3
Branches: 65.0 % 80 52

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  *  Copyright Notice:
       3                 :             :  *  Copyright 2021-2026 DMTF. All rights reserved.
       4                 :             :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5                 :             :  **/
       6                 :             : 
       7                 :             : #include "internal/libspdm_requester_lib.h"
       8                 :             : 
       9                 :             : #if LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP
      10                 :             : /**
      11                 :             :  * This function sends SET_CERTIFICATE
      12                 :             :  * to set certificate on the device.
      13                 :             :  *
      14                 :             :  * @param  spdm_context                 A pointer to the SPDM context.
      15                 :             :  * @param  session_id                   Indicates if it is a secured message protected via SPDM session.
      16                 :             :  *                                      If session_id is NULL, it is a normal message.
      17                 :             :  *                                      If session_id is NOT NULL, it is a secured message.
      18                 :             :  * @param  slot_id                      The number of slot for the certificate chain.
      19                 :             :  * @param  cert_chain                   The pointer for the certificate chain to set.
      20                 :             :  *                                      The cert chain is a full SPDM certificate chain, including Length and Root Cert Hash.
      21                 :             :  * @param  cert_chain_size              The size of the certificate chain to set.
      22                 :             :  * @param  request_attribute            Set certificate request attributes. This field is only used for SPDM 1.3 and above.
      23                 :             :  *                                      And the bit[0~3] of request_attribute must be 0.
      24                 :             :  * @param  key_pair_id                  The value of this field shall be the unique key pair number identifying the desired
      25                 :             :  *                                      asymmetric key pair to associate with SlotID
      26                 :             :  **/
      27                 :          22 : static libspdm_return_t libspdm_try_set_certificate(libspdm_context_t *spdm_context,
      28                 :             :                                                     const uint32_t *session_id, uint8_t slot_id,
      29                 :             :                                                     void *cert_chain, size_t cert_chain_size,
      30                 :             :                                                     uint8_t request_attribute,
      31                 :             :                                                     uint8_t key_pair_id)
      32                 :             : {
      33                 :             :     libspdm_return_t status;
      34                 :             :     spdm_set_certificate_request_t *spdm_request;
      35                 :             :     size_t spdm_request_size;
      36                 :             :     spdm_set_certificate_response_t *spdm_response;
      37                 :             :     size_t spdm_response_size;
      38                 :             :     size_t transport_header_size;
      39                 :             :     uint8_t *message;
      40                 :             :     size_t message_size;
      41                 :             :     libspdm_session_info_t *session_info;
      42                 :             :     libspdm_session_state_t session_state;
      43                 :             : 
      44                 :          22 :     LIBSPDM_ASSERT(slot_id < SPDM_MAX_SLOT_COUNT);
      45                 :             : 
      46         [ -  + ]:          22 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_12) {
      47                 :           0 :         return LIBSPDM_STATUS_UNSUPPORTED_CAP;
      48                 :             :     }
      49         [ +  + ]:          22 :     if (libspdm_get_connection_version (spdm_context) < SPDM_MESSAGE_VERSION_13) {
      50   [ +  +  -  + ]:          14 :         if ((cert_chain == NULL) || (cert_chain_size == 0)) {
      51                 :           2 :             return LIBSPDM_STATUS_INVALID_PARAMETER;
      52                 :             :         }
      53                 :             :     }
      54         [ +  + ]:          20 :     if (libspdm_get_connection_version (spdm_context) >= SPDM_MESSAGE_VERSION_13) {
      55                 :           8 :         const uint8_t set_cert_model =
      56                 :           8 :             (request_attribute & SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_CERT_MODEL_MASK) >>
      57                 :             :             SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_CERT_MODEL_OFFSET;
      58                 :             : 
      59                 :             :         /* Bit[0~3] of request_attribute must be 0 since this value is provided by the slot_id
      60                 :             :          * parameter. */
      61         [ -  + ]:           8 :         if ((request_attribute & SPDM_SET_CERTIFICATE_REQUEST_SLOT_ID_MASK) != 0) {
      62                 :           0 :             return LIBSPDM_STATUS_INVALID_PARAMETER;
      63                 :             :         }
      64                 :             : 
      65                 :             :         /* SET_CERT_CAP for a 1.2 Responder is not checked because it was not defined
      66                 :             :          * in SPDM 1.2.0. */
      67         [ -  + ]:           8 :         if (!libspdm_is_capabilities_flag_supported(
      68                 :             :                 spdm_context, true, 0,
      69                 :             :                 SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_CERT_CAP)) {
      70                 :           0 :             return LIBSPDM_STATUS_UNSUPPORTED_CAP;
      71                 :             :         }
      72         [ +  + ]:           8 :         if (spdm_context->connection_info.multi_key_conn_rsp) {
      73         [ -  + ]:           6 :             if (key_pair_id == 0) {
      74                 :           0 :                 return LIBSPDM_STATUS_INVALID_PARAMETER;
      75                 :             :             }
      76         [ +  - ]:           6 :             if ((request_attribute & SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_ERASE) == 0) {
      77   [ +  +  +  - ]:           6 :                 if ((set_cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_NONE) ||
      78         [ +  + ]:           4 :                     (set_cert_model > SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT) ||
      79         [ +  - ]:           2 :                     ((slot_id == 0) &&
      80                 :             :                      (set_cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT))) {
      81                 :           4 :                     return LIBSPDM_STATUS_INVALID_PARAMETER;
      82                 :             :                 }
      83                 :             :             }
      84                 :             :         } else {
      85   [ +  -  -  + ]:           2 :             if ((key_pair_id != 0) || (set_cert_model != 0)) {
      86                 :           0 :                 return LIBSPDM_STATUS_INVALID_PARAMETER;
      87                 :             :             }
      88                 :             :         }
      89                 :             :     }
      90                 :             : 
      91         [ -  + ]:          16 :     if (spdm_context->connection_info.connection_state < LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
      92                 :           0 :         return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
      93                 :             :     }
      94                 :             : 
      95         [ +  + ]:          16 :     if (session_id != NULL) {
      96                 :           4 :         session_info = libspdm_get_session_info_via_session_id(
      97                 :             :             spdm_context, *session_id);
      98         [ -  + ]:           4 :         if (session_info == NULL) {
      99                 :           0 :             LIBSPDM_ASSERT(false);
     100                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     101                 :             :         }
     102                 :           4 :         session_state = libspdm_secured_message_get_session_state(
     103                 :             :             session_info->secured_message_context);
     104         [ -  + ]:           4 :         if (session_state != LIBSPDM_SESSION_STATE_ESTABLISHED) {
     105                 :           0 :             return LIBSPDM_STATUS_INVALID_STATE_LOCAL;
     106                 :             :         }
     107                 :             :     }
     108                 :             : 
     109                 :          16 :     transport_header_size = spdm_context->local_context.capability.transport_header_size;
     110                 :          16 :     status = libspdm_acquire_sender_buffer (spdm_context, &message_size, (void **)&message);
     111         [ -  + ]:          16 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     112                 :           0 :         return status;
     113                 :             :     }
     114                 :          16 :     LIBSPDM_ASSERT (message_size >= transport_header_size +
     115                 :             :                     spdm_context->local_context.capability.transport_tail_size);
     116                 :          16 :     spdm_request = (void *)(message + transport_header_size);
     117                 :          16 :     spdm_request_size = message_size - transport_header_size -
     118                 :          16 :                         spdm_context->local_context.capability.transport_tail_size;
     119                 :             : 
     120                 :          16 :     LIBSPDM_ASSERT(spdm_request_size >= sizeof(spdm_set_certificate_request_t));
     121                 :          16 :     spdm_request->header.spdm_version = libspdm_get_connection_version (spdm_context);
     122                 :          16 :     spdm_request->header.request_response_code = SPDM_SET_CERTIFICATE;
     123                 :          16 :     spdm_request->header.param1 = slot_id & SPDM_SET_CERTIFICATE_REQUEST_SLOT_ID_MASK;
     124                 :          16 :     spdm_request->header.param2 = 0;
     125                 :             : 
     126         [ +  + ]:          16 :     if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
     127         [ +  + ]:           4 :         if ((request_attribute & SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_ERASE) != 0) {
     128                 :             :             /*the CertChain field shall be absent*/
     129                 :           2 :             cert_chain_size = 0;
     130                 :             :             /*the value of SetCertModel shall be zero*/
     131                 :           2 :             spdm_request->header.param1 &= ~SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_CERT_MODEL_MASK;
     132                 :             :             /*set Erase bit */
     133                 :           2 :             spdm_request->header.param1 |= SPDM_SET_CERTIFICATE_REQUEST_ATTRIBUTES_ERASE;
     134                 :             :         } else {
     135                 :           2 :             spdm_request->header.param1 |= request_attribute;
     136                 :             :         }
     137                 :             : 
     138         [ +  + ]:           4 :         if (spdm_context->connection_info.multi_key_conn_rsp) {
     139                 :           2 :             spdm_request->header.param2 = key_pair_id;
     140                 :             :         }
     141                 :             :     }
     142                 :             : 
     143   [ +  +  +  + ]:          16 :     if ((libspdm_get_connection_version (spdm_context) < SPDM_MESSAGE_VERSION_13) ||
     144                 :             :         (cert_chain_size != 0)) {
     145         [ -  + ]:          14 :         if (cert_chain == NULL) {
     146                 :           0 :             libspdm_release_sender_buffer (spdm_context);
     147                 :           0 :             return LIBSPDM_STATUS_INVALID_PARAMETER;
     148                 :             :         }
     149                 :             : 
     150                 :          14 :         LIBSPDM_ASSERT(spdm_request_size >=
     151                 :             :                        sizeof(spdm_set_certificate_request_t) + cert_chain_size);
     152                 :          14 :         libspdm_copy_mem(spdm_request + 1,
     153                 :             :                          spdm_request_size - sizeof(spdm_set_certificate_request_t),
     154                 :             :                          (uint8_t *)cert_chain, cert_chain_size);
     155                 :             :     }
     156                 :             : 
     157                 :          16 :     spdm_request_size = sizeof(spdm_set_certificate_request_t) + cert_chain_size;
     158                 :             : 
     159                 :          16 :     status = libspdm_send_spdm_request(spdm_context, session_id, spdm_request_size, spdm_request);
     160         [ +  + ]:          16 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     161                 :           2 :         libspdm_release_sender_buffer (spdm_context);
     162                 :           2 :         return status;
     163                 :             :     }
     164                 :          14 :     libspdm_release_sender_buffer (spdm_context);
     165                 :          14 :     spdm_request = (void *)spdm_context->last_spdm_request;
     166                 :             : 
     167                 :             :     /* receive */
     168                 :          14 :     status = libspdm_acquire_receiver_buffer (spdm_context, &message_size, (void **)&message);
     169         [ -  + ]:          14 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     170                 :           0 :         return status;
     171                 :             :     }
     172                 :          14 :     LIBSPDM_ASSERT (message_size >= transport_header_size);
     173                 :          14 :     spdm_response = (void *)(message);
     174                 :          14 :     spdm_response_size = message_size;
     175                 :             : 
     176                 :          14 :     status = libspdm_receive_spdm_response(spdm_context, session_id,
     177                 :             :                                            &spdm_response_size, (void **)&spdm_response);
     178                 :             : 
     179         [ -  + ]:          14 :     if (LIBSPDM_STATUS_IS_ERROR(status)) {
     180                 :           0 :         goto receive_done;
     181                 :             :     }
     182         [ -  + ]:          14 :     if (spdm_response_size < sizeof(spdm_message_header_t)) {
     183                 :           0 :         status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
     184                 :           0 :         goto receive_done;
     185                 :             :     }
     186         [ +  + ]:          14 :     if (spdm_response->header.request_response_code == SPDM_ERROR) {
     187                 :           2 :         status = libspdm_handle_error_response_main(
     188                 :             :             spdm_context, NULL,
     189                 :             :             &spdm_response_size,
     190                 :             :             (void **)&spdm_response, SPDM_SET_CERTIFICATE, SPDM_SET_CERTIFICATE_RSP);
     191         [ +  - ]:           2 :         if (LIBSPDM_STATUS_IS_ERROR(status)) {
     192                 :           2 :             goto receive_done;
     193                 :             :         }
     194         [ -  + ]:          12 :     } else if (spdm_response->header.request_response_code != SPDM_SET_CERTIFICATE_RSP) {
     195                 :           0 :         status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     196                 :           0 :         goto receive_done;
     197                 :             :     }
     198         [ -  + ]:          12 :     if (spdm_response->header.spdm_version != spdm_request->header.spdm_version) {
     199                 :           0 :         status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     200                 :           0 :         goto receive_done;
     201                 :             :     }
     202         [ -  + ]:          12 :     if ((spdm_response->header.param1 & SPDM_CERTIFICATE_RESPONSE_SLOT_ID_MASK) != slot_id) {
     203                 :           0 :         status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
     204                 :           0 :         goto receive_done;
     205                 :             :     }
     206                 :             : 
     207                 :             :     /* -=[Log Message Phase]=- */
     208                 :             :     #if LIBSPDM_ENABLE_MSG_LOG
     209                 :          12 :     libspdm_append_msg_log(spdm_context, spdm_response, spdm_response_size);
     210                 :             :     #endif /* LIBSPDM_ENABLE_MSG_LOG */
     211                 :             : 
     212                 :          12 :     status = LIBSPDM_STATUS_SUCCESS;
     213                 :             : 
     214                 :          14 : receive_done:
     215                 :          14 :     libspdm_release_receiver_buffer (spdm_context);
     216                 :          14 :     return status;
     217                 :             : }
     218                 :             : 
     219                 :          14 : libspdm_return_t libspdm_set_certificate(void *spdm_context,
     220                 :             :                                          const uint32_t *session_id, uint8_t slot_id,
     221                 :             :                                          void *cert_chain, size_t cert_chain_size)
     222                 :             : {
     223                 :             :     libspdm_context_t *context;
     224                 :             :     size_t retry;
     225                 :             :     uint64_t retry_delay_time;
     226                 :             :     libspdm_return_t status;
     227                 :             : 
     228                 :          14 :     context = spdm_context;
     229                 :          14 :     context->crypto_request = true;
     230                 :          14 :     retry = context->retry_times;
     231                 :          14 :     retry_delay_time = context->retry_delay_time;
     232                 :             :     do {
     233                 :          14 :         status = libspdm_try_set_certificate(context, session_id, slot_id,
     234                 :             :                                              cert_chain, cert_chain_size, 0, 0);
     235         [ +  - ]:          14 :         if (status != LIBSPDM_STATUS_BUSY_PEER) {
     236                 :          14 :             return status;
     237                 :             :         }
     238                 :             : 
     239                 :           0 :         libspdm_sleep(retry_delay_time);
     240         [ #  # ]:           0 :     } while (retry-- != 0);
     241                 :             : 
     242                 :           0 :     return status;
     243                 :             : }
     244                 :             : 
     245                 :           8 : libspdm_return_t libspdm_set_certificate_ex(void *spdm_context,
     246                 :             :                                             const uint32_t *session_id, uint8_t slot_id,
     247                 :             :                                             void *cert_chain, size_t cert_chain_size,
     248                 :             :                                             uint8_t request_attribute,
     249                 :             :                                             uint8_t key_pair_id)
     250                 :             : {
     251                 :             :     libspdm_context_t *context;
     252                 :             :     size_t retry;
     253                 :             :     uint64_t retry_delay_time;
     254                 :             :     libspdm_return_t status;
     255                 :             : 
     256                 :           8 :     context = spdm_context;
     257                 :           8 :     context->crypto_request = true;
     258                 :           8 :     retry = context->retry_times;
     259                 :           8 :     retry_delay_time = context->retry_delay_time;
     260                 :             :     do {
     261                 :           8 :         status = libspdm_try_set_certificate(context, session_id, slot_id,
     262                 :             :                                              cert_chain, cert_chain_size,
     263                 :             :                                              request_attribute, key_pair_id);
     264         [ +  - ]:           8 :         if (status != LIBSPDM_STATUS_BUSY_PEER) {
     265                 :           8 :             return status;
     266                 :             :         }
     267                 :             : 
     268                 :           0 :         libspdm_sleep(retry_delay_time);
     269         [ #  # ]:           0 :     } while (retry-- != 0);
     270                 :             : 
     271                 :           0 :     return status;
     272                 :             : }
     273                 :             : 
     274                 :             : #endif /*LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP*/
        

Generated by: LCOV version 2.0-1