blob: 29a0da14495e392967613ad6256ab326c6c59e52 [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
Paul Bakker80d44fe2013-09-09 15:59:20 +020043#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
44 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
45int main( int argc, char *argv[] )
46{
47 ((void) argc);
48 ((void) argv);
49
50 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
51 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
52 return( 0 );
53}
54#else
55
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056#define DFL_FILENAME "keyfile.key"
57#define DFL_DEBUG_LEVEL 0
58#define DFL_OUTPUT_FILENAME "cert.req"
59#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020060#define DFL_KEY_USAGE 0
61#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
63/*
64 * global options
65 */
66struct options
67{
68 char *filename; /* filename of the key file */
69 int debug_level; /* level of debugging */
70 char *output_file; /* where to store the constructed key file */
71 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020072 unsigned char key_usage; /* key usage flags */
73 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074} opt;
75
Paul Bakkercd358032013-09-09 12:08:11 +020076int write_certificate_request( x509write_csr *req, char *output_file )
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 );
84 if( ( ret = x509write_csr_pem( req, output_buf, 4096 ) ) < 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 )
93 return( -1 );
94
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020096
97 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098}
99
100#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200101 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102 "\n acceptable parameters:\n" \
103 " filename=%%s default: keyfile.key\n" \
104 " debug_level=%%d default: 0 (disabled)\n" \
105 " output_file=%%s default: cert.req\n" \
106 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200107 " key_usage=%%s default: (empty)\n" \
108 " Comma-separated-list of values:\n" \
109 " digital_signature\n" \
110 " non_repudiation\n" \
111 " key_encipherment\n" \
112 " data_encipherment\n" \
113 " key_agreement\n" \
114 " key_certificate_sign\n" \
115 " crl_sign\n" \
116 " ns_cert_type=%%s default: (empty)\n" \
117 " Comma-separated-list of values:\n" \
118 " ssl_client\n" \
119 " ssl_server\n" \
120 " email\n" \
121 " object_signing\n" \
122 " ssl_ca\n" \
123 " email_ca\n" \
124 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125 "\n"
126
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127int main( int argc, char *argv[] )
128{
129 int ret = 0;
130 rsa_context rsa;
131 char buf[1024];
132 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200133 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200134 x509write_csr req;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135
136 /*
137 * Set to sane values
138 */
Paul Bakker82e29452013-08-25 11:01:31 +0200139 x509write_csr_init( &req );
140 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141 memset( &rsa, 0, sizeof( rsa_context ) );
142 memset( buf, 0, 1024 );
143
144 if( argc == 0 )
145 {
146 usage:
147 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200148 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149 goto exit;
150 }
151
152 opt.filename = DFL_FILENAME;
153 opt.debug_level = DFL_DEBUG_LEVEL;
154 opt.output_file = DFL_OUTPUT_FILENAME;
155 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200156 opt.key_usage = DFL_KEY_USAGE;
157 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158
159 for( i = 1; i < argc; i++ )
160 {
161
162 p = argv[i];
163 if( ( q = strchr( p, '=' ) ) == NULL )
164 goto usage;
165 *q++ = '\0';
166
167 n = strlen( p );
168 for( j = 0; j < n; j++ )
169 {
170 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
171 argv[i][j] |= 0x20;
172 }
173
174 if( strcmp( p, "filename" ) == 0 )
175 opt.filename = q;
176 else if( strcmp( p, "output_file" ) == 0 )
177 opt.output_file = q;
178 else if( strcmp( p, "debug_level" ) == 0 )
179 {
180 opt.debug_level = atoi( q );
181 if( opt.debug_level < 0 || opt.debug_level > 65535 )
182 goto usage;
183 }
184 else if( strcmp( p, "subject_name" ) == 0 )
185 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186 opt.subject_name = q;
187 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200188 else if( strcmp( p, "key_usage" ) == 0 )
189 {
190 while( q != NULL )
191 {
192 if( ( r = strchr( q, ',' ) ) != NULL )
193 *r++ = '\0';
194
195 if( strcmp( q, "digital_signature" ) == 0 )
196 opt.key_usage |= KU_DIGITAL_SIGNATURE;
197 else if( strcmp( q, "non_repudiation" ) == 0 )
198 opt.key_usage |= KU_NON_REPUDIATION;
199 else if( strcmp( q, "key_encipherment" ) == 0 )
200 opt.key_usage |= KU_KEY_ENCIPHERMENT;
201 else if( strcmp( q, "data_encipherment" ) == 0 )
202 opt.key_usage |= KU_DATA_ENCIPHERMENT;
203 else if( strcmp( q, "key_agreement" ) == 0 )
204 opt.key_usage |= KU_KEY_AGREEMENT;
205 else if( strcmp( q, "key_cert_sign" ) == 0 )
206 opt.key_usage |= KU_KEY_CERT_SIGN;
207 else if( strcmp( q, "crl_sign" ) == 0 )
208 opt.key_usage |= KU_CRL_SIGN;
209 else
210 goto usage;
211
212 q = r;
213 }
214 }
215 else if( strcmp( p, "ns_cert_type" ) == 0 )
216 {
217 while( q != NULL )
218 {
219 if( ( r = strchr( q, ',' ) ) != NULL )
220 *r++ = '\0';
221
222 if( strcmp( q, "ssl_client" ) == 0 )
223 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
224 else if( strcmp( q, "ssl_server" ) == 0 )
225 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
226 else if( strcmp( q, "email" ) == 0 )
227 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
228 else if( strcmp( q, "object_signing" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
230 else if( strcmp( q, "ssl_ca" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
232 else if( strcmp( q, "email_ca" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
234 else if( strcmp( q, "object_signing_ca" ) == 0 )
235 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
236 else
237 goto usage;
238
239 q = r;
240 }
241 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 else
243 goto usage;
244 }
245
Paul Bakker57be6e22013-08-26 14:13:14 +0200246 if( opt.key_usage )
247 x509write_csr_set_key_usage( &req, opt.key_usage );
248
249 if( opt.ns_cert_type )
250 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
251
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000252 /*
253 * 1.0. Check the subject name for validity
254 */
Paul Bakker82e29452013-08-25 11:01:31 +0200255 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200257#ifdef POLARSSL_ERROR_C
258 error_strerror( ret, buf, 1024 );
259#endif
Paul Bakker82e29452013-08-25 11:01:31 +0200260 printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200261 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262 }
263
264 /*
265 * 1.1. Load the key
266 */
267 printf( "\n . Loading the private key ..." );
268 fflush( stdout );
269
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200270 ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271
272 if( ret != 0 )
273 {
274#ifdef POLARSSL_ERROR_C
275 error_strerror( ret, buf, 1024 );
276#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200277 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200278 goto exit;
279 }
280
Paul Bakker82e29452013-08-25 11:01:31 +0200281 x509write_csr_set_rsa_key( &req, &rsa );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200282
283 printf( " ok\n" );
284
285 /*
286 * 1.2. Writing the request
287 */
288 printf( " . Writing the certificate request ..." );
289 fflush( stdout );
290
291 if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 )
292 {
293#ifdef POLARSSL_ERROR_C
294 error_strerror( ret, buf, 1024 );
295#endif
296 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297 goto exit;
298 }
299
300 printf( " ok\n" );
301
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000302exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200303 x509write_csr_free( &req );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000304 rsa_free( &rsa );
305
306#if defined(_WIN32)
307 printf( " + Press Enter to exit this program.\n" );
308 fflush( stdout ); getchar();
309#endif
310
311 return( ret );
312}
313#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
314 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */