blob: c738b4a9e5cdfd9a05a0210a00accf550ae7470a [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#include "polarssl/x509_csr.h"
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020033#include "polarssl/entropy.h"
34#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020035#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#if !defined(POLARSSL_X509_CSR_WRITE_C) || !defined(POLARSSL_FS_IO) || \
Paul Bakker36713e82013-09-17 13:25:29 +020038 !defined(POLARSSL_PK_PARSE_C) || \
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020039 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020040int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 printf( "POLARSSL_X509_CSR_WRITE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020046 "POLARSSL_PK_PARSE_C and/or "
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020047 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
48 "not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020049 return( 0 );
50}
51#else
52
Paul Bakkerbdb912d2012-02-13 23:11:30 +000053#define DFL_FILENAME "keyfile.key"
54#define DFL_DEBUG_LEVEL 0
55#define DFL_OUTPUT_FILENAME "cert.req"
56#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020057#define DFL_KEY_USAGE 0
58#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000059
60/*
61 * global options
62 */
63struct options
64{
Paul Bakker8fc30b12013-11-25 13:29:43 +010065 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066 int debug_level; /* level of debugging */
Paul Bakker8fc30b12013-11-25 13:29:43 +010067 const char *output_file; /* where to store the constructed key file */
68 const char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020069 unsigned char key_usage; /* key usage flags */
70 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000071} opt;
72
Paul Bakker8fc30b12013-11-25 13:29:43 +010073int write_certificate_request( x509write_csr *req, const char *output_file,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020074 int (*f_rng)(void *, unsigned char *, size_t),
75 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076{
Paul Bakker135f1e92013-08-26 16:54:13 +020077 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078 FILE *f;
79 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020080 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081
Paul Bakker135f1e92013-08-26 16:54:13 +020082 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020083 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020084 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085
Paul Bakker135f1e92013-08-26 16:54:13 +020086 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000087
Paul Bakker8eabfc12013-08-25 10:18:25 +020088 if( ( f = fopen( output_file, "w" ) ) == NULL )
89 return( -1 );
90
Paul Bakker135f1e92013-08-26 16:54:13 +020091 if( fwrite( output_buf, 1, len, f ) != len )
92 return( -1 );
93
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020095
96 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097}
98
99#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200100 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101 "\n acceptable parameters:\n" \
102 " filename=%%s default: keyfile.key\n" \
103 " debug_level=%%d default: 0 (disabled)\n" \
104 " output_file=%%s default: cert.req\n" \
105 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200106 " key_usage=%%s default: (empty)\n" \
107 " Comma-separated-list of values:\n" \
108 " digital_signature\n" \
109 " non_repudiation\n" \
110 " key_encipherment\n" \
111 " data_encipherment\n" \
112 " key_agreement\n" \
113 " key_certificate_sign\n" \
114 " crl_sign\n" \
115 " ns_cert_type=%%s default: (empty)\n" \
116 " Comma-separated-list of values:\n" \
117 " ssl_client\n" \
118 " ssl_server\n" \
119 " email\n" \
120 " object_signing\n" \
121 " ssl_ca\n" \
122 " email_ca\n" \
123 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124 "\n"
125
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126int main( int argc, char *argv[] )
127{
128 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200129 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100131 int i;
Paul Bakker57be6e22013-08-26 14:13:14 +0200132 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200133 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200134 entropy_context entropy;
135 ctr_drbg_context ctr_drbg;
136 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137
138 /*
139 * Set to sane values
140 */
Paul Bakker82e29452013-08-25 11:01:31 +0200141 x509write_csr_init( &req );
142 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200143 pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200144 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145
146 if( argc == 0 )
147 {
148 usage:
149 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200150 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 goto exit;
152 }
153
154 opt.filename = DFL_FILENAME;
155 opt.debug_level = DFL_DEBUG_LEVEL;
156 opt.output_file = DFL_OUTPUT_FILENAME;
157 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200158 opt.key_usage = DFL_KEY_USAGE;
159 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160
161 for( i = 1; i < argc; i++ )
162 {
163
164 p = argv[i];
165 if( ( q = strchr( p, '=' ) ) == NULL )
166 goto usage;
167 *q++ = '\0';
168
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169 if( strcmp( p, "filename" ) == 0 )
170 opt.filename = q;
171 else if( strcmp( p, "output_file" ) == 0 )
172 opt.output_file = q;
173 else if( strcmp( p, "debug_level" ) == 0 )
174 {
175 opt.debug_level = atoi( q );
176 if( opt.debug_level < 0 || opt.debug_level > 65535 )
177 goto usage;
178 }
179 else if( strcmp( p, "subject_name" ) == 0 )
180 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181 opt.subject_name = q;
182 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200183 else if( strcmp( p, "key_usage" ) == 0 )
184 {
185 while( q != NULL )
186 {
187 if( ( r = strchr( q, ',' ) ) != NULL )
188 *r++ = '\0';
189
190 if( strcmp( q, "digital_signature" ) == 0 )
191 opt.key_usage |= KU_DIGITAL_SIGNATURE;
192 else if( strcmp( q, "non_repudiation" ) == 0 )
193 opt.key_usage |= KU_NON_REPUDIATION;
194 else if( strcmp( q, "key_encipherment" ) == 0 )
195 opt.key_usage |= KU_KEY_ENCIPHERMENT;
196 else if( strcmp( q, "data_encipherment" ) == 0 )
197 opt.key_usage |= KU_DATA_ENCIPHERMENT;
198 else if( strcmp( q, "key_agreement" ) == 0 )
199 opt.key_usage |= KU_KEY_AGREEMENT;
200 else if( strcmp( q, "key_cert_sign" ) == 0 )
201 opt.key_usage |= KU_KEY_CERT_SIGN;
202 else if( strcmp( q, "crl_sign" ) == 0 )
203 opt.key_usage |= KU_CRL_SIGN;
204 else
205 goto usage;
206
207 q = r;
208 }
209 }
210 else if( strcmp( p, "ns_cert_type" ) == 0 )
211 {
212 while( q != NULL )
213 {
214 if( ( r = strchr( q, ',' ) ) != NULL )
215 *r++ = '\0';
216
217 if( strcmp( q, "ssl_client" ) == 0 )
218 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
219 else if( strcmp( q, "ssl_server" ) == 0 )
220 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
221 else if( strcmp( q, "email" ) == 0 )
222 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
223 else if( strcmp( q, "object_signing" ) == 0 )
224 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
225 else if( strcmp( q, "ssl_ca" ) == 0 )
226 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
227 else if( strcmp( q, "email_ca" ) == 0 )
228 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
229 else if( strcmp( q, "object_signing_ca" ) == 0 )
230 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
231 else
232 goto usage;
233
234 q = r;
235 }
236 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 else
238 goto usage;
239 }
240
Paul Bakker57be6e22013-08-26 14:13:14 +0200241 if( opt.key_usage )
242 x509write_csr_set_key_usage( &req, opt.key_usage );
243
244 if( opt.ns_cert_type )
245 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
246
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200248 * 0. Seed the PRNG
249 */
250 printf( " . Seeding the random number generator..." );
251 fflush( stdout );
252
253 entropy_init( &entropy );
254 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
255 (const unsigned char *) pers,
256 strlen( pers ) ) ) != 0 )
257 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200258 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200259 goto exit;
260 }
261
262 printf( " ok\n" );
263
264 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265 * 1.0. Check the subject name for validity
266 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200267 printf( " . Checking subjet name..." );
268 fflush( stdout );
269
Paul Bakker82e29452013-08-25 11:01:31 +0200270 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200272 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200273 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 }
275
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200276 printf( " ok\n" );
277
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 /*
279 * 1.1. Load the key
280 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200281 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282 fflush( stdout );
283
Paul Bakker1a7550a2013-09-15 13:01:22 +0200284 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285
286 if( ret != 0 )
287 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200288 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200289 goto exit;
290 }
291
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200292 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200293
294 printf( " ok\n" );
295
296 /*
297 * 1.2. Writing the request
298 */
299 printf( " . Writing the certificate request ..." );
300 fflush( stdout );
301
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200302 if( ( ret = write_certificate_request( &req, opt.output_file,
303 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200304 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200305 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000306 goto exit;
307 }
308
309 printf( " ok\n" );
310
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000311exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200312
313 if( ret != 0 && ret != 1)
314 {
315#ifdef POLARSSL_ERROR_C
316 polarssl_strerror( ret, buf, sizeof( buf ) );
317 printf( " - %s\n", buf );
318#else
319 printf("\n");
320#endif
321 }
322
Paul Bakker82e29452013-08-25 11:01:31 +0200323 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200324 pk_free( &key );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200325 entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000326
327#if defined(_WIN32)
328 printf( " + Press Enter to exit this program.\n" );
329 fflush( stdout ); getchar();
330#endif
331
332 return( ret );
333}
Paul Bakker36713e82013-09-17 13:25:29 +0200334#endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200335 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */