xref: /rk3399_ARM-atf/tools/cert_create/src/key.c (revision fd6007de64fd7e16f6d96972643434c04a77f1c6)
1 /*
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"
41 #include "debug.h"
42 #include "key.h"
43 #include "platform_oid.h"
44 #include "sha.h"
45 
46 #define MAX_FILENAME_LEN		1024
47 
48 /*
49  * Create a new key container
50  */
51 static int key_new(key_t *key)
52 {
53 	/* Create key pair container */
54 	key->key = EVP_PKEY_new();
55 	if (key->key == NULL) {
56 		return 0;
57 	}
58 
59 	return 1;
60 }
61 
62 static int key_create_rsa(key_t *key)
63 {
64 	RSA *rsa = NULL;
65 
66 	rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL);
67 	if (rsa == NULL) {
68 		printf("Cannot create RSA key\n");
69 		goto err;
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 	return 0;
80 }
81 
82 #ifndef OPENSSL_NO_EC
83 static int key_create_ecdsa(key_t *key)
84 {
85 	EC_KEY *ec = NULL;
86 
87 	ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
88 	if (ec == NULL) {
89 		printf("Cannot create EC key\n");
90 		goto err;
91 	}
92 	if (!EC_KEY_generate_key(ec)) {
93 		printf("Cannot generate EC key\n");
94 		goto err;
95 	}
96 	EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS);
97 	EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
98 	if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) {
99 		printf("Cannot assign EC key\n");
100 		goto err;
101 	}
102 
103 	return 1;
104 err:
105 	EC_KEY_free(ec);
106 	return 0;
107 }
108 #endif /* OPENSSL_NO_EC */
109 
110 typedef int (*key_create_fn_t)(key_t *key);
111 static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = {
112 	key_create_rsa,
113 #ifndef OPENSSL_NO_EC
114 	key_create_ecdsa,
115 #endif /* OPENSSL_NO_EC */
116 };
117 
118 int key_create(key_t *key, int type)
119 {
120 	if (type >= KEY_ALG_MAX_NUM) {
121 		printf("Invalid key type\n");
122 		return 0;
123 	}
124 
125 	/* Create OpenSSL key container */
126 	if (!key_new(key)) {
127 		return 0;
128 	}
129 
130 	if (key_create_fn[type]) {
131 		return key_create_fn[type](key);
132 	}
133 
134 	return 0;
135 }
136 
137 int key_load(key_t *key, unsigned int *err_code)
138 {
139 	FILE *fp = NULL;
140 	EVP_PKEY *k = NULL;
141 
142 	/* Create OpenSSL key container */
143 	if (!key_new(key)) {
144 		*err_code = KEY_ERR_MALLOC;
145 		return 0;
146 	}
147 
148 	if (key->fn) {
149 		/* Load key from file */
150 		fp = fopen(key->fn, "r");
151 		if (fp) {
152 			k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
153 			fclose(fp);
154 			if (k) {
155 				*err_code = KEY_ERR_NONE;
156 				return 1;
157 			} else {
158 				ERROR("Cannot load key from %s\n", key->fn);
159 				*err_code = KEY_ERR_LOAD;
160 			}
161 		} else {
162 			WARN("Cannot open file %s\n", key->fn);
163 			*err_code = KEY_ERR_OPEN;
164 		}
165 	} else {
166 		WARN("Key filename not specified\n");
167 		*err_code = KEY_ERR_FILENAME;
168 	}
169 
170 	return 0;
171 }
172 
173 int key_store(key_t *key)
174 {
175 	FILE *fp = NULL;
176 
177 	if (key->fn) {
178 		fp = fopen(key->fn, "w");
179 		if (fp) {
180 			PEM_write_PrivateKey(fp, key->key,
181 					NULL, NULL, 0, NULL, NULL);
182 			fclose(fp);
183 			return 1;
184 		} else {
185 			ERROR("Cannot create file %s\n", key->fn);
186 		}
187 	} else {
188 		ERROR("Key filename not specified\n");
189 	}
190 
191 	return 0;
192 }
193