blob: 2f51e19c87809908d1a84d6b8f0423615e868598 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Simon Butcher203a6932016-10-07 15:00:17 +010033#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
34 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
36 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
37 !defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038int main( void )
39{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnardd7389652015-08-06 18:22:26 +020041 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
43 "MBEDTLS_ERROR_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020044 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020045}
46#else
47
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/x509_crt.h"
49#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010050#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010053#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010060#define SET_OID(x, oid) \
61 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char*)oid; } while( 0 )
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000064#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010065 " request_file=%%s default: (empty)\n" \
66 " If request_file is specified, subject_key,\n" \
67 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000068#else
69#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000071
Paul Bakker1014e952013-09-09 13:59:42 +020072#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020073#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_SUBJECT_KEY "subject.key"
75#define DFL_ISSUER_KEY "ca.key"
76#define DFL_SUBJECT_PWD ""
77#define DFL_ISSUER_PWD ""
78#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000079#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
80#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020081#define DFL_NOT_BEFORE "20010101000000"
82#define DFL_NOT_AFTER "20301231235959"
83#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020084#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020085#define DFL_IS_CA 0
86#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010087#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020088#define DFL_KEY_USAGE 0
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010089#define DFL_EXT_KEY_USAGE 0
Paul Bakker9397dcb2013-09-06 09:55:26 +020090#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010091#define DFL_VERSION 3
92#define DFL_AUTH_IDENT 1
93#define DFL_SUBJ_IDENT 1
94#define DFL_CONSTRAINTS 1
95#define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020096
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE \
98 "\n usage: cert_write param=<>...\n" \
99 "\n acceptable parameters:\n" \
100 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100101 " subject_key=%%s default: subject.key\n" \
102 " subject_pwd=%%s default: (empty)\n" \
103 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " issuer_crt=%%s default: (empty)\n" \
106 " If issuer_crt is specified, issuer_name is\n" \
107 " ignored!\n" \
108 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100110 " selfsign=%%d default: 0 (false)\n" \
111 " If selfsign is enabled, issuer_name and\n" \
112 " issuer_key are required (issuer_crt and\n" \
113 " subject_* are ignored\n" \
114 " issuer_key=%%s default: ca.key\n" \
115 " issuer_pwd=%%s default: (empty)\n" \
116 " output_file=%%s default: cert.crt\n" \
117 " serial=%%s default: 1\n" \
118 " not_before=%%s default: 20010101000000\n"\
119 " not_after=%%s default: 20301231235959\n"\
120 " is_ca=%%d default: 0 (disabled)\n" \
121 " max_pathlen=%%d default: -1 (none)\n" \
122 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200123 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200124 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200125 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " version=%%d default: 3\n" \
127 " Possible values: 1, 2, 3\n"\
128 " subject_identifier=%%s default: 1\n" \
129 " Possible values: 0, 1\n" \
130 " (Considered for v3 only)\n"\
131 " authority_identifier=%%s default: 1\n" \
132 " Possible values: 0, 1\n" \
133 " (Considered for v3 only)\n"\
134 " basic_constraints=%%d default: 1\n" \
135 " Possible values: 0, 1\n" \
136 " (Considered for v3 only)\n"\
137 " key_usage=%%s default: (empty)\n" \
138 " Comma-separated-list of values:\n" \
139 " digital_signature\n" \
140 " non_repudiation\n" \
141 " key_encipherment\n" \
142 " data_encipherment\n" \
143 " key_agreement\n" \
144 " key_cert_sign\n" \
145 " crl_sign\n" \
146 " (Considered for v3 only)\n"\
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100147 " ext_key_usage=%%s default: (empty)\n" \
148 " Comma-separated-list of values:\n" \
149 " serverAuth\n" \
150 " clientAuth\n" \
151 " codeSigning\n" \
152 " emailProtection\n" \
153 " timeStamping\n" \
154 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100155 " ns_cert_type=%%s default: (empty)\n" \
156 " Comma-separated-list of values:\n" \
157 " ssl_client\n" \
158 " ssl_server\n" \
159 " email\n" \
160 " object_signing\n" \
161 " ssl_ca\n" \
162 " email_ca\n" \
163 " object_signing_ca\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000164 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000165
Simon Butcher63cb97e2018-12-06 17:43:31 +0000166
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167/*
168 * global options
169 */
170struct options
171{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100172 const char *issuer_crt; /* filename of the issuer certificate */
173 const char *request_file; /* filename of the certificate request */
174 const char *subject_key; /* filename of the subject key file */
175 const char *issuer_key; /* filename of the issuer key file */
176 const char *subject_pwd; /* password for the subject key file */
177 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100178 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100179 const char *subject_name; /* subject name for certificate */
180 const char *issuer_name; /* issuer name for certificate */
181 const char *not_before; /* validity period not before */
182 const char *not_after; /* validity period not after */
183 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200184 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200185 int is_ca; /* is a CA certificate */
186 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100187 int authority_identifier; /* add authority identifier to CRT */
188 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100189 int basic_constraints; /* add basic constraints ext to CRT */
190 int version; /* CRT version */
191 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100193 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 unsigned char ns_cert_type; /* NS cert type */
195} opt;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200198 int (*f_rng)(void *, unsigned char *, size_t),
199 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200{
201 int ret;
202 FILE *f;
203 unsigned char output_buf[4096];
204 size_t len = 0;
205
206 memset( output_buf, 0, 4096 );
Hanno Becker81535d02017-09-13 15:39:59 +0100207 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096,
208 f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209 return( ret );
210
211 len = strlen( (char *) output_buf );
212
213 if( ( f = fopen( output_file, "w" ) ) == NULL )
214 return( -1 );
215
216 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200217 {
218 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200219 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200220 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221
Paul Bakker0c226102014-04-17 16:02:36 +0200222 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
224 return( 0 );
225}
226
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227int main( int argc, char *argv[] )
228{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100229 int ret = 1;
230 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_x509_crt issuer_crt;
232 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
233 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200234 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200236 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100237 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200240 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_x509write_cert crt;
244 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100245 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_entropy_context entropy;
247 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200248 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
250 /*
251 * Set to sane values
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509write_crt_init( &crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pk_init( &loaded_issuer_key );
255 mbedtls_pk_init( &loaded_subject_key );
256 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200257 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Becker30a95102018-10-05 09:49:33 +0100258 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_X509_CSR_PARSE_C)
260 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_x509_crt_init( &issuer_crt );
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100263 memset( buf, 0, sizeof(buf) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200264
265 if( argc == 0 )
266 {
267 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200269 goto exit;
270 }
271
Paul Bakker1014e952013-09-09 13:59:42 +0200272 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200273 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 opt.subject_key = DFL_SUBJECT_KEY;
275 opt.issuer_key = DFL_ISSUER_KEY;
276 opt.subject_pwd = DFL_SUBJECT_PWD;
277 opt.issuer_pwd = DFL_ISSUER_PWD;
278 opt.output_file = DFL_OUTPUT_FILENAME;
279 opt.subject_name = DFL_SUBJECT_NAME;
280 opt.issuer_name = DFL_ISSUER_NAME;
281 opt.not_before = DFL_NOT_BEFORE;
282 opt.not_after = DFL_NOT_AFTER;
283 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200284 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200285 opt.is_ca = DFL_IS_CA;
286 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100288 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100290 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100291 opt.md = DFL_DIGEST;
292 opt.subject_identifier = DFL_SUBJ_IDENT;
293 opt.authority_identifier = DFL_AUTH_IDENT;
294 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295
296 for( i = 1; i < argc; i++ )
297 {
298
299 p = argv[i];
300 if( ( q = strchr( p, '=' ) ) == NULL )
301 goto usage;
302 *q++ = '\0';
303
Paul Bakkere2673fb2013-09-09 15:52:07 +0200304 if( strcmp( p, "request_file" ) == 0 )
305 opt.request_file = q;
306 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 opt.subject_key = q;
308 else if( strcmp( p, "issuer_key" ) == 0 )
309 opt.issuer_key = q;
310 else if( strcmp( p, "subject_pwd" ) == 0 )
311 opt.subject_pwd = q;
312 else if( strcmp( p, "issuer_pwd" ) == 0 )
313 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200314 else if( strcmp( p, "issuer_crt" ) == 0 )
315 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316 else if( strcmp( p, "output_file" ) == 0 )
317 opt.output_file = q;
318 else if( strcmp( p, "subject_name" ) == 0 )
319 {
320 opt.subject_name = q;
321 }
322 else if( strcmp( p, "issuer_name" ) == 0 )
323 {
324 opt.issuer_name = q;
325 }
326 else if( strcmp( p, "not_before" ) == 0 )
327 {
328 opt.not_before = q;
329 }
330 else if( strcmp( p, "not_after" ) == 0 )
331 {
332 opt.not_after = q;
333 }
334 else if( strcmp( p, "serial" ) == 0 )
335 {
336 opt.serial = q;
337 }
Hanno Becker6c13d372017-09-13 12:49:22 +0100338 else if( strcmp( p, "authority_identifier" ) == 0 )
339 {
340 opt.authority_identifier = atoi( q );
341 if( opt.authority_identifier != 0 &&
342 opt.authority_identifier != 1 )
343 {
Hanno Becker17c32762017-10-03 14:56:04 +0100344 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100345 goto usage;
346 }
347 }
348 else if( strcmp( p, "subject_identifier" ) == 0 )
349 {
350 opt.subject_identifier = atoi( q );
351 if( opt.subject_identifier != 0 &&
352 opt.subject_identifier != 1 )
353 {
Hanno Becker17c32762017-10-03 14:56:04 +0100354 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100355 goto usage;
356 }
357 }
358 else if( strcmp( p, "basic_constraints" ) == 0 )
359 {
360 opt.basic_constraints = atoi( q );
361 if( opt.basic_constraints != 0 &&
362 opt.basic_constraints != 1 )
363 {
Hanno Becker17c32762017-10-03 14:56:04 +0100364 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100365 goto usage;
366 }
367 }
368 else if( strcmp( p, "md" ) == 0 )
369 {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200370 const mbedtls_md_info_t *md_info =
371 mbedtls_md_info_from_string( q );
372 if( md_info == NULL )
Hanno Becker17c32762017-10-03 14:56:04 +0100373 {
374 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100375 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100376 }
Gilles Peskine18292fe2020-08-21 20:42:32 +0200377 opt.md = mbedtls_md_get_type( md_info );
Hanno Becker6c13d372017-09-13 12:49:22 +0100378 }
379 else if( strcmp( p, "version" ) == 0 )
380 {
381 opt.version = atoi( q );
382 if( opt.version < 1 || opt.version > 3 )
Hanno Becker17c32762017-10-03 14:56:04 +0100383 {
384 mbedtls_printf( "Invalid argument for option %s\n", p );
Hanno Becker6c13d372017-09-13 12:49:22 +0100385 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100386 }
Hanno Becker38eff432017-09-22 15:38:20 +0100387 opt.version--;
Hanno Becker6c13d372017-09-13 12:49:22 +0100388 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200389 else if( strcmp( p, "selfsign" ) == 0 )
390 {
391 opt.selfsign = atoi( q );
392 if( opt.selfsign < 0 || opt.selfsign > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100393 {
394 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200395 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100396 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200397 }
Paul Bakker15162a02013-09-06 19:27:21 +0200398 else if( strcmp( p, "is_ca" ) == 0 )
399 {
400 opt.is_ca = atoi( q );
401 if( opt.is_ca < 0 || opt.is_ca > 1 )
Hanno Becker17c32762017-10-03 14:56:04 +0100402 {
403 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200404 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100405 }
Paul Bakker15162a02013-09-06 19:27:21 +0200406 }
407 else if( strcmp( p, "max_pathlen" ) == 0 )
408 {
409 opt.max_pathlen = atoi( q );
410 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
Hanno Becker17c32762017-10-03 14:56:04 +0100411 {
412 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker15162a02013-09-06 19:27:21 +0200413 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100414 }
Paul Bakker15162a02013-09-06 19:27:21 +0200415 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200416 else if( strcmp( p, "key_usage" ) == 0 )
417 {
418 while( q != NULL )
419 {
420 if( ( r = strchr( q, ',' ) ) != NULL )
421 *r++ = '\0';
422
423 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100428 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200429 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100430 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200431 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100432 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200433 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200437 else
Hanno Becker17c32762017-10-03 14:56:04 +0100438 {
439 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200440 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100441 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200442
443 q = r;
444 }
445 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100446 else if( strcmp( p, "ext_key_usage" ) == 0 )
447 {
Dave Rodgman64937852022-08-15 14:12:25 +0100448 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
449
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100450 while( q != NULL )
451 {
452 if( ( r = strchr( q, ',' ) ) != NULL )
453 *r++ = '\0';
454
455 ext_key_usage = mbedtls_calloc( 1, sizeof(mbedtls_asn1_sequence) );
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100456 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
457 if( strcmp( q, "serverAuth" ) == 0 )
458 SET_OID( ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH );
459 else if( strcmp( q, "clientAuth" ) == 0 )
460 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH );
461 else if( strcmp( q, "codeSigning" ) == 0 )
462 SET_OID( ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING );
463 else if( strcmp( q, "emailProtection" ) == 0 )
464 SET_OID( ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION );
465 else if( strcmp( q, "timeStamping" ) == 0 )
466 SET_OID( ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING );
467 else if( strcmp( q, "OCSPSigning" ) == 0 )
468 SET_OID( ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING );
469 else
470 goto usage;
Dave Rodgman64937852022-08-15 14:12:25 +0100471
472 *tail = ext_key_usage;
473 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100474
475 q = r;
476 }
477 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200478 else if( strcmp( p, "ns_cert_type" ) == 0 )
479 {
480 while( q != NULL )
481 {
482 if( ( r = strchr( q, ',' ) ) != NULL )
483 *r++ = '\0';
484
485 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100488 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100494 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200495 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100496 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200497 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100498 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 else
Hanno Becker17c32762017-10-03 14:56:04 +0100500 {
501 mbedtls_printf( "Invalid argument for option %s\n", p );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200502 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100503 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200504
505 q = r;
506 }
507 }
508 else
509 goto usage;
510 }
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200513
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200514 /*
515 * 0. Seed the PRNG
516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200518 fflush( stdout );
519
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200520 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200521 (const unsigned char *) pers,
522 strlen( pers ) ) ) != 0 )
523 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100524 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100525 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
526 ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200527 goto exit;
528 }
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200531
Paul Bakker9397dcb2013-09-06 09:55:26 +0200532 // Parse serial to MPI
533 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200535 fflush( stdout );
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200538 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100539 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100540 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200541 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200542 goto exit;
543 }
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200546
Paul Bakker1014e952013-09-09 13:59:42 +0200547 // Parse issuer certificate if present
548 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200549 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200550 {
551 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200552 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200555 fflush( stdout );
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200558 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100559 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100560 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200561 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200562 goto exit;
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200566 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200567 if( ret < 0 )
568 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100569 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100570 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200571 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200572 goto exit;
573 }
574
Paul Bakkere2673fb2013-09-09 15:52:07 +0200575 opt.issuer_name = issuer_name;
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200578 }
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200581 // Parse certificate request if present
582 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200583 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200584 {
585 /*
586 * 1.0.b. Load the CSR
587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589 fflush( stdout );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +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_csr_parse_file "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200595 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200596 goto exit;
597 }
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Gilles Peskine842edf42021-08-04 21:56:10 +0200600 &csr.subject );
Paul Bakkere2673fb2013-09-09 15:52:07 +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 Bakkere2673fb2013-09-09 15:52:07 +0200606 goto exit;
607 }
608
609 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200610 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615
616 /*
617 * 1.1. Load the keys
618 */
619 if( !opt.selfsign && !strlen( opt.request_file ) )
620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200622 fflush( stdout );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200625 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200626 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200627 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100628 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100629 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200630 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200631 goto exit;
632 }
Paul Bakker1014e952013-09-09 13:59:42 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200635 }
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200638 fflush( stdout );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200641 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200642 if( ret != 0 )
643 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100644 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100645 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200646 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200647 goto exit;
648 }
649
650 // Check if key and issuer certificate match
651 //
652 if( strlen( opt.issuer_crt ) )
653 {
Gilles Peskine842edf42021-08-04 21:56:10 +0200654 if( mbedtls_pk_check_pair( &issuer_crt.pk, issuer_key,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200655 mbedtls_ctr_drbg_random, &ctr_drbg ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200656 {
Hanno Becker81535d02017-09-13 15:39:59 +0100657 mbedtls_printf( " failed\n ! issuer_key does not match "
658 "issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200659 goto exit;
660 }
661 }
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200664
665 if( opt.selfsign )
666 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100667 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200668 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
672 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200673
Paul Bakker9397dcb2013-09-06 09:55:26 +0200674 /*
675 * 1.0. Check the names for validity
676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200678 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100679 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100680 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200681 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200682 goto exit;
683 }
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200686 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100687 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100688 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200689 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200690 goto exit;
691 }
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200694 fflush( stdout );
695
Hanno Becker38eff432017-09-22 15:38:20 +0100696 mbedtls_x509write_crt_set_version( &crt, opt.version );
Hanno Becker6c13d372017-09-13 12:49:22 +0100697 mbedtls_x509write_crt_set_md_alg( &crt, opt.md );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200700 if( ret != 0 )
701 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100702 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100703 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200704 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705 goto exit;
706 }
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200709 if( ret != 0 )
710 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100711 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100712 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200713 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200714 goto exit;
715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200718
Hanno Becker38eff432017-09-22 15:38:20 +0100719 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
720 opt.basic_constraints != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200721 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100722 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
723 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200724
Hanno Becker6c13d372017-09-13 12:49:22 +0100725 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
726 opt.max_pathlen );
727 if( ret != 0 )
728 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100729 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100730 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200731 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100732 goto exit;
733 }
734
735 mbedtls_printf( " ok\n" );
736 }
Paul Bakker15162a02013-09-06 19:27:21 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#if defined(MBEDTLS_SHA1_C)
Hanno Becker38eff432017-09-22 15:38:20 +0100739 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
740 opt.subject_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200741 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100742 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
743 fflush( stdout );
744
745 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
746 if( ret != 0 )
747 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100748 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100749 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100750 "_key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200751 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100752 goto exit;
753 }
754
755 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200756 }
757
Hanno Becker38eff432017-09-22 15:38:20 +0100758 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
759 opt.authority_identifier != 0 )
Paul Bakker15162a02013-09-06 19:27:21 +0200760 {
Hanno Becker6c13d372017-09-13 12:49:22 +0100761 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
762 fflush( stdout );
Paul Bakker15162a02013-09-06 19:27:21 +0200763
Hanno Becker6c13d372017-09-13 12:49:22 +0100764 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
765 if( ret != 0 )
766 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100767 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker6c13d372017-09-13 12:49:22 +0100768 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_"
Hanno Becker7f3652d2017-09-22 15:39:02 +0100769 "key_identifier returned -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200770 (unsigned int) -ret, buf );
Hanno Becker6c13d372017-09-13 12:49:22 +0100771 goto exit;
772 }
773
774 mbedtls_printf( " ok\n" );
775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200777
Hanno Becker38eff432017-09-22 15:38:20 +0100778 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
779 opt.key_usage != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200782 fflush( stdout );
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200785 if( ret != 0 )
786 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100787 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100788 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage "
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200789 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200790 goto exit;
791 }
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200794 }
795
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100796 if( opt.ext_key_usage )
797 {
798 mbedtls_printf( " . Adding the Extended Key Usage extension ..." );
799 fflush( stdout );
800
801 ret = mbedtls_x509write_crt_set_ext_key_usage( &crt, opt.ext_key_usage );
802 if( ret != 0 )
803 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100804 mbedtls_strerror( ret, buf, sizeof(buf) );
Dave Rodgmanec9f6b42022-07-27 14:34:58 +0100805 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 +0100806 goto exit;
807 }
808
809 mbedtls_printf( " ok\n" );
810 }
811
Hanno Becker38eff432017-09-22 15:38:20 +0100812 if( opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
813 opt.ns_cert_type != 0 )
Paul Bakker52be08c2013-09-09 12:37:54 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_printf( " . Adding the NS Cert Type 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_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200819 if( ret != 0 )
820 {
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100821 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker81535d02017-09-13 15:39:59 +0100822 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
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
Paul Bakker9397dcb2013-09-06 09:55:26 +0200830 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100831 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200834 fflush( stdout );
835
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200836 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200838 {
Dave Rodgman2ee7bbd2022-08-11 16:23:17 +0100839 mbedtls_strerror( ret, buf, sizeof(buf) );
Hanno Becker7f3652d2017-09-22 15:39:02 +0100840 mbedtls_printf( " failed\n ! write_certificate -0x%04x - %s\n\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200841 (unsigned int) -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200842 goto exit;
843 }
844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200846
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100847 exit_code = MBEDTLS_EXIT_SUCCESS;
848
Paul Bakker9397dcb2013-09-06 09:55:26 +0200849exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100850#if defined(MBEDTLS_X509_CSR_PARSE_C)
851 mbedtls_x509_csr_free( &csr );
852#endif /* MBEDTLS_X509_CSR_PARSE_C */
853 mbedtls_x509_crt_free( &issuer_crt );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_x509write_crt_free( &crt );
855 mbedtls_pk_free( &loaded_subject_key );
856 mbedtls_pk_free( &loaded_issuer_key );
857 mbedtls_mpi_free( &serial );
858 mbedtls_ctr_drbg_free( &ctr_drbg );
859 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200860
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200861 mbedtls_exit( exit_code );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
864 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100865 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */