blob: cbd62521d100ec38e309d73fc2563af14207d046 [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
43#define DFL_FILENAME "keyfile.key"
44#define DFL_DEBUG_LEVEL 0
45#define DFL_OUTPUT_FILENAME "cert.req"
46#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
47
48/*
49 * global options
50 */
51struct options
52{
53 char *filename; /* filename of the key file */
54 int debug_level; /* level of debugging */
55 char *output_file; /* where to store the constructed key file */
56 char *subject_name; /* subject name for certificate request */
57} opt;
58
59void my_debug( void *ctx, int level, const char *str )
60{
61 if( level < opt.debug_level )
62 {
63 fprintf( (FILE *) ctx, "%s", str );
64 fflush( (FILE *) ctx );
65 }
66}
67
68void write_certificate_request( rsa_context *rsa, x509_req_name *req_name,
69 char *output_file )
70{
71 FILE *f;
72 unsigned char output_buf[4096];
73 unsigned char base_buf[4096];
74 unsigned char *c;
75 int ret;
76 size_t len = 0, olen = 4096;
77
78 memset(output_buf, 0, 4096);
Paul Bakkerc70b9822013-04-07 22:00:46 +020079 ret = x509_write_cert_req( output_buf, 4096, rsa, req_name, POLARSSL_MD_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080
81 if( ret < 0 )
82 return;
83
84 len = ret;
85 c = output_buf + 4095 - len;
86
87 base64_encode( base_buf, &olen, c, len );
88
89 c = base_buf;
90
91 f = fopen( output_file, "w" );
92 fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n");
93 while (olen)
94 {
95 int use_len = olen;
96 if (use_len > 64) use_len = 64;
97 fwrite( c, 1, use_len, f );
98 olen -= use_len;
99 c += use_len;
100 fprintf(f, "\n");
101 }
102 fprintf(f, "-----END CERTIFICATE REQUEST-----\n");
103 fclose(f);
104}
105
106#define USAGE \
107 "\n usage: key_app param=<>...\n" \
108 "\n acceptable parameters:\n" \
109 " filename=%%s default: keyfile.key\n" \
110 " debug_level=%%d default: 0 (disabled)\n" \
111 " output_file=%%s default: cert.req\n" \
112 " subject_name=%%s default: CN=Cert,O=PolarSSL,C=NL\n" \
113 "\n"
114
115#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
116 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
117int main( int argc, char *argv[] )
118{
119 ((void) argc);
120 ((void) argv);
121
122 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
123 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
124 return( 0 );
125}
126#else
127int main( int argc, char *argv[] )
128{
129 int ret = 0;
130 rsa_context rsa;
131 char buf[1024];
132 int i, j, n;
133 char *p, *q;
134 char *s, *c, *end;
135 int in_tag;
136 char *oid = NULL;
137 x509_req_name *req_name = NULL;
138 x509_req_name *cur = req_name;
139
140 /*
141 * Set to sane values
142 */
143 memset( &rsa, 0, sizeof( rsa_context ) );
144 memset( buf, 0, 1024 );
145
146 if( argc == 0 )
147 {
148 usage:
149 printf( USAGE );
150 goto exit;
151 }
152
153 opt.filename = DFL_FILENAME;
154 opt.debug_level = DFL_DEBUG_LEVEL;
155 opt.output_file = DFL_OUTPUT_FILENAME;
156 opt.subject_name = DFL_SUBJECT_NAME;
157
158 for( i = 1; i < argc; i++ )
159 {
160
161 p = argv[i];
162 if( ( q = strchr( p, '=' ) ) == NULL )
163 goto usage;
164 *q++ = '\0';
165
166 n = strlen( p );
167 for( j = 0; j < n; j++ )
168 {
169 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
170 argv[i][j] |= 0x20;
171 }
172
173 if( strcmp( p, "filename" ) == 0 )
174 opt.filename = q;
175 else if( strcmp( p, "output_file" ) == 0 )
176 opt.output_file = q;
177 else if( strcmp( p, "debug_level" ) == 0 )
178 {
179 opt.debug_level = atoi( q );
180 if( opt.debug_level < 0 || opt.debug_level > 65535 )
181 goto usage;
182 }
183 else if( strcmp( p, "subject_name" ) == 0 )
184 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185 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 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200205 oid = OID_AT_CN;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206 else if( memcmp( s, "C", 1 ) == 0 && c - s == 1 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200207 oid = OID_AT_COUNTRY;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208 else if( memcmp( s, "O", 1 ) == 0 && c - s == 1 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200209 oid = OID_AT_ORGANIZATION;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210 else if( memcmp( s, "L", 1 ) == 0 && c - s == 1 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200211 oid = OID_AT_LOCALITY;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212 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 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200215 oid = OID_AT_ORG_UNIT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 else if( memcmp( s, "ST", 2 ) == 0 && c - s == 2 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200217 oid = OID_AT_STATE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218 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
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 Bakkerbdb912d2012-02-13 23:11:30 +0000278 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 */