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 RSA *rsa; 47 48 rsa = RSA_generate_key(RSA_KEY_BITS, RSA_F4, NULL, NULL); 49 if (rsa == NULL) { 50 printf("Cannot create RSA key\n"); 51 goto err; 52 } 53 if (!EVP_PKEY_assign_RSA(key->key, rsa)) { 54 printf("Cannot assign RSA key\n"); 55 goto err; 56 } 57 58 return 1; 59 err: 60 RSA_free(rsa); 61 return 0; 62 } 63 64 #ifndef OPENSSL_NO_EC 65 static int key_create_ecdsa(key_t *key) 66 { 67 EC_KEY *ec; 68 69 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 70 if (ec == NULL) { 71 printf("Cannot create EC key\n"); 72 goto err; 73 } 74 if (!EC_KEY_generate_key(ec)) { 75 printf("Cannot generate EC key\n"); 76 goto err; 77 } 78 EC_KEY_set_flags(ec, EC_PKEY_NO_PARAMETERS); 79 EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE); 80 if (!EVP_PKEY_assign_EC_KEY(key->key, ec)) { 81 printf("Cannot assign EC key\n"); 82 goto err; 83 } 84 85 return 1; 86 err: 87 EC_KEY_free(ec); 88 return 0; 89 } 90 #endif /* OPENSSL_NO_EC */ 91 92 typedef int (*key_create_fn_t)(key_t *key); 93 static const key_create_fn_t key_create_fn[KEY_ALG_MAX_NUM] = { 94 key_create_rsa, /* KEY_ALG_RSA */ 95 key_create_rsa, /* KEY_ALG_RSA_1_5 */ 96 #ifndef OPENSSL_NO_EC 97 key_create_ecdsa, /* KEY_ALG_ECDSA */ 98 #endif /* OPENSSL_NO_EC */ 99 }; 100 101 int key_create(key_t *key, int type) 102 { 103 if (type >= KEY_ALG_MAX_NUM) { 104 printf("Invalid key type\n"); 105 return 0; 106 } 107 108 if (key_create_fn[type]) { 109 return key_create_fn[type](key); 110 } 111 112 return 0; 113 } 114 115 int key_load(key_t *key, unsigned int *err_code) 116 { 117 FILE *fp; 118 EVP_PKEY *k; 119 120 if (key->fn) { 121 /* Load key from file */ 122 fp = fopen(key->fn, "r"); 123 if (fp) { 124 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); 125 fclose(fp); 126 if (k) { 127 *err_code = KEY_ERR_NONE; 128 return 1; 129 } else { 130 ERROR("Cannot load key from %s\n", key->fn); 131 *err_code = KEY_ERR_LOAD; 132 } 133 } else { 134 WARN("Cannot open file %s\n", key->fn); 135 *err_code = KEY_ERR_OPEN; 136 } 137 } else { 138 WARN("Key filename not specified\n"); 139 *err_code = KEY_ERR_FILENAME; 140 } 141 142 return 0; 143 } 144 145 int key_store(key_t *key) 146 { 147 FILE *fp; 148 149 if (key->fn) { 150 fp = fopen(key->fn, "w"); 151 if (fp) { 152 PEM_write_PrivateKey(fp, key->key, 153 NULL, NULL, 0, NULL, NULL); 154 fclose(fp); 155 return 1; 156 } else { 157 ERROR("Cannot create file %s\n", key->fn); 158 } 159 } else { 160 ERROR("Key filename not specified\n"); 161 } 162 163 return 0; 164 } 165 166 int key_init(void) 167 { 168 cmd_opt_t cmd_opt; 169 key_t *key; 170 unsigned int i; 171 172 for (i = 0; i < num_keys; i++) { 173 key = &keys[i]; 174 if (key->opt != NULL) { 175 cmd_opt.long_opt.name = key->opt; 176 cmd_opt.long_opt.has_arg = required_argument; 177 cmd_opt.long_opt.flag = NULL; 178 cmd_opt.long_opt.val = CMD_OPT_KEY; 179 cmd_opt.help_msg = key->help_msg; 180 cmd_opt_add(&cmd_opt); 181 } 182 } 183 184 return 0; 185 } 186 187 key_t *key_get_by_opt(const char *opt) 188 { 189 key_t *key; 190 unsigned int i; 191 192 /* Sequential search. This is not a performance concern since the number 193 * of keys is bounded and the code runs on a host machine */ 194 for (i = 0; i < num_keys; i++) { 195 key = &keys[i]; 196 if (0 == strcmp(key->opt, opt)) { 197 return key; 198 } 199 } 200 201 return NULL; 202 } 203