blob: 8bee0a666f2712b5db31a92684593e8378be6689 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040023/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040024#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000025
Simon Butcher203a6932016-10-07 15:00:17 +010026#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
27 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
28 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010029 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Simon Butcher203a6932016-10-07 15:00:17 +010030 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010031int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020032{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010034 "MBEDTLS_FS_IO and/or MBEDTLS_MD_CAN_SHA256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010035 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
36 "MBEDTLS_ERROR_C not defined.\n");
37 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038}
39#else
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crt.h"
42#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010043#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/entropy.h"
45#include "mbedtls/ctr_drbg.h"
46#include "mbedtls/error.h"
Valerio Setti791bbe62023-01-11 10:40:18 +010047#include "test/helpers.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020048
Rich Evans18b78c72015-02-11 14:06:19 +000049#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
Valerio Setti791bbe62023-01-11 10:40:18 +010052#include <errno.h>
Rich Evans18b78c72015-02-11 14:06:19 +000053
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010054#define SET_OID(x, oid) \
Gilles Peskine449bd832023-01-11 14:50:10 +010055 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000058#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010059 " request_file=%%s default: (empty)\n" \
60 " If request_file is specified, subject_key,\n" \
61 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000062#else
63#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000065
Dave Rodgman66e05502022-10-27 16:29:38 +010066#define FORMAT_PEM 0
67#define FORMAT_DER 1
68
Paul Bakker1014e952013-09-09 13:59:42 +020069#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020070#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_SUBJECT_KEY "subject.key"
72#define DFL_ISSUER_KEY "ca.key"
73#define DFL_SUBJECT_PWD ""
74#define DFL_ISSUER_PWD ""
75#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000076#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_NOT_BEFORE "20010101000000"
79#define DFL_NOT_AFTER "20301231235959"
80#define DFL_SERIAL "1"
Valerio Setti791bbe62023-01-11 10:40:18 +010081#define DFL_SERIAL_HEX "1"
Andrzej Kurekccdd9752023-04-01 10:38:30 -040082#define DFL_EXT_SUBJECTALTNAME ""
Paul Bakkerb2d7f232013-09-09 16:24:18 +020083#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020084#define DFL_IS_CA 0
85#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010086#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010088#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020089#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010090#define DFL_VERSION 3
91#define DFL_AUTH_IDENT 1
92#define DFL_SUBJ_IDENT 1
93#define DFL_CONSTRAINTS 1
94#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010095#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020096
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE \
98 "\n usage: cert_write param=<>...\n" \
99 "\n acceptable parameters:\n" \
100 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100101 " subject_key=%%s default: subject.key\n" \
102 " subject_pwd=%%s default: (empty)\n" \
103 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " issuer_crt=%%s default: (empty)\n" \
106 " If issuer_crt is specified, issuer_name is\n" \
107 " ignored!\n" \
108 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100110 " selfsign=%%d default: 0 (false)\n" \
111 " If selfsign is enabled, issuer_name and\n" \
112 " issuer_key are required (issuer_crt and\n" \
113 " subject_* are ignored\n" \
114 " issuer_key=%%s default: ca.key\n" \
115 " issuer_pwd=%%s default: (empty)\n" \
116 " output_file=%%s default: cert.crt\n" \
117 " serial=%%s default: 1\n" \
Valerio Setti791bbe62023-01-11 10:40:18 +0100118 " In decimal format; it can be used as\n" \
119 " alternative to serial_hex, but it's\n" \
120 " limited in max length to\n" \
121 " unsigned long long int\n" \
122 " serial_hex=%%s default: 1\n" \
123 " In hex format; it can be used as\n" \
124 " alternative to serial\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 " not_before=%%s default: 20010101000000\n" \
126 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100127 " is_ca=%%d default: 0 (disabled)\n" \
128 " max_pathlen=%%d default: -1 (none)\n" \
129 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200130 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200131 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200132 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100133 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100135 " subject_identifier=%%s default: 1\n" \
136 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 " (Considered for v3 only)\n" \
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400138 " san=%%s default: (none)\n" \
Andrzej Kurekf70f4602023-04-24 18:39:53 -0400139 " Semicolon-separated-list of values:\n" \
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400140 " DNS:value\n" \
141 " URI:value\n" \
142 " RFC822:value\n" \
143 " IP:value (Only IPv4 is supported)\n" \
Andrzej Kurek446e53d2023-04-25 02:21:07 -0400144 " DN:list of comma separated key=value pairs\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100145 " authority_identifier=%%s default: 1\n" \
146 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100148 " basic_constraints=%%d default: 1\n" \
149 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100151 " key_usage=%%s default: (empty)\n" \
152 " Comma-separated-list of values:\n" \
153 " digital_signature\n" \
154 " non_repudiation\n" \
155 " key_encipherment\n" \
156 " data_encipherment\n" \
157 " key_agreement\n" \
158 " key_cert_sign\n" \
159 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100161 " ext_key_usage=%%s default: (empty)\n" \
162 " Comma-separated-list of values:\n" \
163 " serverAuth\n" \
164 " clientAuth\n" \
165 " codeSigning\n" \
166 " emailProtection\n" \
167 " timeStamping\n" \
168 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100169 " ns_cert_type=%%s default: (empty)\n" \
170 " Comma-separated-list of values:\n" \
171 " ssl_client\n" \
172 " ssl_server\n" \
173 " email\n" \
174 " object_signing\n" \
175 " ssl_ca\n" \
176 " email_ca\n" \
177 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100178 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000179 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000180
Valerio Setti791bbe62023-01-11 10:40:18 +0100181typedef enum {
182 SERIAL_FRMT_UNSPEC,
183 SERIAL_FRMT_DEC,
184 SERIAL_FRMT_HEX
185} serial_format_t;
Simon Butcher63cb97e2018-12-06 17:43:31 +0000186
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187/*
188 * global options
189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100191 const char *issuer_crt; /* filename of the issuer certificate */
192 const char *request_file; /* filename of the certificate request */
193 const char *subject_key; /* filename of the subject key file */
194 const char *issuer_key; /* filename of the issuer key file */
195 const char *subject_pwd; /* password for the subject key file */
196 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100197 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100198 const char *subject_name; /* subject name for certificate */
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400199 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100200 const char *issuer_name; /* issuer name for certificate */
201 const char *not_before; /* validity period not before */
202 const char *not_after; /* validity period not after */
Valerio Setti791bbe62023-01-11 10:40:18 +0100203 const char *serial; /* serial number string (decimal) */
204 const char *serial_hex; /* serial number string (hex) */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200205 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200206 int is_ca; /* is a CA certificate */
207 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100208 int authority_identifier; /* add authority identifier to CRT */
209 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100210 int basic_constraints; /* add basic constraints ext to CRT */
211 int version; /* CRT version */
212 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100214 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100216 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217} opt;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
220 int (*f_rng)(void *, unsigned char *, size_t),
221 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200222{
223 int ret;
224 FILE *f;
225 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100226 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 size_t len = 0;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 memset(output_buf, 0, 4096);
230 if (opt.format == FORMAT_DER) {
231 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
232 f_rng, p_rng);
233 if (ret < 0) {
234 return ret;
235 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236
Dave Rodgman66e05502022-10-27 16:29:38 +0100237 len = ret;
238 output_start = output_buf + 4096 - len;
239 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
241 f_rng, p_rng);
242 if (ret < 0) {
243 return ret;
244 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100247 output_start = output_buf;
248 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if ((f = fopen(output_file, "w")) == NULL) {
251 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200252 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (fwrite(output_start, 1, len, f) != len) {
255 fclose(f);
256 return -1;
257 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 fclose(f);
260
261 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262}
263
Valerio Setti791bbe62023-01-11 10:40:18 +0100264int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
265 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100266{
Valerio Setti791bbe62023-01-11 10:40:18 +0100267 unsigned long long int dec;
268 unsigned int remaining_bytes = sizeof(dec);
269 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100270 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100271 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100272
Valerio Setti791bbe62023-01-11 10:40:18 +0100273 errno = 0;
274 dec = strtoull(ibuf, &end_ptr, 10);
275
276 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100277 return -1;
278 }
279
Valerio Setti791bbe62023-01-11 10:40:18 +0100280 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100281
Valerio Setti791bbe62023-01-11 10:40:18 +0100282 while (remaining_bytes > 0) {
283 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100284 return -1;
285 }
286
Valerio Setti791bbe62023-01-11 10:40:18 +0100287 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
288
289 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100290 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100291 *p = val;
292 (*len)++;
293 p++;
294 }
295
296 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100297 }
298
299 return 0;
300}
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100304 int ret = 1;
305 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_x509_crt issuer_crt;
307 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
308 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200310 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200311 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100312 int i;
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400313 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200315 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100319 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100320 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
321 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100322 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_entropy_context entropy;
324 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200325 const char *pers = "crt example app";
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400326 mbedtls_x509_san_list *cur, *prev;
Andrzej Kurek5da1d752023-04-05 08:30:59 -0400327 mbedtls_asn1_named_data *ext_san_dirname = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400328 uint8_t ip[4] = { 0 };
Paul Bakker9397dcb2013-09-06 09:55:26 +0200329 /*
330 * Set to sane values
331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_x509write_crt_init(&crt);
333 mbedtls_pk_init(&loaded_issuer_key);
334 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_ctr_drbg_init(&ctr_drbg);
336 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200339#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 mbedtls_x509_crt_init(&issuer_crt);
341 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100342 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200343
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200344#if defined(MBEDTLS_USE_PSA_CRYPTO)
345 psa_status_t status = psa_crypto_init();
346 if (status != PSA_SUCCESS) {
347 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
348 (int) status);
349 goto exit;
350 }
351#endif /* MBEDTLS_USE_PSA_CRYPTO */
352
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000353 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100354usage:
355 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200356 goto exit;
357 }
358
Paul Bakker1014e952013-09-09 13:59:42 +0200359 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200360 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200361 opt.subject_key = DFL_SUBJECT_KEY;
362 opt.issuer_key = DFL_ISSUER_KEY;
363 opt.subject_pwd = DFL_SUBJECT_PWD;
364 opt.issuer_pwd = DFL_ISSUER_PWD;
365 opt.output_file = DFL_OUTPUT_FILENAME;
366 opt.subject_name = DFL_SUBJECT_NAME;
367 opt.issuer_name = DFL_ISSUER_NAME;
368 opt.not_before = DFL_NOT_BEFORE;
369 opt.not_after = DFL_NOT_AFTER;
370 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100371 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200372 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200373 opt.is_ca = DFL_IS_CA;
374 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200375 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100376 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100378 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100379 opt.md = DFL_DIGEST;
380 opt.subject_identifier = DFL_SUBJ_IDENT;
381 opt.authority_identifier = DFL_AUTH_IDENT;
382 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100383 opt.format = DFL_FORMAT;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400384 opt.san_list = NULL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387
388 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 *q++ = '\0';
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200395 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200397 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200401 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200403 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200405 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200407 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200409 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200411 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100417 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
418 mbedtls_printf("Invalid attempt to set the serial more than once\n");
419 goto usage;
420 }
421 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200422 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100423 } else if (strcmp(p, "serial_hex") == 0) {
424 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
425 mbedtls_printf("Invalid attempt to set the serial more than once\n");
426 goto usage;
427 }
428 serial_frmt = SERIAL_FRMT_HEX;
429 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 } else if (strcmp(p, "authority_identifier") == 0) {
431 opt.authority_identifier = atoi(q);
432 if (opt.authority_identifier != 0 &&
433 opt.authority_identifier != 1) {
434 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100435 goto usage;
436 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else if (strcmp(p, "subject_identifier") == 0) {
438 opt.subject_identifier = atoi(q);
439 if (opt.subject_identifier != 0 &&
440 opt.subject_identifier != 1) {
441 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100442 goto usage;
443 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 } else if (strcmp(p, "basic_constraints") == 0) {
445 opt.basic_constraints = atoi(q);
446 if (opt.basic_constraints != 0 &&
447 opt.basic_constraints != 1) {
448 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100449 goto usage;
450 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200452 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_md_info_from_string(q);
454 if (md_info == NULL) {
455 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100456 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100457 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 opt.md = mbedtls_md_get_type(md_info);
459 } else if (strcmp(p, "version") == 0) {
460 opt.version = atoi(q);
461 if (opt.version < 1 || opt.version > 3) {
462 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100463 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100464 }
Hanno Becker38eff432017-09-22 15:38:20 +0100465 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (strcmp(p, "selfsign") == 0) {
467 opt.selfsign = atoi(q);
468 if (opt.selfsign < 0 || opt.selfsign > 1) {
469 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200470 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100471 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else if (strcmp(p, "is_ca") == 0) {
473 opt.is_ca = atoi(q);
474 if (opt.is_ca < 0 || opt.is_ca > 1) {
475 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200476 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100477 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 } else if (strcmp(p, "max_pathlen") == 0) {
479 opt.max_pathlen = atoi(q);
480 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
481 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200482 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else if (strcmp(p, "key_usage") == 0) {
485 while (q != NULL) {
486 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100495 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100497 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100499 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 } else {
505 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200506 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100507 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200508
509 q = r;
510 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100512 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 while (q != NULL) {
515 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100516 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100520 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (strcmp(q, "serverAuth") == 0) {
522 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
523 } else if (strcmp(q, "clientAuth") == 0) {
524 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
525 } else if (strcmp(q, "codeSigning") == 0) {
526 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
527 } else if (strcmp(q, "emailProtection") == 0) {
528 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
529 } else if (strcmp(q, "timeStamping") == 0) {
530 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
531 } else if (strcmp(q, "OCSPSigning") == 0) {
532 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
Pengyu Lvb0786072023-05-18 17:18:36 +0800533 } else if (strcmp(q, "any") == 0) {
534 SET_OID(ext_key_usage->buf, MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 } else {
536 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100537 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100538 }
Dave Rodgman64937852022-08-15 14:12:25 +0100539
540 *tail = ext_key_usage;
541 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100542
543 q = r;
544 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400545 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400546 char *subtype_value;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400547 prev = NULL;
548
549 while (q != NULL) {
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400550 char *semicolon;
551 r = q;
552
553 /* Find the first non-escaped ; occurrence and remove escaped ones */
554 do {
555 if ((semicolon = strchr(r, ';')) != NULL) {
556 if (*(semicolon-1) != '\\') {
557 r = semicolon;
558 break;
559 }
560 /* Remove the escape character */
561 size_t size_left = strlen(semicolon);
562 memmove(semicolon-1, semicolon, size_left);
563 *(semicolon + size_left - 1) = '\0';
564 /* r will now point at the character after the semicolon */
565 r = semicolon;
566 }
567
568 } while (semicolon != NULL);
569
570 if (semicolon != NULL) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400571 *r++ = '\0';
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400572 } else {
573 r = NULL;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400574 }
575
576 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
577 if (cur == NULL) {
578 mbedtls_printf("Not enough memory for subjectAltName list\n");
579 goto usage;
580 }
581
582 cur->next = NULL;
583
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400584 if ((subtype_value = strchr(q, ':')) != NULL) {
585 *subtype_value++ = '\0';
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100586 } else {
Waleed Elmelegy58674652023-10-13 10:03:12 +0100587 mbedtls_printf(
588 "Invalid argument for option SAN: Entry should be separated by a colon\n");
Waleed Elmelegy476c1192023-10-12 14:19:25 +0100589 goto usage;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400590 }
591 if (strcmp(q, "RFC822") == 0) {
592 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
593 } else if (strcmp(q, "URI") == 0) {
594 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
595 } else if (strcmp(q, "DNS") == 0) {
596 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
597 } else if (strcmp(q, "IP") == 0) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400598 size_t ip_len = 0;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400599 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400600 ip_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
601 if (ip_len == 0) {
602 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
603 subtype_value);
604 goto exit;
605 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400606 cur->node.san.unstructured_name.p = (unsigned char *) ip;
607 cur->node.san.unstructured_name.len = sizeof(ip);
608 } else if (strcmp(q, "DN") == 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400609 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
610 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
Andrzej Kurek5eebfb82023-04-27 07:50:56 -0400611 subtype_value)) != 0) {
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400612 mbedtls_strerror(ret, buf, sizeof(buf));
613 mbedtls_printf(
614 " failed\n ! mbedtls_x509_string_to_names "
615 "returned -0x%04x - %s\n\n",
616 (unsigned int) -ret, buf);
617 goto exit;
618 }
619 cur->node.san.directory_name = *ext_san_dirname;
620 } else {
621 mbedtls_free(cur);
622 goto usage;
623 }
624
Andrzej Kurekf994bc52023-06-02 05:10:17 -0400625 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
626 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
627 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400628 q = subtype_value;
629 cur->node.san.unstructured_name.p = (unsigned char *) q;
630 cur->node.san.unstructured_name.len = strlen(q);
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400631 }
632
633 if (prev == NULL) {
634 opt.san_list = cur;
635 } else {
636 prev->next = cur;
637 }
638
639 prev = cur;
640 q = r;
641 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 } else if (strcmp(p, "ns_cert_type") == 0) {
643 while (q != NULL) {
644 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100651 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100657 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100659 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100661 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 } else {
663 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200664 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100665 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200666
667 q = r;
668 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 } else if (strcmp(p, "format") == 0) {
670 if (strcmp(q, "der") == 0) {
671 opt.format = FORMAT_DER;
672 } else if (strcmp(q, "pem") == 0) {
673 opt.format = FORMAT_PEM;
674 } else {
675 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100676 goto usage;
677 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200679 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200681 }
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200684
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200685 /*
686 * 0. Seed the PRNG
687 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 mbedtls_printf(" . Seeding the random number generator...");
689 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
692 (const unsigned char *) pers,
693 strlen(pers))) != 0) {
694 mbedtls_strerror(ret, buf, sizeof(buf));
695 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
696 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200697 goto exit;
698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200701
Paul Bakker9397dcb2013-09-06 09:55:26 +0200702 // Parse serial to MPI
703 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 mbedtls_printf(" . Reading serial number...");
705 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200706
Valerio Setti791bbe62023-01-11 10:40:18 +0100707 if (serial_frmt == SERIAL_FRMT_HEX) {
708 ret = mbedtls_test_unhexify(serial, sizeof(serial),
709 opt.serial_hex, &serial_len);
710 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
711 ret = parse_serial_decimal_format(serial, sizeof(serial),
712 opt.serial, &serial_len);
713 }
714
715 if (ret != 0) {
716 mbedtls_printf(" failed\n ! Unable to parse serial\n");
717 goto exit;
718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200721
Paul Bakker1014e952013-09-09 13:59:42 +0200722 // Parse issuer certificate if present
723 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200725 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200726 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_printf(" . Loading the issuer certificate ...");
729 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
732 mbedtls_strerror(ret, buf, sizeof(buf));
733 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
734 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200735 goto exit;
736 }
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
739 &issuer_crt.subject);
740 if (ret < 0) {
741 mbedtls_strerror(ret, buf, sizeof(buf));
742 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
743 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200744 goto exit;
745 }
746
Paul Bakkere2673fb2013-09-09 15:52:07 +0200747 opt.issuer_name = issuer_name;
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200753 // Parse certificate request if present
754 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200756 /*
757 * 1.0.b. Load the CSR
758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_printf(" . Loading the certificate request ...");
760 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
763 mbedtls_strerror(ret, buf, sizeof(buf));
764 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
765 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200766 goto exit;
767 }
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
770 &csr.subject);
771 if (ret < 0) {
772 mbedtls_strerror(ret, buf, sizeof(buf));
773 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
774 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200775 goto exit;
776 }
777
778 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200779 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200784
785 /*
786 * 1.1. Load the keys
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (!opt.selfsign && !strlen(opt.request_file)) {
789 mbedtls_printf(" . Loading the subject key ...");
790 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
793 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
794 if (ret != 0) {
795 mbedtls_strerror(ret, buf, sizeof(buf));
796 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
797 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200798 goto exit;
799 }
Paul Bakker1014e952013-09-09 13:59:42 +0200800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200802 }
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 mbedtls_printf(" . Loading the issuer key ...");
805 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
808 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
809 if (ret != 0) {
810 mbedtls_strerror(ret, buf, sizeof(buf));
811 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
812 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200813 goto exit;
814 }
815
816 // Check if key and issuer certificate match
817 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (strlen(opt.issuer_crt)) {
819 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
820 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
821 mbedtls_printf(" failed\n ! issuer_key does not match "
822 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200823 goto exit;
824 }
825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100830 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200831 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
835 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200836
Paul Bakker9397dcb2013-09-06 09:55:26 +0200837 /*
838 * 1.0. Check the names for validity
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
841 mbedtls_strerror(ret, buf, sizeof(buf));
842 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
843 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200844 goto exit;
845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
848 mbedtls_strerror(ret, buf, sizeof(buf));
849 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
850 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200851 goto exit;
852 }
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 mbedtls_printf(" . Setting certificate values ...");
855 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_x509write_crt_set_version(&crt, opt.version);
858 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100859
Valerio Settiaf4815c2023-01-26 17:43:09 +0100860 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100861 if (ret != 0) {
862 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100863 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100864 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
865 goto exit;
866 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
869 if (ret != 0) {
870 mbedtls_strerror(ret, buf, sizeof(buf));
871 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
872 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200873 goto exit;
874 }
875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
879 opt.basic_constraints != 0) {
880 mbedtls_printf(" . Adding the Basic Constraints extension ...");
881 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
884 opt.max_pathlen);
885 if (ret != 0) {
886 mbedtls_strerror(ret, buf, sizeof(buf));
887 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
888 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100889 goto exit;
890 }
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100893 }
Paul Bakker15162a02013-09-06 19:27:21 +0200894
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100895#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
897 opt.subject_identifier != 0) {
898 mbedtls_printf(" . Adding the Subject Key Identifier ...");
899 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
902 if (ret != 0) {
903 mbedtls_strerror(ret, buf, sizeof(buf));
904 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
905 "_key_identifier returned -0x%04x - %s\n\n",
906 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100907 goto exit;
908 }
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200911 }
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
914 opt.authority_identifier != 0) {
915 mbedtls_printf(" . Adding the Authority Key Identifier ...");
916 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
919 if (ret != 0) {
920 mbedtls_strerror(ret, buf, sizeof(buf));
921 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
922 "key_identifier returned -0x%04x - %s\n\n",
923 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100924 goto exit;
925 }
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100928 }
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100929#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker15162a02013-09-06 19:27:21 +0200930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
932 opt.key_usage != 0) {
933 mbedtls_printf(" . Adding the Key Usage extension ...");
934 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
937 if (ret != 0) {
938 mbedtls_strerror(ret, buf, sizeof(buf));
939 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
940 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200941 goto exit;
942 }
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200945 }
946
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400947 if (opt.san_list != NULL) {
948 ret = mbedtls_x509write_crt_set_subject_alternative_name(&crt, opt.san_list);
949
950 if (ret != 0) {
951 mbedtls_printf(
Andrzej Kurek1bc7df22023-04-04 07:09:04 -0400952 " failed\n ! mbedtls_x509write_crt_set_subject_alternative_name returned %d",
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400953 ret);
954 goto exit;
955 }
956 }
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (opt.ext_key_usage) {
959 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
960 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
963 if (ret != 0) {
964 mbedtls_strerror(ret, buf, sizeof(buf));
965 mbedtls_printf(
966 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
967 (unsigned int) -ret,
968 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100969 goto exit;
970 }
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100973 }
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
976 opt.ns_cert_type != 0) {
977 mbedtls_printf(" . Adding the NS Cert Type extension ...");
978 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
981 if (ret != 0) {
982 mbedtls_strerror(ret, buf, sizeof(buf));
983 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
984 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200985 goto exit;
986 }
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200989 }
990
Paul Bakker9397dcb2013-09-06 09:55:26 +0200991 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100992 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_printf(" . Writing the certificate...");
995 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if ((ret = write_certificate(&crt, opt.output_file,
998 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
999 mbedtls_strerror(ret, buf, sizeof(buf));
1000 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
1001 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +02001002 goto exit;
1003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +02001006
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +01001007 exit_code = MBEDTLS_EXIT_SUCCESS;
1008
Paul Bakker9397dcb2013-09-06 09:55:26 +02001009exit:
Hanno Becker30a95102018-10-05 09:49:33 +01001010#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +01001012#endif /* MBEDTLS_X509_CSR_PARSE_C */
Andrzej Kurek5da1d752023-04-05 08:30:59 -04001013 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_x509_crt_free(&issuer_crt);
1015 mbedtls_x509write_crt_free(&crt);
1016 mbedtls_pk_free(&loaded_subject_key);
1017 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_ctr_drbg_free(&ctr_drbg);
1019 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +02001020#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +02001021 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +02001022#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +02001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +02001025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
1027 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +01001028 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */