blob: f56cae8e7f32a9c08b6d46ee0c9e5efd921f240c [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 )
Paul Bakker0c226102014-04-17 16:02:36 +020092 {
93 fclose( f );
Paul Bakker135f1e92013-08-26 16:54:13 +020094 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +020095 }
Paul Bakker135f1e92013-08-26 16:54:13 +020096
Paul Bakker0c226102014-04-17 16:02:36 +020097 fclose( f );
Paul Bakker8eabfc12013-08-25 10:18:25 +020098
99 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100}
101
102#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200103 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104 "\n acceptable parameters:\n" \
105 " filename=%%s default: keyfile.key\n" \
106 " debug_level=%%d default: 0 (disabled)\n" \
107 " output_file=%%s default: cert.req\n" \
108 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200109 " key_usage=%%s default: (empty)\n" \
110 " Comma-separated-list of values:\n" \
111 " digital_signature\n" \
112 " non_repudiation\n" \
113 " key_encipherment\n" \
114 " data_encipherment\n" \
115 " key_agreement\n" \
116 " key_certificate_sign\n" \
117 " crl_sign\n" \
118 " ns_cert_type=%%s default: (empty)\n" \
119 " Comma-separated-list of values:\n" \
120 " ssl_client\n" \
121 " ssl_server\n" \
122 " email\n" \
123 " object_signing\n" \
124 " ssl_ca\n" \
125 " email_ca\n" \
126 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127 "\n"
128
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129int main( int argc, char *argv[] )
130{
131 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200132 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100134 int i;
Paul Bakker57be6e22013-08-26 14:13:14 +0200135 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200136 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200137 entropy_context entropy;
138 ctr_drbg_context ctr_drbg;
139 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
141 /*
142 * Set to sane values
143 */
Paul Bakker82e29452013-08-25 11:01:31 +0200144 x509write_csr_init( &req );
145 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200146 pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200147 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148
149 if( argc == 0 )
150 {
151 usage:
152 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200153 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154 goto exit;
155 }
156
157 opt.filename = DFL_FILENAME;
158 opt.debug_level = DFL_DEBUG_LEVEL;
159 opt.output_file = DFL_OUTPUT_FILENAME;
160 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200161 opt.key_usage = DFL_KEY_USAGE;
162 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163
164 for( i = 1; i < argc; i++ )
165 {
166
167 p = argv[i];
168 if( ( q = strchr( p, '=' ) ) == NULL )
169 goto usage;
170 *q++ = '\0';
171
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 if( strcmp( p, "filename" ) == 0 )
173 opt.filename = q;
174 else if( strcmp( p, "output_file" ) == 0 )
175 opt.output_file = q;
176 else if( strcmp( p, "debug_level" ) == 0 )
177 {
178 opt.debug_level = atoi( q );
179 if( opt.debug_level < 0 || opt.debug_level > 65535 )
180 goto usage;
181 }
182 else if( strcmp( p, "subject_name" ) == 0 )
183 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 opt.subject_name = q;
185 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200186 else if( strcmp( p, "key_usage" ) == 0 )
187 {
188 while( q != NULL )
189 {
190 if( ( r = strchr( q, ',' ) ) != NULL )
191 *r++ = '\0';
192
193 if( strcmp( q, "digital_signature" ) == 0 )
194 opt.key_usage |= KU_DIGITAL_SIGNATURE;
195 else if( strcmp( q, "non_repudiation" ) == 0 )
196 opt.key_usage |= KU_NON_REPUDIATION;
197 else if( strcmp( q, "key_encipherment" ) == 0 )
198 opt.key_usage |= KU_KEY_ENCIPHERMENT;
199 else if( strcmp( q, "data_encipherment" ) == 0 )
200 opt.key_usage |= KU_DATA_ENCIPHERMENT;
201 else if( strcmp( q, "key_agreement" ) == 0 )
202 opt.key_usage |= KU_KEY_AGREEMENT;
203 else if( strcmp( q, "key_cert_sign" ) == 0 )
204 opt.key_usage |= KU_KEY_CERT_SIGN;
205 else if( strcmp( q, "crl_sign" ) == 0 )
206 opt.key_usage |= KU_CRL_SIGN;
207 else
208 goto usage;
209
210 q = r;
211 }
212 }
213 else if( strcmp( p, "ns_cert_type" ) == 0 )
214 {
215 while( q != NULL )
216 {
217 if( ( r = strchr( q, ',' ) ) != NULL )
218 *r++ = '\0';
219
220 if( strcmp( q, "ssl_client" ) == 0 )
221 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
222 else if( strcmp( q, "ssl_server" ) == 0 )
223 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
224 else if( strcmp( q, "email" ) == 0 )
225 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
226 else if( strcmp( q, "object_signing" ) == 0 )
227 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
228 else if( strcmp( q, "ssl_ca" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
230 else if( strcmp( q, "email_ca" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
232 else if( strcmp( q, "object_signing_ca" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
234 else
235 goto usage;
236
237 q = r;
238 }
239 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 else
241 goto usage;
242 }
243
Paul Bakker57be6e22013-08-26 14:13:14 +0200244 if( opt.key_usage )
245 x509write_csr_set_key_usage( &req, opt.key_usage );
246
247 if( opt.ns_cert_type )
248 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
249
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200251 * 0. Seed the PRNG
252 */
253 printf( " . Seeding the random number generator..." );
254 fflush( stdout );
255
256 entropy_init( &entropy );
257 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
258 (const unsigned char *) pers,
259 strlen( pers ) ) ) != 0 )
260 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200261 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200262 goto exit;
263 }
264
265 printf( " ok\n" );
266
267 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268 * 1.0. Check the subject name for validity
269 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200270 printf( " . Checking subjet name..." );
271 fflush( stdout );
272
Paul Bakker82e29452013-08-25 11:01:31 +0200273 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200275 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200276 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277 }
278
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200279 printf( " ok\n" );
280
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 /*
282 * 1.1. Load the key
283 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200284 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 fflush( stdout );
286
Paul Bakker1a7550a2013-09-15 13:01:22 +0200287 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288
289 if( ret != 0 )
290 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200291 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200292 goto exit;
293 }
294
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200295 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200296
297 printf( " ok\n" );
298
299 /*
300 * 1.2. Writing the request
301 */
302 printf( " . Writing the certificate request ..." );
303 fflush( stdout );
304
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200305 if( ( ret = write_certificate_request( &req, opt.output_file,
306 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200307 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200308 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 goto exit;
310 }
311
312 printf( " ok\n" );
313
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000314exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200315
316 if( ret != 0 && ret != 1)
317 {
318#ifdef POLARSSL_ERROR_C
319 polarssl_strerror( ret, buf, sizeof( buf ) );
320 printf( " - %s\n", buf );
321#else
322 printf("\n");
323#endif
324 }
325
Paul Bakker82e29452013-08-25 11:01:31 +0200326 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200327 pk_free( &key );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200328 entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000329
330#if defined(_WIN32)
331 printf( " + Press Enter to exit this program.\n" );
332 fflush( stdout ); getchar();
333#endif
334
335 return( ret );
336}
Paul Bakker36713e82013-09-17 13:25:29 +0200337#endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200338 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */