blob: 3b67f65056fa415420a3d745d6e02692ab89d753 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakkerbdb912d2012-02-13 23:11:30 +000035#include <string.h>
36#include <stdlib.h>
37#include <stdio.h>
38
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039#include "polarssl/x509_csr.h"
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020040#include "polarssl/entropy.h"
41#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +020042#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#if !defined(POLARSSL_X509_CSR_WRITE_C) || !defined(POLARSSL_FS_IO) || \
Paul Bakker36713e82013-09-17 13:25:29 +020045 !defined(POLARSSL_PK_PARSE_C) || \
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020046 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020047int main( int argc, char *argv[] )
48{
49 ((void) argc);
50 ((void) argv);
51
Rich Evansf90016a2015-01-19 14:26:37 +000052 polarssl_printf( "POLARSSL_X509_CSR_WRITE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020053 "POLARSSL_PK_PARSE_C and/or "
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020054 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
55 "not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020056 return( 0 );
57}
58#else
59
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060#define DFL_FILENAME "keyfile.key"
61#define DFL_DEBUG_LEVEL 0
62#define DFL_OUTPUT_FILENAME "cert.req"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000063#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
Paul Bakker57be6e22013-08-26 14:13:14 +020064#define DFL_KEY_USAGE 0
65#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066
67/*
68 * global options
69 */
70struct options
71{
Paul Bakker8fc30b12013-11-25 13:29:43 +010072 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073 int debug_level; /* level of debugging */
Paul Bakker8fc30b12013-11-25 13:29:43 +010074 const char *output_file; /* where to store the constructed key file */
75 const char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020076 unsigned char key_usage; /* key usage flags */
77 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078} opt;
79
Paul Bakker8fc30b12013-11-25 13:29:43 +010080int write_certificate_request( x509write_csr *req, const char *output_file,
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020081 int (*f_rng)(void *, unsigned char *, size_t),
82 void *p_rng )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083{
Paul Bakker135f1e92013-08-26 16:54:13 +020084 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085 FILE *f;
86 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020087 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker135f1e92013-08-26 16:54:13 +020089 memset( output_buf, 0, 4096 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020090 if( ( ret = x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020091 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092
Paul Bakker135f1e92013-08-26 16:54:13 +020093 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094
Paul Bakker8eabfc12013-08-25 10:18:25 +020095 if( ( f = fopen( output_file, "w" ) ) == NULL )
96 return( -1 );
97
Paul Bakker135f1e92013-08-26 16:54:13 +020098 if( fwrite( output_buf, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +020099 {
100 fclose( f );
Paul Bakker135f1e92013-08-26 16:54:13 +0200101 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200102 }
Paul Bakker135f1e92013-08-26 16:54:13 +0200103
Paul Bakker0c226102014-04-17 16:02:36 +0200104 fclose( f );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200105
106 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107}
108
109#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200110 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111 "\n acceptable parameters:\n" \
112 " filename=%%s default: keyfile.key\n" \
113 " debug_level=%%d default: 0 (disabled)\n" \
114 " output_file=%%s default: cert.req\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000115 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200116 " key_usage=%%s default: (empty)\n" \
117 " Comma-separated-list of values:\n" \
118 " digital_signature\n" \
119 " non_repudiation\n" \
120 " key_encipherment\n" \
121 " data_encipherment\n" \
122 " key_agreement\n" \
123 " key_certificate_sign\n" \
124 " crl_sign\n" \
125 " ns_cert_type=%%s default: (empty)\n" \
126 " Comma-separated-list of values:\n" \
127 " ssl_client\n" \
128 " ssl_server\n" \
129 " email\n" \
130 " object_signing\n" \
131 " ssl_ca\n" \
132 " email_ca\n" \
133 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134 "\n"
135
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136int main( int argc, char *argv[] )
137{
138 int ret = 0;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200139 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100141 int i;
Paul Bakker57be6e22013-08-26 14:13:14 +0200142 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200143 x509write_csr req;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200144 entropy_context entropy;
145 ctr_drbg_context ctr_drbg;
146 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
148 /*
149 * Set to sane values
150 */
Paul Bakker82e29452013-08-25 11:01:31 +0200151 x509write_csr_init( &req );
152 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200153 pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200154 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155
156 if( argc == 0 )
157 {
158 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200160 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161 goto exit;
162 }
163
164 opt.filename = DFL_FILENAME;
165 opt.debug_level = DFL_DEBUG_LEVEL;
166 opt.output_file = DFL_OUTPUT_FILENAME;
167 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200168 opt.key_usage = DFL_KEY_USAGE;
169 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170
171 for( i = 1; i < argc; i++ )
172 {
173
174 p = argv[i];
175 if( ( q = strchr( p, '=' ) ) == NULL )
176 goto usage;
177 *q++ = '\0';
178
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179 if( strcmp( p, "filename" ) == 0 )
180 opt.filename = q;
181 else if( strcmp( p, "output_file" ) == 0 )
182 opt.output_file = q;
183 else if( strcmp( p, "debug_level" ) == 0 )
184 {
185 opt.debug_level = atoi( q );
186 if( opt.debug_level < 0 || opt.debug_level > 65535 )
187 goto usage;
188 }
189 else if( strcmp( p, "subject_name" ) == 0 )
190 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191 opt.subject_name = q;
192 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200193 else if( strcmp( p, "key_usage" ) == 0 )
194 {
195 while( q != NULL )
196 {
197 if( ( r = strchr( q, ',' ) ) != NULL )
198 *r++ = '\0';
199
200 if( strcmp( q, "digital_signature" ) == 0 )
201 opt.key_usage |= KU_DIGITAL_SIGNATURE;
202 else if( strcmp( q, "non_repudiation" ) == 0 )
203 opt.key_usage |= KU_NON_REPUDIATION;
204 else if( strcmp( q, "key_encipherment" ) == 0 )
205 opt.key_usage |= KU_KEY_ENCIPHERMENT;
206 else if( strcmp( q, "data_encipherment" ) == 0 )
207 opt.key_usage |= KU_DATA_ENCIPHERMENT;
208 else if( strcmp( q, "key_agreement" ) == 0 )
209 opt.key_usage |= KU_KEY_AGREEMENT;
210 else if( strcmp( q, "key_cert_sign" ) == 0 )
211 opt.key_usage |= KU_KEY_CERT_SIGN;
212 else if( strcmp( q, "crl_sign" ) == 0 )
213 opt.key_usage |= KU_CRL_SIGN;
214 else
215 goto usage;
216
217 q = r;
218 }
219 }
220 else if( strcmp( p, "ns_cert_type" ) == 0 )
221 {
222 while( q != NULL )
223 {
224 if( ( r = strchr( q, ',' ) ) != NULL )
225 *r++ = '\0';
226
227 if( strcmp( q, "ssl_client" ) == 0 )
228 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
229 else if( strcmp( q, "ssl_server" ) == 0 )
230 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
231 else if( strcmp( q, "email" ) == 0 )
232 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
233 else if( strcmp( q, "object_signing" ) == 0 )
234 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
235 else if( strcmp( q, "ssl_ca" ) == 0 )
236 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
237 else if( strcmp( q, "email_ca" ) == 0 )
238 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
239 else if( strcmp( q, "object_signing_ca" ) == 0 )
240 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
241 else
242 goto usage;
243
244 q = r;
245 }
246 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 else
248 goto usage;
249 }
250
Paul Bakker57be6e22013-08-26 14:13:14 +0200251 if( opt.key_usage )
252 x509write_csr_set_key_usage( &req, opt.key_usage );
253
254 if( opt.ns_cert_type )
255 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
256
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200258 * 0. Seed the PRNG
259 */
Rich Evansf90016a2015-01-19 14:26:37 +0000260 polarssl_printf( " . Seeding the random number generator..." );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200261 fflush( stdout );
262
263 entropy_init( &entropy );
264 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
265 (const unsigned char *) pers,
266 strlen( pers ) ) ) != 0 )
267 {
Rich Evansf90016a2015-01-19 14:26:37 +0000268 polarssl_printf( " failed\n ! ctr_drbg_init returned %d", ret );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200269 goto exit;
270 }
271
Rich Evansf90016a2015-01-19 14:26:37 +0000272 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200273
274 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275 * 1.0. Check the subject name for validity
276 */
Rich Evansf90016a2015-01-19 14:26:37 +0000277 polarssl_printf( " . Checking subjet name..." );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200278 fflush( stdout );
279
Paul Bakker82e29452013-08-25 11:01:31 +0200280 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200283 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000284 }
285
Rich Evansf90016a2015-01-19 14:26:37 +0000286 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200287
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288 /*
289 * 1.1. Load the key
290 */
Rich Evansf90016a2015-01-19 14:26:37 +0000291 polarssl_printf( " . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 fflush( stdout );
293
Paul Bakker1a7550a2013-09-15 13:01:22 +0200294 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
296 if( ret != 0 )
297 {
Rich Evansf90016a2015-01-19 14:26:37 +0000298 polarssl_printf( " failed\n ! pk_parse_keyfile returned %d", ret );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200299 goto exit;
300 }
301
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200302 x509write_csr_set_key( &req, &key );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200303
Rich Evansf90016a2015-01-19 14:26:37 +0000304 polarssl_printf( " ok\n" );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200305
306 /*
307 * 1.2. Writing the request
308 */
Rich Evansf90016a2015-01-19 14:26:37 +0000309 polarssl_printf( " . Writing the certificate request ..." );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200310 fflush( stdout );
311
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200312 if( ( ret = write_certificate_request( &req, opt.output_file,
313 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +0200314 {
Rich Evansf90016a2015-01-19 14:26:37 +0000315 polarssl_printf( " failed\n ! write_certifcate_request %d", ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000316 goto exit;
317 }
318
Rich Evansf90016a2015-01-19 14:26:37 +0000319 polarssl_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000320
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000321exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200322
323 if( ret != 0 && ret != 1)
324 {
325#ifdef POLARSSL_ERROR_C
326 polarssl_strerror( ret, buf, sizeof( buf ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000327 polarssl_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200328#else
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200330#endif
331 }
332
Paul Bakker82e29452013-08-25 11:01:31 +0200333 x509write_csr_free( &req );
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200334 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200335 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200336 entropy_free( &entropy );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000337
338#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000339 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000340 fflush( stdout ); getchar();
341#endif
342
343 return( ret );
344}
Paul Bakker36713e82013-09-17 13:25:29 +0200345#endif /* POLARSSL_X509_CSR_WRITE_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200346 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */