blob: ff651980aa56e7ac7ee43c97ae6d4291b3fa9abd [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 Bakker7c6b2c32013-09-16 13:49:26 +020036#include "polarssl/x509_csr.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
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041#if !defined(POLARSSL_X509_CSR_WRITE_C) || !defined(POLARSSL_FS_IO) || \
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020042 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020043int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048 printf( "POLARSSL_X509_CSR_WRITE_C and/or POLARSSL_FS_IO and/or "
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020049 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
50 "not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020051 return( 0 );
52}
53#else
54
Paul Bakkerbdb912d2012-02-13 23:11:30 +000055#define DFL_FILENAME "keyfile.key"
56#define DFL_DEBUG_LEVEL 0
57#define DFL_OUTPUT_FILENAME "cert.req"
58#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020059#define DFL_KEY_USAGE 0
60#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061
62/*
63 * global options
64 */
65struct options
66{
67 char *filename; /* filename of the key file */
68 int debug_level; /* level of debugging */
69 char *output_file; /* where to store the constructed key file */
70 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020071 unsigned char key_usage; /* key usage flags */
72 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073} opt;
74
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020075int write_certificate_request( x509write_csr *req, char *output_file,
76 int (*f_rng)(void *, unsigned char *, size_t),
77 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078{
Paul Bakker135f1e92013-08-26 16:54:13 +020079 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080 FILE *f;
81 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020082 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083
Paul Bakker135f1e92013-08-26 16:54:13 +020084 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020085 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020086 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000087
Paul Bakker135f1e92013-08-26 16:54:13 +020088 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000089
Paul Bakker8eabfc12013-08-25 10:18:25 +020090 if( ( f = fopen( output_file, "w" ) ) == NULL )
91 return( -1 );
92
Paul Bakker135f1e92013-08-26 16:54:13 +020093 if( fwrite( output_buf, 1, len, f ) != len )
94 return( -1 );
95
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020097
98 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099}
100
101#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200102 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103 "\n acceptable parameters:\n" \
104 " filename=%%s default: keyfile.key\n" \
105 " debug_level=%%d default: 0 (disabled)\n" \
106 " output_file=%%s default: cert.req\n" \
107 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200108 " key_usage=%%s default: (empty)\n" \
109 " Comma-separated-list of values:\n" \
110 " digital_signature\n" \
111 " non_repudiation\n" \
112 " key_encipherment\n" \
113 " data_encipherment\n" \
114 " key_agreement\n" \
115 " key_certificate_sign\n" \
116 " crl_sign\n" \
117 " ns_cert_type=%%s default: (empty)\n" \
118 " Comma-separated-list of values:\n" \
119 " ssl_client\n" \
120 " ssl_server\n" \
121 " email\n" \
122 " object_signing\n" \
123 " ssl_ca\n" \
124 " email_ca\n" \
125 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126 "\n"
127
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128int main( int argc, char *argv[] )
129{
130 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200131 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132 char buf[1024];
133 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200134 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200135 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200136 entropy_context entropy;
137 ctr_drbg_context ctr_drbg;
138 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
140 /*
141 * Set to sane values
142 */
Paul Bakker82e29452013-08-25 11:01:31 +0200143 x509write_csr_init( &req );
144 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200145 pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200146 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
148 if( argc == 0 )
149 {
150 usage:
151 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200152 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 goto exit;
154 }
155
156 opt.filename = DFL_FILENAME;
157 opt.debug_level = DFL_DEBUG_LEVEL;
158 opt.output_file = DFL_OUTPUT_FILENAME;
159 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200160 opt.key_usage = DFL_KEY_USAGE;
161 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162
163 for( i = 1; i < argc; i++ )
164 {
165
166 p = argv[i];
167 if( ( q = strchr( p, '=' ) ) == NULL )
168 goto usage;
169 *q++ = '\0';
170
171 n = strlen( p );
172 for( j = 0; j < n; j++ )
173 {
174 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
175 argv[i][j] |= 0x20;
176 }
177
178 if( strcmp( p, "filename" ) == 0 )
179 opt.filename = q;
180 else if( strcmp( p, "output_file" ) == 0 )
181 opt.output_file = q;
182 else if( strcmp( p, "debug_level" ) == 0 )
183 {
184 opt.debug_level = atoi( q );
185 if( opt.debug_level < 0 || opt.debug_level > 65535 )
186 goto usage;
187 }
188 else if( strcmp( p, "subject_name" ) == 0 )
189 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190 opt.subject_name = q;
191 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200192 else if( strcmp( p, "key_usage" ) == 0 )
193 {
194 while( q != NULL )
195 {
196 if( ( r = strchr( q, ',' ) ) != NULL )
197 *r++ = '\0';
198
199 if( strcmp( q, "digital_signature" ) == 0 )
200 opt.key_usage |= KU_DIGITAL_SIGNATURE;
201 else if( strcmp( q, "non_repudiation" ) == 0 )
202 opt.key_usage |= KU_NON_REPUDIATION;
203 else if( strcmp( q, "key_encipherment" ) == 0 )
204 opt.key_usage |= KU_KEY_ENCIPHERMENT;
205 else if( strcmp( q, "data_encipherment" ) == 0 )
206 opt.key_usage |= KU_DATA_ENCIPHERMENT;
207 else if( strcmp( q, "key_agreement" ) == 0 )
208 opt.key_usage |= KU_KEY_AGREEMENT;
209 else if( strcmp( q, "key_cert_sign" ) == 0 )
210 opt.key_usage |= KU_KEY_CERT_SIGN;
211 else if( strcmp( q, "crl_sign" ) == 0 )
212 opt.key_usage |= KU_CRL_SIGN;
213 else
214 goto usage;
215
216 q = r;
217 }
218 }
219 else if( strcmp( p, "ns_cert_type" ) == 0 )
220 {
221 while( q != NULL )
222 {
223 if( ( r = strchr( q, ',' ) ) != NULL )
224 *r++ = '\0';
225
226 if( strcmp( q, "ssl_client" ) == 0 )
227 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
228 else if( strcmp( q, "ssl_server" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
230 else if( strcmp( q, "email" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
232 else if( strcmp( q, "object_signing" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
234 else if( strcmp( q, "ssl_ca" ) == 0 )
235 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
236 else if( strcmp( q, "email_ca" ) == 0 )
237 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
238 else if( strcmp( q, "object_signing_ca" ) == 0 )
239 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
240 else
241 goto usage;
242
243 q = r;
244 }
245 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000246 else
247 goto usage;
248 }
249
Paul Bakker57be6e22013-08-26 14:13:14 +0200250 if( opt.key_usage )
251 x509write_csr_set_key_usage( &req, opt.key_usage );
252
253 if( opt.ns_cert_type )
254 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
255
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200257 * 0. Seed the PRNG
258 */
259 printf( " . Seeding the random number generator..." );
260 fflush( stdout );
261
262 entropy_init( &entropy );
263 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
264 (const unsigned char *) pers,
265 strlen( pers ) ) ) != 0 )
266 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200267 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200268 goto exit;
269 }
270
271 printf( " ok\n" );
272
273 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 * 1.0. Check the subject name for validity
275 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200276 printf( " . Checking subjet name..." );
277 fflush( stdout );
278
Paul Bakker82e29452013-08-25 11:01:31 +0200279 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000280 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200281 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200282 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 }
284
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200285 printf( " ok\n" );
286
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287 /*
288 * 1.1. Load the key
289 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200290 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000291 fflush( stdout );
292
Paul Bakker1a7550a2013-09-15 13:01:22 +0200293 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294
295 if( ret != 0 )
296 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200297 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200298 goto exit;
299 }
300
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200301 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200302
303 printf( " ok\n" );
304
305 /*
306 * 1.2. Writing the request
307 */
308 printf( " . Writing the certificate request ..." );
309 fflush( stdout );
310
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200311 if( ( ret = write_certificate_request( &req, opt.output_file,
312 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200313 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200314 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000315 goto exit;
316 }
317
318 printf( " ok\n" );
319
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000320exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200321
322 if( ret != 0 && ret != 1)
323 {
324#ifdef POLARSSL_ERROR_C
325 polarssl_strerror( ret, buf, sizeof( buf ) );
326 printf( " - %s\n", buf );
327#else
328 printf("\n");
329#endif
330 }
331
Paul Bakker82e29452013-08-25 11:01:31 +0200332 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200333 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000334
335#if defined(_WIN32)
336 printf( " + Press Enter to exit this program.\n" );
337 fflush( stdout ); getchar();
338#endif
339
340 return( ret );
341}
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200342#endif /* POLARSSL_X509_WRITE_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200343 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */