blob: ec64e2c50b2091d35dc4828c79112ec0a43163a9 [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);
Paul Bakker12f5dbb2012-03-05 13:37:13 +000078 ret = x509_write_cert_req( output_buf, 4096, rsa, req_name, SIG_RSA_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079
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 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 opt.subject_name = q;
185 }
186 else
187 goto usage;
188 }
189
190 /*
191 * 1.0. Check the subject name for validity
192 */
193 s = opt.subject_name;
194 end = s + strlen( s );
195
196 c = s;
197
198 in_tag = 1;
199 while( c <= end )
200 {
201 if( in_tag && *c == '=' )
202 {
203 if( memcmp( s, "CN", 2 ) == 0 && c - s == 2 )
204 oid = OID_CN;
205 else if( memcmp( s, "C", 1 ) == 0 && c - s == 1 )
206 oid = OID_COUNTRY;
207 else if( memcmp( s, "O", 1 ) == 0 && c - s == 1 )
208 oid = OID_ORGANIZATION;
209 else if( memcmp( s, "L", 1 ) == 0 && c - s == 1 )
210 oid = OID_LOCALITY;
211 else if( memcmp( s, "R", 1 ) == 0 && c - s == 1 )
212 oid = OID_PKCS9_EMAIL;
213 else if( memcmp( s, "OU", 2 ) == 0 && c - s == 2 )
214 oid = OID_ORG_UNIT;
215 else if( memcmp( s, "ST", 2 ) == 0 && c - s == 2 )
216 oid = OID_STATE;
217 else
218 {
219 printf("Failed to parse subject name.\n");
220 goto exit;
221 }
222
223 s = c + 1;
224 in_tag = 0;
225 }
226
227 if( !in_tag && ( *c == ',' || c == end ) )
228 {
229 if( c - s > 127 )
230 {
231 printf("Name too large for buffer.\n");
232 goto exit;
233 }
234
235 if( cur == NULL )
236 {
237 req_name = malloc( sizeof(x509_req_name) );
238 cur = req_name;
239 }
240 else
241 {
242 cur->next = malloc( sizeof(x509_req_name) );
243 cur = cur->next;
244 }
245
246 if( cur == NULL )
247 {
248 printf( "Failed to allocate memory.\n" );
249 goto exit;
250 }
251
252 memset( cur, 0, sizeof(x509_req_name) );
253
254 strncpy( cur->oid, oid, strlen( oid ) );
255 strncpy( cur->name, s, c - s );
256
257 s = c + 1;
258 in_tag = 1;
259 }
260 c++;
261 }
262
263 /*
264 * 1.1. Load the key
265 */
266 printf( "\n . Loading the private key ..." );
267 fflush( stdout );
268
269 ret = x509parse_keyfile( &rsa, opt.filename, NULL );
270
271 if( ret != 0 )
272 {
273#ifdef POLARSSL_ERROR_C
274 error_strerror( ret, buf, 1024 );
275#endif
276 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
277 rsa_free( &rsa );
278 goto exit;
279 }
280
281 printf( " ok\n" );
282
283 write_certificate_request( &rsa, req_name, opt.output_file );
284
285exit:
286
287 rsa_free( &rsa );
288
289#if defined(_WIN32)
290 printf( " + Press Enter to exit this program.\n" );
291 fflush( stdout ); getchar();
292#endif
293
294 return( ret );
295}
296#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
297 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */