blob: dc9f00a991675b737ab89c21ca7b87e77213cd49 [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
56#define DFL_SUBJECT_KEY "subject.key"
57#define DFL_ISSUER_KEY "ca.key"
58#define DFL_SUBJECT_PWD ""
59#define DFL_ISSUER_PWD ""
60#define DFL_OUTPUT_FILENAME "cert.crt"
61#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
62#define DFL_ISSUER_NAME "CN=CA,O=PolarSSL,C=NL"
63#define DFL_NOT_BEFORE "20010101000000"
64#define DFL_NOT_AFTER "20301231235959"
65#define DFL_SERIAL "1"
Paul Bakker15162a02013-09-06 19:27:21 +020066#define DFL_IS_CA 0
67#define DFL_MAX_PATHLEN -1
Paul Bakker9397dcb2013-09-06 09:55:26 +020068#define DFL_KEY_USAGE 0
69#define DFL_NS_CERT_TYPE 0
70
71/*
72 * global options
73 */
74struct options
75{
76 char *subject_key; /* filename of the subject key file */
77 char *issuer_key; /* filename of the issuer key file */
78 char *subject_pwd; /* password for the subject key file */
79 char *issuer_pwd; /* password for the issuer key file */
80 char *output_file; /* where to store the constructed key file */
81 char *subject_name; /* subject name for certificate */
82 char *issuer_name; /* issuer name for certificate */
83 char *not_before; /* validity period not before */
84 char *not_after; /* validity period not after */
85 char *serial; /* serial number string */
Paul Bakker15162a02013-09-06 19:27:21 +020086 int is_ca; /* is a CA certificate */
87 int max_pathlen; /* maximum CA path length */
Paul Bakker9397dcb2013-09-06 09:55:26 +020088 unsigned char key_usage; /* key usage flags */
89 unsigned char ns_cert_type; /* NS cert type */
90} opt;
91
92int write_certificate( x509write_cert *crt, char *output_file )
93{
94 int ret;
95 FILE *f;
96 unsigned char output_buf[4096];
97 size_t len = 0;
98
99 memset( output_buf, 0, 4096 );
100 if( ( ret = x509write_crt_pem( crt, output_buf, 4096 ) ) < 0 )
101 return( ret );
102
103 len = strlen( (char *) output_buf );
104
105 if( ( f = fopen( output_file, "w" ) ) == NULL )
106 return( -1 );
107
108 if( fwrite( output_buf, 1, len, f ) != len )
109 return( -1 );
110
111 fclose(f);
112
113 return( 0 );
114}
115
116#define USAGE \
117 "\n usage: cert_write param=<>...\n" \
118 "\n acceptable parameters:\n" \
119 " subject_key=%%s default: subject.key\n" \
120 " subject_pwd=%%s default: (empty)\n" \
121 " issuer_key=%%s default: ca.key\n" \
122 " issuer_pwd=%%s default: (empty)\n" \
123 " output_file=%%s default: cert.crt\n" \
124 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
125 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
126 " serial=%%s default: 1\n" \
127 " not_before=%%s default: 20010101000000\n"\
128 " not_after=%%s default: 20301231235959\n"\
Paul Bakker15162a02013-09-06 19:27:21 +0200129 " is_ca=%%d default: 0 (disabled)\n" \
130 " max_pathlen=%%d default: -1 (none)\n" \
Paul Bakker9397dcb2013-09-06 09:55:26 +0200131 " key_usage=%%s default: (empty)\n" \
132 " Comma-separated-list of values:\n" \
133 " digital_signature\n" \
134 " non_repudiation\n" \
135 " key_encipherment\n" \
136 " data_encipherment\n" \
137 " key_agreement\n" \
138 " key_certificate_sign\n" \
139 " crl_sign\n" \
140 " ns_cert_type=%%s default: (empty)\n" \
141 " Comma-separated-list of values:\n" \
142 " ssl_client\n" \
143 " ssl_server\n" \
144 " email\n" \
145 " object_signing\n" \
146 " ssl_ca\n" \
147 " email_ca\n" \
148 " object_signing_ca\n" \
149 "\n"
150
151int main( int argc, char *argv[] )
152{
153 int ret = 0;
154 rsa_context issuer_rsa, subject_rsa;
155 char buf[1024];
156 int i, j, n;
157 char *p, *q, *r;
158 x509write_cert crt;
159 mpi serial;
160
161 /*
162 * Set to sane values
163 */
164 x509write_crt_init( &crt );
165 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
166 rsa_init( &issuer_rsa, RSA_PKCS_V15, 0 );
167 rsa_init( &subject_rsa, RSA_PKCS_V15, 0 );
168 mpi_init( &serial );
169 memset( buf, 0, 1024 );
170
171 if( argc == 0 )
172 {
173 usage:
174 printf( USAGE );
175 ret = 1;
176 goto exit;
177 }
178
179 opt.subject_key = DFL_SUBJECT_KEY;
180 opt.issuer_key = DFL_ISSUER_KEY;
181 opt.subject_pwd = DFL_SUBJECT_PWD;
182 opt.issuer_pwd = DFL_ISSUER_PWD;
183 opt.output_file = DFL_OUTPUT_FILENAME;
184 opt.subject_name = DFL_SUBJECT_NAME;
185 opt.issuer_name = DFL_ISSUER_NAME;
186 opt.not_before = DFL_NOT_BEFORE;
187 opt.not_after = DFL_NOT_AFTER;
188 opt.serial = DFL_SERIAL;
Paul Bakker15162a02013-09-06 19:27:21 +0200189 opt.is_ca = DFL_IS_CA;
190 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191 opt.key_usage = DFL_KEY_USAGE;
192 opt.ns_cert_type = DFL_NS_CERT_TYPE;
193
194 for( i = 1; i < argc; i++ )
195 {
196
197 p = argv[i];
198 if( ( q = strchr( p, '=' ) ) == NULL )
199 goto usage;
200 *q++ = '\0';
201
202 n = strlen( p );
203 for( j = 0; j < n; j++ )
204 {
205 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
206 argv[i][j] |= 0x20;
207 }
208
209 if( strcmp( p, "subject_key" ) == 0 )
210 opt.subject_key = q;
211 else if( strcmp( p, "issuer_key" ) == 0 )
212 opt.issuer_key = q;
213 else if( strcmp( p, "subject_pwd" ) == 0 )
214 opt.subject_pwd = q;
215 else if( strcmp( p, "issuer_pwd" ) == 0 )
216 opt.issuer_pwd = q;
217 else if( strcmp( p, "output_file" ) == 0 )
218 opt.output_file = q;
219 else if( strcmp( p, "subject_name" ) == 0 )
220 {
221 opt.subject_name = q;
222 }
223 else if( strcmp( p, "issuer_name" ) == 0 )
224 {
225 opt.issuer_name = q;
226 }
227 else if( strcmp( p, "not_before" ) == 0 )
228 {
229 opt.not_before = q;
230 }
231 else if( strcmp( p, "not_after" ) == 0 )
232 {
233 opt.not_after = q;
234 }
235 else if( strcmp( p, "serial" ) == 0 )
236 {
237 opt.serial = q;
238 }
Paul Bakker15162a02013-09-06 19:27:21 +0200239 else if( strcmp( p, "is_ca" ) == 0 )
240 {
241 opt.is_ca = atoi( q );
242 if( opt.is_ca < 0 || opt.is_ca > 1 )
243 goto usage;
244 }
245 else if( strcmp( p, "max_pathlen" ) == 0 )
246 {
247 opt.max_pathlen = atoi( q );
248 if( opt.max_pathlen < -1 || opt.max_pathlen > 127 )
249 goto usage;
250 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200251 else if( strcmp( p, "key_usage" ) == 0 )
252 {
253 while( q != NULL )
254 {
255 if( ( r = strchr( q, ',' ) ) != NULL )
256 *r++ = '\0';
257
258 if( strcmp( q, "digital_signature" ) == 0 )
259 opt.key_usage |= KU_DIGITAL_SIGNATURE;
260 else if( strcmp( q, "non_repudiation" ) == 0 )
261 opt.key_usage |= KU_NON_REPUDIATION;
262 else if( strcmp( q, "key_encipherment" ) == 0 )
263 opt.key_usage |= KU_KEY_ENCIPHERMENT;
264 else if( strcmp( q, "data_encipherment" ) == 0 )
265 opt.key_usage |= KU_DATA_ENCIPHERMENT;
266 else if( strcmp( q, "key_agreement" ) == 0 )
267 opt.key_usage |= KU_KEY_AGREEMENT;
268 else if( strcmp( q, "key_cert_sign" ) == 0 )
269 opt.key_usage |= KU_KEY_CERT_SIGN;
270 else if( strcmp( q, "crl_sign" ) == 0 )
271 opt.key_usage |= KU_CRL_SIGN;
272 else
273 goto usage;
274
275 q = r;
276 }
277 }
278 else if( strcmp( p, "ns_cert_type" ) == 0 )
279 {
280 while( q != NULL )
281 {
282 if( ( r = strchr( q, ',' ) ) != NULL )
283 *r++ = '\0';
284
285 if( strcmp( q, "ssl_client" ) == 0 )
286 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
287 else if( strcmp( q, "ssl_server" ) == 0 )
288 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
289 else if( strcmp( q, "email" ) == 0 )
290 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
291 else if( strcmp( q, "object_signing" ) == 0 )
292 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
293 else if( strcmp( q, "ssl_ca" ) == 0 )
294 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
295 else if( strcmp( q, "email_ca" ) == 0 )
296 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
297 else if( strcmp( q, "object_signing_ca" ) == 0 )
298 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
299 else
300 goto usage;
301
302 q = r;
303 }
304 }
305 else
306 goto usage;
307 }
308
309 // Parse serial to MPI
310 //
311 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
312 {
313#ifdef POLARSSL_ERROR_C
314 error_strerror( ret, buf, 1024 );
315#endif
316 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
317 goto exit;
318 }
319
Paul Bakker9397dcb2013-09-06 09:55:26 +0200320 /*
321 * 1.0. Check the names for validity
322 */
323 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
324 {
325#ifdef POLARSSL_ERROR_C
326 error_strerror( ret, buf, 1024 );
327#endif
328 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
329 goto exit;
330 }
331
332 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
333 {
334#ifdef POLARSSL_ERROR_C
335 error_strerror( ret, buf, 1024 );
336#endif
337 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
338 goto exit;
339 }
340
341 /*
342 * 1.1. Load the keys
343 */
344 printf( "\n . Loading the subject key ..." );
345 fflush( stdout );
346
347 ret = x509parse_keyfile_rsa( &subject_rsa, opt.subject_key, opt.subject_pwd );
348
349 if( ret != 0 )
350 {
351#ifdef POLARSSL_ERROR_C
352 error_strerror( ret, buf, 1024 );
353#endif
354 printf( " failed\n ! x509parse_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
355 goto exit;
356 }
357
358 x509write_crt_set_subject_key( &crt, &subject_rsa );
359
360 printf( " ok\n" );
361
362 printf( " . Loading the issuer key ..." );
363 fflush( stdout );
364
365 ret = x509parse_keyfile_rsa( &issuer_rsa, opt.issuer_key, opt.issuer_pwd );
366
367 if( ret != 0 )
368 {
369#ifdef POLARSSL_ERROR_C
370 error_strerror( ret, buf, 1024 );
371#endif
372 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
373 goto exit;
374 }
375
376 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
377
378 printf( " ok\n" );
379
380 printf( " . Setting certificate values ..." );
381 fflush( stdout );
382
383 ret = x509write_crt_set_serial( &crt, &serial );
384 if( ret != 0 )
385 {
386#ifdef POLARSSL_ERROR_C
387 error_strerror( ret, buf, 1024 );
388#endif
389 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
390 goto exit;
391 }
392
393 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
394 if( ret != 0 )
395 {
396#ifdef POLARSSL_ERROR_C
397 error_strerror( ret, buf, 1024 );
398#endif
399 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
400 goto exit;
401 }
402
403 printf( " ok\n" );
404
Paul Bakker15162a02013-09-06 19:27:21 +0200405 printf( " . Adding the Basic Constraints extension ..." );
406 fflush( stdout );
407
408 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
409 opt.max_pathlen );
410 if( ret != 0 )
411 {
412#ifdef POLARSSL_ERROR_C
413 error_strerror( ret, buf, 1024 );
414#endif
415 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
416 goto exit;
417 }
418
419 printf( " ok\n" );
420
421 printf( " . Adding the Subject Key Identifier ..." );
422 fflush( stdout );
423
424 ret = x509write_crt_set_subject_key_identifier( &crt );
425 if( ret != 0 )
426 {
427#ifdef POLARSSL_ERROR_C
428 error_strerror( ret, buf, 1024 );
429#endif
430 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
431 goto exit;
432 }
433
434 printf( " ok\n" );
435
436 printf( " . Adding the Authority Key Identifier ..." );
437 fflush( stdout );
438
439 ret = x509write_crt_set_authority_key_identifier( &crt );
440 if( ret != 0 )
441 {
442#ifdef POLARSSL_ERROR_C
443 error_strerror( ret, buf, 1024 );
444#endif
445 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
446 goto exit;
447 }
448
449 printf( " ok\n" );
450
Paul Bakker52be08c2013-09-09 12:37:54 +0200451 if( opt.key_usage )
452 {
453 printf( " . Adding the Key Usage extension ..." );
454 fflush( stdout );
455
456 ret = x509write_crt_set_key_usage( &crt, opt.key_usage );
457 if( ret != 0 )
458 {
459#ifdef POLARSSL_ERROR_C
460 error_strerror( ret, buf, 1024 );
461#endif
462 printf( " failed\n ! x509write_crt_set_key_usage returned -0x%02x - %s\n\n", -ret, buf );
463 goto exit;
464 }
465
466 printf( " ok\n" );
467 }
468
469 if( opt.ns_cert_type )
470 {
471 printf( " . Adding the NS Cert Type extension ..." );
472 fflush( stdout );
473
474 ret = x509write_crt_set_ns_cert_type( &crt, opt.ns_cert_type );
475 if( ret != 0 )
476 {
477#ifdef POLARSSL_ERROR_C
478 error_strerror( ret, buf, 1024 );
479#endif
480 printf( " failed\n ! x509write_crt_set_ns_cert_type returned -0x%02x - %s\n\n", -ret, buf );
481 goto exit;
482 }
483
484 printf( " ok\n" );
485 }
486
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487 /*
488 * 1.2. Writing the request
489 */
490 printf( " . Writing the certificate..." );
491 fflush( stdout );
492
493 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
494 {
495#ifdef POLARSSL_ERROR_C
496 error_strerror( ret, buf, 1024 );
497#endif
498 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
499 goto exit;
500 }
501
502 printf( " ok\n" );
503
504exit:
505 x509write_crt_free( &crt );
506 rsa_free( &subject_rsa );
507 rsa_free( &issuer_rsa );
508 mpi_free( &serial );
509
510#if defined(_WIN32)
511 printf( " + Press Enter to exit this program.\n" );
512 fflush( stdout ); getchar();
513#endif
514
515 return( ret );
516}
517#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
518 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */