blob: c6718886877a3518d99e19da11f8dca2d8580260 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker9397dcb2013-09-06 09:55:26 +020038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
43 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020044 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020045 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
Rich Evansf90016a2015-01-19 14:26:37 +000051 polarssl_printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020052 "POLARSSL_FS_IO and/or "
53 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
54 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020055 return( 0 );
56}
57#else
58
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059#include "polarssl/x509_crt.h"
60#include "polarssl/x509_csr.h"
61#include "polarssl/entropy.h"
62#include "polarssl/ctr_drbg.h"
63#include "polarssl/error.h"
64
Paul Bakker1014e952013-09-09 13:59:42 +020065#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020066#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020067#define DFL_SUBJECT_KEY "subject.key"
68#define DFL_ISSUER_KEY "ca.key"
69#define DFL_SUBJECT_PWD ""
70#define DFL_ISSUER_PWD ""
71#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000072#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
73#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_NOT_BEFORE "20010101000000"
75#define DFL_NOT_AFTER "20301231235959"
76#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020077#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020078#define DFL_IS_CA 0
79#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020080#define DFL_KEY_USAGE 0
81#define DFL_NS_CERT_TYPE 0
82
83/*
84 * global options
85 */
86struct options
87{
Paul Bakker8fc30b12013-11-25 13:29:43 +010088 const char *issuer_crt; /* filename of the issuer certificate */
89 const char *request_file; /* filename of the certificate request */
90 const char *subject_key; /* filename of the subject key file */
91 const char *issuer_key; /* filename of the issuer key file */
92 const char *subject_pwd; /* password for the subject key file */
93 const char *issuer_pwd; /* password for the issuer key file */
94 const char *output_file; /* where to store the constructed key file */
95 const char *subject_name; /* subject name for certificate */
96 const char *issuer_name; /* issuer name for certificate */
97 const char *not_before; /* validity period not before */
98 const char *not_after; /* validity period not after */
99 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200100 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200101 int is_ca; /* is a CA certificate */
102 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200103 unsigned char key_usage; /* key usage flags */
104 unsigned char ns_cert_type; /* NS cert type */
105} opt;
106
Paul Bakker8fc30b12013-11-25 13:29:43 +0100107int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200110{
111 int ret;
112 FILE *f;
113 unsigned char output_buf[4096];
114 size_t len = 0;
115
116 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200117 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200118 return( ret );
119
120 len = strlen( (char *) output_buf );
121
122 if( ( f = fopen( output_file, "w" ) ) == NULL )
123 return( -1 );
124
125 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200126 {
127 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200128 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200129 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200130
Paul Bakker0c226102014-04-17 16:02:36 +0200131 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132
133 return( 0 );
134}
135
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200136#if defined(POLARSSL_X509_CSR_PARSE_C)
137#define USAGE_CSR \
138 " request_file=%%s default: (empty)\n" \
139 " If request_file is specified, subject_key,\n" \
140 " subject_pwd and subject_name are ignored!\n"
141#else
142#define USAGE_CSR ""
143#endif /* POLARSSL_X509_CSR_PARSE_C */
144
Paul Bakker9397dcb2013-09-06 09:55:26 +0200145#define USAGE \
146 "\n usage: cert_write param=<>...\n" \
147 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200148 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200149 " subject_key=%%s default: subject.key\n" \
150 " subject_pwd=%%s default: (empty)\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000151 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200152 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200153 " issuer_crt=%%s default: (empty)\n" \
154 " If issuer_crt is specified, issuer_name is\n" \
155 " ignored!\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000156 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200157 "\n" \
158 " selfsign=%%d default: 0 (false)\n" \
159 " If selfsign is enabled, issuer_name and\n" \
160 " issuer_key are required (issuer_crt and\n" \
161 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200162 " issuer_key=%%s default: ca.key\n" \
163 " issuer_pwd=%%s default: (empty)\n" \
164 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200165 " serial=%%s default: 1\n" \
166 " not_before=%%s default: 20010101000000\n"\
167 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200168 " is_ca=%%d default: 0 (disabled)\n" \
169 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200170 " key_usage=%%s default: (empty)\n" \
171 " Comma-separated-list of values:\n" \
172 " digital_signature\n" \
173 " non_repudiation\n" \
174 " key_encipherment\n" \
175 " data_encipherment\n" \
176 " key_agreement\n" \
177 " key_certificate_sign\n" \
178 " crl_sign\n" \
179 " ns_cert_type=%%s default: (empty)\n" \
180 " Comma-separated-list of values:\n" \
181 " ssl_client\n" \
182 " ssl_server\n" \
183 " email\n" \
184 " object_signing\n" \
185 " ssl_ca\n" \
186 " email_ca\n" \
187 " object_signing_ca\n" \
188 "\n"
189
190int main( int argc, char *argv[] )
191{
192 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200193 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200194 pk_context loaded_issuer_key, loaded_subject_key;
195 pk_context *issuer_key = &loaded_issuer_key,
196 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200198 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100199 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200201#if defined(POLARSSL_X509_CSR_PARSE_C)
202 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200203 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200204#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205 x509write_cert crt;
206 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200207 entropy_context entropy;
208 ctr_drbg_context ctr_drbg;
209 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210
211 /*
212 * Set to sane values
213 */
214 x509write_crt_init( &crt );
215 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200216 pk_init( &loaded_issuer_key );
217 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200219#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200220 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200221#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200222 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 memset( buf, 0, 1024 );
224
225 if( argc == 0 )
226 {
227 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000228 polarssl_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200229 ret = 1;
230 goto exit;
231 }
232
Paul Bakker1014e952013-09-09 13:59:42 +0200233 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200234 opt.request_file = DFL_REQUEST_FILE;
235 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236 opt.subject_key = DFL_SUBJECT_KEY;
237 opt.issuer_key = DFL_ISSUER_KEY;
238 opt.subject_pwd = DFL_SUBJECT_PWD;
239 opt.issuer_pwd = DFL_ISSUER_PWD;
240 opt.output_file = DFL_OUTPUT_FILENAME;
241 opt.subject_name = DFL_SUBJECT_NAME;
242 opt.issuer_name = DFL_ISSUER_NAME;
243 opt.not_before = DFL_NOT_BEFORE;
244 opt.not_after = DFL_NOT_AFTER;
245 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200246 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200247 opt.is_ca = DFL_IS_CA;
248 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 opt.key_usage = DFL_KEY_USAGE;
250 opt.ns_cert_type = DFL_NS_CERT_TYPE;
251
252 for( i = 1; i < argc; i++ )
253 {
254
255 p = argv[i];
256 if( ( q = strchr( p, '=' ) ) == NULL )
257 goto usage;
258 *q++ = '\0';
259
Paul Bakkere2673fb2013-09-09 15:52:07 +0200260 if( strcmp( p, "request_file" ) == 0 )
261 opt.request_file = q;
262 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200263 opt.subject_key = q;
264 else if( strcmp( p, "issuer_key" ) == 0 )
265 opt.issuer_key = q;
266 else if( strcmp( p, "subject_pwd" ) == 0 )
267 opt.subject_pwd = q;
268 else if( strcmp( p, "issuer_pwd" ) == 0 )
269 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200270 else if( strcmp( p, "issuer_crt" ) == 0 )
271 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200272 else if( strcmp( p, "output_file" ) == 0 )
273 opt.output_file = q;
274 else if( strcmp( p, "subject_name" ) == 0 )
275 {
276 opt.subject_name = q;
277 }
278 else if( strcmp( p, "issuer_name" ) == 0 )
279 {
280 opt.issuer_name = q;
281 }
282 else if( strcmp( p, "not_before" ) == 0 )
283 {
284 opt.not_before = q;
285 }
286 else if( strcmp( p, "not_after" ) == 0 )
287 {
288 opt.not_after = q;
289 }
290 else if( strcmp( p, "serial" ) == 0 )
291 {
292 opt.serial = q;
293 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200294 else if( strcmp( p, "selfsign" ) == 0 )
295 {
296 opt.selfsign = atoi( q );
297 if( opt.selfsign < 0 || opt.selfsign > 1 )
298 goto usage;
299 }
Paul Bakker15162a02013-09-06 19:27:21 +0200300 else if( strcmp( p, "is_ca" ) == 0 )
301 {
302 opt.is_ca = atoi( q );
303 if( opt.is_ca < 0 || opt.is_ca > 1 )
304 goto usage;
305 }
306 else if( strcmp( p, "max_pathlen" ) == 0 )
307 {
308 opt.max_pathlen = atoi( q );
309 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
310 goto usage;
311 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200312 else if( strcmp( p, "key_usage" ) == 0 )
313 {
314 while( q != NULL )
315 {
316 if( ( r = strchr( q, ',' ) ) != NULL )
317 *r++ = '\0';
318
319 if( strcmp( q, "digital_signature" ) == 0 )
320 opt.key_usage |= KU_DIGITAL_SIGNATURE;
321 else if( strcmp( q, "non_repudiation" ) == 0 )
322 opt.key_usage |= KU_NON_REPUDIATION;
323 else if( strcmp( q, "key_encipherment" ) == 0 )
324 opt.key_usage |= KU_KEY_ENCIPHERMENT;
325 else if( strcmp( q, "data_encipherment" ) == 0 )
326 opt.key_usage |= KU_DATA_ENCIPHERMENT;
327 else if( strcmp( q, "key_agreement" ) == 0 )
328 opt.key_usage |= KU_KEY_AGREEMENT;
329 else if( strcmp( q, "key_cert_sign" ) == 0 )
330 opt.key_usage |= KU_KEY_CERT_SIGN;
331 else if( strcmp( q, "crl_sign" ) == 0 )
332 opt.key_usage |= KU_CRL_SIGN;
333 else
334 goto usage;
335
336 q = r;
337 }
338 }
339 else if( strcmp( p, "ns_cert_type" ) == 0 )
340 {
341 while( q != NULL )
342 {
343 if( ( r = strchr( q, ',' ) ) != NULL )
344 *r++ = '\0';
345
346 if( strcmp( q, "ssl_client" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
348 else if( strcmp( q, "ssl_server" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
350 else if( strcmp( q, "email" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
352 else if( strcmp( q, "object_signing" ) == 0 )
353 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
354 else if( strcmp( q, "ssl_ca" ) == 0 )
355 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
356 else if( strcmp( q, "email_ca" ) == 0 )
357 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
358 else if( strcmp( q, "object_signing_ca" ) == 0 )
359 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
360 else
361 goto usage;
362
363 q = r;
364 }
365 }
366 else
367 goto usage;
368 }
369
Rich Evansf90016a2015-01-19 14:26:37 +0000370 polarssl_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200371
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200372 /*
373 * 0. Seed the PRNG
374 */
Rich Evansf90016a2015-01-19 14:26:37 +0000375 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200376 fflush( stdout );
377
378 entropy_init( &entropy );
379 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
380 (const unsigned char *) pers,
381 strlen( pers ) ) ) != 0 )
382 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700383 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000384 polarssl_printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200385 goto exit;
386 }
387
Rich Evansf90016a2015-01-19 14:26:37 +0000388 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200389
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 // Parse serial to MPI
391 //
Rich Evansf90016a2015-01-19 14:26:37 +0000392 polarssl_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200393 fflush( stdout );
394
Paul Bakker9397dcb2013-09-06 09:55:26 +0200395 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
396 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700397 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399 goto exit;
400 }
401
Rich Evansf90016a2015-01-19 14:26:37 +0000402 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200403
Paul Bakker1014e952013-09-09 13:59:42 +0200404 // Parse issuer certificate if present
405 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200406 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200407 {
408 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200409 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200410 */
Rich Evansf90016a2015-01-19 14:26:37 +0000411 polarssl_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200412 fflush( stdout );
413
Paul Bakkerddf26b42013-09-18 13:46:23 +0200414 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200415 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700416 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000417 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200418 goto exit;
419 }
420
Paul Bakker86d0c192013-09-18 11:11:02 +0200421 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200422 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200423 if( ret < 0 )
424 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700425 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000426 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200427 goto exit;
428 }
429
Paul Bakkere2673fb2013-09-09 15:52:07 +0200430 opt.issuer_name = issuer_name;
431
Rich Evansf90016a2015-01-19 14:26:37 +0000432 polarssl_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200433 }
434
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200435#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200436 // Parse certificate request if present
437 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200438 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200439 {
440 /*
441 * 1.0.b. Load the CSR
442 */
Rich Evansf90016a2015-01-19 14:26:37 +0000443 polarssl_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 fflush( stdout );
445
Paul Bakkerddf26b42013-09-18 13:46:23 +0200446 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200447 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700448 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000449 polarssl_printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200450 goto exit;
451 }
452
Paul Bakker86d0c192013-09-18 11:11:02 +0200453 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200454 &csr.subject );
455 if( ret < 0 )
456 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700457 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000458 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200459 goto exit;
460 }
461
462 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200463 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200464
Rich Evansf90016a2015-01-19 14:26:37 +0000465 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200466 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200467#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200468
469 /*
470 * 1.1. Load the keys
471 */
472 if( !opt.selfsign && !strlen( opt.request_file ) )
473 {
Rich Evansf90016a2015-01-19 14:26:37 +0000474 polarssl_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200475 fflush( stdout );
476
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200478 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200479 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200480 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700481 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000482 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200483 goto exit;
484 }
Paul Bakker1014e952013-09-09 13:59:42 +0200485
Rich Evansf90016a2015-01-19 14:26:37 +0000486 polarssl_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200487 }
488
Rich Evansf90016a2015-01-19 14:26:37 +0000489 polarssl_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200490 fflush( stdout );
491
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
493 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200494 if( ret != 0 )
495 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700496 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000497 polarssl_printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200498 goto exit;
499 }
500
501 // Check if key and issuer certificate match
502 //
503 if( strlen( opt.issuer_crt ) )
504 {
505 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200506 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
507 &pk_rsa( *issuer_key )->N ) != 0 ||
508 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
509 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200510 {
Rich Evansf90016a2015-01-19 14:26:37 +0000511 polarssl_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200512 ret = -1;
513 goto exit;
514 }
515 }
516
Rich Evansf90016a2015-01-19 14:26:37 +0000517 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200518
519 if( opt.selfsign )
520 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100521 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200522 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200523 }
524
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200525 x509write_crt_set_subject_key( &crt, subject_key );
526 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200527
Paul Bakker9397dcb2013-09-06 09:55:26 +0200528 /*
529 * 1.0. Check the names for validity
530 */
531 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
532 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700533 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000534 polarssl_printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 goto exit;
536 }
537
538 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
539 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700540 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000541 polarssl_printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200542 goto exit;
543 }
544
Rich Evansf90016a2015-01-19 14:26:37 +0000545 polarssl_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200546 fflush( stdout );
547
548 ret = x509write_crt_set_serial( &crt, &serial );
549 if( ret != 0 )
550 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700551 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000552 polarssl_printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200553 goto exit;
554 }
555
556 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
557 if( ret != 0 )
558 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700559 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000560 polarssl_printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200561 goto exit;
562 }
563
Rich Evansf90016a2015-01-19 14:26:37 +0000564 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200565
Rich Evansf90016a2015-01-19 14:26:37 +0000566 polarssl_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200567 fflush( stdout );
568
569 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
570 opt.max_pathlen );
571 if( ret != 0 )
572 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700573 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000574 polarssl_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200575 goto exit;
576 }
577
Rich Evansf90016a2015-01-19 14:26:37 +0000578 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200579
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100580#if defined(POLARSSL_SHA1_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000581 polarssl_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200582 fflush( stdout );
583
584 ret = x509write_crt_set_subject_key_identifier( &crt );
585 if( ret != 0 )
586 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700587 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000588 polarssl_printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200589 goto exit;
590 }
591
Rich Evansf90016a2015-01-19 14:26:37 +0000592 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200593
Rich Evansf90016a2015-01-19 14:26:37 +0000594 polarssl_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200595 fflush( stdout );
596
597 ret = x509write_crt_set_authority_key_identifier( &crt );
598 if( ret != 0 )
599 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700600 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000601 polarssl_printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200602 goto exit;
603 }
604
Rich Evansf90016a2015-01-19 14:26:37 +0000605 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100606#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200607
Paul Bakker52be08c2013-09-09 12:37:54 +0200608 if( opt.key_usage )
609 {
Rich Evansf90016a2015-01-19 14:26:37 +0000610 polarssl_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200611 fflush( stdout );
612
613 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
614 if( ret != 0 )
615 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700616 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000617 polarssl_printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200618 goto exit;
619 }
620
Rich Evansf90016a2015-01-19 14:26:37 +0000621 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200622 }
623
624 if( opt.ns_cert_type )
625 {
Rich Evansf90016a2015-01-19 14:26:37 +0000626 polarssl_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200627 fflush( stdout );
628
629 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
630 if( ret != 0 )
631 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700632 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000633 polarssl_printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200634 goto exit;
635 }
636
Rich Evansf90016a2015-01-19 14:26:37 +0000637 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200638 }
639
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 /*
641 * 1.2. Writing the request
642 */
Rich Evansf90016a2015-01-19 14:26:37 +0000643 polarssl_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200644 fflush( stdout );
645
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200646 if( ( ret = write_certificate( &crt, opt.output_file,
647 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200648 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700649 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000650 polarssl_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200651 goto exit;
652 }
653
Rich Evansf90016a2015-01-19 14:26:37 +0000654 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200655
656exit:
657 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200658 pk_free( &loaded_subject_key );
659 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200661 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200662 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200663
664#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000665 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200666 fflush( stdout ); getchar();
667#endif
668
669 return( ret );
670}
Paul Bakker36713e82013-09-17 13:25:29 +0200671#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
672 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200673 POLARSSL_ERROR_C */