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-10-12 08:10:56 Functions: 80.0 % 5 4

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

Generated by: LCOV version 2.0-1