blob: 384ef08d6468ce243c7712820355701ea8fb21a1 [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
36#include "polarssl/error.h"
37#include "polarssl/rsa.h"
38#include "polarssl/x509.h"
39#include "polarssl/base64.h"
40#include "polarssl/x509write.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020041#include "polarssl/oid.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000042
43#define DFL_FILENAME "keyfile.key"
44#define DFL_DEBUG_LEVEL 0
45#define DFL_OUTPUT_FILENAME "cert.req"
46#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020047#define DFL_KEY_USAGE 0
48#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049
50/*
51 * global options
52 */
53struct options
54{
55 char *filename; /* filename of the key file */
56 int debug_level; /* level of debugging */
57 char *output_file; /* where to store the constructed key file */
58 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020059 unsigned char key_usage; /* key usage flags */
60 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061} opt;
62
Paul Bakker82e29452013-08-25 11:01:31 +020063int write_certificate_request( x509_csr *req, char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064{
Paul Bakker135f1e92013-08-26 16:54:13 +020065 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066 FILE *f;
67 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020068 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000069
Paul Bakker135f1e92013-08-26 16:54:13 +020070 memset( output_buf, 0, 4096 );
71 if( ( ret = x509write_csr_pem( req, output_buf, 4096 ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020072 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073
Paul Bakker135f1e92013-08-26 16:54:13 +020074 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000075
Paul Bakker8eabfc12013-08-25 10:18:25 +020076 if( ( f = fopen( output_file, "w" ) ) == NULL )
77 return( -1 );
78
Paul Bakker135f1e92013-08-26 16:54:13 +020079 if( fwrite( output_buf, 1, len, f ) != len )
80 return( -1 );
81
Paul Bakkerbdb912d2012-02-13 23:11:30 +000082 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020083
84 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085}
86
87#define USAGE \
88 "\n usage: key_app param=<>...\n" \
89 "\n acceptable parameters:\n" \
90 " filename=%%s default: keyfile.key\n" \
91 " debug_level=%%d default: 0 (disabled)\n" \
92 " output_file=%%s default: cert.req\n" \
93 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +020094 " key_usage=%%s default: (empty)\n" \
95 " Comma-separated-list of values:\n" \
96 " digital_signature\n" \
97 " non_repudiation\n" \
98 " key_encipherment\n" \
99 " data_encipherment\n" \
100 " key_agreement\n" \
101 " key_certificate_sign\n" \
102 " crl_sign\n" \
103 " ns_cert_type=%%s default: (empty)\n" \
104 " Comma-separated-list of values:\n" \
105 " ssl_client\n" \
106 " ssl_server\n" \
107 " email\n" \
108 " object_signing\n" \
109 " ssl_ca\n" \
110 " email_ca\n" \
111 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112 "\n"
113
114#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
115 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
116int main( int argc, char *argv[] )
117{
118 ((void) argc);
119 ((void) argv);
120
121 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
122 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
123 return( 0 );
124}
125#else
126int main( int argc, char *argv[] )
127{
128 int ret = 0;
129 rsa_context rsa;
130 char buf[1024];
131 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200132 char *p, *q, *r;
Paul Bakker82e29452013-08-25 11:01:31 +0200133 x509_csr req;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134
135 /*
136 * Set to sane values
137 */
Paul Bakker82e29452013-08-25 11:01:31 +0200138 x509write_csr_init( &req );
139 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140 memset( &rsa, 0, sizeof( rsa_context ) );
141 memset( buf, 0, 1024 );
142
143 if( argc == 0 )
144 {
145 usage:
146 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200147 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148 goto exit;
149 }
150
151 opt.filename = DFL_FILENAME;
152 opt.debug_level = DFL_DEBUG_LEVEL;
153 opt.output_file = DFL_OUTPUT_FILENAME;
154 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200155 opt.key_usage = DFL_KEY_USAGE;
156 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157
158 for( i = 1; i < argc; i++ )
159 {
160
161 p = argv[i];
162 if( ( q = strchr( p, '=' ) ) == NULL )
163 goto usage;
164 *q++ = '\0';
165
166 n = strlen( p );
167 for( j = 0; j < n; j++ )
168 {
169 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
170 argv[i][j] |= 0x20;
171 }
172
173 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 /*
252 * 1.0. Check the subject name for validity
253 */
Paul Bakker82e29452013-08-25 11:01:31 +0200254 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200256#ifdef POLARSSL_ERROR_C
257 error_strerror( ret, buf, 1024 );
258#endif
Paul Bakker82e29452013-08-25 11:01:31 +0200259 printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200260 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261 }
262
263 /*
264 * 1.1. Load the key
265 */
266 printf( "\n . Loading the private key ..." );
267 fflush( stdout );
268
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200269 ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270
271 if( ret != 0 )
272 {
273#ifdef POLARSSL_ERROR_C
274 error_strerror( ret, buf, 1024 );
275#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200276 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200277 goto exit;
278 }
279
Paul Bakker82e29452013-08-25 11:01:31 +0200280 x509write_csr_set_rsa_key( &req, &rsa );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200281
282 printf( " ok\n" );
283
284 /*
285 * 1.2. Writing the request
286 */
287 printf( " . Writing the certificate request ..." );
288 fflush( stdout );
289
290 if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 )
291 {
292#ifdef POLARSSL_ERROR_C
293 error_strerror( ret, buf, 1024 );
294#endif
295 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296 goto exit;
297 }
298
299 printf( " ok\n" );
300
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000301exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200302 x509write_csr_free( &req );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000303 rsa_free( &rsa );
304
305#if defined(_WIN32)
306 printf( " + Press Enter to exit this program.\n" );
307 fflush( stdout ); getchar();
308#endif
309
310 return( ret );
311}
312#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
313 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */