blob: f3de95ddb8676e81c1f2b671ac29fc1a791175ab [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Simon Butcher52146072016-10-07 15:00:17 +010035#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
36 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
37 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
38 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
39 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020040int main( void )
41{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 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 +020043 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
45 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/x509_crt.h"
51#include "mbedtls/x509_csr.h"
52#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
Hanno Becker781af0d2017-09-13 12:49:22 +010054#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020056
Rich Evans18b78c72015-02-11 14:06:19 +000057#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000062#define USAGE_CSR \
Hanno Becker7de3ff32017-09-13 15:39:59 +010063 " request_file=%%s default: (empty)\n" \
64 " If request_file is specified, subject_key,\n" \
65 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000066#else
67#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000069
Paul Bakker1014e952013-09-09 13:59:42 +020070#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020071#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020072#define DFL_SUBJECT_KEY "subject.key"
73#define DFL_ISSUER_KEY "ca.key"
74#define DFL_SUBJECT_PWD ""
75#define DFL_ISSUER_PWD ""
76#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000077#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
78#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020079#define DFL_NOT_BEFORE "20010101000000"
80#define DFL_NOT_AFTER "20301231235959"
81#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020082#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020083#define DFL_IS_CA 0
84#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020085#define DFL_KEY_USAGE 0
86#define DFL_NS_CERT_TYPE 0
Hanno Becker781af0d2017-09-13 12:49:22 +010087#define DFL_VERSION 3
88#define DFL_AUTH_IDENT 1
89#define DFL_SUBJ_IDENT 1
90#define DFL_CONSTRAINTS 1
91#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020092
Rich Evans18b78c72015-02-11 14:06:19 +000093#define USAGE \
94 "\n usage: cert_write param=<>...\n" \
95 "\n acceptable parameters:\n" \
96 USAGE_CSR \
Hanno Becker7de3ff32017-09-13 15:39:59 +010097 " subject_key=%%s default: subject.key\n" \
98 " subject_pwd=%%s default: (empty)\n" \
99 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000100 "\n" \
Hanno Becker7de3ff32017-09-13 15:39:59 +0100101 " issuer_crt=%%s default: (empty)\n" \
102 " If issuer_crt is specified, issuer_name is\n" \
103 " ignored!\n" \
104 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000105 "\n" \
Hanno Becker7de3ff32017-09-13 15:39:59 +0100106 " selfsign=%%d default: 0 (false)\n" \
107 " If selfsign is enabled, issuer_name and\n" \
108 " issuer_key are required (issuer_crt and\n" \
109 " subject_* are ignored\n" \
110 " issuer_key=%%s default: ca.key\n" \
111 " issuer_pwd=%%s default: (empty)\n" \
112 " output_file=%%s default: cert.crt\n" \
113 " serial=%%s default: 1\n" \
114 " not_before=%%s default: 20010101000000\n"\
115 " not_after=%%s default: 20301231235959\n"\
116 " is_ca=%%d default: 0 (disabled)\n" \
117 " max_pathlen=%%d default: -1 (none)\n" \
118 " md=%%s default: SHA256\n" \
119 " Supported values:\n" \
120 " MD5, SHA1, SHA256, SHA512\n"\
121 " version=%%d default: 3\n" \
122 " Possible values: 1, 2, 3\n"\
123 " subject_identifier=%%s default: 1\n" \
124 " Possible values: 0, 1\n" \
125 " (Considered for v3 only)\n"\
126 " authority_identifier=%%s default: 1\n" \
127 " Possible values: 0, 1\n" \
128 " (Considered for v3 only)\n"\
129 " basic_constraints=%%d default: 1\n" \
130 " Possible values: 0, 1\n" \
131 " (Considered for v3 only)\n"\
132 " key_usage=%%s default: (empty)\n" \
133 " Comma-separated-list of values:\n" \
134 " digital_signature\n" \
135 " non_repudiation\n" \
136 " key_encipherment\n" \
137 " data_encipherment\n" \
138 " key_agreement\n" \
139 " key_cert_sign\n" \
140 " crl_sign\n" \
141 " (Considered for v3 only)\n"\
142 " ns_cert_type=%%s default: (empty)\n" \
143 " Comma-separated-list of values:\n" \
144 " ssl_client\n" \
145 " ssl_server\n" \
146 " email\n" \
147 " object_signing\n" \
148 " ssl_ca\n" \
149 " email_ca\n" \
150 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000151 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000152
Paul Bakker9397dcb2013-09-06 09:55:26 +0200153/*
154 * global options
155 */
156struct options
157{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100158 const char *issuer_crt; /* filename of the issuer certificate */
159 const char *request_file; /* filename of the certificate request */
160 const char *subject_key; /* filename of the subject key file */
161 const char *issuer_key; /* filename of the issuer key file */
162 const char *subject_pwd; /* password for the subject key file */
163 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker6b1b5982018-08-23 15:26:06 +0100164 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100165 const char *subject_name; /* subject name for certificate */
166 const char *issuer_name; /* issuer name for certificate */
167 const char *not_before; /* validity period not before */
168 const char *not_after; /* validity period not after */
169 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200170 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200171 int is_ca; /* is a CA certificate */
172 int max_pathlen; /* maximum CA path length */
Hanno Becker4f4864a2017-09-22 15:35:16 +0100173 int authority_identifier; /* add authority identifier to CRT */
174 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker781af0d2017-09-13 12:49:22 +0100175 int basic_constraints; /* add basic constraints ext to CRT */
176 int version; /* CRT version */
177 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178 unsigned char key_usage; /* key usage flags */
179 unsigned char ns_cert_type; /* NS cert type */
180} opt;
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200183 int (*f_rng)(void *, unsigned char *, size_t),
184 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185{
186 int ret;
187 FILE *f;
188 unsigned char output_buf[4096];
189 size_t len = 0;
190
191 memset( output_buf, 0, 4096 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100192 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
193 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 return( ret );
195
196 len = strlen( (char *) output_buf );
197
198 if( ( f = fopen( output_file, "w" ) ) == NULL )
199 return( -1 );
200
201 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200202 {
203 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200205 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206
Paul Bakker0c226102014-04-17 16:02:36 +0200207 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208
209 return( 0 );
210}
211
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212int main( int argc, char *argv[] )
213{
214 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_x509_crt issuer_crt;
216 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
217 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200218 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200219 char buf[1024];
Jonathan Leroy3dd85dd2015-10-10 21:58:07 +0200220 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100221 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200222 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroy3dd85dd2015-10-10 21:58:07 +0200224 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_x509write_cert crt;
228 mbedtls_mpi serial;
229 mbedtls_entropy_context entropy;
230 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200231 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200232
233 /*
234 * Set to sane values
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_pk_init( &loaded_issuer_key );
238 mbedtls_pk_init( &loaded_subject_key );
239 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200240 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerb0d59a12018-10-05 09:49:33 +0100241 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CSR_PARSE_C)
243 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200246 memset( buf, 0, 1024 );
247
248 if( argc == 0 )
249 {
250 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252 ret = 1;
253 goto exit;
254 }
255
Paul Bakker1014e952013-09-09 13:59:42 +0200256 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200257 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 opt.subject_key = DFL_SUBJECT_KEY;
259 opt.issuer_key = DFL_ISSUER_KEY;
260 opt.subject_pwd = DFL_SUBJECT_PWD;
261 opt.issuer_pwd = DFL_ISSUER_PWD;
262 opt.output_file = DFL_OUTPUT_FILENAME;
263 opt.subject_name = DFL_SUBJECT_NAME;
264 opt.issuer_name = DFL_ISSUER_NAME;
265 opt.not_before = DFL_NOT_BEFORE;
266 opt.not_after = DFL_NOT_AFTER;
267 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200268 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200269 opt.is_ca = DFL_IS_CA;
270 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271 opt.key_usage = DFL_KEY_USAGE;
272 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100273 opt.version = DFL_VERSION - 1;
Hanno Becker781af0d2017-09-13 12:49:22 +0100274 opt.md = DFL_DIGEST;
275 opt.subject_identifier = DFL_SUBJ_IDENT;
276 opt.authority_identifier = DFL_AUTH_IDENT;
277 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200278
279 for( i = 1; i < argc; i++ )
280 {
281
282 p = argv[i];
283 if( ( q = strchr( p, '=' ) ) == NULL )
284 goto usage;
285 *q++ = '\0';
286
Paul Bakkere2673fb2013-09-09 15:52:07 +0200287 if( strcmp( p, "request_file" ) == 0 )
288 opt.request_file = q;
289 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200290 opt.subject_key = q;
291 else if( strcmp( p, "issuer_key" ) == 0 )
292 opt.issuer_key = q;
293 else if( strcmp( p, "subject_pwd" ) == 0 )
294 opt.subject_pwd = q;
295 else if( strcmp( p, "issuer_pwd" ) == 0 )
296 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200297 else if( strcmp( p, "issuer_crt" ) == 0 )
298 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 else if( strcmp( p, "output_file" ) == 0 )
300 opt.output_file = q;
301 else if( strcmp( p, "subject_name" ) == 0 )
302 {
303 opt.subject_name = q;
304 }
305 else if( strcmp( p, "issuer_name" ) == 0 )
306 {
307 opt.issuer_name = q;
308 }
309 else if( strcmp( p, "not_before" ) == 0 )
310 {
311 opt.not_before = q;
312 }
313 else if( strcmp( p, "not_after" ) == 0 )
314 {
315 opt.not_after = q;
316 }
317 else if( strcmp( p, "serial" ) == 0 )
318 {
319 opt.serial = q;
320 }
Hanno Becker781af0d2017-09-13 12:49:22 +0100321 else if( strcmp( p, "authority_identifier" ) == 0 )
322 {
323 opt.authority_identifier = atoi( q );
324 if( opt.authority_identifier != 0 &&
325 opt.authority_identifier != 1 )
326 {
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100327 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker781af0d2017-09-13 12:49:22 +0100328 goto usage;
329 }
330 }
331 else if( strcmp( p, "subject_identifier" ) == 0 )
332 {
333 opt.subject_identifier = atoi( q );
334 if( opt.subject_identifier != 0 &&
335 opt.subject_identifier != 1 )
336 {
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100337 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker781af0d2017-09-13 12:49:22 +0100338 goto usage;
339 }
340 }
341 else if( strcmp( p, "basic_constraints" ) == 0 )
342 {
343 opt.basic_constraints = atoi( q );
344 if( opt.basic_constraints != 0 &&
345 opt.basic_constraints != 1 )
346 {
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100347 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker781af0d2017-09-13 12:49:22 +0100348 goto usage;
349 }
350 }
351 else if( strcmp( p, "md" ) == 0 )
352 {
353 if( strcmp( q, "SHA1" ) == 0 )
354 opt.md = MBEDTLS_MD_SHA1;
355 else if( strcmp( q, "SHA256" ) == 0 )
356 opt.md = MBEDTLS_MD_SHA256;
357 else if( strcmp( q, "SHA512" ) == 0 )
358 opt.md = MBEDTLS_MD_SHA512;
359 else if( strcmp( q, "MD5" ) == 0 )
360 opt.md = MBEDTLS_MD_MD5;
361 else
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100362 {
363 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker781af0d2017-09-13 12:49:22 +0100364 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100365 }
Hanno Becker781af0d2017-09-13 12:49:22 +0100366 }
367 else if( strcmp( p, "version" ) == 0 )
368 {
369 opt.version = atoi( q );
370 if( opt.version < 1 || opt.version > 3 )
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100371 {
372 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker781af0d2017-09-13 12:49:22 +0100373 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100374 }
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100375 opt.version--;
Hanno Becker781af0d2017-09-13 12:49:22 +0100376 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200377 else if( strcmp( p, "selfsign" ) == 0 )
378 {
379 opt.selfsign = atoi( q );
380 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100381 {
382 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200383 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100384 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200385 }
Paul Bakker15162a02013-09-06 19:27:21 +0200386 else if( strcmp( p, "is_ca" ) == 0 )
387 {
388 opt.is_ca = atoi( q );
389 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100390 {
391 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200392 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100393 }
Paul Bakker15162a02013-09-06 19:27:21 +0200394 }
395 else if( strcmp( p, "max_pathlen" ) == 0 )
396 {
397 opt.max_pathlen = atoi( q );
398 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100399 {
400 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200401 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100402 }
Paul Bakker15162a02013-09-06 19:27:21 +0200403 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200404 else if( strcmp( p, "key_usage" ) == 0 )
405 {
406 while( q != NULL )
407 {
408 if( ( r = strchr( q, ',' ) ) != NULL )
409 *r++ = '\0';
410
411 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100416 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200417 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100418 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100420 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100426 {
427 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200428 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100429 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200430
431 q = r;
432 }
433 }
434 else if( strcmp( p, "ns_cert_type" ) == 0 )
435 {
436 while( q != NULL )
437 {
438 if( ( r = strchr( q, ',' ) ) != NULL )
439 *r++ = '\0';
440
441 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200443 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100444 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200445 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200447 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200449 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100450 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200451 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100452 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100454 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 else
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100456 {
457 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 goto usage;
Hanno Beckercdba5cd2017-10-03 14:56:04 +0100459 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200460
461 q = r;
462 }
463 }
464 else
465 goto usage;
466 }
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200469
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200470 /*
471 * 0. Seed the PRNG
472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200474 fflush( stdout );
475
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200476 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200477 (const unsigned char *) pers,
478 strlen( pers ) ) ) != 0 )
479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100481 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
482 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200483 goto exit;
484 }
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200487
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 // Parse serial to MPI
489 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200491 fflush( stdout );
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100496 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker37de7752017-09-22 15:39:02 +0100497 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200498 goto exit;
499 }
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200502
Paul Bakker1014e952013-09-09 13:59:42 +0200503 // Parse issuer certificate if present
504 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200505 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200506 {
507 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200508 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200511 fflush( stdout );
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100516 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker37de7752017-09-22 15:39:02 +0100517 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200518 goto exit;
519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200522 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200523 if( ret < 0 )
524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100526 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker37de7752017-09-22 15:39:02 +0100527 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200528 goto exit;
529 }
530
Paul Bakkere2673fb2013-09-09 15:52:07 +0200531 opt.issuer_name = issuer_name;
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200534 }
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200537 // Parse certificate request if present
538 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200539 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200540 {
541 /*
542 * 1.0.b. Load the CSR
543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200545 fflush( stdout );
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100550 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker37de7752017-09-22 15:39:02 +0100551 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200552 goto exit;
553 }
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200556 &csr.subject );
557 if( ret < 0 )
558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100560 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker37de7752017-09-22 15:39:02 +0100561 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200562 goto exit;
563 }
564
565 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200566 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200571
572 /*
573 * 1.1. Load the keys
574 */
575 if( !opt.selfsign && !strlen( opt.request_file ) )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200578 fflush( stdout );
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200581 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200582 if( ret != 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 Becker7de3ff32017-09-13 15:39:59 +0100585 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Hanno Becker37de7752017-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 }
Paul Bakker1014e952013-09-09 13:59:42 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200591 }
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200594 fflush( stdout );
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200597 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200598 if( ret != 0 )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100601 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
602 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200603 goto exit;
604 }
605
606 // Check if key and issuer certificate match
607 //
608 if( strlen( opt.issuer_crt ) )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
611 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
612 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
613 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
614 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615 {
Hanno Becker7de3ff32017-09-13 15:39:59 +0100616 mbedtls_printf( " failed\n ! issuer_key does not match "
617 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200618 ret = -1;
619 goto exit;
620 }
621 }
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200624
625 if( opt.selfsign )
626 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100627 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200628 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
632 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200633
Paul Bakker9397dcb2013-09-06 09:55:26 +0200634 /*
635 * 1.0. Check the names for validity
636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100640 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker37de7752017-09-22 15:39:02 +0100641 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 goto exit;
643 }
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100648 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker37de7752017-09-22 15:39:02 +0100649 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650 goto exit;
651 }
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654 fflush( stdout );
655
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100656 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker781af0d2017-09-13 12:49:22 +0100657 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660 if( ret != 0 )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100663 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker37de7752017-09-22 15:39:02 +0100664 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200665 goto exit;
666 }
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200669 if( ret != 0 )
670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100672 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker37de7752017-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 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200678
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100679 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
680 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200681 {
Hanno Becker781af0d2017-09-13 12:49:22 +0100682 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
683 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200684
Hanno Becker781af0d2017-09-13 12:49:22 +0100685 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
686 opt.max_pathlen );
687 if( ret != 0 )
688 {
689 mbedtls_strerror( ret, buf, 1024 );
690 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker37de7752017-09-22 15:39:02 +0100691 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker781af0d2017-09-13 12:49:22 +0100692 goto exit;
693 }
694
695 mbedtls_printf( " ok\n" );
696 }
Paul Bakker15162a02013-09-06 19:27:21 +0200697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA1_C)
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100699 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
700 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200701 {
Hanno Becker781af0d2017-09-13 12:49:22 +0100702 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
703 fflush( stdout );
704
705 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
706 if( ret != 0 )
707 {
708 mbedtls_strerror( ret, buf, 1024 );
709 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker37de7752017-09-22 15:39:02 +0100710 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker781af0d2017-09-13 12:49:22 +0100711 -ret, buf );
712 goto exit;
713 }
714
715 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200716 }
717
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100718 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
719 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200720 {
Hanno Becker781af0d2017-09-13 12:49:22 +0100721 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
722 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200723
Hanno Becker781af0d2017-09-13 12:49:22 +0100724 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
725 if( ret != 0 )
726 {
727 mbedtls_strerror( ret, buf, 1024 );
728 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker37de7752017-09-22 15:39:02 +0100729 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker781af0d2017-09-13 12:49:22 +0100730 -ret, buf );
731 goto exit;
732 }
733
734 mbedtls_printf( " ok\n" );
735 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200737
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100738 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
739 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200742 fflush( stdout );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200745 if( ret != 0 )
746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100748 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker37de7752017-09-22 15:39:02 +0100749 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200750 goto exit;
751 }
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200754 }
755
Hanno Becker54d6c5b2017-09-22 15:38:20 +0100756 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
757 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200760 fflush( stdout );
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200763 if( ret != 0 )
764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7de3ff32017-09-13 15:39:59 +0100766 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker37de7752017-09-22 15:39:02 +0100767 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200768 goto exit;
769 }
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200772 }
773
Paul Bakker9397dcb2013-09-06 09:55:26 +0200774 /*
Hanno Becker6b1b5982018-08-23 15:26:06 +0100775 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200778 fflush( stdout );
779
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200780 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker37de7752017-09-22 15:39:02 +0100784 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker7de3ff32017-09-13 15:39:59 +0100785 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200786 goto exit;
787 }
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200790
791exit:
Hanno Beckerb0d59a12018-10-05 09:49:33 +0100792#if defined(MBEDTLS_X509_CSR_PARSE_C)
793 mbedtls_x509_csr_free( &csr );
794#endif /* MBEDTLS_X509_CSR_PARSE_C */
795 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_x509write_crt_free( &crt );
797 mbedtls_pk_free( &loaded_subject_key );
798 mbedtls_pk_free( &loaded_issuer_key );
799 mbedtls_mpi_free( &serial );
800 mbedtls_ctr_drbg_free( &ctr_drbg );
801 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200802
803#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200805 fflush( stdout ); getchar();
806#endif
807
808 return( ret );
809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
811 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher52146072016-10-07 15:00:17 +0100812 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */