blob: 64359756ff7d0709bc5d162e6b66948ba2336dfd [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Pankaj Guptadd906e62020-12-09 14:02:38 +05302 * Copyright (c) 2015-2021, 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 Castillo11abdcd2014-10-21 11:30:42 +010027/*
Juan Castillof9f39c32015-06-01 16:34:23 +010028 * Create a new key container
Juan Castillo11abdcd2014-10-21 11:30:42 +010029 */
Masahiro Yamadabccb1092017-02-06 21:15:01 +090030int key_new(key_t *key)
Juan Castillo11abdcd2014-10-21 11:30:42 +010031{
Juan Castillo11abdcd2014-10-21 11:30:42 +010032 /* Create key pair container */
Juan Castillof9f39c32015-06-01 16:34:23 +010033 key->key = EVP_PKEY_new();
34 if (key->key == NULL) {
Juan Castillo11abdcd2014-10-21 11:30:42 +010035 return 0;
36 }
37
Juan Castillof9f39c32015-06-01 16:34:23 +010038 return 1;
39}
40
Justin Chadwellfebe86c2019-07-29 17:13:45 +010041static int key_create_rsa(key_t *key, int key_bits)
Juan Castillof9f39c32015-06-01 16:34:23 +010042{
Michalis Pappas38628942017-10-06 16:11:44 +080043 BIGNUM *e;
44 RSA *rsa = NULL;
Juan Castillof9f39c32015-06-01 16:34:23 +010045
Michalis Pappas38628942017-10-06 16:11:44 +080046 e = BN_new();
47 if (e == NULL) {
48 printf("Cannot create RSA exponent\n");
49 goto err;
50 }
51
52 if (!BN_set_word(e, RSA_F4)) {
53 printf("Cannot assign RSA exponent\n");
54 goto err;
55 }
56
57 rsa = RSA_new();
Juan Castilloa2224ab2015-06-30 13:36:57 +010058 if (rsa == NULL) {
59 printf("Cannot create RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010060 goto err;
61 }
Michalis Pappas38628942017-10-06 16:11:44 +080062
Justin Chadwellfebe86c2019-07-29 17:13:45 +010063 if (!RSA_generate_key_ex(rsa, key_bits, e, NULL)) {
Michalis Pappas38628942017-10-06 16:11:44 +080064 printf("Cannot generate RSA key\n");
65 goto err;
66 }
67
Juan Castilloa2224ab2015-06-30 13:36:57 +010068 if (!EVP_PKEY_assign_RSA(key->key, rsa)) {
69 printf("Cannot assign RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010070 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010071 }
72
Justin Chadwellfa3ec4c2019-08-12 12:19:21 +010073 BN_free(e);
Juan Castillof9f39c32015-06-01 16:34:23 +010074 return 1;
Juan Castillof9f39c32015-06-01 16:34:23 +010075err:
76 RSA_free(rsa);
Michalis Pappas38628942017-10-06 16:11:44 +080077 BN_free(e);
Juan Castilloa2224ab2015-06-30 13:36:57 +010078 return 0;
79}
80
81#ifndef OPENSSL_NO_EC
Justin Chadwellfebe86c2019-07-29 17:13:45 +010082static int key_create_ecdsa(key_t *key, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +010083{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +090084 EC_KEY *ec;
Juan Castilloa2224ab2015-06-30 13:36:57 +010085
86 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
87 if (ec == NULL) {
88 printf("Cannot create EC key\n");
89 goto err;
90 }
91 if (!EC_KEY_generate_key(ec)) {
92 printf("Cannot generate EC key\n");
93 goto err;
94 }
95 EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS);
96 EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
97 if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) {
98 printf("Cannot assign EC key\n");
99 goto err;
100 }
101
102 return 1;
103err:
Juan Castillof9f39c32015-06-01 16:34:23 +0100104 EC_KEY_free(ec);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100105 return 0;
106}
107#endif /* OPENSSL_NO_EC */
108
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100109typedef int (*key_create_fn_t)(key_t *key, int key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100110static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800111 key_create_rsa, /* KEY_ALG_RSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100112#ifndef OPENSSL_NO_EC
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800113 key_create_ecdsa, /* KEY_ALG_ECDSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100114#endif /* OPENSSL_NO_EC */
115};
116
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100117int key_create(key_t *key, int type, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +0100118{
119 if (type >= KEY_ALG_MAX_NUM) {
120 printf("Invalid key type\n");
121 return 0;
122 }
123
Juan Castilloa2224ab2015-06-30 13:36:57 +0100124 if (key_create_fn[type]) {
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100125 return key_create_fn[type](key, key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100126 }
Juan Castillof9f39c32015-06-01 16:34:23 +0100127
Juan Castillo11abdcd2014-10-21 11:30:42 +0100128 return 0;
129}
130
Juan Castillof9f39c32015-06-01 16:34:23 +0100131int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +0100132{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900133 FILE *fp;
134 EVP_PKEY *k;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100135
Juan Castillo11abdcd2014-10-21 11:30:42 +0100136 if (key->fn) {
137 /* Load key from file */
138 fp = fopen(key->fn, "r");
139 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100140 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +0100141 fclose(fp);
142 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100143 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100144 return 1;
145 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100146 ERROR("Cannot load key from %s\n", key->fn);
147 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100148 }
149 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100150 WARN("Cannot open file %s\n", key->fn);
151 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100152 }
153 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100154 WARN("Key filename not specified\n");
155 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100156 }
157
Juan Castillo11abdcd2014-10-21 11:30:42 +0100158 return 0;
159}
160
161int key_store(key_t *key)
162{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900163 FILE *fp;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100164
165 if (key->fn) {
166 fp = fopen(key->fn, "w");
167 if (fp) {
168 PEM_write_PrivateKey(fp, key->key,
169 NULL, NULL, 0, NULL, NULL);
170 fclose(fp);
171 return 1;
172 } else {
173 ERROR("Cannot create file %s\n", key->fn);
174 }
175 } else {
176 ERROR("Key filename not specified\n");
177 }
178
179 return 0;
180}
Juan Castillo1218dd52015-07-03 16:23:16 +0100181
182int key_init(void)
183{
Juan Castillo212f7382015-12-15 16:37:57 +0000184 cmd_opt_t cmd_opt;
Juan Castillo1218dd52015-07-03 16:23:16 +0100185 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100186 unsigned int i;
187
Pankaj Guptadd906e62020-12-09 14:02:38 +0530188 keys = malloc((num_def_keys * sizeof(def_keys[0]))
189#ifdef PDEF_KEYS
190 + (num_pdef_keys * sizeof(pdef_keys[0]))
191#endif
192 );
193
194 if (keys == NULL) {
195 ERROR("%s:%d Failed to allocate memory.\n", __func__, __LINE__);
196 return 1;
197 }
198
199 memcpy(&keys[0], &def_keys[0], (num_def_keys * sizeof(def_keys[0])));
200#ifdef PDEF_KEYS
201 memcpy(&keys[num_def_keys], &pdef_keys[0],
202 (num_pdef_keys * sizeof(pdef_keys[0])));
203
204 num_keys = num_def_keys + num_pdef_keys;
205#else
206 num_keys = num_def_keys;
207#endif
208 ;
209
Juan Castillo1218dd52015-07-03 16:23:16 +0100210 for (i = 0; i < num_keys; i++) {
211 key = &keys[i];
212 if (key->opt != NULL) {
Juan Castillo212f7382015-12-15 16:37:57 +0000213 cmd_opt.long_opt.name = key->opt;
214 cmd_opt.long_opt.has_arg = required_argument;
215 cmd_opt.long_opt.flag = NULL;
216 cmd_opt.long_opt.val = CMD_OPT_KEY;
217 cmd_opt.help_msg = key->help_msg;
218 cmd_opt_add(&cmd_opt);
Juan Castillo1218dd52015-07-03 16:23:16 +0100219 }
220 }
221
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900222 return 0;
Juan Castillo1218dd52015-07-03 16:23:16 +0100223}
224
225key_t *key_get_by_opt(const char *opt)
226{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900227 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100228 unsigned int i;
229
230 /* Sequential search. This is not a performance concern since the number
231 * of keys is bounded and the code runs on a host machine */
232 for (i = 0; i < num_keys; i++) {
233 key = &keys[i];
234 if (0 == strcmp(key->opt, opt)) {
235 return key;
236 }
237 }
238
239 return NULL;
240}