blob: 0118abd208f837c0a151a31357da13921cac9609 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Simon Butcher203a6932016-10-07 15:00:17 +010033#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
34 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
36 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
37 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038int main( void )
39{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 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 +020041 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
43 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020044 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020045}
46#else
47
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/x509_crt.h"
49#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010050#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010053#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010060#define SET_OID(x, oid) \
61 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char*)oid; } while( 0 )
62
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
Nicholas Wilson99a96b12015-09-10 18:28:01 +010087#define DFL_SIG_ALG MBEDTLS_MD_SHA256
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 );
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100263 memset( buf, 0, sizeof(buf) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200264
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 {
Dave Rodgman64937852022-08-15 14:12:25 +0100448 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
449
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100450 while( q != NULL )
451 {
452 if( ( r = strchr( q, ',' ) ) != NULL )
453 *r++ = '\0';
454
455 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100456 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
457 if( strcmp( q, "serverAuth" ) == 0 )
458 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
459 else if( strcmp( q, "clientAuth" ) == 0 )
460 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
461 else if( strcmp( q, "codeSigning" ) == 0 )
462 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
463 else if( strcmp( q, "emailProtection" ) == 0 )
464 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
465 else if( strcmp( q, "timeStamping" ) == 0 )
466 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
467 else if( strcmp( q, "OCSPSigning" ) == 0 )
468 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
469 else
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100470 {
471 mbedtls_printf( "Invalid argument for option %s\n", p );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100472 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100473 }
Dave Rodgman64937852022-08-15 14:12:25 +0100474
475 *tail = ext_key_usage;
476 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100477
478 q = r;
479 }
480 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200481 else if( strcmp( p, "ns_cert_type" ) == 0 )
482 {
483 while( q != NULL )
484 {
485 if( ( r = strchr( q, ',' ) ) != NULL )
486 *r++ = '\0';
487
488 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100491 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200492 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200494 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200496 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100497 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200498 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100499 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200500 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100501 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200502 else
Hanno Becker17c32762017-10-03 14:56:04 +0100503 {
504 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200505 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100506 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200507
508 q = r;
509 }
510 }
511 else
512 goto usage;
513 }
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200516
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200517 /*
518 * 0. Seed the PRNG
519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200521 fflush( stdout );
522
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200523 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200524 (const unsigned char *) pers,
525 strlen( pers ) ) ) != 0 )
526 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100527 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100528 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
529 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200530 goto exit;
531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200534
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 // Parse serial to MPI
536 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200538 fflush( stdout );
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200541 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100542 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100543 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200544 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200545 goto exit;
546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200549
Paul Bakker1014e952013-09-09 13:59:42 +0200550 // Parse issuer certificate if present
551 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200552 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200553 {
554 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200555 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200558 fflush( stdout );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200561 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100562 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100563 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200564 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200565 goto exit;
566 }
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200569 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200570 if( ret < 0 )
571 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100572 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100573 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200574 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200575 goto exit;
576 }
577
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 opt.issuer_name = issuer_name;
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200581 }
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200584 // Parse certificate request if present
585 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200586 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200587 {
588 /*
589 * 1.0.b. Load the CSR
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200592 fflush( stdout );
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200595 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100596 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100597 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200598 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200599 goto exit;
600 }
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200603 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200604 if( ret < 0 )
605 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100606 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100607 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200608 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200609 goto exit;
610 }
611
612 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200613 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200618
619 /*
620 * 1.1. Load the keys
621 */
622 if( !opt.selfsign && !strlen( opt.request_file ) )
623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200625 fflush( stdout );
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200628 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200629 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200630 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100631 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100632 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200633 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200634 goto exit;
635 }
Paul Bakker1014e952013-09-09 13:59:42 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200638 }
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200641 fflush( stdout );
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200644 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200645 if( ret != 0 )
646 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100647 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100648 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200649 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200650 goto exit;
651 }
652
653 // Check if key and issuer certificate match
654 //
655 if( strlen( opt.issuer_crt ) )
656 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200657 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200658 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200659 {
Hanno Becker81535d02017-09-13 15:39:59 +0100660 mbedtls_printf( " failed\n ! issuer_key does not match "
661 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200662 goto exit;
663 }
664 }
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200667
668 if( opt.selfsign )
669 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100670 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200671 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200672 }
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
675 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200676
Paul Bakker9397dcb2013-09-06 09:55:26 +0200677 /*
678 * 1.0. Check the names for validity
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200681 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100682 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100683 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200684 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200685 goto exit;
686 }
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200689 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100690 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100691 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200692 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200693 goto exit;
694 }
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200697 fflush( stdout );
698
Hanno Becker38eff432017-09-22 15:38:20 +0100699 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100700 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200703 if( ret != 0 )
704 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100705 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100706 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200707 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200708 goto exit;
709 }
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200712 if( ret != 0 )
713 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100714 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100715 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
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 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200721
Hanno Becker38eff432017-09-22 15:38:20 +0100722 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
723 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200724 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100725 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
726 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200727
Hanno Becker6c13d372017-09-13 12:49:22 +0100728 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
729 opt.max_pathlen );
730 if( ret != 0 )
731 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100732 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100733 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200734 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100735 goto exit;
736 }
737
738 mbedtls_printf( " ok\n" );
739 }
Paul Bakker15162a02013-09-06 19:27:21 +0200740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100742 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
743 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200744 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100745 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
746 fflush( stdout );
747
748 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
749 if( ret != 0 )
750 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100751 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100752 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100753 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200754 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100755 goto exit;
756 }
757
758 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200759 }
760
Hanno Becker38eff432017-09-22 15:38:20 +0100761 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
762 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200763 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100764 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
765 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200766
Hanno Becker6c13d372017-09-13 12:49:22 +0100767 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
768 if( ret != 0 )
769 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100770 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100771 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100772 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200773 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100774 goto exit;
775 }
776
777 mbedtls_printf( " ok\n" );
778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200780
Hanno Becker38eff432017-09-22 15:38:20 +0100781 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
782 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200785 fflush( stdout );
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200788 if( ret != 0 )
789 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100790 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100791 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200792 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200793 goto exit;
794 }
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200797 }
798
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100799 if( opt.ext_key_usage )
800 {
801 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
802 fflush( stdout );
803
804 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
805 if( ret != 0 )
806 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100807 mbedtls_strerror( ret, buf, sizeof(buf) );
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100808 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n", (unsigned int) -ret, buf );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100809 goto exit;
810 }
811
812 mbedtls_printf( " ok\n" );
813 }
814
Hanno Becker38eff432017-09-22 15:38:20 +0100815 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
816 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200819 fflush( stdout );
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200822 if( ret != 0 )
823 {
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100824 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100825 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200826 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200827 goto exit;
828 }
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200831 }
832
Paul Bakker9397dcb2013-09-06 09:55:26 +0200833 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100834 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200837 fflush( stdout );
838
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200839 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200841 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100842 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100843 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200844 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200845 goto exit;
846 }
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200849
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100850 exit_code = MBEDTLS_EXIT_SUCCESS;
851
Paul Bakker9397dcb2013-09-06 09:55:26 +0200852exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100853#if defined(MBEDTLS_X509_CSR_PARSE_C)
854 mbedtls_x509_csr_free( &csr );
855#endif /* MBEDTLS_X509_CSR_PARSE_C */
856 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_x509write_crt_free( &crt );
858 mbedtls_pk_free( &loaded_subject_key );
859 mbedtls_pk_free( &loaded_issuer_key );
860 mbedtls_mpi_free( &serial );
861 mbedtls_ctr_drbg_free( &ctr_drbg );
862 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200863
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200864 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
867 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100868 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */