blob: 59c49b2fe3c86ceca60a8479e9fd8f2bb2f9437f [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"
66#define DFL_KEY_USAGE 0
67#define DFL_NS_CERT_TYPE 0
68
69/*
70 * global options
71 */
72struct options
73{
74 char *subject_key; /* filename of the subject key file */
75 char *issuer_key; /* filename of the issuer key file */
76 char *subject_pwd; /* password for the subject key file */
77 char *issuer_pwd; /* password for the issuer key file */
78 char *output_file; /* where to store the constructed key file */
79 char *subject_name; /* subject name for certificate */
80 char *issuer_name; /* issuer name for certificate */
81 char *not_before; /* validity period not before */
82 char *not_after; /* validity period not after */
83 char *serial; /* serial number string */
84 unsigned char key_usage; /* key usage flags */
85 unsigned char ns_cert_type; /* NS cert type */
86} opt;
87
88int write_certificate( x509write_cert *crt, char *output_file )
89{
90 int ret;
91 FILE *f;
92 unsigned char output_buf[4096];
93 size_t len = 0;
94
95 memset( output_buf, 0, 4096 );
96 if( ( ret = x509write_crt_pem( crt, output_buf, 4096 ) ) < 0 )
97 return( ret );
98
99 len = strlen( (char *) output_buf );
100
101 if( ( f = fopen( output_file, "w" ) ) == NULL )
102 return( -1 );
103
104 if( fwrite( output_buf, 1, len, f ) != len )
105 return( -1 );
106
107 fclose(f);
108
109 return( 0 );
110}
111
112#define USAGE \
113 "\n usage: cert_write param=<>...\n" \
114 "\n acceptable parameters:\n" \
115 " subject_key=%%s default: subject.key\n" \
116 " subject_pwd=%%s default: (empty)\n" \
117 " issuer_key=%%s default: ca.key\n" \
118 " issuer_pwd=%%s default: (empty)\n" \
119 " output_file=%%s default: cert.crt\n" \
120 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
121 " issuer_name=%%s default: CN=CA,O=PolarSSL,C=NL\n" \
122 " serial=%%s default: 1\n" \
123 " not_before=%%s default: 20010101000000\n"\
124 " not_after=%%s default: 20301231235959\n"\
125 " key_usage=%%s default: (empty)\n" \
126 " Comma-separated-list of values:\n" \
127 " digital_signature\n" \
128 " non_repudiation\n" \
129 " key_encipherment\n" \
130 " data_encipherment\n" \
131 " key_agreement\n" \
132 " key_certificate_sign\n" \
133 " crl_sign\n" \
134 " ns_cert_type=%%s default: (empty)\n" \
135 " Comma-separated-list of values:\n" \
136 " ssl_client\n" \
137 " ssl_server\n" \
138 " email\n" \
139 " object_signing\n" \
140 " ssl_ca\n" \
141 " email_ca\n" \
142 " object_signing_ca\n" \
143 "\n"
144
145int main( int argc, char *argv[] )
146{
147 int ret = 0;
148 rsa_context issuer_rsa, subject_rsa;
149 char buf[1024];
150 int i, j, n;
151 char *p, *q, *r;
152 x509write_cert crt;
153 mpi serial;
154
155 /*
156 * Set to sane values
157 */
158 x509write_crt_init( &crt );
159 x509write_crt_set_md_alg( &crt, POLARSSL_MD_SHA1 );
160 rsa_init( &issuer_rsa, RSA_PKCS_V15, 0 );
161 rsa_init( &subject_rsa, RSA_PKCS_V15, 0 );
162 mpi_init( &serial );
163 memset( buf, 0, 1024 );
164
165 if( argc == 0 )
166 {
167 usage:
168 printf( USAGE );
169 ret = 1;
170 goto exit;
171 }
172
173 opt.subject_key = DFL_SUBJECT_KEY;
174 opt.issuer_key = DFL_ISSUER_KEY;
175 opt.subject_pwd = DFL_SUBJECT_PWD;
176 opt.issuer_pwd = DFL_ISSUER_PWD;
177 opt.output_file = DFL_OUTPUT_FILENAME;
178 opt.subject_name = DFL_SUBJECT_NAME;
179 opt.issuer_name = DFL_ISSUER_NAME;
180 opt.not_before = DFL_NOT_BEFORE;
181 opt.not_after = DFL_NOT_AFTER;
182 opt.serial = DFL_SERIAL;
183 opt.key_usage = DFL_KEY_USAGE;
184 opt.ns_cert_type = DFL_NS_CERT_TYPE;
185
186 for( i = 1; i < argc; i++ )
187 {
188
189 p = argv[i];
190 if( ( q = strchr( p, '=' ) ) == NULL )
191 goto usage;
192 *q++ = '\0';
193
194 n = strlen( p );
195 for( j = 0; j < n; j++ )
196 {
197 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
198 argv[i][j] |= 0x20;
199 }
200
201 if( strcmp( p, "subject_key" ) == 0 )
202 opt.subject_key = q;
203 else if( strcmp( p, "issuer_key" ) == 0 )
204 opt.issuer_key = q;
205 else if( strcmp( p, "subject_pwd" ) == 0 )
206 opt.subject_pwd = q;
207 else if( strcmp( p, "issuer_pwd" ) == 0 )
208 opt.issuer_pwd = q;
209 else if( strcmp( p, "output_file" ) == 0 )
210 opt.output_file = q;
211 else if( strcmp( p, "subject_name" ) == 0 )
212 {
213 opt.subject_name = q;
214 }
215 else if( strcmp( p, "issuer_name" ) == 0 )
216 {
217 opt.issuer_name = q;
218 }
219 else if( strcmp( p, "not_before" ) == 0 )
220 {
221 opt.not_before = q;
222 }
223 else if( strcmp( p, "not_after" ) == 0 )
224 {
225 opt.not_after = q;
226 }
227 else if( strcmp( p, "serial" ) == 0 )
228 {
229 opt.serial = q;
230 }
231 else if( strcmp( p, "key_usage" ) == 0 )
232 {
233 while( q != NULL )
234 {
235 if( ( r = strchr( q, ',' ) ) != NULL )
236 *r++ = '\0';
237
238 if( strcmp( q, "digital_signature" ) == 0 )
239 opt.key_usage |= KU_DIGITAL_SIGNATURE;
240 else if( strcmp( q, "non_repudiation" ) == 0 )
241 opt.key_usage |= KU_NON_REPUDIATION;
242 else if( strcmp( q, "key_encipherment" ) == 0 )
243 opt.key_usage |= KU_KEY_ENCIPHERMENT;
244 else if( strcmp( q, "data_encipherment" ) == 0 )
245 opt.key_usage |= KU_DATA_ENCIPHERMENT;
246 else if( strcmp( q, "key_agreement" ) == 0 )
247 opt.key_usage |= KU_KEY_AGREEMENT;
248 else if( strcmp( q, "key_cert_sign" ) == 0 )
249 opt.key_usage |= KU_KEY_CERT_SIGN;
250 else if( strcmp( q, "crl_sign" ) == 0 )
251 opt.key_usage |= KU_CRL_SIGN;
252 else
253 goto usage;
254
255 q = r;
256 }
257 }
258 else if( strcmp( p, "ns_cert_type" ) == 0 )
259 {
260 while( q != NULL )
261 {
262 if( ( r = strchr( q, ',' ) ) != NULL )
263 *r++ = '\0';
264
265 if( strcmp( q, "ssl_client" ) == 0 )
266 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
267 else if( strcmp( q, "ssl_server" ) == 0 )
268 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
269 else if( strcmp( q, "email" ) == 0 )
270 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
271 else if( strcmp( q, "object_signing" ) == 0 )
272 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
273 else if( strcmp( q, "ssl_ca" ) == 0 )
274 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
275 else if( strcmp( q, "email_ca" ) == 0 )
276 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
277 else if( strcmp( q, "object_signing_ca" ) == 0 )
278 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
279 else
280 goto usage;
281
282 q = r;
283 }
284 }
285 else
286 goto usage;
287 }
288
289 // Parse serial to MPI
290 //
291 if( ( ret = mpi_read_string( &serial, 10, opt.serial ) ) != 0 )
292 {
293#ifdef POLARSSL_ERROR_C
294 error_strerror( ret, buf, 1024 );
295#endif
296 printf( " failed\n ! mpi_read_string returned -0x%02x - %s\n\n", -ret, buf );
297 goto exit;
298 }
299
300/*
301 if( opt.key_usage )
302 x509write_csr_set_key_usage( &req, opt.key_usage );
303
304 if( opt.ns_cert_type )
305 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
306*/
307 /*
308 * 1.0. Check the names for validity
309 */
310 if( ( ret = x509write_crt_set_subject_name( &crt, opt.subject_name ) ) != 0 )
311 {
312#ifdef POLARSSL_ERROR_C
313 error_strerror( ret, buf, 1024 );
314#endif
315 printf( " failed\n ! x509write_crt_set_subject_name returned -0x%02x - %s\n\n", -ret, buf );
316 goto exit;
317 }
318
319 if( ( ret = x509write_crt_set_issuer_name( &crt, opt.issuer_name ) ) != 0 )
320 {
321#ifdef POLARSSL_ERROR_C
322 error_strerror( ret, buf, 1024 );
323#endif
324 printf( " failed\n ! x509write_crt_set_issuer_name returned -0x%02x - %s\n\n", -ret, buf );
325 goto exit;
326 }
327
328 /*
329 * 1.1. Load the keys
330 */
331 printf( "\n . Loading the subject key ..." );
332 fflush( stdout );
333
334 ret = x509parse_keyfile_rsa( &subject_rsa, opt.subject_key, opt.subject_pwd );
335
336 if( ret != 0 )
337 {
338#ifdef POLARSSL_ERROR_C
339 error_strerror( ret, buf, 1024 );
340#endif
341 printf( " failed\n ! x509parse_keyfile_rsa returned -0x%02x - %s\n\n", -ret, buf );
342 goto exit;
343 }
344
345 x509write_crt_set_subject_key( &crt, &subject_rsa );
346
347 printf( " ok\n" );
348
349 printf( " . Loading the issuer key ..." );
350 fflush( stdout );
351
352 ret = x509parse_keyfile_rsa( &issuer_rsa, opt.issuer_key, opt.issuer_pwd );
353
354 if( ret != 0 )
355 {
356#ifdef POLARSSL_ERROR_C
357 error_strerror( ret, buf, 1024 );
358#endif
359 printf( " failed\n ! x509parse_keyfile_rsa returned -x%02x - %s\n\n", -ret, buf );
360 goto exit;
361 }
362
363 x509write_crt_set_issuer_key( &crt, &issuer_rsa );
364
365 printf( " ok\n" );
366
367 printf( " . Setting certificate values ..." );
368 fflush( stdout );
369
370 ret = x509write_crt_set_serial( &crt, &serial );
371 if( ret != 0 )
372 {
373#ifdef POLARSSL_ERROR_C
374 error_strerror( ret, buf, 1024 );
375#endif
376 printf( " failed\n ! x509write_crt_set_serial returned -0x%02x - %s\n\n", -ret, buf );
377 goto exit;
378 }
379
380 ret = x509write_crt_set_validity( &crt, opt.not_before, opt.not_after );
381 if( ret != 0 )
382 {
383#ifdef POLARSSL_ERROR_C
384 error_strerror( ret, buf, 1024 );
385#endif
386 printf( " failed\n ! x509write_crt_set_validity returned -0x%02x - %s\n\n", -ret, buf );
387 goto exit;
388 }
389
390 printf( " ok\n" );
391
392 /*
393 * 1.2. Writing the request
394 */
395 printf( " . Writing the certificate..." );
396 fflush( stdout );
397
398 if( ( ret = write_certificate( &crt, opt.output_file ) ) != 0 )
399 {
400#ifdef POLARSSL_ERROR_C
401 error_strerror( ret, buf, 1024 );
402#endif
403 printf( " failed\n ! write_certifcate -0x%02x - %s\n\n", -ret, buf );
404 goto exit;
405 }
406
407 printf( " ok\n" );
408
409exit:
410 x509write_crt_free( &crt );
411 rsa_free( &subject_rsa );
412 rsa_free( &issuer_rsa );
413 mpi_free( &serial );
414
415#if defined(_WIN32)
416 printf( " + Press Enter to exit this program.\n" );
417 fflush( stdout ); getchar();
418#endif
419
420 return( ret );
421}
422#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
423 POLARSSet_serial_X509_WRITE_C && POLARSSL_FS_IO */