blob: 8172a2b0350385f1f8ec27e4a710ed647ea99413 [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Simon Butcher203a6932016-10-07 15:00:17 +010066#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
67 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
68 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
69 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
70 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020071int main( void )
72{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 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 +020074 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
76 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020077 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020078}
79#else
80
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/x509_crt.h"
82#include "mbedtls/x509_csr.h"
83#include "mbedtls/entropy.h"
84#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010085#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020087
Rich Evans18b78c72015-02-11 14:06:19 +000088#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000093#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010094 " request_file=%%s default: (empty)\n" \
95 " If request_file is specified, subject_key,\n" \
96 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000097#else
98#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +0000100
Paul Bakker1014e952013-09-09 13:59:42 +0200101#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +0200102#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +0200103#define DFL_SUBJECT_KEY "subject.key"
104#define DFL_ISSUER_KEY "ca.key"
105#define DFL_SUBJECT_PWD ""
106#define DFL_ISSUER_PWD ""
107#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000108#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
109#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +0200110#define DFL_NOT_BEFORE "20010101000000"
111#define DFL_NOT_AFTER "20301231235959"
112#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200113#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +0200114#define DFL_IS_CA 0
115#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +0200116#define DFL_KEY_USAGE 0
117#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +0100118#define DFL_VERSION 3
119#define DFL_AUTH_IDENT 1
120#define DFL_SUBJ_IDENT 1
121#define DFL_CONSTRAINTS 1
122#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +0200123
Rich Evans18b78c72015-02-11 14:06:19 +0000124#define USAGE \
125 "\n usage: cert_write param=<>...\n" \
126 "\n acceptable parameters:\n" \
127 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100128 " subject_key=%%s default: subject.key\n" \
129 " subject_pwd=%%s default: (empty)\n" \
130 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000131 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100132 " issuer_crt=%%s default: (empty)\n" \
133 " If issuer_crt is specified, issuer_name is\n" \
134 " ignored!\n" \
135 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000136 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100137 " selfsign=%%d default: 0 (false)\n" \
138 " If selfsign is enabled, issuer_name and\n" \
139 " issuer_key are required (issuer_crt and\n" \
140 " subject_* are ignored\n" \
141 " issuer_key=%%s default: ca.key\n" \
142 " issuer_pwd=%%s default: (empty)\n" \
143 " output_file=%%s default: cert.crt\n" \
144 " serial=%%s default: 1\n" \
145 " not_before=%%s default: 20010101000000\n"\
146 " not_after=%%s default: 20301231235959\n"\
147 " is_ca=%%d default: 0 (disabled)\n" \
148 " max_pathlen=%%d default: -1 (none)\n" \
149 " md=%%s default: SHA256\n" \
150 " Supported values:\n" \
Hanno Beckercba45bd2019-06-03 14:36:59 +0100151 " MD2, MD4, MD5, SHA1, SHA256, SHA512\n"\
Hanno Becker81535d02017-09-13 15:39:59 +0100152 " version=%%d default: 3\n" \
153 " Possible values: 1, 2, 3\n"\
154 " subject_identifier=%%s default: 1\n" \
155 " Possible values: 0, 1\n" \
156 " (Considered for v3 only)\n"\
157 " authority_identifier=%%s default: 1\n" \
158 " Possible values: 0, 1\n" \
159 " (Considered for v3 only)\n"\
160 " basic_constraints=%%d default: 1\n" \
161 " Possible values: 0, 1\n" \
162 " (Considered for v3 only)\n"\
163 " key_usage=%%s default: (empty)\n" \
164 " Comma-separated-list of values:\n" \
165 " digital_signature\n" \
166 " non_repudiation\n" \
167 " key_encipherment\n" \
168 " data_encipherment\n" \
169 " key_agreement\n" \
170 " key_cert_sign\n" \
171 " crl_sign\n" \
172 " (Considered for v3 only)\n"\
173 " ns_cert_type=%%s default: (empty)\n" \
174 " Comma-separated-list of values:\n" \
175 " ssl_client\n" \
176 " ssl_server\n" \
177 " email\n" \
178 " object_signing\n" \
179 " ssl_ca\n" \
180 " email_ca\n" \
181 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000182 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000183
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184/*
185 * global options
186 */
187struct options
188{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100189 const char *issuer_crt; /* filename of the issuer certificate */
190 const char *request_file; /* filename of the certificate request */
191 const char *subject_key; /* filename of the subject key file */
192 const char *issuer_key; /* filename of the issuer key file */
193 const char *subject_pwd; /* password for the subject key file */
194 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker45d006a2018-08-23 15:26:06 +0100195 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100196 const char *subject_name; /* subject name for certificate */
197 const char *issuer_name; /* issuer name for certificate */
198 const char *not_before; /* validity period not before */
199 const char *not_after; /* validity period not after */
200 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200201 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200202 int is_ca; /* is a CA certificate */
203 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100204 int authority_identifier; /* add authority identifier to CRT */
205 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100206 int basic_constraints; /* add basic constraints ext to CRT */
207 int version; /* CRT version */
208 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209 unsigned char key_usage; /* key usage flags */
210 unsigned char ns_cert_type; /* NS cert type */
211} opt;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200214 int (*f_rng)(void *, unsigned char *, size_t),
215 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216{
217 int ret;
218 FILE *f;
219 unsigned char output_buf[4096];
220 size_t len = 0;
221
222 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100223 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
224 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225 return( ret );
226
227 len = strlen( (char *) output_buf );
228
229 if( ( f = fopen( output_file, "w" ) ) == NULL )
230 return( -1 );
231
232 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200233 {
234 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200236 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200237
Paul Bakker0c226102014-04-17 16:02:36 +0200238 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239
240 return( 0 );
241}
242
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243int main( int argc, char *argv[] )
244{
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100245 int ret = 1;
246 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_x509_crt issuer_crt;
248 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
249 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200250 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200252 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100253 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200254 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200256 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_x509write_cert crt;
260 mbedtls_mpi serial;
261 mbedtls_entropy_context entropy;
262 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200263 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200264
265 /*
266 * Set to sane values
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_pk_init( &loaded_issuer_key );
270 mbedtls_pk_init( &loaded_subject_key );
271 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200272 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker294e5842018-10-05 09:49:33 +0100273 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_X509_CSR_PARSE_C)
275 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200278 memset( buf, 0, 1024 );
279
280 if( argc == 0 )
281 {
282 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200284 goto exit;
285 }
286
Paul Bakker1014e952013-09-09 13:59:42 +0200287 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200288 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289 opt.subject_key = DFL_SUBJECT_KEY;
290 opt.issuer_key = DFL_ISSUER_KEY;
291 opt.subject_pwd = DFL_SUBJECT_PWD;
292 opt.issuer_pwd = DFL_ISSUER_PWD;
293 opt.output_file = DFL_OUTPUT_FILENAME;
294 opt.subject_name = DFL_SUBJECT_NAME;
295 opt.issuer_name = DFL_ISSUER_NAME;
296 opt.not_before = DFL_NOT_BEFORE;
297 opt.not_after = DFL_NOT_AFTER;
298 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200299 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200300 opt.is_ca = DFL_IS_CA;
301 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200302 opt.key_usage = DFL_KEY_USAGE;
303 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100304 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100305 opt.md = DFL_DIGEST;
306 opt.subject_identifier = DFL_SUBJ_IDENT;
307 opt.authority_identifier = DFL_AUTH_IDENT;
308 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200309
310 for( i = 1; i < argc; i++ )
311 {
312
313 p = argv[i];
314 if( ( q = strchr( p, '=' ) ) == NULL )
315 goto usage;
316 *q++ = '\0';
317
Paul Bakkere2673fb2013-09-09 15:52:07 +0200318 if( strcmp( p, "request_file" ) == 0 )
319 opt.request_file = q;
320 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200321 opt.subject_key = q;
322 else if( strcmp( p, "issuer_key" ) == 0 )
323 opt.issuer_key = q;
324 else if( strcmp( p, "subject_pwd" ) == 0 )
325 opt.subject_pwd = q;
326 else if( strcmp( p, "issuer_pwd" ) == 0 )
327 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200328 else if( strcmp( p, "issuer_crt" ) == 0 )
329 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200330 else if( strcmp( p, "output_file" ) == 0 )
331 opt.output_file = q;
332 else if( strcmp( p, "subject_name" ) == 0 )
333 {
334 opt.subject_name = q;
335 }
336 else if( strcmp( p, "issuer_name" ) == 0 )
337 {
338 opt.issuer_name = q;
339 }
340 else if( strcmp( p, "not_before" ) == 0 )
341 {
342 opt.not_before = q;
343 }
344 else if( strcmp( p, "not_after" ) == 0 )
345 {
346 opt.not_after = q;
347 }
348 else if( strcmp( p, "serial" ) == 0 )
349 {
350 opt.serial = q;
351 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100352 else if( strcmp( p, "authority_identifier" ) == 0 )
353 {
354 opt.authority_identifier = atoi( q );
355 if( opt.authority_identifier != 0 &&
356 opt.authority_identifier != 1 )
357 {
Hanno Becker17c32762017-10-03 14:56:04 +0100358 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100359 goto usage;
360 }
361 }
362 else if( strcmp( p, "subject_identifier" ) == 0 )
363 {
364 opt.subject_identifier = atoi( q );
365 if( opt.subject_identifier != 0 &&
366 opt.subject_identifier != 1 )
367 {
Hanno Becker17c32762017-10-03 14:56:04 +0100368 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100369 goto usage;
370 }
371 }
372 else if( strcmp( p, "basic_constraints" ) == 0 )
373 {
374 opt.basic_constraints = atoi( q );
375 if( opt.basic_constraints != 0 &&
376 opt.basic_constraints != 1 )
377 {
Hanno Becker17c32762017-10-03 14:56:04 +0100378 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100379 goto usage;
380 }
381 }
382 else if( strcmp( p, "md" ) == 0 )
383 {
384 if( strcmp( q, "SHA1" ) == 0 )
385 opt.md = MBEDTLS_MD_SHA1;
386 else if( strcmp( q, "SHA256" ) == 0 )
387 opt.md = MBEDTLS_MD_SHA256;
388 else if( strcmp( q, "SHA512" ) == 0 )
389 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker89563622019-06-03 14:10:44 +0100390 else if( strcmp( q, "MD2" ) == 0 )
391 opt.md = MBEDTLS_MD_MD2;
392 else if( strcmp( q, "MD4" ) == 0 )
393 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100394 else if( strcmp( q, "MD5" ) == 0 )
395 opt.md = MBEDTLS_MD_MD5;
396 else
Hanno Becker17c32762017-10-03 14:56:04 +0100397 {
398 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100399 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100400 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100401 }
402 else if( strcmp( p, "version" ) == 0 )
403 {
404 opt.version = atoi( q );
405 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100406 {
407 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100408 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100409 }
Hanno Becker38eff432017-09-22 15:38:20 +0100410 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100411 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200412 else if( strcmp( p, "selfsign" ) == 0 )
413 {
414 opt.selfsign = atoi( q );
415 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100416 {
417 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200418 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100419 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200420 }
Paul Bakker15162a02013-09-06 19:27:21 +0200421 else if( strcmp( p, "is_ca" ) == 0 )
422 {
423 opt.is_ca = atoi( q );
424 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100425 {
426 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200427 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100428 }
Paul Bakker15162a02013-09-06 19:27:21 +0200429 }
430 else if( strcmp( p, "max_pathlen" ) == 0 )
431 {
432 opt.max_pathlen = atoi( q );
433 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100434 {
435 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200436 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100437 }
Paul Bakker15162a02013-09-06 19:27:21 +0200438 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( p, "key_usage" ) == 0 )
440 {
441 while( q != NULL )
442 {
443 if( ( r = strchr( q, ',' ) ) != NULL )
444 *r++ = '\0';
445
446 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100451 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100453 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100455 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200460 else
Hanno Becker17c32762017-10-03 14:56:04 +0100461 {
462 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100464 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200465
466 q = r;
467 }
468 }
469 else if( strcmp( p, "ns_cert_type" ) == 0 )
470 {
471 while( q != NULL )
472 {
473 if( ( r = strchr( q, ',' ) ) != NULL )
474 *r++ = '\0';
475
476 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200478 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100479 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200480 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200482 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100485 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200486 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100487 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100489 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 else
Hanno Becker17c32762017-10-03 14:56:04 +0100491 {
492 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100494 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200495
496 q = r;
497 }
498 }
499 else
500 goto usage;
501 }
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200504
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200505 /*
506 * 0. Seed the PRNG
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200509 fflush( stdout );
510
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200511 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200512 (const unsigned char *) pers,
513 strlen( pers ) ) ) != 0 )
514 {
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_ctr_drbg_seed returned %d - %s\n",
517 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +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 Bakker9397dcb2013-09-06 09:55:26 +0200523 // Parse serial to MPI
524 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200526 fflush( stdout );
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100531 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100532 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200533 goto exit;
534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200537
Paul Bakker1014e952013-09-09 13:59:42 +0200538 // Parse issuer certificate if present
539 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200540 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200541 {
542 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200543 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200546 fflush( stdout );
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100551 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100552 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200553 goto exit;
554 }
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200557 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200558 if( ret < 0 )
559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100561 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100562 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200563 goto exit;
564 }
565
Paul Bakkere2673fb2013-09-09 15:52:07 +0200566 opt.issuer_name = issuer_name;
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200569 }
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200572 // Parse certificate request if present
573 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200574 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200575 {
576 /*
577 * 1.0.b. Load the CSR
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200580 fflush( stdout );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100585 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100586 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200587 goto exit;
588 }
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200591 &csr.subject );
592 if( ret < 0 )
593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100595 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100596 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200597 goto exit;
598 }
599
600 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200601 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200606
607 /*
608 * 1.1. Load the keys
609 */
610 if( !opt.selfsign && !strlen( opt.request_file ) )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200613 fflush( stdout );
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200616 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200617 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100620 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100621 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200622 goto exit;
623 }
Paul Bakker1014e952013-09-09 13:59:42 +0200624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200626 }
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200629 fflush( stdout );
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200633 if( ret != 0 )
634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100636 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
637 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200638 goto exit;
639 }
640
641 // Check if key and issuer certificate match
642 //
643 if( strlen( opt.issuer_crt ) )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
646 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
647 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
648 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
649 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200650 {
Hanno Becker81535d02017-09-13 15:39:59 +0100651 mbedtls_printf( " failed\n ! issuer_key does not match "
652 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200653 goto exit;
654 }
655 }
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200658
659 if( opt.selfsign )
660 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100661 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200662 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200663 }
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
666 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200667
Paul Bakker9397dcb2013-09-06 09:55:26 +0200668 /*
669 * 1.0. Check the names for validity
670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100674 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100675 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200676 goto exit;
677 }
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100682 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100683 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200684 goto exit;
685 }
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200688 fflush( stdout );
689
Hanno Becker38eff432017-09-22 15:38:20 +0100690 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100691 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200694 if( ret != 0 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100697 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100698 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200699 goto exit;
700 }
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200703 if( ret != 0 )
704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100706 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100707 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200708 goto exit;
709 }
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200712
Hanno Becker38eff432017-09-22 15:38:20 +0100713 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
714 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200715 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100716 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
717 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200718
Hanno Becker6c13d372017-09-13 12:49:22 +0100719 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
720 opt.max_pathlen );
721 if( ret != 0 )
722 {
723 mbedtls_strerror( ret, buf, 1024 );
724 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100725 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100726 goto exit;
727 }
728
729 mbedtls_printf( " ok\n" );
730 }
Paul Bakker15162a02013-09-06 19:27:21 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100733 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
734 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200735 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100736 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
737 fflush( stdout );
738
739 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
740 if( ret != 0 )
741 {
742 mbedtls_strerror( ret, buf, 1024 );
743 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100744 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100745 -ret, buf );
746 goto exit;
747 }
748
749 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200750 }
751
Hanno Becker38eff432017-09-22 15:38:20 +0100752 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
753 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200754 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100755 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
756 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200757
Hanno Becker6c13d372017-09-13 12:49:22 +0100758 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
759 if( ret != 0 )
760 {
761 mbedtls_strerror( ret, buf, 1024 );
762 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100763 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100764 -ret, buf );
765 goto exit;
766 }
767
768 mbedtls_printf( " ok\n" );
769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200771
Hanno Becker38eff432017-09-22 15:38:20 +0100772 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
773 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200776 fflush( stdout );
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200779 if( ret != 0 )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100782 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100783 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200784 goto exit;
785 }
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200788 }
789
Hanno Becker38eff432017-09-22 15:38:20 +0100790 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
791 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200794 fflush( stdout );
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200797 if( ret != 0 )
798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100800 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100801 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200802 goto exit;
803 }
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200806 }
807
Paul Bakker9397dcb2013-09-06 09:55:26 +0200808 /*
Hanno Becker45d006a2018-08-23 15:26:06 +0100809 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200812 fflush( stdout );
813
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200814 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100818 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100819 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200820 goto exit;
821 }
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200824
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100825 exit_code = MBEDTLS_EXIT_SUCCESS;
826
Paul Bakker9397dcb2013-09-06 09:55:26 +0200827exit:
Hanno Becker294e5842018-10-05 09:49:33 +0100828#if defined(MBEDTLS_X509_CSR_PARSE_C)
829 mbedtls_x509_csr_free( &csr );
830#endif /* MBEDTLS_X509_CSR_PARSE_C */
831 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_x509write_crt_free( &crt );
833 mbedtls_pk_free( &loaded_subject_key );
834 mbedtls_pk_free( &loaded_issuer_key );
835 mbedtls_mpi_free( &serial );
836 mbedtls_ctr_drbg_free( &ctr_drbg );
837 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200838
839#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200841 fflush( stdout ); getchar();
842#endif
843
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200844 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200845}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
847 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100848 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */