blob: 6cc05c825fd954560a3bd0ca76cd6a66b041e2ed [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) || \
Paul Bakker4122f3e2013-09-09 16:01:46 +020044 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) || \
45 !defined(POLARSSL_ERROR_C)
Paul Bakker80d44fe2013-09-09 15:59:20 +020046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
51 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker4122f3e2013-09-09 16:01:46 +020052 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO and/or "
53 "POLARSSL_ERROR_C not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020054 return( 0 );
55}
56#else
57
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058#define DFL_FILENAME "keyfile.key"
59#define DFL_DEBUG_LEVEL 0
60#define DFL_OUTPUT_FILENAME "cert.req"
61#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
Paul Bakker57be6e22013-08-26 14:13:14 +020062#define DFL_KEY_USAGE 0
63#define DFL_NS_CERT_TYPE 0
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064
65/*
66 * global options
67 */
68struct options
69{
70 char *filename; /* filename of the key file */
71 int debug_level; /* level of debugging */
72 char *output_file; /* where to store the constructed key file */
73 char *subject_name; /* subject name for certificate request */
Paul Bakker57be6e22013-08-26 14:13:14 +020074 unsigned char key_usage; /* key usage flags */
75 unsigned char ns_cert_type; /* NS cert type */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076} opt;
77
Paul Bakkercd358032013-09-09 12:08:11 +020078int write_certificate_request( x509write_csr *req, char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079{
Paul Bakker135f1e92013-08-26 16:54:13 +020080 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081 FILE *f;
82 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +020083 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084
Paul Bakker135f1e92013-08-26 16:54:13 +020085 memset( output_buf, 0, 4096 );
86 if( ( ret = x509write_csr_pem( req, output_buf, 4096 ) ) < 0 )
Paul Bakker8eabfc12013-08-25 10:18:25 +020087 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker135f1e92013-08-26 16:54:13 +020089 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090
Paul Bakker8eabfc12013-08-25 10:18:25 +020091 if( ( f = fopen( output_file, "w" ) ) == NULL )
92 return( -1 );
93
Paul Bakker135f1e92013-08-26 16:54:13 +020094 if( fwrite( output_buf, 1, len, f ) != len )
95 return( -1 );
96
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +020098
99 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100}
101
102#define USAGE \
Paul Bakker86932742013-09-09 14:09:42 +0200103 "\n usage: cert_req param=<>...\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104 "\n acceptable parameters:\n" \
105 " filename=%%s default: keyfile.key\n" \
106 " debug_level=%%d default: 0 (disabled)\n" \
107 " output_file=%%s default: cert.req\n" \
108 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
Paul Bakker57be6e22013-08-26 14:13:14 +0200109 " key_usage=%%s default: (empty)\n" \
110 " Comma-separated-list of values:\n" \
111 " digital_signature\n" \
112 " non_repudiation\n" \
113 " key_encipherment\n" \
114 " data_encipherment\n" \
115 " key_agreement\n" \
116 " key_certificate_sign\n" \
117 " crl_sign\n" \
118 " ns_cert_type=%%s default: (empty)\n" \
119 " Comma-separated-list of values:\n" \
120 " ssl_client\n" \
121 " ssl_server\n" \
122 " email\n" \
123 " object_signing\n" \
124 " ssl_ca\n" \
125 " email_ca\n" \
126 " object_signing_ca\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127 "\n"
128
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129int main( int argc, char *argv[] )
130{
131 int ret = 0;
132 rsa_context rsa;
133 char buf[1024];
134 int i, j, n;
Paul Bakker57be6e22013-08-26 14:13:14 +0200135 char *p, *q, *r;
Paul Bakkercd358032013-09-09 12:08:11 +0200136 x509write_csr req;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137
138 /*
139 * Set to sane values
140 */
Paul Bakker82e29452013-08-25 11:01:31 +0200141 x509write_csr_init( &req );
142 x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143 memset( &rsa, 0, sizeof( rsa_context ) );
144 memset( buf, 0, 1024 );
145
146 if( argc == 0 )
147 {
148 usage:
149 printf( USAGE );
Paul Bakker57be6e22013-08-26 14:13:14 +0200150 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 goto exit;
152 }
153
154 opt.filename = DFL_FILENAME;
155 opt.debug_level = DFL_DEBUG_LEVEL;
156 opt.output_file = DFL_OUTPUT_FILENAME;
157 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200158 opt.key_usage = DFL_KEY_USAGE;
159 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160
161 for( i = 1; i < argc; i++ )
162 {
163
164 p = argv[i];
165 if( ( q = strchr( p, '=' ) ) == NULL )
166 goto usage;
167 *q++ = '\0';
168
169 n = strlen( p );
170 for( j = 0; j < n; j++ )
171 {
172 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
173 argv[i][j] |= 0x20;
174 }
175
176 if( strcmp( p, "filename" ) == 0 )
177 opt.filename = q;
178 else if( strcmp( p, "output_file" ) == 0 )
179 opt.output_file = q;
180 else if( strcmp( p, "debug_level" ) == 0 )
181 {
182 opt.debug_level = atoi( q );
183 if( opt.debug_level < 0 || opt.debug_level > 65535 )
184 goto usage;
185 }
186 else if( strcmp( p, "subject_name" ) == 0 )
187 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 opt.subject_name = q;
189 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200190 else if( strcmp( p, "key_usage" ) == 0 )
191 {
192 while( q != NULL )
193 {
194 if( ( r = strchr( q, ',' ) ) != NULL )
195 *r++ = '\0';
196
197 if( strcmp( q, "digital_signature" ) == 0 )
198 opt.key_usage |= KU_DIGITAL_SIGNATURE;
199 else if( strcmp( q, "non_repudiation" ) == 0 )
200 opt.key_usage |= KU_NON_REPUDIATION;
201 else if( strcmp( q, "key_encipherment" ) == 0 )
202 opt.key_usage |= KU_KEY_ENCIPHERMENT;
203 else if( strcmp( q, "data_encipherment" ) == 0 )
204 opt.key_usage |= KU_DATA_ENCIPHERMENT;
205 else if( strcmp( q, "key_agreement" ) == 0 )
206 opt.key_usage |= KU_KEY_AGREEMENT;
207 else if( strcmp( q, "key_cert_sign" ) == 0 )
208 opt.key_usage |= KU_KEY_CERT_SIGN;
209 else if( strcmp( q, "crl_sign" ) == 0 )
210 opt.key_usage |= KU_CRL_SIGN;
211 else
212 goto usage;
213
214 q = r;
215 }
216 }
217 else if( strcmp( p, "ns_cert_type" ) == 0 )
218 {
219 while( q != NULL )
220 {
221 if( ( r = strchr( q, ',' ) ) != NULL )
222 *r++ = '\0';
223
224 if( strcmp( q, "ssl_client" ) == 0 )
225 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CLIENT;
226 else if( strcmp( q, "ssl_server" ) == 0 )
227 opt.ns_cert_type |= NS_CERT_TYPE_SSL_SERVER;
228 else if( strcmp( q, "email" ) == 0 )
229 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL;
230 else if( strcmp( q, "object_signing" ) == 0 )
231 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING;
232 else if( strcmp( q, "ssl_ca" ) == 0 )
233 opt.ns_cert_type |= NS_CERT_TYPE_SSL_CA;
234 else if( strcmp( q, "email_ca" ) == 0 )
235 opt.ns_cert_type |= NS_CERT_TYPE_EMAIL_CA;
236 else if( strcmp( q, "object_signing_ca" ) == 0 )
237 opt.ns_cert_type |= NS_CERT_TYPE_OBJECT_SIGNING_CA;
238 else
239 goto usage;
240
241 q = r;
242 }
243 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244 else
245 goto usage;
246 }
247
Paul Bakker57be6e22013-08-26 14:13:14 +0200248 if( opt.key_usage )
249 x509write_csr_set_key_usage( &req, opt.key_usage );
250
251 if( opt.ns_cert_type )
252 x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
253
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254 /*
255 * 1.0. Check the subject name for validity
256 */
Paul Bakker82e29452013-08-25 11:01:31 +0200257 if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200259 error_strerror( ret, buf, 1024 );
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 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 error_strerror( ret, buf, 1024 );
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200275 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200276 goto exit;
277 }
278
Paul Bakker82e29452013-08-25 11:01:31 +0200279 x509write_csr_set_rsa_key( &req, &rsa );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200280
281 printf( " ok\n" );
282
283 /*
284 * 1.2. Writing the request
285 */
286 printf( " . Writing the certificate request ..." );
287 fflush( stdout );
288
289 if( ( ret = write_certificate_request( &req, opt.output_file ) ) != 0 )
290 {
Paul Bakker8eabfc12013-08-25 10:18:25 +0200291 error_strerror( ret, buf, 1024 );
Paul Bakker8eabfc12013-08-25 10:18:25 +0200292 printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293 goto exit;
294 }
295
296 printf( " ok\n" );
297
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000298exit:
Paul Bakker82e29452013-08-25 11:01:31 +0200299 x509write_csr_free( &req );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300 rsa_free( &rsa );
301
302#if defined(_WIN32)
303 printf( " + Press Enter to exit this program.\n" );
304 fflush( stdout ); getchar();
305#endif
306
307 return( ret );
308}
309#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
310 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */