blob: 9c3766c93eb372d6ca8041e7883aa52c5c77bff4 [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) || \
44 !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
45int main( int argc, char *argv[] )
46{
47 ((void) argc);
48 ((void) argv);
49
50 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
51 "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n");
52 return( 0 );
53}
54#else
55
Paul Bakker1014e952013-09-09 13:59:42 +020056#define DFL_ISSUER_CRT ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020057#define DFL_SUBJECT_KEY "subject.key"
58#define DFL_ISSUER_KEY "ca.key"
59#define DFL_SUBJECT_PWD ""
60#define DFL_ISSUER_PWD ""
61#define DFL_OUTPUT_FILENAME "cert.crt"
62#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
63#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
64#define DFL_NOT_BEFORE "20010101000000"
65#define DFL_NOT_AFTER "20301231235959"
66#define DFL_SERIAL "1"
Paul Bakker15162a02013-09-06 19:27:21 +020067#define DFL_IS_CA 0
68#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020069#define DFL_KEY_USAGE 0
70#define DFL_NS_CERT_TYPE 0
71
72/*
73 * global options
74 */
75struct options
76{
Paul Bakker1014e952013-09-09 13:59:42 +020077 char *issuer_crt; /* filename of the issuer certificate */
Paul Bakker9397dcb2013-09-06 09:55:26 +020078 char *subject_key; /* filename of the subject key file */
79 char *issuer_key; /* filename of the issuer key file */
80 char *subject_pwd; /* password for the subject key file */
81 char *issuer_pwd; /* password for the issuer key file */
82 char *output_file; /* where to store the constructed key file */
83 char *subject_name; /* subject name for certificate */
84 char *issuer_name; /* issuer name for certificate */
85 char *not_before; /* validity period not before */
86 char *not_after; /* validity period not after */
87 char *serial; /* serial number string */
Paul Bakker15162a02013-09-06 19:27:21 +020088 int is_ca; /* is a CA certificate */
89 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020090 unsigned char key_usage; /* key usage flags */
91 unsigned char ns_cert_type; /* NS cert type */
92} opt;
93
94int write_certificate( x509write_cert *crt, char *output_file )
95{
96 int ret;
97 FILE *f;
98 unsigned char output_buf[4096];
99 size_t len = 0;
100
101 memset( output_buf, 0, 4096 );
102 if( ( ret = x509write_crt_pem( crt, output_buf, 4096 ) ) < 0 )
103 return( ret );
104
105 len = strlen( (char *) output_buf );
106
107 if( ( f = fopen( output_file, "w" ) ) == NULL )
108 return( -1 );
109
110 if( fwrite( output_buf, 1, len, f ) != len )
111 return( -1 );
112
113 fclose(f);
114
115 return( 0 );
116}
117
118#define USAGE \
119 "\n usage: cert_write param=<>...\n" \
120 "\n acceptable parameters:\n" \
121 " subject_key=%%s default: subject.key\n" \
122 " subject_pwd=%%s default: (empty)\n" \
Paul Bakker1014e952013-09-09 13:59:42 +0200123 " issuer_crt=%%s default: (empty)\n" \
124 " If issuer_crt is specified, issuer_name is\n" \
125 " ignored!\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200126 " issuer_key=%%s default: ca.key\n" \
127 " issuer_pwd=%%s default: (empty)\n" \
128 " output_file=%%s default: cert.crt\n" \
129 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
130 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
131 " serial=%%s default: 1\n" \
132 " not_before=%%s default: 20010101000000\n"\
133 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200134 " is_ca=%%d default: 0 (disabled)\n" \
135 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200136 " key_usage=%%s default: (empty)\n" \
137 " Comma-separated-list of values:\n" \
138 " digital_signature\n" \
139 " non_repudiation\n" \
140 " key_encipherment\n" \
141 " data_encipherment\n" \
142 " key_agreement\n" \
143 " key_certificate_sign\n" \
144 " crl_sign\n" \
145 " ns_cert_type=%%s default: (empty)\n" \
146 " Comma-separated-list of values:\n" \
147 " ssl_client\n" \
148 " ssl_server\n" \
149 " email\n" \
150 " object_signing\n" \
151 " ssl_ca\n" \
152 " email_ca\n" \
153 " object_signing_ca\n" \
154 "\n"
155
156int main( int argc, char *argv[] )
157{
158 int ret = 0;
Paul Bakker1014e952013-09-09 13:59:42 +0200159 x509_cert issuer_crt;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200160 rsa_context issuer_rsa, subject_rsa;
161 char buf[1024];
162 int i, j, n;
163 char *p, *q, *r;
164 x509write_cert crt;
165 mpi serial;
166
167 /*
168 * Set to sane values
169 */
170 x509write_crt_init( &crt );
171 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
172 rsa_init( &issuer_rsa, RSA_PKCS_V15, 0 );
173 rsa_init( &subject_rsa, RSA_PKCS_V15, 0 );
174 mpi_init( &serial );
Paul Bakker1014e952013-09-09 13:59:42 +0200175 memset( &issuer_crt, 0, sizeof(x509_cert) );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200176 memset( buf, 0, 1024 );
177
178 if( argc == 0 )
179 {
180 usage:
181 printf( USAGE );
182 ret = 1;
183 goto exit;
184 }
185
Paul Bakker1014e952013-09-09 13:59:42 +0200186 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 opt.subject_key = DFL_SUBJECT_KEY;
188 opt.issuer_key = DFL_ISSUER_KEY;
189 opt.subject_pwd = DFL_SUBJECT_PWD;
190 opt.issuer_pwd = DFL_ISSUER_PWD;
191 opt.output_file = DFL_OUTPUT_FILENAME;
192 opt.subject_name = DFL_SUBJECT_NAME;
193 opt.issuer_name = DFL_ISSUER_NAME;
194 opt.not_before = DFL_NOT_BEFORE;
195 opt.not_after = DFL_NOT_AFTER;
196 opt.serial = DFL_SERIAL;
Paul Bakker15162a02013-09-06 19:27:21 +0200197 opt.is_ca = DFL_IS_CA;
198 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199 opt.key_usage = DFL_KEY_USAGE;
200 opt.ns_cert_type = DFL_NS_CERT_TYPE;
201
202 for( i = 1; i < argc; i++ )
203 {
204
205 p = argv[i];
206 if( ( q = strchr( p, '=' ) ) == NULL )
207 goto usage;
208 *q++ = '\0';
209
210 n = strlen( p );
211 for( j = 0; j < n; j++ )
212 {
213 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
214 argv[i][j] |= 0x20;
215 }
216
217 if( strcmp( p, "subject_key" ) == 0 )
218 opt.subject_key = q;
219 else if( strcmp( p, "issuer_key" ) == 0 )
220 opt.issuer_key = q;
221 else if( strcmp( p, "subject_pwd" ) == 0 )
222 opt.subject_pwd = q;
223 else if( strcmp( p, "issuer_pwd" ) == 0 )
224 opt.issuer_pwd = q;
Paul Bakker1014e952013-09-09 13:59:42 +0200225 else if( strcmp( p, "issuer_crt" ) == 0 )
226 opt.issuer_crt = q;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227 else if( strcmp( p, "output_file" ) == 0 )
228 opt.output_file = q;
229 else if( strcmp( p, "subject_name" ) == 0 )
230 {
231 opt.subject_name = q;
232 }
233 else if( strcmp( p, "issuer_name" ) == 0 )
234 {
235 opt.issuer_name = q;
236 }
237 else if( strcmp( p, "not_before" ) == 0 )
238 {
239 opt.not_before = q;
240 }
241 else if( strcmp( p, "not_after" ) == 0 )
242 {
243 opt.not_after = q;
244 }
245 else if( strcmp( p, "serial" ) == 0 )
246 {
247 opt.serial = q;
248 }
Paul Bakker15162a02013-09-06 19:27:21 +0200249 else if( strcmp( p, "is_ca" ) == 0 )
250 {
251 opt.is_ca = atoi( q );
252 if( opt.is_ca < 0 || opt.is_ca > 1 )
253 goto usage;
254 }
255 else if( strcmp( p, "max_pathlen" ) == 0 )
256 {
257 opt.max_pathlen = atoi( q );
258 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
259 goto usage;
260 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200261 else if( strcmp( p, "key_usage" ) == 0 )
262 {
263 while( q != NULL )
264 {
265 if( ( r = strchr( q, ',' ) ) != NULL )
266 *r++ = '\0';
267
268 if( strcmp( q, "digital_signature" ) == 0 )
269 opt.key_usage |= KU_DIGITAL_SIGNATURE;
270 else if( strcmp( q, "non_repudiation" ) == 0 )
271 opt.key_usage |= KU_NON_REPUDIATION;
272 else if( strcmp( q, "key_encipherment" ) == 0 )
273 opt.key_usage |= KU_KEY_ENCIPHERMENT;
274 else if( strcmp( q, "data_encipherment" ) == 0 )
275 opt.key_usage |= KU_DATA_ENCIPHERMENT;
276 else if( strcmp( q, "key_agreement" ) == 0 )
277 opt.key_usage |= KU_KEY_AGREEMENT;
278 else if( strcmp( q, "key_cert_sign" ) == 0 )
279 opt.key_usage |= KU_KEY_CERT_SIGN;
280 else if( strcmp( q, "crl_sign" ) == 0 )
281 opt.key_usage |= KU_CRL_SIGN;
282 else
283 goto usage;
284
285 q = r;
286 }
287 }
288 else if( strcmp( p, "ns_cert_type" ) == 0 )
289 {
290 while( q != NULL )
291 {
292 if( ( r = strchr( q, ',' ) ) != NULL )
293 *r++ = '\0';
294
295 if( strcmp( q, "ssl_client" ) == 0 )
296 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
297 else if( strcmp( q, "ssl_server" ) == 0 )
298 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
299 else if( strcmp( q, "email" ) == 0 )
300 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
301 else if( strcmp( q, "object_signing" ) == 0 )
302 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
303 else if( strcmp( q, "ssl_ca" ) == 0 )
304 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
305 else if( strcmp( q, "email_ca" ) == 0 )
306 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
307 else if( strcmp( q, "object_signing_ca" ) == 0 )
308 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
309 else
310 goto usage;
311
312 q = r;
313 }
314 }
315 else
316 goto usage;
317 }
318
Paul Bakker1014e952013-09-09 13:59:42 +0200319 printf("\n");
320
Paul Bakker9397dcb2013-09-06 09:55:26 +0200321 // Parse serial to MPI
322 //
323 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
324 {
325#ifdef POLARSSL_ERROR_C
326 error_strerror( ret, buf, 1024 );
327#endif
328 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
329 goto exit;
330 }
331
Paul Bakker1014e952013-09-09 13:59:42 +0200332 // Parse issuer certificate if present
333 //
334 if( strlen( opt.issuer_crt ) )
335 {
336 /*
337 * 1.0. Load the certificates
338 */
339 printf( " . Loading the issuer certificate ..." );
340 fflush( stdout );
341
342 if( ( ret = x509parse_crtfile( &issuer_crt, opt.issuer_crt ) ) != 0 )
343 {
344#ifdef POLARSSL_ERROR_C
345 error_strerror( ret, buf, 1024 );
346#endif
347 printf( " failed\n ! x509parse_crtfile returned -0x%02x - %s\n\n", -ret, buf );
348 goto exit;
349 }
350
351 ret = x509parse_dn_gets( buf, sizeof(buf), &issuer_crt.issuer );
352 if( ret < 0 )
353 {
354#ifdef POLARSSL_ERROR_C
355 error_strerror( ret, buf, 1024 );
356#endif
357 printf( " failed\n ! x509parse_dn_gets returned -0x%02x - %s\n\n", -ret, buf );
358 goto exit;
359 }
360
361 opt.issuer_name = buf;
362
363 printf( " ok\n" );
364 }
365
Paul Bakker9397dcb2013-09-06 09:55:26 +0200366 /*
367 * 1.0. Check the names for validity
368 */
369 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
370 {
371#ifdef POLARSSL_ERROR_C
372 error_strerror( ret, buf, 1024 );
373#endif
374 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
375 goto exit;
376 }
377
378 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
379 {
380#ifdef POLARSSL_ERROR_C
381 error_strerror( ret, buf, 1024 );
382#endif
383 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
384 goto exit;
385 }
386
387 /*
388 * 1.1. Load the keys
389 */
Paul Bakker1014e952013-09-09 13:59:42 +0200390 printf( " . Loading the subject key ..." );
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391 fflush( stdout );
392
393 ret = x509parse_keyfile_rsa( &subject_rsa, opt.subject_key, opt.subject_pwd );
394
395 if( ret != 0 )
396 {
397#ifdef POLARSSL_ERROR_C
398 error_strerror( ret, buf, 1024 );
399#endif
400 printf( " failed\n ! x509parse_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
401 goto exit;
402 }
403
404 x509write_crt_set_subject_key( &crt, &subject_rsa );
405
406 printf( " ok\n" );
407
408 printf( " . Loading the issuer key ..." );
409 fflush( stdout );
410
411 ret = x509parse_keyfile_rsa( &issuer_rsa, opt.issuer_key, opt.issuer_pwd );
412
413 if( ret != 0 )
414 {
415#ifdef POLARSSL_ERROR_C
416 error_strerror( ret, buf, 1024 );
417#endif
418 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
419 goto exit;
420 }
421
Paul Bakker1014e952013-09-09 13:59:42 +0200422 // Check if key and issuer certificate match
423 //
424 if( strlen( opt.issuer_crt ) )
425 {
426 if( !pk_can_do( &issuer_crt.pk, POLARSSL_PK_RSA ) ||
427 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->N, &issuer_rsa.N ) != 0 ||
428 mpi_cmp_mpi( &pk_rsa( issuer_crt.pk )->E, &issuer_rsa.E ) != 0 )
429 {
430 printf( " failed\n ! issuer_key does not match issuer certificate\n\n" );
431 ret = -1;
432 goto exit;
433 }
434 }
435
Paul Bakker9397dcb2013-09-06 09:55:26 +0200436 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
437
438 printf( " ok\n" );
439
440 printf( " . Setting certificate values ..." );
441 fflush( stdout );
442
443 ret = x509write_crt_set_serial( &crt, &serial );
444 if( ret != 0 )
445 {
446#ifdef POLARSSL_ERROR_C
447 error_strerror( ret, buf, 1024 );
448#endif
449 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
450 goto exit;
451 }
452
453 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
454 if( ret != 0 )
455 {
456#ifdef POLARSSL_ERROR_C
457 error_strerror( ret, buf, 1024 );
458#endif
459 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
460 goto exit;
461 }
462
463 printf( " ok\n" );
464
Paul Bakker15162a02013-09-06 19:27:21 +0200465 printf( " . Adding the Basic Constraints extension ..." );
466 fflush( stdout );
467
468 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
469 opt.max_pathlen );
470 if( ret != 0 )
471 {
472#ifdef POLARSSL_ERROR_C
473 error_strerror( ret, buf, 1024 );
474#endif
475 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
476 goto exit;
477 }
478
479 printf( " ok\n" );
480
481 printf( " . Adding the Subject Key Identifier ..." );
482 fflush( stdout );
483
484 ret = x509write_crt_set_subject_key_identifier( &crt );
485 if( ret != 0 )
486 {
487#ifdef POLARSSL_ERROR_C
488 error_strerror( ret, buf, 1024 );
489#endif
490 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
491 goto exit;
492 }
493
494 printf( " ok\n" );
495
496 printf( " . Adding the Authority Key Identifier ..." );
497 fflush( stdout );
498
499 ret = x509write_crt_set_authority_key_identifier( &crt );
500 if( ret != 0 )
501 {
502#ifdef POLARSSL_ERROR_C
503 error_strerror( ret, buf, 1024 );
504#endif
505 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
506 goto exit;
507 }
508
509 printf( " ok\n" );
510
Paul Bakker52be08c2013-09-09 12:37:54 +0200511 if( opt.key_usage )
512 {
513 printf( " . Adding the Key Usage extension ..." );
514 fflush( stdout );
515
516 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
517 if( ret != 0 )
518 {
519#ifdef POLARSSL_ERROR_C
520 error_strerror( ret, buf, 1024 );
521#endif
522 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
523 goto exit;
524 }
525
526 printf( " ok\n" );
527 }
528
529 if( opt.ns_cert_type )
530 {
531 printf( " . Adding the NS Cert Type extension ..." );
532 fflush( stdout );
533
534 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
535 if( ret != 0 )
536 {
537#ifdef POLARSSL_ERROR_C
538 error_strerror( ret, buf, 1024 );
539#endif
540 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
541 goto exit;
542 }
543
544 printf( " ok\n" );
545 }
546
Paul Bakker9397dcb2013-09-06 09:55:26 +0200547 /*
548 * 1.2. Writing the request
549 */
550 printf( " . Writing the certificate..." );
551 fflush( stdout );
552
553 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
554 {
555#ifdef POLARSSL_ERROR_C
556 error_strerror( ret, buf, 1024 );
557#endif
558 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
559 goto exit;
560 }
561
562 printf( " ok\n" );
563
564exit:
565 x509write_crt_free( &crt );
566 rsa_free( &subject_rsa );
567 rsa_free( &issuer_rsa );
568 mpi_free( &serial );
569
570#if defined(_WIN32)
571 printf( " + Press Enter to exit this program.\n" );
572 fflush( stdout ); getchar();
573#endif
574
575 return( ret );
576}
577#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
578 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */