blob: ce0e4da6eeaf74a2d0137f1eceac316bd0d7f13c [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <getopt.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <openssl/conf.h>
37#include <openssl/evp.h>
38#include <openssl/pem.h>
39
40#include "cert.h"
Juan Castillo1218dd52015-07-03 16:23:16 +010041#include "cmd_opt.h"
Juan Castillo11abdcd2014-10-21 11:30:42 +010042#include "debug.h"
43#include "key.h"
44#include "platform_oid.h"
45#include "sha.h"
46
47#define MAX_FILENAME_LEN 1024
48
49/*
Juan Castillof9f39c32015-06-01 16:34:23 +010050 * Create a new key container
Juan Castillo11abdcd2014-10-21 11:30:42 +010051 */
Masahiro Yamadabccb1092017-02-06 21:15:01 +090052int key_new(key_t *key)
Juan Castillo11abdcd2014-10-21 11:30:42 +010053{
Juan Castillo11abdcd2014-10-21 11:30:42 +010054 /* Create key pair container */
Juan Castillof9f39c32015-06-01 16:34:23 +010055 key->key = EVP_PKEY_new();
56 if (key->key == NULL) {
Juan Castillo11abdcd2014-10-21 11:30:42 +010057 return 0;
58 }
59
Juan Castillof9f39c32015-06-01 16:34:23 +010060 return 1;
61}
62
Juan Castilloa2224ab2015-06-30 13:36:57 +010063static int key_create_rsa(key_t *key)
Juan Castillof9f39c32015-06-01 16:34:23 +010064{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +090065 RSA *rsa;
Juan Castillof9f39c32015-06-01 16:34:23 +010066
Juan Castilloa2224ab2015-06-30 13:36:57 +010067 rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL);
68 if (rsa == NULL) {
69 printf("Cannot create RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010070 goto err;
71 }
Juan Castilloa2224ab2015-06-30 13:36:57 +010072 if (!EVP_PKEY_assign_RSA(key->key, rsa)) {
73 printf("Cannot assign RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010074 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010075 }
76
Juan Castillof9f39c32015-06-01 16:34:23 +010077 return 1;
Juan Castillof9f39c32015-06-01 16:34:23 +010078err:
79 RSA_free(rsa);
Juan Castilloa2224ab2015-06-30 13:36:57 +010080 return 0;
81}
82
83#ifndef OPENSSL_NO_EC
84static int key_create_ecdsa(key_t *key)
85{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +090086 EC_KEY *ec;
Juan Castilloa2224ab2015-06-30 13:36:57 +010087
88 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
89 if (ec == NULL) {
90 printf("Cannot create EC key\n");
91 goto err;
92 }
93 if (!EC_KEY_generate_key(ec)) {
94 printf("Cannot generate EC key\n");
95 goto err;
96 }
97 EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS);
98 EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
99 if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) {
100 printf("Cannot assign EC key\n");
101 goto err;
102 }
103
104 return 1;
105err:
Juan Castillof9f39c32015-06-01 16:34:23 +0100106 EC_KEY_free(ec);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100107 return 0;
108}
109#endif /* OPENSSL_NO_EC */
110
111typedef int (*key_create_fn_t)(key_t *key);
112static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
113 key_create_rsa,
114#ifndef OPENSSL_NO_EC
115 key_create_ecdsa,
116#endif /* OPENSSL_NO_EC */
117};
118
119int key_create(key_t *key, int type)
120{
121 if (type >= KEY_ALG_MAX_NUM) {
122 printf("Invalid key type\n");
123 return 0;
124 }
125
Juan Castilloa2224ab2015-06-30 13:36:57 +0100126 if (key_create_fn[type]) {
127 return key_create_fn[type](key);
128 }
Juan Castillof9f39c32015-06-01 16:34:23 +0100129
Juan Castillo11abdcd2014-10-21 11:30:42 +0100130 return 0;
131}
132
Juan Castillof9f39c32015-06-01 16:34:23 +0100133int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +0100134{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900135 FILE *fp;
136 EVP_PKEY *k;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100137
Juan Castillo11abdcd2014-10-21 11:30:42 +0100138 if (key->fn) {
139 /* Load key from file */
140 fp = fopen(key->fn, "r");
141 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100142 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +0100143 fclose(fp);
144 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100145 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100146 return 1;
147 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100148 ERROR("Cannot load key from %s\n", key->fn);
149 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100150 }
151 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100152 WARN("Cannot open file %s\n", key->fn);
153 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100154 }
155 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100156 WARN("Key filename not specified\n");
157 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100158 }
159
Juan Castillo11abdcd2014-10-21 11:30:42 +0100160 return 0;
161}
162
163int key_store(key_t *key)
164{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900165 FILE *fp;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100166
167 if (key->fn) {
168 fp = fopen(key->fn, "w");
169 if (fp) {
170 PEM_write_PrivateKey(fp, key->key,
171 NULL, NULL, 0, NULL, NULL);
172 fclose(fp);
173 return 1;
174 } else {
175 ERROR("Cannot create file %s\n", key->fn);
176 }
177 } else {
178 ERROR("Key filename not specified\n");
179 }
180
181 return 0;
182}
Juan Castillo1218dd52015-07-03 16:23:16 +0100183
184int key_init(void)
185{
Juan Castillo212f7382015-12-15 16:37:57 +0000186 cmd_opt_t cmd_opt;
Juan Castillo1218dd52015-07-03 16:23:16 +0100187 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100188 unsigned int i;
189
190 for (i = 0; i < num_keys; i++) {
191 key = &keys[i];
192 if (key->opt != NULL) {
Juan Castillo212f7382015-12-15 16:37:57 +0000193 cmd_opt.long_opt.name = key->opt;
194 cmd_opt.long_opt.has_arg = required_argument;
195 cmd_opt.long_opt.flag = NULL;
196 cmd_opt.long_opt.val = CMD_OPT_KEY;
197 cmd_opt.help_msg = key->help_msg;
198 cmd_opt_add(&cmd_opt);
Juan Castillo1218dd52015-07-03 16:23:16 +0100199 }
200 }
201
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900202 return 0;
Juan Castillo1218dd52015-07-03 16:23:16 +0100203}
204
205key_t *key_get_by_opt(const char *opt)
206{
Masahiro Yamada48cb5e52017-02-06 19:47:44 +0900207 key_t *key;
Juan Castillo1218dd52015-07-03 16:23:16 +0100208 unsigned int i;
209
210 /* Sequential search. This is not a performance concern since the number
211 * of keys is bounded and the code runs on a host machine */
212 for (i = 0; i < num_keys; i++) {
213 key = &keys[i];
214 if (0 == strcmp(key->opt, opt)) {
215 return key;
216 }
217 }
218
219 return NULL;
220}