blob: da0a624a06b8fe7e7b535353b25b85c1c9a50166 [file] [log] [blame]
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +01001
Paul Bakker9397dcb2013-09-06 09:55:26 +02002/*
3 * Certificate generation and signing
4 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020019 */
20
Bence Szépkútic662b362021-05-27 11:25:03 +020021#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000025#else
Rich Evans18b78c72015-02-11 14:06:19 +000026#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010027#include <stdlib.h>
28#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010029#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010030#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010031#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
32#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000033
Simon Butcher203a6932016-10-07 15:00:17 +010034#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
35 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
36 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
37 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
38 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020039int main( void )
40{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 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 +020042 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
44 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020045 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/x509_crt.h"
50#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010051#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-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
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010061#define SET_OID(x, oid) \
62 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char*)oid; } while( 0 )
63
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
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010089#define DFL_EXT_KEY_USAGE 0
Paul Bakker9397dcb2013-09-06 09:55:26 +020090#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010091#define DFL_VERSION 3
92#define DFL_AUTH_IDENT 1
93#define DFL_SUBJ_IDENT 1
94#define DFL_CONSTRAINTS 1
95#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020096
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE \
98 "\n usage: cert_write param=<>...\n" \
99 "\n acceptable parameters:\n" \
100 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100101 " subject_key=%%s default: subject.key\n" \
102 " subject_pwd=%%s default: (empty)\n" \
103 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " issuer_crt=%%s default: (empty)\n" \
106 " If issuer_crt is specified, issuer_name is\n" \
107 " ignored!\n" \
108 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100110 " selfsign=%%d default: 0 (false)\n" \
111 " If selfsign is enabled, issuer_name and\n" \
112 " issuer_key are required (issuer_crt and\n" \
113 " subject_* are ignored\n" \
114 " issuer_key=%%s default: ca.key\n" \
115 " issuer_pwd=%%s default: (empty)\n" \
116 " output_file=%%s default: cert.crt\n" \
117 " serial=%%s default: 1\n" \
118 " not_before=%%s default: 20010101000000\n"\
119 " not_after=%%s default: 20301231235959\n"\
120 " is_ca=%%d default: 0 (disabled)\n" \
121 " max_pathlen=%%d default: -1 (none)\n" \
122 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200123 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200124 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200125 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " version=%%d default: 3\n" \
127 " Possible values: 1, 2, 3\n"\
128 " subject_identifier=%%s default: 1\n" \
129 " Possible values: 0, 1\n" \
130 " (Considered for v3 only)\n"\
131 " authority_identifier=%%s default: 1\n" \
132 " Possible values: 0, 1\n" \
133 " (Considered for v3 only)\n"\
134 " basic_constraints=%%d default: 1\n" \
135 " Possible values: 0, 1\n" \
136 " (Considered for v3 only)\n"\
137 " key_usage=%%s default: (empty)\n" \
138 " Comma-separated-list of values:\n" \
139 " digital_signature\n" \
140 " non_repudiation\n" \
141 " key_encipherment\n" \
142 " data_encipherment\n" \
143 " key_agreement\n" \
144 " key_cert_sign\n" \
145 " crl_sign\n" \
146 " (Considered for v3 only)\n"\
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100147 " ext_key_usage=%%s default: (empty)\n" \
148 " Comma-separated-list of values:\n" \
149 " serverAuth\n" \
150 " clientAuth\n" \
151 " codeSigning\n" \
152 " emailProtection\n" \
153 " timeStamping\n" \
154 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100155 " ns_cert_type=%%s default: (empty)\n" \
156 " Comma-separated-list of values:\n" \
157 " ssl_client\n" \
158 " ssl_server\n" \
159 " email\n" \
160 " object_signing\n" \
161 " ssl_ca\n" \
162 " email_ca\n" \
163 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000164 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000165
Simon Butcher63cb97e2018-12-06 17:43:31 +0000166
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167/*
168 * global options
169 */
170struct options
171{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100172 const char *issuer_crt; /* filename of the issuer certificate */
173 const char *request_file; /* filename of the certificate request */
174 const char *subject_key; /* filename of the subject key file */
175 const char *issuer_key; /* filename of the issuer key file */
176 const char *subject_pwd; /* password for the subject key file */
177 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100178 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100179 const char *subject_name; /* subject name for certificate */
180 const char *issuer_name; /* issuer name for certificate */
181 const char *not_before; /* validity period not before */
182 const char *not_after; /* validity period not after */
183 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200184 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200185 int is_ca; /* is a CA certificate */
186 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100187 int authority_identifier; /* add authority identifier to CRT */
188 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100189 int basic_constraints; /* add basic constraints ext to CRT */
190 int version; /* CRT version */
191 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100193 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 unsigned char ns_cert_type; /* NS cert type */
195} opt;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200198 int (*f_rng)(void *, unsigned char *, size_t),
199 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200{
201 int ret;
202 FILE *f;
203 unsigned char output_buf[4096];
204 size_t len = 0;
205
206 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100207 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
208 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209 return( ret );
210
211 len = strlen( (char *) output_buf );
212
213 if( ( f = fopen( output_file, "w" ) ) == NULL )
214 return( -1 );
215
216 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200217 {
218 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200219 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200220 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221
Paul Bakker0c226102014-04-17 16:02:36 +0200222 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
224 return( 0 );
225}
226
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227int main( int argc, char *argv[] )
228{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100229 int ret = 1;
230 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_x509_crt issuer_crt;
232 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
233 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200234 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200236 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100237 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200240 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509write_cert crt;
244 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100245 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_entropy_context entropy;
247 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200248 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
250 /*
251 * Set to sane values
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pk_init( &loaded_issuer_key );
255 mbedtls_pk_init( &loaded_subject_key );
256 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200257 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100258 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_X509_CSR_PARSE_C)
260 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200263 memset( buf, 0, 1024 );
264
265 if( argc == 0 )
266 {
267 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269 goto exit;
270 }
271
Paul Bakker1014e952013-09-09 13:59:42 +0200272 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200273 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.subject_key = DFL_SUBJECT_KEY;
275 opt.issuer_key = DFL_ISSUER_KEY;
276 opt.subject_pwd = DFL_SUBJECT_PWD;
277 opt.issuer_pwd = DFL_ISSUER_PWD;
278 opt.output_file = DFL_OUTPUT_FILENAME;
279 opt.subject_name = DFL_SUBJECT_NAME;
280 opt.issuer_name = DFL_ISSUER_NAME;
281 opt.not_before = DFL_NOT_BEFORE;
282 opt.not_after = DFL_NOT_AFTER;
283 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200284 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200285 opt.is_ca = DFL_IS_CA;
286 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100288 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100290 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100291 opt.md = DFL_DIGEST;
292 opt.subject_identifier = DFL_SUBJ_IDENT;
293 opt.authority_identifier = DFL_AUTH_IDENT;
294 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295
296 for( i = 1; i < argc; i++ )
297 {
298
299 p = argv[i];
300 if( ( q = strchr( p, '=' ) ) == NULL )
301 goto usage;
302 *q++ = '\0';
303
Paul Bakkere2673fb2013-09-09 15:52:07 +0200304 if( strcmp( p, "request_file" ) == 0 )
305 opt.request_file = q;
306 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 opt.subject_key = q;
308 else if( strcmp( p, "issuer_key" ) == 0 )
309 opt.issuer_key = q;
310 else if( strcmp( p, "subject_pwd" ) == 0 )
311 opt.subject_pwd = q;
312 else if( strcmp( p, "issuer_pwd" ) == 0 )
313 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200314 else if( strcmp( p, "issuer_crt" ) == 0 )
315 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316 else if( strcmp( p, "output_file" ) == 0 )
317 opt.output_file = q;
318 else if( strcmp( p, "subject_name" ) == 0 )
319 {
320 opt.subject_name = q;
321 }
322 else if( strcmp( p, "issuer_name" ) == 0 )
323 {
324 opt.issuer_name = q;
325 }
326 else if( strcmp( p, "not_before" ) == 0 )
327 {
328 opt.not_before = q;
329 }
330 else if( strcmp( p, "not_after" ) == 0 )
331 {
332 opt.not_after = q;
333 }
334 else if( strcmp( p, "serial" ) == 0 )
335 {
336 opt.serial = q;
337 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100338 else if( strcmp( p, "authority_identifier" ) == 0 )
339 {
340 opt.authority_identifier = atoi( q );
341 if( opt.authority_identifier != 0 &&
342 opt.authority_identifier != 1 )
343 {
Hanno Becker17c32762017-10-03 14:56:04 +0100344 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100345 goto usage;
346 }
347 }
348 else if( strcmp( p, "subject_identifier" ) == 0 )
349 {
350 opt.subject_identifier = atoi( q );
351 if( opt.subject_identifier != 0 &&
352 opt.subject_identifier != 1 )
353 {
Hanno Becker17c32762017-10-03 14:56:04 +0100354 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100355 goto usage;
356 }
357 }
358 else if( strcmp( p, "basic_constraints" ) == 0 )
359 {
360 opt.basic_constraints = atoi( q );
361 if( opt.basic_constraints != 0 &&
362 opt.basic_constraints != 1 )
363 {
Hanno Becker17c32762017-10-03 14:56:04 +0100364 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100365 goto usage;
366 }
367 }
368 else if( strcmp( p, "md" ) == 0 )
369 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200370 const mbedtls_md_info_t *md_info =
371 mbedtls_md_info_from_string( q );
372 if( md_info == NULL )
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 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200377 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100378 }
379 else if( strcmp( p, "version" ) == 0 )
380 {
381 opt.version = atoi( q );
382 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100383 {
384 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100385 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100386 }
Hanno Becker38eff432017-09-22 15:38:20 +0100387 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100388 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200389 else if( strcmp( p, "selfsign" ) == 0 )
390 {
391 opt.selfsign = atoi( q );
392 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100393 {
394 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200395 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100396 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200397 }
Paul Bakker15162a02013-09-06 19:27:21 +0200398 else if( strcmp( p, "is_ca" ) == 0 )
399 {
400 opt.is_ca = atoi( q );
401 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100402 {
403 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200404 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100405 }
Paul Bakker15162a02013-09-06 19:27:21 +0200406 }
407 else if( strcmp( p, "max_pathlen" ) == 0 )
408 {
409 opt.max_pathlen = atoi( q );
410 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100411 {
412 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200413 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100414 }
Paul Bakker15162a02013-09-06 19:27:21 +0200415 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416 else if( strcmp( p, "key_usage" ) == 0 )
417 {
418 while( q != NULL )
419 {
420 if( ( r = strchr( q, ',' ) ) != NULL )
421 *r++ = '\0';
422
423 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100428 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100430 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100432 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else
Hanno Becker17c32762017-10-03 14:56:04 +0100438 {
439 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200440 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100441 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200442
443 q = r;
444 }
445 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100446 else if( strcmp( p, "ext_key_usage" ) == 0 )
447 {
448 while( q != NULL )
449 {
450 if( ( r = strchr( q, ',' ) ) != NULL )
451 *r++ = '\0';
452
453 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
454 ext_key_usage->next = opt.ext_key_usage;
455 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
456 if( strcmp( q, "serverAuth" ) == 0 )
457 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
458 else if( strcmp( q, "clientAuth" ) == 0 )
459 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
460 else if( strcmp( q, "codeSigning" ) == 0 )
461 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
462 else if( strcmp( q, "emailProtection" ) == 0 )
463 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
464 else if( strcmp( q, "timeStamping" ) == 0 )
465 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
466 else if( strcmp( q, "OCSPSigning" ) == 0 )
467 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
468 else
469 goto usage;
470 opt.ext_key_usage = ext_key_usage;
471
472 q = r;
473 }
474 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200475 else if( strcmp( p, "ns_cert_type" ) == 0 )
476 {
477 while( q != NULL )
478 {
479 if( ( r = strchr( q, ',' ) ) != NULL )
480 *r++ = '\0';
481
482 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100485 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200486 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100491 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200492 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100493 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200494 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100495 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200496 else
Hanno Becker17c32762017-10-03 14:56:04 +0100497 {
498 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100500 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200501
502 q = r;
503 }
504 }
505 else
506 goto usage;
507 }
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200510
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200511 /*
512 * 0. Seed the PRNG
513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200515 fflush( stdout );
516
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200517 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200518 (const unsigned char *) pers,
519 strlen( pers ) ) ) != 0 )
520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100522 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
523 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200524 goto exit;
525 }
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200528
Paul Bakker9397dcb2013-09-06 09:55:26 +0200529 // Parse serial to MPI
530 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200532 fflush( stdout );
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 {
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_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200538 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200539 goto exit;
540 }
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200543
Paul Bakker1014e952013-09-09 13:59:42 +0200544 // Parse issuer certificate if present
545 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200546 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200547 {
548 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200549 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200552 fflush( stdout );
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100557 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200558 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200559 goto exit;
560 }
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200563 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200564 if( ret < 0 )
565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100567 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200568 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200569 goto exit;
570 }
571
Paul Bakkere2673fb2013-09-09 15:52:07 +0200572 opt.issuer_name = issuer_name;
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200575 }
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 // Parse certificate request if present
579 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200580 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200581 {
582 /*
583 * 1.0.b. Load the CSR
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 fflush( stdout );
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100591 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200592 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200593 goto exit;
594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200597 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200598 if( ret < 0 )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100601 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200602 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200603 goto exit;
604 }
605
606 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200607 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200612
613 /*
614 * 1.1. Load the keys
615 */
616 if( !opt.selfsign && !strlen( opt.request_file ) )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200619 fflush( stdout );
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200622 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200623 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100626 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200627 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200628 goto exit;
629 }
Paul Bakker1014e952013-09-09 13:59:42 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200635 fflush( stdout );
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200638 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200639 if( ret != 0 )
640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100642 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200643 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200644 goto exit;
645 }
646
647 // Check if key and issuer certificate match
648 //
649 if( strlen( opt.issuer_crt ) )
650 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200651 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200652 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200653 {
Hanno Becker81535d02017-09-13 15:39:59 +0100654 mbedtls_printf( " failed\n ! issuer_key does not match "
655 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200656 goto exit;
657 }
658 }
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200661
662 if( opt.selfsign )
663 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100664 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200665 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200666 }
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
669 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200670
Paul Bakker9397dcb2013-09-06 09:55:26 +0200671 /*
672 * 1.0. Check the names for validity
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100677 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200678 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200679 goto exit;
680 }
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100685 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200686 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200687 goto exit;
688 }
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200691 fflush( stdout );
692
Hanno Becker38eff432017-09-22 15:38:20 +0100693 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100694 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200697 if( ret != 0 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100700 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200701 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200702 goto exit;
703 }
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200706 if( ret != 0 )
707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100709 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200710 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200711 goto exit;
712 }
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200715
Hanno Becker38eff432017-09-22 15:38:20 +0100716 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
717 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200718 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100719 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
720 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200721
Hanno Becker6c13d372017-09-13 12:49:22 +0100722 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
723 opt.max_pathlen );
724 if( ret != 0 )
725 {
726 mbedtls_strerror( ret, buf, 1024 );
727 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200728 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100729 goto exit;
730 }
731
732 mbedtls_printf( " ok\n" );
733 }
Paul Bakker15162a02013-09-06 19:27:21 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100736 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
737 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200738 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100739 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
740 fflush( stdout );
741
742 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
743 if( ret != 0 )
744 {
745 mbedtls_strerror( ret, buf, 1024 );
746 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100747 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200748 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100749 goto exit;
750 }
751
752 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200753 }
754
Hanno Becker38eff432017-09-22 15:38:20 +0100755 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
756 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200757 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100758 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
759 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200760
Hanno Becker6c13d372017-09-13 12:49:22 +0100761 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
762 if( ret != 0 )
763 {
764 mbedtls_strerror( ret, buf, 1024 );
765 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100766 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200767 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100768 goto exit;
769 }
770
771 mbedtls_printf( " ok\n" );
772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200774
Hanno Becker38eff432017-09-22 15:38:20 +0100775 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
776 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200779 fflush( stdout );
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200782 if( ret != 0 )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100785 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200786 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200787 goto exit;
788 }
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200791 }
792
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100793 if( opt.ext_key_usage )
794 {
795 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
796 fflush( stdout );
797
798 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
799 if( ret != 0 )
800 {
801 mbedtls_strerror( ret, buf, 1024 );
802 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n", -ret, buf );
803 goto exit;
804 }
805
806 mbedtls_printf( " ok\n" );
807 }
808
Hanno Becker38eff432017-09-22 15:38:20 +0100809 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
810 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200813 fflush( stdout );
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200816 if( ret != 0 )
817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100819 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200820 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200821 goto exit;
822 }
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200825 }
826
Paul Bakker9397dcb2013-09-06 09:55:26 +0200827 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100828 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200831 fflush( stdout );
832
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200833 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100837 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200838 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200839 goto exit;
840 }
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200843
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100844 exit_code = MBEDTLS_EXIT_SUCCESS;
845
Paul Bakker9397dcb2013-09-06 09:55:26 +0200846exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100847#if defined(MBEDTLS_X509_CSR_PARSE_C)
848 mbedtls_x509_csr_free( &csr );
849#endif /* MBEDTLS_X509_CSR_PARSE_C */
850 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_x509write_crt_free( &crt );
852 mbedtls_pk_free( &loaded_subject_key );
853 mbedtls_pk_free( &loaded_issuer_key );
854 mbedtls_mpi_free( &serial );
855 mbedtls_ctr_drbg_free( &ctr_drbg );
856 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200857
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200858 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
861 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100862 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */