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