blob: aa1cf54e7b03fe8c13593692e8075a5a7e5ca9c4 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_X509_CRT_WRITE_C) &&\
37 defined(POLARSSL_X509_CRT_PARSE_C) && defined(POLARSSL_FS_IO) &&\
38 defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_CTR_DRBG_C) &&\
39 defined(POLARSSL_ERROR_C)
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020040#include "polarssl/x509_crt.h"
41#include "polarssl/x509_csr.h"
42#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
44#include "polarssl/error.h"
45
Rich Evans18b78c72015-02-11 14:06:19 +000046#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#endif
50
51#if defined(POLARSSL_X509_CSR_PARSE_C)
52#define USAGE_CSR \
53 " request_file=%%s default: (empty)\n" \
54 " If request_file is specified, subject_key,\n" \
55 " subject_pwd and subject_name are ignored!\n"
56#else
57#define USAGE_CSR ""
58#endif /* POLARSSL_X509_CSR_PARSE_C */
59
Paul Bakker1014e952013-09-09 13:59:42 +020060#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020061#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020062#define DFL_SUBJECT_KEY "subject.key"
63#define DFL_ISSUER_KEY "ca.key"
64#define DFL_SUBJECT_PWD ""
65#define DFL_ISSUER_PWD ""
66#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000067#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
68#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020069#define DFL_NOT_BEFORE "20010101000000"
70#define DFL_NOT_AFTER "20301231235959"
71#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020072#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020073#define DFL_IS_CA 0
74#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020075#define DFL_KEY_USAGE 0
76#define DFL_NS_CERT_TYPE 0
77
Rich Evans18b78c72015-02-11 14:06:19 +000078#define USAGE \
79 "\n usage: cert_write param=<>...\n" \
80 "\n acceptable parameters:\n" \
81 USAGE_CSR \
82 " subject_key=%%s default: subject.key\n" \
83 " subject_pwd=%%s default: (empty)\n" \
84 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
85 "\n" \
86 " issuer_crt=%%s default: (empty)\n" \
87 " If issuer_crt is specified, issuer_name is\n" \
88 " ignored!\n" \
89 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
90 "\n" \
91 " selfsign=%%d default: 0 (false)\n" \
92 " If selfsign is enabled, issuer_name and\n" \
93 " issuer_key are required (issuer_crt and\n" \
94 " subject_* are ignored\n" \
95 " issuer_key=%%s default: ca.key\n" \
96 " issuer_pwd=%%s default: (empty)\n" \
97 " output_file=%%s default: cert.crt\n" \
98 " serial=%%s default: 1\n" \
99 " not_before=%%s default: 20010101000000\n"\
100 " not_after=%%s default: 20301231235959\n"\
101 " is_ca=%%d default: 0 (disabled)\n" \
102 " max_pathlen=%%d default: -1 (none)\n" \
103 " key_usage=%%s default: (empty)\n" \
104 " Comma-separated-list of values:\n" \
105 " digital_signature\n" \
106 " non_repudiation\n" \
107 " key_encipherment\n" \
108 " data_encipherment\n" \
109 " key_agreement\n" \
110 " key_certificate_sign\n" \
111 " crl_sign\n" \
112 " ns_cert_type=%%s default: (empty)\n" \
113 " Comma-separated-list of values:\n" \
114 " ssl_client\n" \
115 " ssl_server\n" \
116 " email\n" \
117 " object_signing\n" \
118 " ssl_ca\n" \
119 " email_ca\n" \
120 " object_signing_ca\n" \
121 "\n"
122
123#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
124 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
125 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
126 !defined(POLARSSL_ERROR_C)
127int main( int argc, char *argv[] )
128{
129 ((void) argc);
130 ((void) argv);
131
132 polarssl_printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
133 "POLARSSL_FS_IO and/or "
134 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
135 "POLARSSL_ERROR_C not defined.\n");
136 return( 0 );
137}
138#else
Paul Bakker9397dcb2013-09-06 09:55:26 +0200139/*
140 * global options
141 */
142struct options
143{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100144 const char *issuer_crt; /* filename of the issuer certificate */
145 const char *request_file; /* filename of the certificate request */
146 const char *subject_key; /* filename of the subject key file */
147 const char *issuer_key; /* filename of the issuer key file */
148 const char *subject_pwd; /* password for the subject key file */
149 const char *issuer_pwd; /* password for the issuer key file */
150 const char *output_file; /* where to store the constructed key file */
151 const char *subject_name; /* subject name for certificate */
152 const char *issuer_name; /* issuer name for certificate */
153 const char *not_before; /* validity period not before */
154 const char *not_after; /* validity period not after */
155 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200156 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200157 int is_ca; /* is a CA certificate */
158 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200159 unsigned char key_usage; /* key usage flags */
160 unsigned char ns_cert_type; /* NS cert type */
161} opt;
162
Paul Bakker8fc30b12013-11-25 13:29:43 +0100163int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200164 int (*f_rng)(void *, unsigned char *, size_t),
165 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200166{
167 int ret;
168 FILE *f;
169 unsigned char output_buf[4096];
170 size_t len = 0;
171
172 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200173 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200174 return( ret );
175
176 len = strlen( (char *) output_buf );
177
178 if( ( f = fopen( output_file, "w" ) ) == NULL )
179 return( -1 );
180
181 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200182 {
183 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200185 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200186
Paul Bakker0c226102014-04-17 16:02:36 +0200187 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200188
189 return( 0 );
190}
191
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192int main( int argc, char *argv[] )
193{
194 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200195 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200196 pk_context loaded_issuer_key, loaded_subject_key;
197 pk_context *issuer_key = &loaded_issuer_key,
198 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200200 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100201 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200202 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200203#if defined(POLARSSL_X509_CSR_PARSE_C)
204 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200205 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200206#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207 x509write_cert crt;
208 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200209 entropy_context entropy;
210 ctr_drbg_context ctr_drbg;
211 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212
213 /*
214 * Set to sane values
215 */
216 x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000217 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA256 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200218 pk_init( &loaded_issuer_key );
219 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200220 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200221#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200222 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200223#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200224 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200225 memset( buf, 0, 1024 );
226
227 if( argc == 0 )
228 {
229 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000230 polarssl_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231 ret = 1;
232 goto exit;
233 }
234
Paul Bakker1014e952013-09-09 13:59:42 +0200235 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200236 opt.request_file = DFL_REQUEST_FILE;
237 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200238 opt.subject_key = DFL_SUBJECT_KEY;
239 opt.issuer_key = DFL_ISSUER_KEY;
240 opt.subject_pwd = DFL_SUBJECT_PWD;
241 opt.issuer_pwd = DFL_ISSUER_PWD;
242 opt.output_file = DFL_OUTPUT_FILENAME;
243 opt.subject_name = DFL_SUBJECT_NAME;
244 opt.issuer_name = DFL_ISSUER_NAME;
245 opt.not_before = DFL_NOT_BEFORE;
246 opt.not_after = DFL_NOT_AFTER;
247 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200248 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200249 opt.is_ca = DFL_IS_CA;
250 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251 opt.key_usage = DFL_KEY_USAGE;
252 opt.ns_cert_type = DFL_NS_CERT_TYPE;
253
254 for( i = 1; i < argc; i++ )
255 {
256
257 p = argv[i];
258 if( ( q = strchr( p, '=' ) ) == NULL )
259 goto usage;
260 *q++ = '\0';
261
Paul Bakkere2673fb2013-09-09 15:52:07 +0200262 if( strcmp( p, "request_file" ) == 0 )
263 opt.request_file = q;
264 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200265 opt.subject_key = q;
266 else if( strcmp( p, "issuer_key" ) == 0 )
267 opt.issuer_key = q;
268 else if( strcmp( p, "subject_pwd" ) == 0 )
269 opt.subject_pwd = q;
270 else if( strcmp( p, "issuer_pwd" ) == 0 )
271 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200272 else if( strcmp( p, "issuer_crt" ) == 0 )
273 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200274 else if( strcmp( p, "output_file" ) == 0 )
275 opt.output_file = q;
276 else if( strcmp( p, "subject_name" ) == 0 )
277 {
278 opt.subject_name = q;
279 }
280 else if( strcmp( p, "issuer_name" ) == 0 )
281 {
282 opt.issuer_name = q;
283 }
284 else if( strcmp( p, "not_before" ) == 0 )
285 {
286 opt.not_before = q;
287 }
288 else if( strcmp( p, "not_after" ) == 0 )
289 {
290 opt.not_after = q;
291 }
292 else if( strcmp( p, "serial" ) == 0 )
293 {
294 opt.serial = q;
295 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200296 else if( strcmp( p, "selfsign" ) == 0 )
297 {
298 opt.selfsign = atoi( q );
299 if( opt.selfsign < 0 || opt.selfsign > 1 )
300 goto usage;
301 }
Paul Bakker15162a02013-09-06 19:27:21 +0200302 else if( strcmp( p, "is_ca" ) == 0 )
303 {
304 opt.is_ca = atoi( q );
305 if( opt.is_ca < 0 || opt.is_ca > 1 )
306 goto usage;
307 }
308 else if( strcmp( p, "max_pathlen" ) == 0 )
309 {
310 opt.max_pathlen = atoi( q );
311 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
312 goto usage;
313 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200314 else if( strcmp( p, "key_usage" ) == 0 )
315 {
316 while( q != NULL )
317 {
318 if( ( r = strchr( q, ',' ) ) != NULL )
319 *r++ = '\0';
320
321 if( strcmp( q, "digital_signature" ) == 0 )
322 opt.key_usage |= KU_DIGITAL_SIGNATURE;
323 else if( strcmp( q, "non_repudiation" ) == 0 )
324 opt.key_usage |= KU_NON_REPUDIATION;
325 else if( strcmp( q, "key_encipherment" ) == 0 )
326 opt.key_usage |= KU_KEY_ENCIPHERMENT;
327 else if( strcmp( q, "data_encipherment" ) == 0 )
328 opt.key_usage |= KU_DATA_ENCIPHERMENT;
329 else if( strcmp( q, "key_agreement" ) == 0 )
330 opt.key_usage |= KU_KEY_AGREEMENT;
331 else if( strcmp( q, "key_cert_sign" ) == 0 )
332 opt.key_usage |= KU_KEY_CERT_SIGN;
333 else if( strcmp( q, "crl_sign" ) == 0 )
334 opt.key_usage |= KU_CRL_SIGN;
335 else
336 goto usage;
337
338 q = r;
339 }
340 }
341 else if( strcmp( p, "ns_cert_type" ) == 0 )
342 {
343 while( q != NULL )
344 {
345 if( ( r = strchr( q, ',' ) ) != NULL )
346 *r++ = '\0';
347
348 if( strcmp( q, "ssl_client" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
350 else if( strcmp( q, "ssl_server" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
352 else if( strcmp( q, "email" ) == 0 )
353 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
354 else if( strcmp( q, "object_signing" ) == 0 )
355 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
356 else if( strcmp( q, "ssl_ca" ) == 0 )
357 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
358 else if( strcmp( q, "email_ca" ) == 0 )
359 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
360 else if( strcmp( q, "object_signing_ca" ) == 0 )
361 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
362 else
363 goto usage;
364
365 q = r;
366 }
367 }
368 else
369 goto usage;
370 }
371
Rich Evansf90016a2015-01-19 14:26:37 +0000372 polarssl_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200373
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200374 /*
375 * 0. Seed the PRNG
376 */
Rich Evansf90016a2015-01-19 14:26:37 +0000377 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200378 fflush( stdout );
379
380 entropy_init( &entropy );
381 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
382 (const unsigned char *) pers,
383 strlen( pers ) ) ) != 0 )
384 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700385 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000386 polarssl_printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200387 goto exit;
388 }
389
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200391
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 // Parse serial to MPI
393 //
Rich Evansf90016a2015-01-19 14:26:37 +0000394 polarssl_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200395 fflush( stdout );
396
Paul Bakker9397dcb2013-09-06 09:55:26 +0200397 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
398 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700399 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000400 polarssl_printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200401 goto exit;
402 }
403
Rich Evansf90016a2015-01-19 14:26:37 +0000404 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200405
Paul Bakker1014e952013-09-09 13:59:42 +0200406 // Parse issuer certificate if present
407 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200408 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200409 {
410 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200411 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200412 */
Rich Evansf90016a2015-01-19 14:26:37 +0000413 polarssl_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200414 fflush( stdout );
415
Paul Bakkerddf26b42013-09-18 13:46:23 +0200416 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200417 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700418 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000419 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200420 goto exit;
421 }
422
Paul Bakker86d0c192013-09-18 11:11:02 +0200423 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200424 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200425 if( ret < 0 )
426 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700427 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000428 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200429 goto exit;
430 }
431
Paul Bakkere2673fb2013-09-09 15:52:07 +0200432 opt.issuer_name = issuer_name;
433
Rich Evansf90016a2015-01-19 14:26:37 +0000434 polarssl_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200435 }
436
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200437#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200438 // Parse certificate request if present
439 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200440 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200441 {
442 /*
443 * 1.0.b. Load the CSR
444 */
Rich Evansf90016a2015-01-19 14:26:37 +0000445 polarssl_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 fflush( stdout );
447
Paul Bakkerddf26b42013-09-18 13:46:23 +0200448 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700450 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000451 polarssl_printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200452 goto exit;
453 }
454
Paul Bakker86d0c192013-09-18 11:11:02 +0200455 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200456 &csr.subject );
457 if( ret < 0 )
458 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700459 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000460 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200461 goto exit;
462 }
463
464 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200465 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200466
Rich Evansf90016a2015-01-19 14:26:37 +0000467 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200468 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200469#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200470
471 /*
472 * 1.1. Load the keys
473 */
474 if( !opt.selfsign && !strlen( opt.request_file ) )
475 {
Rich Evansf90016a2015-01-19 14:26:37 +0000476 polarssl_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200477 fflush( stdout );
478
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200480 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200481 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200482 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700483 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000484 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200485 goto exit;
486 }
Paul Bakker1014e952013-09-09 13:59:42 +0200487
Rich Evansf90016a2015-01-19 14:26:37 +0000488 polarssl_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200489 }
490
Rich Evansf90016a2015-01-19 14:26:37 +0000491 polarssl_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200492 fflush( stdout );
493
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
495 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200496 if( ret != 0 )
497 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700498 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000499 polarssl_printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200500 goto exit;
501 }
502
503 // Check if key and issuer certificate match
504 //
505 if( strlen( opt.issuer_crt ) )
506 {
507 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200508 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
509 &pk_rsa( *issuer_key )->N ) != 0 ||
510 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
511 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200512 {
Rich Evansf90016a2015-01-19 14:26:37 +0000513 polarssl_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514 ret = -1;
515 goto exit;
516 }
517 }
518
Rich Evansf90016a2015-01-19 14:26:37 +0000519 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200520
521 if( opt.selfsign )
522 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100523 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200524 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200525 }
526
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200527 x509write_crt_set_subject_key( &crt, subject_key );
528 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200529
Paul Bakker9397dcb2013-09-06 09:55:26 +0200530 /*
531 * 1.0. Check the names for validity
532 */
533 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
534 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700535 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000536 polarssl_printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200537 goto exit;
538 }
539
540 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
541 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700542 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000543 polarssl_printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 goto exit;
545 }
546
Rich Evansf90016a2015-01-19 14:26:37 +0000547 polarssl_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200548 fflush( stdout );
549
550 ret = x509write_crt_set_serial( &crt, &serial );
551 if( ret != 0 )
552 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700553 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000554 polarssl_printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200555 goto exit;
556 }
557
558 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
559 if( ret != 0 )
560 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700561 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000562 polarssl_printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200563 goto exit;
564 }
565
Rich Evansf90016a2015-01-19 14:26:37 +0000566 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200567
Rich Evansf90016a2015-01-19 14:26:37 +0000568 polarssl_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200569 fflush( stdout );
570
571 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
572 opt.max_pathlen );
573 if( ret != 0 )
574 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700575 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000576 polarssl_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200577 goto exit;
578 }
579
Rich Evansf90016a2015-01-19 14:26:37 +0000580 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200581
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100582#if defined(POLARSSL_SHA1_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000583 polarssl_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200584 fflush( stdout );
585
586 ret = x509write_crt_set_subject_key_identifier( &crt );
587 if( ret != 0 )
588 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700589 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000590 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 +0200591 goto exit;
592 }
593
Rich Evansf90016a2015-01-19 14:26:37 +0000594 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200595
Rich Evansf90016a2015-01-19 14:26:37 +0000596 polarssl_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200597 fflush( stdout );
598
599 ret = x509write_crt_set_authority_key_identifier( &crt );
600 if( ret != 0 )
601 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700602 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000603 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 +0200604 goto exit;
605 }
606
Rich Evansf90016a2015-01-19 14:26:37 +0000607 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100608#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200609
Paul Bakker52be08c2013-09-09 12:37:54 +0200610 if( opt.key_usage )
611 {
Rich Evansf90016a2015-01-19 14:26:37 +0000612 polarssl_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200613 fflush( stdout );
614
615 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
616 if( ret != 0 )
617 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700618 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000619 polarssl_printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200620 goto exit;
621 }
622
Rich Evansf90016a2015-01-19 14:26:37 +0000623 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200624 }
625
626 if( opt.ns_cert_type )
627 {
Rich Evansf90016a2015-01-19 14:26:37 +0000628 polarssl_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200629 fflush( stdout );
630
631 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
632 if( ret != 0 )
633 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700634 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000635 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 +0200636 goto exit;
637 }
638
Rich Evansf90016a2015-01-19 14:26:37 +0000639 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200640 }
641
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 /*
643 * 1.2. Writing the request
644 */
Rich Evansf90016a2015-01-19 14:26:37 +0000645 polarssl_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 fflush( stdout );
647
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200648 if( ( ret = write_certificate( &crt, opt.output_file,
649 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700651 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000652 polarssl_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200653 goto exit;
654 }
655
Rich Evansf90016a2015-01-19 14:26:37 +0000656 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200657
658exit:
659 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200660 pk_free( &loaded_subject_key );
661 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200662 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200663 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200664 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200665
666#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000667 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200668 fflush( stdout ); getchar();
669#endif
670
671 return( ret );
672}
Paul Bakker36713e82013-09-17 13:25:29 +0200673#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
674 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200675 POLARSSL_ERROR_C */