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