Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 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" |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 41 | #include "cmd_opt.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 42 | #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 Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 50 | * Create a new key container |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 51 | */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 52 | static int key_new(key_t *key) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 53 | { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 54 | /* Create key pair container */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 55 | key->key = EVP_PKEY_new(); |
| 56 | if (key->key == NULL) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 63 | static int key_create_rsa(key_t *key) |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 64 | { |
| 65 | RSA *rsa = NULL; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 66 | |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 67 | rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL); |
| 68 | if (rsa == NULL) { |
| 69 | printf("Cannot create RSA key\n"); |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 70 | goto err; |
| 71 | } |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 72 | if (!EVP_PKEY_assign_RSA(key->key, rsa)) { |
| 73 | printf("Cannot assign RSA key\n"); |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 74 | goto err; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 77 | return 1; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 78 | err: |
| 79 | RSA_free(rsa); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | #ifndef OPENSSL_NO_EC |
| 84 | static int key_create_ecdsa(key_t *key) |
| 85 | { |
| 86 | EC_KEY *ec = NULL; |
| 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: |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 106 | EC_KEY_free(ec); |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 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 | /* Create OpenSSL key container */ |
| 127 | if (!key_new(key)) { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | if (key_create_fn[type]) { |
| 132 | return key_create_fn[type](key); |
| 133 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 134 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 138 | int key_load(key_t *key, unsigned int *err_code) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 139 | { |
| 140 | FILE *fp = NULL; |
| 141 | EVP_PKEY *k = NULL; |
| 142 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 143 | /* Create OpenSSL key container */ |
| 144 | if (!key_new(key)) { |
| 145 | *err_code = KEY_ERR_MALLOC; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | if (key->fn) { |
| 150 | /* Load key from file */ |
| 151 | fp = fopen(key->fn, "r"); |
| 152 | if (fp) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 153 | k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 154 | fclose(fp); |
| 155 | if (k) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 156 | *err_code = KEY_ERR_NONE; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 157 | return 1; |
| 158 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 159 | ERROR("Cannot load key from %s\n", key->fn); |
| 160 | *err_code = KEY_ERR_LOAD; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 161 | } |
| 162 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 163 | WARN("Cannot open file %s\n", key->fn); |
| 164 | *err_code = KEY_ERR_OPEN; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 165 | } |
| 166 | } else { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 167 | WARN("Key filename not specified\n"); |
| 168 | *err_code = KEY_ERR_FILENAME; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int key_store(key_t *key) |
| 175 | { |
| 176 | FILE *fp = NULL; |
| 177 | |
| 178 | if (key->fn) { |
| 179 | fp = fopen(key->fn, "w"); |
| 180 | if (fp) { |
| 181 | PEM_write_PrivateKey(fp, key->key, |
| 182 | NULL, NULL, 0, NULL, NULL); |
| 183 | fclose(fp); |
| 184 | return 1; |
| 185 | } else { |
| 186 | ERROR("Cannot create file %s\n", key->fn); |
| 187 | } |
| 188 | } else { |
| 189 | ERROR("Key filename not specified\n"); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 194 | |
| 195 | int key_init(void) |
| 196 | { |
| 197 | key_t *key; |
| 198 | int rc = 0; |
| 199 | unsigned int i; |
| 200 | |
| 201 | for (i = 0; i < num_keys; i++) { |
| 202 | key = &keys[i]; |
| 203 | if (key->opt != NULL) { |
| 204 | rc = cmd_opt_add(key->opt, required_argument, |
| 205 | CMD_OPT_KEY); |
| 206 | if (rc != 0) { |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | key_t *key_get_by_opt(const char *opt) |
| 216 | { |
| 217 | key_t *key = NULL; |
| 218 | unsigned int i; |
| 219 | |
| 220 | /* Sequential search. This is not a performance concern since the number |
| 221 | * of keys is bounded and the code runs on a host machine */ |
| 222 | for (i = 0; i < num_keys; i++) { |
| 223 | key = &keys[i]; |
| 224 | if (0 == strcmp(key->opt, opt)) { |
| 225 | return key; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return NULL; |
| 230 | } |