blob: 0591f3b0434c7855148fd4a5a88f433c6999d2f0 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker9397dcb2013-09-06 09:55:26 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker9397dcb2013-09-06 09:55:26 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +020029
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
35 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020036 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020037 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020038int main( int argc, char *argv[] )
39{
40 ((void) argc);
41 ((void) argv);
42
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043 printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020044 "POLARSSL_FS_IO and/or "
45 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
46 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020047 return( 0 );
48}
49#else
50
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020051#include "polarssl/x509_crt.h"
52#include "polarssl/x509_csr.h"
53#include "polarssl/entropy.h"
54#include "polarssl/ctr_drbg.h"
55#include "polarssl/error.h"
56
Paul Bakker1014e952013-09-09 13:59:42 +020057#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020058#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020059#define DFL_SUBJECT_KEY "subject.key"
60#define DFL_ISSUER_KEY "ca.key"
61#define DFL_SUBJECT_PWD ""
62#define DFL_ISSUER_PWD ""
63#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000064#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
65#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020066#define DFL_NOT_BEFORE "20010101000000"
67#define DFL_NOT_AFTER "20301231235959"
68#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020069#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020070#define DFL_IS_CA 0
71#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020072#define DFL_KEY_USAGE 0
73#define DFL_NS_CERT_TYPE 0
74
75/*
76 * global options
77 */
78struct options
79{
Paul Bakker8fc30b12013-11-25 13:29:43 +010080 const char *issuer_crt; /* filename of the issuer certificate */
81 const char *request_file; /* filename of the certificate request */
82 const char *subject_key; /* filename of the subject key file */
83 const char *issuer_key; /* filename of the issuer key file */
84 const char *subject_pwd; /* password for the subject key file */
85 const char *issuer_pwd; /* password for the issuer key file */
86 const char *output_file; /* where to store the constructed key file */
87 const char *subject_name; /* subject name for certificate */
88 const char *issuer_name; /* issuer name for certificate */
89 const char *not_before; /* validity period not before */
90 const char *not_after; /* validity period not after */
91 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020092 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020093 int is_ca; /* is a CA certificate */
94 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020095 unsigned char key_usage; /* key usage flags */
96 unsigned char ns_cert_type; /* NS cert type */
97} opt;
98
Paul Bakker8fc30b12013-11-25 13:29:43 +010099int write_certificate( x509write_cert *crt, const char *output_file,
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200100 int (*f_rng)(void *, unsigned char *, size_t),
101 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200102{
103 int ret;
104 FILE *f;
105 unsigned char output_buf[4096];
106 size_t len = 0;
107
108 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200109 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200110 return( ret );
111
112 len = strlen( (char *) output_buf );
113
114 if( ( f = fopen( output_file, "w" ) ) == NULL )
115 return( -1 );
116
117 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200118 {
119 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200120 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200121 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200122
Paul Bakker0c226102014-04-17 16:02:36 +0200123 fclose( f );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200124
125 return( 0 );
126}
127
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200128#if defined(POLARSSL_X509_CSR_PARSE_C)
129#define USAGE_CSR \
130 " request_file=%%s default: (empty)\n" \
131 " If request_file is specified, subject_key,\n" \
132 " subject_pwd and subject_name are ignored!\n"
133#else
134#define USAGE_CSR ""
135#endif /* POLARSSL_X509_CSR_PARSE_C */
136
Paul Bakker9397dcb2013-09-06 09:55:26 +0200137#define USAGE \
138 "\n usage: cert_write param=<>...\n" \
139 "\n acceptable parameters:\n" \
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200140 USAGE_CSR \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200141 " subject_key=%%s default: subject.key\n" \
142 " subject_pwd=%%s default: (empty)\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000143 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200144 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200145 " issuer_crt=%%s default: (empty)\n" \
146 " If issuer_crt is specified, issuer_name is\n" \
147 " ignored!\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000148 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200149 "\n" \
150 " selfsign=%%d default: 0 (false)\n" \
151 " If selfsign is enabled, issuer_name and\n" \
152 " issuer_key are required (issuer_crt and\n" \
153 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200154 " issuer_key=%%s default: ca.key\n" \
155 " issuer_pwd=%%s default: (empty)\n" \
156 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200157 " serial=%%s default: 1\n" \
158 " not_before=%%s default: 20010101000000\n"\
159 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200160 " is_ca=%%d default: 0 (disabled)\n" \
161 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200162 " key_usage=%%s default: (empty)\n" \
163 " Comma-separated-list of values:\n" \
164 " digital_signature\n" \
165 " non_repudiation\n" \
166 " key_encipherment\n" \
167 " data_encipherment\n" \
168 " key_agreement\n" \
169 " key_certificate_sign\n" \
170 " crl_sign\n" \
171 " ns_cert_type=%%s default: (empty)\n" \
172 " Comma-separated-list of values:\n" \
173 " ssl_client\n" \
174 " ssl_server\n" \
175 " email\n" \
176 " object_signing\n" \
177 " ssl_ca\n" \
178 " email_ca\n" \
179 " object_signing_ca\n" \
180 "\n"
181
182int main( int argc, char *argv[] )
183{
184 int ret = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200185 x509_crt issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200186 pk_context loaded_issuer_key, loaded_subject_key;
187 pk_context *issuer_key = &loaded_issuer_key,
188 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200190 char issuer_name[128];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100191 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192 char *p, *q, *r;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200193#if defined(POLARSSL_X509_CSR_PARSE_C)
194 char subject_name[128];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200195 x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200196#endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 x509write_cert crt;
198 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200199 entropy_context entropy;
200 ctr_drbg_context ctr_drbg;
201 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200202
203 /*
204 * Set to sane values
205 */
206 x509write_crt_init( &crt );
207 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200208 pk_init( &loaded_issuer_key );
209 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210 mpi_init( &serial );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200211#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakker369d2eb2013-09-18 11:58:25 +0200212 x509_csr_init( &csr );
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200213#endif
Paul Bakker369d2eb2013-09-18 11:58:25 +0200214 x509_crt_init( &issuer_crt );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 memset( buf, 0, 1024 );
216
217 if( argc == 0 )
218 {
219 usage:
220 printf( USAGE );
221 ret = 1;
222 goto exit;
223 }
224
Paul Bakker1014e952013-09-09 13:59:42 +0200225 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200226 opt.request_file = DFL_REQUEST_FILE;
227 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228 opt.subject_key = DFL_SUBJECT_KEY;
229 opt.issuer_key = DFL_ISSUER_KEY;
230 opt.subject_pwd = DFL_SUBJECT_PWD;
231 opt.issuer_pwd = DFL_ISSUER_PWD;
232 opt.output_file = DFL_OUTPUT_FILENAME;
233 opt.subject_name = DFL_SUBJECT_NAME;
234 opt.issuer_name = DFL_ISSUER_NAME;
235 opt.not_before = DFL_NOT_BEFORE;
236 opt.not_after = DFL_NOT_AFTER;
237 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200238 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200239 opt.is_ca = DFL_IS_CA;
240 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200241 opt.key_usage = DFL_KEY_USAGE;
242 opt.ns_cert_type = DFL_NS_CERT_TYPE;
243
244 for( i = 1; i < argc; i++ )
245 {
246
247 p = argv[i];
248 if( ( q = strchr( p, '=' ) ) == NULL )
249 goto usage;
250 *q++ = '\0';
251
Paul Bakkere2673fb2013-09-09 15:52:07 +0200252 if( strcmp( p, "request_file" ) == 0 )
253 opt.request_file = q;
254 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200255 opt.subject_key = q;
256 else if( strcmp( p, "issuer_key" ) == 0 )
257 opt.issuer_key = q;
258 else if( strcmp( p, "subject_pwd" ) == 0 )
259 opt.subject_pwd = q;
260 else if( strcmp( p, "issuer_pwd" ) == 0 )
261 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200262 else if( strcmp( p, "issuer_crt" ) == 0 )
263 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200264 else if( strcmp( p, "output_file" ) == 0 )
265 opt.output_file = q;
266 else if( strcmp( p, "subject_name" ) == 0 )
267 {
268 opt.subject_name = q;
269 }
270 else if( strcmp( p, "issuer_name" ) == 0 )
271 {
272 opt.issuer_name = q;
273 }
274 else if( strcmp( p, "not_before" ) == 0 )
275 {
276 opt.not_before = q;
277 }
278 else if( strcmp( p, "not_after" ) == 0 )
279 {
280 opt.not_after = q;
281 }
282 else if( strcmp( p, "serial" ) == 0 )
283 {
284 opt.serial = q;
285 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200286 else if( strcmp( p, "selfsign" ) == 0 )
287 {
288 opt.selfsign = atoi( q );
289 if( opt.selfsign < 0 || opt.selfsign > 1 )
290 goto usage;
291 }
Paul Bakker15162a02013-09-06 19:27:21 +0200292 else if( strcmp( p, "is_ca" ) == 0 )
293 {
294 opt.is_ca = atoi( q );
295 if( opt.is_ca < 0 || opt.is_ca > 1 )
296 goto usage;
297 }
298 else if( strcmp( p, "max_pathlen" ) == 0 )
299 {
300 opt.max_pathlen = atoi( q );
301 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
302 goto usage;
303 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200304 else if( strcmp( p, "key_usage" ) == 0 )
305 {
306 while( q != NULL )
307 {
308 if( ( r = strchr( q, ',' ) ) != NULL )
309 *r++ = '\0';
310
311 if( strcmp( q, "digital_signature" ) == 0 )
312 opt.key_usage |= KU_DIGITAL_SIGNATURE;
313 else if( strcmp( q, "non_repudiation" ) == 0 )
314 opt.key_usage |= KU_NON_REPUDIATION;
315 else if( strcmp( q, "key_encipherment" ) == 0 )
316 opt.key_usage |= KU_KEY_ENCIPHERMENT;
317 else if( strcmp( q, "data_encipherment" ) == 0 )
318 opt.key_usage |= KU_DATA_ENCIPHERMENT;
319 else if( strcmp( q, "key_agreement" ) == 0 )
320 opt.key_usage |= KU_KEY_AGREEMENT;
321 else if( strcmp( q, "key_cert_sign" ) == 0 )
322 opt.key_usage |= KU_KEY_CERT_SIGN;
323 else if( strcmp( q, "crl_sign" ) == 0 )
324 opt.key_usage |= KU_CRL_SIGN;
325 else
326 goto usage;
327
328 q = r;
329 }
330 }
331 else if( strcmp( p, "ns_cert_type" ) == 0 )
332 {
333 while( q != NULL )
334 {
335 if( ( r = strchr( q, ',' ) ) != NULL )
336 *r++ = '\0';
337
338 if( strcmp( q, "ssl_client" ) == 0 )
339 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
340 else if( strcmp( q, "ssl_server" ) == 0 )
341 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
342 else if( strcmp( q, "email" ) == 0 )
343 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
344 else if( strcmp( q, "object_signing" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
346 else if( strcmp( q, "ssl_ca" ) == 0 )
347 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
348 else if( strcmp( q, "email_ca" ) == 0 )
349 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
350 else if( strcmp( q, "object_signing_ca" ) == 0 )
351 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
352 else
353 goto usage;
354
355 q = r;
356 }
357 }
358 else
359 goto usage;
360 }
361
Paul Bakker1014e952013-09-09 13:59:42 +0200362 printf("\n");
363
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200364 /*
365 * 0. Seed the PRNG
366 */
367 printf( " . Seeding the random number generator..." );
368 fflush( stdout );
369
370 entropy_init( &entropy );
371 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
372 (const unsigned char *) pers,
373 strlen( pers ) ) ) != 0 )
374 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700375 polarssl_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200376 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
377 goto exit;
378 }
379
380 printf( " ok\n" );
381
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 // Parse serial to MPI
383 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200384 printf( " . Reading serial number..." );
385 fflush( stdout );
386
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
388 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700389 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
391 goto exit;
392 }
393
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200394 printf( " ok\n" );
395
Paul Bakker1014e952013-09-09 13:59:42 +0200396 // Parse issuer certificate if present
397 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200398 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200399 {
400 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200401 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200402 */
403 printf( " . Loading the issuer certificate ..." );
404 fflush( stdout );
405
Paul Bakkerddf26b42013-09-18 13:46:23 +0200406 if( ( ret = x509_crt_parse_file( &issuer_crt, opt.issuer_crt ) ) != 0 )
Paul Bakker1014e952013-09-09 13:59:42 +0200407 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700408 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200409 printf( " failed\n ! x509_crt_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200410 goto exit;
411 }
412
Paul Bakker86d0c192013-09-18 11:11:02 +0200413 ret = x509_dn_gets( issuer_name, sizeof(issuer_name),
Paul Bakkerfdba4682014-04-25 11:48:35 +0200414 &issuer_crt.subject );
Paul Bakker1014e952013-09-09 13:59:42 +0200415 if( ret < 0 )
416 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700417 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200418 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakker1014e952013-09-09 13:59:42 +0200419 goto exit;
420 }
421
Paul Bakkere2673fb2013-09-09 15:52:07 +0200422 opt.issuer_name = issuer_name;
423
424 printf( " ok\n" );
425 }
426
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200427#if defined(POLARSSL_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200428 // Parse certificate request if present
429 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200430 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200431 {
432 /*
433 * 1.0.b. Load the CSR
434 */
435 printf( " . Loading the certificate request ..." );
436 fflush( stdout );
437
Paul Bakkerddf26b42013-09-18 13:46:23 +0200438 if( ( ret = x509_csr_parse_file( &csr, opt.request_file ) ) != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200439 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700440 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200441 printf( " failed\n ! x509_csr_parse_file returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200442 goto exit;
443 }
444
Paul Bakker86d0c192013-09-18 11:11:02 +0200445 ret = x509_dn_gets( subject_name, sizeof(subject_name),
Paul Bakkere2673fb2013-09-09 15:52:07 +0200446 &csr.subject );
447 if( ret < 0 )
448 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700449 polarssl_strerror( ret, buf, 1024 );
Paul Bakker86d0c192013-09-18 11:11:02 +0200450 printf( " failed\n ! x509_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200451 goto exit;
452 }
453
454 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200455 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200456
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200457 printf( " ok\n" );
458 }
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200459#endif /* POLARSSL_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200460
461 /*
462 * 1.1. Load the keys
463 */
464 if( !opt.selfsign && !strlen( opt.request_file ) )
465 {
466 printf( " . Loading the subject key ..." );
467 fflush( stdout );
468
Paul Bakker1a7550a2013-09-15 13:01:22 +0200469 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200470 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200471 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200472 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700473 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200474 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200475 goto exit;
476 }
Paul Bakker1014e952013-09-09 13:59:42 +0200477
478 printf( " ok\n" );
479 }
480
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200481 printf( " . Loading the issuer key ..." );
482 fflush( stdout );
483
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
485 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200486 if( ret != 0 )
487 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700488 polarssl_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200490 goto exit;
491 }
492
493 // Check if key and issuer certificate match
494 //
495 if( strlen( opt.issuer_crt ) )
496 {
497 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200498 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
499 &pk_rsa( *issuer_key )->N ) != 0 ||
500 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
501 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200502 {
503 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
504 ret = -1;
505 goto exit;
506 }
507 }
508
509 printf( " ok\n" );
510
511 if( opt.selfsign )
512 {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100513 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200514 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200515 }
516
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200517 x509write_crt_set_subject_key( &crt, subject_key );
518 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200519
Paul Bakker9397dcb2013-09-06 09:55:26 +0200520 /*
521 * 1.0. Check the names for validity
522 */
523 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
524 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700525 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
527 goto exit;
528 }
529
530 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
531 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700532 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200533 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
534 goto exit;
535 }
536
Paul Bakker9397dcb2013-09-06 09:55:26 +0200537 printf( " . Setting certificate values ..." );
538 fflush( stdout );
539
540 ret = x509write_crt_set_serial( &crt, &serial );
541 if( ret != 0 )
542 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700543 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
545 goto exit;
546 }
547
548 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
549 if( ret != 0 )
550 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700551 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200552 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
553 goto exit;
554 }
555
556 printf( " ok\n" );
557
Paul Bakker15162a02013-09-06 19:27:21 +0200558 printf( " . Adding the Basic Constraints extension ..." );
559 fflush( stdout );
560
561 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
562 opt.max_pathlen );
563 if( ret != 0 )
564 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700565 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200566 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
567 goto exit;
568 }
569
570 printf( " ok\n" );
571
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100572#if defined(POLARSSL_SHA1_C)
Paul Bakker15162a02013-09-06 19:27:21 +0200573 printf( " . Adding the Subject Key Identifier ..." );
574 fflush( stdout );
575
576 ret = x509write_crt_set_subject_key_identifier( &crt );
577 if( ret != 0 )
578 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700579 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200580 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
581 goto exit;
582 }
583
584 printf( " ok\n" );
585
586 printf( " . Adding the Authority Key Identifier ..." );
587 fflush( stdout );
588
589 ret = x509write_crt_set_authority_key_identifier( &crt );
590 if( ret != 0 )
591 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700592 polarssl_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200593 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
594 goto exit;
595 }
596
597 printf( " ok\n" );
Manuel Pégourié-Gonnard3daaf3d2013-10-27 14:22:02 +0100598#endif /* POLARSSL_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200599
Paul Bakker52be08c2013-09-09 12:37:54 +0200600 if( opt.key_usage )
601 {
602 printf( " . Adding the Key Usage extension ..." );
603 fflush( stdout );
604
605 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
606 if( ret != 0 )
607 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700608 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200609 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
610 goto exit;
611 }
612
613 printf( " ok\n" );
614 }
615
616 if( opt.ns_cert_type )
617 {
618 printf( " . Adding the NS Cert Type extension ..." );
619 fflush( stdout );
620
621 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
622 if( ret != 0 )
623 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700624 polarssl_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200625 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
626 goto exit;
627 }
628
629 printf( " ok\n" );
630 }
631
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 /*
633 * 1.2. Writing the request
634 */
635 printf( " . Writing the certificate..." );
636 fflush( stdout );
637
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200638 if( ( ret = write_certificate( &crt, opt.output_file,
639 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 {
Shuo Chen95a0d112014-04-04 21:04:40 -0700641 polarssl_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
643 goto exit;
644 }
645
646 printf( " ok\n" );
647
648exit:
649 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200650 pk_free( &loaded_subject_key );
651 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200652 mpi_free( &serial );
Paul Bakkera317a982014-06-18 16:44:11 +0200653 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200654 entropy_free( &entropy );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200655
656#if defined(_WIN32)
657 printf( " + Press Enter to exit this program.\n" );
658 fflush( stdout ); getchar();
659#endif
660
661 return( ret );
662}
Paul Bakker36713e82013-09-17 13:25:29 +0200663#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
664 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200665 POLARSSL_ERROR_C */