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 |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 96 | static int key_create_ecdsa(key_t *key, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 97 | { |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 98 | #if USING_OPENSSL3 |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 99 | EVP_PKEY *ec = EVP_EC_gen("prime256v1"); |
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 | } |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 104 | key->key = ec; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 105 | return 1; |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 106 | #else |
| 107 | EC_KEY *ec; |
| 108 | |
| 109 | ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 110 | if (ec == NULL) { |
| 111 | printf("Cannot create EC key\n"); |
| 112 | return 0; |
| 113 | } |
| 114 | if (!EC_KEY_generate_key(ec)) { |
| 115 | printf("Cannot generate EC key\n"); |
| 116 | goto err; |
| 117 | } |
| 118 | EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS); |
| 119 | EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE); |
| 120 | if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) { |
| 121 | printf("Cannot assign EC key\n"); |
| 122 | goto err; |
| 123 | } |
| 124 | |
| 125 | return 1; |
| 126 | |
| 127 | err: |
| 128 | EC_KEY_free(ec); |
| 129 | return 0; |
| 130 | #endif |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 131 | } |
| 132 | #endif /* OPENSSL_NO_EC */ |
| 133 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 134 | typedef int (*key_create_fn_t)(key_t *key, int key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 135 | static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = { |
Qixiang Xu | 31f3bb9 | 2017-09-22 16:21:41 +0800 | [diff] [blame] | 136 | key_create_rsa, /* KEY_ALG_RSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 137 | #ifndef OPENSSL_NO_EC |
Qixiang Xu | 31f3bb9 | 2017-09-22 16:21:41 +0800 | [diff] [blame] | 138 | key_create_ecdsa, /* KEY_ALG_ECDSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 139 | #endif /* OPENSSL_NO_EC */ |
| 140 | }; |
| 141 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 142 | int key_create(key_t *key, int type, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 143 | { |
| 144 | if (type >= KEY_ALG_MAX_NUM) { |
| 145 | printf("Invalid key type\n"); |
| 146 | return 0; |
| 147 | } |
| 148 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 149 | if (key_create_fn[type]) { |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 150 | return key_create_fn[type](key, key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 151 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 152 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 156 | int key_load(key_t *key, unsigned int *err_code) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 157 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 158 | FILE *fp; |
| 159 | EVP_PKEY *k; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 160 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 161 | if (key->fn) { |
| 162 | /* Load key from file */ |
| 163 | fp = fopen(key->fn, "r"); |
| 164 | if (fp) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 165 | k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 166 | fclose(fp); |
| 167 | if (k) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 168 | *err_code = KEY_ERR_NONE; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 169 | return 1; |
| 170 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 171 | ERROR("Cannot load key from %s\n", key->fn); |
| 172 | *err_code = KEY_ERR_LOAD; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 173 | } |
| 174 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 175 | WARN("Cannot open file %s\n", key->fn); |
| 176 | *err_code = KEY_ERR_OPEN; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 177 | } |
| 178 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 179 | WARN("Key filename not specified\n"); |
| 180 | *err_code = KEY_ERR_FILENAME; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int key_store(key_t *key) |
| 187 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 188 | FILE *fp; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 189 | |
| 190 | if (key->fn) { |
| 191 | fp = fopen(key->fn, "w"); |
| 192 | if (fp) { |
| 193 | PEM_write_PrivateKey(fp, key->key, |
| 194 | NULL, NULL, 0, NULL, NULL); |
| 195 | fclose(fp); |
| 196 | return 1; |
| 197 | } else { |
| 198 | ERROR("Cannot create file %s\n", key->fn); |
| 199 | } |
| 200 | } else { |
| 201 | ERROR("Key filename not specified\n"); |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 206 | |
| 207 | int key_init(void) |
| 208 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 209 | cmd_opt_t cmd_opt; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 210 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 211 | unsigned int i; |
| 212 | |
Pankaj Gupta | dd906e6 | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 213 | keys = malloc((num_def_keys * sizeof(def_keys[0])) |
| 214 | #ifdef PDEF_KEYS |
| 215 | + (num_pdef_keys * sizeof(pdef_keys[0])) |
| 216 | #endif |
| 217 | ); |
| 218 | |
| 219 | if (keys == NULL) { |
| 220 | ERROR("%s:%d Failed to allocate memory.\n", __func__, __LINE__); |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | memcpy(&keys[0], &def_keys[0], (num_def_keys * sizeof(def_keys[0]))); |
| 225 | #ifdef PDEF_KEYS |
| 226 | memcpy(&keys[num_def_keys], &pdef_keys[0], |
| 227 | (num_pdef_keys * sizeof(pdef_keys[0]))); |
| 228 | |
| 229 | num_keys = num_def_keys + num_pdef_keys; |
| 230 | #else |
| 231 | num_keys = num_def_keys; |
| 232 | #endif |
| 233 | ; |
| 234 | |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 235 | for (i = 0; i < num_keys; i++) { |
| 236 | key = &keys[i]; |
| 237 | if (key->opt != NULL) { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 238 | cmd_opt.long_opt.name = key->opt; |
| 239 | cmd_opt.long_opt.has_arg = required_argument; |
| 240 | cmd_opt.long_opt.flag = NULL; |
| 241 | cmd_opt.long_opt.val = CMD_OPT_KEY; |
| 242 | cmd_opt.help_msg = key->help_msg; |
| 243 | cmd_opt_add(&cmd_opt); |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 247 | return 0; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | key_t *key_get_by_opt(const char *opt) |
| 251 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 252 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 253 | unsigned int i; |
| 254 | |
| 255 | /* Sequential search. This is not a performance concern since the number |
| 256 | * of keys is bounded and the code runs on a host machine */ |
| 257 | for (i = 0; i < num_keys; i++) { |
| 258 | key = &keys[i]; |
| 259 | if (0 == strcmp(key->opt, opt)) { |
| 260 | return key; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return NULL; |
| 265 | } |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 266 | |
| 267 | void key_cleanup(void) |
| 268 | { |
| 269 | unsigned int i; |
| 270 | |
| 271 | for (i = 0; i < num_keys; i++) { |
| 272 | EVP_PKEY_free(keys[i].key); |
| 273 | if (keys[i].fn != NULL) { |
| 274 | void *ptr = keys[i].fn; |
| 275 | |
| 276 | free(ptr); |
| 277 | keys[i].fn = NULL; |
| 278 | } |
| 279 | } |
| 280 | free(keys); |
| 281 | } |
| 282 | |