blob: f4b139e3bc0e158fe5bb13bf7dec73b7d4e4b2c0 [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) || \
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020043 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_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é-Gonnard31e59402013-09-12 05:59:05 +020049 printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_X509_PARSE_C and/or "
50 "POLARSSL_FS_IO and/or "
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020051 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
52 "not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020053 return( 0 );
54}
55#else
56
Paul Bakkerbdb912d2012-02-13 23:11:30 +000057#define DFL_FILENAME "keyfile.key"
58#define DFL_DEBUG_LEVEL 0
59#define DFL_OUTPUT_FILENAME "cert.req"
60#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020061#define DFL_KEY_USAGE 0
62#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000063
64/*
65 * global options
66 */
67struct options
68{
69 char *filename; /* filename of the key file */
70 int debug_level; /* level of debugging */
71 char *output_file; /* where to store the constructed key file */
72 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020073 unsigned char key_usage; /* key usage flags */
74 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000075} opt;
76
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020077int write_certificate_request( x509write_csr *req, char *output_file,
78 int (*f_rng)(void *, unsigned char *, size_t),
79 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080{
Paul Bakker135f1e92013-08-26 16:54:13 +020081 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000082 FILE *f;
83 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020084 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085
Paul Bakker135f1e92013-08-26 16:54:13 +020086 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020087 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020088 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000089
Paul Bakker135f1e92013-08-26 16:54:13 +020090 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091
Paul Bakker8eabfc12013-08-25 10:18:25 +020092 if( ( f = fopen( output_file, "w" ) ) == NULL )
93 return( -1 );
94
Paul Bakker135f1e92013-08-26 16:54:13 +020095 if( fwrite( output_buf, 1, len, f ) != len )
96 return( -1 );
97
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020099
100 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101}
102
103#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200104 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105 "\n acceptable parameters:\n" \
106 " filename=%%s default: keyfile.key\n" \
107 " debug_level=%%d default: 0 (disabled)\n" \
108 " output_file=%%s default: cert.req\n" \
109 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200110 " key_usage=%%s default: (empty)\n" \
111 " Comma-separated-list of values:\n" \
112 " digital_signature\n" \
113 " non_repudiation\n" \
114 " key_encipherment\n" \
115 " data_encipherment\n" \
116 " key_agreement\n" \
117 " key_certificate_sign\n" \
118 " crl_sign\n" \
119 " ns_cert_type=%%s default: (empty)\n" \
120 " Comma-separated-list of values:\n" \
121 " ssl_client\n" \
122 " ssl_server\n" \
123 " email\n" \
124 " object_signing\n" \
125 " ssl_ca\n" \
126 " email_ca\n" \
127 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128 "\n"
129
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130int main( int argc, char *argv[] )
131{
132 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200133 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134 char buf[1024];
135 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200136 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200137 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200138 entropy_context entropy;
139 ctr_drbg_context ctr_drbg;
140 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
142 /*
143 * Set to sane values
144 */
Paul Bakker82e29452013-08-25 11:01:31 +0200145 x509write_csr_init( &req );
146 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200147 pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200148 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149
150 if( argc == 0 )
151 {
152 usage:
153 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200154 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155 goto exit;
156 }
157
158 opt.filename = DFL_FILENAME;
159 opt.debug_level = DFL_DEBUG_LEVEL;
160 opt.output_file = DFL_OUTPUT_FILENAME;
161 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200162 opt.key_usage = DFL_KEY_USAGE;
163 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164
165 for( i = 1; i < argc; i++ )
166 {
167
168 p = argv[i];
169 if( ( q = strchr( p, '=' ) ) == NULL )
170 goto usage;
171 *q++ = '\0';
172
173 n = strlen( p );
174 for( j = 0; j < n; j++ )
175 {
176 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
177 argv[i][j] |= 0x20;
178 }
179
180 if( strcmp( p, "filename" ) == 0 )
181 opt.filename = q;
182 else if( strcmp( p, "output_file" ) == 0 )
183 opt.output_file = q;
184 else if( strcmp( p, "debug_level" ) == 0 )
185 {
186 opt.debug_level = atoi( q );
187 if( opt.debug_level < 0 || opt.debug_level > 65535 )
188 goto usage;
189 }
190 else if( strcmp( p, "subject_name" ) == 0 )
191 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192 opt.subject_name = q;
193 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200194 else if( strcmp( p, "key_usage" ) == 0 )
195 {
196 while( q != NULL )
197 {
198 if( ( r = strchr( q, ',' ) ) != NULL )
199 *r++ = '\0';
200
201 if( strcmp( q, "digital_signature" ) == 0 )
202 opt.key_usage |= KU_DIGITAL_SIGNATURE;
203 else if( strcmp( q, "non_repudiation" ) == 0 )
204 opt.key_usage |= KU_NON_REPUDIATION;
205 else if( strcmp( q, "key_encipherment" ) == 0 )
206 opt.key_usage |= KU_KEY_ENCIPHERMENT;
207 else if( strcmp( q, "data_encipherment" ) == 0 )
208 opt.key_usage |= KU_DATA_ENCIPHERMENT;
209 else if( strcmp( q, "key_agreement" ) == 0 )
210 opt.key_usage |= KU_KEY_AGREEMENT;
211 else if( strcmp( q, "key_cert_sign" ) == 0 )
212 opt.key_usage |= KU_KEY_CERT_SIGN;
213 else if( strcmp( q, "crl_sign" ) == 0 )
214 opt.key_usage |= KU_CRL_SIGN;
215 else
216 goto usage;
217
218 q = r;
219 }
220 }
221 else if( strcmp( p, "ns_cert_type" ) == 0 )
222 {
223 while( q != NULL )
224 {
225 if( ( r = strchr( q, ',' ) ) != NULL )
226 *r++ = '\0';
227
228 if( strcmp( q, "ssl_client" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
230 else if( strcmp( q, "ssl_server" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
232 else if( strcmp( q, "email" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
234 else if( strcmp( q, "object_signing" ) == 0 )
235 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
236 else if( strcmp( q, "ssl_ca" ) == 0 )
237 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
238 else if( strcmp( q, "email_ca" ) == 0 )
239 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
240 else if( strcmp( q, "object_signing_ca" ) == 0 )
241 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
242 else
243 goto usage;
244
245 q = r;
246 }
247 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248 else
249 goto usage;
250 }
251
Paul Bakker57be6e22013-08-26 14:13:14 +0200252 if( opt.key_usage )
253 x509write_csr_set_key_usage( &req, opt.key_usage );
254
255 if( opt.ns_cert_type )
256 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
257
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200259 * 0. Seed the PRNG
260 */
261 printf( " . Seeding the random number generator..." );
262 fflush( stdout );
263
264 entropy_init( &entropy );
265 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
266 (const unsigned char *) pers,
267 strlen( pers ) ) ) != 0 )
268 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200269 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200270 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 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200283 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200284 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 }
286
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200287 printf( " ok\n" );
288
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 /*
290 * 1.1. Load the key
291 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200292 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293 fflush( stdout );
294
Paul Bakker1a7550a2013-09-15 13:01:22 +0200295 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296
297 if( ret != 0 )
298 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200299 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200300 goto exit;
301 }
302
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200303 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200304
305 printf( " ok\n" );
306
307 /*
308 * 1.2. Writing the request
309 */
310 printf( " . Writing the certificate request ..." );
311 fflush( stdout );
312
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200313 if( ( ret = write_certificate_request( &req, opt.output_file,
314 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200315 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200316 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000317 goto exit;
318 }
319
320 printf( " ok\n" );
321
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000322exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200323
324 if( ret != 0 && ret != 1)
325 {
326#ifdef POLARSSL_ERROR_C
327 polarssl_strerror( ret, buf, sizeof( buf ) );
328 printf( " - %s\n", buf );
329#else
330 printf("\n");
331#endif
332 }
333
Paul Bakker82e29452013-08-25 11:01:31 +0200334 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200335 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336
337#if defined(_WIN32)
338 printf( " + Press Enter to exit this program.\n" );
339 fflush( stdout ); getchar();
340#endif
341
342 return( ret );
343}
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200344#endif /* POLARSSL_X509_WRITE_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200345 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */