blob: 0061b8a1c74410154ea44acfd9683e6250702152 [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Juan Pablo Conde3539c742022-10-25 19:41:02 -04002 * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
Juan Castillo11abdcd2014-10-21 11:30:42 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo11abdcd2014-10-21 11:30:42 +01005 */
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 Castillo1218dd52015-07-03 16:23:16 +010017#include "cmd_opt.h"
Juan Castillo11abdcd2014-10-21 11:30:42 +010018#include "debug.h"
19#include "key.h"
Juan Castillo11abdcd2014-10-21 11:30:42 +010020#include "sha.h"
21
22#define MAX_FILENAME_LEN 1024
23
Pankaj Guptadd906e62020-12-09 14:02:38 +053024key_t *keys;
25unsigned int num_keys;
26
Juan Pablo Conde3539c742022-10-25 19:41:02 -040027#if !USING_OPENSSL3
Juan Castillo11abdcd2014-10-21 11:30:42 +010028/*
Juan Castillof9f39c32015-06-01 16:34:23 +010029 * Create a new key container
Juan Castillo11abdcd2014-10-21 11:30:42 +010030 */
Masahiro Yamadabccb1092017-02-06 21:15:01 +090031int key_new(key_t *key)
Juan Castillo11abdcd2014-10-21 11:30:42 +010032{
Juan Castillo11abdcd2014-10-21 11:30:42 +010033 /* Create key pair container */
Juan Castillof9f39c32015-06-01 16:34:23 +010034 key->key = EVP_PKEY_new();
35 if (key->key == NULL) {
Juan Castillo11abdcd2014-10-21 11:30:42 +010036 return 0;
37 }
38
Juan Castillof9f39c32015-06-01 16:34:23 +010039 return 1;
40}
Juan Pablo Conde3539c742022-10-25 19:41:02 -040041#endif
Juan Castillof9f39c32015-06-01 16:34:23 +010042
Justin Chadwellfebe86c2019-07-29 17:13:45 +010043static int key_create_rsa(key_t *key, int key_bits)
Juan Castillof9f39c32015-06-01 16:34:23 +010044{
Juan Pablo Conde3539c742022-10-25 19:41:02 -040045#if USING_OPENSSL3
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050046 EVP_PKEY *rsa = EVP_RSA_gen(key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +010047 if (rsa == NULL) {
Michalis Pappas38628942017-10-06 16:11:44 +080048 printf("Cannot generate RSA key\n");
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050049 return 0;
Juan Castillo11abdcd2014-10-21 11:30:42 +010050 }
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050051 key->key = rsa;
Juan Castillof9f39c32015-06-01 16:34:23 +010052 return 1;
Juan Pablo Conde3539c742022-10-25 19:41:02 -040053#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
87err:
88 RSA_free(rsa);
89err2:
90 BN_free(e);
91 return 0;
92#endif
Juan Castilloa2224ab2015-06-30 13:36:57 +010093}
94
95#ifndef OPENSSL_NO_EC
Justin Chadwellfebe86c2019-07-29 17:13:45 +010096static int key_create_ecdsa(key_t *key, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +010097{
Juan Pablo Conde3539c742022-10-25 19:41:02 -040098#if USING_OPENSSL3
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050099 EVP_PKEY *ec = EVP_EC_gen("prime256v1");
Juan Castilloa2224ab2015-06-30 13:36:57 +0100100 if (ec == NULL) {
Juan Castilloa2224ab2015-06-30 13:36:57 +0100101 printf("Cannot generate EC key\n");
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -0500102 return 0;
Juan Castilloa2224ab2015-06-30 13:36:57 +0100103 }
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -0500104 key->key = ec;
Juan Castilloa2224ab2015-06-30 13:36:57 +0100105 return 1;
Juan Pablo Conde3539c742022-10-25 19:41:02 -0400106#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
127err:
128 EC_KEY_free(ec);
129 return 0;
130#endif
Juan Castilloa2224ab2015-06-30 13:36:57 +0100131}
132#endif /* OPENSSL_NO_EC */
133
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100134typedef int (*key_create_fn_t)(key_t *key, int key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100135static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800136 key_create_rsa, /* KEY_ALG_RSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100137#ifndef OPENSSL_NO_EC
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800138 key_create_ecdsa, /* KEY_ALG_ECDSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100139#endif /* OPENSSL_NO_EC */
140};
141
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100142int key_create(key_t *key, int type, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +0100143{
144 if (type >= KEY_ALG_MAX_NUM) {
145 printf("Invalid key type\n");
146 return 0;
147 }
148
Juan Castilloa2224ab2015-06-30 13:36:57 +0100149 if (key_create_fn[type]) {
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100150 return key_create_fn[type](key, key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100151 }
Juan Castillof9f39c32015-06-01 16:34:23 +0100152
Juan Castillo11abdcd2014-10-21 11:30:42 +0100153 return 0;
154}
155
Juan Castillof9f39c32015-06-01 16:34:23 +0100156int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +0100157{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900158 FILE *fp;
159 EVP_PKEY *k;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100160
Juan Castillo11abdcd2014-10-21 11:30:42 +0100161 if (key->fn) {
162 /* Load key from file */
163 fp = fopen(key->fn, "r");
164 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100165 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +0100166 fclose(fp);
167 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100168 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100169 return 1;
170 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100171 ERROR("Cannot load key from %s\n", key->fn);
172 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100173 }
174 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100175 WARN("Cannot open file %s\n", key->fn);
176 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100177 }
178 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100179 WARN("Key filename not specified\n");
180 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100181 }
182
Juan Castillo11abdcd2014-10-21 11:30:42 +0100183 return 0;
184}
185
186int key_store(key_t *key)
187{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900188 FILE *fp;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100189
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 Castillo1218dd52015-07-03 16:23:16 +0100206
207int key_init(void)
208{
Juan Castillo212f7382015-12-15 16:37:57 +0000209 cmd_opt_t cmd_opt;
Juan Castillo1218dd52015-07-03 16:23:16 +0100210 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100211 unsigned int i;
212
Pankaj Guptadd906e62020-12-09 14:02:38 +0530213 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 Castillo1218dd52015-07-03 16:23:16 +0100235 for (i = 0; i < num_keys; i++) {
236 key = &keys[i];
237 if (key->opt != NULL) {
Juan Castillo212f7382015-12-15 16:37:57 +0000238 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 Castillo1218dd52015-07-03 16:23:16 +0100244 }
245 }
246
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900247 return 0;
Juan Castillo1218dd52015-07-03 16:23:16 +0100248}
249
250key_t *key_get_by_opt(const char *opt)
251{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900252 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100253 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 Conde3539c742022-10-25 19:41:02 -0400266
267void 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