Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate generation and signing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 9 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 10 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 12 | #endif |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 16 | #if !defined(MBEDTLS_X509_CRT_WRITE_C) || \ |
| 17 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 18 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 19 | !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \ |
| 20 | !defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 21 | int main(void) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 22 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 23 | mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or " |
| 24 | "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or " |
| 25 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 26 | "MBEDTLS_ERROR_C not defined.\n"); |
| 27 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 28 | } |
| 29 | #else |
| 30 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/x509_crt.h" |
| 32 | #include "mbedtls/x509_csr.h" |
| 33 | #include "mbedtls/entropy.h" |
| 34 | #include "mbedtls/ctr_drbg.h" |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 35 | #include "mbedtls/md.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 37 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 41 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | #define USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 44 | " request_file=%%s default: (empty)\n" \ |
| 45 | " If request_file is specified, subject_key,\n" \ |
| 46 | " subject_pwd and subject_name are ignored!\n" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 47 | #else |
| 48 | #define USAGE_CSR "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 50 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 51 | #define DFL_ISSUER_CRT "" |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 52 | #define DFL_REQUEST_FILE "" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 53 | #define DFL_SUBJECT_KEY "subject.key" |
| 54 | #define DFL_ISSUER_KEY "ca.key" |
| 55 | #define DFL_SUBJECT_PWD "" |
| 56 | #define DFL_ISSUER_PWD "" |
| 57 | #define DFL_OUTPUT_FILENAME "cert.crt" |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 58 | #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" |
| 59 | #define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 60 | #define DFL_NOT_BEFORE "20010101000000" |
| 61 | #define DFL_NOT_AFTER "20301231235959" |
| 62 | #define DFL_SERIAL "1" |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 63 | #define DFL_SELFSIGN 0 |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 64 | #define DFL_IS_CA 0 |
| 65 | #define DFL_MAX_PATHLEN -1 |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 66 | #define DFL_KEY_USAGE 0 |
| 67 | #define DFL_NS_CERT_TYPE 0 |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 68 | #define DFL_VERSION 3 |
| 69 | #define DFL_AUTH_IDENT 1 |
| 70 | #define DFL_SUBJ_IDENT 1 |
| 71 | #define DFL_CONSTRAINTS 1 |
| 72 | #define DFL_DIGEST MBEDTLS_MD_SHA256 |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 73 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 74 | #define USAGE \ |
| 75 | "\n usage: cert_write param=<>...\n" \ |
| 76 | "\n acceptable parameters:\n" \ |
| 77 | USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 78 | " subject_key=%%s default: subject.key\n" \ |
| 79 | " subject_pwd=%%s default: (empty)\n" \ |
| 80 | " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 81 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 82 | " issuer_crt=%%s default: (empty)\n" \ |
| 83 | " If issuer_crt is specified, issuer_name is\n" \ |
| 84 | " ignored!\n" \ |
| 85 | " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 86 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 87 | " selfsign=%%d default: 0 (false)\n" \ |
| 88 | " If selfsign is enabled, issuer_name and\n" \ |
| 89 | " issuer_key are required (issuer_crt and\n" \ |
| 90 | " subject_* are ignored\n" \ |
| 91 | " issuer_key=%%s default: ca.key\n" \ |
| 92 | " issuer_pwd=%%s default: (empty)\n" \ |
| 93 | " output_file=%%s default: cert.crt\n" \ |
| 94 | " serial=%%s default: 1\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | " not_before=%%s default: 20010101000000\n" \ |
| 96 | " not_after=%%s default: 20301231235959\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 97 | " is_ca=%%d default: 0 (disabled)\n" \ |
| 98 | " max_pathlen=%%d default: -1 (none)\n" \ |
| 99 | " md=%%s default: SHA256\n" \ |
Gilles Peskine | 18292fe | 2020-08-21 20:42:32 +0200 | [diff] [blame] | 100 | " Supported values (if enabled):\n" \ |
| 101 | " MD2, MD4, MD5, RIPEMD160, SHA1,\n" \ |
| 102 | " SHA224, SHA256, SHA384, SHA512\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 103 | " version=%%d default: 3\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | " Possible values: 1, 2, 3\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 105 | " subject_identifier=%%s default: 1\n" \ |
| 106 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 108 | " authority_identifier=%%s default: 1\n" \ |
| 109 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 111 | " basic_constraints=%%d default: 1\n" \ |
| 112 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 114 | " key_usage=%%s default: (empty)\n" \ |
| 115 | " Comma-separated-list of values:\n" \ |
| 116 | " digital_signature\n" \ |
| 117 | " non_repudiation\n" \ |
| 118 | " key_encipherment\n" \ |
| 119 | " data_encipherment\n" \ |
| 120 | " key_agreement\n" \ |
| 121 | " key_cert_sign\n" \ |
| 122 | " crl_sign\n" \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 124 | " ns_cert_type=%%s default: (empty)\n" \ |
| 125 | " Comma-separated-list of values:\n" \ |
| 126 | " ssl_client\n" \ |
| 127 | " ssl_server\n" \ |
| 128 | " email\n" \ |
| 129 | " object_signing\n" \ |
| 130 | " ssl_ca\n" \ |
| 131 | " email_ca\n" \ |
| 132 | " object_signing_ca\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 133 | "\n" |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 134 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 135 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 136 | /* |
| 137 | * global options |
| 138 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | struct options { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 140 | const char *issuer_crt; /* filename of the issuer certificate */ |
| 141 | const char *request_file; /* filename of the certificate request */ |
| 142 | const char *subject_key; /* filename of the subject key file */ |
| 143 | const char *issuer_key; /* filename of the issuer key file */ |
| 144 | const char *subject_pwd; /* password for the subject key file */ |
| 145 | const char *issuer_pwd; /* password for the issuer key file */ |
Hanno Becker | 25d882b | 2018-08-23 15:26:06 +0100 | [diff] [blame] | 146 | const char *output_file; /* where to store the constructed CRT */ |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 147 | const char *subject_name; /* subject name for certificate */ |
| 148 | const char *issuer_name; /* issuer name for certificate */ |
| 149 | const char *not_before; /* validity period not before */ |
| 150 | const char *not_after; /* validity period not after */ |
| 151 | const char *serial; /* serial number string */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 152 | int selfsign; /* selfsign the certificate */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 153 | int is_ca; /* is a CA certificate */ |
| 154 | int max_pathlen; /* maximum CA path length */ |
Hanno Becker | e1b1d0a | 2017-09-22 15:35:16 +0100 | [diff] [blame] | 155 | int authority_identifier; /* add authority identifier to CRT */ |
| 156 | int subject_identifier; /* add subject identifier to CRT */ |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 157 | int basic_constraints; /* add basic constraints ext to CRT */ |
| 158 | int version; /* CRT version */ |
| 159 | mbedtls_md_type_t md; /* Hash used for signing */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 160 | unsigned char key_usage; /* key usage flags */ |
| 161 | unsigned char ns_cert_type; /* NS cert type */ |
| 162 | } opt; |
| 163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | int write_certificate(mbedtls_x509write_cert *crt, const char *output_file, |
| 165 | int (*f_rng)(void *, unsigned char *, size_t), |
| 166 | void *p_rng) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 167 | { |
| 168 | int ret; |
| 169 | FILE *f; |
| 170 | unsigned char output_buf[4096]; |
| 171 | size_t len = 0; |
| 172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | memset(output_buf, 0, 4096); |
| 174 | if ((ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096, |
| 175 | f_rng, p_rng)) < 0) { |
| 176 | return ret; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 177 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | len = strlen((char *) output_buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | if ((f = fopen(output_file, "w")) == NULL) { |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | if (fwrite(output_buf, 1, len, f) != len) { |
| 186 | fclose(f); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | fclose(f); |
| 191 | |
| 192 | return 0; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 195 | int main(int argc, char *argv[]) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 196 | { |
Andres Amaya Garcia | f9a54d3 | 2018-04-29 21:42:45 +0100 | [diff] [blame] | 197 | int ret = 1; |
| 198 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_x509_crt issuer_crt; |
| 200 | mbedtls_pk_context loaded_issuer_key, loaded_subject_key; |
| 201 | mbedtls_pk_context *issuer_key = &loaded_issuer_key, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | *subject_key = &loaded_subject_key; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 203 | char buf[1024]; |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 204 | char issuer_name[256]; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 205 | int i; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 206 | char *p, *q, *r; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 208 | char subject_name[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_x509_csr csr; |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 210 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_x509write_cert crt; |
| 212 | mbedtls_mpi serial; |
| 213 | mbedtls_entropy_context entropy; |
| 214 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 215 | const char *pers = "crt example app"; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Set to sane values |
| 219 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | mbedtls_x509write_crt_init(&crt); |
| 221 | mbedtls_pk_init(&loaded_issuer_key); |
| 222 | mbedtls_pk_init(&loaded_subject_key); |
| 223 | mbedtls_mpi_init(&serial); |
| 224 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 225 | mbedtls_entropy_init(&entropy); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | mbedtls_x509_csr_init(&csr); |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 228 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 229 | mbedtls_x509_crt_init(&issuer_crt); |
| 230 | memset(buf, 0, 1024); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 231 | |
Przemek Stekiel | b00688f | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 232 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 233 | psa_status_t status = psa_crypto_init(); |
| 234 | if (status != PSA_SUCCESS) { |
| 235 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 236 | (int) status); |
| 237 | goto exit; |
| 238 | } |
| 239 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 240 | |
Aditya Deshpande | 0504ac2 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 241 | if (argc < 2) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | usage: |
| 243 | mbedtls_printf(USAGE); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 244 | goto exit; |
| 245 | } |
| 246 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 247 | opt.issuer_crt = DFL_ISSUER_CRT; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 248 | opt.request_file = DFL_REQUEST_FILE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 249 | opt.subject_key = DFL_SUBJECT_KEY; |
| 250 | opt.issuer_key = DFL_ISSUER_KEY; |
| 251 | opt.subject_pwd = DFL_SUBJECT_PWD; |
| 252 | opt.issuer_pwd = DFL_ISSUER_PWD; |
| 253 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 254 | opt.subject_name = DFL_SUBJECT_NAME; |
| 255 | opt.issuer_name = DFL_ISSUER_NAME; |
| 256 | opt.not_before = DFL_NOT_BEFORE; |
| 257 | opt.not_after = DFL_NOT_AFTER; |
| 258 | opt.serial = DFL_SERIAL; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 259 | opt.selfsign = DFL_SELFSIGN; |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 260 | opt.is_ca = DFL_IS_CA; |
| 261 | opt.max_pathlen = DFL_MAX_PATHLEN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 262 | opt.key_usage = DFL_KEY_USAGE; |
| 263 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame] | 264 | opt.version = DFL_VERSION - 1; |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 265 | opt.md = DFL_DIGEST; |
| 266 | opt.subject_identifier = DFL_SUBJ_IDENT; |
| 267 | opt.authority_identifier = DFL_AUTH_IDENT; |
| 268 | opt.basic_constraints = DFL_CONSTRAINTS; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | for (i = 1; i < argc; i++) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 271 | |
| 272 | p = argv[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | if ((q = strchr(p, '=')) == NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 274 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 276 | *q++ = '\0'; |
| 277 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | if (strcmp(p, "request_file") == 0) { |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 279 | opt.request_file = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | } else if (strcmp(p, "subject_key") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 281 | opt.subject_key = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | } else if (strcmp(p, "issuer_key") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 283 | opt.issuer_key = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | } else if (strcmp(p, "subject_pwd") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 285 | opt.subject_pwd = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 286 | } else if (strcmp(p, "issuer_pwd") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 287 | opt.issuer_pwd = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 288 | } else if (strcmp(p, "issuer_crt") == 0) { |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 289 | opt.issuer_crt = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | } else if (strcmp(p, "output_file") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 291 | opt.output_file = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | } else if (strcmp(p, "subject_name") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 293 | opt.subject_name = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | } else if (strcmp(p, "issuer_name") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 295 | opt.issuer_name = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 296 | } else if (strcmp(p, "not_before") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 297 | opt.not_before = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | } else if (strcmp(p, "not_after") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 299 | opt.not_after = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | } else if (strcmp(p, "serial") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 301 | opt.serial = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 302 | } else if (strcmp(p, "authority_identifier") == 0) { |
| 303 | opt.authority_identifier = atoi(q); |
| 304 | if (opt.authority_identifier != 0 && |
| 305 | opt.authority_identifier != 1) { |
| 306 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 307 | goto usage; |
| 308 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 309 | } else if (strcmp(p, "subject_identifier") == 0) { |
| 310 | opt.subject_identifier = atoi(q); |
| 311 | if (opt.subject_identifier != 0 && |
| 312 | opt.subject_identifier != 1) { |
| 313 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 314 | goto usage; |
| 315 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | } else if (strcmp(p, "basic_constraints") == 0) { |
| 317 | opt.basic_constraints = atoi(q); |
| 318 | if (opt.basic_constraints != 0 && |
| 319 | opt.basic_constraints != 1) { |
| 320 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 321 | goto usage; |
| 322 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | } else if (strcmp(p, "md") == 0) { |
Gilles Peskine | 18292fe | 2020-08-21 20:42:32 +0200 | [diff] [blame] | 324 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | mbedtls_md_info_from_string(q); |
| 326 | if (md_info == NULL) { |
| 327 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 328 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 329 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | opt.md = mbedtls_md_get_type(md_info); |
| 331 | } else if (strcmp(p, "version") == 0) { |
| 332 | opt.version = atoi(q); |
| 333 | if (opt.version < 1 || opt.version > 3) { |
| 334 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 335 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 336 | } |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame] | 337 | opt.version--; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 338 | } else if (strcmp(p, "selfsign") == 0) { |
| 339 | opt.selfsign = atoi(q); |
| 340 | if (opt.selfsign < 0 || opt.selfsign > 1) { |
| 341 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 342 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 343 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | } else if (strcmp(p, "is_ca") == 0) { |
| 345 | opt.is_ca = atoi(q); |
| 346 | if (opt.is_ca < 0 || opt.is_ca > 1) { |
| 347 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 348 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 349 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 350 | } else if (strcmp(p, "max_pathlen") == 0) { |
| 351 | opt.max_pathlen = atoi(q); |
| 352 | if (opt.max_pathlen < -1 || opt.max_pathlen > 127) { |
| 353 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 354 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 355 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | } else if (strcmp(p, "key_usage") == 0) { |
| 357 | while (q != NULL) { |
| 358 | if ((r = strchr(q, ',')) != NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 359 | *r++ = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 362 | if (strcmp(q, "digital_signature") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | } else if (strcmp(q, "non_repudiation") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 366 | } else if (strcmp(q, "key_encipherment") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 367 | opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | } else if (strcmp(q, "data_encipherment") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 369 | opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 370 | } else if (strcmp(q, "key_agreement") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 371 | opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | } else if (strcmp(q, "key_cert_sign") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | } else if (strcmp(q, "crl_sign") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | } else { |
| 377 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 378 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 379 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 380 | |
| 381 | q = r; |
| 382 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | } else if (strcmp(p, "ns_cert_type") == 0) { |
| 384 | while (q != NULL) { |
| 385 | if ((r = strchr(q, ',')) != NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 386 | *r++ = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | if (strcmp(q, "ssl_client") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | } else if (strcmp(q, "ssl_server") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 392 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | } else if (strcmp(q, "email") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | } else if (strcmp(q, "object_signing") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | } else if (strcmp(q, "ssl_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 398 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | } else if (strcmp(q, "email_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 400 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 401 | } else if (strcmp(q, "object_signing_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 402 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | } else { |
| 404 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 405 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 406 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 407 | |
| 408 | q = r; |
| 409 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | } else { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 411 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_printf("\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 416 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 417 | /* |
| 418 | * 0. Seed the PRNG |
| 419 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 420 | mbedtls_printf(" . Seeding the random number generator..."); |
| 421 | fflush(stdout); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 424 | (const unsigned char *) pers, |
| 425 | strlen(pers))) != 0) { |
| 426 | mbedtls_strerror(ret, buf, 1024); |
| 427 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", |
| 428 | ret, buf); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 429 | goto exit; |
| 430 | } |
| 431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 433 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 434 | // Parse serial to MPI |
| 435 | // |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | mbedtls_printf(" . Reading serial number..."); |
| 437 | fflush(stdout); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 438 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) { |
| 440 | mbedtls_strerror(ret, buf, 1024); |
| 441 | mbedtls_printf(" failed\n ! mbedtls_mpi_read_string " |
| 442 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
| 445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 447 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 448 | // Parse issuer certificate if present |
| 449 | // |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | if (!opt.selfsign && strlen(opt.issuer_crt)) { |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 451 | /* |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 452 | * 1.0.a. Load the certificates |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 453 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | mbedtls_printf(" . Loading the issuer certificate ..."); |
| 455 | fflush(stdout); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) { |
| 458 | mbedtls_strerror(ret, buf, 1024); |
| 459 | mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file " |
| 460 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 461 | goto exit; |
| 462 | } |
| 463 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 464 | ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name), |
| 465 | &issuer_crt.subject); |
| 466 | if (ret < 0) { |
| 467 | mbedtls_strerror(ret, buf, 1024); |
| 468 | mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets " |
| 469 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 470 | goto exit; |
| 471 | } |
| 472 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 473 | opt.issuer_name = issuer_name; |
| 474 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | mbedtls_printf(" ok\n"); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 479 | // Parse certificate request if present |
| 480 | // |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 481 | if (!opt.selfsign && strlen(opt.request_file)) { |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 482 | /* |
| 483 | * 1.0.b. Load the CSR |
| 484 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 485 | mbedtls_printf(" . Loading the certificate request ..."); |
| 486 | fflush(stdout); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 488 | if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) { |
| 489 | mbedtls_strerror(ret, buf, 1024); |
| 490 | mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file " |
| 491 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 492 | goto exit; |
| 493 | } |
| 494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 495 | ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name), |
| 496 | &csr.subject); |
| 497 | if (ret < 0) { |
| 498 | mbedtls_strerror(ret, buf, 1024); |
| 499 | mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets " |
| 500 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 501 | goto exit; |
| 502 | } |
| 503 | |
| 504 | opt.subject_name = subject_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 505 | subject_key = &csr.pk; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 507 | mbedtls_printf(" ok\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 508 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * 1.1. Load the keys |
| 513 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 514 | if (!opt.selfsign && !strlen(opt.request_file)) { |
| 515 | mbedtls_printf(" . Loading the subject key ..."); |
| 516 | fflush(stdout); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 517 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 518 | ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key, |
| 519 | opt.subject_pwd); |
| 520 | if (ret != 0) { |
| 521 | mbedtls_strerror(ret, buf, 1024); |
| 522 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile " |
| 523 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 524 | goto exit; |
| 525 | } |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 526 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 527 | mbedtls_printf(" ok\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 530 | mbedtls_printf(" . Loading the issuer key ..."); |
| 531 | fflush(stdout); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 532 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 533 | ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key, |
| 534 | opt.issuer_pwd); |
| 535 | if (ret != 0) { |
| 536 | mbedtls_strerror(ret, buf, 1024); |
| 537 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile " |
| 538 | "returned -x%02x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 539 | goto exit; |
| 540 | } |
| 541 | |
| 542 | // Check if key and issuer certificate match |
| 543 | // |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 544 | if (strlen(opt.issuer_crt)) { |
| 545 | if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key) != 0) { |
| 546 | mbedtls_printf(" failed\n ! issuer_key does not match " |
| 547 | "issuer certificate\n\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 548 | goto exit; |
| 549 | } |
| 550 | } |
| 551 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 552 | mbedtls_printf(" ok\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 554 | if (opt.selfsign) { |
Paul Bakker | 93c6aa4 | 2013-10-28 22:28:09 +0100 | [diff] [blame] | 555 | opt.subject_name = opt.issuer_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 556 | subject_key = issuer_key; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | mbedtls_x509write_crt_set_subject_key(&crt, subject_key); |
| 560 | mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 561 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 562 | /* |
| 563 | * 1.0. Check the names for validity |
| 564 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 565 | if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) { |
| 566 | mbedtls_strerror(ret, buf, 1024); |
| 567 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name " |
| 568 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 569 | goto exit; |
| 570 | } |
| 571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 572 | if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) { |
| 573 | mbedtls_strerror(ret, buf, 1024); |
| 574 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name " |
| 575 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 576 | goto exit; |
| 577 | } |
| 578 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 579 | mbedtls_printf(" . Setting certificate values ..."); |
| 580 | fflush(stdout); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 581 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | mbedtls_x509write_crt_set_version(&crt, opt.version); |
| 583 | mbedtls_x509write_crt_set_md_alg(&crt, opt.md); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 585 | ret = mbedtls_x509write_crt_set_serial(&crt, &serial); |
| 586 | if (ret != 0) { |
| 587 | mbedtls_strerror(ret, buf, 1024); |
| 588 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial " |
| 589 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 590 | goto exit; |
| 591 | } |
| 592 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 593 | ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after); |
| 594 | if (ret != 0) { |
| 595 | mbedtls_strerror(ret, buf, 1024); |
| 596 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity " |
| 597 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 598 | goto exit; |
| 599 | } |
| 600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 601 | mbedtls_printf(" ok\n"); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 604 | opt.basic_constraints != 0) { |
| 605 | mbedtls_printf(" . Adding the Basic Constraints extension ..."); |
| 606 | fflush(stdout); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 607 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 608 | ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca, |
| 609 | opt.max_pathlen); |
| 610 | if (ret != 0) { |
| 611 | mbedtls_strerror(ret, buf, 1024); |
| 612 | mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints " |
| 613 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 614 | goto exit; |
| 615 | } |
| 616 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 617 | mbedtls_printf(" ok\n"); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 618 | } |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | #if defined(MBEDTLS_SHA1_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 622 | opt.subject_identifier != 0) { |
| 623 | mbedtls_printf(" . Adding the Subject Key Identifier ..."); |
| 624 | fflush(stdout); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 625 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 626 | ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt); |
| 627 | if (ret != 0) { |
| 628 | mbedtls_strerror(ret, buf, 1024); |
| 629 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject" |
| 630 | "_key_identifier returned -0x%04x - %s\n\n", |
| 631 | (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 632 | goto exit; |
| 633 | } |
| 634 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 635 | mbedtls_printf(" ok\n"); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 638 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 639 | opt.authority_identifier != 0) { |
| 640 | mbedtls_printf(" . Adding the Authority Key Identifier ..."); |
| 641 | fflush(stdout); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 643 | ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt); |
| 644 | if (ret != 0) { |
| 645 | mbedtls_strerror(ret, buf, 1024); |
| 646 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_" |
| 647 | "key_identifier returned -0x%04x - %s\n\n", |
| 648 | (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 649 | goto exit; |
| 650 | } |
| 651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 652 | mbedtls_printf(" ok\n"); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 656 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 657 | opt.key_usage != 0) { |
| 658 | mbedtls_printf(" . Adding the Key Usage extension ..."); |
| 659 | fflush(stdout); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 660 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 661 | ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage); |
| 662 | if (ret != 0) { |
| 663 | mbedtls_strerror(ret, buf, 1024); |
| 664 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage " |
| 665 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 666 | goto exit; |
| 667 | } |
| 668 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 669 | mbedtls_printf(" ok\n"); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 672 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 673 | opt.ns_cert_type != 0) { |
| 674 | mbedtls_printf(" . Adding the NS Cert Type extension ..."); |
| 675 | fflush(stdout); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 676 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 677 | ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type); |
| 678 | if (ret != 0) { |
| 679 | mbedtls_strerror(ret, buf, 1024); |
| 680 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type " |
| 681 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 682 | goto exit; |
| 683 | } |
| 684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 685 | mbedtls_printf(" ok\n"); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 688 | /* |
Hanno Becker | 25d882b | 2018-08-23 15:26:06 +0100 | [diff] [blame] | 689 | * 1.2. Writing the certificate |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 690 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 691 | mbedtls_printf(" . Writing the certificate..."); |
| 692 | fflush(stdout); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 693 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 694 | if ((ret = write_certificate(&crt, opt.output_file, |
| 695 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 696 | mbedtls_strerror(ret, buf, 1024); |
| 697 | mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n", |
| 698 | (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 699 | goto exit; |
| 700 | } |
| 701 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 702 | mbedtls_printf(" ok\n"); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 703 | |
Andres Amaya Garcia | f9a54d3 | 2018-04-29 21:42:45 +0100 | [diff] [blame] | 704 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 705 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 706 | exit: |
Hanno Becker | 30a9510 | 2018-10-05 09:49:33 +0100 | [diff] [blame] | 707 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 708 | mbedtls_x509_csr_free(&csr); |
Hanno Becker | 30a9510 | 2018-10-05 09:49:33 +0100 | [diff] [blame] | 709 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 710 | mbedtls_x509_crt_free(&issuer_crt); |
| 711 | mbedtls_x509write_crt_free(&crt); |
| 712 | mbedtls_pk_free(&loaded_subject_key); |
| 713 | mbedtls_pk_free(&loaded_issuer_key); |
| 714 | mbedtls_mpi_free(&serial); |
| 715 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 716 | mbedtls_entropy_free(&entropy); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 717 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | c4ddf92 | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 718 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 719 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 720 | |
| 721 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 722 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 723 | fflush(stdout); getchar(); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 724 | #endif |
| 725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 726 | mbedtls_exit(exit_code); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 727 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && |
| 729 | MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 730 | MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */ |