blob: 629051bc8aa9480e6a107af93847538448144dbd [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) || \
39 !defined(POLARSSL_ERROR_C)
40int main( void )
41{
42 polarssl_printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
43 "POLARSSL_FS_IO and/or "
44 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
45 "POLARSSL_ERROR_C not defined.\n");
46 return( 0 );
47}
48#else
49
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020050#include "polarssl/x509_crt.h"
51#include "polarssl/x509_csr.h"
52#include "polarssl/entropy.h"
53#include "polarssl/ctr_drbg.h"
54#include "polarssl/error.h"
55
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
60#if defined(POLARSSL_X509_CSR_PARSE_C)
61#define USAGE_CSR \
62 " request_file=%%s default: (empty)\n" \
63 " If request_file is specified, subject_key,\n" \
64 " subject_pwd and subject_name are ignored!\n"
65#else
66#define USAGE_CSR ""
67#endif /* POLARSSL_X509_CSR_PARSE_C */
68
Paul Bakker1014e952013-09-09 13:59:42 +020069#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020070#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_SUBJECT_KEY "subject.key"
72#define DFL_ISSUER_KEY "ca.key"
73#define DFL_SUBJECT_PWD ""
74#define DFL_ISSUER_PWD ""
75#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000076#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_NOT_BEFORE "20010101000000"
79#define DFL_NOT_AFTER "20301231235959"
80#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020081#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020082#define DFL_IS_CA 0
83#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020084#define DFL_KEY_USAGE 0
85#define DFL_NS_CERT_TYPE 0
86
Rich Evans18b78c72015-02-11 14:06:19 +000087#define USAGE \
88 "\n usage: cert_write param=<>...\n" \
89 "\n acceptable parameters:\n" \
90 USAGE_CSR \
91 " subject_key=%%s default: subject.key\n" \
92 " subject_pwd=%%s default: (empty)\n" \
93 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
94 "\n" \
95 " issuer_crt=%%s default: (empty)\n" \
96 " If issuer_crt is specified, issuer_name is\n" \
97 " ignored!\n" \
98 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
99 "\n" \
100 " selfsign=%%d default: 0 (false)\n" \
101 " If selfsign is enabled, issuer_name and\n" \
102 " issuer_key are required (issuer_crt and\n" \
103 " subject_* are ignored\n" \
104 " issuer_key=%%s default: ca.key\n" \
105 " issuer_pwd=%%s default: (empty)\n" \
106 " output_file=%%s default: cert.crt\n" \
107 " serial=%%s default: 1\n" \
108 " not_before=%%s default: 20010101000000\n"\
109 " not_after=%%s default: 20301231235959\n"\
110 " is_ca=%%d default: 0 (disabled)\n" \
111 " max_pathlen=%%d default: -1 (none)\n" \
112 " key_usage=%%s default: (empty)\n" \
113 " Comma-separated-list of values:\n" \
114 " digital_signature\n" \
115 " non_repudiation\n" \
116 " key_encipherment\n" \
117 " data_encipherment\n" \
118 " key_agreement\n" \
119 " key_certificate_sign\n" \
120 " crl_sign\n" \
121 " ns_cert_type=%%s default: (empty)\n" \
122 " Comma-separated-list of values:\n" \
123 " ssl_client\n" \
124 " ssl_server\n" \
125 " email\n" \
126 " object_signing\n" \
127 " ssl_ca\n" \
128 " email_ca\n" \
129 " object_signing_ca\n" \
130 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000131
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132/*
133 * global options
134 */
135struct options
136{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100137 const char *issuer_crt; /* filename of the issuer certificate */
138 const char *request_file; /* filename of the certificate request */
139 const char *subject_key; /* filename of the subject key file */
140 const char *issuer_key; /* filename of the issuer key file */
141 const char *subject_pwd; /* password for the subject key file */
142 const char *issuer_pwd; /* password for the issuer key file */
143 const char *output_file; /* where to store the constructed key file */
144 const char *subject_name; /* subject name for certificate */
145 const char *issuer_name; /* issuer name for certificate */
146 const char *not_before; /* validity period not before */
147 const char *not_after; /* validity period not after */
148 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200149 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200150 int is_ca; /* is a CA certificate */
151 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152 unsigned char key_usage; /* key usage flags */
153 unsigned char ns_cert_type; /* NS cert type */
154} opt;
155
Paul Bakker8fc30b12013-11-25 13:29:43 +0100156int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200157 int (*f_rng)(void *, unsigned char *, size_t),
158 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200159{
160 int ret;
161 FILE *f;
162 unsigned char output_buf[4096];
163 size_t len = 0;
164
165 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200166 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167 return( ret );
168
169 len = strlen( (char *) output_buf );
170
171 if( ( f = fopen( output_file, "w" ) ) == NULL )
172 return( -1 );
173
174 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200175 {
176 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200177 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200178 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200179
Paul Bakker0c226102014-04-17 16:02:36 +0200180 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181
182 return( 0 );
183}
184
Paul Bakker9397dcb2013-09-06 09:55:26 +0200185int main( int argc, char *argv[] )
186{
187 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200188 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200189 pk_context loaded_issuer_key, loaded_subject_key;
190 pk_context *issuer_key = &loaded_issuer_key,
191 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200193 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100194 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200195 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200196#if defined(POLARSSL_X509_CSR_PARSE_C)
197 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200198 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200199#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200 x509write_cert crt;
201 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200202 entropy_context entropy;
203 ctr_drbg_context ctr_drbg;
204 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205
206 /*
207 * Set to sane values
208 */
209 x509write_crt_init( &crt );
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000210 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA256 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200211 pk_init( &loaded_issuer_key );
212 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200214#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200215 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200216#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200217 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 memset( buf, 0, 1024 );
219
220 if( argc == 0 )
221 {
222 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( USAGE );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200224 ret = 1;
225 goto exit;
226 }
227
Paul Bakker1014e952013-09-09 13:59:42 +0200228 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200229 opt.request_file = DFL_REQUEST_FILE;
230 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231 opt.subject_key = DFL_SUBJECT_KEY;
232 opt.issuer_key = DFL_ISSUER_KEY;
233 opt.subject_pwd = DFL_SUBJECT_PWD;
234 opt.issuer_pwd = DFL_ISSUER_PWD;
235 opt.output_file = DFL_OUTPUT_FILENAME;
236 opt.subject_name = DFL_SUBJECT_NAME;
237 opt.issuer_name = DFL_ISSUER_NAME;
238 opt.not_before = DFL_NOT_BEFORE;
239 opt.not_after = DFL_NOT_AFTER;
240 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200241 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200242 opt.is_ca = DFL_IS_CA;
243 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 opt.key_usage = DFL_KEY_USAGE;
245 opt.ns_cert_type = DFL_NS_CERT_TYPE;
246
247 for( i = 1; i < argc; i++ )
248 {
249
250 p = argv[i];
251 if( ( q = strchr( p, '=' ) ) == NULL )
252 goto usage;
253 *q++ = '\0';
254
Paul Bakkere2673fb2013-09-09 15:52:07 +0200255 if( strcmp( p, "request_file" ) == 0 )
256 opt.request_file = q;
257 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 opt.subject_key = q;
259 else if( strcmp( p, "issuer_key" ) == 0 )
260 opt.issuer_key = q;
261 else if( strcmp( p, "subject_pwd" ) == 0 )
262 opt.subject_pwd = q;
263 else if( strcmp( p, "issuer_pwd" ) == 0 )
264 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200265 else if( strcmp( p, "issuer_crt" ) == 0 )
266 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200267 else if( strcmp( p, "output_file" ) == 0 )
268 opt.output_file = q;
269 else if( strcmp( p, "subject_name" ) == 0 )
270 {
271 opt.subject_name = q;
272 }
273 else if( strcmp( p, "issuer_name" ) == 0 )
274 {
275 opt.issuer_name = q;
276 }
277 else if( strcmp( p, "not_before" ) == 0 )
278 {
279 opt.not_before = q;
280 }
281 else if( strcmp( p, "not_after" ) == 0 )
282 {
283 opt.not_after = q;
284 }
285 else if( strcmp( p, "serial" ) == 0 )
286 {
287 opt.serial = q;
288 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200289 else if( strcmp( p, "selfsign" ) == 0 )
290 {
291 opt.selfsign = atoi( q );
292 if( opt.selfsign < 0 || opt.selfsign > 1 )
293 goto usage;
294 }
Paul Bakker15162a02013-09-06 19:27:21 +0200295 else if( strcmp( p, "is_ca" ) == 0 )
296 {
297 opt.is_ca = atoi( q );
298 if( opt.is_ca < 0 || opt.is_ca > 1 )
299 goto usage;
300 }
301 else if( strcmp( p, "max_pathlen" ) == 0 )
302 {
303 opt.max_pathlen = atoi( q );
304 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
305 goto usage;
306 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200307 else if( strcmp( p, "key_usage" ) == 0 )
308 {
309 while( q != NULL )
310 {
311 if( ( r = strchr( q, ',' ) ) != NULL )
312 *r++ = '\0';
313
314 if( strcmp( q, "digital_signature" ) == 0 )
315 opt.key_usage |= KU_DIGITAL_SIGNATURE;
316 else if( strcmp( q, "non_repudiation" ) == 0 )
317 opt.key_usage |= KU_NON_REPUDIATION;
318 else if( strcmp( q, "key_encipherment" ) == 0 )
319 opt.key_usage |= KU_KEY_ENCIPHERMENT;
320 else if( strcmp( q, "data_encipherment" ) == 0 )
321 opt.key_usage |= KU_DATA_ENCIPHERMENT;
322 else if( strcmp( q, "key_agreement" ) == 0 )
323 opt.key_usage |= KU_KEY_AGREEMENT;
324 else if( strcmp( q, "key_cert_sign" ) == 0 )
325 opt.key_usage |= KU_KEY_CERT_SIGN;
326 else if( strcmp( q, "crl_sign" ) == 0 )
327 opt.key_usage |= KU_CRL_SIGN;
328 else
329 goto usage;
330
331 q = r;
332 }
333 }
334 else if( strcmp( p, "ns_cert_type" ) == 0 )
335 {
336 while( q != NULL )
337 {
338 if( ( r = strchr( q, ',' ) ) != NULL )
339 *r++ = '\0';
340
341 if( strcmp( q, "ssl_client" ) == 0 )
342 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
343 else if( strcmp( q, "ssl_server" ) == 0 )
344 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
345 else if( strcmp( q, "email" ) == 0 )
346 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
347 else if( strcmp( q, "object_signing" ) == 0 )
348 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
349 else if( strcmp( q, "ssl_ca" ) == 0 )
350 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
351 else if( strcmp( q, "email_ca" ) == 0 )
352 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
353 else if( strcmp( q, "object_signing_ca" ) == 0 )
354 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
355 else
356 goto usage;
357
358 q = r;
359 }
360 }
361 else
362 goto usage;
363 }
364
Rich Evansf90016a2015-01-19 14:26:37 +0000365 polarssl_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200366
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200367 /*
368 * 0. Seed the PRNG
369 */
Rich Evansf90016a2015-01-19 14:26:37 +0000370 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200371 fflush( stdout );
372
373 entropy_init( &entropy );
374 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
375 (const unsigned char *) pers,
376 strlen( pers ) ) ) != 0 )
377 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700378 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000379 polarssl_printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200380 goto exit;
381 }
382
Rich Evansf90016a2015-01-19 14:26:37 +0000383 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200384
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 // Parse serial to MPI
386 //
Rich Evansf90016a2015-01-19 14:26:37 +0000387 polarssl_printf( " . Reading serial number..." );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200388 fflush( stdout );
389
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
391 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700392 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 goto exit;
395 }
396
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200398
Paul Bakker1014e952013-09-09 13:59:42 +0200399 // Parse issuer certificate if present
400 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200401 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200402 {
403 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200404 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200405 */
Rich Evansf90016a2015-01-19 14:26:37 +0000406 polarssl_printf( " . Loading the issuer certificate ..." );
Paul Bakker1014e952013-09-09 13:59:42 +0200407 fflush( stdout );
408
Paul Bakkerddf26b42013-09-18 13:46:23 +0200409 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200410 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700411 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000412 polarssl_printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200413 goto exit;
414 }
415
Paul Bakker86d0c192013-09-18 11:11:02 +0200416 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200417 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200418 if( ret < 0 )
419 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700420 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000421 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200422 goto exit;
423 }
424
Paul Bakkere2673fb2013-09-09 15:52:07 +0200425 opt.issuer_name = issuer_name;
426
Rich Evansf90016a2015-01-19 14:26:37 +0000427 polarssl_printf( " ok\n" );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200428 }
429
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200430#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200431 // Parse certificate request if present
432 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200433 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 {
435 /*
436 * 1.0.b. Load the CSR
437 */
Rich Evansf90016a2015-01-19 14:26:37 +0000438 polarssl_printf( " . Loading the certificate request ..." );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200439 fflush( stdout );
440
Paul Bakkerddf26b42013-09-18 13:46:23 +0200441 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200442 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700443 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000444 polarssl_printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200445 goto exit;
446 }
447
Paul Bakker86d0c192013-09-18 11:11:02 +0200448 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449 &csr.subject );
450 if( ret < 0 )
451 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700452 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000453 polarssl_printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200454 goto exit;
455 }
456
457 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200458 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200459
Rich Evansf90016a2015-01-19 14:26:37 +0000460 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200461 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200462#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200463
464 /*
465 * 1.1. Load the keys
466 */
467 if( !opt.selfsign && !strlen( opt.request_file ) )
468 {
Rich Evansf90016a2015-01-19 14:26:37 +0000469 polarssl_printf( " . Loading the subject key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200470 fflush( stdout );
471
Paul Bakker1a7550a2013-09-15 13:01:22 +0200472 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200473 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200474 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200475 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700476 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000477 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200478 goto exit;
479 }
Paul Bakker1014e952013-09-09 13:59:42 +0200480
Rich Evansf90016a2015-01-19 14:26:37 +0000481 polarssl_printf( " ok\n" );
Paul Bakker1014e952013-09-09 13:59:42 +0200482 }
483
Rich Evansf90016a2015-01-19 14:26:37 +0000484 polarssl_printf( " . Loading the issuer key ..." );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200485 fflush( stdout );
486
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
488 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200489 if( ret != 0 )
490 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700491 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000492 polarssl_printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200493 goto exit;
494 }
495
496 // Check if key and issuer certificate match
497 //
498 if( strlen( opt.issuer_crt ) )
499 {
500 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200501 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
502 &pk_rsa( *issuer_key )->N ) != 0 ||
503 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
504 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200505 {
Rich Evansf90016a2015-01-19 14:26:37 +0000506 polarssl_printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200507 ret = -1;
508 goto exit;
509 }
510 }
511
Rich Evansf90016a2015-01-19 14:26:37 +0000512 polarssl_printf( " ok\n" );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200513
514 if( opt.selfsign )
515 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100516 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200517 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200518 }
519
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200520 x509write_crt_set_subject_key( &crt, subject_key );
521 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200522
Paul Bakker9397dcb2013-09-06 09:55:26 +0200523 /*
524 * 1.0. Check the names for validity
525 */
526 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
527 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700528 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000529 polarssl_printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200530 goto exit;
531 }
532
533 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_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_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200537 goto exit;
538 }
539
Rich Evansf90016a2015-01-19 14:26:37 +0000540 polarssl_printf( " . Setting certificate values ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200541 fflush( stdout );
542
543 ret = x509write_crt_set_serial( &crt, &serial );
544 if( ret != 0 )
545 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700546 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000547 polarssl_printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200548 goto exit;
549 }
550
551 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
552 if( ret != 0 )
553 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700554 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000555 polarssl_printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200556 goto exit;
557 }
558
Rich Evansf90016a2015-01-19 14:26:37 +0000559 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200560
Rich Evansf90016a2015-01-19 14:26:37 +0000561 polarssl_printf( " . Adding the Basic Constraints extension ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200562 fflush( stdout );
563
564 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
565 opt.max_pathlen );
566 if( ret != 0 )
567 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700568 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000569 polarssl_printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker15162a02013-09-06 19:27:21 +0200570 goto exit;
571 }
572
Rich Evansf90016a2015-01-19 14:26:37 +0000573 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200574
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100575#if defined(POLARSSL_SHA1_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000576 polarssl_printf( " . Adding the Subject Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200577 fflush( stdout );
578
579 ret = x509write_crt_set_subject_key_identifier( &crt );
580 if( ret != 0 )
581 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700582 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000583 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 +0200584 goto exit;
585 }
586
Rich Evansf90016a2015-01-19 14:26:37 +0000587 polarssl_printf( " ok\n" );
Paul Bakker15162a02013-09-06 19:27:21 +0200588
Rich Evansf90016a2015-01-19 14:26:37 +0000589 polarssl_printf( " . Adding the Authority Key Identifier ..." );
Paul Bakker15162a02013-09-06 19:27:21 +0200590 fflush( stdout );
591
592 ret = x509write_crt_set_authority_key_identifier( &crt );
593 if( ret != 0 )
594 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700595 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000596 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 +0200597 goto exit;
598 }
599
Rich Evansf90016a2015-01-19 14:26:37 +0000600 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100601#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200602
Paul Bakker52be08c2013-09-09 12:37:54 +0200603 if( opt.key_usage )
604 {
Rich Evansf90016a2015-01-19 14:26:37 +0000605 polarssl_printf( " . Adding the Key Usage extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200606 fflush( stdout );
607
608 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
609 if( ret != 0 )
610 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700611 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000612 polarssl_printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker52be08c2013-09-09 12:37:54 +0200613 goto exit;
614 }
615
Rich Evansf90016a2015-01-19 14:26:37 +0000616 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200617 }
618
619 if( opt.ns_cert_type )
620 {
Rich Evansf90016a2015-01-19 14:26:37 +0000621 polarssl_printf( " . Adding the NS Cert Type extension ..." );
Paul Bakker52be08c2013-09-09 12:37:54 +0200622 fflush( stdout );
623
624 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
625 if( ret != 0 )
626 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700627 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000628 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 +0200629 goto exit;
630 }
631
Rich Evansf90016a2015-01-19 14:26:37 +0000632 polarssl_printf( " ok\n" );
Paul Bakker52be08c2013-09-09 12:37:54 +0200633 }
634
Paul Bakker9397dcb2013-09-06 09:55:26 +0200635 /*
636 * 1.2. Writing the request
637 */
Rich Evansf90016a2015-01-19 14:26:37 +0000638 polarssl_printf( " . Writing the certificate..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200639 fflush( stdout );
640
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200641 if( ( ret = write_certificate( &crt, opt.output_file,
642 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200643 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700644 polarssl_strerror( ret, buf, 1024 );
Rich Evansf90016a2015-01-19 14:26:37 +0000645 polarssl_printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200646 goto exit;
647 }
648
Rich Evansf90016a2015-01-19 14:26:37 +0000649 polarssl_printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200650
651exit:
652 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200653 pk_free( &loaded_subject_key );
654 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200655 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200656 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200657 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200658
659#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000660 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200661 fflush( stdout ); getchar();
662#endif
663
664 return( ret );
665}
Paul Bakker36713e82013-09-17 13:25:29 +0200666#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
667 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200668 POLARSSL_ERROR_C */