blob: f0939c8f2b04c03354a11bb9a1cc6b5bcbc7c6ba [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
36#include "polarssl/error.h"
37#include "polarssl/rsa.h"
38#include "polarssl/x509.h"
39#include "polarssl/base64.h"
40#include "polarssl/x509write.h"
41#include "polarssl/oid.h"
42
43#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020044 !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO) || \
45 !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
51 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker4122f3e2013-09-09 16:01:46 +020052 "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO 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
100int write_certificate( x509write_cert *crt, char *output_file )
101{
102 int ret;
103 FILE *f;
104 unsigned char output_buf[4096];
105 size_t len = 0;
106
107 memset( output_buf, 0, 4096 );
108 if( ( ret = x509write_crt_pem( crt, output_buf, 4096 ) ) < 0 )
109 return( ret );
110
111 len = strlen( (char *) output_buf );
112
113 if( ( f = fopen( output_file, "w" ) ) == NULL )
114 return( -1 );
115
116 if( fwrite( output_buf, 1, len, f ) != len )
117 return( -1 );
118
119 fclose(f);
120
121 return( 0 );
122}
123
124#define USAGE \
125 "\n usage: cert_write param=<>...\n" \
126 "\n acceptable parameters:\n" \
Paul Bakkere2673fb2013-09-09 15:52:07 +0200127 " request_file=%%s default: (empty)\n" \
128 " If request_file is specified, subject_key,\n" \
129 " subject_pwd and subject_name are ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200130 " subject_key=%%s default: subject.key\n" \
131 " subject_pwd=%%s default: (empty)\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200132 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
133 "\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200134 " issuer_crt=%%s default: (empty)\n" \
135 " If issuer_crt is specified, issuer_name is\n" \
136 " ignored!\n" \
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200137 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
138 "\n" \
139 " selfsign=%%d default: 0 (false)\n" \
140 " If selfsign is enabled, issuer_name and\n" \
141 " issuer_key are required (issuer_crt and\n" \
142 " subject_* are ignored\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200143 " issuer_key=%%s default: ca.key\n" \
144 " issuer_pwd=%%s default: (empty)\n" \
145 " output_file=%%s default: cert.crt\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200146 " serial=%%s default: 1\n" \
147 " not_before=%%s default: 20010101000000\n"\
148 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200149 " is_ca=%%d default: 0 (disabled)\n" \
150 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200151 " key_usage=%%s default: (empty)\n" \
152 " Comma-separated-list of values:\n" \
153 " digital_signature\n" \
154 " non_repudiation\n" \
155 " key_encipherment\n" \
156 " data_encipherment\n" \
157 " key_agreement\n" \
158 " key_certificate_sign\n" \
159 " crl_sign\n" \
160 " ns_cert_type=%%s default: (empty)\n" \
161 " Comma-separated-list of values:\n" \
162 " ssl_client\n" \
163 " ssl_server\n" \
164 " email\n" \
165 " object_signing\n" \
166 " ssl_ca\n" \
167 " email_ca\n" \
168 " object_signing_ca\n" \
169 "\n"
170
171int main( int argc, char *argv[] )
172{
173 int ret = 0;
Paul Bakker1014e952013-09-09 13:59:42 +0200174 x509_cert issuer_crt;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200175 rsa_context loaded_issuer_rsa, loaded_subject_rsa;
176 rsa_context *issuer_rsa = &loaded_issuer_rsa,
177 *subject_rsa = &loaded_subject_rsa;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200179 char issuer_name[128];
180 char subject_name[128];
Paul Bakker9397dcb2013-09-06 09:55:26 +0200181 int i, j, n;
182 char *p, *q, *r;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200183 x509_csr csr;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184 x509write_cert crt;
185 mpi serial;
186
187 /*
188 * Set to sane values
189 */
190 x509write_crt_init( &crt );
191 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200192 rsa_init( &loaded_issuer_rsa, RSA_PKCS_V15, 0 );
193 rsa_init( &loaded_subject_rsa, RSA_PKCS_V15, 0 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194 mpi_init( &serial );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200195 memset( &csr, 0, sizeof(x509_csr) );
Paul Bakker1014e952013-09-09 13:59:42 +0200196 memset( &issuer_crt, 0, sizeof(x509_cert) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200197 memset( buf, 0, 1024 );
198
199 if( argc == 0 )
200 {
201 usage:
202 printf( USAGE );
203 ret = 1;
204 goto exit;
205 }
206
Paul Bakker1014e952013-09-09 13:59:42 +0200207 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200208 opt.request_file = DFL_REQUEST_FILE;
209 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210 opt.subject_key = DFL_SUBJECT_KEY;
211 opt.issuer_key = DFL_ISSUER_KEY;
212 opt.subject_pwd = DFL_SUBJECT_PWD;
213 opt.issuer_pwd = DFL_ISSUER_PWD;
214 opt.output_file = DFL_OUTPUT_FILENAME;
215 opt.subject_name = DFL_SUBJECT_NAME;
216 opt.issuer_name = DFL_ISSUER_NAME;
217 opt.not_before = DFL_NOT_BEFORE;
218 opt.not_after = DFL_NOT_AFTER;
219 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200220 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200221 opt.is_ca = DFL_IS_CA;
222 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223 opt.key_usage = DFL_KEY_USAGE;
224 opt.ns_cert_type = DFL_NS_CERT_TYPE;
225
226 for( i = 1; i < argc; i++ )
227 {
228
229 p = argv[i];
230 if( ( q = strchr( p, '=' ) ) == NULL )
231 goto usage;
232 *q++ = '\0';
233
234 n = strlen( p );
235 for( j = 0; j < n; j++ )
236 {
237 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
238 argv[i][j] |= 0x20;
239 }
240
Paul Bakkere2673fb2013-09-09 15:52:07 +0200241 if( strcmp( p, "request_file" ) == 0 )
242 opt.request_file = q;
243 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244 opt.subject_key = q;
245 else if( strcmp( p, "issuer_key" ) == 0 )
246 opt.issuer_key = q;
247 else if( strcmp( p, "subject_pwd" ) == 0 )
248 opt.subject_pwd = q;
249 else if( strcmp( p, "issuer_pwd" ) == 0 )
250 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200251 else if( strcmp( p, "issuer_crt" ) == 0 )
252 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253 else if( strcmp( p, "output_file" ) == 0 )
254 opt.output_file = q;
255 else if( strcmp( p, "subject_name" ) == 0 )
256 {
257 opt.subject_name = q;
258 }
259 else if( strcmp( p, "issuer_name" ) == 0 )
260 {
261 opt.issuer_name = q;
262 }
263 else if( strcmp( p, "not_before" ) == 0 )
264 {
265 opt.not_before = q;
266 }
267 else if( strcmp( p, "not_after" ) == 0 )
268 {
269 opt.not_after = q;
270 }
271 else if( strcmp( p, "serial" ) == 0 )
272 {
273 opt.serial = q;
274 }
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200275 else if( strcmp( p, "selfsign" ) == 0 )
276 {
277 opt.selfsign = atoi( q );
278 if( opt.selfsign < 0 || opt.selfsign > 1 )
279 goto usage;
280 }
Paul Bakker15162a02013-09-06 19:27:21 +0200281 else if( strcmp( p, "is_ca" ) == 0 )
282 {
283 opt.is_ca = atoi( q );
284 if( opt.is_ca < 0 || opt.is_ca > 1 )
285 goto usage;
286 }
287 else if( strcmp( p, "max_pathlen" ) == 0 )
288 {
289 opt.max_pathlen = atoi( q );
290 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
291 goto usage;
292 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293 else if( strcmp( p, "key_usage" ) == 0 )
294 {
295 while( q != NULL )
296 {
297 if( ( r = strchr( q, ',' ) ) != NULL )
298 *r++ = '\0';
299
300 if( strcmp( q, "digital_signature" ) == 0 )
301 opt.key_usage |= KU_DIGITAL_SIGNATURE;
302 else if( strcmp( q, "non_repudiation" ) == 0 )
303 opt.key_usage |= KU_NON_REPUDIATION;
304 else if( strcmp( q, "key_encipherment" ) == 0 )
305 opt.key_usage |= KU_KEY_ENCIPHERMENT;
306 else if( strcmp( q, "data_encipherment" ) == 0 )
307 opt.key_usage |= KU_DATA_ENCIPHERMENT;
308 else if( strcmp( q, "key_agreement" ) == 0 )
309 opt.key_usage |= KU_KEY_AGREEMENT;
310 else if( strcmp( q, "key_cert_sign" ) == 0 )
311 opt.key_usage |= KU_KEY_CERT_SIGN;
312 else if( strcmp( q, "crl_sign" ) == 0 )
313 opt.key_usage |= KU_CRL_SIGN;
314 else
315 goto usage;
316
317 q = r;
318 }
319 }
320 else if( strcmp( p, "ns_cert_type" ) == 0 )
321 {
322 while( q != NULL )
323 {
324 if( ( r = strchr( q, ',' ) ) != NULL )
325 *r++ = '\0';
326
327 if( strcmp( q, "ssl_client" ) == 0 )
328 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
329 else if( strcmp( q, "ssl_server" ) == 0 )
330 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
331 else if( strcmp( q, "email" ) == 0 )
332 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
333 else if( strcmp( q, "object_signing" ) == 0 )
334 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
335 else if( strcmp( q, "ssl_ca" ) == 0 )
336 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
337 else if( strcmp( q, "email_ca" ) == 0 )
338 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
339 else if( strcmp( q, "object_signing_ca" ) == 0 )
340 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
341 else
342 goto usage;
343
344 q = r;
345 }
346 }
347 else
348 goto usage;
349 }
350
Paul Bakker1014e952013-09-09 13:59:42 +0200351 printf("\n");
352
Paul Bakker9397dcb2013-09-06 09:55:26 +0200353 // Parse serial to MPI
354 //
355 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
356 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200357 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200358 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
359 goto exit;
360 }
361
Paul Bakker1014e952013-09-09 13:59:42 +0200362 // Parse issuer certificate if present
363 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200364 if( !opt.selfsign && strlen( opt.issuer_crt ) )
Paul Bakker1014e952013-09-09 13:59:42 +0200365 {
366 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200367 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200368 */
369 printf( " . Loading the issuer certificate ..." );
370 fflush( stdout );
371
372 if( ( ret = x509parse_crtfile( &issuer_crt, opt.issuer_crt ) ) != 0 )
373 {
Paul Bakker1014e952013-09-09 13:59:42 +0200374 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200375 printf( " failed\n ! x509parse_crtfile returned -0x%02x - %s\n\n", -ret, buf );
376 goto exit;
377 }
378
Paul Bakkere2673fb2013-09-09 15:52:07 +0200379 ret = x509parse_dn_gets( issuer_name, sizeof(issuer_name),
380 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200381 if( ret < 0 )
382 {
Paul Bakker1014e952013-09-09 13:59:42 +0200383 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200384 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
385 goto exit;
386 }
387
Paul Bakkere2673fb2013-09-09 15:52:07 +0200388 opt.issuer_name = issuer_name;
389
390 printf( " ok\n" );
391 }
392
393 // Parse certificate request if present
394 //
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200395 if( !opt.selfsign && strlen( opt.request_file ) )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200396 {
397 /*
398 * 1.0.b. Load the CSR
399 */
400 printf( " . Loading the certificate request ..." );
401 fflush( stdout );
402
403 if( ( ret = x509parse_csrfile( &csr, opt.request_file ) ) != 0 )
404 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200405 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200406 printf( " failed\n ! x509parse_csrfile returned -0x%02x - %s\n\n", -ret, buf );
407 goto exit;
408 }
409
410 ret = x509parse_dn_gets( subject_name, sizeof(subject_name),
411 &csr.subject );
412 if( ret < 0 )
413 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200414 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200415 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
416 goto exit;
417 }
418
419 opt.subject_name = subject_name;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200420 subject_rsa = pk_rsa( csr.pk );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200421
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200422 printf( " ok\n" );
423 }
424
425 /*
426 * 1.1. Load the keys
427 */
428 if( !opt.selfsign && !strlen( opt.request_file ) )
429 {
430 printf( " . Loading the subject key ..." );
431 fflush( stdout );
432
433 ret = x509parse_keyfile_rsa( &loaded_subject_rsa, opt.subject_key,
434 opt.subject_pwd );
435 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200436 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 error_strerror( ret, buf, 1024 );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200438 printf( " failed\n ! x509parse_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200439 goto exit;
440 }
Paul Bakker1014e952013-09-09 13:59:42 +0200441
442 printf( " ok\n" );
443 }
444
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200445 printf( " . Loading the issuer key ..." );
446 fflush( stdout );
447
448 ret = x509parse_keyfile_rsa( &loaded_issuer_rsa, opt.issuer_key,
449 opt.issuer_pwd );
450 if( ret != 0 )
451 {
452 error_strerror( ret, buf, 1024 );
453 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
454 goto exit;
455 }
456
457 // Check if key and issuer certificate match
458 //
459 if( strlen( opt.issuer_crt ) )
460 {
461 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
462 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N, &issuer_rsa->N ) != 0 ||
463 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E, &issuer_rsa->E ) != 0 )
464 {
465 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
466 ret = -1;
467 goto exit;
468 }
469 }
470
471 printf( " ok\n" );
472
473 if( opt.selfsign )
474 {
475 opt.issuer_name = opt.subject_name;
476 subject_rsa = issuer_rsa;
477 }
478
479 x509write_crt_set_subject_key( &crt, subject_rsa );
480 x509write_crt_set_issuer_key( &crt, issuer_rsa );
481
Paul Bakker9397dcb2013-09-06 09:55:26 +0200482 /*
483 * 1.0. Check the names for validity
484 */
485 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
486 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200488 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
489 goto exit;
490 }
491
492 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
493 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200494 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200495 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
496 goto exit;
497 }
498
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 printf( " . Setting certificate values ..." );
500 fflush( stdout );
501
502 ret = x509write_crt_set_serial( &crt, &serial );
503 if( ret != 0 )
504 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200505 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200506 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
507 goto exit;
508 }
509
510 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
511 if( ret != 0 )
512 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200513 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200514 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
515 goto exit;
516 }
517
518 printf( " ok\n" );
519
Paul Bakker15162a02013-09-06 19:27:21 +0200520 printf( " . Adding the Basic Constraints extension ..." );
521 fflush( stdout );
522
523 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
524 opt.max_pathlen );
525 if( ret != 0 )
526 {
Paul Bakker15162a02013-09-06 19:27:21 +0200527 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200528 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
529 goto exit;
530 }
531
532 printf( " ok\n" );
533
534 printf( " . Adding the Subject Key Identifier ..." );
535 fflush( stdout );
536
537 ret = x509write_crt_set_subject_key_identifier( &crt );
538 if( ret != 0 )
539 {
Paul Bakker15162a02013-09-06 19:27:21 +0200540 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200541 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
542 goto exit;
543 }
544
545 printf( " ok\n" );
546
547 printf( " . Adding the Authority Key Identifier ..." );
548 fflush( stdout );
549
550 ret = x509write_crt_set_authority_key_identifier( &crt );
551 if( ret != 0 )
552 {
Paul Bakker15162a02013-09-06 19:27:21 +0200553 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200554 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
555 goto exit;
556 }
557
558 printf( " ok\n" );
559
Paul Bakker52be08c2013-09-09 12:37:54 +0200560 if( opt.key_usage )
561 {
562 printf( " . Adding the Key Usage extension ..." );
563 fflush( stdout );
564
565 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
566 if( ret != 0 )
567 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200568 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200569 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
570 goto exit;
571 }
572
573 printf( " ok\n" );
574 }
575
576 if( opt.ns_cert_type )
577 {
578 printf( " . Adding the NS Cert Type extension ..." );
579 fflush( stdout );
580
581 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
582 if( ret != 0 )
583 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200584 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200585 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
586 goto exit;
587 }
588
589 printf( " ok\n" );
590 }
591
Paul Bakker9397dcb2013-09-06 09:55:26 +0200592 /*
593 * 1.2. Writing the request
594 */
595 printf( " . Writing the certificate..." );
596 fflush( stdout );
597
598 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
599 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200600 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200601 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
602 goto exit;
603 }
604
605 printf( " ok\n" );
606
607exit:
608 x509write_crt_free( &crt );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200609 rsa_free( &loaded_subject_rsa );
610 rsa_free( &loaded_issuer_rsa );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200611 mpi_free( &serial );
612
613#if defined(_WIN32)
614 printf( " + Press Enter to exit this program.\n" );
615 fflush( stdout ); getchar();
616#endif
617
618 return( ret );
619}
620#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
621 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */