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