blob: c276321b66cbfc3b087fce02d296b70f5f48d6cb [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/config.h"
35
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#include "polarssl/x509_crt.h"
37#include "polarssl/x509_csr.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
40#include "polarssl/error.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020041
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042#if !defined(POLARSSL_X509_CRT_WRITE_C) || \
43 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020044 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020045 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051 printf( "POLARSSL_X509_CRT_WRITE_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020052 "POLARSSL_FS_IO and/or "
53 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
54 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020055 return( 0 );
56}
57#else
58
Paul Bakker1014e952013-09-09 13:59:42 +020059#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020060#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020061#define DFL_SUBJECT_KEY "subject.key"
62#define DFL_ISSUER_KEY "ca.key"
63#define DFL_SUBJECT_PWD ""
64#define DFL_ISSUER_PWD ""
65#define DFL_OUTPUT_FILENAME "cert.crt"
66#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
67#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
68#define DFL_NOT_BEFORE "20010101000000"
69#define DFL_NOT_AFTER "20301231235959"
70#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020071#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020072#define DFL_IS_CA 0
73#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020074#define DFL_KEY_USAGE 0
75#define DFL_NS_CERT_TYPE 0
76
77/*
78 * global options
79 */
80struct options
81{
Paul Bakker1014e952013-09-09 13:59:42 +020082 char *issuer_crt; /* filename of the issuer certificate */
Paul Bakkere2673fb2013-09-09 15:52:07 +020083 char *request_file; /* filename of the certificate request */
Paul Bakker9397dcb2013-09-06 09:55:26 +020084 char *subject_key; /* filename of the subject key file */
85 char *issuer_key; /* filename of the issuer key file */
86 char *subject_pwd; /* password for the subject key file */
87 char *issuer_pwd; /* password for the issuer key file */
88 char *output_file; /* where to store the constructed key file */
89 char *subject_name; /* subject name for certificate */
90 char *issuer_name; /* issuer name for certificate */
91 char *not_before; /* validity period not before */
92 char *not_after; /* validity period not after */
93 char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020094 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020095 int is_ca; /* is a CA certificate */
96 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020097 unsigned char key_usage; /* key usage flags */
98 unsigned char ns_cert_type; /* NS cert type */
99} opt;
100
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200101int write_certificate( x509write_cert *crt, char *output_file,
102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200104{
105 int ret;
106 FILE *f;
107 unsigned char output_buf[4096];
108 size_t len = 0;
109
110 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200111 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200112 return( ret );
113
114 len = strlen( (char *) output_buf );
115
116 if( ( f = fopen( output_file, "w" ) ) == NULL )
117 return( -1 );
118
119 if( fwrite( output_buf, 1, len, f ) != len )
120 return( -1 );
121
122 fclose(f);
123
124 return( 0 );
125}
126
127#define USAGE \
128 "\n usage: cert_write param=<>...\n" \
129 "\n acceptable parameters:\n" \
Paul Bakkere2673fb2013-09-09 15:52:07 +0200130 " request_file=%%s default: (empty)\n" \
131 " If request_file is specified, subject_key,\n" \
132 " subject_pwd and subject_name are ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200133 " subject_key=%%s default: subject.key\n" \
134 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200135 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
136 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200137 " issuer_crt=%%s default: (empty)\n" \
138 " If issuer_crt is specified, issuer_name is\n" \
139 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200140 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
141 "\n" \
142 " selfsign=%%d default: 0 (false)\n" \
143 " If selfsign is enabled, issuer_name and\n" \
144 " issuer_key are required (issuer_crt and\n" \
145 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200146 " issuer_key=%%s default: ca.key\n" \
147 " issuer_pwd=%%s default: (empty)\n" \
148 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200149 " serial=%%s default: 1\n" \
150 " not_before=%%s default: 20010101000000\n"\
151 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200152 " is_ca=%%d default: 0 (disabled)\n" \
153 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200154 " key_usage=%%s default: (empty)\n" \
155 " Comma-separated-list of values:\n" \
156 " digital_signature\n" \
157 " non_repudiation\n" \
158 " key_encipherment\n" \
159 " data_encipherment\n" \
160 " key_agreement\n" \
161 " key_certificate_sign\n" \
162 " crl_sign\n" \
163 " ns_cert_type=%%s default: (empty)\n" \
164 " Comma-separated-list of values:\n" \
165 " ssl_client\n" \
166 " ssl_server\n" \
167 " email\n" \
168 " object_signing\n" \
169 " ssl_ca\n" \
170 " email_ca\n" \
171 " object_signing_ca\n" \
172 "\n"
173
174int main( int argc, char *argv[] )
175{
176 int ret = 0;
Paul Bakker1014e952013-09-09 13:59:42 +0200177 x509_cert issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200178 pk_context loaded_issuer_key, loaded_subject_key;
179 pk_context *issuer_key = &loaded_issuer_key,
180 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200182 char issuer_name[128];
183 char subject_name[128];
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184 int i, j, n;
185 char *p, *q, *r;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200186 x509_csr csr;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 x509write_cert crt;
188 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200189 entropy_context entropy;
190 ctr_drbg_context ctr_drbg;
191 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200192
193 /*
194 * Set to sane values
195 */
196 x509write_crt_init( &crt );
197 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200198 pk_init( &loaded_issuer_key );
199 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200 mpi_init( &serial );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200201 memset( &csr, 0, sizeof(x509_csr) );
Paul Bakker1014e952013-09-09 13:59:42 +0200202 memset( &issuer_crt, 0, sizeof(x509_cert) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 memset( buf, 0, 1024 );
204
205 if( argc == 0 )
206 {
207 usage:
208 printf( USAGE );
209 ret = 1;
210 goto exit;
211 }
212
Paul Bakker1014e952013-09-09 13:59:42 +0200213 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200214 opt.request_file = DFL_REQUEST_FILE;
215 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200216 opt.subject_key = DFL_SUBJECT_KEY;
217 opt.issuer_key = DFL_ISSUER_KEY;
218 opt.subject_pwd = DFL_SUBJECT_PWD;
219 opt.issuer_pwd = DFL_ISSUER_PWD;
220 opt.output_file = DFL_OUTPUT_FILENAME;
221 opt.subject_name = DFL_SUBJECT_NAME;
222 opt.issuer_name = DFL_ISSUER_NAME;
223 opt.not_before = DFL_NOT_BEFORE;
224 opt.not_after = DFL_NOT_AFTER;
225 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200226 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200227 opt.is_ca = DFL_IS_CA;
228 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200229 opt.key_usage = DFL_KEY_USAGE;
230 opt.ns_cert_type = DFL_NS_CERT_TYPE;
231
232 for( i = 1; i < argc; i++ )
233 {
234
235 p = argv[i];
236 if( ( q = strchr( p, '=' ) ) == NULL )
237 goto usage;
238 *q++ = '\0';
239
240 n = strlen( p );
241 for( j = 0; j < n; j++ )
242 {
243 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
244 argv[i][j] |= 0x20;
245 }
246
Paul Bakkere2673fb2013-09-09 15:52:07 +0200247 if( strcmp( p, "request_file" ) == 0 )
248 opt.request_file = q;
249 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200250 opt.subject_key = q;
251 else if( strcmp( p, "issuer_key" ) == 0 )
252 opt.issuer_key = q;
253 else if( strcmp( p, "subject_pwd" ) == 0 )
254 opt.subject_pwd = q;
255 else if( strcmp( p, "issuer_pwd" ) == 0 )
256 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200257 else if( strcmp( p, "issuer_crt" ) == 0 )
258 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200259 else if( strcmp( p, "output_file" ) == 0 )
260 opt.output_file = q;
261 else if( strcmp( p, "subject_name" ) == 0 )
262 {
263 opt.subject_name = q;
264 }
265 else if( strcmp( p, "issuer_name" ) == 0 )
266 {
267 opt.issuer_name = q;
268 }
269 else if( strcmp( p, "not_before" ) == 0 )
270 {
271 opt.not_before = q;
272 }
273 else if( strcmp( p, "not_after" ) == 0 )
274 {
275 opt.not_after = q;
276 }
277 else if( strcmp( p, "serial" ) == 0 )
278 {
279 opt.serial = q;
280 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200281 else if( strcmp( p, "selfsign" ) == 0 )
282 {
283 opt.selfsign = atoi( q );
284 if( opt.selfsign < 0 || opt.selfsign > 1 )
285 goto usage;
286 }
Paul Bakker15162a02013-09-06 19:27:21 +0200287 else if( strcmp( p, "is_ca" ) == 0 )
288 {
289 opt.is_ca = atoi( q );
290 if( opt.is_ca < 0 || opt.is_ca > 1 )
291 goto usage;
292 }
293 else if( strcmp( p, "max_pathlen" ) == 0 )
294 {
295 opt.max_pathlen = atoi( q );
296 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
297 goto usage;
298 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 else if( strcmp( p, "key_usage" ) == 0 )
300 {
301 while( q != NULL )
302 {
303 if( ( r = strchr( q, ',' ) ) != NULL )
304 *r++ = '\0';
305
306 if( strcmp( q, "digital_signature" ) == 0 )
307 opt.key_usage |= KU_DIGITAL_SIGNATURE;
308 else if( strcmp( q, "non_repudiation" ) == 0 )
309 opt.key_usage |= KU_NON_REPUDIATION;
310 else if( strcmp( q, "key_encipherment" ) == 0 )
311 opt.key_usage |= KU_KEY_ENCIPHERMENT;
312 else if( strcmp( q, "data_encipherment" ) == 0 )
313 opt.key_usage |= KU_DATA_ENCIPHERMENT;
314 else if( strcmp( q, "key_agreement" ) == 0 )
315 opt.key_usage |= KU_KEY_AGREEMENT;
316 else if( strcmp( q, "key_cert_sign" ) == 0 )
317 opt.key_usage |= KU_KEY_CERT_SIGN;
318 else if( strcmp( q, "crl_sign" ) == 0 )
319 opt.key_usage |= KU_CRL_SIGN;
320 else
321 goto usage;
322
323 q = r;
324 }
325 }
326 else if( strcmp( p, "ns_cert_type" ) == 0 )
327 {
328 while( q != NULL )
329 {
330 if( ( r = strchr( q, ',' ) ) != NULL )
331 *r++ = '\0';
332
333 if( strcmp( q, "ssl_client" ) == 0 )
334 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
335 else if( strcmp( q, "ssl_server" ) == 0 )
336 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
337 else if( strcmp( q, "email" ) == 0 )
338 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
339 else if( strcmp( q, "object_signing" ) == 0 )
340 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
341 else if( strcmp( q, "ssl_ca" ) == 0 )
342 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
343 else if( strcmp( q, "email_ca" ) == 0 )
344 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
345 else if( strcmp( q, "object_signing_ca" ) == 0 )
346 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
347 else
348 goto usage;
349
350 q = r;
351 }
352 }
353 else
354 goto usage;
355 }
356
Paul Bakker1014e952013-09-09 13:59:42 +0200357 printf("\n");
358
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200359 /*
360 * 0. Seed the PRNG
361 */
362 printf( " . Seeding the random number generator..." );
363 fflush( stdout );
364
365 entropy_init( &entropy );
366 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
367 (const unsigned char *) pers,
368 strlen( pers ) ) ) != 0 )
369 {
370 error_strerror( ret, buf, 1024 );
371 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
372 goto exit;
373 }
374
375 printf( " ok\n" );
376
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377 // Parse serial to MPI
378 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200379 printf( " . Reading serial number..." );
380 fflush( stdout );
381
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
383 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
386 goto exit;
387 }
388
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200389 printf( " ok\n" );
390
Paul Bakker1014e952013-09-09 13:59:42 +0200391 // Parse issuer certificate if present
392 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200393 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200394 {
395 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200396 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200397 */
398 printf( " . Loading the issuer certificate ..." );
399 fflush( stdout );
400
401 if( ( ret = x509parse_crtfile( &issuer_crt, opt.issuer_crt ) ) != 0 )
402 {
Paul Bakker1014e952013-09-09 13:59:42 +0200403 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200404 printf( " failed\n ! x509parse_crtfile returned -0x%02x - %s\n\n", -ret, buf );
405 goto exit;
406 }
407
Paul Bakkere2673fb2013-09-09 15:52:07 +0200408 ret = x509parse_dn_gets( issuer_name, sizeof(issuer_name),
409 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200410 if( ret < 0 )
411 {
Paul Bakker1014e952013-09-09 13:59:42 +0200412 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200413 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
414 goto exit;
415 }
416
Paul Bakkere2673fb2013-09-09 15:52:07 +0200417 opt.issuer_name = issuer_name;
418
419 printf( " ok\n" );
420 }
421
422 // Parse certificate request if present
423 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200424 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200425 {
426 /*
427 * 1.0.b. Load the CSR
428 */
429 printf( " . Loading the certificate request ..." );
430 fflush( stdout );
431
432 if( ( ret = x509parse_csrfile( &csr, opt.request_file ) ) != 0 )
433 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200435 printf( " failed\n ! x509parse_csrfile returned -0x%02x - %s\n\n", -ret, buf );
436 goto exit;
437 }
438
439 ret = x509parse_dn_gets( subject_name, sizeof(subject_name),
440 &csr.subject );
441 if( ret < 0 )
442 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200443 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200444 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
445 goto exit;
446 }
447
448 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200449 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200450
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200451 printf( " ok\n" );
452 }
453
454 /*
455 * 1.1. Load the keys
456 */
457 if( !opt.selfsign && !strlen( opt.request_file ) )
458 {
459 printf( " . Loading the subject key ..." );
460 fflush( stdout );
461
Paul Bakker1a7550a2013-09-15 13:01:22 +0200462 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200463 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200464 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200465 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200466 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200468 goto exit;
469 }
Paul Bakker1014e952013-09-09 13:59:42 +0200470
471 printf( " ok\n" );
472 }
473
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200474 printf( " . Loading the issuer key ..." );
475 fflush( stdout );
476
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
478 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200479 if( ret != 0 )
480 {
481 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200483 goto exit;
484 }
485
486 // Check if key and issuer certificate match
487 //
488 if( strlen( opt.issuer_crt ) )
489 {
490 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200491 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
492 &pk_rsa( *issuer_key )->N ) != 0 ||
493 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
494 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200495 {
496 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
497 ret = -1;
498 goto exit;
499 }
500 }
501
502 printf( " ok\n" );
503
504 if( opt.selfsign )
505 {
506 opt.issuer_name = opt.subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200507 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200508 }
509
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200510 x509write_crt_set_subject_key( &crt, subject_key );
511 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200512
Paul Bakker9397dcb2013-09-06 09:55:26 +0200513 /*
514 * 1.0. Check the names for validity
515 */
516 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
517 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200518 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200519 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
520 goto exit;
521 }
522
523 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
524 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200525 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
527 goto exit;
528 }
529
Paul Bakker9397dcb2013-09-06 09:55:26 +0200530 printf( " . Setting certificate values ..." );
531 fflush( stdout );
532
533 ret = x509write_crt_set_serial( &crt, &serial );
534 if( ret != 0 )
535 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200536 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200537 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
538 goto exit;
539 }
540
541 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
542 if( ret != 0 )
543 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200545 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
546 goto exit;
547 }
548
549 printf( " ok\n" );
550
Paul Bakker15162a02013-09-06 19:27:21 +0200551 printf( " . Adding the Basic Constraints extension ..." );
552 fflush( stdout );
553
554 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
555 opt.max_pathlen );
556 if( ret != 0 )
557 {
Paul Bakker15162a02013-09-06 19:27:21 +0200558 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200559 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
560 goto exit;
561 }
562
563 printf( " ok\n" );
564
565 printf( " . Adding the Subject Key Identifier ..." );
566 fflush( stdout );
567
568 ret = x509write_crt_set_subject_key_identifier( &crt );
569 if( ret != 0 )
570 {
Paul Bakker15162a02013-09-06 19:27:21 +0200571 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200572 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
573 goto exit;
574 }
575
576 printf( " ok\n" );
577
578 printf( " . Adding the Authority Key Identifier ..." );
579 fflush( stdout );
580
581 ret = x509write_crt_set_authority_key_identifier( &crt );
582 if( ret != 0 )
583 {
Paul Bakker15162a02013-09-06 19:27:21 +0200584 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200585 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
586 goto exit;
587 }
588
589 printf( " ok\n" );
590
Paul Bakker52be08c2013-09-09 12:37:54 +0200591 if( opt.key_usage )
592 {
593 printf( " . Adding the Key Usage extension ..." );
594 fflush( stdout );
595
596 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
597 if( ret != 0 )
598 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200599 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200600 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
601 goto exit;
602 }
603
604 printf( " ok\n" );
605 }
606
607 if( opt.ns_cert_type )
608 {
609 printf( " . Adding the NS Cert Type extension ..." );
610 fflush( stdout );
611
612 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
613 if( ret != 0 )
614 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200615 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200616 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
617 goto exit;
618 }
619
620 printf( " ok\n" );
621 }
622
Paul Bakker9397dcb2013-09-06 09:55:26 +0200623 /*
624 * 1.2. Writing the request
625 */
626 printf( " . Writing the certificate..." );
627 fflush( stdout );
628
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200629 if( ( ret = write_certificate( &crt, opt.output_file,
630 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200631 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200633 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
634 goto exit;
635 }
636
637 printf( " ok\n" );
638
639exit:
640 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200641 pk_free( &loaded_subject_key );
642 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200643 mpi_free( &serial );
644
645#if defined(_WIN32)
646 printf( " + Press Enter to exit this program.\n" );
647 fflush( stdout ); getchar();
648#endif
649
650 return( ret );
651}
Paul Bakker36713e82013-09-17 13:25:29 +0200652#endif /* POLARSSL_X509_CRT_WRITE_C && POLARSSL_X509_CRT_PARSE_C &&
653 POLARSSL_FS_IO && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200654 POLARSSL_ERROR_C */