blob: 7a811d49f527322fd636adcd66d628876c2bc048 [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
320/*
321 if( opt.key_usage )
322 x509write_csr_set_key_usage( &req, opt.key_usage );
323
324 if( opt.ns_cert_type )
325 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
326*/
327 /*
328 * 1.0. Check the names for validity
329 */
330 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
331 {
332#ifdef POLARSSL_ERROR_C
333 error_strerror( ret, buf, 1024 );
334#endif
335 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
336 goto exit;
337 }
338
339 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
340 {
341#ifdef POLARSSL_ERROR_C
342 error_strerror( ret, buf, 1024 );
343#endif
344 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
345 goto exit;
346 }
347
348 /*
349 * 1.1. Load the keys
350 */
351 printf( "\n . Loading the subject key ..." );
352 fflush( stdout );
353
354 ret = x509parse_keyfile_rsa( &subject_rsa, opt.subject_key, opt.subject_pwd );
355
356 if( ret != 0 )
357 {
358#ifdef POLARSSL_ERROR_C
359 error_strerror( ret, buf, 1024 );
360#endif
361 printf( " failed\n ! x509parse_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
362 goto exit;
363 }
364
365 x509write_crt_set_subject_key( &crt, &subject_rsa );
366
367 printf( " ok\n" );
368
369 printf( " . Loading the issuer key ..." );
370 fflush( stdout );
371
372 ret = x509parse_keyfile_rsa( &issuer_rsa, opt.issuer_key, opt.issuer_pwd );
373
374 if( ret != 0 )
375 {
376#ifdef POLARSSL_ERROR_C
377 error_strerror( ret, buf, 1024 );
378#endif
379 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
380 goto exit;
381 }
382
383 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
384
385 printf( " ok\n" );
386
387 printf( " . Setting certificate values ..." );
388 fflush( stdout );
389
390 ret = x509write_crt_set_serial( &crt, &serial );
391 if( ret != 0 )
392 {
393#ifdef POLARSSL_ERROR_C
394 error_strerror( ret, buf, 1024 );
395#endif
396 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
397 goto exit;
398 }
399
400 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
401 if( ret != 0 )
402 {
403#ifdef POLARSSL_ERROR_C
404 error_strerror( ret, buf, 1024 );
405#endif
406 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
407 goto exit;
408 }
409
410 printf( " ok\n" );
411
Paul Bakker15162a02013-09-06 19:27:21 +0200412 printf( " . Adding the Basic Constraints extension ..." );
413 fflush( stdout );
414
415 ret = x509write_crt_set_basic_constraints( &crt, opt.is_ca,
416 opt.max_pathlen );
417 if( ret != 0 )
418 {
419#ifdef POLARSSL_ERROR_C
420 error_strerror( ret, buf, 1024 );
421#endif
422 printf( " failed\n ! x509write_crt_set_basic_contraints returned -0x%02x - %s\n\n", -ret, buf );
423 goto exit;
424 }
425
426 printf( " ok\n" );
427
428 printf( " . Adding the Subject Key Identifier ..." );
429 fflush( stdout );
430
431 ret = x509write_crt_set_subject_key_identifier( &crt );
432 if( ret != 0 )
433 {
434#ifdef POLARSSL_ERROR_C
435 error_strerror( ret, buf, 1024 );
436#endif
437 printf( " failed\n ! x509write_crt_set_subject_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
438 goto exit;
439 }
440
441 printf( " ok\n" );
442
443 printf( " . Adding the Authority Key Identifier ..." );
444 fflush( stdout );
445
446 ret = x509write_crt_set_authority_key_identifier( &crt );
447 if( ret != 0 )
448 {
449#ifdef POLARSSL_ERROR_C
450 error_strerror( ret, buf, 1024 );
451#endif
452 printf( " failed\n ! x509write_crt_set_authority_key_identifier returned -0x%02x - %s\n\n", -ret, buf );
453 goto exit;
454 }
455
456 printf( " ok\n" );
457
Paul Bakker9397dcb2013-09-06 09:55:26 +0200458 /*
459 * 1.2. Writing the request
460 */
461 printf( " . Writing the certificate..." );
462 fflush( stdout );
463
464 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
465 {
466#ifdef POLARSSL_ERROR_C
467 error_strerror( ret, buf, 1024 );
468#endif
469 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
470 goto exit;
471 }
472
473 printf( " ok\n" );
474
475exit:
476 x509write_crt_free( &crt );
477 rsa_free( &subject_rsa );
478 rsa_free( &issuer_rsa );
479 mpi_free( &serial );
480
481#if defined(_WIN32)
482 printf( " + Press Enter to exit this program.\n" );
483 fflush( stdout ); getchar();
484#endif
485
486 return( ret );
487}
488#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
489 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */