blob: 290eebcf24ee62f1bb3677ef92ad13ada44aba0f [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker9397dcb2013-09-06 09:55:26 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +02007 *
Paul Bakker9397dcb2013-09-06 09:55:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-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é-Gonnard8d649c62015-03-31 15:10:03 +020036#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) || \
Simon Butcher8c9ab6c2016-10-10 23:23:41 +010039 !defined(POLARSSL_ERROR_C) || !defined(POLARSSL_SHA256_C) || \
40 !defined(POLARSSL_PEM_WRITE_C)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020041int main( void )
42{
Simon Butcher8c9ab6c2016-10-10 23:23:41 +010043 polarssl_printf( "POLARSSL_X509_CRT_WRITE_C and/or "
44 "POLARSSL_X509_CRT_PARSE_C and/or "
45 "POLARSSL_FS_IO and/or POLARSSL_SHA256_C and_or "
46 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
47 "POLARSSL_PEM_WRITE_C and/or "
48 "POLARSSL_ERROR_C not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020049 return( 0 );
50}
51#else
52
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020053#include "polarssl/x509_crt.h"
54#include "polarssl/x509_csr.h"
55#include "polarssl/entropy.h"
56#include "polarssl/ctr_drbg.h"
57#include "polarssl/error.h"
58
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000062
63#if defined(POLARSSL_X509_CSR_PARSE_C)
64#define USAGE_CSR \
65 " request_file=%%s default: (empty)\n" \
66 " If request_file is specified, subject_key,\n" \
67 " subject_pwd and subject_name are ignored!\n"
68#else
69#define USAGE_CSR ""
70#endif /* POLARSSL_X509_CSR_PARSE_C */
71
Paul Bakker1014e952013-09-09 13:59:42 +020072#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020073#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_SUBJECT_KEY "subject.key"
75#define DFL_ISSUER_KEY "ca.key"
76#define DFL_SUBJECT_PWD ""
77#define DFL_ISSUER_PWD ""
78#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000079#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
80#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020081#define DFL_NOT_BEFORE "20010101000000"
82#define DFL_NOT_AFTER "20301231235959"
83#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020084#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020085#define DFL_IS_CA 0
86#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
88#define DFL_NS_CERT_TYPE 0
89
Rich Evans18b78c72015-02-11 14:06:19 +000090#define USAGE \
91 "\n usage: cert_write param=<>...\n" \
92 "\n acceptable parameters:\n" \
93 USAGE_CSR \
94 " subject_key=%%s default: subject.key\n" \
95 " subject_pwd=%%s default: (empty)\n" \
96 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
97 "\n" \
98 " issuer_crt=%%s default: (empty)\n" \
99 " If issuer_crt is specified, issuer_name is\n" \
100 " ignored!\n" \
101 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
102 "\n" \
103 " selfsign=%%d default: 0 (false)\n" \
104 " If selfsign is enabled, issuer_name and\n" \
105 " issuer_key are required (issuer_crt and\n" \
106 " subject_* are ignored\n" \
107 " issuer_key=%%s default: ca.key\n" \
108 " issuer_pwd=%%s default: (empty)\n" \
109 " output_file=%%s default: cert.crt\n" \
110 " serial=%%s default: 1\n" \
111 " not_before=%%s default: 20010101000000\n"\
112 " not_after=%%s default: 20301231235959\n"\
113 " is_ca=%%d default: 0 (disabled)\n" \
114 " max_pathlen=%%d default: -1 (none)\n" \
115 " key_usage=%%s default: (empty)\n" \
116 " Comma-separated-list of values:\n" \
117 " digital_signature\n" \
118 " non_repudiation\n" \
119 " key_encipherment\n" \
120 " data_encipherment\n" \
121 " key_agreement\n" \
122 " key_certificate_sign\n" \
123 " crl_sign\n" \
124 " ns_cert_type=%%s default: (empty)\n" \
125 " Comma-separated-list of values:\n" \
126 " ssl_client\n" \
127 " ssl_server\n" \
128 " email\n" \
129 " object_signing\n" \
130 " ssl_ca\n" \
131 " email_ca\n" \
132 " object_signing_ca\n" \
133 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000134
Paul Bakker9397dcb2013-09-06 09:55:26 +0200135/*
136 * global options
137 */
138struct options
139{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100140 const char *issuer_crt; /* filename of the issuer certificate */
141 const char *request_file; /* filename of the certificate request */
142 const char *subject_key; /* filename of the subject key file */
143 const char *issuer_key; /* filename of the issuer key file */
144 const char *subject_pwd; /* password for the subject key file */
145 const char *issuer_pwd; /* password for the issuer key file */
146 const char *output_file; /* where to store the constructed key file */
147 const char *subject_name; /* subject name for certificate */
148 const char *issuer_name; /* issuer name for certificate */
149 const char *not_before; /* validity period not before */
150 const char *not_after; /* validity period not after */
151 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200152 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200153 int is_ca; /* is a CA certificate */
154 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200155 unsigned char key_usage; /* key usage flags */
156 unsigned char ns_cert_type; /* NS cert type */
157} opt;
158
Paul Bakker8fc30b12013-11-25 13:29:43 +0100159int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200160 int (*f_rng)(void *, unsigned char *, size_t),
161 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200162{
163 int ret;
164 FILE *f;
165 unsigned char output_buf[4096];
166 size_t len = 0;
167
168 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200169 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200170 return( ret );
171
172 len = strlen( (char *) output_buf );
173
174 if( ( f = fopen( output_file, "w" ) ) == NULL )
175 return( -1 );
176
177 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200178 {
179 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200180 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200181 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200182
Paul Bakker0c226102014-04-17 16:02:36 +0200183 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184
185 return( 0 );
186}
187
Paul Bakker9397dcb2013-09-06 09:55:26 +0200188int main( int argc, char *argv[] )
189{
190 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200191 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200192 pk_context loaded_issuer_key, loaded_subject_key;
193 pk_context *issuer_key = &loaded_issuer_key,
194 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 char buf[1024];
Jonathan Leroy2744df42015-10-10 21:58:07 +0200196 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100197 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200198 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200199#if defined(POLARSSL_X509_CSR_PARSE_C)
Jonathan Leroy2744df42015-10-10 21:58:07 +0200200 char subject_name[256];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200201 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200202#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 x509write_cert crt;
204 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200205 entropy_context entropy;
206 ctr_drbg_context ctr_drbg;
207 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208
209 /*
210 * Set to sane values
211 */
212 x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000213 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA256 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200214 pk_init( &loaded_issuer_key );
215 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200217#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200218 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200219#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200220 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221 memset( buf, 0, 1024 );
222
223 if( argc == 0 )
224 {
225 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 ret = 1;
228 goto exit;
229 }
230
Paul Bakker1014e952013-09-09 13:59:42 +0200231 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200232 opt.request_file = DFL_REQUEST_FILE;
233 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200234 opt.subject_key = DFL_SUBJECT_KEY;
235 opt.issuer_key = DFL_ISSUER_KEY;
236 opt.subject_pwd = DFL_SUBJECT_PWD;
237 opt.issuer_pwd = DFL_ISSUER_PWD;
238 opt.output_file = DFL_OUTPUT_FILENAME;
239 opt.subject_name = DFL_SUBJECT_NAME;
240 opt.issuer_name = DFL_ISSUER_NAME;
241 opt.not_before = DFL_NOT_BEFORE;
242 opt.not_after = DFL_NOT_AFTER;
243 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200244 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200245 opt.is_ca = DFL_IS_CA;
246 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200247 opt.key_usage = DFL_KEY_USAGE;
248 opt.ns_cert_type = DFL_NS_CERT_TYPE;
249
250 for( i = 1; i < argc; i++ )
251 {
252
253 p = argv[i];
254 if( ( q = strchr( p, '=' ) ) == NULL )
255 goto usage;
256 *q++ = '\0';
257
Paul Bakkere2673fb2013-09-09 15:52:07 +0200258 if( strcmp( p, "request_file" ) == 0 )
259 opt.request_file = q;
260 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200261 opt.subject_key = q;
262 else if( strcmp( p, "issuer_key" ) == 0 )
263 opt.issuer_key = q;
264 else if( strcmp( p, "subject_pwd" ) == 0 )
265 opt.subject_pwd = q;
266 else if( strcmp( p, "issuer_pwd" ) == 0 )
267 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200268 else if( strcmp( p, "issuer_crt" ) == 0 )
269 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200270 else if( strcmp( p, "output_file" ) == 0 )
271 opt.output_file = q;
272 else if( strcmp( p, "subject_name" ) == 0 )
273 {
274 opt.subject_name = q;
275 }
276 else if( strcmp( p, "issuer_name" ) == 0 )
277 {
278 opt.issuer_name = q;
279 }
280 else if( strcmp( p, "not_before" ) == 0 )
281 {
282 opt.not_before = q;
283 }
284 else if( strcmp( p, "not_after" ) == 0 )
285 {
286 opt.not_after = q;
287 }
288 else if( strcmp( p, "serial" ) == 0 )
289 {
290 opt.serial = q;
291 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200292 else if( strcmp( p, "selfsign" ) == 0 )
293 {
294 opt.selfsign = atoi( q );
295 if( opt.selfsign < 0 || opt.selfsign > 1 )
296 goto usage;
297 }
Paul Bakker15162a02013-09-06 19:27:21 +0200298 else if( strcmp( p, "is_ca" ) == 0 )
299 {
300 opt.is_ca = atoi( q );
301 if( opt.is_ca < 0 || opt.is_ca > 1 )
302 goto usage;
303 }
304 else if( strcmp( p, "max_pathlen" ) == 0 )
305 {
306 opt.max_pathlen = atoi( q );
307 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
308 goto usage;
309 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200310 else if( strcmp( p, "key_usage" ) == 0 )
311 {
312 while( q != NULL )
313 {
314 if( ( r = strchr( q, ',' ) ) != NULL )
315 *r++ = '\0';
316
317 if( strcmp( q, "digital_signature" ) == 0 )
318 opt.key_usage |= KU_DIGITAL_SIGNATURE;
319 else if( strcmp( q, "non_repudiation" ) == 0 )
320 opt.key_usage |= KU_NON_REPUDIATION;
321 else if( strcmp( q, "key_encipherment" ) == 0 )
322 opt.key_usage |= KU_KEY_ENCIPHERMENT;
323 else if( strcmp( q, "data_encipherment" ) == 0 )
324 opt.key_usage |= KU_DATA_ENCIPHERMENT;
325 else if( strcmp( q, "key_agreement" ) == 0 )
326 opt.key_usage |= KU_KEY_AGREEMENT;
327 else if( strcmp( q, "key_cert_sign" ) == 0 )
328 opt.key_usage |= KU_KEY_CERT_SIGN;
329 else if( strcmp( q, "crl_sign" ) == 0 )
330 opt.key_usage |= KU_CRL_SIGN;
331 else
332 goto usage;
333
334 q = r;
335 }
336 }
337 else if( strcmp( p, "ns_cert_type" ) == 0 )
338 {
339 while( q != NULL )
340 {
341 if( ( r = strchr( q, ',' ) ) != NULL )
342 *r++ = '\0';
343
344 if( strcmp( q, "ssl_client" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
346 else if( strcmp( q, "ssl_server" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
348 else if( strcmp( q, "email" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
350 else if( strcmp( q, "object_signing" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
352 else if( strcmp( q, "ssl_ca" ) == 0 )
353 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
354 else if( strcmp( q, "email_ca" ) == 0 )
355 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
356 else if( strcmp( q, "object_signing_ca" ) == 0 )
357 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
358 else
359 goto usage;
360
361 q = r;
362 }
363 }
364 else
365 goto usage;
366 }
367
Rich Evansf90016a2015-01-19 14:26:37 +0000368 polarssl_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200369
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200370 /*
371 * 0. Seed the PRNG
372 */
Rich Evansf90016a2015-01-19 14:26:37 +0000373 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200374 fflush( stdout );
375
376 entropy_init( &entropy );
377 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
378 (const unsigned char *) pers,
379 strlen( pers ) ) ) != 0 )
380 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700381 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000382 polarssl_printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200383 goto exit;
384 }
385
Rich Evansf90016a2015-01-19 14:26:37 +0000386 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200387
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388 // Parse serial to MPI
389 //
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200391 fflush( stdout );
392
Paul Bakker9397dcb2013-09-06 09:55:26 +0200393 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
394 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700395 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000396 polarssl_printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200397 goto exit;
398 }
399
Rich Evansf90016a2015-01-19 14:26:37 +0000400 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200401
Paul Bakker1014e952013-09-09 13:59:42 +0200402 // Parse issuer certificate if present
403 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200404 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200405 {
406 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200407 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200408 */
Rich Evansf90016a2015-01-19 14:26:37 +0000409 polarssl_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200410 fflush( stdout );
411
Paul Bakkerddf26b42013-09-18 13:46:23 +0200412 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200413 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700414 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000415 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200416 goto exit;
417 }
418
Paul Bakker86d0c192013-09-18 11:11:02 +0200419 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200420 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200421 if( ret < 0 )
422 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700423 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000424 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200425 goto exit;
426 }
427
Paul Bakkere2673fb2013-09-09 15:52:07 +0200428 opt.issuer_name = issuer_name;
429
Rich Evansf90016a2015-01-19 14:26:37 +0000430 polarssl_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200431 }
432
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200433#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 // Parse certificate request if present
435 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200436 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 {
438 /*
439 * 1.0.b. Load the CSR
440 */
Rich Evansf90016a2015-01-19 14:26:37 +0000441 polarssl_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200442 fflush( stdout );
443
Paul Bakkerddf26b42013-09-18 13:46:23 +0200444 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200445 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700446 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000447 polarssl_printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200448 goto exit;
449 }
450
Paul Bakker86d0c192013-09-18 11:11:02 +0200451 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200452 &csr.subject );
453 if( ret < 0 )
454 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700455 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000456 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200457 goto exit;
458 }
459
460 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200461 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200462
Rich Evansf90016a2015-01-19 14:26:37 +0000463 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200464 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200465#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200466
467 /*
468 * 1.1. Load the keys
469 */
470 if( !opt.selfsign && !strlen( opt.request_file ) )
471 {
Rich Evansf90016a2015-01-19 14:26:37 +0000472 polarssl_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200473 fflush( stdout );
474
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200476 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200477 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200478 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700479 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000480 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200481 goto exit;
482 }
Paul Bakker1014e952013-09-09 13:59:42 +0200483
Rich Evansf90016a2015-01-19 14:26:37 +0000484 polarssl_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200485 }
486
Rich Evansf90016a2015-01-19 14:26:37 +0000487 polarssl_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200488 fflush( stdout );
489
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
491 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200492 if( ret != 0 )
493 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700494 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000495 polarssl_printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200496 goto exit;
497 }
498
499 // Check if key and issuer certificate match
500 //
501 if( strlen( opt.issuer_crt ) )
502 {
503 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200504 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
505 &pk_rsa( *issuer_key )->N ) != 0 ||
506 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
507 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 {
Rich Evansf90016a2015-01-19 14:26:37 +0000509 polarssl_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200510 ret = -1;
511 goto exit;
512 }
513 }
514
Rich Evansf90016a2015-01-19 14:26:37 +0000515 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200516
517 if( opt.selfsign )
518 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100519 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200520 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200521 }
522
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200523 x509write_crt_set_subject_key( &crt, subject_key );
524 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200525
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 /*
527 * 1.0. Check the names for validity
528 */
529 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
530 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700531 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000532 polarssl_printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200533 goto exit;
534 }
535
536 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
537 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700538 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000539 polarssl_printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200540 goto exit;
541 }
542
Rich Evansf90016a2015-01-19 14:26:37 +0000543 polarssl_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 fflush( stdout );
545
546 ret = x509write_crt_set_serial( &crt, &serial );
547 if( ret != 0 )
548 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700549 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000550 polarssl_printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200551 goto exit;
552 }
553
554 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
555 if( ret != 0 )
556 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700557 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000558 polarssl_printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200559 goto exit;
560 }
561
Rich Evansf90016a2015-01-19 14:26:37 +0000562 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200563
Rich Evansf90016a2015-01-19 14:26:37 +0000564 polarssl_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200565 fflush( stdout );
566
567 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
568 opt.max_pathlen );
569 if( ret != 0 )
570 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700571 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000572 polarssl_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200573 goto exit;
574 }
575
Rich Evansf90016a2015-01-19 14:26:37 +0000576 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200577
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100578#if defined(POLARSSL_SHA1_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000579 polarssl_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200580 fflush( stdout );
581
582 ret = x509write_crt_set_subject_key_identifier( &crt );
583 if( ret != 0 )
584 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700585 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000586 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 +0200587 goto exit;
588 }
589
Rich Evansf90016a2015-01-19 14:26:37 +0000590 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200591
Rich Evansf90016a2015-01-19 14:26:37 +0000592 polarssl_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200593 fflush( stdout );
594
595 ret = x509write_crt_set_authority_key_identifier( &crt );
596 if( ret != 0 )
597 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700598 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000599 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 +0200600 goto exit;
601 }
602
Rich Evansf90016a2015-01-19 14:26:37 +0000603 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100604#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200605
Paul Bakker52be08c2013-09-09 12:37:54 +0200606 if( opt.key_usage )
607 {
Rich Evansf90016a2015-01-19 14:26:37 +0000608 polarssl_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200609 fflush( stdout );
610
611 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
612 if( ret != 0 )
613 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700614 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000615 polarssl_printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200616 goto exit;
617 }
618
Rich Evansf90016a2015-01-19 14:26:37 +0000619 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200620 }
621
622 if( opt.ns_cert_type )
623 {
Rich Evansf90016a2015-01-19 14:26:37 +0000624 polarssl_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200625 fflush( stdout );
626
627 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
628 if( ret != 0 )
629 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700630 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000631 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 +0200632 goto exit;
633 }
634
Rich Evansf90016a2015-01-19 14:26:37 +0000635 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200636 }
637
Paul Bakker9397dcb2013-09-06 09:55:26 +0200638 /*
639 * 1.2. Writing the request
640 */
Rich Evansf90016a2015-01-19 14:26:37 +0000641 polarssl_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 fflush( stdout );
643
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200644 if( ( ret = write_certificate( &crt, opt.output_file,
645 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700647 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000648 polarssl_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200649 goto exit;
650 }
651
Rich Evansf90016a2015-01-19 14:26:37 +0000652 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200653
654exit:
655 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200656 pk_free( &loaded_subject_key );
657 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200658 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200659 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200660 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200661
662#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000663 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200664 fflush( stdout ); getchar();
665#endif
666
667 return( ret );
668}
Paul Bakker36713e82013-09-17 13:25:29 +0200669#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
670 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Simon Butcher8c9ab6c2016-10-10 23:23:41 +0100671 POLARSSL_ERROR_C && MBEDTLS_PEM_WRITE_C */