blob: 3f04c6d5d34eb1aa2b78ea85319624e033e1cf1e [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 Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker9397dcb2013-09-06 09:55:26 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.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) || \
19 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
20 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020022{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
24 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
25 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
26 "MBEDTLS_ERROR_C not defined.\n");
27 mbedtls_exit(0);
Manuel Pégourié-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"
33#include "mbedtls/entropy.h"
34#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010035#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020037
Rich Evans18b78c72015-02-11 14:06:19 +000038#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000043#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010044 " request_file=%%s default: (empty)\n" \
45 " If request_file is specified, subject_key,\n" \
46 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000047#else
48#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000050
Paul Bakker1014e952013-09-09 13:59:42 +020051#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020052#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020053#define DFL_SUBJECT_KEY "subject.key"
54#define DFL_ISSUER_KEY "ca.key"
55#define DFL_SUBJECT_PWD ""
56#define DFL_ISSUER_PWD ""
57#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000058#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
59#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020060#define DFL_NOT_BEFORE "20010101000000"
61#define DFL_NOT_AFTER "20301231235959"
62#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020063#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020064#define DFL_IS_CA 0
65#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020066#define DFL_KEY_USAGE 0
67#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010068#define DFL_VERSION 3
69#define DFL_AUTH_IDENT 1
70#define DFL_SUBJ_IDENT 1
71#define DFL_CONSTRAINTS 1
72#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020073
Rich Evans18b78c72015-02-11 14:06:19 +000074#define USAGE \
75 "\n usage: cert_write param=<>...\n" \
76 "\n acceptable parameters:\n" \
77 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010078 " subject_key=%%s default: subject.key\n" \
79 " subject_pwd=%%s default: (empty)\n" \
80 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000081 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010082 " issuer_crt=%%s default: (empty)\n" \
83 " If issuer_crt is specified, issuer_name is\n" \
84 " ignored!\n" \
85 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000086 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010087 " selfsign=%%d default: 0 (false)\n" \
88 " If selfsign is enabled, issuer_name and\n" \
89 " issuer_key are required (issuer_crt and\n" \
90 " subject_* are ignored\n" \
91 " issuer_key=%%s default: ca.key\n" \
92 " issuer_pwd=%%s default: (empty)\n" \
93 " output_file=%%s default: cert.crt\n" \
94 " serial=%%s default: 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 " not_before=%%s default: 20010101000000\n" \
96 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010097 " is_ca=%%d default: 0 (disabled)\n" \
98 " max_pathlen=%%d default: -1 (none)\n" \
99 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200100 " Supported values (if enabled):\n" \
101 " MD2, MD4, MD5, RIPEMD160, SHA1,\n" \
102 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100103 " version=%%d default: 3\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " subject_identifier=%%s default: 1\n" \
106 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100108 " authority_identifier=%%s default: 1\n" \
109 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100111 " basic_constraints=%%d default: 1\n" \
112 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100114 " key_usage=%%s default: (empty)\n" \
115 " Comma-separated-list of values:\n" \
116 " digital_signature\n" \
117 " non_repudiation\n" \
118 " key_encipherment\n" \
119 " data_encipherment\n" \
120 " key_agreement\n" \
121 " key_cert_sign\n" \
122 " crl_sign\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100124 " ns_cert_type=%%s default: (empty)\n" \
125 " Comma-separated-list of values:\n" \
126 " ssl_client\n" \
127 " ssl_server\n" \
128 " email\n" \
129 " object_signing\n" \
130 " ssl_ca\n" \
131 " email_ca\n" \
132 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000133 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000134
Simon Butcher63cb97e2018-12-06 17:43:31 +0000135
Paul Bakker9397dcb2013-09-06 09:55:26 +0200136/*
137 * global options
138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100140 const char *issuer_crt; /* filename of the issuer certificate */
141 const char *request_file; /* filename of the certificate request */
142 const char *subject_key; /* filename of the subject key file */
143 const char *issuer_key; /* filename of the issuer key file */
144 const char *subject_pwd; /* password for the subject key file */
145 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100146 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100147 const char *subject_name; /* subject name for certificate */
148 const char *issuer_name; /* issuer name for certificate */
149 const char *not_before; /* validity period not before */
150 const char *not_after; /* validity period not after */
151 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200152 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200153 int is_ca; /* is a CA certificate */
154 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100155 int authority_identifier; /* add authority identifier to CRT */
156 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100157 int basic_constraints; /* add basic constraints ext to CRT */
158 int version; /* CRT version */
159 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200160 unsigned char key_usage; /* key usage flags */
161 unsigned char ns_cert_type; /* NS cert type */
162} opt;
163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
165 int (*f_rng)(void *, unsigned char *, size_t),
166 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167{
168 int ret;
169 FILE *f;
170 unsigned char output_buf[4096];
171 size_t len = 0;
172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 memset(output_buf, 0, 4096);
174 if ((ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
175 f_rng, p_rng)) < 0) {
176 return ret;
Paul Bakker0c226102014-04-17 16:02:36 +0200177 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 len = strlen((char *) output_buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if ((f = fopen(output_file, "w")) == NULL) {
182 return -1;
183 }
184
185 if (fwrite(output_buf, 1, len, f) != len) {
186 fclose(f);
187 return -1;
188 }
189
190 fclose(f);
191
192 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200193}
194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100197 int ret = 1;
198 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_x509_crt issuer_crt;
200 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
201 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200204 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100205 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200208 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_x509write_cert crt;
212 mbedtls_mpi serial;
213 mbedtls_entropy_context entropy;
214 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200215 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216
217 /*
218 * Set to sane values
219 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_x509write_crt_init(&crt);
221 mbedtls_pk_init(&loaded_issuer_key);
222 mbedtls_pk_init(&loaded_subject_key);
223 mbedtls_mpi_init(&serial);
224 mbedtls_ctr_drbg_init(&ctr_drbg);
225 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200228#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 mbedtls_x509_crt_init(&issuer_crt);
230 memset(buf, 0, 1024);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231
Przemek Stekielb00688f2023-04-17 11:10:05 +0200232#if defined(MBEDTLS_USE_PSA_CRYPTO)
233 psa_status_t status = psa_crypto_init();
234 if (status != PSA_SUCCESS) {
235 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
236 (int) status);
237 goto exit;
238 }
239#endif /* MBEDTLS_USE_PSA_CRYPTO */
240
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000241 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242usage:
243 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 goto exit;
245 }
246
Paul Bakker1014e952013-09-09 13:59:42 +0200247 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200248 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 opt.subject_key = DFL_SUBJECT_KEY;
250 opt.issuer_key = DFL_ISSUER_KEY;
251 opt.subject_pwd = DFL_SUBJECT_PWD;
252 opt.issuer_pwd = DFL_ISSUER_PWD;
253 opt.output_file = DFL_OUTPUT_FILENAME;
254 opt.subject_name = DFL_SUBJECT_NAME;
255 opt.issuer_name = DFL_ISSUER_NAME;
256 opt.not_before = DFL_NOT_BEFORE;
257 opt.not_after = DFL_NOT_AFTER;
258 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200259 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200260 opt.is_ca = DFL_IS_CA;
261 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 opt.key_usage = DFL_KEY_USAGE;
263 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100264 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100265 opt.md = DFL_DIGEST;
266 opt.subject_identifier = DFL_SUBJ_IDENT;
267 opt.authority_identifier = DFL_AUTH_IDENT;
268 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271
272 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276 *q++ = '\0';
277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200279 opt.request_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200281 opt.subject_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200283 opt.issuer_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200285 opt.subject_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.issuer_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200289 opt.issuer_crt = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200291 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293 opt.subject_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295 opt.issuer_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297 opt.not_before = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 opt.not_after = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200301 opt.serial = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 } else if (strcmp(p, "authority_identifier") == 0) {
303 opt.authority_identifier = atoi(q);
304 if (opt.authority_identifier != 0 &&
305 opt.authority_identifier != 1) {
306 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100307 goto usage;
308 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 } else if (strcmp(p, "subject_identifier") == 0) {
310 opt.subject_identifier = atoi(q);
311 if (opt.subject_identifier != 0 &&
312 opt.subject_identifier != 1) {
313 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100314 goto usage;
315 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 } else if (strcmp(p, "basic_constraints") == 0) {
317 opt.basic_constraints = atoi(q);
318 if (opt.basic_constraints != 0 &&
319 opt.basic_constraints != 1) {
320 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100321 goto usage;
322 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200324 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 mbedtls_md_info_from_string(q);
326 if (md_info == NULL) {
327 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100328 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 opt.md = mbedtls_md_get_type(md_info);
331 } else if (strcmp(p, "version") == 0) {
332 opt.version = atoi(q);
333 if (opt.version < 1 || opt.version > 3) {
334 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100335 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100336 }
Hanno Becker38eff432017-09-22 15:38:20 +0100337 opt.version--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 } else if (strcmp(p, "selfsign") == 0) {
339 opt.selfsign = atoi(q);
340 if (opt.selfsign < 0 || opt.selfsign > 1) {
341 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200342 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100343 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 } else if (strcmp(p, "is_ca") == 0) {
345 opt.is_ca = atoi(q);
346 if (opt.is_ca < 0 || opt.is_ca > 1) {
347 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200348 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100349 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 } else if (strcmp(p, "max_pathlen") == 0) {
351 opt.max_pathlen = atoi(q);
352 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
353 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200354 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100355 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 } else if (strcmp(p, "key_usage") == 0) {
357 while (q != NULL) {
358 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200359 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100367 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100369 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100371 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 } else {
377 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200378 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100379 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200380
381 q = r;
382 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 } else if (strcmp(p, "ns_cert_type") == 0) {
384 while (q != NULL) {
385 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100392 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100398 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100400 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100402 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 } else {
404 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200405 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100406 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200407
408 q = r;
409 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200411 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 }
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200416
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200417 /*
418 * 0. Seed the PRNG
419 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 mbedtls_printf(" . Seeding the random number generator...");
421 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
424 (const unsigned char *) pers,
425 strlen(pers))) != 0) {
426 mbedtls_strerror(ret, buf, 1024);
427 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
428 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200429 goto exit;
430 }
431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200433
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434 // Parse serial to MPI
435 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 mbedtls_printf(" . Reading serial number...");
437 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) {
440 mbedtls_strerror(ret, buf, 1024);
441 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string "
442 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200443 goto exit;
444 }
445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200447
Paul Bakker1014e952013-09-09 13:59:42 +0200448 // Parse issuer certificate if present
449 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200451 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200452 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200453 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 mbedtls_printf(" . Loading the issuer certificate ...");
455 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
458 mbedtls_strerror(ret, buf, 1024);
459 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
460 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200461 goto exit;
462 }
463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
465 &issuer_crt.subject);
466 if (ret < 0) {
467 mbedtls_strerror(ret, buf, 1024);
468 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
469 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200470 goto exit;
471 }
472
Paul Bakkere2673fb2013-09-09 15:52:07 +0200473 opt.issuer_name = issuer_name;
474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200476 }
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200479 // Parse certificate request if present
480 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200482 /*
483 * 1.0.b. Load the CSR
484 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 mbedtls_printf(" . Loading the certificate request ...");
486 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
489 mbedtls_strerror(ret, buf, 1024);
490 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
491 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200492 goto exit;
493 }
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
496 &csr.subject);
497 if (ret < 0) {
498 mbedtls_strerror(ret, buf, 1024);
499 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
500 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200501 goto exit;
502 }
503
504 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200505 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200510
511 /*
512 * 1.1. Load the keys
513 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (!opt.selfsign && !strlen(opt.request_file)) {
515 mbedtls_printf(" . Loading the subject key ...");
516 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
519 opt.subject_pwd);
520 if (ret != 0) {
521 mbedtls_strerror(ret, buf, 1024);
522 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
523 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200524 goto exit;
525 }
Paul Bakker1014e952013-09-09 13:59:42 +0200526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200528 }
529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 mbedtls_printf(" . Loading the issuer key ...");
531 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
534 opt.issuer_pwd);
535 if (ret != 0) {
536 mbedtls_strerror(ret, buf, 1024);
537 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
538 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200539 goto exit;
540 }
541
542 // Check if key and issuer certificate match
543 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 if (strlen(opt.issuer_crt)) {
545 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key) != 0) {
546 mbedtls_printf(" failed\n ! issuer_key does not match "
547 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200548 goto exit;
549 }
550 }
551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100555 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200556 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200557 }
558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
560 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200561
Paul Bakker9397dcb2013-09-06 09:55:26 +0200562 /*
563 * 1.0. Check the names for validity
564 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
566 mbedtls_strerror(ret, buf, 1024);
567 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
568 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200569 goto exit;
570 }
571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
573 mbedtls_strerror(ret, buf, 1024);
574 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
575 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200576 goto exit;
577 }
578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 mbedtls_printf(" . Setting certificate values ...");
580 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 mbedtls_x509write_crt_set_version(&crt, opt.version);
583 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
586 if (ret != 0) {
587 mbedtls_strerror(ret, buf, 1024);
588 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial "
589 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200590 goto exit;
591 }
592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
594 if (ret != 0) {
595 mbedtls_strerror(ret, buf, 1024);
596 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
597 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200598 goto exit;
599 }
600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
604 opt.basic_constraints != 0) {
605 mbedtls_printf(" . Adding the Basic Constraints extension ...");
606 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
609 opt.max_pathlen);
610 if (ret != 0) {
611 mbedtls_strerror(ret, buf, 1024);
612 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
613 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100614 goto exit;
615 }
616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100618 }
Paul Bakker15162a02013-09-06 19:27:21 +0200619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
622 opt.subject_identifier != 0) {
623 mbedtls_printf(" . Adding the Subject Key Identifier ...");
624 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
627 if (ret != 0) {
628 mbedtls_strerror(ret, buf, 1024);
629 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
630 "_key_identifier returned -0x%04x - %s\n\n",
631 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100632 goto exit;
633 }
634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200636 }
637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
639 opt.authority_identifier != 0) {
640 mbedtls_printf(" . Adding the Authority Key Identifier ...");
641 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
644 if (ret != 0) {
645 mbedtls_strerror(ret, buf, 1024);
646 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
647 "key_identifier returned -0x%04x - %s\n\n",
648 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100649 goto exit;
650 }
651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
657 opt.key_usage != 0) {
658 mbedtls_printf(" . Adding the Key Usage extension ...");
659 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
662 if (ret != 0) {
663 mbedtls_strerror(ret, buf, 1024);
664 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
665 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200666 goto exit;
667 }
668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200670 }
671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
673 opt.ns_cert_type != 0) {
674 mbedtls_printf(" . Adding the NS Cert Type extension ...");
675 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
678 if (ret != 0) {
679 mbedtls_strerror(ret, buf, 1024);
680 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
681 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200682 goto exit;
683 }
684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200686 }
687
Paul Bakker9397dcb2013-09-06 09:55:26 +0200688 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100689 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200690 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 mbedtls_printf(" . Writing the certificate...");
692 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if ((ret = write_certificate(&crt, opt.output_file,
695 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
696 mbedtls_strerror(ret, buf, 1024);
697 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
698 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200699 goto exit;
700 }
701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200703
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100704 exit_code = MBEDTLS_EXIT_SUCCESS;
705
Paul Bakker9397dcb2013-09-06 09:55:26 +0200706exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100707#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100709#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 mbedtls_x509_crt_free(&issuer_crt);
711 mbedtls_x509write_crt_free(&crt);
712 mbedtls_pk_free(&loaded_subject_key);
713 mbedtls_pk_free(&loaded_issuer_key);
714 mbedtls_mpi_free(&serial);
715 mbedtls_ctr_drbg_free(&ctr_drbg);
716 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200718 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200719#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200720
721#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 mbedtls_printf(" + Press Enter to exit this program.\n");
723 fflush(stdout); getchar();
Paul Bakker9397dcb2013-09-06 09:55:26 +0200724#endif
725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
729 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100730 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */