blob: 1eeb861e880b21e700137b36198e9b89a5cb8676 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Simon Butcher203a6932016-10-07 15:00:17 +010037#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
38 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
39 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
41 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042int main( void )
43{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 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 +020045 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
47 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020048 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020049}
50#else
51
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/x509_crt.h"
53#include "mbedtls/x509_csr.h"
54#include "mbedtls/entropy.h"
55#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010056#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020058
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000064#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010065 " request_file=%%s default: (empty)\n" \
66 " If request_file is specified, subject_key,\n" \
67 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000068#else
69#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000071
Paul Bakker1014e952013-09-09 13:59:42 +020072#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020073#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_SUBJECT_KEY "subject.key"
75#define DFL_ISSUER_KEY "ca.key"
76#define DFL_SUBJECT_PWD ""
77#define DFL_ISSUER_PWD ""
78#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000079#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
80#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020081#define DFL_NOT_BEFORE "20010101000000"
82#define DFL_NOT_AFTER "20301231235959"
83#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020084#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020085#define DFL_IS_CA 0
86#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
88#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010089#define DFL_VERSION 3
90#define DFL_AUTH_IDENT 1
91#define DFL_SUBJ_IDENT 1
92#define DFL_CONSTRAINTS 1
93#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020094
Rich Evans18b78c72015-02-11 14:06:19 +000095#define USAGE \
96 "\n usage: cert_write param=<>...\n" \
97 "\n acceptable parameters:\n" \
98 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010099 " subject_key=%%s default: subject.key\n" \
100 " subject_pwd=%%s default: (empty)\n" \
101 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000102 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100103 " issuer_crt=%%s default: (empty)\n" \
104 " If issuer_crt is specified, issuer_name is\n" \
105 " ignored!\n" \
106 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000107 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100108 " selfsign=%%d default: 0 (false)\n" \
109 " If selfsign is enabled, issuer_name and\n" \
110 " issuer_key are required (issuer_crt and\n" \
111 " subject_* are ignored\n" \
112 " issuer_key=%%s default: ca.key\n" \
113 " issuer_pwd=%%s default: (empty)\n" \
114 " output_file=%%s default: cert.crt\n" \
115 " serial=%%s default: 1\n" \
116 " not_before=%%s default: 20010101000000\n"\
117 " not_after=%%s default: 20301231235959\n"\
118 " is_ca=%%d default: 0 (disabled)\n" \
119 " max_pathlen=%%d default: -1 (none)\n" \
120 " md=%%s default: SHA256\n" \
121 " Supported values:\n" \
Hanno Becker024b53a2019-06-03 14:36:59 +0100122 " MD2, MD4, MD5, SHA1, SHA256, SHA512\n"\
Hanno Becker81535d02017-09-13 15:39:59 +0100123 " version=%%d default: 3\n" \
124 " Possible values: 1, 2, 3\n"\
125 " subject_identifier=%%s default: 1\n" \
126 " Possible values: 0, 1\n" \
127 " (Considered for v3 only)\n"\
128 " authority_identifier=%%s default: 1\n" \
129 " Possible values: 0, 1\n" \
130 " (Considered for v3 only)\n"\
131 " basic_constraints=%%d default: 1\n" \
132 " Possible values: 0, 1\n" \
133 " (Considered for v3 only)\n"\
134 " key_usage=%%s default: (empty)\n" \
135 " Comma-separated-list of values:\n" \
136 " digital_signature\n" \
137 " non_repudiation\n" \
138 " key_encipherment\n" \
139 " data_encipherment\n" \
140 " key_agreement\n" \
141 " key_cert_sign\n" \
142 " crl_sign\n" \
143 " (Considered for v3 only)\n"\
144 " ns_cert_type=%%s default: (empty)\n" \
145 " Comma-separated-list of values:\n" \
146 " ssl_client\n" \
147 " ssl_server\n" \
148 " email\n" \
149 " object_signing\n" \
150 " ssl_ca\n" \
151 " email_ca\n" \
152 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000153 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000154
Simon Butcher63cb97e2018-12-06 17:43:31 +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 );
Hanno Becker30a95102018-10-05 09:49:33 +0100245 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CSR_PARSE_C)
247 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200250 memset( buf, 0, 1024 );
251
252 if( argc == 0 )
253 {
254 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200256 goto exit;
257 }
258
Paul Bakker1014e952013-09-09 13:59:42 +0200259 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200260 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200261 opt.subject_key = DFL_SUBJECT_KEY;
262 opt.issuer_key = DFL_ISSUER_KEY;
263 opt.subject_pwd = DFL_SUBJECT_PWD;
264 opt.issuer_pwd = DFL_ISSUER_PWD;
265 opt.output_file = DFL_OUTPUT_FILENAME;
266 opt.subject_name = DFL_SUBJECT_NAME;
267 opt.issuer_name = DFL_ISSUER_NAME;
268 opt.not_before = DFL_NOT_BEFORE;
269 opt.not_after = DFL_NOT_AFTER;
270 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200271 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200272 opt.is_ca = DFL_IS_CA;
273 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.key_usage = DFL_KEY_USAGE;
275 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100276 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100277 opt.md = DFL_DIGEST;
278 opt.subject_identifier = DFL_SUBJ_IDENT;
279 opt.authority_identifier = DFL_AUTH_IDENT;
280 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200281
282 for( i = 1; i < argc; i++ )
283 {
284
285 p = argv[i];
286 if( ( q = strchr( p, '=' ) ) == NULL )
287 goto usage;
288 *q++ = '\0';
289
Paul Bakkere2673fb2013-09-09 15:52:07 +0200290 if( strcmp( p, "request_file" ) == 0 )
291 opt.request_file = q;
292 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293 opt.subject_key = q;
294 else if( strcmp( p, "issuer_key" ) == 0 )
295 opt.issuer_key = q;
296 else if( strcmp( p, "subject_pwd" ) == 0 )
297 opt.subject_pwd = q;
298 else if( strcmp( p, "issuer_pwd" ) == 0 )
299 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200300 else if( strcmp( p, "issuer_crt" ) == 0 )
301 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200302 else if( strcmp( p, "output_file" ) == 0 )
303 opt.output_file = q;
304 else if( strcmp( p, "subject_name" ) == 0 )
305 {
306 opt.subject_name = q;
307 }
308 else if( strcmp( p, "issuer_name" ) == 0 )
309 {
310 opt.issuer_name = q;
311 }
312 else if( strcmp( p, "not_before" ) == 0 )
313 {
314 opt.not_before = q;
315 }
316 else if( strcmp( p, "not_after" ) == 0 )
317 {
318 opt.not_after = q;
319 }
320 else if( strcmp( p, "serial" ) == 0 )
321 {
322 opt.serial = q;
323 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100324 else if( strcmp( p, "authority_identifier" ) == 0 )
325 {
326 opt.authority_identifier = atoi( q );
327 if( opt.authority_identifier != 0 &&
328 opt.authority_identifier != 1 )
329 {
Hanno Becker17c32762017-10-03 14:56:04 +0100330 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100331 goto usage;
332 }
333 }
334 else if( strcmp( p, "subject_identifier" ) == 0 )
335 {
336 opt.subject_identifier = atoi( q );
337 if( opt.subject_identifier != 0 &&
338 opt.subject_identifier != 1 )
339 {
Hanno Becker17c32762017-10-03 14:56:04 +0100340 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100341 goto usage;
342 }
343 }
344 else if( strcmp( p, "basic_constraints" ) == 0 )
345 {
346 opt.basic_constraints = atoi( q );
347 if( opt.basic_constraints != 0 &&
348 opt.basic_constraints != 1 )
349 {
Hanno Becker17c32762017-10-03 14:56:04 +0100350 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100351 goto usage;
352 }
353 }
354 else if( strcmp( p, "md" ) == 0 )
355 {
356 if( strcmp( q, "SHA1" ) == 0 )
357 opt.md = MBEDTLS_MD_SHA1;
Ron Eldor991a05b2019-02-12 15:05:53 +0200358 else if( strcmp( q, "SHA224" ) == 0 )
359 opt.md = MBEDTLS_MD_SHA224;
Hanno Becker6c13d372017-09-13 12:49:22 +0100360 else if( strcmp( q, "SHA256" ) == 0 )
361 opt.md = MBEDTLS_MD_SHA256;
Ron Eldor991a05b2019-02-12 15:05:53 +0200362 else if( strcmp( q, "SHA384" ) == 0 )
363 opt.md = MBEDTLS_MD_SHA384;
Hanno Becker6c13d372017-09-13 12:49:22 +0100364 else if( strcmp( q, "SHA512" ) == 0 )
365 opt.md = MBEDTLS_MD_SHA512;
Hanno Becker9dbc5612019-06-03 14:10:44 +0100366 else if( strcmp( q, "MD2" ) == 0 )
367 opt.md = MBEDTLS_MD_MD2;
368 else if( strcmp( q, "MD4" ) == 0 )
369 opt.md = MBEDTLS_MD_MD4;
Hanno Becker6c13d372017-09-13 12:49:22 +0100370 else if( strcmp( q, "MD5" ) == 0 )
371 opt.md = MBEDTLS_MD_MD5;
372 else
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 Becker6c13d372017-09-13 12:49:22 +0100377 }
378 else if( strcmp( p, "version" ) == 0 )
379 {
380 opt.version = atoi( q );
381 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100382 {
383 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100384 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100385 }
Hanno Becker38eff432017-09-22 15:38:20 +0100386 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100387 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200388 else if( strcmp( p, "selfsign" ) == 0 )
389 {
390 opt.selfsign = atoi( q );
391 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100392 {
393 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200394 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100395 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200396 }
Paul Bakker15162a02013-09-06 19:27:21 +0200397 else if( strcmp( p, "is_ca" ) == 0 )
398 {
399 opt.is_ca = atoi( q );
400 if( opt.is_ca < 0 || opt.is_ca > 1 )
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 }
406 else if( strcmp( p, "max_pathlen" ) == 0 )
407 {
408 opt.max_pathlen = atoi( q );
409 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100410 {
411 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200412 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100413 }
Paul Bakker15162a02013-09-06 19:27:21 +0200414 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 else if( strcmp( p, "key_usage" ) == 0 )
416 {
417 while( q != NULL )
418 {
419 if( ( r = strchr( q, ',' ) ) != NULL )
420 *r++ = '\0';
421
422 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200424 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200426 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100427 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200428 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100429 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200430 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100431 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200432 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200436 else
Hanno Becker17c32762017-10-03 14:56:04 +0100437 {
438 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100440 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441
442 q = r;
443 }
444 }
445 else if( strcmp( p, "ns_cert_type" ) == 0 )
446 {
447 while( q != NULL )
448 {
449 if( ( r = strchr( q, ',' ) ) != NULL )
450 *r++ = '\0';
451
452 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100455 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200460 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100461 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200462 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100463 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200464 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100465 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200466 else
Hanno Becker17c32762017-10-03 14:56:04 +0100467 {
468 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200469 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100470 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200471
472 q = r;
473 }
474 }
475 else
476 goto usage;
477 }
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200480
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200481 /*
482 * 0. Seed the PRNG
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200485 fflush( stdout );
486
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200487 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200488 (const unsigned char *) pers,
489 strlen( pers ) ) ) != 0 )
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100492 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
493 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200494 goto exit;
495 }
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200498
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 // Parse serial to MPI
500 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200502 fflush( stdout );
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100507 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200508 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200509 goto exit;
510 }
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200513
Paul Bakker1014e952013-09-09 13:59:42 +0200514 // Parse issuer certificate if present
515 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200516 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200517 {
518 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200519 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200522 fflush( stdout );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100527 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200528 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200529 goto exit;
530 }
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200533 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200534 if( ret < 0 )
535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100537 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200538 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200539 goto exit;
540 }
541
Paul Bakkere2673fb2013-09-09 15:52:07 +0200542 opt.issuer_name = issuer_name;
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200545 }
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200548 // Parse certificate request if present
549 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200550 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 {
552 /*
553 * 1.0.b. Load the CSR
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200556 fflush( stdout );
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100561 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200562 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200563 goto exit;
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200567 &csr.subject );
568 if( ret < 0 )
569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100571 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200572 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200573 goto exit;
574 }
575
576 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200577 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200582
583 /*
584 * 1.1. Load the keys
585 */
586 if( !opt.selfsign && !strlen( opt.request_file ) )
587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200589 fflush( stdout );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200592 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200593 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100596 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200597 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200598 goto exit;
599 }
Paul Bakker1014e952013-09-09 13:59:42 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200602 }
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200605 fflush( stdout );
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200609 if( ret != 0 )
610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100612 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200613 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200614 goto exit;
615 }
616
617 // Check if key and issuer certificate match
618 //
619 if( strlen( opt.issuer_crt ) )
620 {
Ron Eldor0049f782017-02-07 19:14:58 +0200621 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200622 {
Hanno Becker81535d02017-09-13 15:39:59 +0100623 mbedtls_printf( " failed\n ! issuer_key does not match "
624 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200625 goto exit;
626 }
627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200630
631 if( opt.selfsign )
632 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100633 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200634 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200635 }
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
638 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200639
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 /*
641 * 1.0. Check the names for validity
642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_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_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200647 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200648 goto exit;
649 }
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100654 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200655 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200656 goto exit;
657 }
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660 fflush( stdout );
661
Hanno Becker38eff432017-09-22 15:38:20 +0100662 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100663 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200666 if( ret != 0 )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100669 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200670 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200671 goto exit;
672 }
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200675 if( ret != 0 )
676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100678 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200679 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200680 goto exit;
681 }
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200684
Hanno Becker38eff432017-09-22 15:38:20 +0100685 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
686 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200687 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100688 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
689 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200690
Hanno Becker6c13d372017-09-13 12:49:22 +0100691 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
692 opt.max_pathlen );
693 if( ret != 0 )
694 {
695 mbedtls_strerror( ret, buf, 1024 );
696 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200697 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100698 goto exit;
699 }
700
701 mbedtls_printf( " ok\n" );
702 }
Paul Bakker15162a02013-09-06 19:27:21 +0200703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100705 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
706 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200707 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100708 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
709 fflush( stdout );
710
711 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
712 if( ret != 0 )
713 {
714 mbedtls_strerror( ret, buf, 1024 );
715 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100716 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200717 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100718 goto exit;
719 }
720
721 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200722 }
723
Hanno Becker38eff432017-09-22 15:38:20 +0100724 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
725 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200726 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100727 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
728 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200729
Hanno Becker6c13d372017-09-13 12:49:22 +0100730 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
731 if( ret != 0 )
732 {
733 mbedtls_strerror( ret, buf, 1024 );
734 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100735 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200736 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100737 goto exit;
738 }
739
740 mbedtls_printf( " ok\n" );
741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200743
Hanno Becker38eff432017-09-22 15:38:20 +0100744 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
745 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200748 fflush( stdout );
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200751 if( ret != 0 )
752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100754 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200755 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200756 goto exit;
757 }
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200760 }
761
Hanno Becker38eff432017-09-22 15:38:20 +0100762 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
763 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200766 fflush( stdout );
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200769 if( ret != 0 )
770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100772 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200773 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200774 goto exit;
775 }
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200778 }
779
Paul Bakker9397dcb2013-09-06 09:55:26 +0200780 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100781 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200784 fflush( stdout );
785
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200786 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100790 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200791 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200792 goto exit;
793 }
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200796
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100797 exit_code = MBEDTLS_EXIT_SUCCESS;
798
Paul Bakker9397dcb2013-09-06 09:55:26 +0200799exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100800#if defined(MBEDTLS_X509_CSR_PARSE_C)
801 mbedtls_x509_csr_free( &csr );
802#endif /* MBEDTLS_X509_CSR_PARSE_C */
803 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_x509write_crt_free( &crt );
805 mbedtls_pk_free( &loaded_subject_key );
806 mbedtls_pk_free( &loaded_issuer_key );
807 mbedtls_mpi_free( &serial );
808 mbedtls_ctr_drbg_free( &ctr_drbg );
809 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200810
811#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200813 fflush( stdout ); getchar();
814#endif
815
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200816 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
819 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100820 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */