blob: e0d88b207d7f36e1a964e72edbdb289f4158f471 [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
Nicholas Wilson99a96b12015-09-10 18:28:01 +010088#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020089#define DFL_KEY_USAGE 0
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010090#define DFL_EXT_KEY_USAGE 0
Paul Bakker9397dcb2013-09-06 09:55:26 +020091#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010092#define DFL_VERSION 3
93#define DFL_AUTH_IDENT 1
94#define DFL_SUBJ_IDENT 1
95#define DFL_CONSTRAINTS 1
96#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020097
Rich Evans18b78c72015-02-11 14:06:19 +000098#define USAGE \
99 "\n usage: cert_write param=<>...\n" \
100 "\n acceptable parameters:\n" \
101 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100102 " subject_key=%%s default: subject.key\n" \
103 " subject_pwd=%%s default: (empty)\n" \
104 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000105 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100106 " issuer_crt=%%s default: (empty)\n" \
107 " If issuer_crt is specified, issuer_name is\n" \
108 " ignored!\n" \
109 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000110 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100111 " selfsign=%%d default: 0 (false)\n" \
112 " If selfsign is enabled, issuer_name and\n" \
113 " issuer_key are required (issuer_crt and\n" \
114 " subject_* are ignored\n" \
115 " issuer_key=%%s default: ca.key\n" \
116 " issuer_pwd=%%s default: (empty)\n" \
117 " output_file=%%s default: cert.crt\n" \
118 " serial=%%s default: 1\n" \
119 " not_before=%%s default: 20010101000000\n"\
120 " not_after=%%s default: 20301231235959\n"\
121 " is_ca=%%d default: 0 (disabled)\n" \
122 " max_pathlen=%%d default: -1 (none)\n" \
123 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200124 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200125 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200126 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100127 " version=%%d default: 3\n" \
128 " Possible values: 1, 2, 3\n"\
129 " subject_identifier=%%s default: 1\n" \
130 " Possible values: 0, 1\n" \
131 " (Considered for v3 only)\n"\
132 " authority_identifier=%%s default: 1\n" \
133 " Possible values: 0, 1\n" \
134 " (Considered for v3 only)\n"\
135 " basic_constraints=%%d default: 1\n" \
136 " Possible values: 0, 1\n" \
137 " (Considered for v3 only)\n"\
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100138 " sig_alg=%%s default: SHA-256\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100139 " key_usage=%%s default: (empty)\n" \
140 " Comma-separated-list of values:\n" \
141 " digital_signature\n" \
142 " non_repudiation\n" \
143 " key_encipherment\n" \
144 " data_encipherment\n" \
145 " key_agreement\n" \
146 " key_cert_sign\n" \
147 " crl_sign\n" \
148 " (Considered for v3 only)\n"\
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100149 " ext_key_usage=%%s default: (empty)\n" \
150 " Comma-separated-list of values:\n" \
151 " serverAuth\n" \
152 " clientAuth\n" \
153 " codeSigning\n" \
154 " emailProtection\n" \
155 " timeStamping\n" \
156 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100157 " ns_cert_type=%%s default: (empty)\n" \
158 " Comma-separated-list of values:\n" \
159 " ssl_client\n" \
160 " ssl_server\n" \
161 " email\n" \
162 " object_signing\n" \
163 " ssl_ca\n" \
164 " email_ca\n" \
165 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000166 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000167
Simon Butcher63cb97e2018-12-06 17:43:31 +0000168
Paul Bakker9397dcb2013-09-06 09:55:26 +0200169/*
170 * global options
171 */
172struct options
173{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100174 const char *issuer_crt; /* filename of the issuer certificate */
175 const char *request_file; /* filename of the certificate request */
176 const char *subject_key; /* filename of the subject key file */
177 const char *issuer_key; /* filename of the issuer key file */
178 const char *subject_pwd; /* password for the subject key file */
179 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100180 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100181 const char *subject_name; /* subject name for certificate */
182 const char *issuer_name; /* issuer name for certificate */
183 const char *not_before; /* validity period not before */
184 const char *not_after; /* validity period not after */
185 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200186 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200187 int is_ca; /* is a CA certificate */
188 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100189 int authority_identifier; /* add authority identifier to CRT */
190 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100191 int basic_constraints; /* add basic constraints ext to CRT */
192 int version; /* CRT version */
193 mbedtls_md_type_t md; /* Hash used for signing */
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100194 mbedtls_md_type_t sig_alg; /* MD to use generating signature */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100196 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 unsigned char ns_cert_type; /* NS cert type */
198} opt;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200201 int (*f_rng)(void *, unsigned char *, size_t),
202 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203{
204 int ret;
205 FILE *f;
206 unsigned char output_buf[4096];
207 size_t len = 0;
208
209 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100210 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
211 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212 return( ret );
213
214 len = strlen( (char *) output_buf );
215
216 if( ( f = fopen( output_file, "w" ) ) == NULL )
217 return( -1 );
218
219 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200220 {
221 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200222 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200223 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224
Paul Bakker0c226102014-04-17 16:02:36 +0200225 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200226
227 return( 0 );
228}
229
Paul Bakker9397dcb2013-09-06 09:55:26 +0200230int main( int argc, char *argv[] )
231{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100232 int ret = 1;
233 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_x509_crt issuer_crt;
235 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
236 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200237 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200239 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100240 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200241 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200243 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509write_cert crt;
247 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100248 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_entropy_context entropy;
250 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200251 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252
253 /*
254 * Set to sane values
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_pk_init( &loaded_issuer_key );
258 mbedtls_pk_init( &loaded_subject_key );
259 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200260 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100261 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_X509_CSR_PARSE_C)
263 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266 memset( buf, 0, 1024 );
267
268 if( argc == 0 )
269 {
270 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200272 goto exit;
273 }
274
Paul Bakker1014e952013-09-09 13:59:42 +0200275 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200276 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200277 opt.subject_key = DFL_SUBJECT_KEY;
278 opt.issuer_key = DFL_ISSUER_KEY;
279 opt.subject_pwd = DFL_SUBJECT_PWD;
280 opt.issuer_pwd = DFL_ISSUER_PWD;
281 opt.output_file = DFL_OUTPUT_FILENAME;
282 opt.subject_name = DFL_SUBJECT_NAME;
283 opt.issuer_name = DFL_ISSUER_NAME;
284 opt.not_before = DFL_NOT_BEFORE;
285 opt.not_after = DFL_NOT_AFTER;
286 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200287 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200288 opt.is_ca = DFL_IS_CA;
289 opt.max_pathlen = DFL_MAX_PATHLEN;
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100290 opt.sig_alg = DFL_SIG_ALG;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200291 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100292 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100294 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100295 opt.md = DFL_DIGEST;
296 opt.subject_identifier = DFL_SUBJ_IDENT;
297 opt.authority_identifier = DFL_AUTH_IDENT;
298 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299
300 for( i = 1; i < argc; i++ )
301 {
302
303 p = argv[i];
304 if( ( q = strchr( p, '=' ) ) == NULL )
305 goto usage;
306 *q++ = '\0';
307
Paul Bakkere2673fb2013-09-09 15:52:07 +0200308 if( strcmp( p, "request_file" ) == 0 )
309 opt.request_file = q;
310 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200311 opt.subject_key = q;
312 else if( strcmp( p, "issuer_key" ) == 0 )
313 opt.issuer_key = q;
314 else if( strcmp( p, "subject_pwd" ) == 0 )
315 opt.subject_pwd = q;
316 else if( strcmp( p, "issuer_pwd" ) == 0 )
317 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200318 else if( strcmp( p, "issuer_crt" ) == 0 )
319 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200320 else if( strcmp( p, "output_file" ) == 0 )
321 opt.output_file = q;
322 else if( strcmp( p, "subject_name" ) == 0 )
323 {
324 opt.subject_name = q;
325 }
326 else if( strcmp( p, "issuer_name" ) == 0 )
327 {
328 opt.issuer_name = q;
329 }
330 else if( strcmp( p, "not_before" ) == 0 )
331 {
332 opt.not_before = q;
333 }
334 else if( strcmp( p, "not_after" ) == 0 )
335 {
336 opt.not_after = q;
337 }
338 else if( strcmp( p, "serial" ) == 0 )
339 {
340 opt.serial = q;
341 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100342 else if( strcmp( p, "authority_identifier" ) == 0 )
343 {
344 opt.authority_identifier = atoi( q );
345 if( opt.authority_identifier != 0 &&
346 opt.authority_identifier != 1 )
347 {
Hanno Becker17c32762017-10-03 14:56:04 +0100348 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100349 goto usage;
350 }
351 }
352 else if( strcmp( p, "subject_identifier" ) == 0 )
353 {
354 opt.subject_identifier = atoi( q );
355 if( opt.subject_identifier != 0 &&
356 opt.subject_identifier != 1 )
357 {
Hanno Becker17c32762017-10-03 14:56:04 +0100358 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100359 goto usage;
360 }
361 }
362 else if( strcmp( p, "basic_constraints" ) == 0 )
363 {
364 opt.basic_constraints = atoi( q );
365 if( opt.basic_constraints != 0 &&
366 opt.basic_constraints != 1 )
367 {
Hanno Becker17c32762017-10-03 14:56:04 +0100368 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100369 goto usage;
370 }
371 }
372 else if( strcmp( p, "md" ) == 0 )
373 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200374 const mbedtls_md_info_t *md_info =
375 mbedtls_md_info_from_string( q );
376 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100377 {
378 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100379 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100380 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200381 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100382 }
383 else if( strcmp( p, "version" ) == 0 )
384 {
385 opt.version = atoi( q );
386 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100387 {
388 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100389 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100390 }
Hanno Becker38eff432017-09-22 15:38:20 +0100391 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100392 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200393 else if( strcmp( p, "selfsign" ) == 0 )
394 {
395 opt.selfsign = atoi( q );
396 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100397 {
398 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200399 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100400 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200401 }
Paul Bakker15162a02013-09-06 19:27:21 +0200402 else if( strcmp( p, "is_ca" ) == 0 )
403 {
404 opt.is_ca = atoi( q );
405 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100406 {
407 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200408 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100409 }
Paul Bakker15162a02013-09-06 19:27:21 +0200410 }
411 else if( strcmp( p, "max_pathlen" ) == 0 )
412 {
413 opt.max_pathlen = atoi( q );
414 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100415 {
416 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200417 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100418 }
Paul Bakker15162a02013-09-06 19:27:21 +0200419 }
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100420 else if( strcmp( p, "sig_alg") == 0 )
421 {
422 if( strcmp( q, "SHA-1" ) == 0 )
423 opt.sig_alg = MBEDTLS_MD_SHA1;
424 else if( strcmp( q, "SHA-256" ) == 0 )
425 opt.sig_alg = MBEDTLS_MD_SHA256;
426 else if( strcmp( q, "MD5" ) == 0 )
427 opt.sig_alg = MBEDTLS_MD_MD5;
428 else
429 goto usage;
430 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( p, "key_usage" ) == 0 )
432 {
433 while( q != NULL )
434 {
435 if( ( r = strchr( q, ',' ) ) != NULL )
436 *r++ = '\0';
437
438 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200440 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200442 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100443 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100445 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100447 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else
Hanno Becker17c32762017-10-03 14:56:04 +0100453 {
454 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100456 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200457
458 q = r;
459 }
460 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100461 else if( strcmp( p, "ext_key_usage" ) == 0 )
462 {
463 while( q != NULL )
464 {
465 if( ( r = strchr( q, ',' ) ) != NULL )
466 *r++ = '\0';
467
468 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
469 ext_key_usage->next = opt.ext_key_usage;
470 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
471 if( strcmp( q, "serverAuth" ) == 0 )
472 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
473 else if( strcmp( q, "clientAuth" ) == 0 )
474 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
475 else if( strcmp( q, "codeSigning" ) == 0 )
476 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
477 else if( strcmp( q, "emailProtection" ) == 0 )
478 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
479 else if( strcmp( q, "timeStamping" ) == 0 )
480 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
481 else if( strcmp( q, "OCSPSigning" ) == 0 )
482 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
483 else
484 goto usage;
485 opt.ext_key_usage = ext_key_usage;
486
487 q = r;
488 }
489 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 else if( strcmp( p, "ns_cert_type" ) == 0 )
491 {
492 while( q != NULL )
493 {
494 if( ( r = strchr( q, ',' ) ) != NULL )
495 *r++ = '\0';
496
497 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100500 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200501 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200503 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200505 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100506 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200507 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100508 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200509 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100510 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200511 else
Hanno Becker17c32762017-10-03 14:56:04 +0100512 {
513 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200514 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100515 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200516
517 q = r;
518 }
519 }
520 else
521 goto usage;
522 }
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200525
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200526 /*
527 * 0. Seed the PRNG
528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200530 fflush( stdout );
531
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200532 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200533 (const unsigned char *) pers,
534 strlen( pers ) ) ) != 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_ctr_drbg_seed returned %d - %s\n",
538 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +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 Bakker9397dcb2013-09-06 09:55:26 +0200544 // Parse serial to MPI
545 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200547 fflush( stdout );
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100552 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200553 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200554 goto exit;
555 }
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200558
Paul Bakker1014e952013-09-09 13:59:42 +0200559 // Parse issuer certificate if present
560 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200561 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200562 {
563 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200564 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200567 fflush( stdout );
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100572 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200573 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200574 goto exit;
575 }
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200578 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200579 if( ret < 0 )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100582 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200583 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200584 goto exit;
585 }
586
Paul Bakkere2673fb2013-09-09 15:52:07 +0200587 opt.issuer_name = issuer_name;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200593 // Parse certificate request if present
594 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200595 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200596 {
597 /*
598 * 1.0.b. Load the CSR
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200601 fflush( stdout );
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100606 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200607 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200608 goto exit;
609 }
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200612 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200613 if( ret < 0 )
614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100616 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200617 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200618 goto exit;
619 }
620
621 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200622 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200627
628 /*
629 * 1.1. Load the keys
630 */
631 if( !opt.selfsign && !strlen( opt.request_file ) )
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200634 fflush( stdout );
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200637 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200638 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100641 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200642 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200643 goto exit;
644 }
Paul Bakker1014e952013-09-09 13:59:42 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200647 }
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200650 fflush( stdout );
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200653 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200654 if( ret != 0 )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100657 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200658 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200659 goto exit;
660 }
661
662 // Check if key and issuer certificate match
663 //
664 if( strlen( opt.issuer_crt ) )
665 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200666 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200667 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200668 {
Hanno Becker81535d02017-09-13 15:39:59 +0100669 mbedtls_printf( " failed\n ! issuer_key does not match "
670 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200671 goto exit;
672 }
673 }
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200676
677 if( opt.selfsign )
678 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100679 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200680 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200681 }
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
684 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200685
Paul Bakker9397dcb2013-09-06 09:55:26 +0200686 /*
687 * 1.0. Check the names for validity
688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100692 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200693 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200694 goto exit;
695 }
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200698 {
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_issuer_name "
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 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200706 fflush( stdout );
707
Hanno Becker38eff432017-09-22 15:38:20 +0100708 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100709 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200712 if( ret != 0 )
713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100715 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200716 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200717 goto exit;
718 }
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200721 if( ret != 0 )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100724 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200725 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200726 goto exit;
727 }
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200730
Hanno Becker38eff432017-09-22 15:38:20 +0100731 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
732 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200733 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100734 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
735 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200736
Hanno Becker6c13d372017-09-13 12:49:22 +0100737 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
738 opt.max_pathlen );
739 if( ret != 0 )
740 {
741 mbedtls_strerror( ret, buf, 1024 );
742 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200743 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100744 goto exit;
745 }
746
747 mbedtls_printf( " ok\n" );
748 }
Paul Bakker15162a02013-09-06 19:27:21 +0200749
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100750 mbedtls_x509write_crt_set_md_alg( &crt, opt.sig_alg );
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100753 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
754 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200755 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100756 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
757 fflush( stdout );
758
759 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
760 if( ret != 0 )
761 {
762 mbedtls_strerror( ret, buf, 1024 );
763 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100764 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200765 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100766 goto exit;
767 }
768
769 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200770 }
771
Hanno Becker38eff432017-09-22 15:38:20 +0100772 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
773 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200774 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100775 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
776 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200777
Hanno Becker6c13d372017-09-13 12:49:22 +0100778 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
779 if( ret != 0 )
780 {
781 mbedtls_strerror( ret, buf, 1024 );
782 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100783 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200784 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100785 goto exit;
786 }
787
788 mbedtls_printf( " ok\n" );
789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200791
Hanno Becker38eff432017-09-22 15:38:20 +0100792 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
793 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200796 fflush( stdout );
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200799 if( ret != 0 )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100802 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200803 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200804 goto exit;
805 }
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200808 }
809
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100810 if( opt.ext_key_usage )
811 {
812 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
813 fflush( stdout );
814
815 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
816 if( ret != 0 )
817 {
818 mbedtls_strerror( ret, buf, 1024 );
819 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n", -ret, buf );
820 goto exit;
821 }
822
823 mbedtls_printf( " ok\n" );
824 }
825
Hanno Becker38eff432017-09-22 15:38:20 +0100826 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
827 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200830 fflush( stdout );
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200833 if( ret != 0 )
834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker81535d02017-09-13 15:39:59 +0100836 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200837 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200838 goto exit;
839 }
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200842 }
843
Paul Bakker9397dcb2013-09-06 09:55:26 +0200844 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100845 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200848 fflush( stdout );
849
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200850 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_strerror( ret, buf, 1024 );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100854 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200855 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200856 goto exit;
857 }
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200860
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100861 exit_code = MBEDTLS_EXIT_SUCCESS;
862
Paul Bakker9397dcb2013-09-06 09:55:26 +0200863exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100864#if defined(MBEDTLS_X509_CSR_PARSE_C)
865 mbedtls_x509_csr_free( &csr );
866#endif /* MBEDTLS_X509_CSR_PARSE_C */
867 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_x509write_crt_free( &crt );
869 mbedtls_pk_free( &loaded_subject_key );
870 mbedtls_pk_free( &loaded_issuer_key );
871 mbedtls_mpi_free( &serial );
872 mbedtls_ctr_drbg_free( &ctr_drbg );
873 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200874
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200875 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
878 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100879 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */