LCOV - code coverage report
Current view: top level - library/spdm_responder_lib - libspdm_rsp_key_pair_info.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.9 % 69 62
Test Date: 2026-07-05 09:06:49 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024-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_GET_KEY_PAIR_INFO_CAP
      10              : 
      11           10 : libspdm_return_t libspdm_get_response_key_pair_info(libspdm_context_t *spdm_context,
      12              :                                                     size_t request_size, const void *request,
      13              :                                                     size_t *response_size, void *response)
      14              : {
      15              :     const spdm_get_key_pair_info_request_t *spdm_request;
      16              :     spdm_key_pair_info_response_t *spdm_response;
      17              : 
      18              :     libspdm_session_info_t *session_info;
      19              :     libspdm_session_state_t session_state;
      20              : 
      21              :     uint8_t total_key_pairs;
      22              :     uint16_t capabilities;
      23              :     uint16_t key_usage_capabilities;
      24              :     uint16_t current_key_usage;
      25              :     uint32_t asym_algo_capabilities;
      26              :     uint32_t current_asym_algo;
      27              :     uint32_t pqc_asym_algo_capabilities;
      28              :     uint32_t current_pqc_asym_algo;
      29              :     uint8_t pqc_asym_algo_cap_len;
      30              :     uint8_t current_pqc_asym_algo_len;
      31              :     uint16_t public_key_info_len;
      32              :     uint8_t assoc_cert_slot_mask;
      33              :     uint8_t key_pair_id;
      34              :     bool result;
      35              :     uint8_t *public_key_info;
      36              :     uint8_t *ptr;
      37              : 
      38           10 :     spdm_request = request;
      39              : 
      40              :     /* -=[Check Parameters Phase]=- */
      41           10 :     LIBSPDM_ASSERT(spdm_request->header.request_response_code == SPDM_GET_KEY_PAIR_INFO);
      42              : 
      43           10 :     if (libspdm_get_connection_version(spdm_context) < SPDM_MESSAGE_VERSION_13) {
      44            1 :         return libspdm_generate_error_response(spdm_context,
      45              :                                                SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
      46              :                                                SPDM_GET_KEY_PAIR_INFO,
      47              :                                                response_size, response);
      48              :     }
      49              : 
      50            9 :     if (spdm_request->header.spdm_version != libspdm_get_connection_version(spdm_context)) {
      51            1 :         return libspdm_generate_error_response(spdm_context,
      52              :                                                SPDM_ERROR_CODE_VERSION_MISMATCH, 0,
      53              :                                                response_size, response);
      54              :     }
      55              : 
      56            8 :     if (spdm_context->response_state != LIBSPDM_RESPONSE_STATE_NORMAL) {
      57            1 :         return libspdm_responder_handle_response_state(spdm_context,
      58            1 :                                                        spdm_request->header.request_response_code,
      59              :                                                        response_size, response);
      60              :     }
      61              : 
      62            7 :     if (request_size < sizeof(spdm_get_key_pair_info_request_t)) {
      63            1 :         return libspdm_generate_error_response(spdm_context,
      64              :                                                SPDM_ERROR_CODE_INVALID_REQUEST, 0,
      65              :                                                response_size, response);
      66              :     }
      67              : 
      68            6 :     if (spdm_context->connection_info.connection_state <
      69              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED) {
      70            1 :         return libspdm_generate_error_response(
      71              :             spdm_context,
      72              :             SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
      73              :             response_size, response);
      74              :     }
      75              : 
      76            5 :     if (spdm_context->last_spdm_request_session_id_valid) {
      77            0 :         session_info = libspdm_get_session_info_via_session_id(
      78              :             spdm_context,
      79              :             spdm_context->last_spdm_request_session_id);
      80            0 :         if (session_info == NULL) {
      81            0 :             return libspdm_generate_error_response(
      82              :                 spdm_context,
      83              :                 SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
      84              :                 response_size, response);
      85              :         }
      86            0 :         session_state = libspdm_secured_message_get_session_state(
      87              :             session_info->secured_message_context);
      88            0 :         if (session_state != LIBSPDM_SESSION_STATE_ESTABLISHED) {
      89            0 :             return libspdm_generate_error_response(
      90              :                 spdm_context,
      91              :                 SPDM_ERROR_CODE_UNEXPECTED_REQUEST, 0,
      92              :                 response_size, response);
      93              :         }
      94              :     }
      95              : 
      96            5 :     if (!libspdm_is_capabilities_flag_supported(
      97              :             spdm_context, false, 0,
      98              :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP)) {
      99            1 :         return libspdm_generate_error_response(
     100              :             spdm_context, SPDM_ERROR_CODE_UNSUPPORTED_REQUEST,
     101              :             SPDM_GET_KEY_PAIR_INFO, response_size, response);
     102              :     }
     103              : 
     104            4 :     key_pair_id = spdm_request->key_pair_id;
     105              : 
     106            4 :     LIBSPDM_ASSERT(*response_size >= sizeof(spdm_key_pair_info_response_t));
     107            4 :     public_key_info_len = (uint16_t)(*response_size - sizeof(spdm_key_pair_info_response_t));
     108            4 :     libspdm_zero_mem(response, *response_size);
     109              : 
     110              :     /* libspdm_read_key_pair_info() always reports total_key_pairs (even on failure), so read first
     111              :      * and then validate the range -- identical to the SET_KEY_PAIR_INFO_ACK handler. An invalid
     112              :      * key_pair_id (0 or > total_key_pairs) is INVALID_REQUEST; any other read failure is UNSPECIFIED. */
     113            4 :     total_key_pairs = 0;
     114            4 :     public_key_info = (uint8_t*)response + sizeof(spdm_key_pair_info_response_t);
     115            4 :     result = libspdm_read_key_pair_info(
     116              :         spdm_context,
     117              :         key_pair_id,
     118              :         &total_key_pairs,
     119              :         &capabilities,
     120              :         &key_usage_capabilities,
     121              :         &current_key_usage,
     122              :         &asym_algo_capabilities,
     123              :         &current_asym_algo,
     124              :         &pqc_asym_algo_capabilities,
     125              :         &current_pqc_asym_algo,
     126              :         &assoc_cert_slot_mask,
     127              :         &public_key_info_len,
     128              :         public_key_info);
     129            4 :     if ((key_pair_id == 0) || (key_pair_id > total_key_pairs)) {
     130            2 :         return libspdm_generate_error_response(spdm_context,
     131              :                                                SPDM_ERROR_CODE_INVALID_REQUEST, 0,
     132              :                                                response_size, response);
     133              :     }
     134            2 :     if (!result) {
     135            0 :         return libspdm_generate_error_response(spdm_context,
     136              :                                                SPDM_ERROR_CODE_UNSPECIFIED, 0,
     137              :                                                response_size, response);
     138              :     }
     139              : 
     140              :     /*If responder doesn't support SET_KEY_PAIR_INFO_CAP,the capabilities should be 0*/
     141            2 :     if (!libspdm_is_capabilities_flag_supported(
     142              :             spdm_context, false, 0,
     143              :             SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP)) {
     144            2 :         capabilities = 0;
     145              :     }
     146              : 
     147            2 :     spdm_response = response;
     148            2 :     if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
     149              :         /* The SPDM 1.4 tail is PqcAsymAlgoCapLen (1) + PqcAsymAlgoCapabilities (4) +
     150              :          * CurrentPqcAsymAlgoLen (1) + CurrentPqcAsymAlgo (4) = 10 bytes. */
     151            1 :         const size_t pqc_tail_size = sizeof(uint8_t) + sizeof(uint32_t) +
     152              :                                      sizeof(uint8_t) + sizeof(uint32_t);
     153            1 :         LIBSPDM_ASSERT(*response_size >= sizeof(spdm_key_pair_info_response_t) +
     154              :                        public_key_info_len + pqc_tail_size);
     155            1 :         *response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len +
     156              :                          pqc_tail_size;
     157              :     } else {
     158            1 :         LIBSPDM_ASSERT(*response_size >= sizeof(spdm_key_pair_info_response_t));
     159            1 :         *response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
     160              :     }
     161              : 
     162            2 :     spdm_response->header.spdm_version = spdm_request->header.spdm_version;
     163            2 :     spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
     164            2 :     spdm_response->header.param1 = 0;
     165            2 :     spdm_response->header.param2 = 0;
     166            2 :     spdm_response->total_key_pairs = total_key_pairs;
     167            2 :     spdm_response->key_pair_id = key_pair_id;
     168            2 :     spdm_response->capabilities = capabilities & SPDM_KEY_PAIR_CAP_MASK;
     169            2 :     spdm_response->key_usage_capabilities = key_usage_capabilities & SPDM_KEY_USAGE_BIT_MASK;
     170            2 :     spdm_response->current_key_usage = current_key_usage & SPDM_KEY_USAGE_BIT_MASK;
     171            2 :     spdm_response->asym_algo_capabilities = asym_algo_capabilities &
     172              :                                             SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     173            2 :     spdm_response->current_asym_algo = current_asym_algo & SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     174            2 :     spdm_response->public_key_info_len = public_key_info_len;
     175            2 :     spdm_response->assoc_cert_slot_mask = assoc_cert_slot_mask;
     176              : 
     177            2 :     if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_14) {
     178            1 :         ptr = (uint8_t *)spdm_response + sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
     179            1 :         pqc_asym_algo_cap_len = sizeof(pqc_asym_algo_capabilities);
     180            1 :         current_pqc_asym_algo_len = sizeof(current_pqc_asym_algo);
     181            1 :         *ptr = pqc_asym_algo_cap_len;
     182            1 :         ptr += sizeof(uint8_t);
     183            1 :         libspdm_write_uint32 (ptr, pqc_asym_algo_capabilities);
     184            1 :         ptr += sizeof(uint32_t);
     185            1 :         *ptr = current_pqc_asym_algo_len;
     186            1 :         ptr += sizeof(uint8_t);
     187            1 :         libspdm_write_uint32 (ptr, current_pqc_asym_algo);
     188            1 :         ptr += sizeof(uint32_t);
     189              :     }
     190              : 
     191            2 :     return LIBSPDM_STATUS_SUCCESS;
     192              : }
     193              : 
     194              : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
        

Generated by: LCOV version 2.0-1