LCOV - code coverage report
Current view: top level - os_stub/spdm_device_secret_lib_sample - key_pair.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.7 % 165 153
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 6 6

            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 <stdarg.h>
       8              : #include <stddef.h>
       9              : #include <setjmp.h>
      10              : #include <stdint.h>
      11              : #include <stdlib.h>
      12              : #include <stdio.h>
      13              : #include <assert.h>
      14              : #include <string.h>
      15              : 
      16              : #include <base.h>
      17              : #include "library/memlib.h"
      18              : #include "spdm_device_secret_lib_internal.h"
      19              : #include "internal/libspdm_common_lib.h"
      20              : 
      21              : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
      22              : typedef struct {
      23              :     uint16_t capabilities;
      24              :     uint16_t key_usage_capabilities;
      25              :     uint16_t current_key_usage;
      26              :     uint32_t asym_algo_capabilities;
      27              :     uint32_t current_asym_algo;
      28              :     uint16_t public_key_info_len;
      29              :     uint8_t assoc_cert_slot_mask;
      30              :     uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
      31              : } libspdm_key_pair_info_t;
      32              : 
      33              : #define LIBSPDM_MAX_KEY_PAIR_COUNT 9
      34              : 
      35              : libspdm_key_pair_info_t m_key_pair_info[LIBSPDM_MAX_KEY_PAIR_COUNT];
      36              : 
      37              : uint8_t m_total_key_pair_count = 0;
      38              : 
      39            1 : void libspdm_init_key_pair_info() {
      40              : #if (LIBSPDM_RSA_SSA_SUPPORT || LIBSPDM_RSA_PSS_SUPPORT)
      41            1 :     uint8_t public_key_info_rsa[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
      42              :                                      0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
      43              : #endif
      44              : #if LIBSPDM_ECDSA_P256_SUPPORT
      45            1 :     uint8_t public_key_info_ecp256[] = {0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      46              :                                         0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      47              :                                         0x03, 0x01, 0x07};
      48              : #endif
      49              : #if LIBSPDM_ECDSA_P384_SUPPORT
      50            1 :     uint8_t public_key_info_ecp384[] = {0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      51              :                                         0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22};
      52              : #endif
      53              : #if LIBSPDM_ECDSA_P521_SUPPORT
      54            1 :     uint8_t public_key_info_ecp521[] = {0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      55              :                                         0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23};
      56              : #endif
      57              : #if LIBSPDM_SM2_DSA_P256_SUPPORT
      58              :     uint8_t public_key_info_sm2[] = {0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
      59              :                                      0x02, 0x01, 0x06, 0x08, 0x2A, 0x81, 0x1C, 0xCF, 0x55,
      60              :                                      0x01, 0x82, 0x2D};
      61              : #endif
      62              : #if LIBSPDM_EDDSA_ED25519_SUPPORT
      63              :     uint8_t public_key_info_ed25519[] = {0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70};
      64              : #endif
      65              : #if LIBSPDM_EDDSA_ED448_SUPPORT
      66              :     uint8_t public_key_info_ed448[] = {0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x71};
      67              : #endif
      68            1 :     uint8_t index = 0;
      69              :     /*provisioned key pair info*/
      70              : 
      71              : #if (LIBSPDM_RSA_SSA_2048_SUPPORT || LIBSPDM_RSA_PSS_2048_SUPPORT)
      72              :     /*key_pair_id 1*/
      73            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
      74            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      75            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      76            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
      77            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x01;
      78            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
      79            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_rsa);
      80            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
      81            1 :                      m_key_pair_info[index].public_key_info_len,
      82            1 :                      public_key_info_rsa, m_key_pair_info[index].public_key_info_len);
      83            1 :     index++;
      84              : #endif
      85              : 
      86              : #if (LIBSPDM_RSA_SSA_3072_SUPPORT || LIBSPDM_RSA_PSS_3072_SUPPORT)
      87              :     /*key_pair_id 2*/
      88            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
      89            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      90            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
      91            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
      92            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x02;
      93            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
      94            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_rsa);
      95            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
      96            1 :                      m_key_pair_info[index].public_key_info_len,
      97            1 :                      public_key_info_rsa, m_key_pair_info[index].public_key_info_len);
      98            1 :     index++;
      99              : #endif
     100              : 
     101              : #if (LIBSPDM_RSA_SSA_4096_SUPPORT || LIBSPDM_RSA_PSS_4096_SUPPORT)
     102              :     /*key_pair_id 3*/
     103            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     104            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     105            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     106            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     107            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x04;
     108            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA4096;
     109            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_rsa);
     110            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     111            1 :                      m_key_pair_info[index].public_key_info_len,
     112            1 :                      public_key_info_rsa, m_key_pair_info[index].public_key_info_len);
     113            1 :     index++;
     114              : #endif
     115              : 
     116              : #if LIBSPDM_ECDSA_P256_SUPPORT
     117              :     /*key_pair_id 4*/
     118            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     119            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     120            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     121            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     122            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x08;
     123            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ECC256;
     124            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_ecp256);
     125            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     126            1 :                      m_key_pair_info[index].public_key_info_len,
     127            1 :                      public_key_info_ecp256, m_key_pair_info[index].public_key_info_len);
     128            1 :     index++;
     129              : #endif
     130              : 
     131              : #if LIBSPDM_ECDSA_P384_SUPPORT
     132              :     /*key_pair_id 5*/
     133            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     134            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     135            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     136            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     137            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x10;
     138            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ECC384;
     139            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_ecp384);
     140            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     141            1 :                      m_key_pair_info[index].public_key_info_len,
     142            1 :                      public_key_info_ecp384, m_key_pair_info[index].public_key_info_len);
     143            1 :     index++;
     144              : #endif
     145              : 
     146              : #if LIBSPDM_ECDSA_P521_SUPPORT
     147              :     /*key_pair_id 6*/
     148            1 :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     149            1 :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     150            1 :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     151            1 :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     152            1 :     m_key_pair_info[index].assoc_cert_slot_mask = 0x20;
     153            1 :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ECC521;
     154            1 :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_ecp521);
     155            1 :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     156            1 :                      m_key_pair_info[index].public_key_info_len,
     157            1 :                      public_key_info_ecp521, m_key_pair_info[index].public_key_info_len);
     158            1 :     index++;
     159              : #endif
     160              : 
     161              : #if LIBSPDM_SM2_DSA_P256_SUPPORT
     162              :     /*key_pair_id 7*/
     163              :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     164              :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     165              :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     166              :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     167              :     m_key_pair_info[index].assoc_cert_slot_mask = 0x40;
     168              :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_SM2;
     169              :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_sm2);
     170              :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     171              :                      m_key_pair_info[index].public_key_info_len,
     172              :                      public_key_info_sm2, m_key_pair_info[index].public_key_info_len);
     173              :     index++;
     174              : #endif
     175              : 
     176              : #if LIBSPDM_EDDSA_ED25519_SUPPORT
     177              :     /*key_pair_id 8*/
     178              :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     179              :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     180              :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     181              :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     182              :     m_key_pair_info[index].assoc_cert_slot_mask = 0x80;
     183              :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED25519;
     184              :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_ed25519);
     185              :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     186              :                      m_key_pair_info[index].public_key_info_len,
     187              :                      public_key_info_ed25519, m_key_pair_info[index].public_key_info_len);
     188              :     index++;
     189              : #endif
     190              : 
     191              : #if LIBSPDM_EDDSA_ED448_SUPPORT
     192              :     /*key_pair_id 9*/
     193              :     m_key_pair_info[index].capabilities = SPDM_KEY_PAIR_CAP_MASK;
     194              :     m_key_pair_info[index].key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     195              :     m_key_pair_info[index].current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
     196              :     m_key_pair_info[index].asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_MASK;
     197              :     m_key_pair_info[index].assoc_cert_slot_mask = 0x00;
     198              :     m_key_pair_info[index].current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448;
     199              :     m_key_pair_info[index].public_key_info_len = (uint16_t)sizeof(public_key_info_ed448);
     200              :     libspdm_copy_mem(m_key_pair_info[index].public_key_info,
     201              :                      m_key_pair_info[index].public_key_info_len,
     202              :                      public_key_info_ed448, m_key_pair_info[index].public_key_info_len);
     203              :     index++;
     204              : #endif
     205              : 
     206            1 :     m_total_key_pair_count = index;
     207            1 : }
     208              : 
     209           60 : uint8_t libspdm_read_total_key_pairs ()
     210              : {
     211           60 :     if (m_total_key_pair_count == 0) {
     212            1 :         libspdm_init_key_pair_info();
     213              :     }
     214           60 :     return m_total_key_pair_count;
     215              : }
     216              : 
     217              : /**
     218              :  * read the key pair info of the key_pair_id.
     219              :  *
     220              :  * @param  spdm_context               A pointer to the SPDM context.
     221              :  * @param  key_pair_id                Indicate which key pair ID's information to retrieve.
     222              :  *
     223              :  * @param  capabilities               Indicate the capabilities of the requested key pairs.
     224              :  * @param  key_usage_capabilities     Indicate the key usages the responder allows.
     225              :  * @param  current_key_usage          Indicate the currently configured key usage for the requested key pairs ID.
     226              :  * @param  asym_algo_capabilities     Indicate the asymmetric algorithms the Responder supports for this key pair ID.
     227              :  * @param  current_asym_algo          Indicate the currently configured asymmetric algorithm for this key pair ID.
     228              :  * @param  assoc_cert_slot_mask       This field is a bit mask representing the currently associated certificate slots.
     229              :  * @param  public_key_info_len        On input, indicate the size in bytes of the destination buffer to store.
     230              :  *                                    On output, indicate the size in bytes of the public_key_info.
     231              :  *                                    It can be NULL, if public_key_info is not required.
     232              :  * @param  public_key_info            A pointer to a destination buffer to store the public_key_info.
     233              :  *                                    It can be NULL, if public_key_info is not required.
     234              :  *
     235              :  * @retval true  get key pair info successfully.
     236              :  * @retval false get key pair info failed.
     237              :  **/
     238           15 : bool libspdm_read_key_pair_info(
     239              :     void *spdm_context,
     240              :     uint8_t key_pair_id,
     241              :     uint16_t *capabilities,
     242              :     uint16_t *key_usage_capabilities,
     243              :     uint16_t *current_key_usage,
     244              :     uint32_t *asym_algo_capabilities,
     245              :     uint32_t *current_asym_algo,
     246              :     uint8_t *assoc_cert_slot_mask,
     247              :     uint16_t *public_key_info_len,
     248              :     uint8_t *public_key_info)
     249              : {
     250           15 :     LIBSPDM_DEBUG_CODE (
     251              :         uint8_t total_key_pairs;
     252              :         libspdm_data_parameter_t parameter;
     253              :         size_t data_return_size;
     254              :         libspdm_return_t status;
     255              : 
     256              :         parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
     257              :         data_return_size = sizeof(uint8_t);
     258              :         status = libspdm_get_data(spdm_context, LIBSPDM_DATA_TOTAL_KEY_PAIRS,
     259              :                                   &parameter, &total_key_pairs, &data_return_size);
     260              :         if (status != LIBSPDM_STATUS_SUCCESS) {
     261              :         return false;
     262              :     }
     263              : 
     264              :         LIBSPDM_ASSERT(total_key_pairs == libspdm_read_total_key_pairs());
     265              :         );
     266              : 
     267              :     /*check*/
     268           15 :     if (key_pair_id > libspdm_read_total_key_pairs()) {
     269            0 :         return false;
     270              :     }
     271              : 
     272           15 :     if (public_key_info_len != NULL) {
     273            1 :         if (*public_key_info_len < m_key_pair_info[key_pair_id - 1].public_key_info_len) {
     274            0 :             return false;
     275              :         }
     276              :     }
     277              : 
     278              :     /*output*/
     279           15 :     *capabilities = m_key_pair_info[key_pair_id - 1].capabilities;
     280           15 :     *key_usage_capabilities = m_key_pair_info[key_pair_id - 1].key_usage_capabilities;
     281           15 :     *current_key_usage = m_key_pair_info[key_pair_id - 1].current_key_usage;
     282           15 :     *asym_algo_capabilities = m_key_pair_info[key_pair_id - 1].asym_algo_capabilities;
     283           15 :     *current_asym_algo = m_key_pair_info[key_pair_id - 1].current_asym_algo;
     284           15 :     *assoc_cert_slot_mask = m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask;
     285              : 
     286           15 :     if (public_key_info_len != NULL) {
     287            1 :         *public_key_info_len = m_key_pair_info[key_pair_id - 1].public_key_info_len;
     288              :     }
     289           15 :     if (public_key_info != NULL) {
     290            1 :         libspdm_copy_mem(public_key_info, *public_key_info_len,
     291            1 :                          m_key_pair_info[key_pair_id - 1].public_key_info, *public_key_info_len);
     292              : 
     293              :     }
     294              : 
     295           15 :     return true;
     296              : }
     297              : #endif /* LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP */
     298              : 
     299              : #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP
     300              : 
     301              : typedef struct
     302              : {
     303              :     uint8_t key_pair_id;
     304              :     uint8_t operation;
     305              :     uint16_t desired_key_usage;
     306              :     uint32_t desired_asym_algo;
     307              :     uint8_t desired_assoc_cert_slot_mask;
     308              : } libspdm_cached_key_pair_info_data_t;
     309              : 
     310              : 
     311            8 : bool libspdm_read_cached_last_set_key_pair_info_request(uint8_t **last_set_key_pair_info_request,
     312              :                                                         size_t *last_set_key_pair_info_request_len)
     313              : {
     314              :     bool res;
     315            8 :     char file[] = "cached_last_set_key_pair_info_request";
     316              : 
     317            8 :     res = libspdm_read_input_file(file, (void **)last_set_key_pair_info_request,
     318              :                                   last_set_key_pair_info_request_len);
     319              : 
     320            8 :     return res;
     321              : }
     322              : 
     323            5 : bool libspdm_cache_last_set_key_pair_info_request(const uint8_t *last_set_key_pair_info_request,
     324              :                                                   size_t last_set_key_pair_info_request_len)
     325              : {
     326              :     bool res;
     327            5 :     char file[] = "cached_last_set_key_pair_info_request";
     328              : 
     329            5 :     res = libspdm_write_output_file(file, last_set_key_pair_info_request,
     330              :                                     last_set_key_pair_info_request_len);
     331              : 
     332            5 :     return res;
     333              : }
     334              : 
     335           11 : bool libspdm_write_key_pair_info(
     336              :     void *spdm_context,
     337              :     uint8_t key_pair_id,
     338              :     uint8_t operation,
     339              :     uint16_t desired_key_usage,
     340              :     uint32_t desired_asym_algo,
     341              :     uint8_t desired_assoc_cert_slot_mask,
     342              :     bool *need_reset)
     343              : {
     344              :     bool result;
     345              :     libspdm_cached_key_pair_info_data_t *cached_key_pair_info;
     346              :     libspdm_cached_key_pair_info_data_t current_key_pair_info;
     347              :     size_t cached_key_pair_info_len;
     348              : 
     349           11 :     LIBSPDM_DEBUG_CODE (
     350              :         uint8_t total_key_pairs;
     351              :         libspdm_data_parameter_t parameter;
     352              :         size_t data_return_size;
     353              :         libspdm_return_t status;
     354              : 
     355              :         parameter.location = LIBSPDM_DATA_LOCATION_LOCAL;
     356              :         data_return_size = sizeof(uint8_t);
     357              :         status = libspdm_get_data(spdm_context, LIBSPDM_DATA_TOTAL_KEY_PAIRS,
     358              :                                   &parameter, &total_key_pairs, &data_return_size);
     359              :         if (status != LIBSPDM_STATUS_SUCCESS) {
     360              :         return false;
     361              :     }
     362              : 
     363              :         LIBSPDM_ASSERT(total_key_pairs == libspdm_read_total_key_pairs());
     364              :         );
     365              : 
     366              :     /*check*/
     367           11 :     if (key_pair_id > libspdm_read_total_key_pairs()) {
     368            0 :         return false;
     369              :     }
     370              : 
     371           11 :     cached_key_pair_info_len = 0;
     372           11 :     if (*need_reset) {
     373            8 :         result = libspdm_read_cached_last_set_key_pair_info_request(
     374              :             (uint8_t **)&cached_key_pair_info,
     375              :             &cached_key_pair_info_len);
     376              : 
     377            8 :         if ((result) &&
     378            7 :             (cached_key_pair_info_len == sizeof(libspdm_cached_key_pair_info_data_t)) &&
     379            7 :             (cached_key_pair_info->operation == operation) &&
     380            5 :             (cached_key_pair_info->key_pair_id == key_pair_id) &&
     381            4 :             (cached_key_pair_info->desired_key_usage == desired_key_usage) &&
     382            3 :             (cached_key_pair_info->desired_asym_algo == desired_asym_algo) &&
     383            3 :             (cached_key_pair_info->desired_assoc_cert_slot_mask == desired_assoc_cert_slot_mask)) {
     384            3 :             if (operation == SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION) {
     385            1 :                 m_key_pair_info[key_pair_id - 1].current_key_usage = 0;
     386            1 :                 m_key_pair_info[key_pair_id - 1].current_asym_algo = 0;
     387            1 :                 m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask = 0;
     388            2 :             } else if (operation == SPDM_SET_KEY_PAIR_INFO_GENERATE_OPERATION) {
     389            0 :                 m_key_pair_info[key_pair_id - 1].current_key_usage = desired_key_usage;
     390            0 :                 m_key_pair_info[key_pair_id - 1].current_asym_algo = desired_asym_algo;
     391            0 :                 m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask =
     392              :                     desired_assoc_cert_slot_mask;
     393            2 :             } else if (operation == SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION) {
     394            2 :                 if (desired_key_usage != 0) {
     395            1 :                     m_key_pair_info[key_pair_id - 1].current_key_usage = desired_key_usage;
     396              :                 }
     397            2 :                 if (desired_asym_algo != 0) {
     398            1 :                     m_key_pair_info[key_pair_id - 1].current_asym_algo = desired_asym_algo;
     399              :                 }
     400            2 :                 m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask =
     401              :                     desired_assoc_cert_slot_mask;
     402              :             } else {
     403            0 :                 return false;
     404              :             }
     405              : 
     406              :             /*device don't need reset this time*/
     407            3 :             *need_reset = false;
     408            3 :             free(cached_key_pair_info);
     409            3 :             return true;
     410              :         } else {
     411            5 :             if (cached_key_pair_info != NULL) {
     412            4 :                 free(cached_key_pair_info);
     413              :             }
     414              : 
     415            5 :             current_key_pair_info.operation = operation;
     416            5 :             current_key_pair_info.key_pair_id = key_pair_id;
     417            5 :             current_key_pair_info.desired_key_usage = desired_key_usage;
     418            5 :             current_key_pair_info.desired_asym_algo = desired_asym_algo;
     419            5 :             current_key_pair_info.desired_assoc_cert_slot_mask = desired_assoc_cert_slot_mask;
     420              :             /*device need reset this time: cache the last_set_key_pair_info_request */
     421            5 :             result = libspdm_cache_last_set_key_pair_info_request(
     422              :                 (const uint8_t *)&current_key_pair_info,
     423              :                 sizeof(libspdm_cached_key_pair_info_data_t));
     424            5 :             if (!result) {
     425            0 :                 return result;
     426              :             }
     427              : 
     428              :             /*device need reset this time*/
     429            5 :             *need_reset = true;
     430            5 :             return true;
     431              :         }
     432              :     } else {
     433            3 :         if (operation == SPDM_SET_KEY_PAIR_INFO_ERASE_OPERATION) {
     434            1 :             m_key_pair_info[key_pair_id - 1].current_key_usage = 0;
     435            1 :             m_key_pair_info[key_pair_id - 1].current_asym_algo = 0;
     436            1 :             m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask = 0;
     437            2 :         } else if (operation == SPDM_SET_KEY_PAIR_INFO_GENERATE_OPERATION) {
     438            0 :             m_key_pair_info[key_pair_id - 1].current_key_usage = desired_key_usage;
     439            0 :             m_key_pair_info[key_pair_id - 1].current_asym_algo = desired_asym_algo;
     440            0 :             m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask = desired_assoc_cert_slot_mask;
     441            2 :         } else if (operation == SPDM_SET_KEY_PAIR_INFO_CHANGE_OPERATION) {
     442            2 :             if (desired_key_usage != 0) {
     443            1 :                 m_key_pair_info[key_pair_id - 1].current_key_usage = desired_key_usage;
     444              :             }
     445            2 :             if (desired_asym_algo != 0) {
     446            1 :                 m_key_pair_info[key_pair_id - 1].current_asym_algo = desired_asym_algo;
     447              :             }
     448            2 :             m_key_pair_info[key_pair_id - 1].assoc_cert_slot_mask = desired_assoc_cert_slot_mask;
     449              :         } else {
     450            0 :             return false;
     451              :         }
     452              : 
     453            3 :         return true;
     454              :     }
     455              : }
     456              : #endif /* #if LIBSPDM_ENABLE_CAPABILITY_SET_KEY_PAIR_INFO_CAP */
        

Generated by: LCOV version 2.0-1