blob: ef40447be94962a6af7823e286b7e2a784d804e0 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Simon Butcher203a6932016-10-07 15:00:17 +010039#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
40 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
42 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
43 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020044int main( void )
45{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 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 +020047 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
49 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020050 return( 0 );
51}
52#else
53
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/x509_crt.h"
55#include "mbedtls/x509_csr.h"
56#include "mbedtls/entropy.h"
57#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010058#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020060
Rich Evans18b78c72015-02-11 14:06:19 +000061#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000066#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010067 " request_file=%%s default: (empty)\n" \
68 " If request_file is specified, subject_key,\n" \
69 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000070#else
71#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000073
Paul Bakker1014e952013-09-09 13:59:42 +020074#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020075#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020076#define DFL_SUBJECT_KEY "subject.key"
77#define DFL_ISSUER_KEY "ca.key"
78#define DFL_SUBJECT_PWD ""
79#define DFL_ISSUER_PWD ""
80#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000081#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
82#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020083#define DFL_NOT_BEFORE "20010101000000"
84#define DFL_NOT_AFTER "20301231235959"
85#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020086#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020087#define DFL_IS_CA 0
88#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020089#define DFL_KEY_USAGE 0
90#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010091#define DFL_VERSION 3
92#define DFL_AUTH_IDENT 1
93#define DFL_SUBJ_IDENT 1
94#define DFL_CONSTRAINTS 1
95#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020096
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE \
98 "\n usage: cert_write param=<>...\n" \
99 "\n acceptable parameters:\n" \
100 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100101 " subject_key=%%s default: subject.key\n" \
102 " subject_pwd=%%s default: (empty)\n" \
103 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " issuer_crt=%%s default: (empty)\n" \
106 " If issuer_crt is specified, issuer_name is\n" \
107 " ignored!\n" \
108 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100110 " selfsign=%%d default: 0 (false)\n" \
111 " If selfsign is enabled, issuer_name and\n" \
112 " issuer_key are required (issuer_crt and\n" \
113 " subject_* are ignored\n" \
114 " issuer_key=%%s default: ca.key\n" \
115 " issuer_pwd=%%s default: (empty)\n" \
116 " output_file=%%s default: cert.crt\n" \
117 " serial=%%s default: 1\n" \
118 " not_before=%%s default: 20010101000000\n"\
119 " not_after=%%s default: 20301231235959\n"\
120 " is_ca=%%d default: 0 (disabled)\n" \
121 " max_pathlen=%%d default: -1 (none)\n" \
122 " md=%%s default: SHA256\n" \
123 " Supported values:\n" \
Hanno Beckerd0f2d812019-06-03 14:36:59 +0100124 " MD2, MD4, MD5, SHA1, SHA256, SHA512\n"\
Hanno Becker81535d02017-09-13 15:39:59 +0100125 " version=%%d default: 3\n" \
126 " Possible values: 1, 2, 3\n"\
127 " subject_identifier=%%s default: 1\n" \
128 " Possible values: 0, 1\n" \
129 " (Considered for v3 only)\n"\
130 " authority_identifier=%%s default: 1\n" \
131 " Possible values: 0, 1\n" \
132 " (Considered for v3 only)\n"\
133 " basic_constraints=%%d default: 1\n" \
134 " Possible values: 0, 1\n" \
135 " (Considered for v3 only)\n"\
136 " key_usage=%%s default: (empty)\n" \
137 " Comma-separated-list of values:\n" \
138 " digital_signature\n" \
139 " non_repudiation\n" \
140 " key_encipherment\n" \
141 " data_encipherment\n" \
142 " key_agreement\n" \
143 " key_cert_sign\n" \
144 " crl_sign\n" \
145 " (Considered for v3 only)\n"\
146 " ns_cert_type=%%s default: (empty)\n" \
147 " Comma-separated-list of values:\n" \
148 " ssl_client\n" \
149 " ssl_server\n" \
150 " email\n" \
151 " object_signing\n" \
152 " ssl_ca\n" \
153 " email_ca\n" \
154 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000155 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000156
Simon Butcher63cb97e2018-12-06 17:43:31 +0000157
Paul Bakker9397dcb2013-09-06 09:55:26 +0200158/*
159 * global options
160 */
161struct options
162{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100163 const char *issuer_crt; /* filename of the issuer certificate */
164 const char *request_file; /* filename of the certificate request */
165 const char *subject_key; /* filename of the subject key file */
166 const char *issuer_key; /* filename of the issuer key file */
167 const char *subject_pwd; /* password for the subject key file */
168 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100169 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100170 const char *subject_name; /* subject name for certificate */
171 const char *issuer_name; /* issuer name for certificate */
172 const char *not_before; /* validity period not before */
173 const char *not_after; /* validity period not after */
174 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200175 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200176 int is_ca; /* is a CA certificate */
177 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100178 int authority_identifier; /* add authority identifier to CRT */
179 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100180 int basic_constraints; /* add basic constraints ext to CRT */
181 int version; /* CRT version */
182 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200183 unsigned char key_usage; /* key usage flags */
184 unsigned char ns_cert_type; /* NS cert type */
185} opt;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200188 int (*f_rng)(void *, unsigned char *, size_t),
189 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200190{
191 int ret;
192 FILE *f;
193 unsigned char output_buf[4096];
194 size_t len = 0;
195
196 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100197 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
198 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 return( ret );
200
201 len = strlen( (char *) output_buf );
202
203 if( ( f = fopen( output_file, "w" ) ) == NULL )
204 return( -1 );
205
206 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200207 {
208 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200210 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211
Paul Bakker0c226102014-04-17 16:02:36 +0200212 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213
214 return( 0 );
215}
216
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217int main( int argc, char *argv[] )
218{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100219 int ret = 1;
220 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_x509_crt issuer_crt;
222 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
223 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200224 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200226 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100227 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200230 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_x509write_cert crt;
234 mbedtls_mpi serial;
235 mbedtls_entropy_context entropy;
236 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200237 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238
239 /*
240 * Set to sane values
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_pk_init( &loaded_issuer_key );
244 mbedtls_pk_init( &loaded_subject_key );
245 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200246 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100247 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_X509_CSR_PARSE_C)
249 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252 memset( buf, 0, 1024 );
253
254 if( argc == 0 )
255 {
256 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 goto exit;
259 }
260
Paul Bakker1014e952013-09-09 13:59:42 +0200261 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200262 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200263 opt.subject_key = DFL_SUBJECT_KEY;
264 opt.issuer_key = DFL_ISSUER_KEY;
265 opt.subject_pwd = DFL_SUBJECT_PWD;
266 opt.issuer_pwd = DFL_ISSUER_PWD;
267 opt.output_file = DFL_OUTPUT_FILENAME;
268 opt.subject_name = DFL_SUBJECT_NAME;
269 opt.issuer_name = DFL_ISSUER_NAME;
270 opt.not_before = DFL_NOT_BEFORE;
271 opt.not_after = DFL_NOT_AFTER;
272 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200273 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200274 opt.is_ca = DFL_IS_CA;
275 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276 opt.key_usage = DFL_KEY_USAGE;
277 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100278 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100279 opt.md = DFL_DIGEST;
280 opt.subject_identifier = DFL_SUBJ_IDENT;
281 opt.authority_identifier = DFL_AUTH_IDENT;
282 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200283
284 for( i = 1; i < argc; i++ )
285 {
286
287 p = argv[i];
288 if( ( q = strchr( p, '=' ) ) == NULL )
289 goto usage;
290 *q++ = '\0';
291
Paul Bakkere2673fb2013-09-09 15:52:07 +0200292 if( strcmp( p, "request_file" ) == 0 )
293 opt.request_file = q;
294 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295 opt.subject_key = q;
296 else if( strcmp( p, "issuer_key" ) == 0 )
297 opt.issuer_key = q;
298 else if( strcmp( p, "subject_pwd" ) == 0 )
299 opt.subject_pwd = q;
300 else if( strcmp( p, "issuer_pwd" ) == 0 )
301 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200302 else if( strcmp( p, "issuer_crt" ) == 0 )
303 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200304 else if( strcmp( p, "output_file" ) == 0 )
305 opt.output_file = q;
306 else if( strcmp( p, "subject_name" ) == 0 )
307 {
308 opt.subject_name = q;
309 }
310 else if( strcmp( p, "issuer_name" ) == 0 )
311 {
312 opt.issuer_name = q;
313 }
314 else if( strcmp( p, "not_before" ) == 0 )
315 {
316 opt.not_before = q;
317 }
318 else if( strcmp( p, "not_after" ) == 0 )
319 {
320 opt.not_after = q;
321 }
322 else if( strcmp( p, "serial" ) == 0 )
323 {
324 opt.serial = q;
325 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100326 else if( strcmp( p, "authority_identifier" ) == 0 )
327 {
328 opt.authority_identifier = atoi( q );
329 if( opt.authority_identifier != 0 &&
330 opt.authority_identifier != 1 )
331 {
Hanno Becker17c32762017-10-03 14:56:04 +0100332 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100333 goto usage;
334 }
335 }
336 else if( strcmp( p, "subject_identifier" ) == 0 )
337 {
338 opt.subject_identifier = atoi( q );
339 if( opt.subject_identifier != 0 &&
340 opt.subject_identifier != 1 )
341 {
Hanno Becker17c32762017-10-03 14:56:04 +0100342 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100343 goto usage;
344 }
345 }
346 else if( strcmp( p, "basic_constraints" ) == 0 )
347 {
348 opt.basic_constraints = atoi( q );
349 if( opt.basic_constraints != 0 &&
350 opt.basic_constraints != 1 )
351 {
Hanno Becker17c32762017-10-03 14:56:04 +0100352 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100353 goto usage;
354 }
355 }
356 else if( strcmp( p, "md" ) == 0 )
357 {
358 if( strcmp( q, "SHA1" ) == 0 )
359 opt.md = MBEDTLS_MD_SHA1;
360 else if( strcmp( q, "SHA256" ) == 0 )
361 opt.md = MBEDTLS_MD_SHA256;
362 else if( strcmp( q, "SHA512" ) == 0 )
363 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker8a0193e2019-06-03 14:10:44 +0100364 else if( strcmp( q, "MD2" ) == 0 )
365 opt.md = MBEDTLS_MD_MD2;
366 else if( strcmp( q, "MD4" ) == 0 )
367 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100368 else if( strcmp( q, "MD5" ) == 0 )
369 opt.md = MBEDTLS_MD_MD5;
370 else
Hanno Becker17c32762017-10-03 14:56:04 +0100371 {
372 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100373 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100374 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100375 }
376 else if( strcmp( p, "version" ) == 0 )
377 {
378 opt.version = atoi( q );
379 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100380 {
381 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100382 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100383 }
Hanno Becker38eff432017-09-22 15:38:20 +0100384 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100385 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200386 else if( strcmp( p, "selfsign" ) == 0 )
387 {
388 opt.selfsign = atoi( q );
389 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100390 {
391 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200392 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100393 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200394 }
Paul Bakker15162a02013-09-06 19:27:21 +0200395 else if( strcmp( p, "is_ca" ) == 0 )
396 {
397 opt.is_ca = atoi( q );
398 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100399 {
400 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200401 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100402 }
Paul Bakker15162a02013-09-06 19:27:21 +0200403 }
404 else if( strcmp( p, "max_pathlen" ) == 0 )
405 {
406 opt.max_pathlen = atoi( q );
407 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100408 {
409 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200410 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100411 }
Paul Bakker15162a02013-09-06 19:27:21 +0200412 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 else if( strcmp( p, "key_usage" ) == 0 )
414 {
415 while( q != NULL )
416 {
417 if( ( r = strchr( q, ',' ) ) != NULL )
418 *r++ = '\0';
419
420 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200422 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200424 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100425 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200426 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100427 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200428 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100429 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200430 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200432 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434 else
Hanno Becker17c32762017-10-03 14:56:04 +0100435 {
436 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100438 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439
440 q = r;
441 }
442 }
443 else if( strcmp( p, "ns_cert_type" ) == 0 )
444 {
445 while( q != NULL )
446 {
447 if( ( r = strchr( q, ',' ) ) != NULL )
448 *r++ = '\0';
449
450 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100453 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100459 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200460 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100461 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200462 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100463 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200464 else
Hanno Becker17c32762017-10-03 14:56:04 +0100465 {
466 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200467 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100468 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200469
470 q = r;
471 }
472 }
473 else
474 goto usage;
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200478
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200479 /*
480 * 0. Seed the PRNG
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200483 fflush( stdout );
484
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200485 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200486 (const unsigned char *) pers,
487 strlen( pers ) ) ) != 0 )
488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100490 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
491 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200492 goto exit;
493 }
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200496
Paul Bakker9397dcb2013-09-06 09:55:26 +0200497 // Parse serial to MPI
498 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200500 fflush( stdout );
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100505 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100506 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200507 goto exit;
508 }
509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200511
Paul Bakker1014e952013-09-09 13:59:42 +0200512 // Parse issuer certificate if present
513 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200515 {
Hanno Beckerd8eab342019-02-26 18:47:11 +0000516 mbedtls_x509_name *subject;
517
Paul Bakker1014e952013-09-09 13:59:42 +0200518 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200519 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200522 fflush( stdout );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100527 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100528 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200529 goto exit;
530 }
531
Hanno Beckerd8eab342019-02-26 18:47:11 +0000532 ret = mbedtls_x509_crt_get_subject( &issuer_crt, &subject );
533 if( ret != 0 )
534 {
535 mbedtls_strerror( ret, buf, 1024 );
536 mbedtls_printf( " failed\n ! mbedtls_x509_crt_get_subject "
537 "returned -0x%04x - %s\n\n", -ret, buf );
538 goto exit;
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Hanno Beckerd8eab342019-02-26 18:47:11 +0000542 subject );
Paul Bakker1014e952013-09-09 13:59:42 +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 "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100547 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200548 goto exit;
549 }
550
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 opt.issuer_name = issuer_name;
552
Hanno Beckerd8eab342019-02-26 18:47:11 +0000553 mbedtls_x509_name_free( subject );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200556 }
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200559 // Parse certificate request if present
560 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200561 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200562 {
563 /*
564 * 1.0.b. Load the CSR
565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200567 fflush( stdout );
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100572 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100573 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200574 goto exit;
575 }
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 &csr.subject );
579 if( ret < 0 )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100582 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100583 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200584 goto exit;
585 }
586
587 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200588 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200591 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200593
594 /*
595 * 1.1. Load the keys
596 */
597 if( !opt.selfsign && !strlen( opt.request_file ) )
598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200600 fflush( stdout );
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200603 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200604 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100607 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100608 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200609 goto exit;
610 }
Paul Bakker1014e952013-09-09 13:59:42 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200616 fflush( stdout );
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200620 if( ret != 0 )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100623 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
624 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200625 goto exit;
626 }
627
628 // Check if key and issuer certificate match
629 //
630 if( strlen( opt.issuer_crt ) )
631 {
Hanno Beckerd8eab342019-02-26 18:47:11 +0000632 mbedtls_pk_context pk;
633 ret = mbedtls_x509_crt_get_pk( &issuer_crt, &pk );
634 if( ret != 0 )
635 {
636 mbedtls_strerror( ret, buf, 1024 );
637 mbedtls_printf( " failed\n ! mbedtls_x509_crt_get_pk "
638 "returned -0x%04x - %s\n\n", -ret, buf );
639 goto exit;
640 }
641
642 if( mbedtls_pk_check_pair( &pk, issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200643 {
Hanno Becker81535d02017-09-13 15:39:59 +0100644 mbedtls_printf( " failed\n ! issuer_key does not match "
645 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200646 goto exit;
647 }
Hanno Beckerd8eab342019-02-26 18:47:11 +0000648
649 mbedtls_pk_free( &pk );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200650 }
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200653
654 if( opt.selfsign )
655 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100656 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200657 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200658 }
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
661 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200662
Paul Bakker9397dcb2013-09-06 09:55:26 +0200663 /*
664 * 1.0. Check the names for validity
665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100669 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100670 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200671 goto exit;
672 }
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100677 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100678 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200679 goto exit;
680 }
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200683 fflush( stdout );
684
Hanno Becker38eff432017-09-22 15:38:20 +0100685 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100686 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200689 if( ret != 0 )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100692 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100693 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200694 goto exit;
695 }
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200698 if( ret != 0 )
699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100701 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100702 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200703 goto exit;
704 }
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200707
Hanno Becker38eff432017-09-22 15:38:20 +0100708 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
709 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200710 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100711 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
712 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200713
Hanno Becker6c13d372017-09-13 12:49:22 +0100714 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
715 opt.max_pathlen );
716 if( ret != 0 )
717 {
718 mbedtls_strerror( ret, buf, 1024 );
719 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100720 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100721 goto exit;
722 }
723
724 mbedtls_printf( " ok\n" );
725 }
Paul Bakker15162a02013-09-06 19:27:21 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100728 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
729 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200730 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100731 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
732 fflush( stdout );
733
734 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
735 if( ret != 0 )
736 {
737 mbedtls_strerror( ret, buf, 1024 );
738 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100739 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100740 -ret, buf );
741 goto exit;
742 }
743
744 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200745 }
746
Hanno Becker38eff432017-09-22 15:38:20 +0100747 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
748 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200749 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100750 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
751 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200752
Hanno Becker6c13d372017-09-13 12:49:22 +0100753 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
754 if( ret != 0 )
755 {
756 mbedtls_strerror( ret, buf, 1024 );
757 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100758 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100759 -ret, buf );
760 goto exit;
761 }
762
763 mbedtls_printf( " ok\n" );
764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200766
Hanno Becker38eff432017-09-22 15:38:20 +0100767 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
768 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200771 fflush( stdout );
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200774 if( ret != 0 )
775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100777 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100778 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200779 goto exit;
780 }
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200783 }
784
Hanno Becker38eff432017-09-22 15:38:20 +0100785 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
786 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200789 fflush( stdout );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200792 if( ret != 0 )
793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100795 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100796 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200797 goto exit;
798 }
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200801 }
802
Paul Bakker9397dcb2013-09-06 09:55:26 +0200803 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100804 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200807 fflush( stdout );
808
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200809 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100813 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100814 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200815 goto exit;
816 }
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200819
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100820 exit_code = MBEDTLS_EXIT_SUCCESS;
821
Paul Bakker9397dcb2013-09-06 09:55:26 +0200822exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100823#if defined(MBEDTLS_X509_CSR_PARSE_C)
824 mbedtls_x509_csr_free( &csr );
825#endif /* MBEDTLS_X509_CSR_PARSE_C */
826 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_x509write_crt_free( &crt );
828 mbedtls_pk_free( &loaded_subject_key );
829 mbedtls_pk_free( &loaded_issuer_key );
830 mbedtls_mpi_free( &serial );
831 mbedtls_ctr_drbg_free( &ctr_drbg );
832 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200833
834#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200836 fflush( stdout ); getchar();
837#endif
838
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100839 return( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
842 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100843 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */