xref: /rk3399_ARM-atf/tools/cert_create/src/key.c (revision 742c4e1433162efd625bbecada2106a10ed7f46b)
1 /*
2  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
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 #if USE_TBBR_DEFS
17 #include <tbbr_oid.h>
18 #else
19 #include <platform_oid.h>
20 #endif
21 
22 #include "cert.h"
23 #include "cmd_opt.h"
24 #include "debug.h"
25 #include "key.h"
26 #include "sha.h"
27 
28 #define MAX_FILENAME_LEN		1024
29 
30 /*
31  * Create a new key container
32  */
33 int key_new(key_t *key)
34 {
35 	/* Create key pair container */
36 	key->key = EVP_PKEY_new();
37 	if (key->key == NULL) {
38 		return 0;
39 	}
40 
41 	return 1;
42 }
43 
44 static int key_create_rsa(key_t *key)
45 {
46 	BIGNUM *e;
47 	RSA *rsa = NULL;
48 
49 	e = BN_new();
50 	if (e == NULL) {
51 		printf("Cannot create RSA exponent\n");
52 		goto err;
53 	}
54 
55 	if (!BN_set_word(e, RSA_F4)) {
56 		printf("Cannot assign RSA exponent\n");
57 		goto err;
58 	}
59 
60 	rsa = RSA_new();
61 	if (rsa == NULL) {
62 		printf("Cannot create RSA key\n");
63 		goto err;
64 	}
65 
66 	if (!RSA_generate_key_ex(rsa, RSA_KEY_BITS, e, NULL)) {
67 		printf("Cannot generate RSA key\n");
68 		goto err;
69 	}
70 
71 	if (!EVP_PKEY_assign_RSA(key->key, rsa)) {
72 		printf("Cannot assign RSA key\n");
73 		goto err;
74 	}
75 
76 	return 1;
77 err:
78 	RSA_free(rsa);
79 	BN_free(e);
80 	return 0;
81 }
82 
83 #ifndef OPENSSL_NO_EC
84 static int key_create_ecdsa(key_t *key)
85 {
86 	EC_KEY *ec;
87 
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;
105 err:
106 	EC_KEY_free(ec);
107 	return 0;
108 }
109 #endif /* OPENSSL_NO_EC */
110 
111 typedef int (*key_create_fn_t)(key_t *key);
112 static 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 
119 int 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 
126 	if (key_create_fn[type]) {
127 		return key_create_fn[type](key);
128 	}
129 
130 	return 0;
131 }
132 
133 int key_load(key_t *key, unsigned int *err_code)
134 {
135 	FILE *fp;
136 	EVP_PKEY *k;
137 
138 	if (key->fn) {
139 		/* Load key from file */
140 		fp = fopen(key->fn, "r");
141 		if (fp) {
142 			k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
143 			fclose(fp);
144 			if (k) {
145 				*err_code = KEY_ERR_NONE;
146 				return 1;
147 			} else {
148 				ERROR("Cannot load key from %s\n", key->fn);
149 				*err_code = KEY_ERR_LOAD;
150 			}
151 		} else {
152 			WARN("Cannot open file %s\n", key->fn);
153 			*err_code = KEY_ERR_OPEN;
154 		}
155 	} else {
156 		WARN("Key filename not specified\n");
157 		*err_code = KEY_ERR_FILENAME;
158 	}
159 
160 	return 0;
161 }
162 
163 int key_store(key_t *key)
164 {
165 	FILE *fp;
166 
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 }
183 
184 int key_init(void)
185 {
186 	cmd_opt_t cmd_opt;
187 	key_t *key;
188 	unsigned int i;
189 
190 	for (i = 0; i < num_keys; i++) {
191 		key = &keys[i];
192 		if (key->opt != NULL) {
193 			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);
199 		}
200 	}
201 
202 	return 0;
203 }
204 
205 key_t *key_get_by_opt(const char *opt)
206 {
207 	key_t *key;
208 	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 }
221