blob: 8943493e9b025a3a935ca468766bfd51c87eb98e [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;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200175 pk_context loaded_issuer_key, loaded_subject_key;
176 pk_context *issuer_key = &loaded_issuer_key,
177 *subject_key = &loaded_subject_key;
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 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200192 pk_init( &loaded_issuer_key );
193 pk_init( &loaded_subject_key );
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;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200420 subject_key = &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
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200433 ret = x509parse_keyfile( &loaded_subject_key, opt.subject_key,
434 opt.subject_pwd );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200435 if( ret != 0 )
Paul Bakkere2673fb2013-09-09 15:52:07 +0200436 {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200437 error_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200438 printf( " failed\n ! x509parse_keyfile 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
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200448 ret = x509parse_keyfile( &loaded_issuer_key, opt.issuer_key,
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200449 opt.issuer_pwd );
450 if( ret != 0 )
451 {
452 error_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200453 printf( " failed\n ! x509parse_keyfile returned -x%02x - %s\n\n", -ret, buf );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200454 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 ) ||
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200462 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N,
463 &pk_rsa( *issuer_key )->N ) != 0 ||
464 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E,
465 &pk_rsa( *issuer_key )->E ) != 0 )
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200466 {
467 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
468 ret = -1;
469 goto exit;
470 }
471 }
472
473 printf( " ok\n" );
474
475 if( opt.selfsign )
476 {
477 opt.issuer_name = opt.subject_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200478 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200479 }
480
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200481 x509write_crt_set_subject_key( &crt, subject_key );
482 x509write_crt_set_issuer_key( &crt, issuer_key );
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200483
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 /*
485 * 1.0. Check the names for validity
486 */
487 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
488 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200490 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
491 goto exit;
492 }
493
494 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
495 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200496 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200497 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
498 goto exit;
499 }
500
Paul Bakker9397dcb2013-09-06 09:55:26 +0200501 printf( " . Setting certificate values ..." );
502 fflush( stdout );
503
504 ret = x509write_crt_set_serial( &crt, &serial );
505 if( ret != 0 )
506 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200507 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200508 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
509 goto exit;
510 }
511
512 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
513 if( ret != 0 )
514 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200515 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200516 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
517 goto exit;
518 }
519
520 printf( " ok\n" );
521
Paul Bakker15162a02013-09-06 19:27:21 +0200522 printf( " . Adding the Basic Constraints extension ..." );
523 fflush( stdout );
524
525 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
526 opt.max_pathlen );
527 if( ret != 0 )
528 {
Paul Bakker15162a02013-09-06 19:27:21 +0200529 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200530 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
531 goto exit;
532 }
533
534 printf( " ok\n" );
535
536 printf( " . Adding the Subject Key Identifier ..." );
537 fflush( stdout );
538
539 ret = x509write_crt_set_subject_key_identifier( &crt );
540 if( ret != 0 )
541 {
Paul Bakker15162a02013-09-06 19:27:21 +0200542 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200543 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
544 goto exit;
545 }
546
547 printf( " ok\n" );
548
549 printf( " . Adding the Authority Key Identifier ..." );
550 fflush( stdout );
551
552 ret = x509write_crt_set_authority_key_identifier( &crt );
553 if( ret != 0 )
554 {
Paul Bakker15162a02013-09-06 19:27:21 +0200555 error_strerror( ret, buf, 1024 );
Paul Bakker15162a02013-09-06 19:27:21 +0200556 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
557 goto exit;
558 }
559
560 printf( " ok\n" );
561
Paul Bakker52be08c2013-09-09 12:37:54 +0200562 if( opt.key_usage )
563 {
564 printf( " . Adding the Key Usage extension ..." );
565 fflush( stdout );
566
567 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
568 if( ret != 0 )
569 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200570 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200571 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
572 goto exit;
573 }
574
575 printf( " ok\n" );
576 }
577
578 if( opt.ns_cert_type )
579 {
580 printf( " . Adding the NS Cert Type extension ..." );
581 fflush( stdout );
582
583 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
584 if( ret != 0 )
585 {
Paul Bakker52be08c2013-09-09 12:37:54 +0200586 error_strerror( ret, buf, 1024 );
Paul Bakker52be08c2013-09-09 12:37:54 +0200587 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
588 goto exit;
589 }
590
591 printf( " ok\n" );
592 }
593
Paul Bakker9397dcb2013-09-06 09:55:26 +0200594 /*
595 * 1.2. Writing the request
596 */
597 printf( " . Writing the certificate..." );
598 fflush( stdout );
599
600 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
601 {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200602 error_strerror( ret, buf, 1024 );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200603 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
604 goto exit;
605 }
606
607 printf( " ok\n" );
608
609exit:
610 x509write_crt_free( &crt );
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200611 pk_free( &loaded_subject_key );
612 pk_free( &loaded_issuer_key );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200613 mpi_free( &serial );
614
615#if defined(_WIN32)
616 printf( " + Press Enter to exit this program.\n" );
617 fflush( stdout ); getchar();
618#endif
619
620 return( ret );
621}
622#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
623 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */