blob: 30e9e0aa49d56251e11586908b3ec0fbb552cd5e [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)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020033int main( void )
34{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnardd7389652015-08-06 18:22:26 +020036 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
38 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020039 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" \
107 " not_before=%%s default: 20010101000000\n"\
108 " not_after=%%s default: 20301231235959\n"\
109 " 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" \
116 " Possible values: 1, 2, 3\n"\
117 " subject_identifier=%%s default: 1\n" \
118 " Possible values: 0, 1\n" \
119 " (Considered for v3 only)\n"\
120 " authority_identifier=%%s default: 1\n" \
121 " Possible values: 0, 1\n" \
122 " (Considered for v3 only)\n"\
123 " basic_constraints=%%d default: 1\n" \
124 " Possible values: 0, 1\n" \
125 " (Considered for v3 only)\n"\
126 " 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" \
135 " (Considered for v3 only)\n"\
136 " 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 */
151struct options
152{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100153 const char *issuer_crt; /* filename of the issuer certificate */
154 const char *request_file; /* filename of the certificate request */
155 const char *subject_key; /* filename of the subject key file */
156 const char *issuer_key; /* filename of the issuer key file */
157 const char *subject_pwd; /* password for the subject key file */
158 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100159 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100160 const char *subject_name; /* subject name for certificate */
161 const char *issuer_name; /* issuer name for certificate */
162 const char *not_before; /* validity period not before */
163 const char *not_after; /* validity period not after */
164 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200165 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200166 int is_ca; /* is a CA certificate */
167 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100168 int authority_identifier; /* add authority identifier to CRT */
169 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100170 int basic_constraints; /* add basic constraints ext to CRT */
171 int version; /* CRT version */
172 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200173 unsigned char key_usage; /* key usage flags */
174 unsigned char ns_cert_type; /* NS cert type */
175} opt;
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200178 int (*f_rng)(void *, unsigned char *, size_t),
179 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200180{
181 int ret;
182 FILE *f;
183 unsigned char output_buf[4096];
184 size_t len = 0;
185
186 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100187 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
188 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189 return( ret );
190
191 len = strlen( (char *) output_buf );
192
193 if( ( f = fopen( output_file, "w" ) ) == NULL )
194 return( -1 );
195
196 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200197 {
198 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200200 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201
Paul Bakker0c226102014-04-17 16:02:36 +0200202 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203
204 return( 0 );
205}
206
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207int main( int argc, char *argv[] )
208{
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,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200214 *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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_pk_init( &loaded_issuer_key );
234 mbedtls_pk_init( &loaded_subject_key );
235 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200236 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100237 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_X509_CSR_PARSE_C)
239 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200242 memset( buf, 0, 1024 );
243
244 if( argc == 0 )
245 {
246 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200248 goto exit;
249 }
250
Paul Bakker1014e952013-09-09 13:59:42 +0200251 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200252 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253 opt.subject_key = DFL_SUBJECT_KEY;
254 opt.issuer_key = DFL_ISSUER_KEY;
255 opt.subject_pwd = DFL_SUBJECT_PWD;
256 opt.issuer_pwd = DFL_ISSUER_PWD;
257 opt.output_file = DFL_OUTPUT_FILENAME;
258 opt.subject_name = DFL_SUBJECT_NAME;
259 opt.issuer_name = DFL_ISSUER_NAME;
260 opt.not_before = DFL_NOT_BEFORE;
261 opt.not_after = DFL_NOT_AFTER;
262 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200263 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200264 opt.is_ca = DFL_IS_CA;
265 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266 opt.key_usage = DFL_KEY_USAGE;
267 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100268 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100269 opt.md = DFL_DIGEST;
270 opt.subject_identifier = DFL_SUBJ_IDENT;
271 opt.authority_identifier = DFL_AUTH_IDENT;
272 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200273
274 for( i = 1; i < argc; i++ )
275 {
276
277 p = argv[i];
278 if( ( q = strchr( p, '=' ) ) == NULL )
279 goto usage;
280 *q++ = '\0';
281
Paul Bakkere2673fb2013-09-09 15:52:07 +0200282 if( strcmp( p, "request_file" ) == 0 )
283 opt.request_file = q;
284 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200285 opt.subject_key = q;
286 else if( strcmp( p, "issuer_key" ) == 0 )
287 opt.issuer_key = q;
288 else if( strcmp( p, "subject_pwd" ) == 0 )
289 opt.subject_pwd = q;
290 else if( strcmp( p, "issuer_pwd" ) == 0 )
291 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200292 else if( strcmp( p, "issuer_crt" ) == 0 )
293 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294 else if( strcmp( p, "output_file" ) == 0 )
295 opt.output_file = q;
296 else if( strcmp( p, "subject_name" ) == 0 )
297 {
298 opt.subject_name = q;
299 }
300 else if( strcmp( p, "issuer_name" ) == 0 )
301 {
302 opt.issuer_name = q;
303 }
304 else if( strcmp( p, "not_before" ) == 0 )
305 {
306 opt.not_before = q;
307 }
308 else if( strcmp( p, "not_after" ) == 0 )
309 {
310 opt.not_after = q;
311 }
312 else if( strcmp( p, "serial" ) == 0 )
313 {
314 opt.serial = q;
315 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100316 else if( strcmp( p, "authority_identifier" ) == 0 )
317 {
318 opt.authority_identifier = atoi( q );
319 if( opt.authority_identifier != 0 &&
320 opt.authority_identifier != 1 )
321 {
Hanno Becker17c32762017-10-03 14:56:04 +0100322 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100323 goto usage;
324 }
325 }
326 else if( strcmp( p, "subject_identifier" ) == 0 )
327 {
328 opt.subject_identifier = atoi( q );
329 if( opt.subject_identifier != 0 &&
330 opt.subject_identifier != 1 )
331 {
Hanno Becker17c32762017-10-03 14:56:04 +0100332 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100333 goto usage;
334 }
335 }
336 else if( strcmp( p, "basic_constraints" ) == 0 )
337 {
338 opt.basic_constraints = atoi( q );
339 if( opt.basic_constraints != 0 &&
340 opt.basic_constraints != 1 )
341 {
Hanno Becker17c32762017-10-03 14:56:04 +0100342 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100343 goto usage;
344 }
345 }
346 else if( strcmp( p, "md" ) == 0 )
347 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200348 const mbedtls_md_info_t *md_info =
349 mbedtls_md_info_from_string( q );
350 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100351 {
352 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100353 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100354 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200355 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100356 }
357 else if( strcmp( p, "version" ) == 0 )
358 {
359 opt.version = atoi( q );
360 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100361 {
362 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100363 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100364 }
Hanno Becker38eff432017-09-22 15:38:20 +0100365 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100366 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200367 else if( strcmp( p, "selfsign" ) == 0 )
368 {
369 opt.selfsign = atoi( q );
370 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100371 {
372 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200373 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100374 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200375 }
Paul Bakker15162a02013-09-06 19:27:21 +0200376 else if( strcmp( p, "is_ca" ) == 0 )
377 {
378 opt.is_ca = atoi( q );
379 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100380 {
381 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200382 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100383 }
Paul Bakker15162a02013-09-06 19:27:21 +0200384 }
385 else if( strcmp( p, "max_pathlen" ) == 0 )
386 {
387 opt.max_pathlen = atoi( q );
388 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100389 {
390 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200391 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100392 }
Paul Bakker15162a02013-09-06 19:27:21 +0200393 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 else if( strcmp( p, "key_usage" ) == 0 )
395 {
396 while( q != NULL )
397 {
398 if( ( r = strchr( q, ',' ) ) != NULL )
399 *r++ = '\0';
400
401 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200403 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200405 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100406 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200407 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100408 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200409 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100410 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200411 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 else
Hanno Becker17c32762017-10-03 14:56:04 +0100416 {
417 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200418 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100419 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200420
421 q = r;
422 }
423 }
424 else if( strcmp( p, "ns_cert_type" ) == 0 )
425 {
426 while( q != NULL )
427 {
428 if( ( r = strchr( q, ',' ) ) != NULL )
429 *r++ = '\0';
430
431 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100434 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100440 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100442 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200443 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100444 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200445 else
Hanno Becker17c32762017-10-03 14:56:04 +0100446 {
447 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100449 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450
451 q = r;
452 }
453 }
454 else
455 goto usage;
456 }
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200459
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200460 /*
461 * 0. Seed the PRNG
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200464 fflush( stdout );
465
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200466 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200467 (const unsigned char *) pers,
468 strlen( pers ) ) ) != 0 )
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100471 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
472 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200473 goto exit;
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200477
Paul Bakker9397dcb2013-09-06 09:55:26 +0200478 // Parse serial to MPI
479 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200481 fflush( stdout );
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100486 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200487 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 goto exit;
489 }
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200492
Paul Bakker1014e952013-09-09 13:59:42 +0200493 // Parse issuer certificate if present
494 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200495 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200496 {
497 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200498 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200501 fflush( stdout );
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100506 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200507 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200508 goto exit;
509 }
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200512 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200513 if( ret < 0 )
514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100516 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200517 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200518 goto exit;
519 }
520
Paul Bakkere2673fb2013-09-09 15:52:07 +0200521 opt.issuer_name = issuer_name;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200524 }
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200527 // Parse certificate request if present
528 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200529 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200530 {
531 /*
532 * 1.0.b. Load the CSR
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200535 fflush( stdout );
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100540 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200541 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200542 goto exit;
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200546 &csr.subject );
547 if( ret < 0 )
548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100550 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200551 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200552 goto exit;
553 }
554
555 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200556 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200561
562 /*
563 * 1.1. Load the keys
564 */
565 if( !opt.selfsign && !strlen( opt.request_file ) )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200568 fflush( stdout );
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200571 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200572 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100575 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200576 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200577 goto exit;
578 }
Paul Bakker1014e952013-09-09 13:59:42 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200581 }
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200584 fflush( stdout );
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200588 if( ret != 0 )
589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100591 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200592 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200593 goto exit;
594 }
595
596 // Check if key and issuer certificate match
597 //
598 if( strlen( opt.issuer_crt ) )
599 {
Ron Eldor0049f782017-02-07 19:14:58 +0200600 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200601 {
Hanno Becker81535d02017-09-13 15:39:59 +0100602 mbedtls_printf( " failed\n ! issuer_key does not match "
603 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200604 goto exit;
605 }
606 }
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200609
610 if( opt.selfsign )
611 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100612 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200613 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200614 }
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
617 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200618
Paul Bakker9397dcb2013-09-06 09:55:26 +0200619 /*
620 * 1.0. Check the names for validity
621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100625 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200626 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200627 goto exit;
628 }
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100633 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200634 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 goto exit;
636 }
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639 fflush( stdout );
640
Hanno Becker38eff432017-09-22 15:38:20 +0100641 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100642 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 if( ret != 0 )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100648 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200649 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650 goto exit;
651 }
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654 if( ret != 0 )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100657 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200658 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200659 goto exit;
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200663
Hanno Becker38eff432017-09-22 15:38:20 +0100664 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
665 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200666 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100667 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
668 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200669
Hanno Becker6c13d372017-09-13 12:49:22 +0100670 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
671 opt.max_pathlen );
672 if( ret != 0 )
673 {
674 mbedtls_strerror( ret, buf, 1024 );
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000675 mbedtls_printf( " failed\n ! x509write_crt_set_basic_constraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200676 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100677 goto exit;
678 }
679
680 mbedtls_printf( " ok\n" );
681 }
Paul Bakker15162a02013-09-06 19:27:21 +0200682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100684 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
685 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200686 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100687 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
688 fflush( stdout );
689
690 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
691 if( ret != 0 )
692 {
693 mbedtls_strerror( ret, buf, 1024 );
694 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100695 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200696 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100697 goto exit;
698 }
699
700 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200701 }
702
Hanno Becker38eff432017-09-22 15:38:20 +0100703 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
704 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200705 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100706 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
707 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200708
Hanno Becker6c13d372017-09-13 12:49:22 +0100709 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
710 if( ret != 0 )
711 {
712 mbedtls_strerror( ret, buf, 1024 );
713 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100714 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200715 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100716 goto exit;
717 }
718
719 mbedtls_printf( " ok\n" );
720 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200722
Hanno Becker38eff432017-09-22 15:38:20 +0100723 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
724 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200727 fflush( stdout );
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200730 if( ret != 0 )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100733 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200734 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200735 goto exit;
736 }
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200739 }
740
Hanno Becker38eff432017-09-22 15:38:20 +0100741 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
742 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200745 fflush( stdout );
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200748 if( ret != 0 )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100751 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200752 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200753 goto exit;
754 }
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200757 }
758
Paul Bakker9397dcb2013-09-06 09:55:26 +0200759 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100760 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200763 fflush( stdout );
764
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200765 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100769 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200770 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200771 goto exit;
772 }
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200775
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100776 exit_code = MBEDTLS_EXIT_SUCCESS;
777
Paul Bakker9397dcb2013-09-06 09:55:26 +0200778exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100779#if defined(MBEDTLS_X509_CSR_PARSE_C)
780 mbedtls_x509_csr_free( &csr );
781#endif /* MBEDTLS_X509_CSR_PARSE_C */
782 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_x509write_crt_free( &crt );
784 mbedtls_pk_free( &loaded_subject_key );
785 mbedtls_pk_free( &loaded_issuer_key );
786 mbedtls_mpi_free( &serial );
787 mbedtls_ctr_drbg_free( &ctr_drbg );
788 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200789
790#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200792 fflush( stdout ); getchar();
793#endif
794
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200795 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
798 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100799 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */