blob: dc45f9496f861caa050d0a416b6d9973f221a54d [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{
65 char *filename; /* filename of the key file */
66 int debug_level; /* level of debugging */
67 char *output_file; /* where to store the constructed key file */
68 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
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020073int write_certificate_request( x509write_csr *req, char *output_file,
74 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];
131 int i, j, n;
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
169 n = strlen( p );
170 for( j = 0; j < n; j++ )
171 {
172 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
173 argv[i][j] |= 0x20;
174 }
175
176 if( strcmp( p, "filename" ) == 0 )
177 opt.filename = q;
178 else if( strcmp( p, "output_file" ) == 0 )
179 opt.output_file = q;
180 else if( strcmp( p, "debug_level" ) == 0 )
181 {
182 opt.debug_level = atoi( q );
183 if( opt.debug_level < 0 || opt.debug_level > 65535 )
184 goto usage;
185 }
186 else if( strcmp( p, "subject_name" ) == 0 )
187 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 opt.subject_name = q;
189 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200190 else if( strcmp( p, "key_usage" ) == 0 )
191 {
192 while( q != NULL )
193 {
194 if( ( r = strchr( q, ',' ) ) != NULL )
195 *r++ = '\0';
196
197 if( strcmp( q, "digital_signature" ) == 0 )
198 opt.key_usage |= KU_DIGITAL_SIGNATURE;
199 else if( strcmp( q, "non_repudiation" ) == 0 )
200 opt.key_usage |= KU_NON_REPUDIATION;
201 else if( strcmp( q, "key_encipherment" ) == 0 )
202 opt.key_usage |= KU_KEY_ENCIPHERMENT;
203 else if( strcmp( q, "data_encipherment" ) == 0 )
204 opt.key_usage |= KU_DATA_ENCIPHERMENT;
205 else if( strcmp( q, "key_agreement" ) == 0 )
206 opt.key_usage |= KU_KEY_AGREEMENT;
207 else if( strcmp( q, "key_cert_sign" ) == 0 )
208 opt.key_usage |= KU_KEY_CERT_SIGN;
209 else if( strcmp( q, "crl_sign" ) == 0 )
210 opt.key_usage |= KU_CRL_SIGN;
211 else
212 goto usage;
213
214 q = r;
215 }
216 }
217 else if( strcmp( p, "ns_cert_type" ) == 0 )
218 {
219 while( q != NULL )
220 {
221 if( ( r = strchr( q, ',' ) ) != NULL )
222 *r++ = '\0';
223
224 if( strcmp( q, "ssl_client" ) == 0 )
225 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
226 else if( strcmp( q, "ssl_server" ) == 0 )
227 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
228 else if( strcmp( q, "email" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
230 else if( strcmp( q, "object_signing" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
232 else if( strcmp( q, "ssl_ca" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
234 else if( strcmp( q, "email_ca" ) == 0 )
235 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
236 else if( strcmp( q, "object_signing_ca" ) == 0 )
237 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
238 else
239 goto usage;
240
241 q = r;
242 }
243 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244 else
245 goto usage;
246 }
247
Paul Bakker57be6e22013-08-26 14:13:14 +0200248 if( opt.key_usage )
249 x509write_csr_set_key_usage( &req, opt.key_usage );
250
251 if( opt.ns_cert_type )
252 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
253
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200255 * 0. Seed the PRNG
256 */
257 printf( " . Seeding the random number generator..." );
258 fflush( stdout );
259
260 entropy_init( &entropy );
261 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
262 (const unsigned char *) pers,
263 strlen( pers ) ) ) != 0 )
264 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200265 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200266 goto exit;
267 }
268
269 printf( " ok\n" );
270
271 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272 * 1.0. Check the subject name for validity
273 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200274 printf( " . Checking subjet name..." );
275 fflush( stdout );
276
Paul Bakker82e29452013-08-25 11:01:31 +0200277 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200279 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200280 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 }
282
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200283 printf( " ok\n" );
284
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 /*
286 * 1.1. Load the key
287 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200288 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 fflush( stdout );
290
Paul Bakker1a7550a2013-09-15 13:01:22 +0200291 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292
293 if( ret != 0 )
294 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200295 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200296 goto exit;
297 }
298
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200299 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200300
301 printf( " ok\n" );
302
303 /*
304 * 1.2. Writing the request
305 */
306 printf( " . Writing the certificate request ..." );
307 fflush( stdout );
308
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200309 if( ( ret = write_certificate_request( &req, opt.output_file,
310 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200311 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200312 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000313 goto exit;
314 }
315
316 printf( " ok\n" );
317
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000318exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200319
320 if( ret != 0 && ret != 1)
321 {
322#ifdef POLARSSL_ERROR_C
323 polarssl_strerror( ret, buf, sizeof( buf ) );
324 printf( " - %s\n", buf );
325#else
326 printf("\n");
327#endif
328 }
329
Paul Bakker82e29452013-08-25 11:01:31 +0200330 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200331 pk_free( &key );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200332 entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333
334#if defined(_WIN32)
335 printf( " + Press Enter to exit this program.\n" );
336 fflush( stdout ); getchar();
337#endif
338
339 return( ret );
340}
Paul Bakker36713e82013-09-17 13:25:29 +0200341#endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200342 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */