blob: 2857a3b07652086671aff98aa373bd94ba629188 [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -05002 * 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 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{
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050043 EVP_PKEY *rsa = EVP_RSA_gen(key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +010044 if (rsa == NULL) {
Michalis Pappas38628942017-10-06 16:11:44 +080045 printf("Cannot generate RSA key\n");
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050046 return 0;
Juan Castillo11abdcd2014-10-21 11:30:42 +010047 }
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050048 key->key = rsa;
Juan Castillof9f39c32015-06-01 16:34:23 +010049 return 1;
Juan Castilloa2224ab2015-06-30 13:36:57 +010050}
51
52#ifndef OPENSSL_NO_EC
Justin Chadwellfebe86c2019-07-29 17:13:45 +010053static int key_create_ecdsa(key_t *key, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +010054{
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050055 EVP_PKEY *ec = EVP_EC_gen("prime256v1");
Juan Castilloa2224ab2015-06-30 13:36:57 +010056 if (ec == NULL) {
Juan Castilloa2224ab2015-06-30 13:36:57 +010057 printf("Cannot generate EC key\n");
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050058 return 0;
Juan Castilloa2224ab2015-06-30 13:36:57 +010059 }
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050060 key->key = ec;
Juan Castilloa2224ab2015-06-30 13:36:57 +010061 return 1;
Juan Castilloa2224ab2015-06-30 13:36:57 +010062}
63#endif /* OPENSSL_NO_EC */
64
Justin Chadwellfebe86c2019-07-29 17:13:45 +010065typedef int (*key_create_fn_t)(key_t *key, int key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +010066static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
Qixiang Xu31f3bb92017-09-22 16:21:41 +080067 key_create_rsa, /* KEY_ALG_RSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +010068#ifndef OPENSSL_NO_EC
Qixiang Xu31f3bb92017-09-22 16:21:41 +080069 key_create_ecdsa, /* KEY_ALG_ECDSA */
Juan Castilloa2224ab2015-06-30 13:36:57 +010070#endif /* OPENSSL_NO_EC */
71};
72
Justin Chadwellfebe86c2019-07-29 17:13:45 +010073int key_create(key_t *key, int type, int key_bits)
Juan Castilloa2224ab2015-06-30 13:36:57 +010074{
75 if (type >= KEY_ALG_MAX_NUM) {
76 printf("Invalid key type\n");
77 return 0;
78 }
79
Juan Castilloa2224ab2015-06-30 13:36:57 +010080 if (key_create_fn[type]) {
Justin Chadwellfebe86c2019-07-29 17:13:45 +010081 return key_create_fn[type](key, key_bits);
Juan Castilloa2224ab2015-06-30 13:36:57 +010082 }
Juan Castillof9f39c32015-06-01 16:34:23 +010083
Juan Castillo11abdcd2014-10-21 11:30:42 +010084 return 0;
85}
86
Juan Castillof9f39c32015-06-01 16:34:23 +010087int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +010088{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +090089 FILE *fp;
90 EVP_PKEY *k;
Juan Castillo11abdcd2014-10-21 11:30:42 +010091
Juan Castillo11abdcd2014-10-21 11:30:42 +010092 if (key->fn) {
93 /* Load key from file */
94 fp = fopen(key->fn, "r");
95 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +010096 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +010097 fclose(fp);
98 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +010099 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100100 return 1;
101 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100102 ERROR("Cannot load key from %s\n", key->fn);
103 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100104 }
105 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100106 WARN("Cannot open file %s\n", key->fn);
107 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100108 }
109 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100110 WARN("Key filename not specified\n");
111 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100112 }
113
Juan Castillo11abdcd2014-10-21 11:30:42 +0100114 return 0;
115}
116
117int key_store(key_t *key)
118{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900119 FILE *fp;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100120
121 if (key->fn) {
122 fp = fopen(key->fn, "w");
123 if (fp) {
124 PEM_write_PrivateKey(fp, key->key,
125 NULL, NULL, 0, NULL, NULL);
126 fclose(fp);
127 return 1;
128 } else {
129 ERROR("Cannot create file %s\n", key->fn);
130 }
131 } else {
132 ERROR("Key filename not specified\n");
133 }
134
135 return 0;
136}
Juan Castillo1218dd52015-07-03 16:23:16 +0100137
138int key_init(void)
139{
Juan Castillo212f7382015-12-15 16:37:57 +0000140 cmd_opt_t cmd_opt;
Juan Castillo1218dd52015-07-03 16:23:16 +0100141 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100142 unsigned int i;
143
Pankaj Guptadd906e62020-12-09 14:02:38 +0530144 keys = malloc((num_def_keys * sizeof(def_keys[0]))
145#ifdef PDEF_KEYS
146 + (num_pdef_keys * sizeof(pdef_keys[0]))
147#endif
148 );
149
150 if (keys == NULL) {
151 ERROR("%s:%d Failed to allocate memory.\n", __func__, __LINE__);
152 return 1;
153 }
154
155 memcpy(&keys[0], &def_keys[0], (num_def_keys * sizeof(def_keys[0])));
156#ifdef PDEF_KEYS
157 memcpy(&keys[num_def_keys], &pdef_keys[0],
158 (num_pdef_keys * sizeof(pdef_keys[0])));
159
160 num_keys = num_def_keys + num_pdef_keys;
161#else
162 num_keys = num_def_keys;
163#endif
164 ;
165
Juan Castillo1218dd52015-07-03 16:23:16 +0100166 for (i = 0; i < num_keys; i++) {
167 key = &keys[i];
168 if (key->opt != NULL) {
Juan Castillo212f7382015-12-15 16:37:57 +0000169 cmd_opt.long_opt.name = key->opt;
170 cmd_opt.long_opt.has_arg = required_argument;
171 cmd_opt.long_opt.flag = NULL;
172 cmd_opt.long_opt.val = CMD_OPT_KEY;
173 cmd_opt.help_msg = key->help_msg;
174 cmd_opt_add(&cmd_opt);
Juan Castillo1218dd52015-07-03 16:23:16 +0100175 }
176 }
177
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900178 return 0;
Juan Castillo1218dd52015-07-03 16:23:16 +0100179}
180
181key_t *key_get_by_opt(const char *opt)
182{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900183 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100184 unsigned int i;
185
186 /* Sequential search. This is not a performance concern since the number
187 * of keys is bounded and the code runs on a host machine */
188 for (i = 0; i < num_keys; i++) {
189 key = &keys[i];
190 if (0 == strcmp(key->opt, opt)) {
191 return key;
192 }
193 }
194
195 return NULL;
196}