blob: 75865e3a7533a882baa763ba1fc70bdcf33701b9 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
36 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
37 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
38 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020039int main( void )
40{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnardd7389652015-08-06 18:22:26 +020042 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
44 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020045 return( 0 );
46}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/x509_crt.h"
50#include "mbedtls/x509_csr.h"
51#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
53#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000060#define USAGE_CSR \
61 " request_file=%%s default: (empty)\n" \
62 " If request_file is specified, subject_key,\n" \
63 " subject_pwd and subject_name are ignored!\n"
64#else
65#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000067
Paul Bakker1014e952013-09-09 13:59:42 +020068#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020069#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020070#define DFL_SUBJECT_KEY "subject.key"
71#define DFL_ISSUER_KEY "ca.key"
72#define DFL_SUBJECT_PWD ""
73#define DFL_ISSUER_PWD ""
74#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000075#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
76#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020077#define DFL_NOT_BEFORE "20010101000000"
78#define DFL_NOT_AFTER "20301231235959"
79#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020080#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020081#define DFL_IS_CA 0
82#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020083#define DFL_KEY_USAGE 0
84#define DFL_NS_CERT_TYPE 0
85
Rich Evans18b78c72015-02-11 14:06:19 +000086#define USAGE \
87 "\n usage: cert_write param=<>...\n" \
88 "\n acceptable parameters:\n" \
89 USAGE_CSR \
90 " subject_key=%%s default: subject.key\n" \
91 " subject_pwd=%%s default: (empty)\n" \
92 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
93 "\n" \
94 " issuer_crt=%%s default: (empty)\n" \
95 " If issuer_crt is specified, issuer_name is\n" \
96 " ignored!\n" \
97 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
98 "\n" \
99 " selfsign=%%d default: 0 (false)\n" \
100 " If selfsign is enabled, issuer_name and\n" \
101 " issuer_key are required (issuer_crt and\n" \
102 " subject_* are ignored\n" \
103 " issuer_key=%%s default: ca.key\n" \
104 " issuer_pwd=%%s default: (empty)\n" \
105 " output_file=%%s default: cert.crt\n" \
106 " serial=%%s default: 1\n" \
107 " not_before=%%s default: 20010101000000\n"\
108 " not_after=%%s default: 20301231235959\n"\
109 " is_ca=%%d default: 0 (disabled)\n" \
110 " max_pathlen=%%d default: -1 (none)\n" \
111 " key_usage=%%s default: (empty)\n" \
112 " Comma-separated-list of values:\n" \
113 " digital_signature\n" \
114 " non_repudiation\n" \
115 " key_encipherment\n" \
116 " data_encipherment\n" \
117 " key_agreement\n" \
118 " key_certificate_sign\n" \
119 " crl_sign\n" \
120 " ns_cert_type=%%s default: (empty)\n" \
121 " Comma-separated-list of values:\n" \
122 " ssl_client\n" \
123 " ssl_server\n" \
124 " email\n" \
125 " object_signing\n" \
126 " ssl_ca\n" \
127 " email_ca\n" \
128 " object_signing_ca\n" \
129 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000130
Paul Bakker9397dcb2013-09-06 09:55:26 +0200131/*
132 * global options
133 */
134struct options
135{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100136 const char *issuer_crt; /* filename of the issuer certificate */
137 const char *request_file; /* filename of the certificate request */
138 const char *subject_key; /* filename of the subject key file */
139 const char *issuer_key; /* filename of the issuer key file */
140 const char *subject_pwd; /* password for the subject key file */
141 const char *issuer_pwd; /* password for the issuer key file */
142 const char *output_file; /* where to store the constructed key file */
143 const char *subject_name; /* subject name for certificate */
144 const char *issuer_name; /* issuer name for certificate */
145 const char *not_before; /* validity period not before */
146 const char *not_after; /* validity period not after */
147 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200148 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200149 int is_ca; /* is a CA certificate */
150 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200151 unsigned char key_usage; /* key usage flags */
152 unsigned char ns_cert_type; /* NS cert type */
153} opt;
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200156 int (*f_rng)(void *, unsigned char *, size_t),
157 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200158{
159 int ret;
160 FILE *f;
161 unsigned char output_buf[4096];
162 size_t len = 0;
163
164 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200166 return( ret );
167
168 len = strlen( (char *) output_buf );
169
170 if( ( f = fopen( output_file, "w" ) ) == NULL )
171 return( -1 );
172
173 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200174 {
175 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200176 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200177 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178
Paul Bakker0c226102014-04-17 16:02:36 +0200179 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200180
181 return( 0 );
182}
183
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184int main( int argc, char *argv[] )
185{
186 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_x509_crt issuer_crt;
188 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
189 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200190 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200192 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100193 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200196 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_x509write_cert crt;
200 mbedtls_mpi serial;
201 mbedtls_entropy_context entropy;
202 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200203 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204
205 /*
206 * Set to sane values
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_x509write_crt_init( &crt );
209 mbedtls_x509write_crt_set_md_alg( &crt, MBEDTLS_MD_SHA256 );
210 mbedtls_pk_init( &loaded_issuer_key );
211 mbedtls_pk_init( &loaded_subject_key );
212 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200213 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_X509_CSR_PARSE_C)
215 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 memset( buf, 0, 1024 );
219
220 if( argc == 0 )
221 {
222 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224 ret = 1;
225 goto exit;
226 }
227
Paul Bakker1014e952013-09-09 13:59:42 +0200228 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200229 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200230 opt.subject_key = DFL_SUBJECT_KEY;
231 opt.issuer_key = DFL_ISSUER_KEY;
232 opt.subject_pwd = DFL_SUBJECT_PWD;
233 opt.issuer_pwd = DFL_ISSUER_PWD;
234 opt.output_file = DFL_OUTPUT_FILENAME;
235 opt.subject_name = DFL_SUBJECT_NAME;
236 opt.issuer_name = DFL_ISSUER_NAME;
237 opt.not_before = DFL_NOT_BEFORE;
238 opt.not_after = DFL_NOT_AFTER;
239 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200240 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200241 opt.is_ca = DFL_IS_CA;
242 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243 opt.key_usage = DFL_KEY_USAGE;
244 opt.ns_cert_type = DFL_NS_CERT_TYPE;
245
246 for( i = 1; i < argc; i++ )
247 {
248
249 p = argv[i];
250 if( ( q = strchr( p, '=' ) ) == NULL )
251 goto usage;
252 *q++ = '\0';
253
Paul Bakkere2673fb2013-09-09 15:52:07 +0200254 if( strcmp( p, "request_file" ) == 0 )
255 opt.request_file = q;
256 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200257 opt.subject_key = q;
258 else if( strcmp( p, "issuer_key" ) == 0 )
259 opt.issuer_key = q;
260 else if( strcmp( p, "subject_pwd" ) == 0 )
261 opt.subject_pwd = q;
262 else if( strcmp( p, "issuer_pwd" ) == 0 )
263 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200264 else if( strcmp( p, "issuer_crt" ) == 0 )
265 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266 else if( strcmp( p, "output_file" ) == 0 )
267 opt.output_file = q;
268 else if( strcmp( p, "subject_name" ) == 0 )
269 {
270 opt.subject_name = q;
271 }
272 else if( strcmp( p, "issuer_name" ) == 0 )
273 {
274 opt.issuer_name = q;
275 }
276 else if( strcmp( p, "not_before" ) == 0 )
277 {
278 opt.not_before = q;
279 }
280 else if( strcmp( p, "not_after" ) == 0 )
281 {
282 opt.not_after = q;
283 }
284 else if( strcmp( p, "serial" ) == 0 )
285 {
286 opt.serial = q;
287 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200288 else if( strcmp( p, "selfsign" ) == 0 )
289 {
290 opt.selfsign = atoi( q );
291 if( opt.selfsign < 0 || opt.selfsign > 1 )
292 goto usage;
293 }
Paul Bakker15162a02013-09-06 19:27:21 +0200294 else if( strcmp( p, "is_ca" ) == 0 )
295 {
296 opt.is_ca = atoi( q );
297 if( opt.is_ca < 0 || opt.is_ca > 1 )
298 goto usage;
299 }
300 else if( strcmp( p, "max_pathlen" ) == 0 )
301 {
302 opt.max_pathlen = atoi( q );
303 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
304 goto usage;
305 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306 else if( strcmp( p, "key_usage" ) == 0 )
307 {
308 while( q != NULL )
309 {
310 if( ( r = strchr( q, ',' ) ) != NULL )
311 *r++ = '\0';
312
313 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200315 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200317 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100318 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200319 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100320 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200321 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100322 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200323 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200325 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200327 else
328 goto usage;
329
330 q = r;
331 }
332 }
333 else if( strcmp( p, "ns_cert_type" ) == 0 )
334 {
335 while( q != NULL )
336 {
337 if( ( r = strchr( q, ',' ) ) != NULL )
338 *r++ = '\0';
339
340 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200342 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100343 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200344 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200346 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200348 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100349 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200350 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100351 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200352 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100353 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200354 else
355 goto usage;
356
357 q = r;
358 }
359 }
360 else
361 goto usage;
362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200365
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200366 /*
367 * 0. Seed the PRNG
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200370 fflush( stdout );
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200373 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200374 (const unsigned char *) pers,
375 strlen( pers ) ) ) != 0 )
376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200378 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200379 goto exit;
380 }
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200383
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 // Parse serial to MPI
385 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200387 fflush( stdout );
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_strerror( ret, buf, 1024 );
392 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200393 goto exit;
394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200397
Paul Bakker1014e952013-09-09 13:59:42 +0200398 // Parse issuer certificate if present
399 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200400 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200401 {
402 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200403 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200406 fflush( stdout );
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_strerror( ret, buf, 1024 );
411 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200412 goto exit;
413 }
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200416 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200417 if( ret < 0 )
418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_strerror( ret, buf, 1024 );
420 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200421 goto exit;
422 }
423
Paul Bakkere2673fb2013-09-09 15:52:07 +0200424 opt.issuer_name = issuer_name;
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200427 }
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200430 // Parse certificate request if present
431 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200432 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200433 {
434 /*
435 * 1.0.b. Load the CSR
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200438 fflush( stdout );
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_strerror( ret, buf, 1024 );
443 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 goto exit;
445 }
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200448 &csr.subject );
449 if( ret < 0 )
450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_strerror( ret, buf, 1024 );
452 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200453 goto exit;
454 }
455
456 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200457 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200462
463 /*
464 * 1.1. Load the keys
465 */
466 if( !opt.selfsign && !strlen( opt.request_file ) )
467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200469 fflush( stdout );
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200472 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200473 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_strerror( ret, buf, 1024 );
476 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200477 goto exit;
478 }
Paul Bakker1014e952013-09-09 13:59:42 +0200479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200481 }
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200484 fflush( stdout );
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200488 if( ret != 0 )
489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_strerror( ret, buf, 1024 );
491 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200492 goto exit;
493 }
494
495 // Check if key and issuer certificate match
496 //
497 if( strlen( opt.issuer_crt ) )
498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
500 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
501 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
502 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
503 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200506 ret = -1;
507 goto exit;
508 }
509 }
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200512
513 if( opt.selfsign )
514 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100515 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200516 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
520 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200521
Paul Bakker9397dcb2013-09-06 09:55:26 +0200522 /*
523 * 1.0. Check the names for validity
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_strerror( ret, buf, 1024 );
528 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200529 goto exit;
530 }
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_strerror( ret, buf, 1024 );
535 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200536 goto exit;
537 }
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200540 fflush( stdout );
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 if( ret != 0 )
544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_strerror( ret, buf, 1024 );
546 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200547 goto exit;
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200551 if( ret != 0 )
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_strerror( ret, buf, 1024 );
554 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200555 goto exit;
556 }
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200561 fflush( stdout );
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
Paul Bakker15162a02013-09-06 19:27:21 +0200564 opt.max_pathlen );
565 if( ret != 0 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_strerror( ret, buf, 1024 );
568 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200569 goto exit;
570 }
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SHA1_C)
575 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200576 fflush( stdout );
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200579 if( ret != 0 )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_strerror( ret, buf, 1024 );
582 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200583 goto exit;
584 }
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200589 fflush( stdout );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200592 if( ret != 0 )
593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_strerror( ret, buf, 1024 );
595 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200596 goto exit;
597 }
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_printf( " ok\n" );
600#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200601
Paul Bakker52be08c2013-09-09 12:37:54 +0200602 if( opt.key_usage )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200605 fflush( stdout );
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200608 if( ret != 0 )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_strerror( ret, buf, 1024 );
611 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200612 goto exit;
613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200616 }
617
618 if( opt.ns_cert_type )
619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200621 fflush( stdout );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200624 if( ret != 0 )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_strerror( ret, buf, 1024 );
627 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200628 goto exit;
629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200632 }
633
Paul Bakker9397dcb2013-09-06 09:55:26 +0200634 /*
635 * 1.2. Writing the request
636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200638 fflush( stdout );
639
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200640 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_strerror( ret, buf, 1024 );
644 mbedtls_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200645 goto exit;
646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200649
650exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_x509write_crt_free( &crt );
652 mbedtls_pk_free( &loaded_subject_key );
653 mbedtls_pk_free( &loaded_issuer_key );
654 mbedtls_mpi_free( &serial );
655 mbedtls_ctr_drbg_free( &ctr_drbg );
656 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200657
658#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660 fflush( stdout ); getchar();
661#endif
662
663 return( ret );
664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
666 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
667 MBEDTLS_ERROR_C */