blob: 02ff836aaf1932e697015f44bdb4eef1f92a95ae [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
Przemek Stekielb00688f2023-04-17 11:10:05 +0200244#if defined(MBEDTLS_USE_PSA_CRYPTO)
245 psa_status_t status = psa_crypto_init();
246 if (status != PSA_SUCCESS) {
247 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
248 (int) status);
249 goto exit;
250 }
251#endif /* MBEDTLS_USE_PSA_CRYPTO */
252
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000253 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254usage:
255 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200256 goto exit;
257 }
258
Paul Bakker1014e952013-09-09 13:59:42 +0200259 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200260 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200261 opt.subject_key = DFL_SUBJECT_KEY;
262 opt.issuer_key = DFL_ISSUER_KEY;
263 opt.subject_pwd = DFL_SUBJECT_PWD;
264 opt.issuer_pwd = DFL_ISSUER_PWD;
265 opt.output_file = DFL_OUTPUT_FILENAME;
266 opt.subject_name = DFL_SUBJECT_NAME;
267 opt.issuer_name = DFL_ISSUER_NAME;
268 opt.not_before = DFL_NOT_BEFORE;
269 opt.not_after = DFL_NOT_AFTER;
270 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200271 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200272 opt.is_ca = DFL_IS_CA;
273 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.key_usage = DFL_KEY_USAGE;
275 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100276 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100277 opt.md = DFL_DIGEST;
278 opt.subject_identifier = DFL_SUBJ_IDENT;
279 opt.authority_identifier = DFL_AUTH_IDENT;
280 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200283
284 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200286 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200288 *q++ = '\0';
289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200291 opt.request_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293 opt.subject_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295 opt.issuer_key = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297 opt.subject_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 opt.issuer_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200301 opt.issuer_crt = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200305 opt.subject_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 opt.issuer_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200309 opt.not_before = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200311 opt.not_after = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200313 opt.serial = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 } else if (strcmp(p, "authority_identifier") == 0) {
315 opt.authority_identifier = atoi(q);
316 if (opt.authority_identifier != 0 &&
317 opt.authority_identifier != 1) {
318 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100319 goto usage;
320 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 } else if (strcmp(p, "subject_identifier") == 0) {
322 opt.subject_identifier = atoi(q);
323 if (opt.subject_identifier != 0 &&
324 opt.subject_identifier != 1) {
325 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100326 goto usage;
327 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 } else if (strcmp(p, "basic_constraints") == 0) {
329 opt.basic_constraints = atoi(q);
330 if (opt.basic_constraints != 0 &&
331 opt.basic_constraints != 1) {
332 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100333 goto usage;
334 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200336 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 mbedtls_md_info_from_string(q);
338 if (md_info == NULL) {
339 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100340 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100341 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 opt.md = mbedtls_md_get_type(md_info);
343 } else if (strcmp(p, "version") == 0) {
344 opt.version = atoi(q);
345 if (opt.version < 1 || opt.version > 3) {
346 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100347 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100348 }
Hanno Becker38eff432017-09-22 15:38:20 +0100349 opt.version--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 } else if (strcmp(p, "selfsign") == 0) {
351 opt.selfsign = atoi(q);
352 if (opt.selfsign < 0 || opt.selfsign > 1) {
353 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200354 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100355 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 } else if (strcmp(p, "is_ca") == 0) {
357 opt.is_ca = atoi(q);
358 if (opt.is_ca < 0 || opt.is_ca > 1) {
359 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200360 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100361 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 } else if (strcmp(p, "max_pathlen") == 0) {
363 opt.max_pathlen = atoi(q);
364 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
365 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200366 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100367 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 } else if (strcmp(p, "key_usage") == 0) {
369 while (q != NULL) {
370 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200371 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100379 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100381 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100383 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 } else {
389 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100391 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392
393 q = r;
394 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 } else if (strcmp(p, "ns_cert_type") == 0) {
396 while (q != NULL) {
397 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200398 *r++ = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100404 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100410 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100412 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100414 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 } else {
416 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200417 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100418 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419
420 q = r;
421 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 }
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200428
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200429 /*
430 * 0. Seed the PRNG
431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_printf(" . Seeding the random number generator...");
433 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
436 (const unsigned char *) pers,
437 strlen(pers))) != 0) {
438 mbedtls_strerror(ret, buf, 1024);
439 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
440 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200441 goto exit;
442 }
443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200445
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 // Parse serial to MPI
447 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 mbedtls_printf(" . Reading serial number...");
449 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) {
452 mbedtls_strerror(ret, buf, 1024);
453 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string "
454 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 goto exit;
456 }
457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200459
Paul Bakker1014e952013-09-09 13:59:42 +0200460 // Parse issuer certificate if present
461 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200463 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200464 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200465 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 mbedtls_printf(" . Loading the issuer certificate ...");
467 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
470 mbedtls_strerror(ret, buf, 1024);
471 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
472 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200473 goto exit;
474 }
475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
477 &issuer_crt.subject);
478 if (ret < 0) {
479 mbedtls_strerror(ret, buf, 1024);
480 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
481 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200482 goto exit;
483 }
484
Paul Bakkere2673fb2013-09-09 15:52:07 +0200485 opt.issuer_name = issuer_name;
486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200488 }
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200491 // Parse certificate request if present
492 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200494 /*
495 * 1.0.b. Load the CSR
496 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 mbedtls_printf(" . Loading the certificate request ...");
498 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
501 mbedtls_strerror(ret, buf, 1024);
502 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
503 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200504 goto exit;
505 }
506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
508 &csr.subject);
509 if (ret < 0) {
510 mbedtls_strerror(ret, buf, 1024);
511 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
512 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200513 goto exit;
514 }
515
516 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200517 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200522
523 /*
524 * 1.1. Load the keys
525 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if (!opt.selfsign && !strlen(opt.request_file)) {
527 mbedtls_printf(" . Loading the subject key ...");
528 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
531 opt.subject_pwd);
532 if (ret != 0) {
533 mbedtls_strerror(ret, buf, 1024);
534 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
535 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200536 goto exit;
537 }
Paul Bakker1014e952013-09-09 13:59:42 +0200538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100539 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200540 }
541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 mbedtls_printf(" . Loading the issuer key ...");
543 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
546 opt.issuer_pwd);
547 if (ret != 0) {
548 mbedtls_strerror(ret, buf, 1024);
549 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
550 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200551 goto exit;
552 }
553
554 // Check if key and issuer certificate match
555 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if (strlen(opt.issuer_crt)) {
557 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key) != 0) {
558 mbedtls_printf(" failed\n ! issuer_key does not match "
559 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200560 goto exit;
561 }
562 }
563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100564 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100567 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200568 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200569 }
570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
572 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200573
Paul Bakker9397dcb2013-09-06 09:55:26 +0200574 /*
575 * 1.0. Check the names for validity
576 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
578 mbedtls_strerror(ret, buf, 1024);
579 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
580 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200581 goto exit;
582 }
583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
585 mbedtls_strerror(ret, buf, 1024);
586 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
587 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200588 goto exit;
589 }
590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 mbedtls_printf(" . Setting certificate values ...");
592 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 mbedtls_x509write_crt_set_version(&crt, opt.version);
595 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
598 if (ret != 0) {
599 mbedtls_strerror(ret, buf, 1024);
600 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial "
601 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200602 goto exit;
603 }
604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
606 if (ret != 0) {
607 mbedtls_strerror(ret, buf, 1024);
608 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
609 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200610 goto exit;
611 }
612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
616 opt.basic_constraints != 0) {
617 mbedtls_printf(" . Adding the Basic Constraints extension ...");
618 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
621 opt.max_pathlen);
622 if (ret != 0) {
623 mbedtls_strerror(ret, buf, 1024);
624 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
625 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100626 goto exit;
627 }
628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100630 }
Paul Bakker15162a02013-09-06 19:27:21 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
634 opt.subject_identifier != 0) {
635 mbedtls_printf(" . Adding the Subject Key Identifier ...");
636 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
639 if (ret != 0) {
640 mbedtls_strerror(ret, buf, 1024);
641 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
642 "_key_identifier returned -0x%04x - %s\n\n",
643 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100644 goto exit;
645 }
646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200648 }
649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100650 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
651 opt.authority_identifier != 0) {
652 mbedtls_printf(" . Adding the Authority Key Identifier ...");
653 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
656 if (ret != 0) {
657 mbedtls_strerror(ret, buf, 1024);
658 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
659 "key_identifier returned -0x%04x - %s\n\n",
660 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100661 goto exit;
662 }
663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
669 opt.key_usage != 0) {
670 mbedtls_printf(" . Adding the Key Usage extension ...");
671 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
674 if (ret != 0) {
675 mbedtls_strerror(ret, buf, 1024);
676 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
677 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200678 goto exit;
679 }
680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200682 }
683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
685 opt.ns_cert_type != 0) {
686 mbedtls_printf(" . Adding the NS Cert Type extension ...");
687 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
690 if (ret != 0) {
691 mbedtls_strerror(ret, buf, 1024);
692 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
693 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200694 goto exit;
695 }
696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200698 }
699
Paul Bakker9397dcb2013-09-06 09:55:26 +0200700 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100701 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200702 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 mbedtls_printf(" . Writing the certificate...");
704 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 if ((ret = write_certificate(&crt, opt.output_file,
707 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
708 mbedtls_strerror(ret, buf, 1024);
709 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
710 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200711 goto exit;
712 }
713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200715
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100716 exit_code = MBEDTLS_EXIT_SUCCESS;
717
Paul Bakker9397dcb2013-09-06 09:55:26 +0200718exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100719#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100721#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 mbedtls_x509_crt_free(&issuer_crt);
723 mbedtls_x509write_crt_free(&crt);
724 mbedtls_pk_free(&loaded_subject_key);
725 mbedtls_pk_free(&loaded_issuer_key);
726 mbedtls_mpi_free(&serial);
727 mbedtls_ctr_drbg_free(&ctr_drbg);
728 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200729#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200730 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200732
733#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 mbedtls_printf(" + Press Enter to exit this program.\n");
735 fflush(stdout); getchar();
Paul Bakker9397dcb2013-09-06 09:55:26 +0200736#endif
737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
741 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100742 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */