blob: 54f1462053c0cef0133b8157b6b179cc0aed5d4e [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Certificate request generation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
Paul Bakkerbdb912d2012-02-13 23:11:30 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "polarssl/x509_csr.h"
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020034#include "polarssl/entropy.h"
35#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020036#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000037
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#if !defined(POLARSSL_X509_CSR_WRITE_C) || !defined(POLARSSL_FS_IO) || \
Paul Bakker36713e82013-09-17 13:25:29 +020039 !defined(POLARSSL_PK_PARSE_C) || \
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020040 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020041int main( int argc, char *argv[] )
42{
43 ((void) argc);
44 ((void) argv);
45
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046 printf( "POLARSSL_X509_CSR_WRITE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020047 "POLARSSL_PK_PARSE_C and/or "
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020048 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
49 "not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020050 return( 0 );
51}
52#else
53
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054#define DFL_FILENAME "keyfile.key"
55#define DFL_DEBUG_LEVEL 0
56#define DFL_OUTPUT_FILENAME "cert.req"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000057#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
Paul Bakker57be6e22013-08-26 14:13:14 +020058#define DFL_KEY_USAGE 0
59#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060
61/*
62 * global options
63 */
64struct options
65{
Paul Bakker8fc30b12013-11-25 13:29:43 +010066 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067 int debug_level; /* level of debugging */
Paul Bakker8fc30b12013-11-25 13:29:43 +010068 const char *output_file; /* where to store the constructed key file */
69 const char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020070 unsigned char key_usage; /* key usage flags */
71 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072} opt;
73
Paul Bakker8fc30b12013-11-25 13:29:43 +010074int write_certificate_request( x509write_csr *req, const char *output_file,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020075 int (*f_rng)(void *, unsigned char *, size_t),
76 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000077{
Paul Bakker135f1e92013-08-26 16:54:13 +020078 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079 FILE *f;
80 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020081 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000082
Paul Bakker135f1e92013-08-26 16:54:13 +020083 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020084 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020085 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086
Paul Bakker135f1e92013-08-26 16:54:13 +020087 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker8eabfc12013-08-25 10:18:25 +020089 if( ( f = fopen( output_file, "w" ) ) == NULL )
90 return( -1 );
91
Paul Bakker135f1e92013-08-26 16:54:13 +020092 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +020093 {
94 fclose( f );
Paul Bakker135f1e92013-08-26 16:54:13 +020095 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +020096 }
Paul Bakker135f1e92013-08-26 16:54:13 +020097
Paul Bakker0c226102014-04-17 16:02:36 +020098 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" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000109 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\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];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100135 int i;
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
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173 if( strcmp( p, "filename" ) == 0 )
174 opt.filename = q;
175 else if( strcmp( p, "output_file" ) == 0 )
176 opt.output_file = q;
177 else if( strcmp( p, "debug_level" ) == 0 )
178 {
179 opt.debug_level = atoi( q );
180 if( opt.debug_level < 0 || opt.debug_level > 65535 )
181 goto usage;
182 }
183 else if( strcmp( p, "subject_name" ) == 0 )
184 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185 opt.subject_name = q;
186 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200187 else if( strcmp( p, "key_usage" ) == 0 )
188 {
189 while( q != NULL )
190 {
191 if( ( r = strchr( q, ',' ) ) != NULL )
192 *r++ = '\0';
193
194 if( strcmp( q, "digital_signature" ) == 0 )
195 opt.key_usage |= KU_DIGITAL_SIGNATURE;
196 else if( strcmp( q, "non_repudiation" ) == 0 )
197 opt.key_usage |= KU_NON_REPUDIATION;
198 else if( strcmp( q, "key_encipherment" ) == 0 )
199 opt.key_usage |= KU_KEY_ENCIPHERMENT;
200 else if( strcmp( q, "data_encipherment" ) == 0 )
201 opt.key_usage |= KU_DATA_ENCIPHERMENT;
202 else if( strcmp( q, "key_agreement" ) == 0 )
203 opt.key_usage |= KU_KEY_AGREEMENT;
204 else if( strcmp( q, "key_cert_sign" ) == 0 )
205 opt.key_usage |= KU_KEY_CERT_SIGN;
206 else if( strcmp( q, "crl_sign" ) == 0 )
207 opt.key_usage |= KU_CRL_SIGN;
208 else
209 goto usage;
210
211 q = r;
212 }
213 }
214 else if( strcmp( p, "ns_cert_type" ) == 0 )
215 {
216 while( q != NULL )
217 {
218 if( ( r = strchr( q, ',' ) ) != NULL )
219 *r++ = '\0';
220
221 if( strcmp( q, "ssl_client" ) == 0 )
222 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
223 else if( strcmp( q, "ssl_server" ) == 0 )
224 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
225 else if( strcmp( q, "email" ) == 0 )
226 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
227 else if( strcmp( q, "object_signing" ) == 0 )
228 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
229 else if( strcmp( q, "ssl_ca" ) == 0 )
230 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
231 else if( strcmp( q, "email_ca" ) == 0 )
232 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
233 else if( strcmp( q, "object_signing_ca" ) == 0 )
234 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
235 else
236 goto usage;
237
238 q = r;
239 }
240 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241 else
242 goto usage;
243 }
244
Paul Bakker57be6e22013-08-26 14:13:14 +0200245 if( opt.key_usage )
246 x509write_csr_set_key_usage( &req, opt.key_usage );
247
248 if( opt.ns_cert_type )
249 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
250
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000251 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200252 * 0. Seed the PRNG
253 */
254 printf( " . Seeding the random number generator..." );
255 fflush( stdout );
256
257 entropy_init( &entropy );
258 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
259 (const unsigned char *) pers,
260 strlen( pers ) ) ) != 0 )
261 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200262 printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200263 goto exit;
264 }
265
266 printf( " ok\n" );
267
268 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269 * 1.0. Check the subject name for validity
270 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200271 printf( " . Checking subjet name..." );
272 fflush( stdout );
273
Paul Bakker82e29452013-08-25 11:01:31 +0200274 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200276 printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200277 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 }
279
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200280 printf( " ok\n" );
281
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282 /*
283 * 1.1. Load the key
284 */
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200285 printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286 fflush( stdout );
287
Paul Bakker1a7550a2013-09-15 13:01:22 +0200288 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289
290 if( ret != 0 )
291 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200292 printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200293 goto exit;
294 }
295
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200296 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200297
298 printf( " ok\n" );
299
300 /*
301 * 1.2. Writing the request
302 */
303 printf( " . Writing the certificate request ..." );
304 fflush( stdout );
305
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200306 if( ( ret = write_certificate_request( &req, opt.output_file,
307 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200308 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200309 printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000310 goto exit;
311 }
312
313 printf( " ok\n" );
314
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000315exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200316
317 if( ret != 0 && ret != 1)
318 {
319#ifdef POLARSSL_ERROR_C
320 polarssl_strerror( ret, buf, sizeof( buf ) );
321 printf( " - %s\n", buf );
322#else
323 printf("\n");
324#endif
325 }
326
Paul Bakker82e29452013-08-25 11:01:31 +0200327 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200328 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200329 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200330 entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000331
332#if defined(_WIN32)
333 printf( " + Press Enter to exit this program.\n" );
334 fflush( stdout ); getchar();
335#endif
336
337 return( ret );
338}
Paul Bakker36713e82013-09-17 13:25:29 +0200339#endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200340 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */