blob: 91d448a618165a3ff3966e353412e902aaadf351 [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 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 Bakker1014e952013-09-09 13:59:42 +020080 char *issuer_crt; /* filename of the issuer certificate */
Paul Bakkere2673fb2013-09-09 15:52:07 +020081 char *request_file; /* filename of the certificate request */
Paul Bakker9397dcb2013-09-06 09:55:26 +020082 char *subject_key; /* filename of the subject key file */
83 char *issuer_key; /* filename of the issuer key file */
84 char *subject_pwd; /* password for the subject key file */
85 char *issuer_pwd; /* password for the issuer key file */
86 char *output_file; /* where to store the constructed key file */
87 char *subject_name; /* subject name for certificate */
88 char *issuer_name; /* issuer name for certificate */
89 char *not_before; /* validity period not before */
90 char *not_after; /* validity period not after */
91 char *serial; /* serial number string */
Paul Bakker15162a02013-09-06 19:27:21 +020092 int is_ca; /* is a CA certificate */
93 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020094 unsigned char key_usage; /* key usage flags */
95 unsigned char ns_cert_type; /* NS cert type */
96} opt;
97
98int write_certificate( x509write_cert *crt, char *output_file )
99{
100 int ret;
101 FILE *f;
102 unsigned char output_buf[4096];
103 size_t len = 0;
104
105 memset( output_buf, 0, 4096 );
106 if( ( ret = x509write_crt_pem( crt, output_buf, 4096 ) ) < 0 )
107 return( ret );
108
109 len = strlen( (char *) output_buf );
110
111 if( ( f = fopen( output_file, "w" ) ) == NULL )
112 return( -1 );
113
114 if( fwrite( output_buf, 1, len, f ) != len )
115 return( -1 );
116
117 fclose(f);
118
119 return( 0 );
120}
121
122#define USAGE \
123 "\n usage: cert_write param=<>...\n" \
124 "\n acceptable parameters:\n" \
Paul Bakkere2673fb2013-09-09 15:52:07 +0200125 " request_file=%%s default: (empty)\n" \
126 " If request_file is specified, subject_key,\n" \
127 " subject_pwd and subject_name are ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200128 " subject_key=%%s default: subject.key\n" \
129 " subject_pwd=%%s default: (empty)\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200130 " issuer_crt=%%s default: (empty)\n" \
131 " If issuer_crt is specified, issuer_name is\n" \
132 " ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200133 " issuer_key=%%s default: ca.key\n" \
134 " issuer_pwd=%%s default: (empty)\n" \
135 " output_file=%%s default: cert.crt\n" \
136 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
137 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
138 " serial=%%s default: 1\n" \
139 " not_before=%%s default: 20010101000000\n"\
140 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200141 " is_ca=%%d default: 0 (disabled)\n" \
142 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200143 " key_usage=%%s default: (empty)\n" \
144 " Comma-separated-list of values:\n" \
145 " digital_signature\n" \
146 " non_repudiation\n" \
147 " key_encipherment\n" \
148 " data_encipherment\n" \
149 " key_agreement\n" \
150 " key_certificate_sign\n" \
151 " crl_sign\n" \
152 " ns_cert_type=%%s default: (empty)\n" \
153 " Comma-separated-list of values:\n" \
154 " ssl_client\n" \
155 " ssl_server\n" \
156 " email\n" \
157 " object_signing\n" \
158 " ssl_ca\n" \
159 " email_ca\n" \
160 " object_signing_ca\n" \
161 "\n"
162
163int main( int argc, char *argv[] )
164{
165 int ret = 0;
Paul Bakker1014e952013-09-09 13:59:42 +0200166 x509_cert issuer_crt;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200167 rsa_context issuer_rsa, subject_rsa;
168 char buf[1024];
Paul Bakkere2673fb2013-09-09 15:52:07 +0200169 char issuer_name[128];
170 char subject_name[128];
Paul Bakker9397dcb2013-09-06 09:55:26 +0200171 int i, j, n;
172 char *p, *q, *r;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200173 x509_csr csr;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200174 x509write_cert crt;
175 mpi serial;
176
177 /*
178 * Set to sane values
179 */
180 x509write_crt_init( &crt );
181 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
182 rsa_init( &issuer_rsa, RSA_PKCS_V15, 0 );
183 rsa_init( &subject_rsa, RSA_PKCS_V15, 0 );
184 mpi_init( &serial );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200185 memset( &csr, 0, sizeof(x509_csr) );
Paul Bakker1014e952013-09-09 13:59:42 +0200186 memset( &issuer_crt, 0, sizeof(x509_cert) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 memset( buf, 0, 1024 );
188
189 if( argc == 0 )
190 {
191 usage:
192 printf( USAGE );
193 ret = 1;
194 goto exit;
195 }
196
Paul Bakker1014e952013-09-09 13:59:42 +0200197 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200198 opt.request_file = DFL_REQUEST_FILE;
199 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200200 opt.subject_key = DFL_SUBJECT_KEY;
201 opt.issuer_key = DFL_ISSUER_KEY;
202 opt.subject_pwd = DFL_SUBJECT_PWD;
203 opt.issuer_pwd = DFL_ISSUER_PWD;
204 opt.output_file = DFL_OUTPUT_FILENAME;
205 opt.subject_name = DFL_SUBJECT_NAME;
206 opt.issuer_name = DFL_ISSUER_NAME;
207 opt.not_before = DFL_NOT_BEFORE;
208 opt.not_after = DFL_NOT_AFTER;
209 opt.serial = DFL_SERIAL;
Paul Bakker15162a02013-09-06 19:27:21 +0200210 opt.is_ca = DFL_IS_CA;
211 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212 opt.key_usage = DFL_KEY_USAGE;
213 opt.ns_cert_type = DFL_NS_CERT_TYPE;
214
215 for( i = 1; i < argc; i++ )
216 {
217
218 p = argv[i];
219 if( ( q = strchr( p, '=' ) ) == NULL )
220 goto usage;
221 *q++ = '\0';
222
223 n = strlen( p );
224 for( j = 0; j < n; j++ )
225 {
226 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
227 argv[i][j] |= 0x20;
228 }
229
Paul Bakkere2673fb2013-09-09 15:52:07 +0200230 if( strcmp( p, "request_file" ) == 0 )
231 opt.request_file = q;
232 else if( strcmp( p, "subject_key" ) == 0 )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200233 opt.subject_key = q;
234 else if( strcmp( p, "issuer_key" ) == 0 )
235 opt.issuer_key = q;
236 else if( strcmp( p, "subject_pwd" ) == 0 )
237 opt.subject_pwd = q;
238 else if( strcmp( p, "issuer_pwd" ) == 0 )
239 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200240 else if( strcmp( p, "issuer_crt" ) == 0 )
241 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200242 else if( strcmp( p, "output_file" ) == 0 )
243 opt.output_file = q;
244 else if( strcmp( p, "subject_name" ) == 0 )
245 {
246 opt.subject_name = q;
247 }
248 else if( strcmp( p, "issuer_name" ) == 0 )
249 {
250 opt.issuer_name = q;
251 }
252 else if( strcmp( p, "not_before" ) == 0 )
253 {
254 opt.not_before = q;
255 }
256 else if( strcmp( p, "not_after" ) == 0 )
257 {
258 opt.not_after = q;
259 }
260 else if( strcmp( p, "serial" ) == 0 )
261 {
262 opt.serial = q;
263 }
Paul Bakker15162a02013-09-06 19:27:21 +0200264 else if( strcmp( p, "is_ca" ) == 0 )
265 {
266 opt.is_ca = atoi( q );
267 if( opt.is_ca < 0 || opt.is_ca > 1 )
268 goto usage;
269 }
270 else if( strcmp( p, "max_pathlen" ) == 0 )
271 {
272 opt.max_pathlen = atoi( q );
273 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
274 goto usage;
275 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200276 else if( strcmp( p, "key_usage" ) == 0 )
277 {
278 while( q != NULL )
279 {
280 if( ( r = strchr( q, ',' ) ) != NULL )
281 *r++ = '\0';
282
283 if( strcmp( q, "digital_signature" ) == 0 )
284 opt.key_usage |= KU_DIGITAL_SIGNATURE;
285 else if( strcmp( q, "non_repudiation" ) == 0 )
286 opt.key_usage |= KU_NON_REPUDIATION;
287 else if( strcmp( q, "key_encipherment" ) == 0 )
288 opt.key_usage |= KU_KEY_ENCIPHERMENT;
289 else if( strcmp( q, "data_encipherment" ) == 0 )
290 opt.key_usage |= KU_DATA_ENCIPHERMENT;
291 else if( strcmp( q, "key_agreement" ) == 0 )
292 opt.key_usage |= KU_KEY_AGREEMENT;
293 else if( strcmp( q, "key_cert_sign" ) == 0 )
294 opt.key_usage |= KU_KEY_CERT_SIGN;
295 else if( strcmp( q, "crl_sign" ) == 0 )
296 opt.key_usage |= KU_CRL_SIGN;
297 else
298 goto usage;
299
300 q = r;
301 }
302 }
303 else if( strcmp( p, "ns_cert_type" ) == 0 )
304 {
305 while( q != NULL )
306 {
307 if( ( r = strchr( q, ',' ) ) != NULL )
308 *r++ = '\0';
309
310 if( strcmp( q, "ssl_client" ) == 0 )
311 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
312 else if( strcmp( q, "ssl_server" ) == 0 )
313 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
314 else if( strcmp( q, "email" ) == 0 )
315 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
316 else if( strcmp( q, "object_signing" ) == 0 )
317 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
318 else if( strcmp( q, "ssl_ca" ) == 0 )
319 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
320 else if( strcmp( q, "email_ca" ) == 0 )
321 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
322 else if( strcmp( q, "object_signing_ca" ) == 0 )
323 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
324 else
325 goto usage;
326
327 q = r;
328 }
329 }
330 else
331 goto usage;
332 }
333
Paul Bakker1014e952013-09-09 13:59:42 +0200334 printf("\n");
335
Paul Bakker9397dcb2013-09-06 09:55:26 +0200336 // Parse serial to MPI
337 //
338 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
339 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200340 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200341 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
342 goto exit;
343 }
344
Paul Bakker1014e952013-09-09 13:59:42 +0200345 // Parse issuer certificate if present
346 //
347 if( strlen( opt.issuer_crt ) )
348 {
349 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200350 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200351 */
352 printf( " . Loading the issuer certificate ..." );
353 fflush( stdout );
354
355 if( ( ret = x509parse_crtfile( &issuer_crt, opt.issuer_crt ) ) != 0 )
356 {
Paul Bakker1014e952013-09-09 13:59:42 +0200357 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200358 printf( " failed\n ! x509parse_crtfile returned -0x%02x - %s\n\n", -ret, buf );
359 goto exit;
360 }
361
Paul Bakkere2673fb2013-09-09 15:52:07 +0200362 ret = x509parse_dn_gets( issuer_name, sizeof(issuer_name),
363 &issuer_crt.issuer );
Paul Bakker1014e952013-09-09 13:59:42 +0200364 if( ret < 0 )
365 {
Paul Bakker1014e952013-09-09 13:59:42 +0200366 error_strerror( ret, buf, 1024 );
Paul Bakker1014e952013-09-09 13:59:42 +0200367 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
368 goto exit;
369 }
370
Paul Bakkere2673fb2013-09-09 15:52:07 +0200371 opt.issuer_name = issuer_name;
372
373 printf( " ok\n" );
374 }
375
376 // Parse certificate request if present
377 //
378 if( strlen( opt.request_file ) )
379 {
380 /*
381 * 1.0.b. Load the CSR
382 */
383 printf( " . Loading the certificate request ..." );
384 fflush( stdout );
385
386 if( ( ret = x509parse_csrfile( &csr, opt.request_file ) ) != 0 )
387 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200388 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200389 printf( " failed\n ! x509parse_csrfile returned -0x%02x - %s\n\n", -ret, buf );
390 goto exit;
391 }
392
393 ret = x509parse_dn_gets( subject_name, sizeof(subject_name),
394 &csr.subject );
395 if( ret < 0 )
396 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200397 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200398 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
399 goto exit;
400 }
401
402 opt.subject_name = subject_name;
403
404 if( ( ret = rsa_copy( &subject_rsa, pk_rsa( csr.pk ) ) ) != 0 )
405 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200406 error_strerror( ret, buf, 1024 );
Paul Bakkere2673fb2013-09-09 15:52:07 +0200407 printf( " failed\n ! rsa_copy returned -0x%02x - %s\n\n", -ret, buf );
408 goto exit;
409 }
Paul Bakker1014e952013-09-09 13:59:42 +0200410
411 printf( " ok\n" );
412 }
413
Paul Bakker9397dcb2013-09-06 09:55:26 +0200414 /*
415 * 1.0. Check the names for validity
416 */
417 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
418 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200420 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
421 goto exit;
422 }
423
424 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
425 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200426 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
428 goto exit;
429 }
430
431 /*
432 * 1.1. Load the keys
433 */
Paul Bakkere2673fb2013-09-09 15:52:07 +0200434 if( !strlen( opt.request_file ) )
Paul Bakker9397dcb2013-09-06 09:55:26 +0200435 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200436 printf( " . Loading the subject key ..." );
437 fflush( stdout );
438
439 ret = x509parse_keyfile_rsa( &subject_rsa, opt.subject_key,
440 opt.subject_pwd );
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_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
445 goto exit;
446 }
447
448 printf( " ok\n" );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200449 }
450
451 x509write_crt_set_subject_key( &crt, &subject_rsa );
452
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453
454 printf( " . Loading the issuer key ..." );
455 fflush( stdout );
456
457 ret = x509parse_keyfile_rsa( &issuer_rsa, opt.issuer_key, opt.issuer_pwd );
458
459 if( ret != 0 )
460 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200461 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200462 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
463 goto exit;
464 }
465
Paul Bakker1014e952013-09-09 13:59:42 +0200466 // Check if key and issuer certificate match
467 //
468 if( strlen( opt.issuer_crt ) )
469 {
470 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
471 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N, &issuer_rsa.N ) != 0 ||
472 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E, &issuer_rsa.E ) != 0 )
473 {
474 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
475 ret = -1;
476 goto exit;
477 }
478 }
479
Paul Bakker9397dcb2013-09-06 09:55:26 +0200480 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
481
482 printf( " ok\n" );
483
484 printf( " . Setting certificate values ..." );
485 fflush( stdout );
486
487 ret = x509write_crt_set_serial( &crt, &serial );
488 if( ret != 0 )
489 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
492 goto exit;
493 }
494
495 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
496 if( ret != 0 )
497 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200498 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
500 goto exit;
501 }
502
503 printf( " ok\n" );
504
Paul Bakker15162a02013-09-06 19:27:21 +0200505 printf( " . Adding the Basic Constraints extension ..." );
506 fflush( stdout );
507
508 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
509 opt.max_pathlen );
510 if( ret != 0 )
511 {
Paul Bakker15162a02013-09-06 19:27:21 +0200512 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200513 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
514 goto exit;
515 }
516
517 printf( " ok\n" );
518
519 printf( " . Adding the Subject Key Identifier ..." );
520 fflush( stdout );
521
522 ret = x509write_crt_set_subject_key_identifier( &crt );
523 if( ret != 0 )
524 {
Paul Bakker15162a02013-09-06 19:27:21 +0200525 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200526 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
527 goto exit;
528 }
529
530 printf( " ok\n" );
531
532 printf( " . Adding the Authority Key Identifier ..." );
533 fflush( stdout );
534
535 ret = x509write_crt_set_authority_key_identifier( &crt );
536 if( ret != 0 )
537 {
Paul Bakker15162a02013-09-06 19:27:21 +0200538 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200539 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
540 goto exit;
541 }
542
543 printf( " ok\n" );
544
Paul Bakker52be08c2013-09-09 12:37:54 +0200545 if( opt.key_usage )
546 {
547 printf( " . Adding the Key Usage extension ..." );
548 fflush( stdout );
549
550 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
551 if( ret != 0 )
552 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200553 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200554 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
555 goto exit;
556 }
557
558 printf( " ok\n" );
559 }
560
561 if( opt.ns_cert_type )
562 {
563 printf( " . Adding the NS Cert Type extension ..." );
564 fflush( stdout );
565
566 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
567 if( ret != 0 )
568 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200569 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200570 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
571 goto exit;
572 }
573
574 printf( " ok\n" );
575 }
576
Paul Bakker9397dcb2013-09-06 09:55:26 +0200577 /*
578 * 1.2. Writing the request
579 */
580 printf( " . Writing the certificate..." );
581 fflush( stdout );
582
583 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
584 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200585 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200586 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
587 goto exit;
588 }
589
590 printf( " ok\n" );
591
592exit:
593 x509write_crt_free( &crt );
594 rsa_free( &subject_rsa );
595 rsa_free( &issuer_rsa );
596 mpi_free( &serial );
597
598#if defined(_WIN32)
599 printf( " + Press Enter to exit this program.\n" );
600 fflush( stdout ); getchar();
601#endif
602
603 return( ret );
604}
605#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
606 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */