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, 95 #ifndef OPENSSL_NO_EC 96 key_create_ecdsa, 97 #endif /* OPENSSL_NO_EC */ 98 }; 99 100 int key_create(key_t *key, int type) 101 { 102 if (type >= KEY_ALG_MAX_NUM) { 103 printf("Invalid key type\n"); 104 return 0; 105 } 106 107 if (key_create_fn[type]) { 108 return key_create_fn[type](key); 109 } 110 111 return 0; 112 } 113 114 int key_load(key_t *key, unsigned int *err_code) 115 { 116 FILE *fp; 117 EVP_PKEY *k; 118 119 if (key->fn) { 120 /* Load key from file */ 121 fp = fopen(key->fn, "r"); 122 if (fp) { 123 k = PEM_read_PrivateKey(fp, &key->key, NULL, NULL); 124 fclose(fp); 125 if (k) { 126 *err_code = KEY_ERR_NONE; 127 return 1; 128 } else { 129 ERROR("Cannot load key from %s\n", key->fn); 130 *err_code = KEY_ERR_LOAD; 131 } 132 } else { 133 WARN("Cannot open file %s\n", key->fn); 134 *err_code = KEY_ERR_OPEN; 135 } 136 } else { 137 WARN("Key filename not specified\n"); 138 *err_code = KEY_ERR_FILENAME; 139 } 140 141 return 0; 142 } 143 144 int key_store(key_t *key) 145 { 146 FILE *fp; 147 148 if (key->fn) { 149 fp = fopen(key->fn, "w"); 150 if (fp) { 151 PEM_write_PrivateKey(fp, key->key, 152 NULL, NULL, 0, NULL, NULL); 153 fclose(fp); 154 return 1; 155 } else { 156 ERROR("Cannot create file %s\n", key->fn); 157 } 158 } else { 159 ERROR("Key filename not specified\n"); 160 } 161 162 return 0; 163 } 164 165 int key_init(void) 166 { 167 cmd_opt_t cmd_opt; 168 key_t *key; 169 unsigned int i; 170 171 for (i = 0; i < num_keys; i++) { 172 key = &keys[i]; 173 if (key->opt != NULL) { 174 cmd_opt.long_opt.name = key->opt; 175 cmd_opt.long_opt.has_arg = required_argument; 176 cmd_opt.long_opt.flag = NULL; 177 cmd_opt.long_opt.val = CMD_OPT_KEY; 178 cmd_opt.help_msg = key->help_msg; 179 cmd_opt_add(&cmd_opt); 180 } 181 } 182 183 return 0; 184 } 185 186 key_t *key_get_by_opt(const char *opt) 187 { 188 key_t *key; 189 unsigned int i; 190 191 /* Sequential search. This is not a performance concern since the number 192 * of keys is bounded and the code runs on a host machine */ 193 for (i = 0; i < num_keys; i++) { 194 key = &keys[i]; 195 if (0 == strcmp(key->opt, opt)) { 196 return key; 197 } 198 } 199 200 return NULL; 201 } 202