blob: 0b52bb1865f048ce2b5b7ee50c723e60f2fc3d8a [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 Bakker9397dcb2013-09-06 09:55:26 +020036#include "polarssl/x509write.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020037#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
39#include "polarssl/error.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020040
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020041#if !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_X509_PARSE_C) || \
42 !defined(POLARSSL_FS_IO) || \
43 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020044 !defined(POLARSSL_ERROR_C)
Paul Bakker9397dcb2013-09-06 09:55:26 +020045int main( int argc, char *argv[] )
46{
47 ((void) argc);
48 ((void) argv);
49
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020050 printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_X509_PARSE_C and/or "
51 "POLARSSL_FS_IO and/or "
52 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
53 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +020054 return( 0 );
55}
56#else
57
Paul Bakker1014e952013-09-09 13:59:42 +020058#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020059#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020060#define DFL_SUBJECT_KEY "subject.key"
61#define DFL_ISSUER_KEY "ca.key"
62#define DFL_SUBJECT_PWD ""
63#define DFL_ISSUER_PWD ""
64#define DFL_OUTPUT_FILENAME "cert.crt"
65#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
66#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
67#define DFL_NOT_BEFORE "20010101000000"
68#define DFL_NOT_AFTER "20301231235959"
69#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020070#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020071#define DFL_IS_CA 0
72#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020073#define DFL_KEY_USAGE 0
74#define DFL_NS_CERT_TYPE 0
75
76/*
77 * global options
78 */
79struct options
80{
Paul Bakker1014e952013-09-09 13:59:42 +020081 char *issuer_crt; /* filename of the issuer certificate */
Paul Bakkere2673fb2013-09-09 15:52:07 +020082 char *request_file; /* filename of the certificate request */
Paul Bakker9397dcb2013-09-06 09:55:26 +020083 char *subject_key; /* filename of the subject key file */
84 char *issuer_key; /* filename of the issuer key file */
85 char *subject_pwd; /* password for the subject key file */
86 char *issuer_pwd; /* password for the issuer key file */
87 char *output_file; /* where to store the constructed key file */
88 char *subject_name; /* subject name for certificate */
89 char *issuer_name; /* issuer name for certificate */
90 char *not_before; /* validity period not before */
91 char *not_after; /* validity period not after */
92 char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +020093 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +020094 int is_ca; /* is a CA certificate */
95 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020096 unsigned char key_usage; /* key usage flags */
97 unsigned char ns_cert_type; /* NS cert type */
98} opt;
99
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200100int write_certificate( x509write_cert *crt, char *output_file,
101 int (*f_rng)(void *, unsigned char *, size_t),
102 void *p_rng )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200103{
104 int ret;
105 FILE *f;
106 unsigned char output_buf[4096];
107 size_t len = 0;
108
109 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200110 if( ( ret = x509write_crt_pem( crt, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200111 return( ret );
112
113 len = strlen( (char *) output_buf );
114
115 if( ( f = fopen( output_file, "w" ) ) == NULL )
116 return( -1 );
117
118 if( fwrite( output_buf, 1, len, f ) != len )
119 return( -1 );
120
121 fclose(f);
122
123 return( 0 );
124}
125
126#define USAGE \
127 "\n usage: cert_write param=<>...\n" \
128 "\n acceptable parameters:\n" \
Paul Bakkere2673fb2013-09-09 15:52:07 +0200129 " request_file=%%s default: (empty)\n" \
130 " If request_file is specified, subject_key,\n" \
131 " subject_pwd and subject_name are ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200132 " subject_key=%%s default: subject.key\n" \
133 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200134 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
135 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200136 " issuer_crt=%%s default: (empty)\n" \
137 " If issuer_crt is specified, issuer_name is\n" \
138 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200139 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
140 "\n" \
141 " selfsign=%%d default: 0 (false)\n" \
142 " If selfsign is enabled, issuer_name and\n" \
143 " issuer_key are required (issuer_crt and\n" \
144 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200145 " issuer_key=%%s default: ca.key\n" \
146 " issuer_pwd=%%s default: (empty)\n" \
147 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200148 " serial=%%s default: 1\n" \
149 " not_before=%%s default: 20010101000000\n"\
150 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200151 " is_ca=%%d default: 0 (disabled)\n" \
152 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200153 " key_usage=%%s default: (empty)\n" \
154 " Comma-separated-list of values:\n" \
155 " digital_signature\n" \
156 " non_repudiation\n" \
157 " key_encipherment\n" \
158 " data_encipherment\n" \
159 " key_agreement\n" \
160 " key_certificate_sign\n" \
161 " crl_sign\n" \
162 " ns_cert_type=%%s default: (empty)\n" \
163 " Comma-separated-list of values:\n" \
164 " ssl_client\n" \
165 " ssl_server\n" \
166 " email\n" \
167 " object_signing\n" \
168 " ssl_ca\n" \
169 " email_ca\n" \
170 " object_signing_ca\n" \
171 "\n"
172
173int main( int argc, char *argv[] )
174{
175 int ret = 0;
Paul Bakker1014e952013-09-09 13:59:42 +0200176 x509_cert issuer_crt;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200177 pk_context loaded_issuer_key, loaded_subject_key;
178 pk_context *issuer_key = &loaded_issuer_key,
179 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200180 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200181 char issuer_name[128];
182 char subject_name[128];
Paul Bakker9397dcb2013-09-06 09:55:26 +0200183 int i, j, n;
184 char *p, *q, *r;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200185 x509_csr csr;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200186 x509write_cert crt;
187 mpi serial;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200188 entropy_context entropy;
189 ctr_drbg_context ctr_drbg;
190 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191
192 /*
193 * Set to sane values
194 */
195 x509write_crt_init( &crt );
196 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200197 pk_init( &loaded_issuer_key );
198 pk_init( &loaded_subject_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 mpi_init( &serial );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200200 memset( &csr, 0, sizeof(x509_csr) );
Paul Bakker1014e952013-09-09 13:59:42 +0200201 memset( &issuer_crt, 0, sizeof(x509_cert) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200202 memset( buf, 0, 1024 );
203
204 if( argc == 0 )
205 {
206 usage:
207 printf( USAGE );
208 ret = 1;
209 goto exit;
210 }
211
Paul Bakker1014e952013-09-09 13:59:42 +0200212 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200213 opt.request_file = DFL_REQUEST_FILE;
214 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 opt.subject_key = DFL_SUBJECT_KEY;
216 opt.issuer_key = DFL_ISSUER_KEY;
217 opt.subject_pwd = DFL_SUBJECT_PWD;
218 opt.issuer_pwd = DFL_ISSUER_PWD;
219 opt.output_file = DFL_OUTPUT_FILENAME;
220 opt.subject_name = DFL_SUBJECT_NAME;
221 opt.issuer_name = DFL_ISSUER_NAME;
222 opt.not_before = DFL_NOT_BEFORE;
223 opt.not_after = DFL_NOT_AFTER;
224 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200225 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200226 opt.is_ca = DFL_IS_CA;
227 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200228 opt.key_usage = DFL_KEY_USAGE;
229 opt.ns_cert_type = DFL_NS_CERT_TYPE;
230
231 for( i = 1; i < argc; i++ )
232 {
233
234 p = argv[i];
235 if( ( q = strchr( p, '=' ) ) == NULL )
236 goto usage;
237 *q++ = '\0';
238
239 n = strlen( p );
240 for( j = 0; j < n; j++ )
241 {
242 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
243 argv[i][j] |= 0x20;
244 }
245
Paul Bakkere2673fb2013-09-09 15:52:07 +0200246 if( strcmp( p, "request_file" ) == 0 )
247 opt.request_file = q;
248 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 opt.subject_key = q;
250 else if( strcmp( p, "issuer_key" ) == 0 )
251 opt.issuer_key = q;
252 else if( strcmp( p, "subject_pwd" ) == 0 )
253 opt.subject_pwd = q;
254 else if( strcmp( p, "issuer_pwd" ) == 0 )
255 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200256 else if( strcmp( p, "issuer_crt" ) == 0 )
257 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200258 else if( strcmp( p, "output_file" ) == 0 )
259 opt.output_file = q;
260 else if( strcmp( p, "subject_name" ) == 0 )
261 {
262 opt.subject_name = q;
263 }
264 else if( strcmp( p, "issuer_name" ) == 0 )
265 {
266 opt.issuer_name = q;
267 }
268 else if( strcmp( p, "not_before" ) == 0 )
269 {
270 opt.not_before = q;
271 }
272 else if( strcmp( p, "not_after" ) == 0 )
273 {
274 opt.not_after = q;
275 }
276 else if( strcmp( p, "serial" ) == 0 )
277 {
278 opt.serial = q;
279 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200280 else if( strcmp( p, "selfsign" ) == 0 )
281 {
282 opt.selfsign = atoi( q );
283 if( opt.selfsign < 0 || opt.selfsign > 1 )
284 goto usage;
285 }
Paul Bakker15162a02013-09-06 19:27:21 +0200286 else if( strcmp( p, "is_ca" ) == 0 )
287 {
288 opt.is_ca = atoi( q );
289 if( opt.is_ca < 0 || opt.is_ca > 1 )
290 goto usage;
291 }
292 else if( strcmp( p, "max_pathlen" ) == 0 )
293 {
294 opt.max_pathlen = atoi( q );
295 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
296 goto usage;
297 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200298 else if( strcmp( p, "key_usage" ) == 0 )
299 {
300 while( q != NULL )
301 {
302 if( ( r = strchr( q, ',' ) ) != NULL )
303 *r++ = '\0';
304
305 if( strcmp( q, "digital_signature" ) == 0 )
306 opt.key_usage |= KU_DIGITAL_SIGNATURE;
307 else if( strcmp( q, "non_repudiation" ) == 0 )
308 opt.key_usage |= KU_NON_REPUDIATION;
309 else if( strcmp( q, "key_encipherment" ) == 0 )
310 opt.key_usage |= KU_KEY_ENCIPHERMENT;
311 else if( strcmp( q, "data_encipherment" ) == 0 )
312 opt.key_usage |= KU_DATA_ENCIPHERMENT;
313 else if( strcmp( q, "key_agreement" ) == 0 )
314 opt.key_usage |= KU_KEY_AGREEMENT;
315 else if( strcmp( q, "key_cert_sign" ) == 0 )
316 opt.key_usage |= KU_KEY_CERT_SIGN;
317 else if( strcmp( q, "crl_sign" ) == 0 )
318 opt.key_usage |= KU_CRL_SIGN;
319 else
320 goto usage;
321
322 q = r;
323 }
324 }
325 else if( strcmp( p, "ns_cert_type" ) == 0 )
326 {
327 while( q != NULL )
328 {
329 if( ( r = strchr( q, ',' ) ) != NULL )
330 *r++ = '\0';
331
332 if( strcmp( q, "ssl_client" ) == 0 )
333 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
334 else if( strcmp( q, "ssl_server" ) == 0 )
335 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
336 else if( strcmp( q, "email" ) == 0 )
337 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
338 else if( strcmp( q, "object_signing" ) == 0 )
339 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
340 else if( strcmp( q, "ssl_ca" ) == 0 )
341 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
342 else if( strcmp( q, "email_ca" ) == 0 )
343 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
344 else if( strcmp( q, "object_signing_ca" ) == 0 )
345 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
346 else
347 goto usage;
348
349 q = r;
350 }
351 }
352 else
353 goto usage;
354 }
355
Paul Bakker1014e952013-09-09 13:59:42 +0200356 printf("\n");
357
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200358 /*
359 * 0. Seed the PRNG
360 */
361 printf( " . Seeding the random number generator..." );
362 fflush( stdout );
363
364 entropy_init( &entropy );
365 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
366 (const unsigned char *) pers,
367 strlen( pers ) ) ) != 0 )
368 {
369 error_strerror( ret, buf, 1024 );
370 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
371 goto exit;
372 }
373
374 printf( " ok\n" );
375
Paul Bakker9397dcb2013-09-06 09:55:26 +0200376 // Parse serial to MPI
377 //
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200378 printf( " . Reading serial number..." );
379 fflush( stdout );
380
Paul Bakker9397dcb2013-09-06 09:55:26 +0200381 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
382 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200383 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200384 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
385 goto exit;
386 }
387
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200388 printf( " ok\n" );
389
Paul Bakker1014e952013-09-09 13:59:42 +0200390 // Parse issuer certificate if present
391 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200392 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200393 {
394 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200395 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200396 */
397 printf( " . Loading the issuer certificate ..." );
398 fflush( stdout );
399
400 if( ( ret = x509parse_crtfile( &issuer_crt, opt.issuer_crt ) ) != 0 )
401 {
Paul Bakker1014e952013-09-09 13:59:42 +0200402 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200403 printf( " failed\n ! x509parse_crtfile returned -0x%02x - %s\n\n", -ret, buf );
404 goto exit;
405 }
406
Paul Bakkere2673fb2013-09-09 15:52:07 +0200407 ret = x509parse_dn_gets( issuer_name, sizeof(issuer_name),
408 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200409 if( ret < 0 )
410 {
Paul Bakker1014e952013-09-09 13:59:42 +0200411 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200412 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
413 goto exit;
414 }
415
Paul Bakkere2673fb2013-09-09 15:52:07 +0200416 opt.issuer_name = issuer_name;
417
418 printf( " ok\n" );
419 }
420
421 // Parse certificate request if present
422 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200423 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200424 {
425 /*
426 * 1.0.b. Load the CSR
427 */
428 printf( " . Loading the certificate request ..." );
429 fflush( stdout );
430
431 if( ( ret = x509parse_csrfile( &csr, opt.request_file ) ) != 0 )
432 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200433 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 printf( " failed\n ! x509parse_csrfile returned -0x%02x - %s\n\n", -ret, buf );
435 goto exit;
436 }
437
438 ret = x509parse_dn_gets( subject_name, sizeof(subject_name),
439 &csr.subject );
440 if( ret < 0 )
441 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200442 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200443 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
444 goto exit;
445 }
446
447 opt.subject_name = subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200448 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200449
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200450 printf( " ok\n" );
451 }
452
453 /*
454 * 1.1. Load the keys
455 */
456 if( !opt.selfsign && !strlen( opt.request_file ) )
457 {
458 printf( " . Loading the subject key ..." );
459 fflush( stdout );
460
Paul Bakker1a7550a2013-09-15 13:01:22 +0200461 ret = pk_parse_keyfile( &loaded_subject_key, opt.subject_key,
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200462 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200463 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200464 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200465 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200466 printf( " failed\n ! pk_parse_keyfile returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200467 goto exit;
468 }
Paul Bakker1014e952013-09-09 13:59:42 +0200469
470 printf( " ok\n" );
471 }
472
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200473 printf( " . Loading the issuer key ..." );
474 fflush( stdout );
475
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 ret = pk_parse_keyfile( &loaded_issuer_key, opt.issuer_key,
477 opt.issuer_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200478 if( ret != 0 )
479 {
480 error_strerror( ret, buf, 1024 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481 printf( " failed\n ! pk_parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200482 goto exit;
483 }
484
485 // Check if key and issuer certificate match
486 //
487 if( strlen( opt.issuer_crt ) )
488 {
489 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200490 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
491 &pk_rsa( *issuer_key )->N ) != 0 ||
492 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
493 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200494 {
495 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
496 ret = -1;
497 goto exit;
498 }
499 }
500
501 printf( " ok\n" );
502
503 if( opt.selfsign )
504 {
505 opt.issuer_name = opt.subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200506 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200507 }
508
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200509 x509write_crt_set_subject_key( &crt, subject_key );
510 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200511
Paul Bakker9397dcb2013-09-06 09:55:26 +0200512 /*
513 * 1.0. Check the names for validity
514 */
515 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
516 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200517 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200518 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
519 goto exit;
520 }
521
522 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
523 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200524 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200525 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
526 goto exit;
527 }
528
Paul Bakker9397dcb2013-09-06 09:55:26 +0200529 printf( " . Setting certificate values ..." );
530 fflush( stdout );
531
532 ret = x509write_crt_set_serial( &crt, &serial );
533 if( ret != 0 )
534 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200535 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200536 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
537 goto exit;
538 }
539
540 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
541 if( ret != 0 )
542 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
545 goto exit;
546 }
547
548 printf( " ok\n" );
549
Paul Bakker15162a02013-09-06 19:27:21 +0200550 printf( " . Adding the Basic Constraints extension ..." );
551 fflush( stdout );
552
553 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
554 opt.max_pathlen );
555 if( ret != 0 )
556 {
Paul Bakker15162a02013-09-06 19:27:21 +0200557 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200558 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
559 goto exit;
560 }
561
562 printf( " ok\n" );
563
564 printf( " . Adding the Subject Key Identifier ..." );
565 fflush( stdout );
566
567 ret = x509write_crt_set_subject_key_identifier( &crt );
568 if( ret != 0 )
569 {
Paul Bakker15162a02013-09-06 19:27:21 +0200570 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200571 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
572 goto exit;
573 }
574
575 printf( " ok\n" );
576
577 printf( " . Adding the Authority Key Identifier ..." );
578 fflush( stdout );
579
580 ret = x509write_crt_set_authority_key_identifier( &crt );
581 if( ret != 0 )
582 {
Paul Bakker15162a02013-09-06 19:27:21 +0200583 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200584 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
585 goto exit;
586 }
587
588 printf( " ok\n" );
589
Paul Bakker52be08c2013-09-09 12:37:54 +0200590 if( opt.key_usage )
591 {
592 printf( " . Adding the Key Usage extension ..." );
593 fflush( stdout );
594
595 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
596 if( ret != 0 )
597 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200598 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200599 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
600 goto exit;
601 }
602
603 printf( " ok\n" );
604 }
605
606 if( opt.ns_cert_type )
607 {
608 printf( " . Adding the NS Cert Type extension ..." );
609 fflush( stdout );
610
611 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
612 if( ret != 0 )
613 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200614 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200615 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
616 goto exit;
617 }
618
619 printf( " ok\n" );
620 }
621
Paul Bakker9397dcb2013-09-06 09:55:26 +0200622 /*
623 * 1.2. Writing the request
624 */
625 printf( " . Writing the certificate..." );
626 fflush( stdout );
627
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200628 if( ( ret = write_certificate( &crt, opt.output_file,
629 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200630 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200631 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200632 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
633 goto exit;
634 }
635
636 printf( " ok\n" );
637
638exit:
639 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200640 pk_free( &loaded_subject_key );
641 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642 mpi_free( &serial );
643
644#if defined(_WIN32)
645 printf( " + Press Enter to exit this program.\n" );
646 fflush( stdout ); getchar();
647#endif
648
649 return( ret );
650}
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200651#endif /* POLARSSL_X509_WRITE_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
652 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
653 POLARSSL_ERROR_C */