blob: 4bcfbbeee6bd7b0fae53450251cf731bb259c725 [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"
41
42#define DFL_FILENAME "keyfile.key"
43#define DFL_DEBUG_LEVEL 0
44#define DFL_OUTPUT_FILENAME "cert.req"
45#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
46
47/*
48 * global options
49 */
50struct options
51{
52 char *filename; /* filename of the key file */
53 int debug_level; /* level of debugging */
54 char *output_file; /* where to store the constructed key file */
55 char *subject_name; /* subject name for certificate request */
56} opt;
57
58void my_debug( void *ctx, int level, const char *str )
59{
60 if( level < opt.debug_level )
61 {
62 fprintf( (FILE *) ctx, "%s", str );
63 fflush( (FILE *) ctx );
64 }
65}
66
67void write_certificate_request( rsa_context *rsa, x509_req_name *req_name,
68 char *output_file )
69{
70 FILE *f;
71 unsigned char output_buf[4096];
72 unsigned char base_buf[4096];
73 unsigned char *c;
74 int ret;
75 size_t len = 0, olen = 4096;
76
77 memset(output_buf, 0, 4096);
78 ret = x509_write_cert_req( output_buf, 4096, rsa, req_name );
79
80 if( ret < 0 )
81 return;
82
83 len = ret;
84 c = output_buf + 4095 - len;
85
86 base64_encode( base_buf, &olen, c, len );
87
88 c = base_buf;
89
90 f = fopen( output_file, "w" );
91 fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n");
92 while (olen)
93 {
94 int use_len = olen;
95 if (use_len > 64) use_len = 64;
96 fwrite( c, 1, use_len, f );
97 olen -= use_len;
98 c += use_len;
99 fprintf(f, "\n");
100 }
101 fprintf(f, "-----END CERTIFICATE REQUEST-----\n");
102 fclose(f);
103}
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" \
112 "\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;
132 char *p, *q;
133 char *s, *c, *end;
134 int in_tag;
135 char *oid = NULL;
136 x509_req_name *req_name = NULL;
137 x509_req_name *cur = req_name;
138
139 /*
140 * Set to sane values
141 */
142 memset( &rsa, 0, sizeof( rsa_context ) );
143 memset( buf, 0, 1024 );
144
145 if( argc == 0 )
146 {
147 usage:
148 printf( USAGE );
149 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;
156
157 for( i = 1; i < argc; i++ )
158 {
159
160 p = argv[i];
161 if( ( q = strchr( p, '=' ) ) == NULL )
162 goto usage;
163 *q++ = '\0';
164
165 n = strlen( p );
166 for( j = 0; j < n; j++ )
167 {
168 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
169 argv[i][j] |= 0x20;
170 }
171
172 if( strcmp( p, "filename" ) == 0 )
173 opt.filename = q;
174 else if( strcmp( p, "output_file" ) == 0 )
175 opt.output_file = q;
176 else if( strcmp( p, "debug_level" ) == 0 )
177 {
178 opt.debug_level = atoi( q );
179 if( opt.debug_level < 0 || opt.debug_level > 65535 )
180 goto usage;
181 }
182 else if( strcmp( p, "subject_name" ) == 0 )
183 {
184 printf("p: '%s', q: '%s'\n", p, q);
185 opt.subject_name = q;
186 }
187 else
188 goto usage;
189 }
190
191 /*
192 * 1.0. Check the subject name for validity
193 */
194 s = opt.subject_name;
195 end = s + strlen( s );
196
197 c = s;
198
199 in_tag = 1;
200 while( c <= end )
201 {
202 if( in_tag && *c == '=' )
203 {
204 if( memcmp( s, "CN", 2 ) == 0 && c - s == 2 )
205 oid = OID_CN;
206 else if( memcmp( s, "C", 1 ) == 0 && c - s == 1 )
207 oid = OID_COUNTRY;
208 else if( memcmp( s, "O", 1 ) == 0 && c - s == 1 )
209 oid = OID_ORGANIZATION;
210 else if( memcmp( s, "L", 1 ) == 0 && c - s == 1 )
211 oid = OID_LOCALITY;
212 else if( memcmp( s, "R", 1 ) == 0 && c - s == 1 )
213 oid = OID_PKCS9_EMAIL;
214 else if( memcmp( s, "OU", 2 ) == 0 && c - s == 2 )
215 oid = OID_ORG_UNIT;
216 else if( memcmp( s, "ST", 2 ) == 0 && c - s == 2 )
217 oid = OID_STATE;
218 else
219 {
220 printf("Failed to parse subject name.\n");
221 goto exit;
222 }
223
224 s = c + 1;
225 in_tag = 0;
226 }
227
228 if( !in_tag && ( *c == ',' || c == end ) )
229 {
230 if( c - s > 127 )
231 {
232 printf("Name too large for buffer.\n");
233 goto exit;
234 }
235
236 if( cur == NULL )
237 {
238 req_name = malloc( sizeof(x509_req_name) );
239 cur = req_name;
240 }
241 else
242 {
243 cur->next = malloc( sizeof(x509_req_name) );
244 cur = cur->next;
245 }
246
247 if( cur == NULL )
248 {
249 printf( "Failed to allocate memory.\n" );
250 goto exit;
251 }
252
253 memset( cur, 0, sizeof(x509_req_name) );
254
255 strncpy( cur->oid, oid, strlen( oid ) );
256 strncpy( cur->name, s, c - s );
257
258 s = c + 1;
259 in_tag = 1;
260 }
261 c++;
262 }
263
264 /*
265 * 1.1. Load the key
266 */
267 printf( "\n . Loading the private key ..." );
268 fflush( stdout );
269
270 ret = x509parse_keyfile( &rsa, opt.filename, NULL );
271
272 if( ret != 0 )
273 {
274#ifdef POLARSSL_ERROR_C
275 error_strerror( ret, buf, 1024 );
276#endif
277 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
278 rsa_free( &rsa );
279 goto exit;
280 }
281
282 printf( " ok\n" );
283
284 write_certificate_request( &rsa, req_name, opt.output_file );
285
286exit:
287
288 rsa_free( &rsa );
289
290#if defined(_WIN32)
291 printf( " + Press Enter to exit this program.\n" );
292 fflush( stdout ); getchar();
293#endif
294
295 return( ret );
296}
297#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
298 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */