blob: e65fb97e61dd4eb7d983564cf80c549ab077d682 [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/entropy.h"
38#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020039#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020041#if !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_X509_PARSE_C) || \
42 !defined(POLARSSL_FS_IO) || \
43 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020044 !defined(POLARSSL_ERROR_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020045int main( int argc, char *argv[] )
46{
47 ((void) argc);
48 ((void) argv);
49
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020050 printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_X509_PARSE_C and/or "
51 "POLARSSL_FS_IO and/or "
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020052 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
53 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020054 return( 0 );
55}
56#else
57
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058#define DFL_FILENAME "keyfile.key"
59#define DFL_DEBUG_LEVEL 0
60#define DFL_OUTPUT_FILENAME "cert.req"
61#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020062#define DFL_KEY_USAGE 0
63#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064
65/*
66 * global options
67 */
68struct options
69{
70 char *filename; /* filename of the key file */
71 int debug_level; /* level of debugging */
72 char *output_file; /* where to store the constructed key file */
73 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020074 unsigned char key_usage; /* key usage flags */
75 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076} opt;
77
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020078int write_certificate_request( x509write_csr *req, char *output_file,
79 int (*f_rng)(void *, unsigned char *, size_t),
80 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081{
Paul Bakker135f1e92013-08-26 16:54:13 +020082 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083 FILE *f;
84 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020085 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086
Paul Bakker135f1e92013-08-26 16:54:13 +020087 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020088 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020089 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090
Paul Bakker135f1e92013-08-26 16:54:13 +020091 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092
Paul Bakker8eabfc12013-08-25 10:18:25 +020093 if( ( f = fopen( output_file, "w" ) ) == NULL )
94 return( -1 );
95
Paul Bakker135f1e92013-08-26 16:54:13 +020096 if( fwrite( output_buf, 1, len, f ) != len )
97 return( -1 );
98
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200100
101 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102}
103
104#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200105 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106 "\n acceptable parameters:\n" \
107 " filename=%%s default: keyfile.key\n" \
108 " debug_level=%%d default: 0 (disabled)\n" \
109 " output_file=%%s default: cert.req\n" \
110 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200111 " key_usage=%%s default: (empty)\n" \
112 " Comma-separated-list of values:\n" \
113 " digital_signature\n" \
114 " non_repudiation\n" \
115 " key_encipherment\n" \
116 " data_encipherment\n" \
117 " key_agreement\n" \
118 " key_certificate_sign\n" \
119 " crl_sign\n" \
120 " ns_cert_type=%%s default: (empty)\n" \
121 " Comma-separated-list of values:\n" \
122 " ssl_client\n" \
123 " ssl_server\n" \
124 " email\n" \
125 " object_signing\n" \
126 " ssl_ca\n" \
127 " email_ca\n" \
128 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129 "\n"
130
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131int main( int argc, char *argv[] )
132{
133 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200134 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135 char buf[1024];
136 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200137 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200138 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200139 entropy_context entropy;
140 ctr_drbg_context ctr_drbg;
141 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142
143 /*
144 * Set to sane values
145 */
Paul Bakker82e29452013-08-25 11:01:31 +0200146 x509write_csr_init( &req );
147 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200148 pk_init( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149 memset( buf, 0, 1024 );
150
151 if( argc == 0 )
152 {
153 usage:
154 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200155 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156 goto exit;
157 }
158
159 opt.filename = DFL_FILENAME;
160 opt.debug_level = DFL_DEBUG_LEVEL;
161 opt.output_file = DFL_OUTPUT_FILENAME;
162 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200163 opt.key_usage = DFL_KEY_USAGE;
164 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165
166 for( i = 1; i < argc; i++ )
167 {
168
169 p = argv[i];
170 if( ( q = strchr( p, '=' ) ) == NULL )
171 goto usage;
172 *q++ = '\0';
173
174 n = strlen( p );
175 for( j = 0; j < n; j++ )
176 {
177 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
178 argv[i][j] |= 0x20;
179 }
180
181 if( strcmp( p, "filename" ) == 0 )
182 opt.filename = q;
183 else if( strcmp( p, "output_file" ) == 0 )
184 opt.output_file = q;
185 else if( strcmp( p, "debug_level" ) == 0 )
186 {
187 opt.debug_level = atoi( q );
188 if( opt.debug_level < 0 || opt.debug_level > 65535 )
189 goto usage;
190 }
191 else if( strcmp( p, "subject_name" ) == 0 )
192 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193 opt.subject_name = q;
194 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200195 else if( strcmp( p, "key_usage" ) == 0 )
196 {
197 while( q != NULL )
198 {
199 if( ( r = strchr( q, ',' ) ) != NULL )
200 *r++ = '\0';
201
202 if( strcmp( q, "digital_signature" ) == 0 )
203 opt.key_usage |= KU_DIGITAL_SIGNATURE;
204 else if( strcmp( q, "non_repudiation" ) == 0 )
205 opt.key_usage |= KU_NON_REPUDIATION;
206 else if( strcmp( q, "key_encipherment" ) == 0 )
207 opt.key_usage |= KU_KEY_ENCIPHERMENT;
208 else if( strcmp( q, "data_encipherment" ) == 0 )
209 opt.key_usage |= KU_DATA_ENCIPHERMENT;
210 else if( strcmp( q, "key_agreement" ) == 0 )
211 opt.key_usage |= KU_KEY_AGREEMENT;
212 else if( strcmp( q, "key_cert_sign" ) == 0 )
213 opt.key_usage |= KU_KEY_CERT_SIGN;
214 else if( strcmp( q, "crl_sign" ) == 0 )
215 opt.key_usage |= KU_CRL_SIGN;
216 else
217 goto usage;
218
219 q = r;
220 }
221 }
222 else if( strcmp( p, "ns_cert_type" ) == 0 )
223 {
224 while( q != NULL )
225 {
226 if( ( r = strchr( q, ',' ) ) != NULL )
227 *r++ = '\0';
228
229 if( strcmp( q, "ssl_client" ) == 0 )
230 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
231 else if( strcmp( q, "ssl_server" ) == 0 )
232 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
233 else if( strcmp( q, "email" ) == 0 )
234 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
235 else if( strcmp( q, "object_signing" ) == 0 )
236 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
237 else if( strcmp( q, "ssl_ca" ) == 0 )
238 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
239 else if( strcmp( q, "email_ca" ) == 0 )
240 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
241 else if( strcmp( q, "object_signing_ca" ) == 0 )
242 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
243 else
244 goto usage;
245
246 q = r;
247 }
248 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 else
250 goto usage;
251 }
252
Paul Bakker57be6e22013-08-26 14:13:14 +0200253 if( opt.key_usage )
254 x509write_csr_set_key_usage( &req, opt.key_usage );
255
256 if( opt.ns_cert_type )
257 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
258
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200260 * 0. Seed the PRNG
261 */
262 printf( " . Seeding the random number generator..." );
263 fflush( stdout );
264
265 entropy_init( &entropy );
266 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
267 (const unsigned char *) pers,
268 strlen( pers ) ) ) != 0 )
269 {
270 error_strerror( ret, buf, 1024 );
271 printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
272 goto exit;
273 }
274
275 printf( " ok\n" );
276
277 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 * 1.0. Check the subject name for validity
279 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200280 printf( " . Checking subjet name..." );
281 fflush( stdout );
282
Paul Bakker82e29452013-08-25 11:01:31 +0200283 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000284 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200285 error_strerror( ret, buf, 1024 );
Paul Bakker82e29452013-08-25 11:01:31 +0200286 printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200287 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288 }
289
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200290 printf( " ok\n" );
291
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 /*
293 * 1.1. Load the key
294 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200295 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296 fflush( stdout );
297
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200298 ret = x509parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299
300 if( ret != 0 )
301 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000302 error_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200303 printf( " failed\n ! x509parse_keyfile returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200304 goto exit;
305 }
306
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200307 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200308
309 printf( " ok\n" );
310
311 /*
312 * 1.2. Writing the request
313 */
314 printf( " . Writing the certificate request ..." );
315 fflush( stdout );
316
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200317 if( ( ret = write_certificate_request( &req, opt.output_file,
318 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200319 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200320 error_strerror( ret, buf, 1024 );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200321 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000322 goto exit;
323 }
324
325 printf( " ok\n" );
326
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000327exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200328 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200329 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000330
331#if defined(_WIN32)
332 printf( " + Press Enter to exit this program.\n" );
333 fflush( stdout ); getchar();
334#endif
335
336 return( ret );
337}
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200338#endif /* POLARSSL_X509_WRITE_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200339 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
340 POLARSSL_ERROR_C */