blob: 1f4b1a09a65b15986c1aa01af567f747e51e832e [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
Dave Rodgman66e05502022-10-27 16:29:38 +010072#define FORMAT_PEM 0
73#define FORMAT_DER 1
74
Paul Bakker1014e952013-09-09 13:59:42 +020075#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020076#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020077#define DFL_SUBJECT_KEY "subject.key"
78#define DFL_ISSUER_KEY "ca.key"
79#define DFL_SUBJECT_PWD ""
80#define DFL_ISSUER_PWD ""
81#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000082#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
83#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020084#define DFL_NOT_BEFORE "20010101000000"
85#define DFL_NOT_AFTER "20301231235959"
86#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020087#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020088#define DFL_IS_CA 0
89#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010090#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020091#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010092#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020093#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010094#define DFL_VERSION 3
95#define DFL_AUTH_IDENT 1
96#define DFL_SUBJ_IDENT 1
97#define DFL_CONSTRAINTS 1
98#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010099#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +0200100
Rich Evans18b78c72015-02-11 14:06:19 +0000101#define USAGE \
102 "\n usage: cert_write param=<>...\n" \
103 "\n acceptable parameters:\n" \
104 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " subject_key=%%s default: subject.key\n" \
106 " subject_pwd=%%s default: (empty)\n" \
107 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000108 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100109 " issuer_crt=%%s default: (empty)\n" \
110 " If issuer_crt is specified, issuer_name is\n" \
111 " ignored!\n" \
112 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000113 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100114 " selfsign=%%d default: 0 (false)\n" \
115 " If selfsign is enabled, issuer_name and\n" \
116 " issuer_key are required (issuer_crt and\n" \
117 " subject_* are ignored\n" \
118 " issuer_key=%%s default: ca.key\n" \
119 " issuer_pwd=%%s default: (empty)\n" \
120 " output_file=%%s default: cert.crt\n" \
121 " serial=%%s default: 1\n" \
122 " not_before=%%s default: 20010101000000\n"\
123 " not_after=%%s default: 20301231235959\n"\
124 " is_ca=%%d default: 0 (disabled)\n" \
125 " max_pathlen=%%d default: -1 (none)\n" \
126 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200127 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200128 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200129 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100130 " version=%%d default: 3\n" \
131 " Possible values: 1, 2, 3\n"\
132 " subject_identifier=%%s default: 1\n" \
133 " Possible values: 0, 1\n" \
134 " (Considered for v3 only)\n"\
135 " authority_identifier=%%s default: 1\n" \
136 " Possible values: 0, 1\n" \
137 " (Considered for v3 only)\n"\
138 " basic_constraints=%%d default: 1\n" \
139 " Possible values: 0, 1\n" \
140 " (Considered for v3 only)\n"\
141 " key_usage=%%s default: (empty)\n" \
142 " Comma-separated-list of values:\n" \
143 " digital_signature\n" \
144 " non_repudiation\n" \
145 " key_encipherment\n" \
146 " data_encipherment\n" \
147 " key_agreement\n" \
148 " key_cert_sign\n" \
149 " crl_sign\n" \
150 " (Considered for v3 only)\n"\
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100151 " ext_key_usage=%%s default: (empty)\n" \
152 " Comma-separated-list of values:\n" \
153 " serverAuth\n" \
154 " clientAuth\n" \
155 " codeSigning\n" \
156 " emailProtection\n" \
157 " timeStamping\n" \
158 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100159 " ns_cert_type=%%s default: (empty)\n" \
160 " Comma-separated-list of values:\n" \
161 " ssl_client\n" \
162 " ssl_server\n" \
163 " email\n" \
164 " object_signing\n" \
165 " ssl_ca\n" \
166 " email_ca\n" \
167 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100168 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000169 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000170
Simon Butcher63cb97e2018-12-06 17:43:31 +0000171
Paul Bakker9397dcb2013-09-06 09:55:26 +0200172/*
173 * global options
174 */
175struct options
176{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100177 const char *issuer_crt; /* filename of the issuer certificate */
178 const char *request_file; /* filename of the certificate request */
179 const char *subject_key; /* filename of the subject key file */
180 const char *issuer_key; /* filename of the issuer key file */
181 const char *subject_pwd; /* password for the subject key file */
182 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100183 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100184 const char *subject_name; /* subject name for certificate */
185 const char *issuer_name; /* issuer name for certificate */
186 const char *not_before; /* validity period not before */
187 const char *not_after; /* validity period not after */
188 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200189 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200190 int is_ca; /* is a CA certificate */
191 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100192 int authority_identifier; /* add authority identifier to CRT */
193 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100194 int basic_constraints; /* add basic constraints ext to CRT */
195 int version; /* CRT version */
196 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100198 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100200 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201} opt;
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200204 int (*f_rng)(void *, unsigned char *, size_t),
205 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206{
207 int ret;
208 FILE *f;
209 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100210 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211 size_t len = 0;
212
213 memset( output_buf, 0, 4096 );
Dave Rodgman66e05502022-10-27 16:29:38 +0100214 if ( opt.format == FORMAT_DER )
215 {
216 ret = mbedtls_x509write_crt_der( crt, output_buf, 4096,
217 f_rng, p_rng );
218 if( ret < 0 )
219 return( ret );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200220
Dave Rodgman66e05502022-10-27 16:29:38 +0100221 len = ret;
222 output_start = output_buf + 4096 - len;
223 } else {
224 ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
225 f_rng, p_rng );
226 if( ret < 0 )
227 return( ret );
228
229 len = strlen( (char *) output_buf );
230 output_start = output_buf;
231 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200232
233 if( ( f = fopen( output_file, "w" ) ) == NULL )
234 return( -1 );
235
Dave Rodgman66e05502022-10-27 16:29:38 +0100236 if( fwrite( output_start, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200237 {
238 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200240 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200241
Paul Bakker0c226102014-04-17 16:02:36 +0200242 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243
244 return( 0 );
245}
246
Paul Bakker9397dcb2013-09-06 09:55:26 +0200247int main( int argc, char *argv[] )
248{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100249 int ret = 1;
250 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_x509_crt issuer_crt;
252 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
253 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200254 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200255 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200256 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100257 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200260 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_x509write_cert crt;
264 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100265 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_entropy_context entropy;
267 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200268 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269
270 /*
271 * Set to sane values
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_pk_init( &loaded_issuer_key );
275 mbedtls_pk_init( &loaded_subject_key );
276 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200277 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100278 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_X509_CSR_PARSE_C)
280 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_x509_crt_init( &issuer_crt );
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100283 memset( buf, 0, sizeof(buf) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200284
285 if( argc == 0 )
286 {
287 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289 goto exit;
290 }
291
Paul Bakker1014e952013-09-09 13:59:42 +0200292 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200293 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294 opt.subject_key = DFL_SUBJECT_KEY;
295 opt.issuer_key = DFL_ISSUER_KEY;
296 opt.subject_pwd = DFL_SUBJECT_PWD;
297 opt.issuer_pwd = DFL_ISSUER_PWD;
298 opt.output_file = DFL_OUTPUT_FILENAME;
299 opt.subject_name = DFL_SUBJECT_NAME;
300 opt.issuer_name = DFL_ISSUER_NAME;
301 opt.not_before = DFL_NOT_BEFORE;
302 opt.not_after = DFL_NOT_AFTER;
303 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200304 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200305 opt.is_ca = DFL_IS_CA;
306 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100308 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200309 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100310 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100311 opt.md = DFL_DIGEST;
312 opt.subject_identifier = DFL_SUBJ_IDENT;
313 opt.authority_identifier = DFL_AUTH_IDENT;
314 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100315 opt.format = DFL_FORMAT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316
317 for( i = 1; i < argc; i++ )
318 {
319
320 p = argv[i];
321 if( ( q = strchr( p, '=' ) ) == NULL )
322 goto usage;
323 *q++ = '\0';
324
Paul Bakkere2673fb2013-09-09 15:52:07 +0200325 if( strcmp( p, "request_file" ) == 0 )
326 opt.request_file = q;
327 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200328 opt.subject_key = q;
329 else if( strcmp( p, "issuer_key" ) == 0 )
330 opt.issuer_key = q;
331 else if( strcmp( p, "subject_pwd" ) == 0 )
332 opt.subject_pwd = q;
333 else if( strcmp( p, "issuer_pwd" ) == 0 )
334 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200335 else if( strcmp( p, "issuer_crt" ) == 0 )
336 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200337 else if( strcmp( p, "output_file" ) == 0 )
338 opt.output_file = q;
339 else if( strcmp( p, "subject_name" ) == 0 )
340 {
341 opt.subject_name = q;
342 }
343 else if( strcmp( p, "issuer_name" ) == 0 )
344 {
345 opt.issuer_name = q;
346 }
347 else if( strcmp( p, "not_before" ) == 0 )
348 {
349 opt.not_before = q;
350 }
351 else if( strcmp( p, "not_after" ) == 0 )
352 {
353 opt.not_after = q;
354 }
355 else if( strcmp( p, "serial" ) == 0 )
356 {
357 opt.serial = q;
358 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100359 else if( strcmp( p, "authority_identifier" ) == 0 )
360 {
361 opt.authority_identifier = atoi( q );
362 if( opt.authority_identifier != 0 &&
363 opt.authority_identifier != 1 )
364 {
Hanno Becker17c32762017-10-03 14:56:04 +0100365 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100366 goto usage;
367 }
368 }
369 else if( strcmp( p, "subject_identifier" ) == 0 )
370 {
371 opt.subject_identifier = atoi( q );
372 if( opt.subject_identifier != 0 &&
373 opt.subject_identifier != 1 )
374 {
Hanno Becker17c32762017-10-03 14:56:04 +0100375 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100376 goto usage;
377 }
378 }
379 else if( strcmp( p, "basic_constraints" ) == 0 )
380 {
381 opt.basic_constraints = atoi( q );
382 if( opt.basic_constraints != 0 &&
383 opt.basic_constraints != 1 )
384 {
Hanno Becker17c32762017-10-03 14:56:04 +0100385 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100386 goto usage;
387 }
388 }
389 else if( strcmp( p, "md" ) == 0 )
390 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200391 const mbedtls_md_info_t *md_info =
392 mbedtls_md_info_from_string( q );
393 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100394 {
395 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100396 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100397 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200398 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100399 }
400 else if( strcmp( p, "version" ) == 0 )
401 {
402 opt.version = atoi( q );
403 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100404 {
405 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100406 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100407 }
Hanno Becker38eff432017-09-22 15:38:20 +0100408 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100409 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200410 else if( strcmp( p, "selfsign" ) == 0 )
411 {
412 opt.selfsign = atoi( q );
413 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100414 {
415 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200416 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100417 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200418 }
Paul Bakker15162a02013-09-06 19:27:21 +0200419 else if( strcmp( p, "is_ca" ) == 0 )
420 {
421 opt.is_ca = atoi( q );
422 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100423 {
424 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200425 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100426 }
Paul Bakker15162a02013-09-06 19:27:21 +0200427 }
428 else if( strcmp( p, "max_pathlen" ) == 0 )
429 {
430 opt.max_pathlen = atoi( q );
431 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100432 {
433 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200434 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100435 }
Paul Bakker15162a02013-09-06 19:27:21 +0200436 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else if( strcmp( p, "key_usage" ) == 0 )
438 {
439 while( q != NULL )
440 {
441 if( ( r = strchr( q, ',' ) ) != NULL )
442 *r++ = '\0';
443
444 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200446 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200448 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100449 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200450 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100451 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200452 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100453 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 else
Hanno Becker17c32762017-10-03 14:56:04 +0100459 {
460 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100462 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200463
464 q = r;
465 }
466 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100467 else if( strcmp( p, "ext_key_usage" ) == 0 )
468 {
Dave Rodgman64937852022-08-15 14:12:25 +0100469 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
470
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100471 while( q != NULL )
472 {
473 if( ( r = strchr( q, ',' ) ) != NULL )
474 *r++ = '\0';
475
476 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100477 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
478 if( strcmp( q, "serverAuth" ) == 0 )
479 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
480 else if( strcmp( q, "clientAuth" ) == 0 )
481 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
482 else if( strcmp( q, "codeSigning" ) == 0 )
483 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
484 else if( strcmp( q, "emailProtection" ) == 0 )
485 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
486 else if( strcmp( q, "timeStamping" ) == 0 )
487 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
488 else if( strcmp( q, "OCSPSigning" ) == 0 )
489 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
490 else
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100491 {
492 mbedtls_printf( "Invalid argument for option %s\n", p );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100493 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100494 }
Dave Rodgman64937852022-08-15 14:12:25 +0100495
496 *tail = ext_key_usage;
497 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100498
499 q = r;
500 }
501 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200502 else if( strcmp( p, "ns_cert_type" ) == 0 )
503 {
504 while( q != NULL )
505 {
506 if( ( r = strchr( q, ',' ) ) != NULL )
507 *r++ = '\0';
508
509 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200511 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100512 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200513 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200515 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200517 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100518 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200519 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100520 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200521 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100522 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200523 else
Hanno Becker17c32762017-10-03 14:56:04 +0100524 {
525 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100527 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200528
529 q = r;
530 }
531 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100532 else if( strcmp( p, "format" ) == 0 )
533 {
534 if ( strcmp(q, "der" ) == 0 ) opt.format = FORMAT_DER;
535 else if ( strcmp(q, "pem" ) == 0 ) opt.format = FORMAT_PEM;
536 else
537 {
538 mbedtls_printf( "Invalid argument for option %s\n", p );
539 goto usage;
540 }
541 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200542 else
543 goto usage;
544 }
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200547
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200548 /*
549 * 0. Seed the PRNG
550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200552 fflush( stdout );
553
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200554 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200555 (const unsigned char *) pers,
556 strlen( pers ) ) ) != 0 )
557 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100558 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100559 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
560 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200561 goto exit;
562 }
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200565
Paul Bakker9397dcb2013-09-06 09:55:26 +0200566 // Parse serial to MPI
567 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200569 fflush( stdout );
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200572 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100573 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100574 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200575 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200576 goto exit;
577 }
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200580
Paul Bakker1014e952013-09-09 13:59:42 +0200581 // Parse issuer certificate if present
582 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200583 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200584 {
585 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200589 fflush( stdout );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200592 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100593 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100594 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200595 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200596 goto exit;
597 }
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200600 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200601 if( ret < 0 )
602 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100603 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100604 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200605 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200606 goto exit;
607 }
608
Paul Bakkere2673fb2013-09-09 15:52:07 +0200609 opt.issuer_name = issuer_name;
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200612 }
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200615 // Parse certificate request if present
616 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200617 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200618 {
619 /*
620 * 1.0.b. Load the CSR
621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200623 fflush( stdout );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200626 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100627 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100628 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200629 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200630 goto exit;
631 }
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200634 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200635 if( ret < 0 )
636 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100637 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100638 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200639 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200640 goto exit;
641 }
642
643 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200644 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200649
650 /*
651 * 1.1. Load the keys
652 */
653 if( !opt.selfsign && !strlen( opt.request_file ) )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200656 fflush( stdout );
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200659 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200660 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200661 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100662 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100663 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200664 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200665 goto exit;
666 }
Paul Bakker1014e952013-09-09 13:59:42 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200672 fflush( stdout );
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200675 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200676 if( ret != 0 )
677 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100678 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100679 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200680 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200681 goto exit;
682 }
683
684 // Check if key and issuer certificate match
685 //
686 if( strlen( opt.issuer_crt ) )
687 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200688 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200689 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200690 {
Hanno Becker81535d02017-09-13 15:39:59 +0100691 mbedtls_printf( " failed\n ! issuer_key does not match "
692 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200693 goto exit;
694 }
695 }
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200698
699 if( opt.selfsign )
700 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100701 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200702 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200703 }
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
706 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200707
Paul Bakker9397dcb2013-09-06 09:55:26 +0200708 /*
709 * 1.0. Check the names for validity
710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200712 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100713 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100714 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200715 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200716 goto exit;
717 }
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200720 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100721 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100722 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200723 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200724 goto exit;
725 }
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200728 fflush( stdout );
729
Hanno Becker38eff432017-09-22 15:38:20 +0100730 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100731 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200734 if( ret != 0 )
735 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100736 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100737 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200738 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200739 goto exit;
740 }
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200743 if( ret != 0 )
744 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100745 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100746 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200747 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200748 goto exit;
749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200752
Hanno Becker38eff432017-09-22 15:38:20 +0100753 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
754 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200755 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100756 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
757 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200758
Hanno Becker6c13d372017-09-13 12:49:22 +0100759 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
760 opt.max_pathlen );
761 if( ret != 0 )
762 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100763 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100764 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200765 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100766 goto exit;
767 }
768
769 mbedtls_printf( " ok\n" );
770 }
Paul Bakker15162a02013-09-06 19:27:21 +0200771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100773 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
774 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200775 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100776 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
777 fflush( stdout );
778
779 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
780 if( ret != 0 )
781 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100782 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100783 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100784 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200785 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100786 goto exit;
787 }
788
789 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200790 }
791
Hanno Becker38eff432017-09-22 15:38:20 +0100792 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
793 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200794 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100795 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
796 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200797
Hanno Becker6c13d372017-09-13 12:49:22 +0100798 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
799 if( ret != 0 )
800 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100801 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100802 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100803 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200804 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100805 goto exit;
806 }
807
808 mbedtls_printf( " ok\n" );
809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200811
Hanno Becker38eff432017-09-22 15:38:20 +0100812 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
813 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200816 fflush( stdout );
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200819 if( ret != 0 )
820 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100821 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100822 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200823 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200824 goto exit;
825 }
826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200828 }
829
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100830 if( opt.ext_key_usage )
831 {
832 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
833 fflush( stdout );
834
835 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
836 if( ret != 0 )
837 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100838 mbedtls_strerror( ret, buf, sizeof(buf) );
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100839 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 +0100840 goto exit;
841 }
842
843 mbedtls_printf( " ok\n" );
844 }
845
Hanno Becker38eff432017-09-22 15:38:20 +0100846 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
847 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200850 fflush( stdout );
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200853 if( ret != 0 )
854 {
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100855 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100856 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200857 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200858 goto exit;
859 }
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200862 }
863
Paul Bakker9397dcb2013-09-06 09:55:26 +0200864 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100865 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200868 fflush( stdout );
869
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200870 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200872 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100873 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100874 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200875 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200876 goto exit;
877 }
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200880
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100881 exit_code = MBEDTLS_EXIT_SUCCESS;
882
Paul Bakker9397dcb2013-09-06 09:55:26 +0200883exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100884#if defined(MBEDTLS_X509_CSR_PARSE_C)
885 mbedtls_x509_csr_free( &csr );
886#endif /* MBEDTLS_X509_CSR_PARSE_C */
887 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_x509write_crt_free( &crt );
889 mbedtls_pk_free( &loaded_subject_key );
890 mbedtls_pk_free( &loaded_issuer_key );
891 mbedtls_mpi_free( &serial );
892 mbedtls_ctr_drbg_free( &ctr_drbg );
893 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200894
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200895 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
898 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100899 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */