blob: 45e2456dcf16d668b52f4e2e189ea473312c0d02 [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
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +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) && \
Rich Evans18b78c72015-02-11 14:06:19 +000039 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"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000122
Rich Evans18b78c72015-02-11 14:06:19 +0000123#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)
Rich Evans85b05ec2015-02-12 11:37:29 +0000127int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000128{
Rich Evans18b78c72015-02-11 14:06:19 +0000129 polarssl_printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
130 "POLARSSL_FS_IO and/or "
131 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
132 "POLARSSL_ERROR_C not defined.\n");
133 return( 0 );
134}
135#else
Paul Bakker9397dcb2013-09-06 09:55:26 +0200136/*
137 * global options
138 */
139struct options
140{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100141 const char *issuer_crt; /* filename of the issuer certificate */
142 const char *request_file; /* filename of the certificate request */
143 const char *subject_key; /* filename of the subject key file */
144 const char *issuer_key; /* filename of the issuer key file */
145 const char *subject_pwd; /* password for the subject key file */
146 const char *issuer_pwd; /* password for the issuer key file */
147 const char *output_file; /* where to store the constructed key file */
148 const char *subject_name; /* subject name for certificate */
149 const char *issuer_name; /* issuer name for certificate */
150 const char *not_before; /* validity period not before */
151 const char *not_after; /* validity period not after */
152 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200153 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200154 int is_ca; /* is a CA certificate */
155 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200156 unsigned char key_usage; /* key usage flags */
157 unsigned char ns_cert_type; /* NS cert type */
158} opt;
159
Paul Bakker8fc30b12013-11-25 13:29:43 +0100160int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200161 int (*f_rng)(void *, unsigned char *, size_t),
162 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200163{
164 int ret;
165 FILE *f;
166 unsigned char output_buf[4096];
167 size_t len = 0;
168
169 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200170 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200171 return( ret );
172
173 len = strlen( (char *) output_buf );
174
175 if( ( f = fopen( output_file, "w" ) ) == NULL )
176 return( -1 );
177
178 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200179 {
180 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200182 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200183
Paul Bakker0c226102014-04-17 16:02:36 +0200184 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185
186 return( 0 );
187}
188
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189int main( int argc, char *argv[] )
190{
191 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200192 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200193 pk_context loaded_issuer_key, loaded_subject_key;
194 pk_context *issuer_key = &loaded_issuer_key,
195 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200197 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100198 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200200#if defined(POLARSSL_X509_CSR_PARSE_C)
201 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200202 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200203#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204 x509write_cert crt;
205 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200206 entropy_context entropy;
207 ctr_drbg_context ctr_drbg;
208 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200209
210 /*
211 * Set to sane values
212 */
213 x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000214 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA256 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200215 pk_init( &loaded_issuer_key );
216 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200218#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200219 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200220#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200221 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200222 memset( buf, 0, 1024 );
223
224 if( argc == 0 )
225 {
226 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228 ret = 1;
229 goto exit;
230 }
231
Paul Bakker1014e952013-09-09 13:59:42 +0200232 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200233 opt.request_file = DFL_REQUEST_FILE;
234 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235 opt.subject_key = DFL_SUBJECT_KEY;
236 opt.issuer_key = DFL_ISSUER_KEY;
237 opt.subject_pwd = DFL_SUBJECT_PWD;
238 opt.issuer_pwd = DFL_ISSUER_PWD;
239 opt.output_file = DFL_OUTPUT_FILENAME;
240 opt.subject_name = DFL_SUBJECT_NAME;
241 opt.issuer_name = DFL_ISSUER_NAME;
242 opt.not_before = DFL_NOT_BEFORE;
243 opt.not_after = DFL_NOT_AFTER;
244 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200245 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200246 opt.is_ca = DFL_IS_CA;
247 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200248 opt.key_usage = DFL_KEY_USAGE;
249 opt.ns_cert_type = DFL_NS_CERT_TYPE;
250
251 for( i = 1; i < argc; i++ )
252 {
253
254 p = argv[i];
255 if( ( q = strchr( p, '=' ) ) == NULL )
256 goto usage;
257 *q++ = '\0';
258
Paul Bakkere2673fb2013-09-09 15:52:07 +0200259 if( strcmp( p, "request_file" ) == 0 )
260 opt.request_file = q;
261 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262 opt.subject_key = q;
263 else if( strcmp( p, "issuer_key" ) == 0 )
264 opt.issuer_key = q;
265 else if( strcmp( p, "subject_pwd" ) == 0 )
266 opt.subject_pwd = q;
267 else if( strcmp( p, "issuer_pwd" ) == 0 )
268 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200269 else if( strcmp( p, "issuer_crt" ) == 0 )
270 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271 else if( strcmp( p, "output_file" ) == 0 )
272 opt.output_file = q;
273 else if( strcmp( p, "subject_name" ) == 0 )
274 {
275 opt.subject_name = q;
276 }
277 else if( strcmp( p, "issuer_name" ) == 0 )
278 {
279 opt.issuer_name = q;
280 }
281 else if( strcmp( p, "not_before" ) == 0 )
282 {
283 opt.not_before = q;
284 }
285 else if( strcmp( p, "not_after" ) == 0 )
286 {
287 opt.not_after = q;
288 }
289 else if( strcmp( p, "serial" ) == 0 )
290 {
291 opt.serial = q;
292 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200293 else if( strcmp( p, "selfsign" ) == 0 )
294 {
295 opt.selfsign = atoi( q );
296 if( opt.selfsign < 0 || opt.selfsign > 1 )
297 goto usage;
298 }
Paul Bakker15162a02013-09-06 19:27:21 +0200299 else if( strcmp( p, "is_ca" ) == 0 )
300 {
301 opt.is_ca = atoi( q );
302 if( opt.is_ca < 0 || opt.is_ca > 1 )
303 goto usage;
304 }
305 else if( strcmp( p, "max_pathlen" ) == 0 )
306 {
307 opt.max_pathlen = atoi( q );
308 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
309 goto usage;
310 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200311 else if( strcmp( p, "key_usage" ) == 0 )
312 {
313 while( q != NULL )
314 {
315 if( ( r = strchr( q, ',' ) ) != NULL )
316 *r++ = '\0';
317
318 if( strcmp( q, "digital_signature" ) == 0 )
319 opt.key_usage |= KU_DIGITAL_SIGNATURE;
320 else if( strcmp( q, "non_repudiation" ) == 0 )
321 opt.key_usage |= KU_NON_REPUDIATION;
322 else if( strcmp( q, "key_encipherment" ) == 0 )
323 opt.key_usage |= KU_KEY_ENCIPHERMENT;
324 else if( strcmp( q, "data_encipherment" ) == 0 )
325 opt.key_usage |= KU_DATA_ENCIPHERMENT;
326 else if( strcmp( q, "key_agreement" ) == 0 )
327 opt.key_usage |= KU_KEY_AGREEMENT;
328 else if( strcmp( q, "key_cert_sign" ) == 0 )
329 opt.key_usage |= KU_KEY_CERT_SIGN;
330 else if( strcmp( q, "crl_sign" ) == 0 )
331 opt.key_usage |= KU_CRL_SIGN;
332 else
333 goto usage;
334
335 q = r;
336 }
337 }
338 else if( strcmp( p, "ns_cert_type" ) == 0 )
339 {
340 while( q != NULL )
341 {
342 if( ( r = strchr( q, ',' ) ) != NULL )
343 *r++ = '\0';
344
345 if( strcmp( q, "ssl_client" ) == 0 )
346 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
347 else if( strcmp( q, "ssl_server" ) == 0 )
348 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
349 else if( strcmp( q, "email" ) == 0 )
350 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
351 else if( strcmp( q, "object_signing" ) == 0 )
352 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
353 else if( strcmp( q, "ssl_ca" ) == 0 )
354 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
355 else if( strcmp( q, "email_ca" ) == 0 )
356 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
357 else if( strcmp( q, "object_signing_ca" ) == 0 )
358 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
359 else
360 goto usage;
361
362 q = r;
363 }
364 }
365 else
366 goto usage;
367 }
368
Rich Evansf90016a2015-01-19 14:26:37 +0000369 polarssl_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200370
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200371 /*
372 * 0. Seed the PRNG
373 */
Rich Evansf90016a2015-01-19 14:26:37 +0000374 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200375 fflush( stdout );
376
377 entropy_init( &entropy );
378 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
379 (const unsigned char *) pers,
380 strlen( pers ) ) ) != 0 )
381 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700382 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000383 polarssl_printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200384 goto exit;
385 }
386
Rich Evansf90016a2015-01-19 14:26:37 +0000387 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200388
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 // Parse serial to MPI
390 //
Rich Evansf90016a2015-01-19 14:26:37 +0000391 polarssl_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200392 fflush( stdout );
393
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
395 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700396 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200398 goto exit;
399 }
400
Rich Evansf90016a2015-01-19 14:26:37 +0000401 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200402
Paul Bakker1014e952013-09-09 13:59:42 +0200403 // Parse issuer certificate if present
404 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200405 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200406 {
407 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200408 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200409 */
Rich Evansf90016a2015-01-19 14:26:37 +0000410 polarssl_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200411 fflush( stdout );
412
Paul Bakkerddf26b42013-09-18 13:46:23 +0200413 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200414 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700415 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000416 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200417 goto exit;
418 }
419
Paul Bakker86d0c192013-09-18 11:11:02 +0200420 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200421 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200422 if( ret < 0 )
423 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700424 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000425 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200426 goto exit;
427 }
428
Paul Bakkere2673fb2013-09-09 15:52:07 +0200429 opt.issuer_name = issuer_name;
430
Rich Evansf90016a2015-01-19 14:26:37 +0000431 polarssl_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200432 }
433
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200434#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200435 // Parse certificate request if present
436 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200437 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200438 {
439 /*
440 * 1.0.b. Load the CSR
441 */
Rich Evansf90016a2015-01-19 14:26:37 +0000442 polarssl_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200443 fflush( stdout );
444
Paul Bakkerddf26b42013-09-18 13:46:23 +0200445 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700447 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000448 polarssl_printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449 goto exit;
450 }
451
Paul Bakker86d0c192013-09-18 11:11:02 +0200452 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200453 &csr.subject );
454 if( ret < 0 )
455 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700456 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000457 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200458 goto exit;
459 }
460
461 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200462 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200463
Rich Evansf90016a2015-01-19 14:26:37 +0000464 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200465 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200466#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200467
468 /*
469 * 1.1. Load the keys
470 */
471 if( !opt.selfsign && !strlen( opt.request_file ) )
472 {
Rich Evansf90016a2015-01-19 14:26:37 +0000473 polarssl_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200474 fflush( stdout );
475
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200477 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200478 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200479 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700480 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000481 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200482 goto exit;
483 }
Paul Bakker1014e952013-09-09 13:59:42 +0200484
Rich Evansf90016a2015-01-19 14:26:37 +0000485 polarssl_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200486 }
487
Rich Evansf90016a2015-01-19 14:26:37 +0000488 polarssl_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200489 fflush( stdout );
490
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
492 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200493 if( ret != 0 )
494 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700495 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000496 polarssl_printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200497 goto exit;
498 }
499
500 // Check if key and issuer certificate match
501 //
502 if( strlen( opt.issuer_crt ) )
503 {
504 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200505 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
506 &pk_rsa( *issuer_key )->N ) != 0 ||
507 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
508 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200509 {
Rich Evansf90016a2015-01-19 14:26:37 +0000510 polarssl_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200511 ret = -1;
512 goto exit;
513 }
514 }
515
Rich Evansf90016a2015-01-19 14:26:37 +0000516 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200517
518 if( opt.selfsign )
519 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100520 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200521 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200522 }
523
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200524 x509write_crt_set_subject_key( &crt, subject_key );
525 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200526
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527 /*
528 * 1.0. Check the names for validity
529 */
530 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
531 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700532 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000533 polarssl_printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200534 goto exit;
535 }
536
537 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
538 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700539 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000540 polarssl_printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200541 goto exit;
542 }
543
Rich Evansf90016a2015-01-19 14:26:37 +0000544 polarssl_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200545 fflush( stdout );
546
547 ret = x509write_crt_set_serial( &crt, &serial );
548 if( ret != 0 )
549 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700550 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000551 polarssl_printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200552 goto exit;
553 }
554
555 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
556 if( ret != 0 )
557 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700558 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000559 polarssl_printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200560 goto exit;
561 }
562
Rich Evansf90016a2015-01-19 14:26:37 +0000563 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200564
Rich Evansf90016a2015-01-19 14:26:37 +0000565 polarssl_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200566 fflush( stdout );
567
568 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
569 opt.max_pathlen );
570 if( ret != 0 )
571 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700572 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000573 polarssl_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200574 goto exit;
575 }
576
Rich Evansf90016a2015-01-19 14:26:37 +0000577 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200578
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100579#if defined(POLARSSL_SHA1_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000580 polarssl_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200581 fflush( stdout );
582
583 ret = x509write_crt_set_subject_key_identifier( &crt );
584 if( ret != 0 )
585 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700586 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000587 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 +0200588 goto exit;
589 }
590
Rich Evansf90016a2015-01-19 14:26:37 +0000591 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200592
Rich Evansf90016a2015-01-19 14:26:37 +0000593 polarssl_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200594 fflush( stdout );
595
596 ret = x509write_crt_set_authority_key_identifier( &crt );
597 if( ret != 0 )
598 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700599 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000600 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 +0200601 goto exit;
602 }
603
Rich Evansf90016a2015-01-19 14:26:37 +0000604 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100605#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200606
Paul Bakker52be08c2013-09-09 12:37:54 +0200607 if( opt.key_usage )
608 {
Rich Evansf90016a2015-01-19 14:26:37 +0000609 polarssl_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200610 fflush( stdout );
611
612 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
613 if( ret != 0 )
614 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700615 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000616 polarssl_printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200617 goto exit;
618 }
619
Rich Evansf90016a2015-01-19 14:26:37 +0000620 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200621 }
622
623 if( opt.ns_cert_type )
624 {
Rich Evansf90016a2015-01-19 14:26:37 +0000625 polarssl_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200626 fflush( stdout );
627
628 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
629 if( ret != 0 )
630 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700631 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000632 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 +0200633 goto exit;
634 }
635
Rich Evansf90016a2015-01-19 14:26:37 +0000636 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200637 }
638
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639 /*
640 * 1.2. Writing the request
641 */
Rich Evansf90016a2015-01-19 14:26:37 +0000642 polarssl_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200643 fflush( stdout );
644
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200645 if( ( ret = write_certificate( &crt, opt.output_file,
646 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200647 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700648 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000649 polarssl_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650 goto exit;
651 }
652
Rich Evansf90016a2015-01-19 14:26:37 +0000653 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200654
655exit:
656 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200657 pk_free( &loaded_subject_key );
658 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200659 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200660 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200661 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200662
663#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000664 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200665 fflush( stdout ); getchar();
666#endif
667
668 return( ret );
669}
Paul Bakker36713e82013-09-17 13:25:29 +0200670#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
671 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200672 POLARSSL_ERROR_C */