Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [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 Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 27 | /* |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 28 | * Create a new key container |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 29 | */ |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 30 | int key_new(key_t *key) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 31 | { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 32 | /* Create key pair container */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 33 | key->key = EVP_PKEY_new(); |
| 34 | if (key->key == NULL) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 38 | return 1; |
| 39 | } |
| 40 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 41 | static int key_create_rsa(key_t *key, int key_bits) |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 42 | { |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 43 | EVP_PKEY *rsa = EVP_RSA_gen(key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 44 | if (rsa == NULL) { |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 45 | printf("Cannot generate RSA key\n"); |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 46 | return 0; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 47 | } |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 48 | key->key = rsa; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 49 | return 1; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | #ifndef OPENSSL_NO_EC |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 53 | static int key_create_ecdsa(key_t *key, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 54 | { |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 55 | EVP_PKEY *ec = EVP_EC_gen("prime256v1"); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 56 | if (ec == NULL) { |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 57 | printf("Cannot generate EC key\n"); |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 58 | return 0; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 59 | } |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 60 | key->key = ec; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 61 | return 1; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 62 | } |
| 63 | #endif /* OPENSSL_NO_EC */ |
| 64 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 65 | typedef int (*key_create_fn_t)(key_t *key, int key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 66 | 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] | 67 | key_create_rsa, /* KEY_ALG_RSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 68 | #ifndef OPENSSL_NO_EC |
Qixiang Xu | 31f3bb9 | 2017-09-22 16:21:41 +0800 | [diff] [blame] | 69 | key_create_ecdsa, /* KEY_ALG_ECDSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 70 | #endif /* OPENSSL_NO_EC */ |
| 71 | }; |
| 72 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 73 | int key_create(key_t *key, int type, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 74 | { |
| 75 | if (type >= KEY_ALG_MAX_NUM) { |
| 76 | printf("Invalid key type\n"); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 80 | if (key_create_fn[type]) { |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 81 | return key_create_fn[type](key, key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 82 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 83 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 87 | int key_load(key_t *key, unsigned int *err_code) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 88 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 89 | FILE *fp; |
| 90 | EVP_PKEY *k; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 91 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 92 | if (key->fn) { |
| 93 | /* Load key from file */ |
| 94 | fp = fopen(key->fn, "r"); |
| 95 | if (fp) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 96 | k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 97 | fclose(fp); |
| 98 | if (k) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 99 | *err_code = KEY_ERR_NONE; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 100 | return 1; |
| 101 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 102 | ERROR("Cannot load key from %s\n", key->fn); |
| 103 | *err_code = KEY_ERR_LOAD; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 104 | } |
| 105 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 106 | WARN("Cannot open file %s\n", key->fn); |
| 107 | *err_code = KEY_ERR_OPEN; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 108 | } |
| 109 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 110 | WARN("Key filename not specified\n"); |
| 111 | *err_code = KEY_ERR_FILENAME; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int key_store(key_t *key) |
| 118 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 119 | FILE *fp; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 120 | |
| 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 Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 137 | |
| 138 | int key_init(void) |
| 139 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 140 | cmd_opt_t cmd_opt; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 141 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 142 | unsigned int i; |
| 143 | |
Pankaj Gupta | dd906e6 | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 144 | 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 Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 166 | for (i = 0; i < num_keys; i++) { |
| 167 | key = &keys[i]; |
| 168 | if (key->opt != NULL) { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 169 | 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 Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 178 | return 0; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | key_t *key_get_by_opt(const char *opt) |
| 182 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 183 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 184 | 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 | } |