blob: 521f25a93b7fb2c1b9c8cfdb5851d0f78dbb5eff [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
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100157#if defined(MBEDTLS_CHECK_PARAMS)
158#define mbedtls_exit exit
159void mbedtls_param_failed( const char *failure_condition,
160 const char *file,
161 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000162{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100163 mbedtls_printf( "%s:%i: Input param failed - %s\n",
164 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000165 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
166}
167#endif
168
Paul Bakker9397dcb2013-09-06 09:55:26 +0200169/*
170 * global options
171 */
172struct options
173{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100174 const char *issuer_crt; /* filename of the issuer certificate */
175 const char *request_file; /* filename of the certificate request */
176 const char *subject_key; /* filename of the subject key file */
177 const char *issuer_key; /* filename of the issuer key file */
178 const char *subject_pwd; /* password for the subject key file */
179 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100180 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100181 const char *subject_name; /* subject name for certificate */
182 const char *issuer_name; /* issuer name for certificate */
183 const char *not_before; /* validity period not before */
184 const char *not_after; /* validity period not after */
185 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200186 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200187 int is_ca; /* is a CA certificate */
188 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100189 int authority_identifier; /* add authority identifier to CRT */
190 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100191 int basic_constraints; /* add basic constraints ext to CRT */
192 int version; /* CRT version */
193 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 unsigned char key_usage; /* key usage flags */
195 unsigned char ns_cert_type; /* NS cert type */
196} opt;
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200199 int (*f_rng)(void *, unsigned char *, size_t),
200 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201{
202 int ret;
203 FILE *f;
204 unsigned char output_buf[4096];
205 size_t len = 0;
206
207 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100208 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
209 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210 return( ret );
211
212 len = strlen( (char *) output_buf );
213
214 if( ( f = fopen( output_file, "w" ) ) == NULL )
215 return( -1 );
216
217 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200218 {
219 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200220 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200221 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200222
Paul Bakker0c226102014-04-17 16:02:36 +0200223 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224
225 return( 0 );
226}
227
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228int main( int argc, char *argv[] )
229{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100230 int ret = 1;
231 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509_crt issuer_crt;
233 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
234 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200235 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200237 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100238 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200241 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509write_cert crt;
245 mbedtls_mpi serial;
246 mbedtls_entropy_context entropy;
247 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200248 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
250 /*
251 * Set to sane values
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pk_init( &loaded_issuer_key );
255 mbedtls_pk_init( &loaded_subject_key );
256 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200257 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100258 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_X509_CSR_PARSE_C)
260 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200263 memset( buf, 0, 1024 );
264
265 if( argc == 0 )
266 {
267 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269 goto exit;
270 }
271
Paul Bakker1014e952013-09-09 13:59:42 +0200272 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200273 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.subject_key = DFL_SUBJECT_KEY;
275 opt.issuer_key = DFL_ISSUER_KEY;
276 opt.subject_pwd = DFL_SUBJECT_PWD;
277 opt.issuer_pwd = DFL_ISSUER_PWD;
278 opt.output_file = DFL_OUTPUT_FILENAME;
279 opt.subject_name = DFL_SUBJECT_NAME;
280 opt.issuer_name = DFL_ISSUER_NAME;
281 opt.not_before = DFL_NOT_BEFORE;
282 opt.not_after = DFL_NOT_AFTER;
283 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200284 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200285 opt.is_ca = DFL_IS_CA;
286 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.key_usage = DFL_KEY_USAGE;
288 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100289 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100290 opt.md = DFL_DIGEST;
291 opt.subject_identifier = DFL_SUBJ_IDENT;
292 opt.authority_identifier = DFL_AUTH_IDENT;
293 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294
295 for( i = 1; i < argc; i++ )
296 {
297
298 p = argv[i];
299 if( ( q = strchr( p, '=' ) ) == NULL )
300 goto usage;
301 *q++ = '\0';
302
Paul Bakkere2673fb2013-09-09 15:52:07 +0200303 if( strcmp( p, "request_file" ) == 0 )
304 opt.request_file = q;
305 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306 opt.subject_key = q;
307 else if( strcmp( p, "issuer_key" ) == 0 )
308 opt.issuer_key = q;
309 else if( strcmp( p, "subject_pwd" ) == 0 )
310 opt.subject_pwd = q;
311 else if( strcmp( p, "issuer_pwd" ) == 0 )
312 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200313 else if( strcmp( p, "issuer_crt" ) == 0 )
314 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200315 else if( strcmp( p, "output_file" ) == 0 )
316 opt.output_file = q;
317 else if( strcmp( p, "subject_name" ) == 0 )
318 {
319 opt.subject_name = q;
320 }
321 else if( strcmp( p, "issuer_name" ) == 0 )
322 {
323 opt.issuer_name = q;
324 }
325 else if( strcmp( p, "not_before" ) == 0 )
326 {
327 opt.not_before = q;
328 }
329 else if( strcmp( p, "not_after" ) == 0 )
330 {
331 opt.not_after = q;
332 }
333 else if( strcmp( p, "serial" ) == 0 )
334 {
335 opt.serial = q;
336 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100337 else if( strcmp( p, "authority_identifier" ) == 0 )
338 {
339 opt.authority_identifier = atoi( q );
340 if( opt.authority_identifier != 0 &&
341 opt.authority_identifier != 1 )
342 {
Hanno Becker17c32762017-10-03 14:56:04 +0100343 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100344 goto usage;
345 }
346 }
347 else if( strcmp( p, "subject_identifier" ) == 0 )
348 {
349 opt.subject_identifier = atoi( q );
350 if( opt.subject_identifier != 0 &&
351 opt.subject_identifier != 1 )
352 {
Hanno Becker17c32762017-10-03 14:56:04 +0100353 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100354 goto usage;
355 }
356 }
357 else if( strcmp( p, "basic_constraints" ) == 0 )
358 {
359 opt.basic_constraints = atoi( q );
360 if( opt.basic_constraints != 0 &&
361 opt.basic_constraints != 1 )
362 {
Hanno Becker17c32762017-10-03 14:56:04 +0100363 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100364 goto usage;
365 }
366 }
367 else if( strcmp( p, "md" ) == 0 )
368 {
369 if( strcmp( q, "SHA1" ) == 0 )
370 opt.md = MBEDTLS_MD_SHA1;
371 else if( strcmp( q, "SHA256" ) == 0 )
372 opt.md = MBEDTLS_MD_SHA256;
373 else if( strcmp( q, "SHA512" ) == 0 )
374 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker8a0193e2019-06-03 14:10:44 +0100375 else if( strcmp( q, "MD2" ) == 0 )
376 opt.md = MBEDTLS_MD_MD2;
377 else if( strcmp( q, "MD4" ) == 0 )
378 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100379 else if( strcmp( q, "MD5" ) == 0 )
380 opt.md = MBEDTLS_MD_MD5;
381 else
Hanno Becker17c32762017-10-03 14:56:04 +0100382 {
383 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100384 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100385 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100386 }
387 else if( strcmp( p, "version" ) == 0 )
388 {
389 opt.version = atoi( q );
390 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100391 {
392 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100393 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100394 }
Hanno Becker38eff432017-09-22 15:38:20 +0100395 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100396 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200397 else if( strcmp( p, "selfsign" ) == 0 )
398 {
399 opt.selfsign = atoi( q );
400 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100401 {
402 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200403 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100404 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200405 }
Paul Bakker15162a02013-09-06 19:27:21 +0200406 else if( strcmp( p, "is_ca" ) == 0 )
407 {
408 opt.is_ca = atoi( q );
409 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100410 {
411 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200412 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100413 }
Paul Bakker15162a02013-09-06 19:27:21 +0200414 }
415 else if( strcmp( p, "max_pathlen" ) == 0 )
416 {
417 opt.max_pathlen = atoi( q );
418 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100419 {
420 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200421 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100422 }
Paul Bakker15162a02013-09-06 19:27:21 +0200423 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200424 else if( strcmp( p, "key_usage" ) == 0 )
425 {
426 while( q != NULL )
427 {
428 if( ( r = strchr( q, ',' ) ) != NULL )
429 *r++ = '\0';
430
431 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100436 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100438 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100440 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200443 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200445 else
Hanno Becker17c32762017-10-03 14:56:04 +0100446 {
447 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100449 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450
451 q = r;
452 }
453 }
454 else if( strcmp( p, "ns_cert_type" ) == 0 )
455 {
456 while( q != NULL )
457 {
458 if( ( r = strchr( q, ',' ) ) != NULL )
459 *r++ = '\0';
460
461 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100464 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200465 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200467 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200469 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100470 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200471 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100472 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200473 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100474 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200475 else
Hanno Becker17c32762017-10-03 14:56:04 +0100476 {
477 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200478 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100479 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200480
481 q = r;
482 }
483 }
484 else
485 goto usage;
486 }
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200489
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200490 /*
491 * 0. Seed the PRNG
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200494 fflush( stdout );
495
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200496 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200497 (const unsigned char *) pers,
498 strlen( pers ) ) ) != 0 )
499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100501 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
502 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200503 goto exit;
504 }
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200507
Paul Bakker9397dcb2013-09-06 09:55:26 +0200508 // Parse serial to MPI
509 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200511 fflush( stdout );
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100516 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100517 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200518 goto exit;
519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200522
Paul Bakker1014e952013-09-09 13:59:42 +0200523 // Parse issuer certificate if present
524 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200525 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200526 {
Hanno Beckerd8eab342019-02-26 18:47:11 +0000527 mbedtls_x509_name *subject;
528
Paul Bakker1014e952013-09-09 13:59:42 +0200529 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200530 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200533 fflush( stdout );
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100538 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100539 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200540 goto exit;
541 }
542
Hanno Beckerd8eab342019-02-26 18:47:11 +0000543 ret = mbedtls_x509_crt_get_subject( &issuer_crt, &subject );
544 if( ret != 0 )
545 {
546 mbedtls_strerror( ret, buf, 1024 );
547 mbedtls_printf( " failed\n ! mbedtls_x509_crt_get_subject "
548 "returned -0x%04x - %s\n\n", -ret, buf );
549 goto exit;
550 }
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Hanno Beckerd8eab342019-02-26 18:47:11 +0000553 subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200554 if( ret < 0 )
555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100557 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100558 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200559 goto exit;
560 }
561
Paul Bakkere2673fb2013-09-09 15:52:07 +0200562 opt.issuer_name = issuer_name;
563
Hanno Beckerd8eab342019-02-26 18:47:11 +0000564 mbedtls_x509_name_free( subject );
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200567 }
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200570 // Parse certificate request if present
571 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200572 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200573 {
574 /*
575 * 1.0.b. Load the CSR
576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 fflush( stdout );
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100583 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100584 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200585 goto exit;
586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589 &csr.subject );
590 if( ret < 0 )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100593 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100594 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200595 goto exit;
596 }
597
598 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200599 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200604
605 /*
606 * 1.1. Load the keys
607 */
608 if( !opt.selfsign && !strlen( opt.request_file ) )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200611 fflush( stdout );
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200614 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100618 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100619 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200620 goto exit;
621 }
Paul Bakker1014e952013-09-09 13:59:42 +0200622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200624 }
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200627 fflush( stdout );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200631 if( ret != 0 )
632 {
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_pk_parse_keyfile "
635 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200636 goto exit;
637 }
638
639 // Check if key and issuer certificate match
640 //
641 if( strlen( opt.issuer_crt ) )
642 {
Hanno Beckerd8eab342019-02-26 18:47:11 +0000643 mbedtls_pk_context pk;
644 ret = mbedtls_x509_crt_get_pk( &issuer_crt, &pk );
645 if( ret != 0 )
646 {
647 mbedtls_strerror( ret, buf, 1024 );
648 mbedtls_printf( " failed\n ! mbedtls_x509_crt_get_pk "
649 "returned -0x%04x - %s\n\n", -ret, buf );
650 goto exit;
651 }
652
653 if( mbedtls_pk_check_pair( &pk, issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200654 {
Hanno Becker81535d02017-09-13 15:39:59 +0100655 mbedtls_printf( " failed\n ! issuer_key does not match "
656 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200657 goto exit;
658 }
Hanno Beckerd8eab342019-02-26 18:47:11 +0000659
660 mbedtls_pk_free( &pk );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200661 }
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200664
665 if( opt.selfsign )
666 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100667 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200668 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
672 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200673
Paul Bakker9397dcb2013-09-06 09:55:26 +0200674 /*
675 * 1.0. Check the names for validity
676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100680 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100681 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200682 goto exit;
683 }
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100688 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100689 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200690 goto exit;
691 }
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200694 fflush( stdout );
695
Hanno Becker38eff432017-09-22 15:38:20 +0100696 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100697 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200700 if( ret != 0 )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100703 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100704 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705 goto exit;
706 }
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200709 if( ret != 0 )
710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100712 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100713 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200714 goto exit;
715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200718
Hanno Becker38eff432017-09-22 15:38:20 +0100719 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
720 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200721 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100722 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
723 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200724
Hanno Becker6c13d372017-09-13 12:49:22 +0100725 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
726 opt.max_pathlen );
727 if( ret != 0 )
728 {
729 mbedtls_strerror( ret, buf, 1024 );
730 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100731 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100732 goto exit;
733 }
734
735 mbedtls_printf( " ok\n" );
736 }
Paul Bakker15162a02013-09-06 19:27:21 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100739 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
740 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200741 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100742 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
743 fflush( stdout );
744
745 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
746 if( ret != 0 )
747 {
748 mbedtls_strerror( ret, buf, 1024 );
749 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100750 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100751 -ret, buf );
752 goto exit;
753 }
754
755 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200756 }
757
Hanno Becker38eff432017-09-22 15:38:20 +0100758 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
759 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200760 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100761 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
762 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200763
Hanno Becker6c13d372017-09-13 12:49:22 +0100764 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
765 if( ret != 0 )
766 {
767 mbedtls_strerror( ret, buf, 1024 );
768 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100769 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100770 -ret, buf );
771 goto exit;
772 }
773
774 mbedtls_printf( " ok\n" );
775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200777
Hanno Becker38eff432017-09-22 15:38:20 +0100778 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
779 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200782 fflush( stdout );
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200785 if( ret != 0 )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100788 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100789 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200790 goto exit;
791 }
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200794 }
795
Hanno Becker38eff432017-09-22 15:38:20 +0100796 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
797 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200800 fflush( stdout );
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200803 if( ret != 0 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100806 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100807 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200808 goto exit;
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200812 }
813
Paul Bakker9397dcb2013-09-06 09:55:26 +0200814 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100815 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200818 fflush( stdout );
819
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200820 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100824 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100825 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200826 goto exit;
827 }
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200830
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100831 exit_code = MBEDTLS_EXIT_SUCCESS;
832
Paul Bakker9397dcb2013-09-06 09:55:26 +0200833exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100834#if defined(MBEDTLS_X509_CSR_PARSE_C)
835 mbedtls_x509_csr_free( &csr );
836#endif /* MBEDTLS_X509_CSR_PARSE_C */
837 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_x509write_crt_free( &crt );
839 mbedtls_pk_free( &loaded_subject_key );
840 mbedtls_pk_free( &loaded_issuer_key );
841 mbedtls_mpi_free( &serial );
842 mbedtls_ctr_drbg_free( &ctr_drbg );
843 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200844
845#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200847 fflush( stdout ); getchar();
848#endif
849
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100850 return( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
853 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100854 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */