blob: 763f8684f653cc5839e3312743162ca01e2857fe [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Simon Butcher203a6932016-10-07 15:00:17 +010033#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
34 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
36 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
37 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038int main( void )
39{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 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 +020041 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
43 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020044 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020045}
46#else
47
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/x509_crt.h"
49#include "mbedtls/x509_csr.h"
50#include "mbedtls/entropy.h"
51#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010052#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000060#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010061 " request_file=%%s default: (empty)\n" \
62 " If request_file is specified, subject_key,\n" \
63 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000064#else
65#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000067
Paul Bakker1014e952013-09-09 13:59:42 +020068#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020069#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020070#define DFL_SUBJECT_KEY "subject.key"
71#define DFL_ISSUER_KEY "ca.key"
72#define DFL_SUBJECT_PWD ""
73#define DFL_ISSUER_PWD ""
74#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000075#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
76#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020077#define DFL_NOT_BEFORE "20010101000000"
78#define DFL_NOT_AFTER "20301231235959"
79#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020080#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020081#define DFL_IS_CA 0
82#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020083#define DFL_KEY_USAGE 0
84#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010085#define DFL_VERSION 3
86#define DFL_AUTH_IDENT 1
87#define DFL_SUBJ_IDENT 1
88#define DFL_CONSTRAINTS 1
89#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020090
Rich Evans18b78c72015-02-11 14:06:19 +000091#define USAGE \
92 "\n usage: cert_write param=<>...\n" \
93 "\n acceptable parameters:\n" \
94 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010095 " subject_key=%%s default: subject.key\n" \
96 " subject_pwd=%%s default: (empty)\n" \
97 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000098 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +010099 " issuer_crt=%%s default: (empty)\n" \
100 " If issuer_crt is specified, issuer_name is\n" \
101 " ignored!\n" \
102 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000103 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100104 " selfsign=%%d default: 0 (false)\n" \
105 " If selfsign is enabled, issuer_name and\n" \
106 " issuer_key are required (issuer_crt and\n" \
107 " subject_* are ignored\n" \
108 " issuer_key=%%s default: ca.key\n" \
109 " issuer_pwd=%%s default: (empty)\n" \
110 " output_file=%%s default: cert.crt\n" \
111 " serial=%%s default: 1\n" \
112 " not_before=%%s default: 20010101000000\n"\
113 " not_after=%%s default: 20301231235959\n"\
114 " is_ca=%%d default: 0 (disabled)\n" \
115 " max_pathlen=%%d default: -1 (none)\n" \
116 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200117 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200118 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200119 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100120 " version=%%d default: 3\n" \
121 " Possible values: 1, 2, 3\n"\
122 " subject_identifier=%%s default: 1\n" \
123 " Possible values: 0, 1\n" \
124 " (Considered for v3 only)\n"\
125 " authority_identifier=%%s default: 1\n" \
126 " Possible values: 0, 1\n" \
127 " (Considered for v3 only)\n"\
128 " basic_constraints=%%d default: 1\n" \
129 " Possible values: 0, 1\n" \
130 " (Considered for v3 only)\n"\
131 " key_usage=%%s default: (empty)\n" \
132 " Comma-separated-list of values:\n" \
133 " digital_signature\n" \
134 " non_repudiation\n" \
135 " key_encipherment\n" \
136 " data_encipherment\n" \
137 " key_agreement\n" \
138 " key_cert_sign\n" \
139 " crl_sign\n" \
140 " (Considered for v3 only)\n"\
141 " ns_cert_type=%%s default: (empty)\n" \
142 " Comma-separated-list of values:\n" \
143 " ssl_client\n" \
144 " ssl_server\n" \
145 " email\n" \
146 " object_signing\n" \
147 " ssl_ca\n" \
148 " email_ca\n" \
149 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000150 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000151
Simon Butcher63cb97e2018-12-06 17:43:31 +0000152
Paul Bakker9397dcb2013-09-06 09:55:26 +0200153/*
154 * global options
155 */
156struct options
157{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100158 const char *issuer_crt; /* filename of the issuer certificate */
159 const char *request_file; /* filename of the certificate request */
160 const char *subject_key; /* filename of the subject key file */
161 const char *issuer_key; /* filename of the issuer key file */
162 const char *subject_pwd; /* password for the subject key file */
163 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100164 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100165 const char *subject_name; /* subject name for certificate */
166 const char *issuer_name; /* issuer name for certificate */
167 const char *not_before; /* validity period not before */
168 const char *not_after; /* validity period not after */
169 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200170 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200171 int is_ca; /* is a CA certificate */
172 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100173 int authority_identifier; /* add authority identifier to CRT */
174 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100175 int basic_constraints; /* add basic constraints ext to CRT */
176 int version; /* CRT version */
177 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178 unsigned char key_usage; /* key usage flags */
179 unsigned char ns_cert_type; /* NS cert type */
180} opt;
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200183 int (*f_rng)(void *, unsigned char *, size_t),
184 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185{
186 int ret;
187 FILE *f;
188 unsigned char output_buf[4096];
189 size_t len = 0;
190
191 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100192 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
193 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 return( ret );
195
196 len = strlen( (char *) output_buf );
197
198 if( ( f = fopen( output_file, "w" ) ) == NULL )
199 return( -1 );
200
201 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200202 {
203 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200205 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206
Paul Bakker0c226102014-04-17 16:02:36 +0200207 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208
209 return( 0 );
210}
211
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212int main( int argc, char *argv[] )
213{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100214 int ret = 1;
215 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_x509_crt issuer_crt;
217 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
218 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200219 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200220 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200221 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100222 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200225 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_x509write_cert crt;
229 mbedtls_mpi serial;
230 mbedtls_entropy_context entropy;
231 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200232 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200233
234 /*
235 * Set to sane values
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_pk_init( &loaded_issuer_key );
239 mbedtls_pk_init( &loaded_subject_key );
240 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200241 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100242 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_X509_CSR_PARSE_C)
244 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200247 memset( buf, 0, 1024 );
248
249 if( argc == 0 )
250 {
251 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253 goto exit;
254 }
255
Paul Bakker1014e952013-09-09 13:59:42 +0200256 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200257 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 opt.subject_key = DFL_SUBJECT_KEY;
259 opt.issuer_key = DFL_ISSUER_KEY;
260 opt.subject_pwd = DFL_SUBJECT_PWD;
261 opt.issuer_pwd = DFL_ISSUER_PWD;
262 opt.output_file = DFL_OUTPUT_FILENAME;
263 opt.subject_name = DFL_SUBJECT_NAME;
264 opt.issuer_name = DFL_ISSUER_NAME;
265 opt.not_before = DFL_NOT_BEFORE;
266 opt.not_after = DFL_NOT_AFTER;
267 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200268 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200269 opt.is_ca = DFL_IS_CA;
270 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271 opt.key_usage = DFL_KEY_USAGE;
272 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100273 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100274 opt.md = DFL_DIGEST;
275 opt.subject_identifier = DFL_SUBJ_IDENT;
276 opt.authority_identifier = DFL_AUTH_IDENT;
277 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200278
279 for( i = 1; i < argc; i++ )
280 {
281
282 p = argv[i];
283 if( ( q = strchr( p, '=' ) ) == NULL )
284 goto usage;
285 *q++ = '\0';
286
Paul Bakkere2673fb2013-09-09 15:52:07 +0200287 if( strcmp( p, "request_file" ) == 0 )
288 opt.request_file = q;
289 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200290 opt.subject_key = q;
291 else if( strcmp( p, "issuer_key" ) == 0 )
292 opt.issuer_key = q;
293 else if( strcmp( p, "subject_pwd" ) == 0 )
294 opt.subject_pwd = q;
295 else if( strcmp( p, "issuer_pwd" ) == 0 )
296 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200297 else if( strcmp( p, "issuer_crt" ) == 0 )
298 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 else if( strcmp( p, "output_file" ) == 0 )
300 opt.output_file = q;
301 else if( strcmp( p, "subject_name" ) == 0 )
302 {
303 opt.subject_name = q;
304 }
305 else if( strcmp( p, "issuer_name" ) == 0 )
306 {
307 opt.issuer_name = q;
308 }
309 else if( strcmp( p, "not_before" ) == 0 )
310 {
311 opt.not_before = q;
312 }
313 else if( strcmp( p, "not_after" ) == 0 )
314 {
315 opt.not_after = q;
316 }
317 else if( strcmp( p, "serial" ) == 0 )
318 {
319 opt.serial = q;
320 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100321 else if( strcmp( p, "authority_identifier" ) == 0 )
322 {
323 opt.authority_identifier = atoi( q );
324 if( opt.authority_identifier != 0 &&
325 opt.authority_identifier != 1 )
326 {
Hanno Becker17c32762017-10-03 14:56:04 +0100327 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100328 goto usage;
329 }
330 }
331 else if( strcmp( p, "subject_identifier" ) == 0 )
332 {
333 opt.subject_identifier = atoi( q );
334 if( opt.subject_identifier != 0 &&
335 opt.subject_identifier != 1 )
336 {
Hanno Becker17c32762017-10-03 14:56:04 +0100337 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100338 goto usage;
339 }
340 }
341 else if( strcmp( p, "basic_constraints" ) == 0 )
342 {
343 opt.basic_constraints = atoi( q );
344 if( opt.basic_constraints != 0 &&
345 opt.basic_constraints != 1 )
346 {
Hanno Becker17c32762017-10-03 14:56:04 +0100347 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100348 goto usage;
349 }
350 }
351 else if( strcmp( p, "md" ) == 0 )
352 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200353 const mbedtls_md_info_t *md_info =
354 mbedtls_md_info_from_string( q );
355 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100356 {
357 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100358 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100359 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200360 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100361 }
362 else if( strcmp( p, "version" ) == 0 )
363 {
364 opt.version = atoi( q );
365 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100366 {
367 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100368 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100369 }
Hanno Becker38eff432017-09-22 15:38:20 +0100370 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100371 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200372 else if( strcmp( p, "selfsign" ) == 0 )
373 {
374 opt.selfsign = atoi( q );
375 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100376 {
377 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200378 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100379 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200380 }
Paul Bakker15162a02013-09-06 19:27:21 +0200381 else if( strcmp( p, "is_ca" ) == 0 )
382 {
383 opt.is_ca = atoi( q );
384 if( opt.is_ca < 0 || opt.is_ca > 1 )
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 }
390 else if( strcmp( p, "max_pathlen" ) == 0 )
391 {
392 opt.max_pathlen = atoi( q );
393 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100394 {
395 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200396 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100397 }
Paul Bakker15162a02013-09-06 19:27:21 +0200398 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399 else if( strcmp( p, "key_usage" ) == 0 )
400 {
401 while( q != NULL )
402 {
403 if( ( r = strchr( q, ',' ) ) != NULL )
404 *r++ = '\0';
405
406 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200408 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200410 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100411 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200412 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100413 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200414 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100415 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200418 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200420 else
Hanno Becker17c32762017-10-03 14:56:04 +0100421 {
422 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100424 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425
426 q = r;
427 }
428 }
429 else if( strcmp( p, "ns_cert_type" ) == 0 )
430 {
431 while( q != NULL )
432 {
433 if( ( r = strchr( q, ',' ) ) != NULL )
434 *r++ = '\0';
435
436 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200438 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100439 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200440 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200442 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100445 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100447 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100449 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else
Hanno Becker17c32762017-10-03 14:56:04 +0100451 {
452 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100454 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455
456 q = r;
457 }
458 }
459 else
460 goto usage;
461 }
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200464
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200465 /*
466 * 0. Seed the PRNG
467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200469 fflush( stdout );
470
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200471 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200472 (const unsigned char *) pers,
473 strlen( pers ) ) ) != 0 )
474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100476 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
477 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200478 goto exit;
479 }
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200482
Paul Bakker9397dcb2013-09-06 09:55:26 +0200483 // Parse serial to MPI
484 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200486 fflush( stdout );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100491 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200492 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493 goto exit;
494 }
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200497
Paul Bakker1014e952013-09-09 13:59:42 +0200498 // Parse issuer certificate if present
499 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200500 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200501 {
502 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200503 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200506 fflush( stdout );
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100511 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200512 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200513 goto exit;
514 }
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200517 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200518 if( ret < 0 )
519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100521 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200522 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200523 goto exit;
524 }
525
Paul Bakkere2673fb2013-09-09 15:52:07 +0200526 opt.issuer_name = issuer_name;
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200529 }
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200532 // Parse certificate request if present
533 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200534 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200535 {
536 /*
537 * 1.0.b. Load the CSR
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200540 fflush( stdout );
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100545 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200546 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200547 goto exit;
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200551 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200552 if( ret < 0 )
553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100555 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200556 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200557 goto exit;
558 }
559
560 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200561 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200566
567 /*
568 * 1.1. Load the keys
569 */
570 if( !opt.selfsign && !strlen( opt.request_file ) )
571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200573 fflush( stdout );
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200576 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200577 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100580 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200581 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200582 goto exit;
583 }
Paul Bakker1014e952013-09-09 13:59:42 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200589 fflush( stdout );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200592 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200593 if( ret != 0 )
594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100596 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200597 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200598 goto exit;
599 }
600
601 // Check if key and issuer certificate match
602 //
603 if( strlen( opt.issuer_crt ) )
604 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200605 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200606 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200607 {
Hanno Becker81535d02017-09-13 15:39:59 +0100608 mbedtls_printf( " failed\n ! issuer_key does not match "
609 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200610 goto exit;
611 }
612 }
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615
616 if( opt.selfsign )
617 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100618 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200619 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200620 }
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
623 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200624
Paul Bakker9397dcb2013-09-06 09:55:26 +0200625 /*
626 * 1.0. Check the names for validity
627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100631 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200632 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200633 goto exit;
634 }
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100639 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200640 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200641 goto exit;
642 }
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 fflush( stdout );
646
Hanno Becker38eff432017-09-22 15:38:20 +0100647 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100648 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
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_serial "
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 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660 if( ret != 0 )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100663 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200664 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200665 goto exit;
666 }
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200669
Hanno Becker38eff432017-09-22 15:38:20 +0100670 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
671 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200672 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100673 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
674 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200675
Hanno Becker6c13d372017-09-13 12:49:22 +0100676 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
677 opt.max_pathlen );
678 if( ret != 0 )
679 {
680 mbedtls_strerror( ret, buf, 1024 );
681 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200682 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100683 goto exit;
684 }
685
686 mbedtls_printf( " ok\n" );
687 }
Paul Bakker15162a02013-09-06 19:27:21 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100690 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
691 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200692 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100693 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
694 fflush( stdout );
695
696 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
697 if( ret != 0 )
698 {
699 mbedtls_strerror( ret, buf, 1024 );
700 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100701 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200702 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100703 goto exit;
704 }
705
706 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200707 }
708
Hanno Becker38eff432017-09-22 15:38:20 +0100709 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
710 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200711 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100712 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
713 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200714
Hanno Becker6c13d372017-09-13 12:49:22 +0100715 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
716 if( ret != 0 )
717 {
718 mbedtls_strerror( ret, buf, 1024 );
719 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100720 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200721 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100722 goto exit;
723 }
724
725 mbedtls_printf( " ok\n" );
726 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200728
Hanno Becker38eff432017-09-22 15:38:20 +0100729 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
730 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200733 fflush( stdout );
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200736 if( ret != 0 )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100739 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200740 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200741 goto exit;
742 }
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200745 }
746
Hanno Becker38eff432017-09-22 15:38:20 +0100747 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
748 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200751 fflush( stdout );
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200754 if( ret != 0 )
755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100757 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200758 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200759 goto exit;
760 }
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200763 }
764
Paul Bakker9397dcb2013-09-06 09:55:26 +0200765 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100766 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200769 fflush( stdout );
770
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200771 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100775 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200776 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200777 goto exit;
778 }
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200781
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100782 exit_code = MBEDTLS_EXIT_SUCCESS;
783
Paul Bakker9397dcb2013-09-06 09:55:26 +0200784exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100785#if defined(MBEDTLS_X509_CSR_PARSE_C)
786 mbedtls_x509_csr_free( &csr );
787#endif /* MBEDTLS_X509_CSR_PARSE_C */
788 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_x509write_crt_free( &crt );
790 mbedtls_pk_free( &loaded_subject_key );
791 mbedtls_pk_free( &loaded_issuer_key );
792 mbedtls_mpi_free( &serial );
793 mbedtls_ctr_drbg_free( &ctr_drbg );
794 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200795
796#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200798 fflush( stdout ); getchar();
799#endif
800
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200801 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
804 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100805 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */