blob: 6072d9ccee1127ce4e86fea60ead1ad684edecd5 [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"
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/*
Juan Castillof9f39c32015-06-01 16:34:23 +010049 * Create a new key container
Juan Castillo11abdcd2014-10-21 11:30:42 +010050 */
Juan Castillof9f39c32015-06-01 16:34:23 +010051static int key_new(key_t *key)
Juan Castillo11abdcd2014-10-21 11:30:42 +010052{
Juan Castillo11abdcd2014-10-21 11:30:42 +010053 /* Create key pair container */
Juan Castillof9f39c32015-06-01 16:34:23 +010054 key->key = EVP_PKEY_new();
55 if (key->key == NULL) {
Juan Castillo11abdcd2014-10-21 11:30:42 +010056 return 0;
57 }
58
Juan Castillof9f39c32015-06-01 16:34:23 +010059 return 1;
60}
61
Juan Castilloa2224ab2015-06-30 13:36:57 +010062static int key_create_rsa(key_t *key)
Juan Castillof9f39c32015-06-01 16:34:23 +010063{
64 RSA *rsa = NULL;
Juan Castillof9f39c32015-06-01 16:34:23 +010065
Juan Castilloa2224ab2015-06-30 13:36:57 +010066 rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL);
67 if (rsa == NULL) {
68 printf("Cannot create RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010069 goto err;
70 }
Juan Castilloa2224ab2015-06-30 13:36:57 +010071 if (!EVP_PKEY_assign_RSA(key->key, rsa)) {
72 printf("Cannot assign RSA key\n");
Juan Castillof9f39c32015-06-01 16:34:23 +010073 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010074 }
75
Juan Castillof9f39c32015-06-01 16:34:23 +010076 return 1;
Juan Castillof9f39c32015-06-01 16:34:23 +010077err:
78 RSA_free(rsa);
Juan Castilloa2224ab2015-06-30 13:36:57 +010079 return 0;
80}
81
82#ifndef OPENSSL_NO_EC
83static 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;
104err:
Juan Castillof9f39c32015-06-01 16:34:23 +0100105 EC_KEY_free(ec);
Juan Castilloa2224ab2015-06-30 13:36:57 +0100106 return 0;
107}
108#endif /* OPENSSL_NO_EC */
109
110typedef int (*key_create_fn_t)(key_t *key);
111static 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
118int 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 }
Juan Castillof9f39c32015-06-01 16:34:23 +0100133
Juan Castillo11abdcd2014-10-21 11:30:42 +0100134 return 0;
135}
136
Juan Castillof9f39c32015-06-01 16:34:23 +0100137int key_load(key_t *key, unsigned int *err_code)
Juan Castillo11abdcd2014-10-21 11:30:42 +0100138{
139 FILE *fp = NULL;
140 EVP_PKEY *k = NULL;
141
Juan Castillof9f39c32015-06-01 16:34:23 +0100142 /* Create OpenSSL key container */
143 if (!key_new(key)) {
144 *err_code = KEY_ERR_MALLOC;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100145 return 0;
146 }
147
148 if (key->fn) {
149 /* Load key from file */
150 fp = fopen(key->fn, "r");
151 if (fp) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100152 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL);
Juan Castillo11abdcd2014-10-21 11:30:42 +0100153 fclose(fp);
154 if (k) {
Juan Castillof9f39c32015-06-01 16:34:23 +0100155 *err_code = KEY_ERR_NONE;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100156 return 1;
157 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100158 ERROR("Cannot load key from %s\n", key->fn);
159 *err_code = KEY_ERR_LOAD;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100160 }
161 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100162 WARN("Cannot open file %s\n", key->fn);
163 *err_code = KEY_ERR_OPEN;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100164 }
165 } else {
Juan Castillof9f39c32015-06-01 16:34:23 +0100166 WARN("Key filename not specified\n");
167 *err_code = KEY_ERR_FILENAME;
Juan Castillo11abdcd2014-10-21 11:30:42 +0100168 }
169
Juan Castillo11abdcd2014-10-21 11:30:42 +0100170 return 0;
171}
172
173int 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}