Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
Chris Kay | a9f543a | 2024-06-14 11:31:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved. |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Manish V Badarkhe | 7b425de | 2024-07-19 08:31:51 +0100 | [diff] [blame] | 7 | #define _POSIX_C_SOURCE 200809L |
| 8 | |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <ctype.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 11 | #include <getopt.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 15 | #include <stdbool.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 16 | |
| 17 | #include <openssl/conf.h> |
| 18 | #include <openssl/engine.h> |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/pem.h> |
| 21 | #include <openssl/sha.h> |
| 22 | #include <openssl/x509v3.h> |
| 23 | |
| 24 | #include "cert.h" |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 25 | #include "cmd_opt.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 26 | #include "debug.h" |
| 27 | #include "ext.h" |
| 28 | #include "key.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 29 | #include "sha.h" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Helper macros to simplify the code. This macro assigns the return value of |
| 33 | * the 'fn' function to 'v' and exits if the value is NULL. |
| 34 | */ |
| 35 | #define CHECK_NULL(v, fn) \ |
| 36 | do { \ |
| 37 | v = fn; \ |
| 38 | if (v == NULL) { \ |
| 39 | ERROR("NULL object at %s:%d\n", __FILE__, __LINE__); \ |
| 40 | exit(1); \ |
| 41 | } \ |
| 42 | } while (0) |
| 43 | |
| 44 | /* |
| 45 | * This macro assigns the NID corresponding to 'oid' to 'v' and exits if the |
| 46 | * NID is undefined. |
| 47 | */ |
| 48 | #define CHECK_OID(v, oid) \ |
| 49 | do { \ |
| 50 | v = OBJ_txt2nid(oid); \ |
| 51 | if (v == NID_undef) { \ |
Sandrine Bailleux | 0d11b48 | 2020-01-15 11:01:25 +0100 | [diff] [blame] | 52 | ERROR("Cannot find extension %s\n", oid); \ |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 53 | exit(1); \ |
| 54 | } \ |
| 55 | } while (0) |
| 56 | |
| 57 | #define MAX_FILENAME_LEN 1024 |
| 58 | #define VAL_DAYS 7300 |
| 59 | #define ID_TO_BIT_MASK(id) (1 << id) |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 60 | #define NUM_ELEM(x) ((sizeof(x)) / (sizeof(x[0]))) |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 61 | #define HELP_OPT_MAX_LEN 128 |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 62 | |
| 63 | /* Global options */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 64 | static int key_alg; |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 65 | static int hash_alg; |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 66 | static int key_size; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 67 | static int new_keys; |
| 68 | static int save_keys; |
| 69 | static int print_cert; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 70 | |
Chris Kay | a9f543a | 2024-06-14 11:31:03 +0000 | [diff] [blame] | 71 | static const char build_msg[] = "Built : " __TIME__ ", " __DATE__; |
| 72 | static const char platform_msg[] = PLAT_MSG; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 73 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 74 | static const char *key_algs_str[] = { |
| 75 | [KEY_ALG_RSA] = "rsa", |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 76 | #ifndef OPENSSL_NO_EC |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 77 | [KEY_ALG_ECDSA_NIST] = "ecdsa", |
Donald Chan | 50d5397 | 2024-04-10 14:56:53 -0700 | [diff] [blame] | 78 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 79 | [KEY_ALG_ECDSA_BRAINPOOL_R] = "ecdsa-brainpool-regular", |
| 80 | [KEY_ALG_ECDSA_BRAINPOOL_T] = "ecdsa-brainpool-twisted", |
Donald Chan | 50d5397 | 2024-04-10 14:56:53 -0700 | [diff] [blame] | 81 | #endif |
Juan Castillo | a2224ab | 2015-06-30 13:36:57 +0100 | [diff] [blame] | 82 | #endif /* OPENSSL_NO_EC */ |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 83 | }; |
| 84 | |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 85 | static const char *hash_algs_str[] = { |
| 86 | [HASH_ALG_SHA256] = "sha256", |
| 87 | [HASH_ALG_SHA384] = "sha384", |
| 88 | [HASH_ALG_SHA512] = "sha512", |
| 89 | }; |
| 90 | |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 91 | static void print_help(const char *cmd, const struct option *long_opt) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 92 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 93 | int rem, i = 0; |
| 94 | const struct option *opt; |
| 95 | char line[HELP_OPT_MAX_LEN]; |
| 96 | char *p; |
| 97 | |
| 98 | assert(cmd != NULL); |
| 99 | assert(long_opt != NULL); |
| 100 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 101 | printf("\n\n"); |
| 102 | printf("The certificate generation tool loads the binary images and\n" |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 103 | "optionally the RSA or ECC keys, and outputs the key and content\n" |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 104 | "certificates properly signed to implement the chain of trust.\n" |
| 105 | "If keys are provided, they must be in PEM format.\n" |
| 106 | "Certificates are generated in DER format.\n"); |
| 107 | printf("\n"); |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 108 | printf("Usage:\n"); |
| 109 | printf("\t%s [OPTIONS]\n\n", cmd); |
| 110 | |
| 111 | printf("Available options:\n"); |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 112 | opt = long_opt; |
| 113 | while (opt->name) { |
| 114 | p = line; |
| 115 | rem = HELP_OPT_MAX_LEN; |
| 116 | if (isalpha(opt->val)) { |
| 117 | /* Short format */ |
| 118 | sprintf(p, "-%c,", (char)opt->val); |
| 119 | p += 3; |
| 120 | rem -= 3; |
| 121 | } |
| 122 | snprintf(p, rem, "--%s %s", opt->name, |
| 123 | (opt->has_arg == required_argument) ? "<arg>" : ""); |
| 124 | printf("\t%-32s %s\n", line, cmd_opt_get_help_msg(i)); |
| 125 | opt++; |
| 126 | i++; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 127 | } |
| 128 | printf("\n"); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 131 | static int get_key_alg(const char *key_alg_str) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0 ; i < NUM_ELEM(key_algs_str) ; i++) { |
| 136 | if (0 == strcmp(key_alg_str, key_algs_str[i])) { |
| 137 | return i; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return -1; |
| 142 | } |
| 143 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 144 | static int get_key_size(const char *key_size_str) |
| 145 | { |
| 146 | char *end; |
| 147 | long key_size; |
| 148 | |
| 149 | key_size = strtol(key_size_str, &end, 10); |
| 150 | if (*end != '\0') |
| 151 | return -1; |
| 152 | |
| 153 | return key_size; |
| 154 | } |
| 155 | |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 156 | static int get_hash_alg(const char *hash_alg_str) |
| 157 | { |
| 158 | int i; |
| 159 | |
| 160 | for (i = 0 ; i < NUM_ELEM(hash_algs_str) ; i++) { |
| 161 | if (0 == strcmp(hash_alg_str, hash_algs_str[i])) { |
| 162 | return i; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return -1; |
| 167 | } |
| 168 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 169 | static void check_cmd_params(void) |
| 170 | { |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 171 | cert_t *cert; |
| 172 | ext_t *ext; |
Manish V Badarkhe | 7b425de | 2024-07-19 08:31:51 +0100 | [diff] [blame] | 173 | cert_key_t *key; |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 174 | int i, j; |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 175 | bool valid_size; |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 176 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 177 | /* Only save new keys */ |
| 178 | if (save_keys && !new_keys) { |
| 179 | ERROR("Only new keys can be saved to disk\n"); |
| 180 | exit(1); |
| 181 | } |
| 182 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 183 | /* Validate key-size */ |
| 184 | valid_size = false; |
| 185 | for (i = 0; i < KEY_SIZE_MAX_NUM; i++) { |
| 186 | if (key_size == KEY_SIZES[key_alg][i]) { |
| 187 | valid_size = true; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | if (!valid_size) { |
| 192 | ERROR("'%d' is not a valid key size for '%s'\n", |
| 193 | key_size, key_algs_str[key_alg]); |
| 194 | NOTICE("Valid sizes are: "); |
| 195 | for (i = 0; i < KEY_SIZE_MAX_NUM && |
| 196 | KEY_SIZES[key_alg][i] != 0; i++) { |
| 197 | printf("%d ", KEY_SIZES[key_alg][i]); |
| 198 | } |
| 199 | printf("\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 203 | /* Check that all required options have been specified in the |
| 204 | * command line */ |
| 205 | for (i = 0; i < num_certs; i++) { |
| 206 | cert = &certs[i]; |
| 207 | if (cert->fn == NULL) { |
| 208 | /* Certificate not requested. Skip to the next one */ |
| 209 | continue; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 212 | /* Check that all parameters required to create this certificate |
| 213 | * have been specified in the command line */ |
| 214 | for (j = 0; j < cert->num_ext; j++) { |
| 215 | ext = &extensions[cert->ext[j]]; |
| 216 | switch (ext->type) { |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 217 | case EXT_TYPE_NVCOUNTER: |
| 218 | /* Counter value must be specified */ |
| 219 | if ((!ext->optional) && (ext->arg == NULL)) { |
| 220 | ERROR("Value for '%s' not specified\n", |
| 221 | ext->ln); |
| 222 | exit(1); |
| 223 | } |
| 224 | break; |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 225 | case EXT_TYPE_PKEY: |
| 226 | /* Key filename must be specified */ |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 227 | key = &keys[ext->attr.key]; |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 228 | if (!new_keys && key->fn == NULL) { |
| 229 | ERROR("Key '%s' required by '%s' not " |
| 230 | "specified\n", key->desc, |
| 231 | cert->cn); |
| 232 | exit(1); |
| 233 | } |
| 234 | break; |
| 235 | case EXT_TYPE_HASH: |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Binary image must be specified |
| 238 | * unless it is explicitly made optional. |
| 239 | */ |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 240 | if ((!ext->optional) && (ext->arg == NULL)) { |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 241 | ERROR("Image for '%s' not specified\n", |
| 242 | ext->ln); |
| 243 | exit(1); |
| 244 | } |
| 245 | break; |
| 246 | default: |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 247 | ERROR("Unknown extension type '%d' in '%s'\n", |
| 248 | ext->type, ext->ln); |
Juan Castillo | 86958fd | 2015-07-08 12:11:38 +0100 | [diff] [blame] | 249 | exit(1); |
| 250 | break; |
| 251 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 256 | /* Common command line options */ |
| 257 | static const cmd_opt_t common_cmd_opt[] = { |
| 258 | { |
| 259 | { "help", no_argument, NULL, 'h' }, |
| 260 | "Print this message and exit" |
| 261 | }, |
| 262 | { |
| 263 | { "key-alg", required_argument, NULL, 'a' }, |
Donald Chan | 50d5397 | 2024-04-10 14:56:53 -0700 | [diff] [blame] | 264 | "Key algorithm: 'rsa' (default)- RSAPSS scheme as per PKCS#1 v2.1, " |
| 265 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
Lionel Debieve | fefeffb | 2022-11-14 11:03:42 +0100 | [diff] [blame] | 266 | "'ecdsa', 'ecdsa-brainpool-regular', 'ecdsa-brainpool-twisted'" |
Donald Chan | 50d5397 | 2024-04-10 14:56:53 -0700 | [diff] [blame] | 267 | #else |
| 268 | "'ecdsa'" |
| 269 | #endif |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 270 | }, |
| 271 | { |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 272 | { "key-size", required_argument, NULL, 'b' }, |
| 273 | "Key size (for supported algorithms)." |
| 274 | }, |
| 275 | { |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 276 | { "hash-alg", required_argument, NULL, 's' }, |
| 277 | "Hash algorithm : 'sha256' (default), 'sha384', 'sha512'" |
| 278 | }, |
| 279 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 280 | { "save-keys", no_argument, NULL, 'k' }, |
| 281 | "Save key pairs into files. Filenames must be provided" |
| 282 | }, |
| 283 | { |
| 284 | { "new-keys", no_argument, NULL, 'n' }, |
| 285 | "Generate new key pairs if no key files are provided" |
| 286 | }, |
| 287 | { |
| 288 | { "print-cert", no_argument, NULL, 'p' }, |
| 289 | "Print the certificates in the standard output" |
| 290 | } |
| 291 | }; |
| 292 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 293 | int main(int argc, char *argv[]) |
| 294 | { |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 295 | STACK_OF(X509_EXTENSION) * sk; |
Michalis Pappas | 3862894 | 2017-10-06 16:11:44 +0800 | [diff] [blame] | 296 | X509_EXTENSION *cert_ext = NULL; |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 297 | ext_t *ext; |
Manish V Badarkhe | 7b425de | 2024-07-19 08:31:51 +0100 | [diff] [blame] | 298 | cert_key_t *key; |
Masahiro Yamada | 48cb5e5 | 2017-02-06 19:47:44 +0900 | [diff] [blame] | 299 | cert_t *cert; |
| 300 | FILE *file; |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 301 | int i, j, ext_nid, nvctr; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 302 | int c, opt_idx = 0; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 303 | const struct option *cmd_opt; |
| 304 | const char *cur_opt; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 305 | unsigned int err_code; |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 306 | unsigned char md[SHA512_DIGEST_LENGTH]; |
| 307 | unsigned int md_len; |
Juan Castillo | ac40293 | 2015-03-05 14:30:00 +0000 | [diff] [blame] | 308 | const EVP_MD *md_info; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 309 | |
| 310 | NOTICE("CoT Generation Tool: %s\n", build_msg); |
| 311 | NOTICE("Target platform: %s\n", platform_msg); |
| 312 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 313 | /* Set default options */ |
| 314 | key_alg = KEY_ALG_RSA; |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 315 | hash_alg = HASH_ALG_SHA256; |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 316 | key_size = -1; |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 317 | |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 318 | /* Add common command line options */ |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 319 | for (i = 0; i < NUM_ELEM(common_cmd_opt); i++) { |
| 320 | cmd_opt_add(&common_cmd_opt[i]); |
| 321 | } |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 322 | |
| 323 | /* Initialize the certificates */ |
| 324 | if (cert_init() != 0) { |
| 325 | ERROR("Cannot initialize certificates\n"); |
| 326 | exit(1); |
| 327 | } |
| 328 | |
| 329 | /* Initialize the keys */ |
| 330 | if (key_init() != 0) { |
| 331 | ERROR("Cannot initialize keys\n"); |
| 332 | exit(1); |
| 333 | } |
| 334 | |
| 335 | /* Initialize the new types and register OIDs for the extensions */ |
| 336 | if (ext_init() != 0) { |
Sandrine Bailleux | 0d11b48 | 2020-01-15 11:01:25 +0100 | [diff] [blame] | 337 | ERROR("Cannot initialize extensions\n"); |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 338 | exit(1); |
| 339 | } |
| 340 | |
| 341 | /* Get the command line options populated during the initialization */ |
| 342 | cmd_opt = cmd_opt_get_array(); |
| 343 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 344 | while (1) { |
| 345 | /* getopt_long stores the option index here. */ |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 346 | c = getopt_long(argc, argv, "a:b:hknps:", cmd_opt, &opt_idx); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 347 | |
| 348 | /* Detect the end of the options. */ |
| 349 | if (c == -1) { |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | switch (c) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 354 | case 'a': |
| 355 | key_alg = get_key_alg(optarg); |
| 356 | if (key_alg < 0) { |
| 357 | ERROR("Invalid key algorithm '%s'\n", optarg); |
| 358 | exit(1); |
| 359 | } |
| 360 | break; |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 361 | case 'b': |
| 362 | key_size = get_key_size(optarg); |
| 363 | if (key_size <= 0) { |
| 364 | ERROR("Invalid key size '%s'\n", optarg); |
| 365 | exit(1); |
| 366 | } |
| 367 | break; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 368 | case 'h': |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 369 | print_help(argv[0], cmd_opt); |
Roberto Vargas | 58a5b2b | 2018-06-27 08:23:22 +0100 | [diff] [blame] | 370 | exit(0); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 371 | case 'k': |
| 372 | save_keys = 1; |
| 373 | break; |
| 374 | case 'n': |
| 375 | new_keys = 1; |
| 376 | break; |
| 377 | case 'p': |
| 378 | print_cert = 1; |
| 379 | break; |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 380 | case 's': |
| 381 | hash_alg = get_hash_alg(optarg); |
| 382 | if (hash_alg < 0) { |
| 383 | ERROR("Invalid hash algorithm '%s'\n", optarg); |
| 384 | exit(1); |
| 385 | } |
| 386 | break; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 387 | case CMD_OPT_EXT: |
| 388 | cur_opt = cmd_opt_get_name(opt_idx); |
| 389 | ext = ext_get_by_opt(cur_opt); |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 390 | ext->arg = strdup(optarg); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 391 | break; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 392 | case CMD_OPT_KEY: |
| 393 | cur_opt = cmd_opt_get_name(opt_idx); |
| 394 | key = key_get_by_opt(cur_opt); |
| 395 | key->fn = strdup(optarg); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 396 | break; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 397 | case CMD_OPT_CERT: |
| 398 | cur_opt = cmd_opt_get_name(opt_idx); |
| 399 | cert = cert_get_by_opt(cur_opt); |
| 400 | cert->fn = strdup(optarg); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 401 | break; |
| 402 | case '?': |
| 403 | default: |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 404 | print_help(argv[0], cmd_opt); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 405 | exit(1); |
| 406 | } |
| 407 | } |
| 408 | |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 409 | /* Select a reasonable default key-size */ |
| 410 | if (key_size == -1) { |
| 411 | key_size = KEY_SIZES[key_alg][0]; |
| 412 | } |
| 413 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 414 | /* Check command line arguments */ |
| 415 | check_cmd_params(); |
| 416 | |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 417 | /* Indicate SHA as image hash algorithm in the certificate |
Juan Castillo | ac40293 | 2015-03-05 14:30:00 +0000 | [diff] [blame] | 418 | * extension */ |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 419 | if (hash_alg == HASH_ALG_SHA384) { |
| 420 | md_info = EVP_sha384(); |
| 421 | md_len = SHA384_DIGEST_LENGTH; |
| 422 | } else if (hash_alg == HASH_ALG_SHA512) { |
| 423 | md_info = EVP_sha512(); |
| 424 | md_len = SHA512_DIGEST_LENGTH; |
| 425 | } else { |
| 426 | md_info = EVP_sha256(); |
| 427 | md_len = SHA256_DIGEST_LENGTH; |
| 428 | } |
Juan Castillo | ac40293 | 2015-03-05 14:30:00 +0000 | [diff] [blame] | 429 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 430 | /* Load private keys from files (or generate new ones) */ |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 431 | for (i = 0 ; i < num_keys ; i++) { |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 432 | #if !USING_OPENSSL3 |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 433 | if (!key_new(&keys[i])) { |
| 434 | ERROR("Failed to allocate key container\n"); |
| 435 | exit(1); |
| 436 | } |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 437 | #endif |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 438 | |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 439 | /* First try to load the key from disk */ |
Sandrine Bailleux | ea9f2f5 | 2023-10-16 14:52:06 +0200 | [diff] [blame] | 440 | err_code = key_load(&keys[i]); |
| 441 | if (err_code == KEY_ERR_NONE) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 442 | /* Key loaded successfully */ |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | /* Key not loaded. Check the error code */ |
Masahiro Yamada | bccb109 | 2017-02-06 21:15:01 +0900 | [diff] [blame] | 447 | if (err_code == KEY_ERR_LOAD) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 448 | /* File exists, but it does not contain a valid private |
| 449 | * key. Abort. */ |
| 450 | ERROR("Error loading '%s'\n", keys[i].fn); |
| 451 | exit(1); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 452 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 453 | |
| 454 | /* File does not exist, could not be opened or no filename was |
| 455 | * given */ |
| 456 | if (new_keys) { |
| 457 | /* Try to create a new key */ |
| 458 | NOTICE("Creating new key for '%s'\n", keys[i].desc); |
Justin Chadwell | febe86c | 2019-07-29 17:13:45 +0100 | [diff] [blame] | 459 | if (!key_create(&keys[i], key_alg, key_size)) { |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 460 | ERROR("Error creating key '%s'\n", keys[i].desc); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 461 | exit(1); |
| 462 | } |
Juan Castillo | f9f39c3 | 2015-06-01 16:34:23 +0100 | [diff] [blame] | 463 | } else { |
| 464 | if (err_code == KEY_ERR_OPEN) { |
| 465 | ERROR("Error opening '%s'\n", keys[i].fn); |
| 466 | } else { |
| 467 | ERROR("Key '%s' not specified\n", keys[i].desc); |
| 468 | } |
| 469 | exit(1); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 470 | } |
| 471 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 472 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 473 | /* Create the certificates */ |
| 474 | for (i = 0 ; i < num_certs ; i++) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 475 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 476 | cert = &certs[i]; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 477 | |
Manish V Badarkhe | 2b7e70e | 2021-01-26 10:55:49 +0000 | [diff] [blame] | 478 | if (cert->fn == NULL) { |
| 479 | /* Certificate not requested. Skip to the next one */ |
| 480 | continue; |
| 481 | } |
| 482 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 483 | /* Create a new stack of extensions. This stack will be used |
| 484 | * to create the certificate */ |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 485 | CHECK_NULL(sk, sk_X509_EXTENSION_new_null()); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 486 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 487 | for (j = 0 ; j < cert->num_ext ; j++) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 488 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 489 | ext = &extensions[cert->ext[j]]; |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 490 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 491 | /* Get OpenSSL internal ID for this extension */ |
| 492 | CHECK_OID(ext_nid, ext->oid); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 493 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 494 | /* |
| 495 | * Three types of extensions are currently supported: |
| 496 | * - EXT_TYPE_NVCOUNTER |
| 497 | * - EXT_TYPE_HASH |
| 498 | * - EXT_TYPE_PKEY |
| 499 | */ |
| 500 | switch (ext->type) { |
| 501 | case EXT_TYPE_NVCOUNTER: |
Jimmy Brisson | c913fa6 | 2021-01-20 15:34:51 -0600 | [diff] [blame] | 502 | if (ext->optional && ext->arg == NULL) { |
| 503 | /* Skip this NVCounter */ |
| 504 | continue; |
| 505 | } else { |
| 506 | /* Checked by `check_cmd_params` */ |
| 507 | assert(ext->arg != NULL); |
Yatharth Kochar | 79f8640 | 2016-06-22 14:49:27 +0100 | [diff] [blame] | 508 | nvctr = atoi(ext->arg); |
| 509 | CHECK_NULL(cert_ext, ext_new_nvcounter(ext_nid, |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 510 | EXT_CRIT, nvctr)); |
Yatharth Kochar | 79f8640 | 2016-06-22 14:49:27 +0100 | [diff] [blame] | 511 | } |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 512 | break; |
| 513 | case EXT_TYPE_HASH: |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 514 | if (ext->arg == NULL) { |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 515 | if (ext->optional) { |
| 516 | /* Include a hash filled with zeros */ |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 517 | memset(md, 0x0, SHA512_DIGEST_LENGTH); |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 518 | } else { |
| 519 | /* Do not include this hash in the certificate */ |
Jimmy Brisson | c913fa6 | 2021-01-20 15:34:51 -0600 | [diff] [blame] | 520 | continue; |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 521 | } |
| 522 | } else { |
| 523 | /* Calculate the hash of the file */ |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 524 | if (!sha_file(hash_alg, ext->arg, md)) { |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 525 | ERROR("Cannot calculate hash of %s\n", |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 526 | ext->arg); |
Yatharth Kochar | 5752b59 | 2015-08-21 15:30:55 +0100 | [diff] [blame] | 527 | exit(1); |
| 528 | } |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 529 | } |
| 530 | CHECK_NULL(cert_ext, ext_new_hash(ext_nid, |
| 531 | EXT_CRIT, md_info, md, |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 532 | md_len)); |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 533 | break; |
| 534 | case EXT_TYPE_PKEY: |
| 535 | CHECK_NULL(cert_ext, ext_new_key(ext_nid, |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 536 | EXT_CRIT, keys[ext->attr.key].key)); |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 537 | break; |
| 538 | default: |
Juan Castillo | 4352998 | 2016-01-22 11:05:24 +0000 | [diff] [blame] | 539 | ERROR("Unknown extension type '%d' in %s\n", |
| 540 | ext->type, cert->cn); |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 541 | exit(1); |
| 542 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 543 | |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 544 | /* Push the extension into the stack */ |
| 545 | sk_X509_EXTENSION_push(sk, cert_ext); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 546 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 547 | |
Soby Mathew | 2fd70f6 | 2017-08-31 11:50:29 +0100 | [diff] [blame] | 548 | /* Create certificate. Signed with corresponding key */ |
Manish V Badarkhe | 2b7e70e | 2021-01-26 10:55:49 +0000 | [diff] [blame] | 549 | if (!cert_new(hash_alg, cert, VAL_DAYS, 0, sk)) { |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 550 | ERROR("Cannot create %s\n", cert->cn); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 551 | exit(1); |
| 552 | } |
| 553 | |
Jimmy Brisson | 63d0693 | 2020-07-24 14:31:48 -0500 | [diff] [blame] | 554 | for (cert_ext = sk_X509_EXTENSION_pop(sk); cert_ext != NULL; |
| 555 | cert_ext = sk_X509_EXTENSION_pop(sk)) { |
| 556 | X509_EXTENSION_free(cert_ext); |
| 557 | } |
| 558 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 559 | sk_X509_EXTENSION_free(sk); |
| 560 | } |
| 561 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 562 | |
| 563 | /* Print the certificates */ |
| 564 | if (print_cert) { |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 565 | for (i = 0 ; i < num_certs ; i++) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 566 | if (!certs[i].x) { |
| 567 | continue; |
| 568 | } |
| 569 | printf("\n\n=====================================\n\n"); |
| 570 | X509_print_fp(stdout, certs[i].x); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* Save created certificates to files */ |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 575 | for (i = 0 ; i < num_certs ; i++) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 576 | if (certs[i].x && certs[i].fn) { |
| 577 | file = fopen(certs[i].fn, "w"); |
| 578 | if (file != NULL) { |
| 579 | i2d_X509_fp(file, certs[i].x); |
| 580 | fclose(file); |
| 581 | } else { |
| 582 | ERROR("Cannot create file %s\n", certs[i].fn); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* Save keys */ |
| 588 | if (save_keys) { |
Juan Castillo | e6d30e9 | 2015-06-12 11:27:59 +0100 | [diff] [blame] | 589 | for (i = 0 ; i < num_keys ; i++) { |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 590 | if (!key_store(&keys[i])) { |
| 591 | ERROR("Cannot save %s\n", keys[i].desc); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Jimmy Brisson | 5328796 | 2020-07-27 10:43:40 -0500 | [diff] [blame] | 596 | /* If we got here, then we must have filled the key array completely. |
| 597 | * We can then safely call free on all of the keys in the array |
| 598 | */ |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 599 | key_cleanup(); |
Jimmy Brisson | 5328796 | 2020-07-27 10:43:40 -0500 | [diff] [blame] | 600 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 601 | #ifndef OPENSSL_NO_ENGINE |
| 602 | ENGINE_cleanup(); |
| 603 | #endif |
| 604 | CRYPTO_cleanup_all_ex_data(); |
| 605 | |
Jimmy Brisson | 54db69e | 2020-07-27 11:23:20 -0500 | [diff] [blame] | 606 | |
| 607 | /* We allocated strings through strdup, so now we have to free them */ |
Jimmy Brisson | 54db69e | 2020-07-27 11:23:20 -0500 | [diff] [blame] | 608 | |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 609 | ext_cleanup(); |
Jimmy Brisson | 54db69e | 2020-07-27 11:23:20 -0500 | [diff] [blame] | 610 | |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 611 | cert_cleanup(); |
Jimmy Brisson | 54db69e | 2020-07-27 11:23:20 -0500 | [diff] [blame] | 612 | |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 613 | return 0; |
| 614 | } |