blob: ea20144e95de2dca4a08e6b71f8baae8bb42264e [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Simon Butcher203a6932016-10-07 15:00:17 +010028#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
29 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
30 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
31 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
32 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020034{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
36 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
37 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
38 "MBEDTLS_ERROR_C not defined.\n");
39 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020040}
41#else
42
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/x509_crt.h"
44#include "mbedtls/x509_csr.h"
45#include "mbedtls/entropy.h"
46#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010047#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020049
Rich Evans18b78c72015-02-11 14:06:19 +000050#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000055#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010056 " request_file=%%s default: (empty)\n" \
57 " If request_file is specified, subject_key,\n" \
58 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000059#else
60#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000062
Paul Bakker1014e952013-09-09 13:59:42 +020063#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020064#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020065#define DFL_SUBJECT_KEY "subject.key"
66#define DFL_ISSUER_KEY "ca.key"
67#define DFL_SUBJECT_PWD ""
68#define DFL_ISSUER_PWD ""
69#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000070#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
71#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020072#define DFL_NOT_BEFORE "20010101000000"
73#define DFL_NOT_AFTER "20301231235959"
74#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020075#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020076#define DFL_IS_CA 0
77#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_KEY_USAGE 0
79#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010080#define DFL_VERSION 3
81#define DFL_AUTH_IDENT 1
82#define DFL_SUBJ_IDENT 1
83#define DFL_CONSTRAINTS 1
84#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020085
Rich Evans18b78c72015-02-11 14:06:19 +000086#define USAGE \
87 "\n usage: cert_write param=<>...\n" \
88 "\n acceptable parameters:\n" \
89 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010090 " subject_key=%%s default: subject.key\n" \
91 " subject_pwd=%%s default: (empty)\n" \
92 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000093 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010094 " issuer_crt=%%s default: (empty)\n" \
95 " If issuer_crt is specified, issuer_name is\n" \
96 " ignored!\n" \
97 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000098 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010099 " selfsign=%%d default: 0 (false)\n" \
100 " If selfsign is enabled, issuer_name and\n" \
101 " issuer_key are required (issuer_crt and\n" \
102 " subject_* are ignored\n" \
103 " issuer_key=%%s default: ca.key\n" \
104 " issuer_pwd=%%s default: (empty)\n" \
105 " output_file=%%s default: cert.crt\n" \
106 " serial=%%s default: 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 " not_before=%%s default: 20010101000000\n" \
108 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100109 " is_ca=%%d default: 0 (disabled)\n" \
110 " max_pathlen=%%d default: -1 (none)\n" \
111 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200112 " Supported values (if enabled):\n" \
113 " MD2, MD4, MD5, RIPEMD160, SHA1,\n" \
114 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100115 " version=%%d default: 3\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100117 " subject_identifier=%%s default: 1\n" \
118 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100120 " authority_identifier=%%s default: 1\n" \
121 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100123 " basic_constraints=%%d default: 1\n" \
124 " Possible values: 0, 1\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " key_usage=%%s default: (empty)\n" \
127 " Comma-separated-list of values:\n" \
128 " digital_signature\n" \
129 " non_repudiation\n" \
130 " key_encipherment\n" \
131 " data_encipherment\n" \
132 " key_agreement\n" \
133 " key_cert_sign\n" \
134 " crl_sign\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100136 " ns_cert_type=%%s default: (empty)\n" \
137 " Comma-separated-list of values:\n" \
138 " ssl_client\n" \
139 " ssl_server\n" \
140 " email\n" \
141 " object_signing\n" \
142 " ssl_ca\n" \
143 " email_ca\n" \
144 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000145 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000146
Simon Butcher63cb97e2018-12-06 17:43:31 +0000147
Paul Bakker9397dcb2013-09-06 09:55:26 +0200148/*
149 * global options
150 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100152 const char *issuer_crt; /* filename of the issuer certificate */
153 const char *request_file; /* filename of the certificate request */
154 const char *subject_key; /* filename of the subject key file */
155 const char *issuer_key; /* filename of the issuer key file */
156 const char *subject_pwd; /* password for the subject key file */
157 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100158 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100159 const char *subject_name; /* subject name for certificate */
160 const char *issuer_name; /* issuer name for certificate */
161 const char *not_before; /* validity period not before */
162 const char *not_after; /* validity period not after */
163 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200164 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200165 int is_ca; /* is a CA certificate */
166 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100167 int authority_identifier; /* add authority identifier to CRT */
168 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100169 int basic_constraints; /* add basic constraints ext to CRT */
170 int version; /* CRT version */
171 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200172 unsigned char key_usage; /* key usage flags */
173 unsigned char ns_cert_type; /* NS cert type */
174} opt;
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
177 int (*f_rng)(void *, unsigned char *, size_t),
178 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200179{
180 int ret;
181 FILE *f;
182 unsigned char output_buf[4096];
183 size_t len = 0;
184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 memset(output_buf, 0, 4096);
186 if ((ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
187 f_rng, p_rng)) < 0) {
188 return ret;
Paul Bakker0c226102014-04-17 16:02:36 +0200189 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 len = strlen((char *) output_buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if ((f = fopen(output_file, "w")) == NULL) {
194 return -1;
195 }
196
197 if (fwrite(output_buf, 1, len, f) != len) {
198 fclose(f);
199 return -1;
200 }
201
202 fclose(f);
203
204 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205}
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100209 int ret = 1;
210 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_x509_crt issuer_crt;
212 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
213 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200216 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100217 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200220 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_x509write_cert crt;
224 mbedtls_mpi serial;
225 mbedtls_entropy_context entropy;
226 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200227 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228
229 /*
230 * Set to sane values
231 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 mbedtls_x509write_crt_init(&crt);
233 mbedtls_pk_init(&loaded_issuer_key);
234 mbedtls_pk_init(&loaded_subject_key);
235 mbedtls_mpi_init(&serial);
236 mbedtls_ctr_drbg_init(&ctr_drbg);
237 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200240#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 mbedtls_x509_crt_init(&issuer_crt);
242 memset(buf, 0, 1024);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000244 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245usage:
246 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200247 goto exit;
248 }
249
Paul Bakker1014e952013-09-09 13:59:42 +0200250 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200251 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252 opt.subject_key = DFL_SUBJECT_KEY;
253 opt.issuer_key = DFL_ISSUER_KEY;
254 opt.subject_pwd = DFL_SUBJECT_PWD;
255 opt.issuer_pwd = DFL_ISSUER_PWD;
256 opt.output_file = DFL_OUTPUT_FILENAME;
257 opt.subject_name = DFL_SUBJECT_NAME;
258 opt.issuer_name = DFL_ISSUER_NAME;
259 opt.not_before = DFL_NOT_BEFORE;
260 opt.not_after = DFL_NOT_AFTER;
261 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200262 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200263 opt.is_ca = DFL_IS_CA;
264 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200265 opt.key_usage = DFL_KEY_USAGE;
266 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100267 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100268 opt.md = DFL_DIGEST;
269 opt.subject_identifier = DFL_SUBJ_IDENT;
270 opt.authority_identifier = DFL_AUTH_IDENT;
271 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274
275 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200277 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200279 *q++ = '\0';
280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200282 opt.request_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200284 opt.subject_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200286 opt.issuer_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200288 opt.subject_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200290 opt.issuer_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200292 opt.issuer_crt = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200296 opt.subject_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200298 opt.issuer_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200300 opt.not_before = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200302 opt.not_after = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200304 opt.serial = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 } else if (strcmp(p, "authority_identifier") == 0) {
306 opt.authority_identifier = atoi(q);
307 if (opt.authority_identifier != 0 &&
308 opt.authority_identifier != 1) {
309 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100310 goto usage;
311 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 } else if (strcmp(p, "subject_identifier") == 0) {
313 opt.subject_identifier = atoi(q);
314 if (opt.subject_identifier != 0 &&
315 opt.subject_identifier != 1) {
316 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100317 goto usage;
318 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 } else if (strcmp(p, "basic_constraints") == 0) {
320 opt.basic_constraints = atoi(q);
321 if (opt.basic_constraints != 0 &&
322 opt.basic_constraints != 1) {
323 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100324 goto usage;
325 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200327 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 mbedtls_md_info_from_string(q);
329 if (md_info == NULL) {
330 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100331 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100332 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 opt.md = mbedtls_md_get_type(md_info);
334 } else if (strcmp(p, "version") == 0) {
335 opt.version = atoi(q);
336 if (opt.version < 1 || opt.version > 3) {
337 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100338 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100339 }
Hanno Becker38eff432017-09-22 15:38:20 +0100340 opt.version--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 } else if (strcmp(p, "selfsign") == 0) {
342 opt.selfsign = atoi(q);
343 if (opt.selfsign < 0 || opt.selfsign > 1) {
344 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200345 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100346 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 } else if (strcmp(p, "is_ca") == 0) {
348 opt.is_ca = atoi(q);
349 if (opt.is_ca < 0 || opt.is_ca > 1) {
350 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200351 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100352 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 } else if (strcmp(p, "max_pathlen") == 0) {
354 opt.max_pathlen = atoi(q);
355 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
356 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200357 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100358 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 } else if (strcmp(p, "key_usage") == 0) {
360 while (q != NULL) {
361 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200362 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100370 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100372 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100374 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 } else {
380 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200381 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100382 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200383
384 q = r;
385 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 } else if (strcmp(p, "ns_cert_type") == 0) {
387 while (q != NULL) {
388 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100395 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100401 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100403 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100405 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 } else {
407 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200408 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100409 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200410
411 q = r;
412 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200414 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416 }
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200419
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200420 /*
421 * 0. Seed the PRNG
422 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 mbedtls_printf(" . Seeding the random number generator...");
424 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
427 (const unsigned char *) pers,
428 strlen(pers))) != 0) {
429 mbedtls_strerror(ret, buf, 1024);
430 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
431 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200432 goto exit;
433 }
434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200436
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 // Parse serial to MPI
438 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 mbedtls_printf(" . Reading serial number...");
440 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) {
443 mbedtls_strerror(ret, buf, 1024);
444 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string "
445 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 goto exit;
447 }
448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200450
Paul Bakker1014e952013-09-09 13:59:42 +0200451 // Parse issuer certificate if present
452 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200454 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200455 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200456 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 mbedtls_printf(" . Loading the issuer certificate ...");
458 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
461 mbedtls_strerror(ret, buf, 1024);
462 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
463 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200464 goto exit;
465 }
466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
468 &issuer_crt.subject);
469 if (ret < 0) {
470 mbedtls_strerror(ret, buf, 1024);
471 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
472 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200473 goto exit;
474 }
475
Paul Bakkere2673fb2013-09-09 15:52:07 +0200476 opt.issuer_name = issuer_name;
477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200479 }
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200482 // Parse certificate request if present
483 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200485 /*
486 * 1.0.b. Load the CSR
487 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 mbedtls_printf(" . Loading the certificate request ...");
489 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
492 mbedtls_strerror(ret, buf, 1024);
493 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
494 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200495 goto exit;
496 }
497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
499 &csr.subject);
500 if (ret < 0) {
501 mbedtls_strerror(ret, buf, 1024);
502 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
503 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200504 goto exit;
505 }
506
507 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200508 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200511 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200513
514 /*
515 * 1.1. Load the keys
516 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 if (!opt.selfsign && !strlen(opt.request_file)) {
518 mbedtls_printf(" . Loading the subject key ...");
519 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
522 opt.subject_pwd);
523 if (ret != 0) {
524 mbedtls_strerror(ret, buf, 1024);
525 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
526 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200527 goto exit;
528 }
Paul Bakker1014e952013-09-09 13:59:42 +0200529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200531 }
532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 mbedtls_printf(" . Loading the issuer key ...");
534 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
537 opt.issuer_pwd);
538 if (ret != 0) {
539 mbedtls_strerror(ret, buf, 1024);
540 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
541 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200542 goto exit;
543 }
544
545 // Check if key and issuer certificate match
546 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 if (strlen(opt.issuer_crt)) {
548 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key) != 0) {
549 mbedtls_printf(" failed\n ! issuer_key does not match "
550 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200551 goto exit;
552 }
553 }
554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100558 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200559 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200560 }
561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
563 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200564
Paul Bakker9397dcb2013-09-06 09:55:26 +0200565 /*
566 * 1.0. Check the names for validity
567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
569 mbedtls_strerror(ret, buf, 1024);
570 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
571 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200572 goto exit;
573 }
574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
576 mbedtls_strerror(ret, buf, 1024);
577 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
578 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200579 goto exit;
580 }
581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 mbedtls_printf(" . Setting certificate values ...");
583 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 mbedtls_x509write_crt_set_version(&crt, opt.version);
586 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
589 if (ret != 0) {
590 mbedtls_strerror(ret, buf, 1024);
591 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial "
592 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200593 goto exit;
594 }
595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
597 if (ret != 0) {
598 mbedtls_strerror(ret, buf, 1024);
599 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
600 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200601 goto exit;
602 }
603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
607 opt.basic_constraints != 0) {
608 mbedtls_printf(" . Adding the Basic Constraints extension ...");
609 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
612 opt.max_pathlen);
613 if (ret != 0) {
614 mbedtls_strerror(ret, buf, 1024);
615 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
616 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100617 goto exit;
618 }
619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100621 }
Paul Bakker15162a02013-09-06 19:27:21 +0200622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
625 opt.subject_identifier != 0) {
626 mbedtls_printf(" . Adding the Subject Key Identifier ...");
627 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
630 if (ret != 0) {
631 mbedtls_strerror(ret, buf, 1024);
632 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
633 "_key_identifier returned -0x%04x - %s\n\n",
634 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100635 goto exit;
636 }
637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200639 }
640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
642 opt.authority_identifier != 0) {
643 mbedtls_printf(" . Adding the Authority Key Identifier ...");
644 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
647 if (ret != 0) {
648 mbedtls_strerror(ret, buf, 1024);
649 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
650 "key_identifier returned -0x%04x - %s\n\n",
651 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100652 goto exit;
653 }
654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
660 opt.key_usage != 0) {
661 mbedtls_printf(" . Adding the Key Usage extension ...");
662 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
665 if (ret != 0) {
666 mbedtls_strerror(ret, buf, 1024);
667 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
668 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200669 goto exit;
670 }
671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200673 }
674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
676 opt.ns_cert_type != 0) {
677 mbedtls_printf(" . Adding the NS Cert Type extension ...");
678 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
681 if (ret != 0) {
682 mbedtls_strerror(ret, buf, 1024);
683 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
684 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200685 goto exit;
686 }
687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200689 }
690
Paul Bakker9397dcb2013-09-06 09:55:26 +0200691 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100692 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200693 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 mbedtls_printf(" . Writing the certificate...");
695 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 if ((ret = write_certificate(&crt, opt.output_file,
698 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
699 mbedtls_strerror(ret, buf, 1024);
700 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
701 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200702 goto exit;
703 }
704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200706
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100707 exit_code = MBEDTLS_EXIT_SUCCESS;
708
Paul Bakker9397dcb2013-09-06 09:55:26 +0200709exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100710#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100712#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 mbedtls_x509_crt_free(&issuer_crt);
714 mbedtls_x509write_crt_free(&crt);
715 mbedtls_pk_free(&loaded_subject_key);
716 mbedtls_pk_free(&loaded_issuer_key);
717 mbedtls_mpi_free(&serial);
718 mbedtls_ctr_drbg_free(&ctr_drbg);
719 mbedtls_entropy_free(&entropy);
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 */