blob: b98f23368b8523e10230d2ce7befca033bf68025 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Certificate request generation
3 *
4 * Copyright (C) 2006-2011, 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
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036#include "polarssl/x509write.h"
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020037#include "polarssl/error.h"
38#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020041#if !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) || \
42 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020043 !defined(POLARSSL_ERROR_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020044int main( int argc, char *argv[] )
45{
46 ((void) argc);
47 ((void) argv);
48
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020049 printf( "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO and/or "
50 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
51 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020052 return( 0 );
53}
54#else
55
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056#define DFL_FILENAME "keyfile.key"
57#define DFL_DEBUG_LEVEL 0
58#define DFL_OUTPUT_FILENAME "cert.req"
59#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020060#define DFL_KEY_USAGE 0
61#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
63/*
64 * global options
65 */
66struct options
67{
68 char *filename; /* filename of the key file */
69 int debug_level; /* level of debugging */
70 char *output_file; /* where to store the constructed key file */
71 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020072 unsigned char key_usage; /* key usage flags */
73 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074} opt;
75
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020076int write_certificate_request( x509write_csr *req, char *output_file,
77 int (*f_rng)(void *, unsigned char *, size_t),
78 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079{
Paul Bakker135f1e92013-08-26 16:54:13 +020080 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081 FILE *f;
82 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020083 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084
Paul Bakker135f1e92013-08-26 16:54:13 +020085 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020086 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020087 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker135f1e92013-08-26 16:54:13 +020089 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090
Paul Bakker8eabfc12013-08-25 10:18:25 +020091 if( ( f = fopen( output_file, "w" ) ) == NULL )
92 return( -1 );
93
Paul Bakker135f1e92013-08-26 16:54:13 +020094 if( fwrite( output_buf, 1, len, f ) != len )
95 return( -1 );
96
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020098
99 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100}
101
102#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200103 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104 "\n acceptable parameters:\n" \
105 " filename=%%s default: keyfile.key\n" \
106 " debug_level=%%d default: 0 (disabled)\n" \
107 " output_file=%%s default: cert.req\n" \
108 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200109 " key_usage=%%s default: (empty)\n" \
110 " Comma-separated-list of values:\n" \
111 " digital_signature\n" \
112 " non_repudiation\n" \
113 " key_encipherment\n" \
114 " data_encipherment\n" \
115 " key_agreement\n" \
116 " key_certificate_sign\n" \
117 " crl_sign\n" \
118 " ns_cert_type=%%s default: (empty)\n" \
119 " Comma-separated-list of values:\n" \
120 " ssl_client\n" \
121 " ssl_server\n" \
122 " email\n" \
123 " object_signing\n" \
124 " ssl_ca\n" \
125 " email_ca\n" \
126 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127 "\n"
128
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129int main( int argc, char *argv[] )
130{
131 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200132 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133 char buf[1024];
134 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200135 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200136 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200137 entropy_context entropy;
138 ctr_drbg_context ctr_drbg;
139 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
141 /*
142 * Set to sane values
143 */
Paul Bakker82e29452013-08-25 11:01:31 +0200144 x509write_csr_init( &req );
145 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200146 pk_init( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147 memset( buf, 0, 1024 );
148
149 if( argc == 0 )
150 {
151 usage:
152 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200153 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154 goto exit;
155 }
156
157 opt.filename = DFL_FILENAME;
158 opt.debug_level = DFL_DEBUG_LEVEL;
159 opt.output_file = DFL_OUTPUT_FILENAME;
160 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200161 opt.key_usage = DFL_KEY_USAGE;
162 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163
164 for( i = 1; i < argc; i++ )
165 {
166
167 p = argv[i];
168 if( ( q = strchr( p, '=' ) ) == NULL )
169 goto usage;
170 *q++ = '\0';
171
172 n = strlen( p );
173 for( j = 0; j < n; j++ )
174 {
175 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
176 argv[i][j] |= 0x20;
177 }
178
179 if( strcmp( p, "filename" ) == 0 )
180 opt.filename = q;
181 else if( strcmp( p, "output_file" ) == 0 )
182 opt.output_file = q;
183 else if( strcmp( p, "debug_level" ) == 0 )
184 {
185 opt.debug_level = atoi( q );
186 if( opt.debug_level < 0 || opt.debug_level > 65535 )
187 goto usage;
188 }
189 else if( strcmp( p, "subject_name" ) == 0 )
190 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191 opt.subject_name = q;
192 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200193 else if( strcmp( p, "key_usage" ) == 0 )
194 {
195 while( q != NULL )
196 {
197 if( ( r = strchr( q, ',' ) ) != NULL )
198 *r++ = '\0';
199
200 if( strcmp( q, "digital_signature" ) == 0 )
201 opt.key_usage |= KU_DIGITAL_SIGNATURE;
202 else if( strcmp( q, "non_repudiation" ) == 0 )
203 opt.key_usage |= KU_NON_REPUDIATION;
204 else if( strcmp( q, "key_encipherment" ) == 0 )
205 opt.key_usage |= KU_KEY_ENCIPHERMENT;
206 else if( strcmp( q, "data_encipherment" ) == 0 )
207 opt.key_usage |= KU_DATA_ENCIPHERMENT;
208 else if( strcmp( q, "key_agreement" ) == 0 )
209 opt.key_usage |= KU_KEY_AGREEMENT;
210 else if( strcmp( q, "key_cert_sign" ) == 0 )
211 opt.key_usage |= KU_KEY_CERT_SIGN;
212 else if( strcmp( q, "crl_sign" ) == 0 )
213 opt.key_usage |= KU_CRL_SIGN;
214 else
215 goto usage;
216
217 q = r;
218 }
219 }
220 else if( strcmp( p, "ns_cert_type" ) == 0 )
221 {
222 while( q != NULL )
223 {
224 if( ( r = strchr( q, ',' ) ) != NULL )
225 *r++ = '\0';
226
227 if( strcmp( q, "ssl_client" ) == 0 )
228 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
229 else if( strcmp( q, "ssl_server" ) == 0 )
230 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
231 else if( strcmp( q, "email" ) == 0 )
232 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
233 else if( strcmp( q, "object_signing" ) == 0 )
234 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
235 else if( strcmp( q, "ssl_ca" ) == 0 )
236 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
237 else if( strcmp( q, "email_ca" ) == 0 )
238 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
239 else if( strcmp( q, "object_signing_ca" ) == 0 )
240 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
241 else
242 goto usage;
243
244 q = r;
245 }
246 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 else
248 goto usage;
249 }
250
Paul Bakker57be6e22013-08-26 14:13:14 +0200251 if( opt.key_usage )
252 x509write_csr_set_key_usage( &req, opt.key_usage );
253
254 if( opt.ns_cert_type )
255 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
256
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200258 * 0. Seed the PRNG
259 */
260 printf( " . Seeding the random number generator..." );
261 fflush( stdout );
262
263 entropy_init( &entropy );
264 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
265 (const unsigned char *) pers,
266 strlen( pers ) ) ) != 0 )
267 {
268 error_strerror( ret, buf, 1024 );
269 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
270 goto exit;
271 }
272
273 printf( " ok\n" );
274
275 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276 * 1.0. Check the subject name for validity
277 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200278 printf( " . Checking subjet name..." );
279 fflush( stdout );
280
Paul Bakker82e29452013-08-25 11:01:31 +0200281 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200283 error_strerror( ret, buf, 1024 );
Paul Bakker82e29452013-08-25 11:01:31 +0200284 printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200285 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286 }
287
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200288 printf( " ok\n" );
289
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000290 /*
291 * 1.1. Load the key
292 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200293 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294 fflush( stdout );
295
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200296 ret = x509parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297
298 if( ret != 0 )
299 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300 error_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200301 printf( " failed\n ! x509parse_keyfile returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200302 goto exit;
303 }
304
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200305 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200306
307 printf( " ok\n" );
308
309 /*
310 * 1.2. Writing the request
311 */
312 printf( " . Writing the certificate request ..." );
313 fflush( stdout );
314
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200315 if( ( ret = write_certificate_request( &req, opt.output_file,
316 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200317 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200318 error_strerror( ret, buf, 1024 );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200319 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000320 goto exit;
321 }
322
323 printf( " ok\n" );
324
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000325exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200326 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200327 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000328
329#if defined(_WIN32)
330 printf( " + Press Enter to exit this program.\n" );
331 fflush( stdout ); getchar();
332#endif
333
334 return( ret );
335}
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200336#endif /* POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
337 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
338 POLARSSL_ERROR_C */