blob: fa994613d64b2972a2851555136eaed2f6ba3ffd [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000037
Simon Butcher203a6932016-10-07 15:00:17 +010038#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
39 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
41 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
42 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020043int main( void )
44{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 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 +020046 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
48 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020049 return( 0 );
50}
51#else
52
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/x509_crt.h"
54#include "mbedtls/x509_csr.h"
55#include "mbedtls/entropy.h"
56#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010057#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059
Rich Evans18b78c72015-02-11 14:06:19 +000060#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000065#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010066 " request_file=%%s default: (empty)\n" \
67 " If request_file is specified, subject_key,\n" \
68 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000069#else
70#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000072
Paul Bakker1014e952013-09-09 13:59:42 +020073#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020074#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020075#define DFL_SUBJECT_KEY "subject.key"
76#define DFL_ISSUER_KEY "ca.key"
77#define DFL_SUBJECT_PWD ""
78#define DFL_ISSUER_PWD ""
79#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000080#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
81#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020082#define DFL_NOT_BEFORE "20010101000000"
83#define DFL_NOT_AFTER "20301231235959"
84#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020085#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020086#define DFL_IS_CA 0
87#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020088#define DFL_KEY_USAGE 0
89#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010090#define DFL_VERSION 3
91#define DFL_AUTH_IDENT 1
92#define DFL_SUBJ_IDENT 1
93#define DFL_CONSTRAINTS 1
94#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020095
Rich Evans18b78c72015-02-11 14:06:19 +000096#define USAGE \
97 "\n usage: cert_write param=<>...\n" \
98 "\n acceptable parameters:\n" \
99 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100100 " subject_key=%%s default: subject.key\n" \
101 " subject_pwd=%%s default: (empty)\n" \
102 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000103 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100104 " issuer_crt=%%s default: (empty)\n" \
105 " If issuer_crt is specified, issuer_name is\n" \
106 " ignored!\n" \
107 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000108 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100109 " selfsign=%%d default: 0 (false)\n" \
110 " If selfsign is enabled, issuer_name and\n" \
111 " issuer_key are required (issuer_crt and\n" \
112 " subject_* are ignored\n" \
113 " issuer_key=%%s default: ca.key\n" \
114 " issuer_pwd=%%s default: (empty)\n" \
115 " output_file=%%s default: cert.crt\n" \
116 " serial=%%s default: 1\n" \
117 " not_before=%%s default: 20010101000000\n"\
118 " not_after=%%s default: 20301231235959\n"\
119 " is_ca=%%d default: 0 (disabled)\n" \
120 " max_pathlen=%%d default: -1 (none)\n" \
121 " md=%%s default: SHA256\n" \
122 " Supported values:\n" \
123 " MD5, SHA1, SHA256, SHA512\n"\
124 " version=%%d default: 3\n" \
125 " Possible values: 1, 2, 3\n"\
126 " subject_identifier=%%s default: 1\n" \
127 " Possible values: 0, 1\n" \
128 " (Considered for v3 only)\n"\
129 " authority_identifier=%%s default: 1\n" \
130 " Possible values: 0, 1\n" \
131 " (Considered for v3 only)\n"\
132 " basic_constraints=%%d default: 1\n" \
133 " Possible values: 0, 1\n" \
134 " (Considered for v3 only)\n"\
135 " key_usage=%%s default: (empty)\n" \
136 " Comma-separated-list of values:\n" \
137 " digital_signature\n" \
138 " non_repudiation\n" \
139 " key_encipherment\n" \
140 " data_encipherment\n" \
141 " key_agreement\n" \
142 " key_cert_sign\n" \
143 " crl_sign\n" \
144 " (Considered for v3 only)\n"\
145 " ns_cert_type=%%s default: (empty)\n" \
146 " Comma-separated-list of values:\n" \
147 " ssl_client\n" \
148 " ssl_server\n" \
149 " email\n" \
150 " object_signing\n" \
151 " ssl_ca\n" \
152 " email_ca\n" \
153 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000154 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000155
Paul Bakker9397dcb2013-09-06 09:55:26 +0200156/*
157 * global options
158 */
159struct options
160{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100161 const char *issuer_crt; /* filename of the issuer certificate */
162 const char *request_file; /* filename of the certificate request */
163 const char *subject_key; /* filename of the subject key file */
164 const char *issuer_key; /* filename of the issuer key file */
165 const char *subject_pwd; /* password for the subject key file */
166 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100167 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100168 const char *subject_name; /* subject name for certificate */
169 const char *issuer_name; /* issuer name for certificate */
170 const char *not_before; /* validity period not before */
171 const char *not_after; /* validity period not after */
172 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200173 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200174 int is_ca; /* is a CA certificate */
175 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100176 int authority_identifier; /* add authority identifier to CRT */
177 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100178 int basic_constraints; /* add basic constraints ext to CRT */
179 int version; /* CRT version */
180 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181 unsigned char key_usage; /* key usage flags */
182 unsigned char ns_cert_type; /* NS cert type */
183} opt;
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200186 int (*f_rng)(void *, unsigned char *, size_t),
187 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200188{
189 int ret;
190 FILE *f;
191 unsigned char output_buf[4096];
192 size_t len = 0;
193
194 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100195 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
196 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 return( ret );
198
199 len = strlen( (char *) output_buf );
200
201 if( ( f = fopen( output_file, "w" ) ) == NULL )
202 return( -1 );
203
204 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200205 {
206 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200208 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209
Paul Bakker0c226102014-04-17 16:02:36 +0200210 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211
212 return( 0 );
213}
214
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215int main( int argc, char *argv[] )
216{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100217 int ret = 1;
218 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_x509_crt issuer_crt;
220 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
221 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200222 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200224 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100225 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200226 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200228 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_x509write_cert crt;
232 mbedtls_mpi serial;
233 mbedtls_entropy_context entropy;
234 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200235 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236
237 /*
238 * Set to sane values
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_pk_init( &loaded_issuer_key );
242 mbedtls_pk_init( &loaded_subject_key );
243 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200244 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_X509_CSR_PARSE_C)
246 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 memset( buf, 0, 1024 );
250
251 if( argc == 0 )
252 {
253 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200255 goto exit;
256 }
257
Paul Bakker1014e952013-09-09 13:59:42 +0200258 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200259 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200260 opt.subject_key = DFL_SUBJECT_KEY;
261 opt.issuer_key = DFL_ISSUER_KEY;
262 opt.subject_pwd = DFL_SUBJECT_PWD;
263 opt.issuer_pwd = DFL_ISSUER_PWD;
264 opt.output_file = DFL_OUTPUT_FILENAME;
265 opt.subject_name = DFL_SUBJECT_NAME;
266 opt.issuer_name = DFL_ISSUER_NAME;
267 opt.not_before = DFL_NOT_BEFORE;
268 opt.not_after = DFL_NOT_AFTER;
269 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200270 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200271 opt.is_ca = DFL_IS_CA;
272 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200273 opt.key_usage = DFL_KEY_USAGE;
274 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100275 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100276 opt.md = DFL_DIGEST;
277 opt.subject_identifier = DFL_SUBJ_IDENT;
278 opt.authority_identifier = DFL_AUTH_IDENT;
279 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200280
281 for( i = 1; i < argc; i++ )
282 {
283
284 p = argv[i];
285 if( ( q = strchr( p, '=' ) ) == NULL )
286 goto usage;
287 *q++ = '\0';
288
Paul Bakkere2673fb2013-09-09 15:52:07 +0200289 if( strcmp( p, "request_file" ) == 0 )
290 opt.request_file = q;
291 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200292 opt.subject_key = q;
293 else if( strcmp( p, "issuer_key" ) == 0 )
294 opt.issuer_key = q;
295 else if( strcmp( p, "subject_pwd" ) == 0 )
296 opt.subject_pwd = q;
297 else if( strcmp( p, "issuer_pwd" ) == 0 )
298 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200299 else if( strcmp( p, "issuer_crt" ) == 0 )
300 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200301 else if( strcmp( p, "output_file" ) == 0 )
302 opt.output_file = q;
303 else if( strcmp( p, "subject_name" ) == 0 )
304 {
305 opt.subject_name = q;
306 }
307 else if( strcmp( p, "issuer_name" ) == 0 )
308 {
309 opt.issuer_name = q;
310 }
311 else if( strcmp( p, "not_before" ) == 0 )
312 {
313 opt.not_before = q;
314 }
315 else if( strcmp( p, "not_after" ) == 0 )
316 {
317 opt.not_after = q;
318 }
319 else if( strcmp( p, "serial" ) == 0 )
320 {
321 opt.serial = q;
322 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100323 else if( strcmp( p, "authority_identifier" ) == 0 )
324 {
325 opt.authority_identifier = atoi( q );
326 if( opt.authority_identifier != 0 &&
327 opt.authority_identifier != 1 )
328 {
Hanno Becker17c32762017-10-03 14:56:04 +0100329 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100330 goto usage;
331 }
332 }
333 else if( strcmp( p, "subject_identifier" ) == 0 )
334 {
335 opt.subject_identifier = atoi( q );
336 if( opt.subject_identifier != 0 &&
337 opt.subject_identifier != 1 )
338 {
Hanno Becker17c32762017-10-03 14:56:04 +0100339 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100340 goto usage;
341 }
342 }
343 else if( strcmp( p, "basic_constraints" ) == 0 )
344 {
345 opt.basic_constraints = atoi( q );
346 if( opt.basic_constraints != 0 &&
347 opt.basic_constraints != 1 )
348 {
Hanno Becker17c32762017-10-03 14:56:04 +0100349 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100350 goto usage;
351 }
352 }
353 else if( strcmp( p, "md" ) == 0 )
354 {
355 if( strcmp( q, "SHA1" ) == 0 )
356 opt.md = MBEDTLS_MD_SHA1;
357 else if( strcmp( q, "SHA256" ) == 0 )
358 opt.md = MBEDTLS_MD_SHA256;
359 else if( strcmp( q, "SHA512" ) == 0 )
360 opt.md = MBEDTLS_MD_SHA512;
361 else if( strcmp( q, "MD5" ) == 0 )
362 opt.md = MBEDTLS_MD_MD5;
363 else
Hanno Becker17c32762017-10-03 14:56:04 +0100364 {
365 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100366 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100367 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100368 }
369 else if( strcmp( p, "version" ) == 0 )
370 {
371 opt.version = atoi( q );
372 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100373 {
374 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100375 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100376 }
Hanno Becker38eff432017-09-22 15:38:20 +0100377 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100378 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200379 else if( strcmp( p, "selfsign" ) == 0 )
380 {
381 opt.selfsign = atoi( q );
382 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100383 {
384 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200385 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100386 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200387 }
Paul Bakker15162a02013-09-06 19:27:21 +0200388 else if( strcmp( p, "is_ca" ) == 0 )
389 {
390 opt.is_ca = atoi( q );
391 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100392 {
393 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200394 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100395 }
Paul Bakker15162a02013-09-06 19:27:21 +0200396 }
397 else if( strcmp( p, "max_pathlen" ) == 0 )
398 {
399 opt.max_pathlen = atoi( q );
400 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100401 {
402 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200403 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100404 }
Paul Bakker15162a02013-09-06 19:27:21 +0200405 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200406 else if( strcmp( p, "key_usage" ) == 0 )
407 {
408 while( q != NULL )
409 {
410 if( ( r = strchr( q, ',' ) ) != NULL )
411 *r++ = '\0';
412
413 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200417 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100418 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100420 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100422 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 else
Hanno Becker17c32762017-10-03 14:56:04 +0100428 {
429 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200430 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100431 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200432
433 q = r;
434 }
435 }
436 else if( strcmp( p, "ns_cert_type" ) == 0 )
437 {
438 while( q != NULL )
439 {
440 if( ( r = strchr( q, ',' ) ) != NULL )
441 *r++ = '\0';
442
443 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200445 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100446 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200447 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200449 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200451 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100452 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100454 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100456 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200457 else
Hanno Becker17c32762017-10-03 14:56:04 +0100458 {
459 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200460 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100461 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200462
463 q = r;
464 }
465 }
466 else
467 goto usage;
468 }
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200471
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200472 /*
473 * 0. Seed the PRNG
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200476 fflush( stdout );
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200479 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200480 (const unsigned char *) pers,
481 strlen( pers ) ) ) != 0 )
482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100484 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
485 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200486 goto exit;
487 }
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200490
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 // Parse serial to MPI
492 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200494 fflush( stdout );
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100499 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100500 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200501 goto exit;
502 }
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200505
Paul Bakker1014e952013-09-09 13:59:42 +0200506 // Parse issuer certificate if present
507 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200509 {
510 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200511 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200514 fflush( stdout );
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100519 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100520 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200521 goto exit;
522 }
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200525 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200526 if( ret < 0 )
527 {
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_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100530 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200531 goto exit;
532 }
533
Paul Bakkere2673fb2013-09-09 15:52:07 +0200534 opt.issuer_name = issuer_name;
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200537 }
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200540 // Parse certificate request if present
541 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200542 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200543 {
544 /*
545 * 1.0.b. Load the CSR
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 fflush( stdout );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100553 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100554 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200555 goto exit;
556 }
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200559 &csr.subject );
560 if( ret < 0 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100563 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100564 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200565 goto exit;
566 }
567
568 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200569 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200574
575 /*
576 * 1.1. Load the keys
577 */
578 if( !opt.selfsign && !strlen( opt.request_file ) )
579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200581 fflush( stdout );
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200584 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200585 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100588 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100589 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200590 goto exit;
591 }
Paul Bakker1014e952013-09-09 13:59:42 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200597 fflush( stdout );
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200601 if( ret != 0 )
602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100604 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
605 "returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200606 goto exit;
607 }
608
609 // Check if key and issuer certificate match
610 //
611 if( strlen( opt.issuer_crt ) )
612 {
Ron Eldor0049f782017-02-07 19:14:58 +0200613 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200614 {
Hanno Becker81535d02017-09-13 15:39:59 +0100615 mbedtls_printf( " failed\n ! issuer_key does not match "
616 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200617 goto exit;
618 }
619 }
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200622
623 if( opt.selfsign )
624 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100625 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200626 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
630 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200631
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 /*
633 * 1.0. Check the names for validity
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100638 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100639 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 goto exit;
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100646 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100647 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200648 goto exit;
649 }
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200652 fflush( stdout );
653
Hanno Becker38eff432017-09-22 15:38:20 +0100654 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100655 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200658 if( ret != 0 )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100661 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100662 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200663 goto exit;
664 }
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200667 if( ret != 0 )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100670 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100671 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200672 goto exit;
673 }
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200676
Hanno Becker38eff432017-09-22 15:38:20 +0100677 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
678 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200679 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100680 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
681 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200682
Hanno Becker6c13d372017-09-13 12:49:22 +0100683 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
684 opt.max_pathlen );
685 if( ret != 0 )
686 {
687 mbedtls_strerror( ret, buf, 1024 );
688 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100689 "returned -0x%04x - %s\n\n", -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100690 goto exit;
691 }
692
693 mbedtls_printf( " ok\n" );
694 }
Paul Bakker15162a02013-09-06 19:27:21 +0200695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100697 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
698 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200699 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100700 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
701 fflush( stdout );
702
703 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
704 if( ret != 0 )
705 {
706 mbedtls_strerror( ret, buf, 1024 );
707 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100708 "_key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100709 -ret, buf );
710 goto exit;
711 }
712
713 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200714 }
715
Hanno Becker38eff432017-09-22 15:38:20 +0100716 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
717 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200718 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100719 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
720 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200721
Hanno Becker6c13d372017-09-13 12:49:22 +0100722 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
723 if( ret != 0 )
724 {
725 mbedtls_strerror( ret, buf, 1024 );
726 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100727 "key_identifier returned -0x%04x - %s\n\n",
Hanno Becker6c13d372017-09-13 12:49:22 +0100728 -ret, buf );
729 goto exit;
730 }
731
732 mbedtls_printf( " ok\n" );
733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200735
Hanno Becker38eff432017-09-22 15:38:20 +0100736 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
737 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200740 fflush( stdout );
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200743 if( ret != 0 )
744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100746 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100747 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200748 goto exit;
749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200752 }
753
Hanno Becker38eff432017-09-22 15:38:20 +0100754 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
755 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200758 fflush( stdout );
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200761 if( ret != 0 )
762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100764 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Hanno Becker7f3652d2017-09-22 15:39:02 +0100765 "returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200766 goto exit;
767 }
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200770 }
771
Paul Bakker9397dcb2013-09-06 09:55:26 +0200772 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100773 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200776 fflush( stdout );
777
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200778 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100782 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Hanno Becker81535d02017-09-13 15:39:59 +0100783 -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200784 goto exit;
785 }
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200788
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100789 exit_code = MBEDTLS_EXIT_SUCCESS;
790
Paul Bakker9397dcb2013-09-06 09:55:26 +0200791exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_x509write_crt_free( &crt );
793 mbedtls_pk_free( &loaded_subject_key );
794 mbedtls_pk_free( &loaded_issuer_key );
795 mbedtls_mpi_free( &serial );
796 mbedtls_ctr_drbg_free( &ctr_drbg );
797 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200798
799#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200801 fflush( stdout ); getchar();
802#endif
803
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100804 return( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
807 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100808 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */