Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
Sandrine Bailleux | 0b14483 | 2020-01-07 15:12:08 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, 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 | |
| 24 | /* |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 25 | * Create a new key container |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 26 | */ |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 27 | int key_new(key_t *key) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 28 | { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 29 | /* Create key pair container */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 30 | key->key = EVP_PKEY_new(); |
| 31 | if (key->key == NULL) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 32 | return 0; |
| 33 | } |
| 34 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 35 | return 1; |
| 36 | } |
| 37 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 38 | static int key_create_rsa(key_t *key, int key_bits) |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 39 | { |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 40 | BIGNUM *e; |
| 41 | RSA *rsa = NULL; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 42 | |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 43 | 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 Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 55 | if (rsa == NULL) { |
| 56 | printf("Cannot create RSA key\n"); |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 57 | goto err; |
| 58 | } |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 59 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 60 | if (!RSA_generate_key_ex(rsa, key_bits, e, NULL)) { |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 61 | printf("Cannot generate RSA key\n"); |
| 62 | goto err; |
| 63 | } |
| 64 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 65 | if (!EVP_PKEY_assign_RSA(key->key, rsa)) { |
| 66 | printf("Cannot assign RSA key\n"); |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 67 | goto err; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Justin Chadwell | fa3ec4c | 2019-08-12 12:19:21 +0100 | [diff] [blame] | 70 | BN_free(e); |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 71 | return 1; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 72 | err: |
| 73 | RSA_free(rsa); |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 74 | BN_free(e); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | #ifndef OPENSSL_NO_EC |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 79 | static int key_create_ecdsa(key_t *key, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 80 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 81 | EC_KEY *ec; |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 82 | |
| 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; |
| 100 | err: |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 101 | EC_KEY_free(ec); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | #endif /* OPENSSL_NO_EC */ |
| 105 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 106 | typedef int (*key_create_fn_t)(key_t *key, int key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 107 | 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] | 108 | key_create_rsa, /* KEY_ALG_RSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 109 | #ifndef OPENSSL_NO_EC |
Qixiang Xu | 31f3bb9 | 2017-09-22 16:21:41 +0800 | [diff] [blame] | 110 | key_create_ecdsa, /* KEY_ALG_ECDSA */ |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 111 | #endif /* OPENSSL_NO_EC */ |
| 112 | }; |
| 113 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 114 | int key_create(key_t *key, int type, int key_bits) |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 115 | { |
| 116 | if (type >= KEY_ALG_MAX_NUM) { |
| 117 | printf("Invalid key type\n"); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 121 | if (key_create_fn[type]) { |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 122 | return key_create_fn[type](key, key_bits); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 123 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 124 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 128 | int key_load(key_t *key, unsigned int *err_code) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 129 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 130 | FILE *fp; |
| 131 | EVP_PKEY *k; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 132 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 133 | if (key->fn) { |
| 134 | /* Load key from file */ |
| 135 | fp = fopen(key->fn, "r"); |
| 136 | if (fp) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 137 | k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 138 | fclose(fp); |
| 139 | if (k) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 140 | *err_code = KEY_ERR_NONE; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 141 | return 1; |
| 142 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 143 | ERROR("Cannot load key from %s\n", key->fn); |
| 144 | *err_code = KEY_ERR_LOAD; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 145 | } |
| 146 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 147 | WARN("Cannot open file %s\n", key->fn); |
| 148 | *err_code = KEY_ERR_OPEN; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 149 | } |
| 150 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 151 | WARN("Key filename not specified\n"); |
| 152 | *err_code = KEY_ERR_FILENAME; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int key_store(key_t *key) |
| 159 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 160 | FILE *fp; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 161 | |
| 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 Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 178 | |
| 179 | int key_init(void) |
| 180 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 181 | cmd_opt_t cmd_opt; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 182 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 183 | unsigned int i; |
| 184 | |
| 185 | for (i = 0; i < num_keys; i++) { |
| 186 | key = &keys[i]; |
| 187 | if (key->opt != NULL) { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 188 | 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 Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 197 | return 0; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | key_t *key_get_by_opt(const char *opt) |
| 201 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 202 | key_t *key; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 203 | 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 | } |