LCOV - code coverage report
Current view: top level - unit_test/test_spdm_requester - get_key_pair_info.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 21.7 % 286 62
Test Date: 2025-06-29 08:09:00 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024 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 "spdm_unit_test.h"
       8              : #include "internal/libspdm_requester_lib.h"
       9              : #include "internal/libspdm_secured_message_lib.h"
      10              : 
      11              : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
      12              : 
      13              : uint8_t m_response_total_key_pairs;
      14              : uint8_t m_response_key_pair_id;
      15              : uint16_t m_response_capabilities;
      16              : uint16_t m_response_key_usage_capabilities;
      17              : uint16_t m_response_current_key_usage;
      18              : uint32_t m_response_asym_algo_capabilities;
      19              : uint32_t m_response_current_asym_algo;
      20              : uint8_t m_response_assoc_cert_slot_mask;
      21              : 
      22            1 : libspdm_return_t libspdm_requester_get_key_pair_info_test_send_message(
      23              :     void *spdm_context, size_t request_size, const void *request,
      24              :     uint64_t timeout)
      25              : {
      26              :     libspdm_test_context_t *spdm_test_context;
      27              : 
      28            1 :     spdm_test_context = libspdm_get_test_context();
      29            1 :     switch (spdm_test_context->case_id) {
      30            1 :     case 0x1:
      31              :     case 0x2:
      32            1 :         return LIBSPDM_STATUS_SUCCESS;
      33            0 :     default:
      34            0 :         return LIBSPDM_STATUS_SEND_FAIL;
      35              :     }
      36              : }
      37              : 
      38            1 : libspdm_return_t libspdm_requester_get_key_pair_info_test_receive_message(
      39              :     void *spdm_context, size_t *response_size,
      40              :     void **response, uint64_t timeout)
      41              : {
      42              :     libspdm_test_context_t *spdm_test_context;
      43              : 
      44            1 :     spdm_test_context = libspdm_get_test_context();
      45            1 :     switch (spdm_test_context->case_id) {
      46            1 :     case 0x1: {
      47              :         spdm_key_pair_info_response_t *spdm_response;
      48              :         size_t spdm_response_size;
      49              :         size_t transport_header_size;
      50              : 
      51              :         uint8_t total_key_pairs;
      52              :         uint8_t key_pair_id;
      53              :         uint16_t public_key_info_len;
      54              :         uint16_t capabilities;
      55              :         uint16_t key_usage_capabilities;
      56              :         uint16_t current_key_usage;
      57              :         uint32_t asym_algo_capabilities;
      58              :         uint32_t current_asym_algo;
      59              :         uint8_t assoc_cert_slot_mask;
      60              : 
      61            1 :         uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
      62              :                                              0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
      63              : 
      64            1 :         key_pair_id = 1;
      65            1 :         total_key_pairs = 1;
      66            1 :         public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
      67              : 
      68            1 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
      69            1 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
      70            1 :         spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
      71              : 
      72            1 :         capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
      73            1 :         key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      74            1 :         current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      75            1 :         asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
      76            1 :         current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
      77              : 
      78              :         /*association with slot 1*/
      79            1 :         assoc_cert_slot_mask = 0x02;
      80              : 
      81            1 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
      82            1 :         spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
      83            1 :         spdm_response->header.param1 = 0;
      84            1 :         spdm_response->header.param2 = 0;
      85            1 :         spdm_response->total_key_pairs = total_key_pairs;
      86            1 :         spdm_response->key_pair_id = key_pair_id;
      87            1 :         spdm_response->capabilities = capabilities;
      88            1 :         spdm_response->key_usage_capabilities = key_usage_capabilities;
      89            1 :         spdm_response->current_key_usage = current_key_usage;
      90            1 :         spdm_response->asym_algo_capabilities = asym_algo_capabilities;
      91            1 :         spdm_response->current_asym_algo = current_asym_algo;
      92            1 :         spdm_response->public_key_info_len = public_key_info_len;
      93            1 :         spdm_response->assoc_cert_slot_mask = assoc_cert_slot_mask;
      94              : 
      95            1 :         libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
      96              :                          public_key_info_rsa2048, public_key_info_len);
      97              : 
      98            1 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
      99              :                                               false, spdm_response_size,
     100              :                                               spdm_response, response_size,
     101              :                                               response);
     102              :     }
     103            1 :         return LIBSPDM_STATUS_SUCCESS;
     104              : 
     105            0 :     case 0x2: {
     106              :         spdm_key_pair_info_response_t *spdm_response;
     107              :         size_t spdm_response_size;
     108              :         size_t transport_header_size;
     109              : 
     110              :         uint16_t public_key_info_len;
     111            0 :         uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
     112              :                                              0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
     113              : 
     114            0 :         public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
     115              : 
     116            0 :         transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
     117            0 :         spdm_response = (void *)((uint8_t *)*response + transport_header_size);
     118            0 :         spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
     119              : 
     120            0 :         spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
     121            0 :         spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
     122            0 :         spdm_response->header.param1 = 0;
     123            0 :         spdm_response->header.param2 = 0;
     124            0 :         spdm_response->total_key_pairs = m_response_total_key_pairs;
     125            0 :         spdm_response->key_pair_id = m_response_key_pair_id;
     126            0 :         spdm_response->capabilities = m_response_capabilities;
     127            0 :         spdm_response->key_usage_capabilities = m_response_key_usage_capabilities;
     128            0 :         spdm_response->current_key_usage = m_response_current_key_usage;
     129            0 :         spdm_response->asym_algo_capabilities = m_response_asym_algo_capabilities;
     130            0 :         spdm_response->current_asym_algo = m_response_current_asym_algo;
     131            0 :         spdm_response->public_key_info_len = public_key_info_len;
     132            0 :         spdm_response->assoc_cert_slot_mask = m_response_assoc_cert_slot_mask;
     133              : 
     134              : 
     135            0 :         libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
     136              :                          public_key_info_rsa2048, public_key_info_len);
     137            0 :         libspdm_transport_test_encode_message(spdm_context, NULL, false,
     138              :                                               false, spdm_response_size,
     139              :                                               spdm_response, response_size,
     140              :                                               response);
     141              :     }
     142            0 :         return LIBSPDM_STATUS_SUCCESS;
     143              : 
     144            0 :     default:
     145            0 :         return LIBSPDM_STATUS_RECEIVE_FAIL;
     146              :     }
     147              : }
     148              : 
     149              : /**
     150              :  * Test 1: Successful response to get key pair info
     151              :  * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
     152              :  **/
     153            1 : void libspdm_test_requester_get_key_pair_info_case1(void **state)
     154              : {
     155              :     libspdm_return_t status;
     156              :     libspdm_test_context_t *spdm_test_context;
     157              :     libspdm_context_t *spdm_context;
     158              : 
     159              :     uint8_t key_pair_id;
     160              :     uint8_t associated_slot_id;
     161              :     uint8_t total_key_pairs;
     162              :     uint16_t capabilities;
     163              :     uint16_t key_usage_capabilities;
     164              :     uint16_t current_key_usage;
     165              :     uint32_t asym_algo_capabilities;
     166              :     uint32_t current_asym_algo;
     167              :     uint16_t public_key_info_len;
     168              :     uint8_t assoc_cert_slot_mask;
     169              :     uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
     170              : 
     171            1 :     key_pair_id = 1;
     172            1 :     associated_slot_id = 1;
     173            1 :     spdm_test_context = *state;
     174            1 :     spdm_context = spdm_test_context->spdm_context;
     175            1 :     spdm_test_context->case_id = 0x1;
     176            1 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     177              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     178              : 
     179            1 :     spdm_context->connection_info.connection_state =
     180              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     181            1 :     spdm_context->connection_info.capability.flags |=
     182              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
     183              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
     184              : 
     185            1 :     spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
     186            1 :     public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
     187              : 
     188            1 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     189              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     190              :                                        &asym_algo_capabilities, &current_asym_algo,
     191              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     192              :                                        public_key_info);
     193              : 
     194            1 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     195            1 :     assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
     196            1 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     197            1 :     assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     198            1 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     199            1 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     200            1 : }
     201              : 
     202              : /**
     203              :  * Test 2: The collection of multiple sub-cases.
     204              :  **/
     205            0 : void libspdm_test_requester_get_key_pair_info_case3(void **state)
     206              : {
     207              :     libspdm_return_t status;
     208              :     libspdm_test_context_t *spdm_test_context;
     209              :     libspdm_context_t *spdm_context;
     210              : 
     211              :     uint8_t key_pair_id;
     212              :     uint8_t associated_slot_id;
     213              :     uint8_t total_key_pairs;
     214              :     uint16_t capabilities;
     215              :     uint16_t key_usage_capabilities;
     216              :     uint16_t current_key_usage;
     217              :     uint32_t asym_algo_capabilities;
     218              :     uint32_t current_asym_algo;
     219              :     uint16_t public_key_info_len;
     220              :     uint8_t assoc_cert_slot_mask;
     221              :     uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
     222              : 
     223            0 :     spdm_test_context = *state;
     224            0 :     spdm_context = spdm_test_context->spdm_context;
     225            0 :     spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
     226              :                                             SPDM_VERSION_NUMBER_SHIFT_BIT;
     227              : 
     228            0 :     spdm_context->connection_info.connection_state =
     229              :         LIBSPDM_CONNECTION_STATE_NEGOTIATED;
     230            0 :     spdm_context->connection_info.capability.flags |=
     231              :         SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
     232              : 
     233            0 :     spdm_test_context->case_id = 0x2;
     234            0 :     public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
     235              : 
     236              :     /* Sub Case 1: key_pair_id > total_key_pairs*/
     237            0 :     key_pair_id = 2;
     238            0 :     associated_slot_id = 1;
     239            0 :     spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
     240              : 
     241            0 :     m_response_total_key_pairs = 1;
     242            0 :     m_response_key_pair_id = 2;
     243            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     244            0 :     m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     245            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     246            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     247            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     248              :     /* association with slot 1*/
     249            0 :     m_response_assoc_cert_slot_mask = 0x2;
     250              : 
     251            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     252              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     253              :                                        &asym_algo_capabilities, &current_asym_algo,
     254              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     255              :                                        public_key_info);
     256              : 
     257            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     258              : 
     259              :     /* Sub Case 2: responder and requester, the KeyPairID are not consistent.*/
     260            0 :     key_pair_id = 0xA0;
     261            0 :     associated_slot_id = 1;
     262            0 :     spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
     263              : 
     264            0 :     m_response_total_key_pairs = 1;
     265            0 :     m_response_key_pair_id = 1;
     266            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     267            0 :     m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     268            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     269            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     270            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     271              :     /* association with slot 1*/
     272            0 :     m_response_assoc_cert_slot_mask = 0x2;
     273              : 
     274            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     275              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     276              :                                        &asym_algo_capabilities, &current_asym_algo,
     277              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     278              :                                        public_key_info);
     279              : 
     280            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     281              : 
     282              :     /* Sub Case 3: not set capabilities.*/
     283            0 :     key_pair_id = 1;
     284            0 :     associated_slot_id = 1;
     285            0 :     spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
     286              : 
     287            0 :     m_response_total_key_pairs = 1;
     288            0 :     m_response_key_pair_id = 1;
     289            0 :     m_response_capabilities = 0;
     290            0 :     m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     291            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     292            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     293            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     294              :     /* association with slot 1*/
     295            0 :     m_response_assoc_cert_slot_mask = 0x2;
     296              : 
     297            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     298              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     299              :                                        &asym_algo_capabilities, &current_asym_algo,
     300              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     301              :                                        public_key_info);
     302              : 
     303            0 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     304            0 :     assert_int_equal(capabilities, 0);
     305            0 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     306            0 :     assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     307            0 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     308            0 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     309              : 
     310              :     /* Sub Case 4: This bit shall not be set if CertAssocCap is not set.
     311              :      *              Set the ShareableCap bit and not set the CertAssocCap bit.*/
     312            0 :     key_pair_id = 1;
     313            0 :     associated_slot_id = 1;
     314            0 :     spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
     315              : 
     316            0 :     m_response_total_key_pairs = 1;
     317            0 :     m_response_key_pair_id = 1;
     318              : 
     319            0 :     m_response_capabilities = 0;
     320            0 :     m_response_capabilities |= SPDM_KEY_PAIR_CAP_SHAREABLE_CAP;
     321            0 :     m_response_capabilities &= ~SPDM_KEY_PAIR_CAP_CERT_ASSOC_CAP;
     322              : 
     323            0 :     m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     324            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     325            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     326            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     327              :     /* association with slot 1*/
     328            0 :     m_response_assoc_cert_slot_mask = 0x2;
     329              : 
     330            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     331              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     332              :                                        &asym_algo_capabilities, &current_asym_algo,
     333              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     334              :                                        public_key_info);
     335              : 
     336            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     337              : 
     338              :     /* Sub Case 5: KeyUsageCapabilities at least one bit shall be set, not set KeyUsageCapabilities.*/
     339            0 :     m_response_total_key_pairs = 1;
     340            0 :     m_response_key_pair_id = 1;
     341            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     342            0 :     m_response_key_usage_capabilities =0;
     343            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     344            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     345            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     346              :     /* association with slot 1*/
     347            0 :     m_response_assoc_cert_slot_mask = 0x2;
     348              : 
     349            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     350              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     351              :                                        &asym_algo_capabilities, &current_asym_algo,
     352              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     353              :                                        public_key_info);
     354              : 
     355            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     356              : 
     357              :     /* Sub Case 6: KeyUsageCapabilities Set multiple bits.*/
     358            0 :     m_response_total_key_pairs = 1;
     359            0 :     m_response_key_pair_id = 1;
     360            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     361            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     362              :                                         SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
     363              :                                         SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
     364              :                                         SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
     365              :                                         SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
     366              :                                         SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
     367            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     368            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     369            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     370              :     /* association with slot 1*/
     371            0 :     m_response_assoc_cert_slot_mask = 0x2;
     372              : 
     373            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     374              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     375              :                                        &asym_algo_capabilities, &current_asym_algo,
     376              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     377              :                                        public_key_info);
     378              : 
     379            0 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     380            0 :     assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
     381            0 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK);
     382            0 :     assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     383            0 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     384            0 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     385              : 
     386              :     /* Sub Case 7: not set CurrentKeyUsage*/
     387            0 :     m_response_total_key_pairs = 1;
     388            0 :     m_response_key_pair_id = 1;
     389            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     390            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     391            0 :     m_response_current_key_usage = 0;
     392            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     393            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     394              :     /* association with slot 1*/
     395            0 :     m_response_assoc_cert_slot_mask = 0x2;
     396              : 
     397            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     398              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     399              :                                        &asym_algo_capabilities, &current_asym_algo,
     400              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     401              :                                        public_key_info);
     402            0 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     403            0 :     assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
     404            0 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     405            0 :     assert_int_equal(current_key_usage, 0);
     406            0 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     407            0 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     408              : 
     409              :     /* Sub Case 8:  CurrentKeyUsage Set multiple bits.*/
     410            0 :     m_response_total_key_pairs = 1;
     411            0 :     m_response_key_pair_id = 1;
     412            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     413            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     414            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     415              :                                    SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
     416              :                                    SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
     417              :                                    SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
     418              :                                    SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
     419              :                                    SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
     420            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     421            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     422              :     /* association with slot 1*/
     423            0 :     m_response_assoc_cert_slot_mask = 0x2;
     424              : 
     425            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     426              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     427              :                                        &asym_algo_capabilities, &current_asym_algo,
     428              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     429              :                                        public_key_info);
     430            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     431              : 
     432              :     /* Sub Case 9:  CurrentKeyUsage and KeyUsageCapabilities, Set multiple bits.*/
     433            0 :     m_response_total_key_pairs = 1;
     434            0 :     m_response_key_pair_id = 1;
     435            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     436            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     437              :                                         SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
     438            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     439              :                                    SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
     440            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     441            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     442              :     /* association with slot 1*/
     443            0 :     m_response_assoc_cert_slot_mask = 0x2;
     444              : 
     445            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     446              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     447              :                                        &asym_algo_capabilities, &current_asym_algo,
     448              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     449              :                                        public_key_info);
     450            0 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     451            0 :     assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
     452            0 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     453              :                      SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
     454            0 :     assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
     455              :                      SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
     456            0 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     457            0 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     458              : 
     459              :     /* Sub Case 10: CurrentKeyUsage and KeyUsageCapabilities are not consistent.*/
     460            0 :     m_response_total_key_pairs = 1;
     461            0 :     m_response_key_pair_id = 1;
     462            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     463            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
     464            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     465            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     466            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     467              :     /* association with slot 1*/
     468            0 :     m_response_assoc_cert_slot_mask = 0x2;
     469              : 
     470            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     471              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     472              :                                        &asym_algo_capabilities, &current_asym_algo,
     473              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     474              :                                        public_key_info);
     475            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     476              : 
     477              :     /* Sub Case 11: AsymAlgoCapabilities, at least one bit shall be set.not set AsymAlgoCapabilities*/
     478            0 :     m_response_total_key_pairs = 1;
     479            0 :     m_response_key_pair_id = 1;
     480            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     481            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     482            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     483            0 :     m_response_asym_algo_capabilities = 0;
     484            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     485              :     /* association with slot 1*/
     486            0 :     m_response_assoc_cert_slot_mask = 0x2;
     487              : 
     488            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     489              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     490              :                                        &asym_algo_capabilities, &current_asym_algo,
     491              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     492              :                                        public_key_info);
     493              : 
     494            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     495              : 
     496              :     /* Sub Case 12: AsymAlgoCapabilities Set multiple bits.*/
     497            0 :     m_response_total_key_pairs = 1;
     498            0 :     m_response_key_pair_id = 1;
     499            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     500            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     501            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     502            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
     503              :                                         SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448;
     504            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     505              :     /* association with slot 1*/
     506            0 :     m_response_assoc_cert_slot_mask = 0x2;
     507              : 
     508            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     509              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     510              :                                        &asym_algo_capabilities, &current_asym_algo,
     511              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     512              :                                        public_key_info);
     513              : 
     514            0 :     assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
     515            0 :     assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
     516            0 :     assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     517            0 :     assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
     518            0 :     assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
     519              :                      SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448);
     520            0 :     assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
     521              : 
     522              :     /* Sub Case 13: CurrentAsymAlgo Set bits more than one bit.*/
     523            0 :     m_response_total_key_pairs = 1;
     524            0 :     m_response_key_pair_id = 1;
     525            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     526            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     527            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     528            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     529            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
     530              :                                    SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
     531              :     /* association with slot 1*/
     532            0 :     m_response_assoc_cert_slot_mask = 0x2;
     533              : 
     534            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     535              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     536              :                                        &asym_algo_capabilities, &current_asym_algo,
     537              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     538              :                                        public_key_info);
     539              : 
     540            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     541              : 
     542              :     /* Sub Case 14: AsymAlgoCapabilities and AsymAlgoCapabilities are not consistent.*/
     543            0 :     m_response_total_key_pairs = 1;
     544            0 :     m_response_key_pair_id = 1;
     545            0 :     m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
     546            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     547            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     548            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
     549            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     550              :     /* association with slot 1*/
     551            0 :     m_response_assoc_cert_slot_mask = 0x2;
     552              : 
     553            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     554              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     555              :                                        &asym_algo_capabilities, &current_asym_algo,
     556              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     557              :                                        public_key_info);
     558              : 
     559            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     560              : 
     561              :     /* Sub Case 15: AssocCertSlotMask set more than one bit, but ShareableCap is not set.*/
     562            0 :     m_response_total_key_pairs = 1;
     563            0 :     m_response_key_pair_id = 1;
     564            0 :     m_response_capabilities = 0;
     565            0 :     m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     566            0 :     m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     567            0 :     m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     568            0 :     m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
     569              :     /* association with slot 1*/
     570            0 :     m_response_assoc_cert_slot_mask = 0xFF;
     571              : 
     572            0 :     status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
     573              :                                        &capabilities, &key_usage_capabilities, &current_key_usage,
     574              :                                        &asym_algo_capabilities, &current_asym_algo,
     575              :                                        &assoc_cert_slot_mask, &public_key_info_len,
     576              :                                        public_key_info);
     577              : 
     578            0 :     assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
     579            0 : }
     580              : 
     581            1 : int libspdm_requester_get_key_pair_info_test_main(void)
     582              : {
     583            1 :     const struct CMUnitTest spdm_requester_get_key_pair_info_tests[] = {
     584              :         /* Successful response to get key pair info, key_pair_id is 1*/
     585              :         cmocka_unit_test(libspdm_test_requester_get_key_pair_info_case1),
     586              :     };
     587              : 
     588            1 :     libspdm_test_context_t test_context = {
     589              :         LIBSPDM_TEST_CONTEXT_VERSION,
     590              :         true,
     591              :         libspdm_requester_get_key_pair_info_test_send_message,
     592              :         libspdm_requester_get_key_pair_info_test_receive_message,
     593              :     };
     594              : 
     595            1 :     libspdm_setup_test_context(&test_context);
     596              : 
     597            1 :     return cmocka_run_group_tests(spdm_requester_get_key_pair_info_tests,
     598              :                                   libspdm_unit_test_group_setup,
     599              :                                   libspdm_unit_test_group_teardown);
     600              : }
     601              : 
     602              : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
        

Generated by: LCOV version 2.0-1