blob: c22f5a4b229ff74bcfffef5aaa15ba5d34c4ade8 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Simon Butcher203a6932016-10-07 15:00:17 +010037#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
38 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
39 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
41 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042int main( void )
43{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 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 +020045 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
47 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020048 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020049}
50#else
51
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/x509_crt.h"
53#include "mbedtls/x509_csr.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010056#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000064#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010065 " request_file=%%s default: (empty)\n" \
66 " If request_file is specified, subject_key,\n" \
67 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000068#else
69#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000071
Paul Bakker1014e952013-09-09 13:59:42 +020072#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020073#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_SUBJECT_KEY "subject.key"
75#define DFL_ISSUER_KEY "ca.key"
76#define DFL_SUBJECT_PWD ""
77#define DFL_ISSUER_PWD ""
78#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000079#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
80#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020081#define DFL_NOT_BEFORE "20010101000000"
82#define DFL_NOT_AFTER "20301231235959"
83#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020084#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020085#define DFL_IS_CA 0
86#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
88#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010089#define DFL_VERSION 3
90#define DFL_AUTH_IDENT 1
91#define DFL_SUBJ_IDENT 1
92#define DFL_CONSTRAINTS 1
93#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020094
Rich Evans18b78c72015-02-11 14:06:19 +000095#define USAGE \
96 "\n usage: cert_write param=<>...\n" \
97 "\n acceptable parameters:\n" \
98 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010099 " subject_key=%%s default: subject.key\n" \
100 " subject_pwd=%%s default: (empty)\n" \
101 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000102 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100103 " issuer_crt=%%s default: (empty)\n" \
104 " If issuer_crt is specified, issuer_name is\n" \
105 " ignored!\n" \
106 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000107 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100108 " selfsign=%%d default: 0 (false)\n" \
109 " If selfsign is enabled, issuer_name and\n" \
110 " issuer_key are required (issuer_crt and\n" \
111 " subject_* are ignored\n" \
112 " issuer_key=%%s default: ca.key\n" \
113 " issuer_pwd=%%s default: (empty)\n" \
114 " output_file=%%s default: cert.crt\n" \
115 " serial=%%s default: 1\n" \
116 " not_before=%%s default: 20010101000000\n"\
117 " not_after=%%s default: 20301231235959\n"\
118 " is_ca=%%d default: 0 (disabled)\n" \
119 " max_pathlen=%%d default: -1 (none)\n" \
120 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200121 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200122 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200123 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100124 " version=%%d default: 3\n" \
125 " Possible values: 1, 2, 3\n"\
126 " subject_identifier=%%s default: 1\n" \
127 " Possible values: 0, 1\n" \
128 " (Considered for v3 only)\n"\
129 " authority_identifier=%%s default: 1\n" \
130 " Possible values: 0, 1\n" \
131 " (Considered for v3 only)\n"\
132 " basic_constraints=%%d default: 1\n" \
133 " Possible values: 0, 1\n" \
134 " (Considered for v3 only)\n"\
135 " key_usage=%%s default: (empty)\n" \
136 " Comma-separated-list of values:\n" \
137 " digital_signature\n" \
138 " non_repudiation\n" \
139 " key_encipherment\n" \
140 " data_encipherment\n" \
141 " key_agreement\n" \
142 " key_cert_sign\n" \
143 " crl_sign\n" \
144 " (Considered for v3 only)\n"\
145 " ns_cert_type=%%s default: (empty)\n" \
146 " Comma-separated-list of values:\n" \
147 " ssl_client\n" \
148 " ssl_server\n" \
149 " email\n" \
150 " object_signing\n" \
151 " ssl_ca\n" \
152 " email_ca\n" \
153 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000154 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000155
Simon Butcher63cb97e2018-12-06 17:43:31 +0000156
Paul Bakker9397dcb2013-09-06 09:55:26 +0200157/*
158 * global options
159 */
160struct options
161{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100162 const char *issuer_crt; /* filename of the issuer certificate */
163 const char *request_file; /* filename of the certificate request */
164 const char *subject_key; /* filename of the subject key file */
165 const char *issuer_key; /* filename of the issuer key file */
166 const char *subject_pwd; /* password for the subject key file */
167 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100168 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100169 const char *subject_name; /* subject name for certificate */
170 const char *issuer_name; /* issuer name for certificate */
171 const char *not_before; /* validity period not before */
172 const char *not_after; /* validity period not after */
173 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200174 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200175 int is_ca; /* is a CA certificate */
176 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100177 int authority_identifier; /* add authority identifier to CRT */
178 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100179 int basic_constraints; /* add basic constraints ext to CRT */
180 int version; /* CRT version */
181 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200182 unsigned char key_usage; /* key usage flags */
183 unsigned char ns_cert_type; /* NS cert type */
184} opt;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200187 int (*f_rng)(void *, unsigned char *, size_t),
188 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189{
190 int ret;
191 FILE *f;
192 unsigned char output_buf[4096];
193 size_t len = 0;
194
195 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100196 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
197 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200198 return( ret );
199
200 len = strlen( (char *) output_buf );
201
202 if( ( f = fopen( output_file, "w" ) ) == NULL )
203 return( -1 );
204
205 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200206 {
207 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200209 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210
Paul Bakker0c226102014-04-17 16:02:36 +0200211 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212
213 return( 0 );
214}
215
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216int main( int argc, char *argv[] )
217{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100218 int ret = 1;
219 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_x509_crt issuer_crt;
221 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
222 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200223 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200225 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100226 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200229 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509write_cert crt;
233 mbedtls_mpi serial;
234 mbedtls_entropy_context entropy;
235 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200236 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200237
238 /*
239 * Set to sane values
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_pk_init( &loaded_issuer_key );
243 mbedtls_pk_init( &loaded_subject_key );
244 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200245 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100246 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_X509_CSR_PARSE_C)
248 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251 memset( buf, 0, 1024 );
252
253 if( argc == 0 )
254 {
255 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200257 goto exit;
258 }
259
Paul Bakker1014e952013-09-09 13:59:42 +0200260 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200261 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 opt.subject_key = DFL_SUBJECT_KEY;
263 opt.issuer_key = DFL_ISSUER_KEY;
264 opt.subject_pwd = DFL_SUBJECT_PWD;
265 opt.issuer_pwd = DFL_ISSUER_PWD;
266 opt.output_file = DFL_OUTPUT_FILENAME;
267 opt.subject_name = DFL_SUBJECT_NAME;
268 opt.issuer_name = DFL_ISSUER_NAME;
269 opt.not_before = DFL_NOT_BEFORE;
270 opt.not_after = DFL_NOT_AFTER;
271 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200272 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200273 opt.is_ca = DFL_IS_CA;
274 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275 opt.key_usage = DFL_KEY_USAGE;
276 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100277 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100278 opt.md = DFL_DIGEST;
279 opt.subject_identifier = DFL_SUBJ_IDENT;
280 opt.authority_identifier = DFL_AUTH_IDENT;
281 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200282
283 for( i = 1; i < argc; i++ )
284 {
285
286 p = argv[i];
287 if( ( q = strchr( p, '=' ) ) == NULL )
288 goto usage;
289 *q++ = '\0';
290
Paul Bakkere2673fb2013-09-09 15:52:07 +0200291 if( strcmp( p, "request_file" ) == 0 )
292 opt.request_file = q;
293 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294 opt.subject_key = q;
295 else if( strcmp( p, "issuer_key" ) == 0 )
296 opt.issuer_key = q;
297 else if( strcmp( p, "subject_pwd" ) == 0 )
298 opt.subject_pwd = q;
299 else if( strcmp( p, "issuer_pwd" ) == 0 )
300 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200301 else if( strcmp( p, "issuer_crt" ) == 0 )
302 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 else if( strcmp( p, "output_file" ) == 0 )
304 opt.output_file = q;
305 else if( strcmp( p, "subject_name" ) == 0 )
306 {
307 opt.subject_name = q;
308 }
309 else if( strcmp( p, "issuer_name" ) == 0 )
310 {
311 opt.issuer_name = q;
312 }
313 else if( strcmp( p, "not_before" ) == 0 )
314 {
315 opt.not_before = q;
316 }
317 else if( strcmp( p, "not_after" ) == 0 )
318 {
319 opt.not_after = q;
320 }
321 else if( strcmp( p, "serial" ) == 0 )
322 {
323 opt.serial = q;
324 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100325 else if( strcmp( p, "authority_identifier" ) == 0 )
326 {
327 opt.authority_identifier = atoi( q );
328 if( opt.authority_identifier != 0 &&
329 opt.authority_identifier != 1 )
330 {
Hanno Becker17c32762017-10-03 14:56:04 +0100331 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100332 goto usage;
333 }
334 }
335 else if( strcmp( p, "subject_identifier" ) == 0 )
336 {
337 opt.subject_identifier = atoi( q );
338 if( opt.subject_identifier != 0 &&
339 opt.subject_identifier != 1 )
340 {
Hanno Becker17c32762017-10-03 14:56:04 +0100341 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100342 goto usage;
343 }
344 }
345 else if( strcmp( p, "basic_constraints" ) == 0 )
346 {
347 opt.basic_constraints = atoi( q );
348 if( opt.basic_constraints != 0 &&
349 opt.basic_constraints != 1 )
350 {
Hanno Becker17c32762017-10-03 14:56:04 +0100351 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100352 goto usage;
353 }
354 }
355 else if( strcmp( p, "md" ) == 0 )
356 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200357 const mbedtls_md_info_t *md_info =
358 mbedtls_md_info_from_string( q );
359 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100360 {
361 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100362 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100363 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200364 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100365 }
366 else if( strcmp( p, "version" ) == 0 )
367 {
368 opt.version = atoi( q );
369 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100370 {
371 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100372 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100373 }
Hanno Becker38eff432017-09-22 15:38:20 +0100374 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100375 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200376 else if( strcmp( p, "selfsign" ) == 0 )
377 {
378 opt.selfsign = atoi( q );
379 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100380 {
381 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200382 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100383 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200384 }
Paul Bakker15162a02013-09-06 19:27:21 +0200385 else if( strcmp( p, "is_ca" ) == 0 )
386 {
387 opt.is_ca = atoi( q );
388 if( opt.is_ca < 0 || opt.is_ca > 1 )
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 }
394 else if( strcmp( p, "max_pathlen" ) == 0 )
395 {
396 opt.max_pathlen = atoi( q );
397 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100398 {
399 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200400 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100401 }
Paul Bakker15162a02013-09-06 19:27:21 +0200402 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200403 else if( strcmp( p, "key_usage" ) == 0 )
404 {
405 while( q != NULL )
406 {
407 if( ( r = strchr( q, ',' ) ) != NULL )
408 *r++ = '\0';
409
410 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200412 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200414 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100415 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100417 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200418 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100419 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200420 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200422 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200424 else
Hanno Becker17c32762017-10-03 14:56:04 +0100425 {
426 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100428 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429
430 q = r;
431 }
432 }
433 else if( strcmp( p, "ns_cert_type" ) == 0 )
434 {
435 while( q != NULL )
436 {
437 if( ( r = strchr( q, ',' ) ) != NULL )
438 *r++ = '\0';
439
440 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200442 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100443 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100449 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100451 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100453 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else
Hanno Becker17c32762017-10-03 14:56:04 +0100455 {
456 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200457 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100458 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200459
460 q = r;
461 }
462 }
463 else
464 goto usage;
465 }
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200468
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200469 /*
470 * 0. Seed the PRNG
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200473 fflush( stdout );
474
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200475 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200476 (const unsigned char *) pers,
477 strlen( pers ) ) ) != 0 )
478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100480 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
481 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200482 goto exit;
483 }
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200486
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487 // Parse serial to MPI
488 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200490 fflush( stdout );
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100495 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200496 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200497 goto exit;
498 }
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200501
Paul Bakker1014e952013-09-09 13:59:42 +0200502 // Parse issuer certificate if present
503 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200504 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200505 {
506 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200507 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200510 fflush( stdout );
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100515 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200516 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200517 goto exit;
518 }
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200521 &issuer_crt.MBEDTLS_PRIVATE(subject) );
Paul Bakker1014e952013-09-09 13:59:42 +0200522 if( ret < 0 )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100525 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200526 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200527 goto exit;
528 }
529
Paul Bakkere2673fb2013-09-09 15:52:07 +0200530 opt.issuer_name = issuer_name;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200533 }
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200536 // Parse certificate request if present
537 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200538 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200539 {
540 /*
541 * 1.0.b. Load the CSR
542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200544 fflush( stdout );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100549 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200550 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 goto exit;
552 }
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200555 &csr.MBEDTLS_PRIVATE(subject) );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200556 if( ret < 0 )
557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100559 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200560 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200561 goto exit;
562 }
563
564 opt.subject_name = subject_name;
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200565 subject_key = &csr.MBEDTLS_PRIVATE(pk);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200570
571 /*
572 * 1.1. Load the keys
573 */
574 if( !opt.selfsign && !strlen( opt.request_file ) )
575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200577 fflush( stdout );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200580 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200581 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100584 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200585 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 goto exit;
587 }
Paul Bakker1014e952013-09-09 13:59:42 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200593 fflush( stdout );
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200597 if( ret != 0 )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100600 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200601 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200602 goto exit;
603 }
604
605 // Check if key and issuer certificate match
606 //
607 if( strlen( opt.issuer_crt ) )
608 {
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200609 if( mbedtls_pk_check_pair( &issuer_crt.MBEDTLS_PRIVATE(pk), issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200610 {
Hanno Becker81535d02017-09-13 15:39:59 +0100611 mbedtls_printf( " failed\n ! issuer_key does not match "
612 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200613 goto exit;
614 }
615 }
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200618
619 if( opt.selfsign )
620 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100621 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200622 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200623 }
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
626 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200627
Paul Bakker9397dcb2013-09-06 09:55:26 +0200628 /*
629 * 1.0. Check the names for validity
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100634 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200635 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 goto exit;
637 }
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100642 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200643 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 goto exit;
645 }
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200648 fflush( stdout );
649
Hanno Becker38eff432017-09-22 15:38:20 +0100650 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100651 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
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_serial "
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 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200663 if( ret != 0 )
664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100666 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200667 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200668 goto exit;
669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200672
Hanno Becker38eff432017-09-22 15:38:20 +0100673 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
674 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200675 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100676 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
677 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200678
Hanno Becker6c13d372017-09-13 12:49:22 +0100679 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
680 opt.max_pathlen );
681 if( ret != 0 )
682 {
683 mbedtls_strerror( ret, buf, 1024 );
684 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200685 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100686 goto exit;
687 }
688
689 mbedtls_printf( " ok\n" );
690 }
Paul Bakker15162a02013-09-06 19:27:21 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100693 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
694 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200695 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100696 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
697 fflush( stdout );
698
699 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
700 if( ret != 0 )
701 {
702 mbedtls_strerror( ret, buf, 1024 );
703 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100704 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200705 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100706 goto exit;
707 }
708
709 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200710 }
711
Hanno Becker38eff432017-09-22 15:38:20 +0100712 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
713 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200714 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100715 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
716 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200717
Hanno Becker6c13d372017-09-13 12:49:22 +0100718 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
719 if( ret != 0 )
720 {
721 mbedtls_strerror( ret, buf, 1024 );
722 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100723 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200724 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100725 goto exit;
726 }
727
728 mbedtls_printf( " ok\n" );
729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200731
Hanno Becker38eff432017-09-22 15:38:20 +0100732 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
733 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200736 fflush( stdout );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200739 if( ret != 0 )
740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100742 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200743 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200744 goto exit;
745 }
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200748 }
749
Hanno Becker38eff432017-09-22 15:38:20 +0100750 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
751 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200754 fflush( stdout );
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200757 if( ret != 0 )
758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100760 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200761 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200762 goto exit;
763 }
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200766 }
767
Paul Bakker9397dcb2013-09-06 09:55:26 +0200768 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100769 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200772 fflush( stdout );
773
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200774 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100778 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200779 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200780 goto exit;
781 }
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200784
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100785 exit_code = MBEDTLS_EXIT_SUCCESS;
786
Paul Bakker9397dcb2013-09-06 09:55:26 +0200787exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100788#if defined(MBEDTLS_X509_CSR_PARSE_C)
789 mbedtls_x509_csr_free( &csr );
790#endif /* MBEDTLS_X509_CSR_PARSE_C */
791 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_x509write_crt_free( &crt );
793 mbedtls_pk_free( &loaded_subject_key );
794 mbedtls_pk_free( &loaded_issuer_key );
795 mbedtls_mpi_free( &serial );
796 mbedtls_ctr_drbg_free( &ctr_drbg );
797 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200798
799#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200801 fflush( stdout ); getchar();
802#endif
803
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200804 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
807 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100808 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */