blob: c93ff1e9862e400c63d235f24081c0af1bd2132a [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Simon Butcher203a6932016-10-07 15:00:17 +010024#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
25 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
27 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
28 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020029int main( void )
30{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 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 +020032 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
34 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020035 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020036}
37#else
38
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/x509_crt.h"
40#include "mbedtls/x509_csr.h"
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010043#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020045
Rich Evans18b78c72015-02-11 14:06:19 +000046#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000051#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010052 " request_file=%%s default: (empty)\n" \
53 " If request_file is specified, subject_key,\n" \
54 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000055#else
56#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000058
Paul Bakker1014e952013-09-09 13:59:42 +020059#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020060#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020061#define DFL_SUBJECT_KEY "subject.key"
62#define DFL_ISSUER_KEY "ca.key"
63#define DFL_SUBJECT_PWD ""
64#define DFL_ISSUER_PWD ""
65#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000066#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
67#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020068#define DFL_NOT_BEFORE "20010101000000"
69#define DFL_NOT_AFTER "20301231235959"
70#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020071#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020072#define DFL_IS_CA 0
73#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_KEY_USAGE 0
75#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010076#define DFL_VERSION 3
77#define DFL_AUTH_IDENT 1
78#define DFL_SUBJ_IDENT 1
79#define DFL_CONSTRAINTS 1
80#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020081
Rich Evans18b78c72015-02-11 14:06:19 +000082#define USAGE \
83 "\n usage: cert_write param=<>...\n" \
84 "\n acceptable parameters:\n" \
85 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010086 " subject_key=%%s default: subject.key\n" \
87 " subject_pwd=%%s default: (empty)\n" \
88 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000089 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010090 " issuer_crt=%%s default: (empty)\n" \
91 " If issuer_crt is specified, issuer_name is\n" \
92 " ignored!\n" \
93 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000094 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010095 " selfsign=%%d default: 0 (false)\n" \
96 " If selfsign is enabled, issuer_name and\n" \
97 " issuer_key are required (issuer_crt and\n" \
98 " subject_* are ignored\n" \
99 " issuer_key=%%s default: ca.key\n" \
100 " issuer_pwd=%%s default: (empty)\n" \
101 " output_file=%%s default: cert.crt\n" \
102 " serial=%%s default: 1\n" \
103 " not_before=%%s default: 20010101000000\n"\
104 " not_after=%%s default: 20301231235959\n"\
105 " is_ca=%%d default: 0 (disabled)\n" \
106 " max_pathlen=%%d default: -1 (none)\n" \
107 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200108 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200109 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200110 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100111 " version=%%d default: 3\n" \
112 " Possible values: 1, 2, 3\n"\
113 " subject_identifier=%%s default: 1\n" \
114 " Possible values: 0, 1\n" \
115 " (Considered for v3 only)\n"\
116 " authority_identifier=%%s default: 1\n" \
117 " Possible values: 0, 1\n" \
118 " (Considered for v3 only)\n"\
119 " basic_constraints=%%d default: 1\n" \
120 " Possible values: 0, 1\n" \
121 " (Considered for v3 only)\n"\
122 " key_usage=%%s default: (empty)\n" \
123 " Comma-separated-list of values:\n" \
124 " digital_signature\n" \
125 " non_repudiation\n" \
126 " key_encipherment\n" \
127 " data_encipherment\n" \
128 " key_agreement\n" \
129 " key_cert_sign\n" \
130 " crl_sign\n" \
131 " (Considered for v3 only)\n"\
132 " ns_cert_type=%%s default: (empty)\n" \
133 " Comma-separated-list of values:\n" \
134 " ssl_client\n" \
135 " ssl_server\n" \
136 " email\n" \
137 " object_signing\n" \
138 " ssl_ca\n" \
139 " email_ca\n" \
140 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000141 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000142
Simon Butcher63cb97e2018-12-06 17:43:31 +0000143
Paul Bakker9397dcb2013-09-06 09:55:26 +0200144/*
145 * global options
146 */
147struct options
148{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100149 const char *issuer_crt; /* filename of the issuer certificate */
150 const char *request_file; /* filename of the certificate request */
151 const char *subject_key; /* filename of the subject key file */
152 const char *issuer_key; /* filename of the issuer key file */
153 const char *subject_pwd; /* password for the subject key file */
154 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100155 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100156 const char *subject_name; /* subject name for certificate */
157 const char *issuer_name; /* issuer name for certificate */
158 const char *not_before; /* validity period not before */
159 const char *not_after; /* validity period not after */
160 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200161 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200162 int is_ca; /* is a CA certificate */
163 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100164 int authority_identifier; /* add authority identifier to CRT */
165 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100166 int basic_constraints; /* add basic constraints ext to CRT */
167 int version; /* CRT version */
168 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200169 unsigned char key_usage; /* key usage flags */
170 unsigned char ns_cert_type; /* NS cert type */
171} opt;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200174 int (*f_rng)(void *, unsigned char *, size_t),
175 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200176{
177 int ret;
178 FILE *f;
179 unsigned char output_buf[4096];
180 size_t len = 0;
181
182 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100183 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
184 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185 return( ret );
186
187 len = strlen( (char *) output_buf );
188
189 if( ( f = fopen( output_file, "w" ) ) == NULL )
190 return( -1 );
191
192 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200193 {
194 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200196 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197
Paul Bakker0c226102014-04-17 16:02:36 +0200198 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199
200 return( 0 );
201}
202
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203int main( int argc, char *argv[] )
204{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100205 int ret = 1;
206 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_x509_crt issuer_crt;
208 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
209 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200210 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200212 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100213 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200214 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200216 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_x509write_cert crt;
220 mbedtls_mpi serial;
221 mbedtls_entropy_context entropy;
222 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200223 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224
225 /*
226 * Set to sane values
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_pk_init( &loaded_issuer_key );
230 mbedtls_pk_init( &loaded_subject_key );
231 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200232 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100233 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_X509_CSR_PARSE_C)
235 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238 memset( buf, 0, 1024 );
239
240 if( argc == 0 )
241 {
242 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 goto exit;
245 }
246
Paul Bakker1014e952013-09-09 13:59:42 +0200247 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200248 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 opt.subject_key = DFL_SUBJECT_KEY;
250 opt.issuer_key = DFL_ISSUER_KEY;
251 opt.subject_pwd = DFL_SUBJECT_PWD;
252 opt.issuer_pwd = DFL_ISSUER_PWD;
253 opt.output_file = DFL_OUTPUT_FILENAME;
254 opt.subject_name = DFL_SUBJECT_NAME;
255 opt.issuer_name = DFL_ISSUER_NAME;
256 opt.not_before = DFL_NOT_BEFORE;
257 opt.not_after = DFL_NOT_AFTER;
258 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200259 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200260 opt.is_ca = DFL_IS_CA;
261 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 opt.key_usage = DFL_KEY_USAGE;
263 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100264 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100265 opt.md = DFL_DIGEST;
266 opt.subject_identifier = DFL_SUBJ_IDENT;
267 opt.authority_identifier = DFL_AUTH_IDENT;
268 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269
270 for( i = 1; i < argc; i++ )
271 {
272
273 p = argv[i];
274 if( ( q = strchr( p, '=' ) ) == NULL )
275 goto usage;
276 *q++ = '\0';
277
Paul Bakkere2673fb2013-09-09 15:52:07 +0200278 if( strcmp( p, "request_file" ) == 0 )
279 opt.request_file = q;
280 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200281 opt.subject_key = q;
282 else if( strcmp( p, "issuer_key" ) == 0 )
283 opt.issuer_key = q;
284 else if( strcmp( p, "subject_pwd" ) == 0 )
285 opt.subject_pwd = q;
286 else if( strcmp( p, "issuer_pwd" ) == 0 )
287 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200288 else if( strcmp( p, "issuer_crt" ) == 0 )
289 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200290 else if( strcmp( p, "output_file" ) == 0 )
291 opt.output_file = q;
292 else if( strcmp( p, "subject_name" ) == 0 )
293 {
294 opt.subject_name = q;
295 }
296 else if( strcmp( p, "issuer_name" ) == 0 )
297 {
298 opt.issuer_name = q;
299 }
300 else if( strcmp( p, "not_before" ) == 0 )
301 {
302 opt.not_before = q;
303 }
304 else if( strcmp( p, "not_after" ) == 0 )
305 {
306 opt.not_after = q;
307 }
308 else if( strcmp( p, "serial" ) == 0 )
309 {
310 opt.serial = q;
311 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100312 else if( strcmp( p, "authority_identifier" ) == 0 )
313 {
314 opt.authority_identifier = atoi( q );
315 if( opt.authority_identifier != 0 &&
316 opt.authority_identifier != 1 )
317 {
Hanno Becker17c32762017-10-03 14:56:04 +0100318 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100319 goto usage;
320 }
321 }
322 else if( strcmp( p, "subject_identifier" ) == 0 )
323 {
324 opt.subject_identifier = atoi( q );
325 if( opt.subject_identifier != 0 &&
326 opt.subject_identifier != 1 )
327 {
Hanno Becker17c32762017-10-03 14:56:04 +0100328 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100329 goto usage;
330 }
331 }
332 else if( strcmp( p, "basic_constraints" ) == 0 )
333 {
334 opt.basic_constraints = atoi( q );
335 if( opt.basic_constraints != 0 &&
336 opt.basic_constraints != 1 )
337 {
Hanno Becker17c32762017-10-03 14:56:04 +0100338 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100339 goto usage;
340 }
341 }
342 else if( strcmp( p, "md" ) == 0 )
343 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200344 const mbedtls_md_info_t *md_info =
345 mbedtls_md_info_from_string( q );
346 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100347 {
348 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100349 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100350 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200351 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100352 }
353 else if( strcmp( p, "version" ) == 0 )
354 {
355 opt.version = atoi( q );
356 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100357 {
358 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100359 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100360 }
Hanno Becker38eff432017-09-22 15:38:20 +0100361 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100362 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200363 else if( strcmp( p, "selfsign" ) == 0 )
364 {
365 opt.selfsign = atoi( q );
366 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100367 {
368 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200369 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100370 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200371 }
Paul Bakker15162a02013-09-06 19:27:21 +0200372 else if( strcmp( p, "is_ca" ) == 0 )
373 {
374 opt.is_ca = atoi( q );
375 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100376 {
377 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200378 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100379 }
Paul Bakker15162a02013-09-06 19:27:21 +0200380 }
381 else if( strcmp( p, "max_pathlen" ) == 0 )
382 {
383 opt.max_pathlen = atoi( q );
384 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100385 {
386 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200387 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100388 }
Paul Bakker15162a02013-09-06 19:27:21 +0200389 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 else if( strcmp( p, "key_usage" ) == 0 )
391 {
392 while( q != NULL )
393 {
394 if( ( r = strchr( q, ',' ) ) != NULL )
395 *r++ = '\0';
396
397 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200401 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100402 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200403 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100404 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200405 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100406 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200407 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200409 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200411 else
Hanno Becker17c32762017-10-03 14:56:04 +0100412 {
413 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200414 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100415 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416
417 q = r;
418 }
419 }
420 else if( strcmp( p, "ns_cert_type" ) == 0 )
421 {
422 while( q != NULL )
423 {
424 if( ( r = strchr( q, ',' ) ) != NULL )
425 *r++ = '\0';
426
427 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100430 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100436 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100438 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100440 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441 else
Hanno Becker17c32762017-10-03 14:56:04 +0100442 {
443 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100445 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446
447 q = r;
448 }
449 }
450 else
451 goto usage;
452 }
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200455
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200456 /*
457 * 0. Seed the PRNG
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200460 fflush( stdout );
461
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200462 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200463 (const unsigned char *) pers,
464 strlen( pers ) ) ) != 0 )
465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100467 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
468 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200469 goto exit;
470 }
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200473
Paul Bakker9397dcb2013-09-06 09:55:26 +0200474 // Parse serial to MPI
475 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200477 fflush( stdout );
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100482 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200483 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 goto exit;
485 }
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200488
Paul Bakker1014e952013-09-09 13:59:42 +0200489 // Parse issuer certificate if present
490 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200491 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200492 {
493 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200494 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200497 fflush( stdout );
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100502 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200503 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200504 goto exit;
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200508 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200509 if( ret < 0 )
510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100512 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200513 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200514 goto exit;
515 }
516
Paul Bakkere2673fb2013-09-09 15:52:07 +0200517 opt.issuer_name = issuer_name;
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200520 }
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200523 // Parse certificate request if present
524 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200525 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200526 {
527 /*
528 * 1.0.b. Load the CSR
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200531 fflush( stdout );
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100536 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200537 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200538 goto exit;
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200542 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200543 if( ret < 0 )
544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100546 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200547 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 goto exit;
549 }
550
551 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200552 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200555 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200557
558 /*
559 * 1.1. Load the keys
560 */
561 if( !opt.selfsign && !strlen( opt.request_file ) )
562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200564 fflush( stdout );
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200567 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200568 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100571 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200572 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200573 goto exit;
574 }
Paul Bakker1014e952013-09-09 13:59:42 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200577 }
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200580 fflush( stdout );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200583 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200584 if( ret != 0 )
585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100587 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200588 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200589 goto exit;
590 }
591
592 // Check if key and issuer certificate match
593 //
594 if( strlen( opt.issuer_crt ) )
595 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200596 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200597 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200598 {
Hanno Becker81535d02017-09-13 15:39:59 +0100599 mbedtls_printf( " failed\n ! issuer_key does not match "
600 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200601 goto exit;
602 }
603 }
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200606
607 if( opt.selfsign )
608 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100609 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200610 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200611 }
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
614 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615
Paul Bakker9397dcb2013-09-06 09:55:26 +0200616 /*
617 * 1.0. Check the names for validity
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100622 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200623 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200624 goto exit;
625 }
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100630 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200631 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 goto exit;
633 }
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 fflush( stdout );
637
Hanno Becker38eff432017-09-22 15:38:20 +0100638 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100639 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 if( ret != 0 )
643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100645 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200646 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200647 goto exit;
648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200651 if( ret != 0 )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100654 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200655 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200656 goto exit;
657 }
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660
Hanno Becker38eff432017-09-22 15:38:20 +0100661 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
662 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200663 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100664 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
665 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200666
Hanno Becker6c13d372017-09-13 12:49:22 +0100667 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
668 opt.max_pathlen );
669 if( ret != 0 )
670 {
671 mbedtls_strerror( ret, buf, 1024 );
672 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200673 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100674 goto exit;
675 }
676
677 mbedtls_printf( " ok\n" );
678 }
Paul Bakker15162a02013-09-06 19:27:21 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100681 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
682 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200683 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100684 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
685 fflush( stdout );
686
687 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
688 if( ret != 0 )
689 {
690 mbedtls_strerror( ret, buf, 1024 );
691 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100692 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200693 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100694 goto exit;
695 }
696
697 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200698 }
699
Hanno Becker38eff432017-09-22 15:38:20 +0100700 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
701 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200702 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100703 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
704 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200705
Hanno Becker6c13d372017-09-13 12:49:22 +0100706 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
707 if( ret != 0 )
708 {
709 mbedtls_strerror( ret, buf, 1024 );
710 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100711 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200712 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100713 goto exit;
714 }
715
716 mbedtls_printf( " ok\n" );
717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200719
Hanno Becker38eff432017-09-22 15:38:20 +0100720 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
721 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200724 fflush( stdout );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200727 if( ret != 0 )
728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100730 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200731 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200732 goto exit;
733 }
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200736 }
737
Hanno Becker38eff432017-09-22 15:38:20 +0100738 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
739 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200742 fflush( stdout );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200745 if( ret != 0 )
746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100748 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200749 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200750 goto exit;
751 }
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200754 }
755
Paul Bakker9397dcb2013-09-06 09:55:26 +0200756 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100757 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200760 fflush( stdout );
761
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200762 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100766 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200767 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200768 goto exit;
769 }
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200772
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100773 exit_code = MBEDTLS_EXIT_SUCCESS;
774
Paul Bakker9397dcb2013-09-06 09:55:26 +0200775exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100776#if defined(MBEDTLS_X509_CSR_PARSE_C)
777 mbedtls_x509_csr_free( &csr );
778#endif /* MBEDTLS_X509_CSR_PARSE_C */
779 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_x509write_crt_free( &crt );
781 mbedtls_pk_free( &loaded_subject_key );
782 mbedtls_pk_free( &loaded_issuer_key );
783 mbedtls_mpi_free( &serial );
784 mbedtls_ctr_drbg_free( &ctr_drbg );
785 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200786
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200787 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
790 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100791 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */