Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
laurenw-arm | 00185ea | 2023-08-15 14:53:59 -0500 | [diff] [blame] | 2 | * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved. |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
laurenw-arm | 00185ea | 2023-08-15 14:53:59 -0500 | [diff] [blame] | 7 | #include <assert.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 8 | #include <getopt.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
Robin van der Gracht | 06b5cdb | 2023-09-12 11:16:23 +0200 | [diff] [blame] | 13 | /* Suppress OpenSSL engine deprecation warnings */ |
| 14 | #define OPENSSL_SUPPRESS_DEPRECATED |
| 15 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 16 | #include <openssl/conf.h> |
Robin van der Gracht | 06b5cdb | 2023-09-12 11:16:23 +0200 | [diff] [blame] | 17 | #include <openssl/engine.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 18 | #include <openssl/evp.h> |
| 19 | #include <openssl/pem.h> |
| 20 | |
| 21 | #include "cert.h" |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 22 | #include "cmd_opt.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 23 | #include "debug.h" |
| 24 | #include "key.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 25 | #include "sha.h" |
| 26 | |
| 27 | #define MAX_FILENAME_LEN 1024 |
| 28 | |
Pankaj Gupta | dd906e6 | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 29 | key_t *keys; |
| 30 | unsigned int num_keys; |
| 31 | |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 32 | #if !USING_OPENSSL3 |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 33 | /* |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 34 | * Create a new key container |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 35 | */ |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 36 | int key_new(key_t *key) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 37 | { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 38 | /* Create key pair container */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 39 | key->key = EVP_PKEY_new(); |
| 40 | if (key->key == NULL) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 44 | return 1; |
| 45 | } |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 46 | #endif |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 47 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 48 | static int key_create_rsa(key_t *key, int key_bits) |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 49 | { |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 50 | #if USING_OPENSSL3 |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 51 | EVP_PKEY *rsa = EVP_RSA_gen(key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 52 | if (rsa == NULL) { |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 53 | printf("Cannot generate RSA key\n"); |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 54 | return 0; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 55 | } |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 56 | key->key = rsa; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 57 | return 1; |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 58 | #else |
| 59 | BIGNUM *e; |
| 60 | RSA *rsa = NULL; |
| 61 | |
| 62 | e = BN_new(); |
| 63 | if (e == NULL) { |
| 64 | printf("Cannot create RSA exponent\n"); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | if (!BN_set_word(e, RSA_F4)) { |
| 69 | printf("Cannot assign RSA exponent\n"); |
| 70 | goto err2; |
| 71 | } |
| 72 | |
| 73 | rsa = RSA_new(); |
| 74 | if (rsa == NULL) { |
| 75 | printf("Cannot create RSA key\n"); |
| 76 | goto err2; |
| 77 | } |
| 78 | |
| 79 | if (!RSA_generate_key_ex(rsa, key_bits, e, NULL)) { |
| 80 | printf("Cannot generate RSA key\n"); |
| 81 | goto err; |
| 82 | } |
| 83 | |
| 84 | if (!EVP_PKEY_assign_RSA(key->key, rsa)) { |
| 85 | printf("Cannot assign RSA key\n"); |
| 86 | goto err; |
| 87 | } |
| 88 | |
| 89 | BN_free(e); |
| 90 | return 1; |
| 91 | |
| 92 | err: |
| 93 | RSA_free(rsa); |
| 94 | err2: |
| 95 | BN_free(e); |
| 96 | return 0; |
| 97 | #endif |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | #ifndef OPENSSL_NO_EC |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 101 | #if USING_OPENSSL3 |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 102 | static int key_create_ecdsa(key_t *key, int key_bits, const char *curve) |
| 103 | { |
| 104 | EVP_PKEY *ec = EVP_EC_gen(curve); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 105 | if (ec == NULL) { |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 106 | printf("Cannot generate EC key\n"); |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 107 | return 0; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 108 | } |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 109 | |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 110 | key->key = ec; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 111 | return 1; |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int key_create_ecdsa_nist(key_t *key, int key_bits) |
| 115 | { |
laurenw-arm | 00185ea | 2023-08-15 14:53:59 -0500 | [diff] [blame] | 116 | if (key_bits == 384) { |
| 117 | return key_create_ecdsa(key, key_bits, "secp384r1"); |
| 118 | } else { |
| 119 | assert(key_bits == 256); |
| 120 | return key_create_ecdsa(key, key_bits, "prime256v1"); |
| 121 | } |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int key_create_ecdsa_brainpool_r(key_t *key, int key_bits) |
| 125 | { |
| 126 | return key_create_ecdsa(key, key_bits, "brainpoolP256r1"); |
| 127 | } |
| 128 | |
| 129 | static int key_create_ecdsa_brainpool_t(key_t *key, int key_bits) |
| 130 | { |
| 131 | return key_create_ecdsa(key, key_bits, "brainpoolP256t1"); |
| 132 | } |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 133 | #else |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 134 | static int key_create_ecdsa(key_t *key, int key_bits, const int curve_id) |
| 135 | { |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 136 | EC_KEY *ec; |
| 137 | |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 138 | ec = EC_KEY_new_by_curve_name(curve_id); |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 139 | if (ec == NULL) { |
| 140 | printf("Cannot create EC key\n"); |
| 141 | return 0; |
| 142 | } |
| 143 | if (!EC_KEY_generate_key(ec)) { |
| 144 | printf("Cannot generate EC key\n"); |
| 145 | goto err; |
| 146 | } |
| 147 | EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS); |
| 148 | EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE); |
| 149 | if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) { |
| 150 | printf("Cannot assign EC key\n"); |
| 151 | goto err; |
| 152 | } |
| 153 | |
| 154 | return 1; |
| 155 | |
| 156 | err: |
| 157 | EC_KEY_free(ec); |
| 158 | return 0; |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static int key_create_ecdsa_nist(key_t *key, int key_bits) |
| 162 | { |
laurenw-arm | 00185ea | 2023-08-15 14:53:59 -0500 | [diff] [blame] | 163 | if (key_bits == 384) { |
| 164 | return key_create_ecdsa(key, key_bits, NID_secp384r1); |
| 165 | } else { |
| 166 | assert(key_bits == 256); |
| 167 | return key_create_ecdsa(key, key_bits, NID_X9_62_prime256v1); |
| 168 | } |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static int key_create_ecdsa_brainpool_r(key_t *key, int key_bits) |
| 172 | { |
| 173 | return key_create_ecdsa(key, key_bits, NID_brainpoolP256r1); |
| 174 | } |
| 175 | |
| 176 | static int key_create_ecdsa_brainpool_t(key_t *key, int key_bits) |
| 177 | { |
| 178 | return key_create_ecdsa(key, key_bits, NID_brainpoolP256t1); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 179 | } |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 180 | #endif /* USING_OPENSSL3 */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 181 | #endif /* OPENSSL_NO_EC */ |
| 182 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 183 | typedef int (*key_create_fn_t)(key_t *key, int key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 184 | static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = { |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 185 | [KEY_ALG_RSA] = key_create_rsa, |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 186 | #ifndef OPENSSL_NO_EC |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 187 | [KEY_ALG_ECDSA_NIST] = key_create_ecdsa_nist, |
| 188 | [KEY_ALG_ECDSA_BRAINPOOL_R] = key_create_ecdsa_brainpool_r, |
| 189 | [KEY_ALG_ECDSA_BRAINPOOL_T] = key_create_ecdsa_brainpool_t, |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 190 | #endif /* OPENSSL_NO_EC */ |
| 191 | }; |
| 192 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 193 | int key_create(key_t *key, int type, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 194 | { |
| 195 | if (type >= KEY_ALG_MAX_NUM) { |
| 196 | printf("Invalid key type\n"); |
| 197 | return 0; |
| 198 | } |
| 199 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 200 | if (key_create_fn[type]) { |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 201 | return key_create_fn[type](key, key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 202 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 203 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Robin van der Gracht | 06b5cdb | 2023-09-12 11:16:23 +0200 | [diff] [blame] | 207 | static EVP_PKEY *key_load_pkcs11(const char *uri) |
| 208 | { |
| 209 | char *key_pass; |
| 210 | EVP_PKEY *pkey; |
| 211 | ENGINE *e; |
| 212 | |
| 213 | ENGINE_load_builtin_engines(); |
| 214 | e = ENGINE_by_id("pkcs11"); |
| 215 | if (!e) { |
| 216 | fprintf(stderr, "Cannot Load PKCS#11 ENGINE\n"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | if (!ENGINE_init(e)) { |
| 221 | fprintf(stderr, "Cannot ENGINE_init\n"); |
| 222 | goto err; |
| 223 | } |
| 224 | |
| 225 | key_pass = getenv("PKCS11_PIN"); |
| 226 | if (key_pass) { |
| 227 | if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) { |
| 228 | fprintf(stderr, "Cannot Set PKCS#11 PIN\n"); |
| 229 | goto err; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | pkey = ENGINE_load_private_key(e, uri, NULL, NULL); |
| 234 | if (pkey) |
| 235 | return pkey; |
| 236 | err: |
| 237 | ENGINE_free(e); |
| 238 | return NULL; |
| 239 | |
| 240 | } |
| 241 | |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 242 | unsigned int key_load(key_t *key) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 243 | { |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 244 | if (key->fn == NULL) { |
| 245 | VERBOSE("Key not specified\n"); |
| 246 | return KEY_ERR_FILENAME; |
| 247 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 248 | |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 249 | if (strncmp(key->fn, "pkcs11:", 7) == 0) { |
| 250 | /* Load key through pkcs11 */ |
| 251 | key->key = key_load_pkcs11(key->fn); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 252 | } else { |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 253 | /* Load key from file */ |
| 254 | FILE *fp = fopen(key->fn, "r"); |
| 255 | if (fp == NULL) { |
| 256 | WARN("Cannot open file %s\n", key->fn); |
| 257 | return KEY_ERR_OPEN; |
| 258 | } |
| 259 | |
| 260 | key->key = PEM_read_PrivateKey(fp, NULL, NULL, NULL); |
| 261 | fclose(fp); |
| 262 | } |
| 263 | |
| 264 | if (key->key == NULL) { |
| 265 | ERROR("Cannot load key from %s\n", key->fn); |
| 266 | return KEY_ERR_LOAD; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 269 | return KEY_ERR_NONE; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int key_store(key_t *key) |
| 273 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 274 | FILE *fp; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 275 | |
| 276 | if (key->fn) { |
Robin van der Gracht | 06b5cdb | 2023-09-12 11:16:23 +0200 | [diff] [blame] | 277 | if (!strncmp(key->fn, "pkcs11:", 7)) { |
| 278 | ERROR("PKCS11 URI provided instead of a file"); |
| 279 | return 0; |
| 280 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 281 | fp = fopen(key->fn, "w"); |
| 282 | if (fp) { |
| 283 | PEM_write_PrivateKey(fp, key->key, |
| 284 | NULL, NULL, 0, NULL, NULL); |
| 285 | fclose(fp); |
| 286 | return 1; |
| 287 | } else { |
| 288 | ERROR("Cannot create file %s\n", key->fn); |
| 289 | } |
| 290 | } else { |
| 291 | ERROR("Key filename not specified\n"); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 296 | |
| 297 | int key_init(void) |
| 298 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 299 | cmd_opt_t cmd_opt; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 300 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 301 | unsigned int i; |
| 302 | |
Pankaj Gupta | dd906e6 | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 303 | keys = malloc((num_def_keys * sizeof(def_keys[0])) |
| 304 | #ifdef PDEF_KEYS |
| 305 | + (num_pdef_keys * sizeof(pdef_keys[0])) |
| 306 | #endif |
| 307 | ); |
| 308 | |
| 309 | if (keys == NULL) { |
| 310 | ERROR("%s:%d Failed to allocate memory.\n", __func__, __LINE__); |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | memcpy(&keys[0], &def_keys[0], (num_def_keys * sizeof(def_keys[0]))); |
| 315 | #ifdef PDEF_KEYS |
| 316 | memcpy(&keys[num_def_keys], &pdef_keys[0], |
| 317 | (num_pdef_keys * sizeof(pdef_keys[0]))); |
| 318 | |
| 319 | num_keys = num_def_keys + num_pdef_keys; |
| 320 | #else |
| 321 | num_keys = num_def_keys; |
| 322 | #endif |
| 323 | ; |
| 324 | |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 325 | for (i = 0; i < num_keys; i++) { |
| 326 | key = &keys[i]; |
| 327 | if (key->opt != NULL) { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 328 | cmd_opt.long_opt.name = key->opt; |
| 329 | cmd_opt.long_opt.has_arg = required_argument; |
| 330 | cmd_opt.long_opt.flag = NULL; |
| 331 | cmd_opt.long_opt.val = CMD_OPT_KEY; |
| 332 | cmd_opt.help_msg = key->help_msg; |
| 333 | cmd_opt_add(&cmd_opt); |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 337 | return 0; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | key_t *key_get_by_opt(const char *opt) |
| 341 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 342 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 343 | unsigned int i; |
| 344 | |
| 345 | /* Sequential search. This is not a performance concern since the number |
| 346 | * of keys is bounded and the code runs on a host machine */ |
| 347 | for (i = 0; i < num_keys; i++) { |
| 348 | key = &keys[i]; |
| 349 | if (0 == strcmp(key->opt, opt)) { |
| 350 | return key; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return NULL; |
| 355 | } |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 356 | |
| 357 | void key_cleanup(void) |
| 358 | { |
| 359 | unsigned int i; |
| 360 | |
| 361 | for (i = 0; i < num_keys; i++) { |
| 362 | EVP_PKEY_free(keys[i].key); |
| 363 | if (keys[i].fn != NULL) { |
| 364 | void *ptr = keys[i].fn; |
| 365 | |
| 366 | free(ptr); |
| 367 | keys[i].fn = NULL; |
| 368 | } |
| 369 | } |
| 370 | free(keys); |
| 371 | } |
| 372 | |