blob: b30db19b7a1ccfd592020cf879a3bd004e2b1382 [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{
65 FILE *f;
66 unsigned char output_buf[4096];
67 unsigned char base_buf[4096];
68 unsigned char *c;
69 int ret;
70 size_t len = 0, olen = 4096;
71
72 memset(output_buf, 0, 4096);
Paul Bakker82e29452013-08-25 11:01:31 +020073 ret = x509write_csr_der( req, output_buf, 4096 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074
75 if( ret < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020076 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000077
78 len = ret;
79 c = output_buf + 4095 - len;
80
Paul Bakker8eabfc12013-08-25 10:18:25 +020081 if( ( ret = base64_encode( base_buf, &olen, c, len ) ) != 0 )
82 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083
84 c = base_buf;
85
Paul Bakker8eabfc12013-08-25 10:18:25 +020086 if( ( f = fopen( output_file, "w" ) ) == NULL )
87 return( -1 );
88
Paul Bakkerbdb912d2012-02-13 23:11:30 +000089 fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n");
90 while (olen)
91 {
92 int use_len = olen;
93 if (use_len > 64) use_len = 64;
94 fwrite( c, 1, use_len, f );
95 olen -= use_len;
96 c += use_len;
97 fprintf(f, "\n");
98 }
99 fprintf(f, "-----END CERTIFICATE REQUEST-----\n");
100 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200101
102 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103}
104
105#define USAGE \
106 "\n usage: key_app param=<>...\n" \
107 "\n acceptable parameters:\n" \
108 " filename=%%s default: keyfile.key\n" \
109 " debug_level=%%d default: 0 (disabled)\n" \
110 " output_file=%%s default: cert.req\n" \
111 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200112 " key_usage=%%s default: (empty)\n" \
113 " Comma-separated-list of values:\n" \
114 " digital_signature\n" \
115 " non_repudiation\n" \
116 " key_encipherment\n" \
117 " data_encipherment\n" \
118 " key_agreement\n" \
119 " key_certificate_sign\n" \
120 " crl_sign\n" \
121 " ns_cert_type=%%s default: (empty)\n" \
122 " Comma-separated-list of values:\n" \
123 " ssl_client\n" \
124 " ssl_server\n" \
125 " email\n" \
126 " object_signing\n" \
127 " ssl_ca\n" \
128 " email_ca\n" \
129 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130 "\n"
131
132#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
133 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
134int main( int argc, char *argv[] )
135{
136 ((void) argc);
137 ((void) argv);
138
139 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
140 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
141 return( 0 );
142}
143#else
144int main( int argc, char *argv[] )
145{
146 int ret = 0;
147 rsa_context rsa;
148 char buf[1024];
149 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200150 char *p, *q, *r;
Paul Bakker82e29452013-08-25 11:01:31 +0200151 x509_csr req;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152
153 /*
154 * Set to sane values
155 */
Paul Bakker82e29452013-08-25 11:01:31 +0200156 x509write_csr_init( &req );
157 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158 memset( &rsa, 0, sizeof( rsa_context ) );
159 memset( buf, 0, 1024 );
160
161 if( argc == 0 )
162 {
163 usage:
164 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200165 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166 goto exit;
167 }
168
169 opt.filename = DFL_FILENAME;
170 opt.debug_level = DFL_DEBUG_LEVEL;
171 opt.output_file = DFL_OUTPUT_FILENAME;
172 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200173 opt.key_usage = DFL_KEY_USAGE;
174 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175
176 for( i = 1; i < argc; i++ )
177 {
178
179 p = argv[i];
180 if( ( q = strchr( p, '=' ) ) == NULL )
181 goto usage;
182 *q++ = '\0';
183
184 n = strlen( p );
185 for( j = 0; j < n; j++ )
186 {
187 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
188 argv[i][j] |= 0x20;
189 }
190
191 if( strcmp( p, "filename" ) == 0 )
192 opt.filename = q;
193 else if( strcmp( p, "output_file" ) == 0 )
194 opt.output_file = q;
195 else if( strcmp( p, "debug_level" ) == 0 )
196 {
197 opt.debug_level = atoi( q );
198 if( opt.debug_level < 0 || opt.debug_level > 65535 )
199 goto usage;
200 }
201 else if( strcmp( p, "subject_name" ) == 0 )
202 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203 opt.subject_name = q;
204 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200205 else if( strcmp( p, "key_usage" ) == 0 )
206 {
207 while( q != NULL )
208 {
209 if( ( r = strchr( q, ',' ) ) != NULL )
210 *r++ = '\0';
211
212 if( strcmp( q, "digital_signature" ) == 0 )
213 opt.key_usage |= KU_DIGITAL_SIGNATURE;
214 else if( strcmp( q, "non_repudiation" ) == 0 )
215 opt.key_usage |= KU_NON_REPUDIATION;
216 else if( strcmp( q, "key_encipherment" ) == 0 )
217 opt.key_usage |= KU_KEY_ENCIPHERMENT;
218 else if( strcmp( q, "data_encipherment" ) == 0 )
219 opt.key_usage |= KU_DATA_ENCIPHERMENT;
220 else if( strcmp( q, "key_agreement" ) == 0 )
221 opt.key_usage |= KU_KEY_AGREEMENT;
222 else if( strcmp( q, "key_cert_sign" ) == 0 )
223 opt.key_usage |= KU_KEY_CERT_SIGN;
224 else if( strcmp( q, "crl_sign" ) == 0 )
225 opt.key_usage |= KU_CRL_SIGN;
226 else
227 goto usage;
228
229 q = r;
230 }
231 }
232 else if( strcmp( p, "ns_cert_type" ) == 0 )
233 {
234 while( q != NULL )
235 {
236 if( ( r = strchr( q, ',' ) ) != NULL )
237 *r++ = '\0';
238
239 if( strcmp( q, "ssl_client" ) == 0 )
240 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
241 else if( strcmp( q, "ssl_server" ) == 0 )
242 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
243 else if( strcmp( q, "email" ) == 0 )
244 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
245 else if( strcmp( q, "object_signing" ) == 0 )
246 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
247 else if( strcmp( q, "ssl_ca" ) == 0 )
248 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
249 else if( strcmp( q, "email_ca" ) == 0 )
250 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
251 else if( strcmp( q, "object_signing_ca" ) == 0 )
252 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
253 else
254 goto usage;
255
256 q = r;
257 }
258 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259 else
260 goto usage;
261 }
262
Paul Bakker57be6e22013-08-26 14:13:14 +0200263 if( opt.key_usage )
264 x509write_csr_set_key_usage( &req, opt.key_usage );
265
266 if( opt.ns_cert_type )
267 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
268
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269 /*
270 * 1.0. Check the subject name for validity
271 */
Paul Bakker82e29452013-08-25 11:01:31 +0200272 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200274#ifdef POLARSSL_ERROR_C
275 error_strerror( ret, buf, 1024 );
276#endif
Paul Bakker82e29452013-08-25 11:01:31 +0200277 printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200278 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000279 }
280
281 /*
282 * 1.1. Load the key
283 */
284 printf( "\n . Loading the private key ..." );
285 fflush( stdout );
286
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200287 ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288
289 if( ret != 0 )
290 {
291#ifdef POLARSSL_ERROR_C
292 error_strerror( ret, buf, 1024 );
293#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200294 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200295 goto exit;
296 }
297
Paul Bakker82e29452013-08-25 11:01:31 +0200298 x509write_csr_set_rsa_key( &req, &rsa );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200299
300 printf( " ok\n" );
301
302 /*
303 * 1.2. Writing the request
304 */
305 printf( " . Writing the certificate request ..." );
306 fflush( stdout );
307
308 if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 )
309 {
310#ifdef POLARSSL_ERROR_C
311 error_strerror( ret, buf, 1024 );
312#endif
313 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000314 goto exit;
315 }
316
317 printf( " ok\n" );
318
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000319exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200320 x509write_csr_free( &req );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000321 rsa_free( &rsa );
322
323#if defined(_WIN32)
324 printf( " + Press Enter to exit this program.\n" );
325 fflush( stdout ); getchar();
326#endif
327
328 return( ret );
329}
330#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
331 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */