blob: 1ab16ec4d5b6b0587e807891f50325dd446c2152 [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 Garciaa7ac5ab2018-04-29 21:42:45 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020034#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaa7ac5ab2018-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");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020050 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020051}
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 Beckercba45bd2019-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
Paul Bakker9397dcb2013-09-06 09:55:26 +0200157/*
158 * global options
159 */
160struct options
161{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100162 const char *issuer_crt; /* filename of the issuer certificate */
163 const char *request_file; /* filename of the certificate request */
164 const char *subject_key; /* filename of the subject key file */
165 const char *issuer_key; /* filename of the issuer key file */
166 const char *subject_pwd; /* password for the subject key file */
167 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker45d006a2018-08-23 15:26:06 +0100168 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100169 const char *subject_name; /* subject name for certificate */
170 const char *issuer_name; /* issuer name for certificate */
171 const char *not_before; /* validity period not before */
172 const char *not_after; /* validity period not after */
173 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200174 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200175 int is_ca; /* is a CA certificate */
176 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100177 int authority_identifier; /* add authority identifier to CRT */
178 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100179 int basic_constraints; /* add basic constraints ext to CRT */
180 int version; /* CRT version */
181 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200182 unsigned char key_usage; /* key usage flags */
183 unsigned char ns_cert_type; /* NS cert type */
184} opt;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200187 int (*f_rng)(void *, unsigned char *, size_t),
188 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189{
190 int ret;
191 FILE *f;
192 unsigned char output_buf[4096];
193 size_t len = 0;
194
195 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100196 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
197 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200198 return( ret );
199
200 len = strlen( (char *) output_buf );
201
202 if( ( f = fopen( output_file, "w" ) ) == NULL )
203 return( -1 );
204
205 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200206 {
207 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200209 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210
Paul Bakker0c226102014-04-17 16:02:36 +0200211 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212
213 return( 0 );
214}
215
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216int main( int argc, char *argv[] )
217{
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100218 int ret = 1;
219 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_x509_crt issuer_crt;
221 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
222 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200223 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200225 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100226 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200229 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_x509write_cert crt;
233 mbedtls_mpi serial;
234 mbedtls_entropy_context entropy;
235 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200236 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200237
238 /*
239 * Set to sane values
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_pk_init( &loaded_issuer_key );
243 mbedtls_pk_init( &loaded_subject_key );
244 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200245 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker294e5842018-10-05 09:49:33 +0100246 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_X509_CSR_PARSE_C)
248 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251 memset( buf, 0, 1024 );
252
253 if( argc == 0 )
254 {
255 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200257 goto exit;
258 }
259
Paul Bakker1014e952013-09-09 13:59:42 +0200260 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200261 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 opt.subject_key = DFL_SUBJECT_KEY;
263 opt.issuer_key = DFL_ISSUER_KEY;
264 opt.subject_pwd = DFL_SUBJECT_PWD;
265 opt.issuer_pwd = DFL_ISSUER_PWD;
266 opt.output_file = DFL_OUTPUT_FILENAME;
267 opt.subject_name = DFL_SUBJECT_NAME;
268 opt.issuer_name = DFL_ISSUER_NAME;
269 opt.not_before = DFL_NOT_BEFORE;
270 opt.not_after = DFL_NOT_AFTER;
271 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200272 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200273 opt.is_ca = DFL_IS_CA;
274 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275 opt.key_usage = DFL_KEY_USAGE;
276 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100277 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100278 opt.md = DFL_DIGEST;
279 opt.subject_identifier = DFL_SUBJ_IDENT;
280 opt.authority_identifier = DFL_AUTH_IDENT;
281 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200282
283 for( i = 1; i < argc; i++ )
284 {
285
286 p = argv[i];
287 if( ( q = strchr( p, '=' ) ) == NULL )
288 goto usage;
289 *q++ = '\0';
290
Paul Bakkere2673fb2013-09-09 15:52:07 +0200291 if( strcmp( p, "request_file" ) == 0 )
292 opt.request_file = q;
293 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294 opt.subject_key = q;
295 else if( strcmp( p, "issuer_key" ) == 0 )
296 opt.issuer_key = q;
297 else if( strcmp( p, "subject_pwd" ) == 0 )
298 opt.subject_pwd = q;
299 else if( strcmp( p, "issuer_pwd" ) == 0 )
300 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200301 else if( strcmp( p, "issuer_crt" ) == 0 )
302 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 else if( strcmp( p, "output_file" ) == 0 )
304 opt.output_file = q;
305 else if( strcmp( p, "subject_name" ) == 0 )
306 {
307 opt.subject_name = q;
308 }
309 else if( strcmp( p, "issuer_name" ) == 0 )
310 {
311 opt.issuer_name = q;
312 }
313 else if( strcmp( p, "not_before" ) == 0 )
314 {
315 opt.not_before = q;
316 }
317 else if( strcmp( p, "not_after" ) == 0 )
318 {
319 opt.not_after = q;
320 }
321 else if( strcmp( p, "serial" ) == 0 )
322 {
323 opt.serial = q;
324 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100325 else if( strcmp( p, "authority_identifier" ) == 0 )
326 {
327 opt.authority_identifier = atoi( q );
328 if( opt.authority_identifier != 0 &&
329 opt.authority_identifier != 1 )
330 {
Hanno Becker17c32762017-10-03 14:56:04 +0100331 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100332 goto usage;
333 }
334 }
335 else if( strcmp( p, "subject_identifier" ) == 0 )
336 {
337 opt.subject_identifier = atoi( q );
338 if( opt.subject_identifier != 0 &&
339 opt.subject_identifier != 1 )
340 {
Hanno Becker17c32762017-10-03 14:56:04 +0100341 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100342 goto usage;
343 }
344 }
345 else if( strcmp( p, "basic_constraints" ) == 0 )
346 {
347 opt.basic_constraints = atoi( q );
348 if( opt.basic_constraints != 0 &&
349 opt.basic_constraints != 1 )
350 {
Hanno Becker17c32762017-10-03 14:56:04 +0100351 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100352 goto usage;
353 }
354 }
355 else if( strcmp( p, "md" ) == 0 )
356 {
357 if( strcmp( q, "SHA1" ) == 0 )
358 opt.md = MBEDTLS_MD_SHA1;
359 else if( strcmp( q, "SHA256" ) == 0 )
360 opt.md = MBEDTLS_MD_SHA256;
361 else if( strcmp( q, "SHA512" ) == 0 )
362 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker89563622019-06-03 14:10:44 +0100363 else if( strcmp( q, "MD2" ) == 0 )
364 opt.md = MBEDTLS_MD_MD2;
365 else if( strcmp( q, "MD4" ) == 0 )
366 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100367 else if( strcmp( q, "MD5" ) == 0 )
368 opt.md = MBEDTLS_MD_MD5;
369 else
Hanno Becker17c32762017-10-03 14:56:04 +0100370 {
371 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100372 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100373 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100374 }
375 else if( strcmp( p, "version" ) == 0 )
376 {
377 opt.version = atoi( q );
378 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100379 {
380 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100381 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100382 }
Hanno Becker38eff432017-09-22 15:38:20 +0100383 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100384 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200385 else if( strcmp( p, "selfsign" ) == 0 )
386 {
387 opt.selfsign = atoi( q );
388 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100389 {
390 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200391 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100392 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200393 }
Paul Bakker15162a02013-09-06 19:27:21 +0200394 else if( strcmp( p, "is_ca" ) == 0 )
395 {
396 opt.is_ca = atoi( q );
397 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100398 {
399 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200400 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100401 }
Paul Bakker15162a02013-09-06 19:27:21 +0200402 }
403 else if( strcmp( p, "max_pathlen" ) == 0 )
404 {
405 opt.max_pathlen = atoi( q );
406 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100407 {
408 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200409 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100410 }
Paul Bakker15162a02013-09-06 19:27:21 +0200411 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200412 else if( strcmp( p, "key_usage" ) == 0 )
413 {
414 while( q != NULL )
415 {
416 if( ( r = strchr( q, ',' ) ) != NULL )
417 *r++ = '\0';
418
419 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100424 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100426 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100428 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else
Hanno Becker17c32762017-10-03 14:56:04 +0100434 {
435 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200436 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100437 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200438
439 q = r;
440 }
441 }
442 else if( strcmp( p, "ns_cert_type" ) == 0 )
443 {
444 while( q != NULL )
445 {
446 if( ( r = strchr( q, ',' ) ) != NULL )
447 *r++ = '\0';
448
449 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200451 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100452 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200457 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100458 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200459 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100460 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100462 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463 else
Hanno Becker17c32762017-10-03 14:56:04 +0100464 {
465 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200466 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100467 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200468
469 q = r;
470 }
471 }
472 else
473 goto usage;
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200477
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200478 /*
479 * 0. Seed the PRNG
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200482 fflush( stdout );
483
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200484 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200485 (const unsigned char *) pers,
486 strlen( pers ) ) ) != 0 )
487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100489 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
490 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200491 goto exit;
492 }
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200495
Paul Bakker9397dcb2013-09-06 09:55:26 +0200496 // Parse serial to MPI
497 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200499 fflush( stdout );
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100504 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100505 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200506 goto exit;
507 }
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200510
Paul Bakker1014e952013-09-09 13:59:42 +0200511 // Parse issuer certificate if present
512 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200513 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200514 {
515 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200516 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200519 fflush( stdout );
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100524 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100525 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200526 goto exit;
527 }
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200530 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200531 if( ret < 0 )
532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100534 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100535 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200536 goto exit;
537 }
538
Paul Bakkere2673fb2013-09-09 15:52:07 +0200539 opt.issuer_name = issuer_name;
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200542 }
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200545 // Parse certificate request if present
546 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200547 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 {
549 /*
550 * 1.0.b. Load the CSR
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200553 fflush( stdout );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100558 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100559 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200560 goto exit;
561 }
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200564 &csr.subject );
565 if( ret < 0 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100568 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100569 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200570 goto exit;
571 }
572
573 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200574 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200577 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200579
580 /*
581 * 1.1. Load the keys
582 */
583 if( !opt.selfsign && !strlen( opt.request_file ) )
584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200586 fflush( stdout );
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200589 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200590 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200591 {
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_pk_parse_keyfile "
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 }
Paul Bakker1014e952013-09-09 13:59:42 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200599 }
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200602 fflush( stdout );
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200606 if( ret != 0 )
607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100609 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
610 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200611 goto exit;
612 }
613
614 // Check if key and issuer certificate match
615 //
616 if( strlen( opt.issuer_crt ) )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
619 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
620 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
621 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
622 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200623 {
Hanno Becker81535d02017-09-13 15:39:59 +0100624 mbedtls_printf( " failed\n ! issuer_key does not match "
625 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200626 goto exit;
627 }
628 }
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200631
632 if( opt.selfsign )
633 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100634 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200635 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200636 }
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
639 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200640
Paul Bakker9397dcb2013-09-06 09:55:26 +0200641 /*
642 * 1.0. Check the names for validity
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100647 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100648 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200649 goto exit;
650 }
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100655 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100656 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200657 goto exit;
658 }
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200661 fflush( stdout );
662
Hanno Becker38eff432017-09-22 15:38:20 +0100663 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100664 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200667 if( ret != 0 )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100670 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100671 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200672 goto exit;
673 }
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200676 if( ret != 0 )
677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100679 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100680 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200681 goto exit;
682 }
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200685
Hanno Becker38eff432017-09-22 15:38:20 +0100686 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
687 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200688 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100689 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
690 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200691
Hanno Becker6c13d372017-09-13 12:49:22 +0100692 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
693 opt.max_pathlen );
694 if( ret != 0 )
695 {
696 mbedtls_strerror( ret, buf, 1024 );
697 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100698 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100699 goto exit;
700 }
701
702 mbedtls_printf( " ok\n" );
703 }
Paul Bakker15162a02013-09-06 19:27:21 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100706 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
707 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200708 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100709 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
710 fflush( stdout );
711
712 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
713 if( ret != 0 )
714 {
715 mbedtls_strerror( ret, buf, 1024 );
716 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100717 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100718 -ret, buf );
719 goto exit;
720 }
721
722 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200723 }
724
Hanno Becker38eff432017-09-22 15:38:20 +0100725 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
726 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200727 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100728 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
729 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200730
Hanno Becker6c13d372017-09-13 12:49:22 +0100731 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
732 if( ret != 0 )
733 {
734 mbedtls_strerror( ret, buf, 1024 );
735 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100736 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100737 -ret, buf );
738 goto exit;
739 }
740
741 mbedtls_printf( " ok\n" );
742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200744
Hanno Becker38eff432017-09-22 15:38:20 +0100745 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
746 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200749 fflush( stdout );
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200752 if( ret != 0 )
753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100755 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100756 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200757 goto exit;
758 }
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200761 }
762
Hanno Becker38eff432017-09-22 15:38:20 +0100763 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
764 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200767 fflush( stdout );
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200770 if( ret != 0 )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100773 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100774 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200775 goto exit;
776 }
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200779 }
780
Paul Bakker9397dcb2013-09-06 09:55:26 +0200781 /*
Hanno Becker45d006a2018-08-23 15:26:06 +0100782 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200785 fflush( stdout );
786
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200787 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100791 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100792 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200793 goto exit;
794 }
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200797
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100798 exit_code = MBEDTLS_EXIT_SUCCESS;
799
Paul Bakker9397dcb2013-09-06 09:55:26 +0200800exit:
Hanno Becker294e5842018-10-05 09:49:33 +0100801#if defined(MBEDTLS_X509_CSR_PARSE_C)
802 mbedtls_x509_csr_free( &csr );
803#endif /* MBEDTLS_X509_CSR_PARSE_C */
804 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_x509write_crt_free( &crt );
806 mbedtls_pk_free( &loaded_subject_key );
807 mbedtls_pk_free( &loaded_issuer_key );
808 mbedtls_mpi_free( &serial );
809 mbedtls_ctr_drbg_free( &ctr_drbg );
810 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200811
812#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200814 fflush( stdout ); getchar();
815#endif
816
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200817 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
820 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100821 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */