blob: 3cabff4b5a634c252adf870f9e74de6fccfd3edf [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
Ben Taylor440cb2a2025-03-05 09:40:08 +0000209static int write_certificate(mbedtls_x509write_cert *crt, const char *output_file)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210{
211 int ret;
212 FILE *f;
213 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100214 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 size_t len = 0;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memset(output_buf, 0, 4096);
218 if (opt.format == FORMAT_DER) {
Ben Taylor440cb2a2025-03-05 09:40:08 +0000219 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096);
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ret < 0) {
221 return ret;
222 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
Dave Rodgman66e05502022-10-27 16:29:38 +0100224 len = ret;
225 output_start = output_buf + 4096 - len;
226 } else {
Ben Taylor440cb2a2025-03-05 09:40:08 +0000227 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096);
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (ret < 0) {
229 return ret;
230 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100233 output_start = output_buf;
234 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if ((f = fopen(output_file, "w")) == NULL) {
237 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200238 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (fwrite(output_start, 1, len, f) != len) {
241 fclose(f);
242 return -1;
243 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 fclose(f);
246
247 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200248}
249
Michael Schuster8db8d612024-06-01 21:15:02 +0200250static int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
Michael Schuster04200932024-06-12 00:05:25 +0200251 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100252{
Valerio Setti791bbe62023-01-11 10:40:18 +0100253 unsigned long long int dec;
254 unsigned int remaining_bytes = sizeof(dec);
255 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100256 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100257 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100258
Valerio Setti791bbe62023-01-11 10:40:18 +0100259 errno = 0;
260 dec = strtoull(ibuf, &end_ptr, 10);
261
262 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100263 return -1;
264 }
265
Valerio Setti791bbe62023-01-11 10:40:18 +0100266 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100267
Valerio Setti791bbe62023-01-11 10:40:18 +0100268 while (remaining_bytes > 0) {
269 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100270 return -1;
271 }
272
Valerio Setti791bbe62023-01-11 10:40:18 +0100273 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
274
275 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100276 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100277 *p = val;
278 (*len)++;
279 p++;
280 }
281
282 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100283 }
284
285 return 0;
286}
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100290 int ret = 1;
291 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_x509_crt issuer_crt;
293 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
294 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200296 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200297 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100298 int i;
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400299 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200301 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100305 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100306 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
307 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100308 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_entropy_context entropy;
310 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200311 const char *pers = "crt example app";
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400312 mbedtls_x509_san_list *cur, *prev;
313 uint8_t ip[4] = { 0 };
Paul Bakker9397dcb2013-09-06 09:55:26 +0200314 /*
315 * Set to sane values
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_x509write_crt_init(&crt);
318 mbedtls_pk_init(&loaded_issuer_key);
319 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ctr_drbg_init(&ctr_drbg);
321 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200324#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_x509_crt_init(&issuer_crt);
326 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100327 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200328
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200329#if defined(MBEDTLS_USE_PSA_CRYPTO)
330 psa_status_t status = psa_crypto_init();
331 if (status != PSA_SUCCESS) {
332 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
333 (int) status);
334 goto exit;
335 }
336#endif /* MBEDTLS_USE_PSA_CRYPTO */
337
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000338 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100339usage:
340 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200341 goto exit;
342 }
343
Paul Bakker1014e952013-09-09 13:59:42 +0200344 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200345 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200346 opt.subject_key = DFL_SUBJECT_KEY;
347 opt.issuer_key = DFL_ISSUER_KEY;
348 opt.subject_pwd = DFL_SUBJECT_PWD;
349 opt.issuer_pwd = DFL_ISSUER_PWD;
350 opt.output_file = DFL_OUTPUT_FILENAME;
351 opt.subject_name = DFL_SUBJECT_NAME;
352 opt.issuer_name = DFL_ISSUER_NAME;
353 opt.not_before = DFL_NOT_BEFORE;
354 opt.not_after = DFL_NOT_AFTER;
355 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100356 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200357 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200358 opt.is_ca = DFL_IS_CA;
359 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200360 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100361 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200362 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100363 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100364 opt.md = DFL_DIGEST;
365 opt.subject_identifier = DFL_SUBJ_IDENT;
366 opt.authority_identifier = DFL_AUTH_IDENT;
367 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100368 opt.format = DFL_FORMAT;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400369 opt.san_list = NULL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200372
373 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200375 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377 *q++ = '\0';
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200380 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200390 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200396 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200398 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200400 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100402 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
403 mbedtls_printf("Invalid attempt to set the serial more than once\n");
404 goto usage;
405 }
406 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200407 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100408 } else if (strcmp(p, "serial_hex") == 0) {
409 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
410 mbedtls_printf("Invalid attempt to set the serial more than once\n");
411 goto usage;
412 }
413 serial_frmt = SERIAL_FRMT_HEX;
414 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 } else if (strcmp(p, "authority_identifier") == 0) {
416 opt.authority_identifier = atoi(q);
417 if (opt.authority_identifier != 0 &&
418 opt.authority_identifier != 1) {
419 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100420 goto usage;
421 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 } else if (strcmp(p, "subject_identifier") == 0) {
423 opt.subject_identifier = atoi(q);
424 if (opt.subject_identifier != 0 &&
425 opt.subject_identifier != 1) {
426 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100427 goto usage;
428 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 } else if (strcmp(p, "basic_constraints") == 0) {
430 opt.basic_constraints = atoi(q);
431 if (opt.basic_constraints != 0 &&
432 opt.basic_constraints != 1) {
433 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100434 goto usage;
435 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200437 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_md_info_from_string(q);
439 if (md_info == NULL) {
440 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100441 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100442 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 opt.md = mbedtls_md_get_type(md_info);
444 } else if (strcmp(p, "version") == 0) {
445 opt.version = atoi(q);
446 if (opt.version < 1 || opt.version > 3) {
447 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100448 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100449 }
Hanno Becker38eff432017-09-22 15:38:20 +0100450 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 } else if (strcmp(p, "selfsign") == 0) {
452 opt.selfsign = atoi(q);
453 if (opt.selfsign < 0 || opt.selfsign > 1) {
454 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200455 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100456 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 } else if (strcmp(p, "is_ca") == 0) {
458 opt.is_ca = atoi(q);
459 if (opt.is_ca < 0 || opt.is_ca > 1) {
460 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200461 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100462 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } else if (strcmp(p, "max_pathlen") == 0) {
464 opt.max_pathlen = atoi(q);
465 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
466 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200467 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100468 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 } else if (strcmp(p, "key_usage") == 0) {
470 while (q != NULL) {
471 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200472 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100480 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100482 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100484 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 } else {
490 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100492 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493
494 q = r;
495 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100497 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 while (q != NULL) {
500 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100501 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100505 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (strcmp(q, "serverAuth") == 0) {
507 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
508 } else if (strcmp(q, "clientAuth") == 0) {
509 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
510 } else if (strcmp(q, "codeSigning") == 0) {
511 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
512 } else if (strcmp(q, "emailProtection") == 0) {
513 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
514 } else if (strcmp(q, "timeStamping") == 0) {
515 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
516 } else if (strcmp(q, "OCSPSigning") == 0) {
517 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
Pengyu Lvb0786072023-05-18 17:18:36 +0800518 } else if (strcmp(q, "any") == 0) {
519 SET_OID(ext_key_usage->buf, MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 } else {
521 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100522 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100523 }
Dave Rodgman64937852022-08-15 14:12:25 +0100524
525 *tail = ext_key_usage;
526 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100527
528 q = r;
529 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400530 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400531 char *subtype_value;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400532 prev = NULL;
533
534 while (q != NULL) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400535 char *semicolon;
536 r = q;
537
538 /* Find the first non-escaped ; occurrence and remove escaped ones */
539 do {
540 if ((semicolon = strchr(r, ';')) != NULL) {
541 if (*(semicolon-1) != '\\') {
542 r = semicolon;
543 break;
544 }
545 /* Remove the escape character */
546 size_t size_left = strlen(semicolon);
547 memmove(semicolon-1, semicolon, size_left);
548 *(semicolon + size_left - 1) = '\0';
549 /* r will now point at the character after the semicolon */
550 r = semicolon;
551 }
552
553 } while (semicolon != NULL);
554
555 if (semicolon != NULL) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400556 *r++ = '\0';
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400557 } else {
558 r = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400559 }
560
561 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
562 if (cur == NULL) {
563 mbedtls_printf("Not enough memory for subjectAltName list\n");
564 goto usage;
565 }
566
567 cur->next = NULL;
568
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400569 if ((subtype_value = strchr(q, ':')) != NULL) {
570 *subtype_value++ = '\0';
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100571 } else {
Waleed Elmelegy58674652023-10-13 10:03:12 +0100572 mbedtls_printf(
David Horstmann4a493b22023-10-17 14:57:23 +0100573 "Invalid argument for option SAN: Entry must be of the form TYPE:value\n");
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100574 goto usage;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400575 }
576 if (strcmp(q, "RFC822") == 0) {
577 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
578 } else if (strcmp(q, "URI") == 0) {
579 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
580 } else if (strcmp(q, "DNS") == 0) {
581 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
582 } else if (strcmp(q, "IP") == 0) {
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000583 size_t ip_addr_len = 0;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400584 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000585 ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
586 if (ip_addr_len == 0) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400587 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
588 subtype_value);
589 goto exit;
590 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400591 cur->node.san.unstructured_name.p = (unsigned char *) ip;
592 cur->node.san.unstructured_name.len = sizeof(ip);
593 } else if (strcmp(q, "DN") == 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400594 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
Manuel Pégourié-Gonnard6b114792025-05-05 17:09:14 +0200595 /* Work around an API mismatch between string_to_names() and
596 * mbedtls_x509_subject_alternative_name, which holds an
597 * actual mbedtls_x509_name while a pointer to one would be
Manuel Pégourié-Gonnardbb8c0ab2025-05-19 12:28:42 +0200598 * more convenient here. (Note mbedtls_x509_name and
599 * mbedtls_asn1_named_data are synonymous, again
600 * string_to_names() uses one while
601 * cur->node.san.directory_name is nominally the other.) */
Manuel Pégourié-Gonnard6b114792025-05-05 17:09:14 +0200602 mbedtls_asn1_named_data *tmp_san_dirname = NULL;
603 if ((ret = mbedtls_x509_string_to_names(&tmp_san_dirname,
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400604 subtype_value)) != 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400605 mbedtls_strerror(ret, buf, sizeof(buf));
606 mbedtls_printf(
607 " failed\n ! mbedtls_x509_string_to_names "
608 "returned -0x%04x - %s\n\n",
609 (unsigned int) -ret, buf);
610 goto exit;
611 }
Manuel Pégourié-Gonnard6b114792025-05-05 17:09:14 +0200612 cur->node.san.directory_name = *tmp_san_dirname;
613 mbedtls_free(tmp_san_dirname);
614 tmp_san_dirname = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400615 } else {
616 mbedtls_free(cur);
617 goto usage;
618 }
619
Andrzej Kurekf994bc52023-06-02 05:10:17 -0400620 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
621 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
622 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400623 q = subtype_value;
624 cur->node.san.unstructured_name.p = (unsigned char *) q;
625 cur->node.san.unstructured_name.len = strlen(q);
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400626 }
627
628 if (prev == NULL) {
629 opt.san_list = cur;
630 } else {
631 prev->next = cur;
632 }
633
634 prev = cur;
635 q = r;
636 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 } else if (strcmp(p, "ns_cert_type") == 0) {
638 while (q != NULL) {
639 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100646 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100652 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100654 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100656 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 } else {
658 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200659 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100660 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200661
662 q = r;
663 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 } else if (strcmp(p, "format") == 0) {
665 if (strcmp(q, "der") == 0) {
666 opt.format = FORMAT_DER;
667 } else if (strcmp(q, "pem") == 0) {
668 opt.format = FORMAT_PEM;
669 } else {
670 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100671 goto usage;
672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200674 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200676 }
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200679
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200680 /*
681 * 0. Seed the PRNG
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_printf(" . Seeding the random number generator...");
684 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
687 (const unsigned char *) pers,
688 strlen(pers))) != 0) {
689 mbedtls_strerror(ret, buf, sizeof(buf));
690 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
691 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200692 goto exit;
693 }
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200696
Paul Bakker9397dcb2013-09-06 09:55:26 +0200697 // Parse serial to MPI
698 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 mbedtls_printf(" . Reading serial number...");
700 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200701
Valerio Setti791bbe62023-01-11 10:40:18 +0100702 if (serial_frmt == SERIAL_FRMT_HEX) {
703 ret = mbedtls_test_unhexify(serial, sizeof(serial),
704 opt.serial_hex, &serial_len);
705 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
706 ret = parse_serial_decimal_format(serial, sizeof(serial),
707 opt.serial, &serial_len);
708 }
709
710 if (ret != 0) {
711 mbedtls_printf(" failed\n ! Unable to parse serial\n");
712 goto exit;
713 }
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200716
Paul Bakker1014e952013-09-09 13:59:42 +0200717 // Parse issuer certificate if present
718 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200720 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200721 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200722 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 mbedtls_printf(" . Loading the issuer certificate ...");
724 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
727 mbedtls_strerror(ret, buf, sizeof(buf));
728 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
729 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200730 goto exit;
731 }
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
734 &issuer_crt.subject);
735 if (ret < 0) {
736 mbedtls_strerror(ret, buf, sizeof(buf));
737 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
738 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200739 goto exit;
740 }
741
Paul Bakkere2673fb2013-09-09 15:52:07 +0200742 opt.issuer_name = issuer_name;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200745 }
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200748 // Parse certificate request if present
749 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200751 /*
752 * 1.0.b. Load the CSR
753 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 mbedtls_printf(" . Loading the certificate request ...");
755 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
758 mbedtls_strerror(ret, buf, sizeof(buf));
759 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
760 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200761 goto exit;
762 }
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
765 &csr.subject);
766 if (ret < 0) {
767 mbedtls_strerror(ret, buf, sizeof(buf));
768 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
769 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200770 goto exit;
771 }
772
773 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200774 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200779
780 /*
781 * 1.1. Load the keys
782 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (!opt.selfsign && !strlen(opt.request_file)) {
784 mbedtls_printf(" . Loading the subject key ...");
785 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000788 opt.subject_pwd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (ret != 0) {
790 mbedtls_strerror(ret, buf, sizeof(buf));
791 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
792 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200793 goto exit;
794 }
Paul Bakker1014e952013-09-09 13:59:42 +0200795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_printf(" . Loading the issuer key ...");
800 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000803 opt.issuer_pwd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ret != 0) {
805 mbedtls_strerror(ret, buf, sizeof(buf));
806 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
807 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200808 goto exit;
809 }
810
811 // Check if key and issuer certificate match
812 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (strlen(opt.issuer_crt)) {
Ben Taylor440cb2a2025-03-05 09:40:08 +0000814 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_printf(" failed\n ! issuer_key does not match "
816 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200817 goto exit;
818 }
819 }
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100824 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200825 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200826 }
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
829 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200830
Paul Bakker9397dcb2013-09-06 09:55:26 +0200831 /*
832 * 1.0. Check the names for validity
833 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
835 mbedtls_strerror(ret, buf, sizeof(buf));
836 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
837 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200838 goto exit;
839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
842 mbedtls_strerror(ret, buf, sizeof(buf));
843 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
844 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200845 goto exit;
846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 mbedtls_printf(" . Setting certificate values ...");
849 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_x509write_crt_set_version(&crt, opt.version);
852 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100853
Valerio Settiaf4815c2023-01-26 17:43:09 +0100854 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100855 if (ret != 0) {
856 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100857 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100858 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
859 goto exit;
860 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
863 if (ret != 0) {
864 mbedtls_strerror(ret, buf, sizeof(buf));
865 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
866 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200867 goto exit;
868 }
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
873 opt.basic_constraints != 0) {
874 mbedtls_printf(" . Adding the Basic Constraints extension ...");
875 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
878 opt.max_pathlen);
879 if (ret != 0) {
880 mbedtls_strerror(ret, buf, sizeof(buf));
881 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
882 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100883 goto exit;
884 }
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100887 }
Paul Bakker15162a02013-09-06 19:27:21 +0200888
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100889#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
891 opt.subject_identifier != 0) {
892 mbedtls_printf(" . Adding the Subject Key Identifier ...");
893 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
896 if (ret != 0) {
897 mbedtls_strerror(ret, buf, sizeof(buf));
898 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
899 "_key_identifier returned -0x%04x - %s\n\n",
900 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100901 goto exit;
902 }
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
908 opt.authority_identifier != 0) {
909 mbedtls_printf(" . Adding the Authority Key Identifier ...");
910 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
913 if (ret != 0) {
914 mbedtls_strerror(ret, buf, sizeof(buf));
915 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
916 "key_identifier returned -0x%04x - %s\n\n",
917 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100918 goto exit;
919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100922 }
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100923#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker15162a02013-09-06 19:27:21 +0200924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
926 opt.key_usage != 0) {
927 mbedtls_printf(" . Adding the Key Usage extension ...");
928 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
931 if (ret != 0) {
932 mbedtls_strerror(ret, buf, sizeof(buf));
933 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
934 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200935 goto exit;
936 }
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200939 }
940
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400941 if (opt.san_list != NULL) {
942 ret = mbedtls_x509write_crt_set_subject_alternative_name(&crt, opt.san_list);
943
944 if (ret != 0) {
945 mbedtls_printf(
Andrzej Kurek1bc7df22023-04-04 07:09:04 -0400946 " failed\n ! mbedtls_x509write_crt_set_subject_alternative_name returned %d",
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400947 ret);
948 goto exit;
949 }
950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (opt.ext_key_usage) {
953 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
954 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
957 if (ret != 0) {
958 mbedtls_strerror(ret, buf, sizeof(buf));
959 mbedtls_printf(
960 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
961 (unsigned int) -ret,
962 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100963 goto exit;
964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
970 opt.ns_cert_type != 0) {
971 mbedtls_printf(" . Adding the NS Cert Type extension ...");
972 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
975 if (ret != 0) {
976 mbedtls_strerror(ret, buf, sizeof(buf));
977 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
978 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200979 goto exit;
980 }
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200983 }
984
Paul Bakker9397dcb2013-09-06 09:55:26 +0200985 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100986 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200987 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_printf(" . Writing the certificate...");
989 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200990
Ben Taylor440cb2a2025-03-05 09:40:08 +0000991 if ((ret = write_certificate(&crt, opt.output_file)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_strerror(ret, buf, sizeof(buf));
993 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
994 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200995 goto exit;
996 }
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200999
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +01001000 exit_code = MBEDTLS_EXIT_SUCCESS;
1001
Paul Bakker9397dcb2013-09-06 09:55:26 +02001002exit:
Manuel Pégourié-Gonnardb0958622025-05-05 17:31:35 +02001003 cur = opt.san_list;
1004 while (cur != NULL) {
1005 mbedtls_x509_san_list *next = cur->next;
1006 /* Note: mbedtls_x509_free_subject_alt_name() is not what we want here.
1007 * It's the right thing for entries that were parsed from a certificate,
1008 * where pointers are to the raw certificate, but here all the
1009 * pointers were allocated while parsing from a user-provided string. */
1010 if (cur->node.type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
Manuel Pégourié-Gonnard6b8f5172025-05-21 11:17:39 +02001011 mbedtls_x509_name *dn = &cur->node.san.directory_name;
1012 mbedtls_free(dn->oid.p);
1013 mbedtls_free(dn->val.p);
1014 mbedtls_asn1_free_named_data_list(&dn->next);
Manuel Pégourié-Gonnardb0958622025-05-05 17:31:35 +02001015 }
1016 mbedtls_free(cur);
1017 cur = next;
1018 }
1019
Hanno Becker30a95102018-10-05 09:49:33 +01001020#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +01001022#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_x509_crt_free(&issuer_crt);
1024 mbedtls_x509write_crt_free(&crt);
1025 mbedtls_pk_free(&loaded_subject_key);
1026 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_ctr_drbg_free(&ctr_drbg);
1028 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +02001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +02001030 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +02001031#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +02001034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
1036 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +01001037 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */