blob: 0f0703f4b2bb2fd1ce63d720c28cdad6e47c2d63 [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"\
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100137 " sig_alg=%%s default: SHA-256\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100138 " key_usage=%%s default: (empty)\n" \
139 " Comma-separated-list of values:\n" \
140 " digital_signature\n" \
141 " non_repudiation\n" \
142 " key_encipherment\n" \
143 " data_encipherment\n" \
144 " key_agreement\n" \
145 " key_cert_sign\n" \
146 " crl_sign\n" \
147 " (Considered for v3 only)\n"\
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100148 " ext_key_usage=%%s default: (empty)\n" \
149 " Comma-separated-list of values:\n" \
150 " serverAuth\n" \
151 " clientAuth\n" \
152 " codeSigning\n" \
153 " emailProtection\n" \
154 " timeStamping\n" \
155 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100156 " ns_cert_type=%%s default: (empty)\n" \
157 " Comma-separated-list of values:\n" \
158 " ssl_client\n" \
159 " ssl_server\n" \
160 " email\n" \
161 " object_signing\n" \
162 " ssl_ca\n" \
163 " email_ca\n" \
164 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000165 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000166
Simon Butcher63cb97e2018-12-06 17:43:31 +0000167
Paul Bakker9397dcb2013-09-06 09:55:26 +0200168/*
169 * global options
170 */
171struct options
172{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100173 const char *issuer_crt; /* filename of the issuer certificate */
174 const char *request_file; /* filename of the certificate request */
175 const char *subject_key; /* filename of the subject key file */
176 const char *issuer_key; /* filename of the issuer key file */
177 const char *subject_pwd; /* password for the subject key file */
178 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100179 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100180 const char *subject_name; /* subject name for certificate */
181 const char *issuer_name; /* issuer name for certificate */
182 const char *not_before; /* validity period not before */
183 const char *not_after; /* validity period not after */
184 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200185 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200186 int is_ca; /* is a CA certificate */
187 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100188 int authority_identifier; /* add authority identifier to CRT */
189 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100190 int basic_constraints; /* add basic constraints ext to CRT */
191 int version; /* CRT version */
192 mbedtls_md_type_t md; /* Hash used for signing */
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100193 mbedtls_md_type_t sig_alg; /* MD to use generating signature */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100195 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196 unsigned char ns_cert_type; /* NS cert type */
197} opt;
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200200 int (*f_rng)(void *, unsigned char *, size_t),
201 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200202{
203 int ret;
204 FILE *f;
205 unsigned char output_buf[4096];
206 size_t len = 0;
207
208 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100209 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
210 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211 return( ret );
212
213 len = strlen( (char *) output_buf );
214
215 if( ( f = fopen( output_file, "w" ) ) == NULL )
216 return( -1 );
217
218 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200219 {
220 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200222 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
Paul Bakker0c226102014-04-17 16:02:36 +0200224 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225
226 return( 0 );
227}
228
Paul Bakker9397dcb2013-09-06 09:55:26 +0200229int main( int argc, char *argv[] )
230{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100231 int ret = 1;
232 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_x509_crt issuer_crt;
234 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
235 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200236 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200237 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200238 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100239 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200240 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200242 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_x509write_cert crt;
246 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100247 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_entropy_context entropy;
249 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200250 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251
252 /*
253 * Set to sane values
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_pk_init( &loaded_issuer_key );
257 mbedtls_pk_init( &loaded_subject_key );
258 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200259 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100260 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_X509_CSR_PARSE_C)
262 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_x509_crt_init( &issuer_crt );
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100265 memset( buf, 0, sizeof(buf) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266
267 if( argc == 0 )
268 {
269 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271 goto exit;
272 }
273
Paul Bakker1014e952013-09-09 13:59:42 +0200274 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200275 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276 opt.subject_key = DFL_SUBJECT_KEY;
277 opt.issuer_key = DFL_ISSUER_KEY;
278 opt.subject_pwd = DFL_SUBJECT_PWD;
279 opt.issuer_pwd = DFL_ISSUER_PWD;
280 opt.output_file = DFL_OUTPUT_FILENAME;
281 opt.subject_name = DFL_SUBJECT_NAME;
282 opt.issuer_name = DFL_ISSUER_NAME;
283 opt.not_before = DFL_NOT_BEFORE;
284 opt.not_after = DFL_NOT_AFTER;
285 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200286 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200287 opt.is_ca = DFL_IS_CA;
288 opt.max_pathlen = DFL_MAX_PATHLEN;
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100289 opt.sig_alg = DFL_SIG_ALG;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200290 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100291 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200292 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100293 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100294 opt.md = DFL_DIGEST;
295 opt.subject_identifier = DFL_SUBJ_IDENT;
296 opt.authority_identifier = DFL_AUTH_IDENT;
297 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200298
299 for( i = 1; i < argc; i++ )
300 {
301
302 p = argv[i];
303 if( ( q = strchr( p, '=' ) ) == NULL )
304 goto usage;
305 *q++ = '\0';
306
Paul Bakkere2673fb2013-09-09 15:52:07 +0200307 if( strcmp( p, "request_file" ) == 0 )
308 opt.request_file = q;
309 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200310 opt.subject_key = q;
311 else if( strcmp( p, "issuer_key" ) == 0 )
312 opt.issuer_key = q;
313 else if( strcmp( p, "subject_pwd" ) == 0 )
314 opt.subject_pwd = q;
315 else if( strcmp( p, "issuer_pwd" ) == 0 )
316 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200317 else if( strcmp( p, "issuer_crt" ) == 0 )
318 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200319 else if( strcmp( p, "output_file" ) == 0 )
320 opt.output_file = q;
321 else if( strcmp( p, "subject_name" ) == 0 )
322 {
323 opt.subject_name = q;
324 }
325 else if( strcmp( p, "issuer_name" ) == 0 )
326 {
327 opt.issuer_name = q;
328 }
329 else if( strcmp( p, "not_before" ) == 0 )
330 {
331 opt.not_before = q;
332 }
333 else if( strcmp( p, "not_after" ) == 0 )
334 {
335 opt.not_after = q;
336 }
337 else if( strcmp( p, "serial" ) == 0 )
338 {
339 opt.serial = q;
340 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100341 else if( strcmp( p, "authority_identifier" ) == 0 )
342 {
343 opt.authority_identifier = atoi( q );
344 if( opt.authority_identifier != 0 &&
345 opt.authority_identifier != 1 )
346 {
Hanno Becker17c32762017-10-03 14:56:04 +0100347 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100348 goto usage;
349 }
350 }
351 else if( strcmp( p, "subject_identifier" ) == 0 )
352 {
353 opt.subject_identifier = atoi( q );
354 if( opt.subject_identifier != 0 &&
355 opt.subject_identifier != 1 )
356 {
Hanno Becker17c32762017-10-03 14:56:04 +0100357 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100358 goto usage;
359 }
360 }
361 else if( strcmp( p, "basic_constraints" ) == 0 )
362 {
363 opt.basic_constraints = atoi( q );
364 if( opt.basic_constraints != 0 &&
365 opt.basic_constraints != 1 )
366 {
Hanno Becker17c32762017-10-03 14:56:04 +0100367 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100368 goto usage;
369 }
370 }
371 else if( strcmp( p, "md" ) == 0 )
372 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200373 const mbedtls_md_info_t *md_info =
374 mbedtls_md_info_from_string( q );
375 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100376 {
377 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100378 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100379 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200380 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100381 }
382 else if( strcmp( p, "version" ) == 0 )
383 {
384 opt.version = atoi( q );
385 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100386 {
387 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100388 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100389 }
Hanno Becker38eff432017-09-22 15:38:20 +0100390 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100391 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200392 else if( strcmp( p, "selfsign" ) == 0 )
393 {
394 opt.selfsign = atoi( q );
395 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100396 {
397 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200398 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100399 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200400 }
Paul Bakker15162a02013-09-06 19:27:21 +0200401 else if( strcmp( p, "is_ca" ) == 0 )
402 {
403 opt.is_ca = atoi( q );
404 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100405 {
406 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200407 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100408 }
Paul Bakker15162a02013-09-06 19:27:21 +0200409 }
410 else if( strcmp( p, "max_pathlen" ) == 0 )
411 {
412 opt.max_pathlen = atoi( q );
413 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100414 {
415 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200416 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100417 }
Paul Bakker15162a02013-09-06 19:27:21 +0200418 }
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100419 else if( strcmp( p, "sig_alg") == 0 )
420 {
421 if( strcmp( q, "SHA-1" ) == 0 )
422 opt.sig_alg = MBEDTLS_MD_SHA1;
423 else if( strcmp( q, "SHA-256" ) == 0 )
424 opt.sig_alg = MBEDTLS_MD_SHA256;
425 else if( strcmp( q, "MD5" ) == 0 )
426 opt.sig_alg = MBEDTLS_MD_MD5;
427 else
428 goto usage;
429 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200430 else if( strcmp( p, "key_usage" ) == 0 )
431 {
432 while( q != NULL )
433 {
434 if( ( r = strchr( q, ',' ) ) != NULL )
435 *r++ = '\0';
436
437 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200439 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200441 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100442 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200443 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100444 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200445 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100446 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200447 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200449 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200451 else
Hanno Becker17c32762017-10-03 14:56:04 +0100452 {
453 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200454 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100455 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200456
457 q = r;
458 }
459 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100460 else if( strcmp( p, "ext_key_usage" ) == 0 )
461 {
462 while( q != NULL )
463 {
464 if( ( r = strchr( q, ',' ) ) != NULL )
465 *r++ = '\0';
466
467 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
468 ext_key_usage->next = opt.ext_key_usage;
469 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
470 if( strcmp( q, "serverAuth" ) == 0 )
471 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
472 else if( strcmp( q, "clientAuth" ) == 0 )
473 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
474 else if( strcmp( q, "codeSigning" ) == 0 )
475 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
476 else if( strcmp( q, "emailProtection" ) == 0 )
477 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
478 else if( strcmp( q, "timeStamping" ) == 0 )
479 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
480 else if( strcmp( q, "OCSPSigning" ) == 0 )
481 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
482 else
483 goto usage;
484 opt.ext_key_usage = ext_key_usage;
485
486 q = r;
487 }
488 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489 else if( strcmp( p, "ns_cert_type" ) == 0 )
490 {
491 while( q != NULL )
492 {
493 if( ( r = strchr( q, ',' ) ) != NULL )
494 *r++ = '\0';
495
496 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200498 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100499 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200500 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200502 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200504 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100505 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200506 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100507 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200508 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100509 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200510 else
Hanno Becker17c32762017-10-03 14:56:04 +0100511 {
512 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200513 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100514 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200515
516 q = r;
517 }
518 }
519 else
520 goto usage;
521 }
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200524
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200525 /*
526 * 0. Seed the PRNG
527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200529 fflush( stdout );
530
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200531 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200532 (const unsigned char *) pers,
533 strlen( pers ) ) ) != 0 )
534 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100535 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100536 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
537 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200538 goto exit;
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200542
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 // Parse serial to MPI
544 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200546 fflush( stdout );
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200549 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100550 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100551 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200552 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200553 goto exit;
554 }
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200557
Paul Bakker1014e952013-09-09 13:59:42 +0200558 // Parse issuer certificate if present
559 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200560 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200561 {
562 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200563 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200566 fflush( stdout );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200569 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100570 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100571 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200572 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200573 goto exit;
574 }
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200577 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200578 if( ret < 0 )
579 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100580 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100581 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200582 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200583 goto exit;
584 }
585
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 opt.issuer_name = issuer_name;
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589 }
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200592 // Parse certificate request if present
593 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200594 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200595 {
596 /*
597 * 1.0.b. Load the CSR
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200600 fflush( stdout );
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200603 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100604 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100605 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200606 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200607 goto exit;
608 }
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200611 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200612 if( ret < 0 )
613 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100614 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100615 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200616 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200617 goto exit;
618 }
619
620 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200621 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200626
627 /*
628 * 1.1. Load the keys
629 */
630 if( !opt.selfsign && !strlen( opt.request_file ) )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200633 fflush( stdout );
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200636 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200637 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200638 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100639 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100640 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200641 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200642 goto exit;
643 }
Paul Bakker1014e952013-09-09 13:59:42 +0200644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200649 fflush( stdout );
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200652 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200653 if( ret != 0 )
654 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100655 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100656 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200657 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200658 goto exit;
659 }
660
661 // Check if key and issuer certificate match
662 //
663 if( strlen( opt.issuer_crt ) )
664 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200665 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200666 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200667 {
Hanno Becker81535d02017-09-13 15:39:59 +0100668 mbedtls_printf( " failed\n ! issuer_key does not match "
669 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200670 goto exit;
671 }
672 }
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200675
676 if( opt.selfsign )
677 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100678 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200679 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200680 }
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
683 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200684
Paul Bakker9397dcb2013-09-06 09:55:26 +0200685 /*
686 * 1.0. Check the names for validity
687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_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_subject_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 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200697 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100698 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100699 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200700 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200701 goto exit;
702 }
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705 fflush( stdout );
706
Hanno Becker38eff432017-09-22 15:38:20 +0100707 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100708 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200711 if( ret != 0 )
712 {
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_serial "
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 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200720 if( ret != 0 )
721 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100722 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100723 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200724 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200725 goto exit;
726 }
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200729
Hanno Becker38eff432017-09-22 15:38:20 +0100730 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
731 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200732 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100733 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
734 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200735
Hanno Becker6c13d372017-09-13 12:49:22 +0100736 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
737 opt.max_pathlen );
738 if( ret != 0 )
739 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100740 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100741 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200742 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100743 goto exit;
744 }
745
746 mbedtls_printf( " ok\n" );
747 }
Paul Bakker15162a02013-09-06 19:27:21 +0200748
Nicholas Wilson99a96b12015-09-10 18:28:01 +0100749 mbedtls_x509write_crt_set_md_alg( &crt, opt.sig_alg );
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100752 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
753 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200754 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100755 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
756 fflush( stdout );
757
758 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
759 if( ret != 0 )
760 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100761 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100762 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100763 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200764 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100765 goto exit;
766 }
767
768 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200769 }
770
Hanno Becker38eff432017-09-22 15:38:20 +0100771 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
772 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200773 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100774 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
775 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200776
Hanno Becker6c13d372017-09-13 12:49:22 +0100777 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
778 if( ret != 0 )
779 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100780 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100781 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100782 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200783 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100784 goto exit;
785 }
786
787 mbedtls_printf( " ok\n" );
788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200790
Hanno Becker38eff432017-09-22 15:38:20 +0100791 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
792 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200795 fflush( stdout );
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200798 if( ret != 0 )
799 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100800 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100801 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200802 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200803 goto exit;
804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200807 }
808
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100809 if( opt.ext_key_usage )
810 {
811 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
812 fflush( stdout );
813
814 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
815 if( ret != 0 )
816 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100817 mbedtls_strerror( ret, buf, sizeof(buf) );
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100818 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 +0100819 goto exit;
820 }
821
822 mbedtls_printf( " ok\n" );
823 }
824
Hanno Becker38eff432017-09-22 15:38:20 +0100825 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
826 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200829 fflush( stdout );
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200832 if( ret != 0 )
833 {
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100834 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100835 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200836 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200837 goto exit;
838 }
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200841 }
842
Paul Bakker9397dcb2013-09-06 09:55:26 +0200843 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100844 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200847 fflush( stdout );
848
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200849 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200851 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100852 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100853 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200854 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200855 goto exit;
856 }
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200859
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100860 exit_code = MBEDTLS_EXIT_SUCCESS;
861
Paul Bakker9397dcb2013-09-06 09:55:26 +0200862exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100863#if defined(MBEDTLS_X509_CSR_PARSE_C)
864 mbedtls_x509_csr_free( &csr );
865#endif /* MBEDTLS_X509_CSR_PARSE_C */
866 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_x509write_crt_free( &crt );
868 mbedtls_pk_free( &loaded_subject_key );
869 mbedtls_pk_free( &loaded_issuer_key );
870 mbedtls_mpi_free( &serial );
871 mbedtls_ctr_drbg_free( &ctr_drbg );
872 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200873
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200874 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
877 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100878 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */