blob: 5993f24657a1716be6a620e47ef2f8dfd1d8b190 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker9397dcb2013-09-06 09:55:26 +02006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040013/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040014#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Simon Butcher203a6932016-10-07 15:00:17 +010016#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) || \
Elena Uziunaite0916cd72024-05-23 17:01:07 +010019 !defined(MBEDTLS_ERROR_C) || !defined(PSA_WANT_ALG_SHA_256) || \
Valerio Settie3511762024-01-22 16:28:23 +010020 !defined(MBEDTLS_PEM_WRITE_C) || !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010021int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020022{
Gilles Peskine449bd832023-01-11 14:50:10 +010023 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Elena Uziunaite0916cd72024-05-23 17:01:07 +010024 "MBEDTLS_FS_IO and/or PSA_WANT_ALG_SHA_256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010025 "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é-Gonnard8d649c62015-03-31 15:10:03 +020028}
29#else
30
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/x509_crt.h"
32#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010033#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/entropy.h"
35#include "mbedtls/ctr_drbg.h"
36#include "mbedtls/error.h"
Valerio Setti791bbe62023-01-11 10:40:18 +010037#include "test/helpers.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020038
Rich Evans18b78c72015-02-11 14:06:19 +000039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
Valerio Setti791bbe62023-01-11 10:40:18 +010042#include <errno.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010044#define SET_OID(x, oid) \
Gilles Peskine449bd832023-01-11 14:50:10 +010045 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000048#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010049 " request_file=%%s default: (empty)\n" \
50 " If request_file is specified, subject_key,\n" \
51 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000052#else
53#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000055
Dave Rodgman66e05502022-10-27 16:29:38 +010056#define FORMAT_PEM 0
57#define FORMAT_DER 1
58
Paul Bakker1014e952013-09-09 13:59:42 +020059#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020060#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020061#define DFL_SUBJECT_KEY "subject.key"
62#define DFL_ISSUER_KEY "ca.key"
63#define DFL_SUBJECT_PWD ""
64#define DFL_ISSUER_PWD ""
65#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000066#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
67#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020068#define DFL_NOT_BEFORE "20010101000000"
69#define DFL_NOT_AFTER "20301231235959"
70#define DFL_SERIAL "1"
Valerio Setti791bbe62023-01-11 10:40:18 +010071#define DFL_SERIAL_HEX "1"
Andrzej Kurekccdd9752023-04-01 10:38:30 -040072#define DFL_EXT_SUBJECTALTNAME ""
Paul Bakkerb2d7f232013-09-09 16:24:18 +020073#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020074#define DFL_IS_CA 0
75#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010076#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020077#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010078#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020079#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010080#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
Dave Rodgman66e05502022-10-27 16:29:38 +010085#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020086
Rich Evans18b78c72015-02-11 14:06:19 +000087#define USAGE \
88 "\n usage: cert_write param=<>...\n" \
89 "\n acceptable parameters:\n" \
90 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010091 " subject_key=%%s default: subject.key\n" \
92 " subject_pwd=%%s default: (empty)\n" \
93 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000094 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010095 " issuer_crt=%%s default: (empty)\n" \
96 " If issuer_crt is specified, issuer_name is\n" \
97 " ignored!\n" \
98 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000099 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100100 " selfsign=%%d default: 0 (false)\n" \
101 " If selfsign is enabled, issuer_name and\n" \
102 " issuer_key are required (issuer_crt and\n" \
103 " subject_* are ignored\n" \
104 " issuer_key=%%s default: ca.key\n" \
105 " issuer_pwd=%%s default: (empty)\n" \
106 " output_file=%%s default: cert.crt\n" \
107 " serial=%%s default: 1\n" \
Valerio Setti791bbe62023-01-11 10:40:18 +0100108 " In decimal format; it can be used as\n" \
109 " alternative to serial_hex, but it's\n" \
110 " limited in max length to\n" \
111 " unsigned long long int\n" \
112 " serial_hex=%%s default: 1\n" \
113 " In hex format; it can be used as\n" \
114 " alternative to serial\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 " not_before=%%s default: 20010101000000\n" \
116 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100117 " is_ca=%%d default: 0 (disabled)\n" \
118 " max_pathlen=%%d default: -1 (none)\n" \
119 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200120 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200121 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200122 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100123 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100125 " subject_identifier=%%s default: 1\n" \
126 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 " (Considered for v3 only)\n" \
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400128 " san=%%s default: (none)\n" \
Andrzej Kurekf70f4602023-04-24 18:39:53 -0400129 " Semicolon-separated-list of values:\n" \
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400130 " DNS:value\n" \
131 " URI:value\n" \
132 " RFC822:value\n" \
133 " IP:value (Only IPv4 is supported)\n" \
Andrzej Kurek446e53d2023-04-25 02:21:07 -0400134 " DN:list of comma separated key=value pairs\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100135 " authority_identifier=%%s default: 1\n" \
136 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100138 " basic_constraints=%%d default: 1\n" \
139 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100141 " key_usage=%%s default: (empty)\n" \
142 " Comma-separated-list of values:\n" \
143 " digital_signature\n" \
144 " non_repudiation\n" \
145 " key_encipherment\n" \
146 " data_encipherment\n" \
147 " key_agreement\n" \
148 " key_cert_sign\n" \
149 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100151 " ext_key_usage=%%s default: (empty)\n" \
152 " Comma-separated-list of values:\n" \
153 " serverAuth\n" \
154 " clientAuth\n" \
155 " codeSigning\n" \
156 " emailProtection\n" \
157 " timeStamping\n" \
158 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100159 " ns_cert_type=%%s default: (empty)\n" \
160 " Comma-separated-list of values:\n" \
161 " ssl_client\n" \
162 " ssl_server\n" \
163 " email\n" \
164 " object_signing\n" \
165 " ssl_ca\n" \
166 " email_ca\n" \
167 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100168 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000169 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000170
Valerio Setti791bbe62023-01-11 10:40:18 +0100171typedef enum {
172 SERIAL_FRMT_UNSPEC,
173 SERIAL_FRMT_DEC,
174 SERIAL_FRMT_HEX
175} serial_format_t;
Simon Butcher63cb97e2018-12-06 17:43:31 +0000176
Paul Bakker9397dcb2013-09-06 09:55:26 +0200177/*
178 * global options
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100181 const char *issuer_crt; /* filename of the issuer certificate */
182 const char *request_file; /* filename of the certificate request */
183 const char *subject_key; /* filename of the subject key file */
184 const char *issuer_key; /* filename of the issuer key file */
185 const char *subject_pwd; /* password for the subject key file */
186 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100187 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100188 const char *subject_name; /* subject name for certificate */
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400189 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100190 const char *issuer_name; /* issuer name for certificate */
191 const char *not_before; /* validity period not before */
192 const char *not_after; /* validity period not after */
Valerio Setti791bbe62023-01-11 10:40:18 +0100193 const char *serial; /* serial number string (decimal) */
194 const char *serial_hex; /* serial number string (hex) */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200195 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200196 int is_ca; /* is a CA certificate */
197 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100198 int authority_identifier; /* add authority identifier to CRT */
199 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100200 int basic_constraints; /* add basic constraints ext to CRT */
201 int version; /* CRT version */
202 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100204 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100206 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207} opt;
208
Michael Schuster8db8d612024-06-01 21:15:02 +0200209static int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
Michael Schuster04200932024-06-12 00:05:25 +0200210 int (*f_rng)(void *, unsigned char *, size_t),
211 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212{
213 int ret;
214 FILE *f;
215 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100216 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217 size_t len = 0;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 memset(output_buf, 0, 4096);
220 if (opt.format == FORMAT_DER) {
221 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
222 f_rng, p_rng);
223 if (ret < 0) {
224 return ret;
225 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200226
Dave Rodgman66e05502022-10-27 16:29:38 +0100227 len = ret;
228 output_start = output_buf + 4096 - len;
229 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
231 f_rng, p_rng);
232 if (ret < 0) {
233 return ret;
234 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100237 output_start = output_buf;
238 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if ((f = fopen(output_file, "w")) == NULL) {
241 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200242 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (fwrite(output_start, 1, len, f) != len) {
245 fclose(f);
246 return -1;
247 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 fclose(f);
250
251 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252}
253
Michael Schuster8db8d612024-06-01 21:15:02 +0200254static int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
Michael Schuster04200932024-06-12 00:05:25 +0200255 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100256{
Valerio Setti791bbe62023-01-11 10:40:18 +0100257 unsigned long long int dec;
258 unsigned int remaining_bytes = sizeof(dec);
259 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100260 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100261 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100262
Valerio Setti791bbe62023-01-11 10:40:18 +0100263 errno = 0;
264 dec = strtoull(ibuf, &end_ptr, 10);
265
266 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100267 return -1;
268 }
269
Valerio Setti791bbe62023-01-11 10:40:18 +0100270 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100271
Valerio Setti791bbe62023-01-11 10:40:18 +0100272 while (remaining_bytes > 0) {
273 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100274 return -1;
275 }
276
Valerio Setti791bbe62023-01-11 10:40:18 +0100277 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
278
279 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100280 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100281 *p = val;
282 (*len)++;
283 p++;
284 }
285
286 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100287 }
288
289 return 0;
290}
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100294 int ret = 1;
295 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_x509_crt issuer_crt;
297 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
298 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200300 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200301 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100302 int i;
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400303 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200305 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100309 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100310 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
311 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100312 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_entropy_context entropy;
314 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200315 const char *pers = "crt example app";
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400316 mbedtls_x509_san_list *cur, *prev;
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400317 mbedtls_asn1_named_data *ext_san_dirname = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400318 uint8_t ip[4] = { 0 };
Paul Bakker9397dcb2013-09-06 09:55:26 +0200319 /*
320 * Set to sane values
321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_x509write_crt_init(&crt);
323 mbedtls_pk_init(&loaded_issuer_key);
324 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_ctr_drbg_init(&ctr_drbg);
326 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200329#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_x509_crt_init(&issuer_crt);
331 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100332 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200333
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200334#if defined(MBEDTLS_USE_PSA_CRYPTO)
335 psa_status_t status = psa_crypto_init();
336 if (status != PSA_SUCCESS) {
337 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
338 (int) status);
339 goto exit;
340 }
341#endif /* MBEDTLS_USE_PSA_CRYPTO */
342
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000343 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100344usage:
345 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200346 goto exit;
347 }
348
Paul Bakker1014e952013-09-09 13:59:42 +0200349 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200350 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200351 opt.subject_key = DFL_SUBJECT_KEY;
352 opt.issuer_key = DFL_ISSUER_KEY;
353 opt.subject_pwd = DFL_SUBJECT_PWD;
354 opt.issuer_pwd = DFL_ISSUER_PWD;
355 opt.output_file = DFL_OUTPUT_FILENAME;
356 opt.subject_name = DFL_SUBJECT_NAME;
357 opt.issuer_name = DFL_ISSUER_NAME;
358 opt.not_before = DFL_NOT_BEFORE;
359 opt.not_after = DFL_NOT_AFTER;
360 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100361 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200362 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200363 opt.is_ca = DFL_IS_CA;
364 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200365 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100366 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200367 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100368 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100369 opt.md = DFL_DIGEST;
370 opt.subject_identifier = DFL_SUBJ_IDENT;
371 opt.authority_identifier = DFL_AUTH_IDENT;
372 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100373 opt.format = DFL_FORMAT;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400374 opt.san_list = NULL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377
378 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200380 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 *q++ = '\0';
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200385 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200393 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200395 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200397 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200401 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200403 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200405 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100407 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
408 mbedtls_printf("Invalid attempt to set the serial more than once\n");
409 goto usage;
410 }
411 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200412 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100413 } else if (strcmp(p, "serial_hex") == 0) {
414 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
415 mbedtls_printf("Invalid attempt to set the serial more than once\n");
416 goto usage;
417 }
418 serial_frmt = SERIAL_FRMT_HEX;
419 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 } else if (strcmp(p, "authority_identifier") == 0) {
421 opt.authority_identifier = atoi(q);
422 if (opt.authority_identifier != 0 &&
423 opt.authority_identifier != 1) {
424 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100425 goto usage;
426 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 } else if (strcmp(p, "subject_identifier") == 0) {
428 opt.subject_identifier = atoi(q);
429 if (opt.subject_identifier != 0 &&
430 opt.subject_identifier != 1) {
431 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100432 goto usage;
433 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 } else if (strcmp(p, "basic_constraints") == 0) {
435 opt.basic_constraints = atoi(q);
436 if (opt.basic_constraints != 0 &&
437 opt.basic_constraints != 1) {
438 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100439 goto usage;
440 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200442 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_md_info_from_string(q);
444 if (md_info == NULL) {
445 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100446 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100447 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 opt.md = mbedtls_md_get_type(md_info);
449 } else if (strcmp(p, "version") == 0) {
450 opt.version = atoi(q);
451 if (opt.version < 1 || opt.version > 3) {
452 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100453 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100454 }
Hanno Becker38eff432017-09-22 15:38:20 +0100455 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (strcmp(p, "selfsign") == 0) {
457 opt.selfsign = atoi(q);
458 if (opt.selfsign < 0 || opt.selfsign > 1) {
459 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200460 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100461 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else if (strcmp(p, "is_ca") == 0) {
463 opt.is_ca = atoi(q);
464 if (opt.is_ca < 0 || opt.is_ca > 1) {
465 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200466 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100467 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else if (strcmp(p, "max_pathlen") == 0) {
469 opt.max_pathlen = atoi(q);
470 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
471 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200472 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100473 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 } else if (strcmp(p, "key_usage") == 0) {
475 while (q != NULL) {
476 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200477 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100485 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100487 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100489 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 } else {
495 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200496 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100497 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200498
499 q = r;
500 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100502 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 while (q != NULL) {
505 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100506 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100510 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (strcmp(q, "serverAuth") == 0) {
512 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
513 } else if (strcmp(q, "clientAuth") == 0) {
514 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
515 } else if (strcmp(q, "codeSigning") == 0) {
516 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
517 } else if (strcmp(q, "emailProtection") == 0) {
518 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
519 } else if (strcmp(q, "timeStamping") == 0) {
520 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
521 } else if (strcmp(q, "OCSPSigning") == 0) {
522 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
Pengyu Lvb0786072023-05-18 17:18:36 +0800523 } else if (strcmp(q, "any") == 0) {
524 SET_OID(ext_key_usage->buf, MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 } else {
526 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100527 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100528 }
Dave Rodgman64937852022-08-15 14:12:25 +0100529
530 *tail = ext_key_usage;
531 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100532
533 q = r;
534 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400535 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400536 char *subtype_value;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400537 prev = NULL;
538
539 while (q != NULL) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400540 char *semicolon;
541 r = q;
542
543 /* Find the first non-escaped ; occurrence and remove escaped ones */
544 do {
545 if ((semicolon = strchr(r, ';')) != NULL) {
546 if (*(semicolon-1) != '\\') {
547 r = semicolon;
548 break;
549 }
550 /* Remove the escape character */
551 size_t size_left = strlen(semicolon);
552 memmove(semicolon-1, semicolon, size_left);
553 *(semicolon + size_left - 1) = '\0';
554 /* r will now point at the character after the semicolon */
555 r = semicolon;
556 }
557
558 } while (semicolon != NULL);
559
560 if (semicolon != NULL) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400561 *r++ = '\0';
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400562 } else {
563 r = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400564 }
565
566 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
567 if (cur == NULL) {
568 mbedtls_printf("Not enough memory for subjectAltName list\n");
569 goto usage;
570 }
571
572 cur->next = NULL;
573
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400574 if ((subtype_value = strchr(q, ':')) != NULL) {
575 *subtype_value++ = '\0';
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100576 } else {
Waleed Elmelegy58674652023-10-13 10:03:12 +0100577 mbedtls_printf(
David Horstmann4a493b22023-10-17 14:57:23 +0100578 "Invalid argument for option SAN: Entry must be of the form TYPE:value\n");
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100579 goto usage;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400580 }
581 if (strcmp(q, "RFC822") == 0) {
582 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
583 } else if (strcmp(q, "URI") == 0) {
584 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
585 } else if (strcmp(q, "DNS") == 0) {
586 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
587 } else if (strcmp(q, "IP") == 0) {
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000588 size_t ip_addr_len = 0;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400589 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000590 ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
591 if (ip_addr_len == 0) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400592 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
593 subtype_value);
594 goto exit;
595 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400596 cur->node.san.unstructured_name.p = (unsigned char *) ip;
597 cur->node.san.unstructured_name.len = sizeof(ip);
598 } else if (strcmp(q, "DN") == 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400599 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
600 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400601 subtype_value)) != 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400602 mbedtls_strerror(ret, buf, sizeof(buf));
603 mbedtls_printf(
604 " failed\n ! mbedtls_x509_string_to_names "
605 "returned -0x%04x - %s\n\n",
606 (unsigned int) -ret, buf);
607 goto exit;
608 }
609 cur->node.san.directory_name = *ext_san_dirname;
610 } else {
611 mbedtls_free(cur);
612 goto usage;
613 }
614
Andrzej Kurekf994bc52023-06-02 05:10:17 -0400615 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
616 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
617 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400618 q = subtype_value;
619 cur->node.san.unstructured_name.p = (unsigned char *) q;
620 cur->node.san.unstructured_name.len = strlen(q);
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400621 }
622
623 if (prev == NULL) {
624 opt.san_list = cur;
625 } else {
626 prev->next = cur;
627 }
628
629 prev = cur;
630 q = r;
631 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 } else if (strcmp(p, "ns_cert_type") == 0) {
633 while (q != NULL) {
634 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100641 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100647 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100649 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100651 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 } else {
653 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100655 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200656
657 q = r;
658 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 } else if (strcmp(p, "format") == 0) {
660 if (strcmp(q, "der") == 0) {
661 opt.format = FORMAT_DER;
662 } else if (strcmp(q, "pem") == 0) {
663 opt.format = FORMAT_PEM;
664 } else {
665 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100666 goto usage;
667 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200669 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200674
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200675 /*
676 * 0. Seed the PRNG
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_printf(" . Seeding the random number generator...");
679 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
682 (const unsigned char *) pers,
683 strlen(pers))) != 0) {
684 mbedtls_strerror(ret, buf, sizeof(buf));
685 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
686 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200687 goto exit;
688 }
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200691
Paul Bakker9397dcb2013-09-06 09:55:26 +0200692 // Parse serial to MPI
693 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_printf(" . Reading serial number...");
695 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200696
Valerio Setti791bbe62023-01-11 10:40:18 +0100697 if (serial_frmt == SERIAL_FRMT_HEX) {
698 ret = mbedtls_test_unhexify(serial, sizeof(serial),
699 opt.serial_hex, &serial_len);
700 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
701 ret = parse_serial_decimal_format(serial, sizeof(serial),
702 opt.serial, &serial_len);
703 }
704
705 if (ret != 0) {
706 mbedtls_printf(" failed\n ! Unable to parse serial\n");
707 goto exit;
708 }
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200711
Paul Bakker1014e952013-09-09 13:59:42 +0200712 // Parse issuer certificate if present
713 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200715 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200716 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 mbedtls_printf(" . Loading the issuer certificate ...");
719 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
722 mbedtls_strerror(ret, buf, sizeof(buf));
723 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
724 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200725 goto exit;
726 }
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
729 &issuer_crt.subject);
730 if (ret < 0) {
731 mbedtls_strerror(ret, buf, sizeof(buf));
732 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
733 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200734 goto exit;
735 }
736
Paul Bakkere2673fb2013-09-09 15:52:07 +0200737 opt.issuer_name = issuer_name;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200740 }
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200743 // Parse certificate request if present
744 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200746 /*
747 * 1.0.b. Load the CSR
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 mbedtls_printf(" . Loading the certificate request ...");
750 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
753 mbedtls_strerror(ret, buf, sizeof(buf));
754 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
755 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200756 goto exit;
757 }
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
760 &csr.subject);
761 if (ret < 0) {
762 mbedtls_strerror(ret, buf, sizeof(buf));
763 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
764 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200765 goto exit;
766 }
767
768 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200769 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200774
775 /*
776 * 1.1. Load the keys
777 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (!opt.selfsign && !strlen(opt.request_file)) {
779 mbedtls_printf(" . Loading the subject key ...");
780 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
783 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
784 if (ret != 0) {
785 mbedtls_strerror(ret, buf, sizeof(buf));
786 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
787 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200788 goto exit;
789 }
Paul Bakker1014e952013-09-09 13:59:42 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200792 }
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 mbedtls_printf(" . Loading the issuer key ...");
795 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
798 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
799 if (ret != 0) {
800 mbedtls_strerror(ret, buf, sizeof(buf));
801 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
802 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200803 goto exit;
804 }
805
806 // Check if key and issuer certificate match
807 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (strlen(opt.issuer_crt)) {
809 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
810 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
811 mbedtls_printf(" failed\n ! issuer_key does not match "
812 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200813 goto exit;
814 }
815 }
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100820 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200821 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200822 }
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
825 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200826
Paul Bakker9397dcb2013-09-06 09:55:26 +0200827 /*
828 * 1.0. Check the names for validity
829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
831 mbedtls_strerror(ret, buf, sizeof(buf));
832 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
833 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200834 goto exit;
835 }
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
838 mbedtls_strerror(ret, buf, sizeof(buf));
839 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
840 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200841 goto exit;
842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_printf(" . Setting certificate values ...");
845 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_x509write_crt_set_version(&crt, opt.version);
848 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100849
Valerio Settiaf4815c2023-01-26 17:43:09 +0100850 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100851 if (ret != 0) {
852 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100853 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100854 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
855 goto exit;
856 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
859 if (ret != 0) {
860 mbedtls_strerror(ret, buf, sizeof(buf));
861 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
862 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200863 goto exit;
864 }
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
869 opt.basic_constraints != 0) {
870 mbedtls_printf(" . Adding the Basic Constraints extension ...");
871 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
874 opt.max_pathlen);
875 if (ret != 0) {
876 mbedtls_strerror(ret, buf, sizeof(buf));
877 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
878 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100879 goto exit;
880 }
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100883 }
Paul Bakker15162a02013-09-06 19:27:21 +0200884
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100885#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
887 opt.subject_identifier != 0) {
888 mbedtls_printf(" . Adding the Subject Key Identifier ...");
889 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
892 if (ret != 0) {
893 mbedtls_strerror(ret, buf, sizeof(buf));
894 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
895 "_key_identifier returned -0x%04x - %s\n\n",
896 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100897 goto exit;
898 }
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200901 }
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
904 opt.authority_identifier != 0) {
905 mbedtls_printf(" . Adding the Authority Key Identifier ...");
906 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
909 if (ret != 0) {
910 mbedtls_strerror(ret, buf, sizeof(buf));
911 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
912 "key_identifier returned -0x%04x - %s\n\n",
913 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100914 goto exit;
915 }
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100918 }
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100919#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker15162a02013-09-06 19:27:21 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
922 opt.key_usage != 0) {
923 mbedtls_printf(" . Adding the Key Usage extension ...");
924 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
927 if (ret != 0) {
928 mbedtls_strerror(ret, buf, sizeof(buf));
929 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
930 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200931 goto exit;
932 }
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200935 }
936
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400937 if (opt.san_list != NULL) {
938 ret = mbedtls_x509write_crt_set_subject_alternative_name(&crt, opt.san_list);
939
940 if (ret != 0) {
941 mbedtls_printf(
Andrzej Kurek1bc7df22023-04-04 07:09:04 -0400942 " failed\n ! mbedtls_x509write_crt_set_subject_alternative_name returned %d",
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400943 ret);
944 goto exit;
945 }
946 }
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (opt.ext_key_usage) {
949 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
950 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
953 if (ret != 0) {
954 mbedtls_strerror(ret, buf, sizeof(buf));
955 mbedtls_printf(
956 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
957 (unsigned int) -ret,
958 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100959 goto exit;
960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
966 opt.ns_cert_type != 0) {
967 mbedtls_printf(" . Adding the NS Cert Type extension ...");
968 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
971 if (ret != 0) {
972 mbedtls_strerror(ret, buf, sizeof(buf));
973 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
974 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200975 goto exit;
976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200979 }
980
Paul Bakker9397dcb2013-09-06 09:55:26 +0200981 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100982 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_printf(" . Writing the certificate...");
985 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((ret = write_certificate(&crt, opt.output_file,
988 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
989 mbedtls_strerror(ret, buf, sizeof(buf));
990 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
991 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200992 goto exit;
993 }
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200996
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100997 exit_code = MBEDTLS_EXIT_SUCCESS;
998
Paul Bakker9397dcb2013-09-06 09:55:26 +0200999exit:
Hanno Becker30a95102018-10-05 09:49:33 +01001000#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +01001002#endif /* MBEDTLS_X509_CSR_PARSE_C */
Andrzej Kurek5da1d752023-04-05 08:30:59 -04001003 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_x509_crt_free(&issuer_crt);
1005 mbedtls_x509write_crt_free(&crt);
1006 mbedtls_pk_free(&loaded_subject_key);
1007 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_ctr_drbg_free(&ctr_drbg);
1009 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +02001010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +02001011 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +02001012#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +02001015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
1017 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +01001018 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */