blob: 8c4e6da22b8d12ef2ec068438d569f1f13fe1e85 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker9397dcb2013-09-06 09:55:26 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +02007 *
Paul Bakker9397dcb2013-09-06 09:55:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
37 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
38 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
39 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020040int main( void )
41{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf( "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
43 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and_or "
44 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
45 "MBEDTLS_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/x509_crt.h"
51#include "mbedtls/x509_csr.h"
52#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
54#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000061#define USAGE_CSR \
62 " request_file=%%s default: (empty)\n" \
63 " If request_file is specified, subject_key,\n" \
64 " subject_pwd and subject_name are ignored!\n"
65#else
66#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000068
Paul Bakker1014e952013-09-09 13:59:42 +020069#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020070#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_SUBJECT_KEY "subject.key"
72#define DFL_ISSUER_KEY "ca.key"
73#define DFL_SUBJECT_PWD ""
74#define DFL_ISSUER_PWD ""
75#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000076#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_NOT_BEFORE "20010101000000"
79#define DFL_NOT_AFTER "20301231235959"
80#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020081#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020082#define DFL_IS_CA 0
83#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020084#define DFL_KEY_USAGE 0
85#define DFL_NS_CERT_TYPE 0
86
Rich Evans18b78c72015-02-11 14:06:19 +000087#define USAGE \
88 "\n usage: cert_write param=<>...\n" \
89 "\n acceptable parameters:\n" \
90 USAGE_CSR \
91 " subject_key=%%s default: subject.key\n" \
92 " subject_pwd=%%s default: (empty)\n" \
93 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
94 "\n" \
95 " issuer_crt=%%s default: (empty)\n" \
96 " If issuer_crt is specified, issuer_name is\n" \
97 " ignored!\n" \
98 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
99 "\n" \
100 " selfsign=%%d default: 0 (false)\n" \
101 " If selfsign is enabled, issuer_name and\n" \
102 " issuer_key are required (issuer_crt and\n" \
103 " subject_* are ignored\n" \
104 " issuer_key=%%s default: ca.key\n" \
105 " issuer_pwd=%%s default: (empty)\n" \
106 " output_file=%%s default: cert.crt\n" \
107 " serial=%%s default: 1\n" \
108 " not_before=%%s default: 20010101000000\n"\
109 " not_after=%%s default: 20301231235959\n"\
110 " is_ca=%%d default: 0 (disabled)\n" \
111 " max_pathlen=%%d default: -1 (none)\n" \
112 " key_usage=%%s default: (empty)\n" \
113 " Comma-separated-list of values:\n" \
114 " digital_signature\n" \
115 " non_repudiation\n" \
116 " key_encipherment\n" \
117 " data_encipherment\n" \
118 " key_agreement\n" \
119 " key_certificate_sign\n" \
120 " crl_sign\n" \
121 " ns_cert_type=%%s default: (empty)\n" \
122 " Comma-separated-list of values:\n" \
123 " ssl_client\n" \
124 " ssl_server\n" \
125 " email\n" \
126 " object_signing\n" \
127 " ssl_ca\n" \
128 " email_ca\n" \
129 " object_signing_ca\n" \
130 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000131
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132/*
133 * global options
134 */
135struct options
136{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100137 const char *issuer_crt; /* filename of the issuer certificate */
138 const char *request_file; /* filename of the certificate request */
139 const char *subject_key; /* filename of the subject key file */
140 const char *issuer_key; /* filename of the issuer key file */
141 const char *subject_pwd; /* password for the subject key file */
142 const char *issuer_pwd; /* password for the issuer key file */
143 const char *output_file; /* where to store the constructed key file */
144 const char *subject_name; /* subject name for certificate */
145 const char *issuer_name; /* issuer name for certificate */
146 const char *not_before; /* validity period not before */
147 const char *not_after; /* validity period not after */
148 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200149 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200150 int is_ca; /* is a CA certificate */
151 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152 unsigned char key_usage; /* key usage flags */
153 unsigned char ns_cert_type; /* NS cert type */
154} opt;
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int write_certificate( mbedtls_x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200157 int (*f_rng)(void *, unsigned char *, size_t),
158 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200159{
160 int ret;
161 FILE *f;
162 unsigned char output_buf[4096];
163 size_t len = 0;
164
165 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167 return( ret );
168
169 len = strlen( (char *) output_buf );
170
171 if( ( f = fopen( output_file, "w" ) ) == NULL )
172 return( -1 );
173
174 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200175 {
176 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200177 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200178 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200179
Paul Bakker0c226102014-04-17 16:02:36 +0200180 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181
182 return( 0 );
183}
184
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185int main( int argc, char *argv[] )
186{
187 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_x509_crt issuer_crt;
189 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
190 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200191 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200193 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100194 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200197 char subject_name[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_x509write_cert crt;
201 mbedtls_mpi serial;
202 mbedtls_entropy_context entropy;
203 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200204 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205
206 /*
207 * Set to sane values
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_x509write_crt_init( &crt );
210 mbedtls_x509write_crt_set_md_alg( &crt, MBEDTLS_MD_SHA256 );
211 mbedtls_pk_init( &loaded_issuer_key );
212 mbedtls_pk_init( &loaded_subject_key );
213 mbedtls_mpi_init( &serial );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200214 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_X509_CSR_PARSE_C)
216 mbedtls_x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200219 memset( buf, 0, 1024 );
220
221 if( argc == 0 )
222 {
223 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225 ret = 1;
226 goto exit;
227 }
228
Paul Bakker1014e952013-09-09 13:59:42 +0200229 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200230 opt.request_file = DFL_REQUEST_FILE;
231 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200232 opt.subject_key = DFL_SUBJECT_KEY;
233 opt.issuer_key = DFL_ISSUER_KEY;
234 opt.subject_pwd = DFL_SUBJECT_PWD;
235 opt.issuer_pwd = DFL_ISSUER_PWD;
236 opt.output_file = DFL_OUTPUT_FILENAME;
237 opt.subject_name = DFL_SUBJECT_NAME;
238 opt.issuer_name = DFL_ISSUER_NAME;
239 opt.not_before = DFL_NOT_BEFORE;
240 opt.not_after = DFL_NOT_AFTER;
241 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200242 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200243 opt.is_ca = DFL_IS_CA;
244 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200245 opt.key_usage = DFL_KEY_USAGE;
246 opt.ns_cert_type = DFL_NS_CERT_TYPE;
247
248 for( i = 1; i < argc; i++ )
249 {
250
251 p = argv[i];
252 if( ( q = strchr( p, '=' ) ) == NULL )
253 goto usage;
254 *q++ = '\0';
255
Paul Bakkere2673fb2013-09-09 15:52:07 +0200256 if( strcmp( p, "request_file" ) == 0 )
257 opt.request_file = q;
258 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200259 opt.subject_key = q;
260 else if( strcmp( p, "issuer_key" ) == 0 )
261 opt.issuer_key = q;
262 else if( strcmp( p, "subject_pwd" ) == 0 )
263 opt.subject_pwd = q;
264 else if( strcmp( p, "issuer_pwd" ) == 0 )
265 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200266 else if( strcmp( p, "issuer_crt" ) == 0 )
267 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200268 else if( strcmp( p, "output_file" ) == 0 )
269 opt.output_file = q;
270 else if( strcmp( p, "subject_name" ) == 0 )
271 {
272 opt.subject_name = q;
273 }
274 else if( strcmp( p, "issuer_name" ) == 0 )
275 {
276 opt.issuer_name = q;
277 }
278 else if( strcmp( p, "not_before" ) == 0 )
279 {
280 opt.not_before = q;
281 }
282 else if( strcmp( p, "not_after" ) == 0 )
283 {
284 opt.not_after = q;
285 }
286 else if( strcmp( p, "serial" ) == 0 )
287 {
288 opt.serial = q;
289 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200290 else if( strcmp( p, "selfsign" ) == 0 )
291 {
292 opt.selfsign = atoi( q );
293 if( opt.selfsign < 0 || opt.selfsign > 1 )
294 goto usage;
295 }
Paul Bakker15162a02013-09-06 19:27:21 +0200296 else if( strcmp( p, "is_ca" ) == 0 )
297 {
298 opt.is_ca = atoi( q );
299 if( opt.is_ca < 0 || opt.is_ca > 1 )
300 goto usage;
301 }
302 else if( strcmp( p, "max_pathlen" ) == 0 )
303 {
304 opt.max_pathlen = atoi( q );
305 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
306 goto usage;
307 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200308 else if( strcmp( p, "key_usage" ) == 0 )
309 {
310 while( q != NULL )
311 {
312 if( ( r = strchr( q, ',' ) ) != NULL )
313 *r++ = '\0';
314
315 if( strcmp( q, "digital_signature" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200317 else if( strcmp( q, "non_repudiation" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200319 else if( strcmp( q, "key_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100320 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200321 else if( strcmp( q, "data_encipherment" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100322 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200323 else if( strcmp( q, "key_agreement" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100324 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200325 else if( strcmp( q, "key_cert_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200327 else if( strcmp( q, "crl_sign" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200329 else
330 goto usage;
331
332 q = r;
333 }
334 }
335 else if( strcmp( p, "ns_cert_type" ) == 0 )
336 {
337 while( q != NULL )
338 {
339 if( ( r = strchr( q, ',' ) ) != NULL )
340 *r++ = '\0';
341
342 if( strcmp( q, "ssl_client" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200344 else if( strcmp( q, "ssl_server" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100345 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200346 else if( strcmp( q, "email" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200348 else if( strcmp( q, "object_signing" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200350 else if( strcmp( q, "ssl_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100351 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200352 else if( strcmp( q, "email_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100353 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200354 else if( strcmp( q, "object_signing_ca" ) == 0 )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100355 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200356 else
357 goto usage;
358
359 q = r;
360 }
361 }
362 else
363 goto usage;
364 }
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200367
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200368 /*
369 * 0. Seed the PRNG
370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200372 fflush( stdout );
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200375 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200376 (const unsigned char *) pers,
377 strlen( pers ) ) ) != 0 )
378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200380 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200381 goto exit;
382 }
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200385
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 // Parse serial to MPI
387 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200389 fflush( stdout );
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( ( ret = mbedtls_mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_strerror( ret, buf, 1024 );
394 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200395 goto exit;
396 }
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200399
Paul Bakker1014e952013-09-09 13:59:42 +0200400 // Parse issuer certificate if present
401 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200402 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200403 {
404 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200405 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200408 fflush( stdout );
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 if( ( ret = mbedtls_x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_strerror( ret, buf, 1024 );
413 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200414 goto exit;
415 }
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 ret = mbedtls_x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200418 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200419 if( ret < 0 )
420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_strerror( ret, buf, 1024 );
422 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200423 goto exit;
424 }
425
Paul Bakkere2673fb2013-09-09 15:52:07 +0200426 opt.issuer_name = issuer_name;
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200429 }
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200432 // Parse certificate request if present
433 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200434 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200435 {
436 /*
437 * 1.0.b. Load the CSR
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200440 fflush( stdout );
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ( ret = mbedtls_x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_strerror( ret, buf, 1024 );
445 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 goto exit;
447 }
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 ret = mbedtls_x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200450 &csr.subject );
451 if( ret < 0 )
452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_strerror( ret, buf, 1024 );
454 mbedtls_printf( " failed\n ! mbedtls_x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200455 goto exit;
456 }
457
458 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200459 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200464
465 /*
466 * 1.1. Load the keys
467 */
468 if( !opt.selfsign && !strlen( opt.request_file ) )
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200471 fflush( stdout );
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 ret = mbedtls_pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200474 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200475 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_strerror( ret, buf, 1024 );
478 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200479 goto exit;
480 }
Paul Bakker1014e952013-09-09 13:59:42 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200483 }
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200486 fflush( stdout );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 ret = mbedtls_pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200490 if( ret != 0 )
491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_strerror( ret, buf, 1024 );
493 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200494 goto exit;
495 }
496
497 // Check if key and issuer certificate match
498 //
499 if( strlen( opt.issuer_crt ) )
500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( !mbedtls_pk_can_do( &issuer_crt.pk, MBEDTLS_PK_RSA ) ||
502 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->N,
503 &mbedtls_pk_rsa( *issuer_key )->N ) != 0 ||
504 mbedtls_mpi_cmp_mpi( &mbedtls_pk_rsa( issuer_crt.pk )->E,
505 &mbedtls_pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 ret = -1;
509 goto exit;
510 }
511 }
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514
515 if( opt.selfsign )
516 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100517 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200518 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_x509write_crt_set_subject_key( &crt, subject_key );
522 mbedtls_x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200523
Paul Bakker9397dcb2013-09-06 09:55:26 +0200524 /*
525 * 1.0. Check the names for validity
526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( ( ret = mbedtls_x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_strerror( ret, buf, 1024 );
530 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 +0200531 goto exit;
532 }
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_strerror( ret, buf, 1024 );
537 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 +0200538 goto exit;
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200542 fflush( stdout );
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 ret = mbedtls_x509write_crt_set_serial( &crt, &serial );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200545 if( ret != 0 )
546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_strerror( ret, buf, 1024 );
548 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200549 goto exit;
550 }
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 ret = mbedtls_x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200553 if( ret != 0 )
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_strerror( ret, buf, 1024 );
556 mbedtls_printf( " failed\n ! mbedtls_x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200557 goto exit;
558 }
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200563 fflush( stdout );
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 ret = mbedtls_x509write_crt_set_basic_constraints( &crt, opt.is_ca,
Paul Bakker15162a02013-09-06 19:27:21 +0200566 opt.max_pathlen );
567 if( ret != 0 )
568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_strerror( ret, buf, 1024 );
570 mbedtls_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200571 goto exit;
572 }
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_SHA1_C)
577 mbedtls_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200578 fflush( stdout );
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 ret = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200581 if( ret != 0 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_strerror( ret, buf, 1024 );
584 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 +0200585 goto exit;
586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200591 fflush( stdout );
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 ret = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
Paul Bakker15162a02013-09-06 19:27:21 +0200594 if( ret != 0 )
595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_strerror( ret, buf, 1024 );
597 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 +0200598 goto exit;
599 }
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_printf( " ok\n" );
602#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200603
Paul Bakker52be08c2013-09-09 12:37:54 +0200604 if( opt.key_usage )
605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200607 fflush( stdout );
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 ret = mbedtls_x509write_crt_set_key_usage( &crt, opt.key_usage );
Paul Bakker52be08c2013-09-09 12:37:54 +0200610 if( ret != 0 )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_strerror( ret, buf, 1024 );
613 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 +0200614 goto exit;
615 }
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200618 }
619
620 if( opt.ns_cert_type )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200623 fflush( stdout );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 ret = mbedtls_x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
Paul Bakker52be08c2013-09-09 12:37:54 +0200626 if( ret != 0 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_strerror( ret, buf, 1024 );
629 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 +0200630 goto exit;
631 }
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200634 }
635
Paul Bakker9397dcb2013-09-06 09:55:26 +0200636 /*
637 * 1.2. Writing the request
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 fflush( stdout );
641
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200642 if( ( ret = write_certificate( &crt, opt.output_file,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_strerror( ret, buf, 1024 );
646 mbedtls_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200647 goto exit;
648 }
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200651
652exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_x509write_crt_free( &crt );
654 mbedtls_pk_free( &loaded_subject_key );
655 mbedtls_pk_free( &loaded_issuer_key );
656 mbedtls_mpi_free( &serial );
657 mbedtls_ctr_drbg_free( &ctr_drbg );
658 mbedtls_entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200659
660#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200662 fflush( stdout ); getchar();
663#endif
664
665 return( ret );
666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
668 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
669 MBEDTLS_ERROR_C */