blob: fcc9d53162de8bb8eea12a066c2ffcb9032acc09 [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Sandrine Bailleux0b144832020-01-07 15:12:08 +01002 * Copyright (c) 2015-2020, 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
24/*
Juan Castillof9f39c32015-06-01 16:34:23 +010025 * Create a new key container
Juan Castillo11abdcd2014-10-21 11:30:42 +010026 */
Masahiro Yamadabccb1092017-02-06 21:15:01 +090027int key_new(key_t *key)
Juan Castillo11abdcd2014-10-21 11:30:42 +010028{
Juan Castillo11abdcd2014-10-21 11:30:42 +010029 /* Create key pair container */
Juan Castillof9f39c32015-06-01 16:34:23 +010030 key->key = EVP_PKEY_new();
31 if (key->key == NULL) {
Juan Castillo11abdcd2014-10-21 11:30:42 +010032 return 0;
33 }
34
Juan Castillof9f39c32015-06-01 16:34:23 +010035 return 1;
36}
37
Justin Chadwellfebe86c2019-07-29 17:13:45 +010038static int key_create_rsa(key_t *key, int key_bits)
Juan Castillof9f39c32015-06-01 16:34:23 +010039{
Michalis Pappas38628942017-10-06 16:11:44 +080040 BIGNUM *e;
41 RSA *rsa = NULL;
Juan Castillof9f39c32015-06-01 16:34:23 +010042
Michalis Pappas38628942017-10-06 16:11:44 +080043 e = BN_new();
44 if (e == NULL) {
45 printf("Cannot create RSA exponent\n");
46 goto err;
47 }
48
49 if (!BN_set_word(e, RSA_F4)) {
50 printf("Cannot assign RSA exponent\n");
51 goto err;
52 }
53
54 rsa = RSA_new();
Juan Castilloa2224ab2015-06-30 13:36:57 +010055 if (rsa == NULL) {
56 printf("Cannot create RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010057 goto err;
58 }
Michalis Pappas38628942017-10-06 16:11:44 +080059
Justin Chadwellfebe86c2019-07-29 17:13:45 +010060 if (!RSA_generate_key_ex(rsa, key_bits, e, NULL)) {
Michalis Pappas38628942017-10-06 16:11:44 +080061 printf("Cannot generate RSA key\n");
62 goto err;
63 }
64
Juan Castilloa2224ab2015-06-30 13:36:57 +010065 if (!EVP_PKEY_assign_RSA(key->key, rsa)) {
66 printf("Cannot assign RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010067 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010068 }
69
Justin Chadwellfa3ec4c2019-08-12 12:19:21 +010070 BN_free(e);
Juan Castillof9f39c32015-06-01 16:34:23 +010071 return 1;
Juan Castillof9f39c32015-06-01 16:34:23 +010072err:
73 RSA_free(rsa);
Michalis Pappas38628942017-10-06 16:11:44 +080074 BN_free(e);
Juan Castilloa2224ab2015-06-30 13:36:57 +010075 return 0;
76}
77
78#ifndef OPENSSL_NO_EC
Justin Chadwellfebe86c2019-07-29 17:13:45 +010079static int key_create_ecdsa(key_t *key, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +010080{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +090081 EC_KEY *ec;
Juan Castilloa2224ab2015-06-30 13:36:57 +010082
83 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
84 if (ec == NULL) {
85 printf("Cannot create EC key\n");
86 goto err;
87 }
88 if (!EC_KEY_generate_key(ec)) {
89 printf("Cannot generate EC key\n");
90 goto err;
91 }
92 EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS);
93 EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
94 if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) {
95 printf("Cannot assign EC key\n");
96 goto err;
97 }
98
99 return 1;
100err:
Juan Castillof9f39c32015-06-01 16:34:23 +0100101 EC_KEY_free(ec);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100102 return 0;
103}
104#endif /* OPENSSL_NO_EC */
105
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100106typedef int (*key_create_fn_t)(key_t *key, int key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100107static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800108 key_create_rsa, /* KEY_ALG_RSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100109#ifndef OPENSSL_NO_EC
Qixiang Xu31f3bb92017-09-22 16:21:41 +0800110 key_create_ecdsa, /* KEY_ALG_ECDSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +0100111#endif /* OPENSSL_NO_EC */
112};
113
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100114int key_create(key_t *key, int type, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +0100115{
116 if (type >= KEY_ALG_MAX_NUM) {
117 printf("Invalid key type\n");
118 return 0;
119 }
120
Juan Castilloa2224ab2015-06-30 13:36:57 +0100121 if (key_create_fn[type]) {
Justin Chadwellfebe86c2019-07-29 17:13:45 +0100122 return key_create_fn[type](key, key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100123 }
Juan Castillof9f39c32015-06-01 16:34:23 +0100124
Juan Castillo11abdcd2014-10-21 11:30:42 +0100125 return 0;
126}
127
Juan Castillof9f39c32015-06-01 16:34:23 +0100128int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +0100129{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900130 FILE *fp;
131 EVP_PKEY *k;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100132
Juan Castillo11abdcd2014-10-21 11:30:42 +0100133 if (key->fn) {
134 /* Load key from file */
135 fp = fopen(key->fn, "r");
136 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100137 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +0100138 fclose(fp);
139 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100140 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100141 return 1;
142 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100143 ERROR("Cannot load key from %s\n", key->fn);
144 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100145 }
146 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100147 WARN("Cannot open file %s\n", key->fn);
148 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100149 }
150 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100151 WARN("Key filename not specified\n");
152 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100153 }
154
Juan Castillo11abdcd2014-10-21 11:30:42 +0100155 return 0;
156}
157
158int key_store(key_t *key)
159{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900160 FILE *fp;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100161
162 if (key->fn) {
163 fp = fopen(key->fn, "w");
164 if (fp) {
165 PEM_write_PrivateKey(fp, key->key,
166 NULL, NULL, 0, NULL, NULL);
167 fclose(fp);
168 return 1;
169 } else {
170 ERROR("Cannot create file %s\n", key->fn);
171 }
172 } else {
173 ERROR("Key filename not specified\n");
174 }
175
176 return 0;
177}
Juan Castillo1218dd52015-07-03 16:23:16 +0100178
179int key_init(void)
180{
Juan Castillo212f7382015-12-15 16:37:57 +0000181 cmd_opt_t cmd_opt;
Juan Castillo1218dd52015-07-03 16:23:16 +0100182 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100183 unsigned int i;
184
185 for (i = 0; i < num_keys; i++) {
186 key = &keys[i];
187 if (key->opt != NULL) {
Juan Castillo212f7382015-12-15 16:37:57 +0000188 cmd_opt.long_opt.name = key->opt;
189 cmd_opt.long_opt.has_arg = required_argument;
190 cmd_opt.long_opt.flag = NULL;
191 cmd_opt.long_opt.val = CMD_OPT_KEY;
192 cmd_opt.help_msg = key->help_msg;
193 cmd_opt_add(&cmd_opt);
Juan Castillo1218dd52015-07-03 16:23:16 +0100194 }
195 }
196
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900197 return 0;
Juan Castillo1218dd52015-07-03 16:23:16 +0100198}
199
200key_t *key_get_by_opt(const char *opt)
201{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900202 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100203 unsigned int i;
204
205 /* Sequential search. This is not a performance concern since the number
206 * of keys is bounded and the code runs on a host machine */
207 for (i = 0; i < num_keys; i++) {
208 key = &keys[i];
209 if (0 == strcmp(key->opt, opt)) {
210 return key;
211 }
212 }
213
214 return NULL;
215}