blob: 4f813371bf83fb9dce434c3280e8a195c30b24e5 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker9397dcb2013-09-06 09:55:26 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Simon Butcher203a6932016-10-07 15:00:17 +010064#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
65 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
66 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
67 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
68 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020069int main( void )
70{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 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 +020072 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
74 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020076}
77#else
78
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/x509_crt.h"
80#include "mbedtls/x509_csr.h"
81#include "mbedtls/entropy.h"
82#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010083#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020085
Rich Evans18b78c72015-02-11 14:06:19 +000086#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000091#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010092 " request_file=%%s default: (empty)\n" \
93 " If request_file is specified, subject_key,\n" \
94 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000095#else
96#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000098
Paul Bakker1014e952013-09-09 13:59:42 +020099#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +0200100#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +0200101#define DFL_SUBJECT_KEY "subject.key"
102#define DFL_ISSUER_KEY "ca.key"
103#define DFL_SUBJECT_PWD ""
104#define DFL_ISSUER_PWD ""
105#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000106#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
107#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +0200108#define DFL_NOT_BEFORE "20010101000000"
109#define DFL_NOT_AFTER "20301231235959"
110#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200111#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +0200112#define DFL_IS_CA 0
113#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +0200114#define DFL_KEY_USAGE 0
115#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +0100116#define DFL_VERSION 3
117#define DFL_AUTH_IDENT 1
118#define DFL_SUBJ_IDENT 1
119#define DFL_CONSTRAINTS 1
120#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +0200121
Rich Evans18b78c72015-02-11 14:06:19 +0000122#define USAGE \
123 "\n usage: cert_write param=<>...\n" \
124 "\n acceptable parameters:\n" \
125 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " subject_key=%%s default: subject.key\n" \
127 " subject_pwd=%%s default: (empty)\n" \
128 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000129 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100130 " issuer_crt=%%s default: (empty)\n" \
131 " If issuer_crt is specified, issuer_name is\n" \
132 " ignored!\n" \
133 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000134 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100135 " selfsign=%%d default: 0 (false)\n" \
136 " If selfsign is enabled, issuer_name and\n" \
137 " issuer_key are required (issuer_crt and\n" \
138 " subject_* are ignored\n" \
139 " issuer_key=%%s default: ca.key\n" \
140 " issuer_pwd=%%s default: (empty)\n" \
141 " output_file=%%s default: cert.crt\n" \
142 " serial=%%s default: 1\n" \
143 " not_before=%%s default: 20010101000000\n"\
144 " not_after=%%s default: 20301231235959\n"\
145 " is_ca=%%d default: 0 (disabled)\n" \
146 " max_pathlen=%%d default: -1 (none)\n" \
147 " md=%%s default: SHA256\n" \
148 " Supported values:\n" \
Hanno Beckercba45bd2019-06-03 14:36:59 +0100149 " MD2, MD4, MD5, SHA1, SHA256, SHA512\n"\
Hanno Becker81535d02017-09-13 15:39:59 +0100150 " version=%%d default: 3\n" \
151 " Possible values: 1, 2, 3\n"\
152 " subject_identifier=%%s default: 1\n" \
153 " Possible values: 0, 1\n" \
154 " (Considered for v3 only)\n"\
155 " authority_identifier=%%s default: 1\n" \
156 " Possible values: 0, 1\n" \
157 " (Considered for v3 only)\n"\
158 " basic_constraints=%%d default: 1\n" \
159 " Possible values: 0, 1\n" \
160 " (Considered for v3 only)\n"\
161 " key_usage=%%s default: (empty)\n" \
162 " Comma-separated-list of values:\n" \
163 " digital_signature\n" \
164 " non_repudiation\n" \
165 " key_encipherment\n" \
166 " data_encipherment\n" \
167 " key_agreement\n" \
168 " key_cert_sign\n" \
169 " crl_sign\n" \
170 " (Considered for v3 only)\n"\
171 " ns_cert_type=%%s default: (empty)\n" \
172 " Comma-separated-list of values:\n" \
173 " ssl_client\n" \
174 " ssl_server\n" \
175 " email\n" \
176 " object_signing\n" \
177 " ssl_ca\n" \
178 " email_ca\n" \
179 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000180 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000181
Paul Bakker9397dcb2013-09-06 09:55:26 +0200182/*
183 * global options
184 */
185struct options
186{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100187 const char *issuer_crt; /* filename of the issuer certificate */
188 const char *request_file; /* filename of the certificate request */
189 const char *subject_key; /* filename of the subject key file */
190 const char *issuer_key; /* filename of the issuer key file */
191 const char *subject_pwd; /* password for the subject key file */
192 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker45d006a2018-08-23 15:26:06 +0100193 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100194 const char *subject_name; /* subject name for certificate */
195 const char *issuer_name; /* issuer name for certificate */
196 const char *not_before; /* validity period not before */
197 const char *not_after; /* validity period not after */
198 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200199 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200200 int is_ca; /* is a CA certificate */
201 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100202 int authority_identifier; /* add authority identifier to CRT */
203 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100204 int basic_constraints; /* add basic constraints ext to CRT */
205 int version; /* CRT version */
206 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207 unsigned char key_usage; /* key usage flags */
208 unsigned char ns_cert_type; /* NS cert type */
209} opt;
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200212 int (*f_rng)(void *, unsigned char *, size_t),
213 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200214{
215 int ret;
216 FILE *f;
217 unsigned char output_buf[4096];
218 size_t len = 0;
219
220 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100221 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
222 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 return( ret );
224
225 len = strlen( (char *) output_buf );
226
227 if( ( f = fopen( output_file, "w" ) ) == NULL )
228 return( -1 );
229
230 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200231 {
232 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200233 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200234 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235
Paul Bakker0c226102014-04-17 16:02:36 +0200236 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200237
238 return( 0 );
239}
240
Paul Bakker9397dcb2013-09-06 09:55:26 +0200241int main( int argc, char *argv[] )
242{
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100243 int ret = 1;
244 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509_crt issuer_crt;
246 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
247 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200248 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200250 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100251 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200254 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_x509write_cert crt;
258 mbedtls_mpi serial;
259 mbedtls_entropy_context entropy;
260 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200261 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262
263 /*
264 * Set to sane values
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_pk_init( &loaded_issuer_key );
268 mbedtls_pk_init( &loaded_subject_key );
269 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200270 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker294e5842018-10-05 09:49:33 +0100271 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_X509_CSR_PARSE_C)
273 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276 memset( buf, 0, 1024 );
277
278 if( argc == 0 )
279 {
280 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200282 goto exit;
283 }
284
Paul Bakker1014e952013-09-09 13:59:42 +0200285 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200286 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.subject_key = DFL_SUBJECT_KEY;
288 opt.issuer_key = DFL_ISSUER_KEY;
289 opt.subject_pwd = DFL_SUBJECT_PWD;
290 opt.issuer_pwd = DFL_ISSUER_PWD;
291 opt.output_file = DFL_OUTPUT_FILENAME;
292 opt.subject_name = DFL_SUBJECT_NAME;
293 opt.issuer_name = DFL_ISSUER_NAME;
294 opt.not_before = DFL_NOT_BEFORE;
295 opt.not_after = DFL_NOT_AFTER;
296 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200297 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200298 opt.is_ca = DFL_IS_CA;
299 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200300 opt.key_usage = DFL_KEY_USAGE;
301 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100302 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100303 opt.md = DFL_DIGEST;
304 opt.subject_identifier = DFL_SUBJ_IDENT;
305 opt.authority_identifier = DFL_AUTH_IDENT;
306 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307
308 for( i = 1; i < argc; i++ )
309 {
310
311 p = argv[i];
312 if( ( q = strchr( p, '=' ) ) == NULL )
313 goto usage;
314 *q++ = '\0';
315
Paul Bakkere2673fb2013-09-09 15:52:07 +0200316 if( strcmp( p, "request_file" ) == 0 )
317 opt.request_file = q;
318 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200319 opt.subject_key = q;
320 else if( strcmp( p, "issuer_key" ) == 0 )
321 opt.issuer_key = q;
322 else if( strcmp( p, "subject_pwd" ) == 0 )
323 opt.subject_pwd = q;
324 else if( strcmp( p, "issuer_pwd" ) == 0 )
325 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200326 else if( strcmp( p, "issuer_crt" ) == 0 )
327 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200328 else if( strcmp( p, "output_file" ) == 0 )
329 opt.output_file = q;
330 else if( strcmp( p, "subject_name" ) == 0 )
331 {
332 opt.subject_name = q;
333 }
334 else if( strcmp( p, "issuer_name" ) == 0 )
335 {
336 opt.issuer_name = q;
337 }
338 else if( strcmp( p, "not_before" ) == 0 )
339 {
340 opt.not_before = q;
341 }
342 else if( strcmp( p, "not_after" ) == 0 )
343 {
344 opt.not_after = q;
345 }
346 else if( strcmp( p, "serial" ) == 0 )
347 {
348 opt.serial = q;
349 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100350 else if( strcmp( p, "authority_identifier" ) == 0 )
351 {
352 opt.authority_identifier = atoi( q );
353 if( opt.authority_identifier != 0 &&
354 opt.authority_identifier != 1 )
355 {
Hanno Becker17c32762017-10-03 14:56:04 +0100356 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100357 goto usage;
358 }
359 }
360 else if( strcmp( p, "subject_identifier" ) == 0 )
361 {
362 opt.subject_identifier = atoi( q );
363 if( opt.subject_identifier != 0 &&
364 opt.subject_identifier != 1 )
365 {
Hanno Becker17c32762017-10-03 14:56:04 +0100366 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100367 goto usage;
368 }
369 }
370 else if( strcmp( p, "basic_constraints" ) == 0 )
371 {
372 opt.basic_constraints = atoi( q );
373 if( opt.basic_constraints != 0 &&
374 opt.basic_constraints != 1 )
375 {
Hanno Becker17c32762017-10-03 14:56:04 +0100376 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100377 goto usage;
378 }
379 }
380 else if( strcmp( p, "md" ) == 0 )
381 {
382 if( strcmp( q, "SHA1" ) == 0 )
383 opt.md = MBEDTLS_MD_SHA1;
384 else if( strcmp( q, "SHA256" ) == 0 )
385 opt.md = MBEDTLS_MD_SHA256;
386 else if( strcmp( q, "SHA512" ) == 0 )
387 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker89563622019-06-03 14:10:44 +0100388 else if( strcmp( q, "MD2" ) == 0 )
389 opt.md = MBEDTLS_MD_MD2;
390 else if( strcmp( q, "MD4" ) == 0 )
391 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100392 else if( strcmp( q, "MD5" ) == 0 )
393 opt.md = MBEDTLS_MD_MD5;
394 else
Hanno Becker17c32762017-10-03 14:56:04 +0100395 {
396 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100397 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100398 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100399 }
400 else if( strcmp( p, "version" ) == 0 )
401 {
402 opt.version = atoi( q );
403 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100404 {
405 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100406 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100407 }
Hanno Becker38eff432017-09-22 15:38:20 +0100408 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100409 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200410 else if( strcmp( p, "selfsign" ) == 0 )
411 {
412 opt.selfsign = atoi( q );
413 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100414 {
415 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200416 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100417 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200418 }
Paul Bakker15162a02013-09-06 19:27:21 +0200419 else if( strcmp( p, "is_ca" ) == 0 )
420 {
421 opt.is_ca = atoi( q );
422 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100423 {
424 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200425 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100426 }
Paul Bakker15162a02013-09-06 19:27:21 +0200427 }
428 else if( strcmp( p, "max_pathlen" ) == 0 )
429 {
430 opt.max_pathlen = atoi( q );
431 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100432 {
433 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200434 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100435 }
Paul Bakker15162a02013-09-06 19:27:21 +0200436 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( p, "key_usage" ) == 0 )
438 {
439 while( q != NULL )
440 {
441 if( ( r = strchr( q, ',' ) ) != NULL )
442 *r++ = '\0';
443
444 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100449 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100451 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100453 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 else
Hanno Becker17c32762017-10-03 14:56:04 +0100459 {
460 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100462 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463
464 q = r;
465 }
466 }
467 else if( strcmp( p, "ns_cert_type" ) == 0 )
468 {
469 while( q != NULL )
470 {
471 if( ( r = strchr( q, ',' ) ) != NULL )
472 *r++ = '\0';
473
474 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200476 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100477 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200478 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200480 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200482 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100483 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100485 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200486 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100487 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 else
Hanno Becker17c32762017-10-03 14:56:04 +0100489 {
490 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100492 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493
494 q = r;
495 }
496 }
497 else
498 goto usage;
499 }
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200502
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200503 /*
504 * 0. Seed the PRNG
505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200507 fflush( stdout );
508
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200509 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200510 (const unsigned char *) pers,
511 strlen( pers ) ) ) != 0 )
512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100514 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
515 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200516 goto exit;
517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200520
Paul Bakker9397dcb2013-09-06 09:55:26 +0200521 // Parse serial to MPI
522 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200524 fflush( stdout );
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100529 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100530 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200531 goto exit;
532 }
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200535
Paul Bakker1014e952013-09-09 13:59:42 +0200536 // Parse issuer certificate if present
537 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200538 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200539 {
540 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200541 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200544 fflush( stdout );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100549 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100550 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200551 goto exit;
552 }
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200555 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200556 if( ret < 0 )
557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100559 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100560 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200561 goto exit;
562 }
563
Paul Bakkere2673fb2013-09-09 15:52:07 +0200564 opt.issuer_name = issuer_name;
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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
644 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
645 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
646 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
647 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200648 {
Hanno Becker81535d02017-09-13 15:39:59 +0100649 mbedtls_printf( " failed\n ! issuer_key does not match "
650 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200651 goto exit;
652 }
653 }
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200656
657 if( opt.selfsign )
658 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100659 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200660 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200661 }
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
664 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200665
Paul Bakker9397dcb2013-09-06 09:55:26 +0200666 /*
667 * 1.0. Check the names for validity
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100672 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100673 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200674 goto exit;
675 }
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_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_issuer_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 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200686 fflush( stdout );
687
Hanno Becker38eff432017-09-22 15:38:20 +0100688 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100689 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200692 if( ret != 0 )
693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100695 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100696 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200697 goto exit;
698 }
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200701 if( ret != 0 )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100704 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100705 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200706 goto exit;
707 }
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200710
Hanno Becker38eff432017-09-22 15:38:20 +0100711 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
712 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200713 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100714 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
715 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200716
Hanno Becker6c13d372017-09-13 12:49:22 +0100717 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
718 opt.max_pathlen );
719 if( ret != 0 )
720 {
721 mbedtls_strerror( ret, buf, 1024 );
722 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100723 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100724 goto exit;
725 }
726
727 mbedtls_printf( " ok\n" );
728 }
Paul Bakker15162a02013-09-06 19:27:21 +0200729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100731 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
732 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200733 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100734 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
735 fflush( stdout );
736
737 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
738 if( ret != 0 )
739 {
740 mbedtls_strerror( ret, buf, 1024 );
741 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100742 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100743 -ret, buf );
744 goto exit;
745 }
746
747 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200748 }
749
Hanno Becker38eff432017-09-22 15:38:20 +0100750 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
751 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200752 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100753 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
754 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200755
Hanno Becker6c13d372017-09-13 12:49:22 +0100756 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
757 if( ret != 0 )
758 {
759 mbedtls_strerror( ret, buf, 1024 );
760 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100761 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100762 -ret, buf );
763 goto exit;
764 }
765
766 mbedtls_printf( " ok\n" );
767 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200769
Hanno Becker38eff432017-09-22 15:38:20 +0100770 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
771 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200774 fflush( stdout );
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200777 if( ret != 0 )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100780 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100781 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200782 goto exit;
783 }
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200786 }
787
Hanno Becker38eff432017-09-22 15:38:20 +0100788 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
789 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200792 fflush( stdout );
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200795 if( ret != 0 )
796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100798 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100799 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200800 goto exit;
801 }
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200804 }
805
Paul Bakker9397dcb2013-09-06 09:55:26 +0200806 /*
Hanno Becker45d006a2018-08-23 15:26:06 +0100807 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200810 fflush( stdout );
811
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200812 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100816 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100817 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200818 goto exit;
819 }
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200822
Andres Amaya Garciaa7ac5ab2018-04-29 21:42:45 +0100823 exit_code = MBEDTLS_EXIT_SUCCESS;
824
Paul Bakker9397dcb2013-09-06 09:55:26 +0200825exit:
Hanno Becker294e5842018-10-05 09:49:33 +0100826#if defined(MBEDTLS_X509_CSR_PARSE_C)
827 mbedtls_x509_csr_free( &csr );
828#endif /* MBEDTLS_X509_CSR_PARSE_C */
829 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_x509write_crt_free( &crt );
831 mbedtls_pk_free( &loaded_subject_key );
832 mbedtls_pk_free( &loaded_issuer_key );
833 mbedtls_mpi_free( &serial );
834 mbedtls_ctr_drbg_free( &ctr_drbg );
835 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200836
837#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200839 fflush( stdout ); getchar();
840#endif
841
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200842 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
845 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100846 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */