blob: 78333abdd7cec9dd454aff69cf63015ad73831d3 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Certificate request generation
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "polarssl/config.h"
32
33#include "polarssl/error.h"
34#include "polarssl/rsa.h"
35#include "polarssl/x509.h"
36#include "polarssl/base64.h"
37#include "polarssl/x509write.h"
38
39#define DFL_FILENAME "keyfile.key"
40#define DFL_DEBUG_LEVEL 0
41#define DFL_OUTPUT_FILENAME "cert.req"
42#define DFL_SUBJECT_NAME "CN=Cert,O=PolarSSL,C=NL"
43
44/*
45 * global options
46 */
47struct options
48{
49 char *filename; /* filename of the key file */
50 int debug_level; /* level of debugging */
51 char *output_file; /* where to store the constructed key file */
52 char *subject_name; /* subject name for certificate request */
53} opt;
54
55void my_debug( void *ctx, int level, const char *str )
56{
57 if( level < opt.debug_level )
58 {
59 fprintf( (FILE *) ctx, "%s", str );
60 fflush( (FILE *) ctx );
61 }
62}
63
64void write_certificate_request( rsa_context *rsa, x509_req_name *req_name,
65 char *output_file )
66{
67 FILE *f;
68 unsigned char output_buf[4096];
69 unsigned char base_buf[4096];
70 unsigned char *c;
71 int ret;
72 size_t len = 0, olen = 4096;
73
74 memset(output_buf, 0, 4096);
Paul Bakker12f5dbb2012-03-05 13:37:13 +000075 ret = x509_write_cert_req( output_buf, 4096, rsa, req_name, SIG_RSA_SHA1 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076
77 if( ret < 0 )
78 return;
79
80 len = ret;
81 c = output_buf + 4095 - len;
82
83 base64_encode( base_buf, &olen, c, len );
84
85 c = base_buf;
86
87 f = fopen( output_file, "w" );
88 fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n");
89 while (olen)
90 {
91 int use_len = olen;
92 if (use_len > 64) use_len = 64;
93 fwrite( c, 1, use_len, f );
94 olen -= use_len;
95 c += use_len;
96 fprintf(f, "\n");
97 }
98 fprintf(f, "-----END CERTIFICATE REQUEST-----\n");
99 fclose(f);
100}
101
102#define USAGE \
103 "\n usage: key_app param=<>...\n" \
104 "\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" \
109 "\n"
110
111#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
112 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
113int main( int argc, char *argv[] )
114{
115 ((void) argc);
116 ((void) argv);
117
118 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
119 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
120 return( 0 );
121}
122#else
123int main( int argc, char *argv[] )
124{
125 int ret = 0;
126 rsa_context rsa;
127 char buf[1024];
Paul Bakker256a4af2013-11-30 15:13:02 +0100128 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129 char *p, *q;
130 char *s, *c, *end;
131 int in_tag;
132 char *oid = NULL;
133 x509_req_name *req_name = NULL;
134 x509_req_name *cur = req_name;
135
136 /*
137 * Set to sane values
138 */
139 memset( &rsa, 0, sizeof( rsa_context ) );
140 memset( buf, 0, 1024 );
141
142 if( argc == 0 )
143 {
144 usage:
145 printf( USAGE );
146 goto exit;
147 }
148
149 opt.filename = DFL_FILENAME;
150 opt.debug_level = DFL_DEBUG_LEVEL;
151 opt.output_file = DFL_OUTPUT_FILENAME;
152 opt.subject_name = DFL_SUBJECT_NAME;
153
154 for( i = 1; i < argc; i++ )
155 {
156
157 p = argv[i];
158 if( ( q = strchr( p, '=' ) ) == NULL )
159 goto usage;
160 *q++ = '\0';
161
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162 if( strcmp( p, "filename" ) == 0 )
163 opt.filename = q;
164 else if( strcmp( p, "output_file" ) == 0 )
165 opt.output_file = q;
166 else if( strcmp( p, "debug_level" ) == 0 )
167 {
168 opt.debug_level = atoi( q );
169 if( opt.debug_level < 0 || opt.debug_level > 65535 )
170 goto usage;
171 }
172 else if( strcmp( p, "subject_name" ) == 0 )
173 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174 opt.subject_name = q;
175 }
176 else
177 goto usage;
178 }
179
180 /*
181 * 1.0. Check the subject name for validity
182 */
183 s = opt.subject_name;
184 end = s + strlen( s );
185
186 c = s;
187
188 in_tag = 1;
189 while( c <= end )
190 {
191 if( in_tag && *c == '=' )
192 {
193 if( memcmp( s, "CN", 2 ) == 0 && c - s == 2 )
194 oid = OID_CN;
195 else if( memcmp( s, "C", 1 ) == 0 && c - s == 1 )
196 oid = OID_COUNTRY;
197 else if( memcmp( s, "O", 1 ) == 0 && c - s == 1 )
198 oid = OID_ORGANIZATION;
199 else if( memcmp( s, "L", 1 ) == 0 && c - s == 1 )
200 oid = OID_LOCALITY;
201 else if( memcmp( s, "R", 1 ) == 0 && c - s == 1 )
202 oid = OID_PKCS9_EMAIL;
203 else if( memcmp( s, "OU", 2 ) == 0 && c - s == 2 )
204 oid = OID_ORG_UNIT;
205 else if( memcmp( s, "ST", 2 ) == 0 && c - s == 2 )
206 oid = OID_STATE;
207 else
208 {
209 printf("Failed to parse subject name.\n");
210 goto exit;
211 }
212
213 s = c + 1;
214 in_tag = 0;
215 }
216
217 if( !in_tag && ( *c == ',' || c == end ) )
218 {
219 if( c - s > 127 )
220 {
221 printf("Name too large for buffer.\n");
222 goto exit;
223 }
224
225 if( cur == NULL )
226 {
227 req_name = malloc( sizeof(x509_req_name) );
228 cur = req_name;
229 }
230 else
231 {
232 cur->next = malloc( sizeof(x509_req_name) );
233 cur = cur->next;
234 }
235
236 if( cur == NULL )
237 {
238 printf( "Failed to allocate memory.\n" );
239 goto exit;
240 }
241
242 memset( cur, 0, sizeof(x509_req_name) );
243
244 strncpy( cur->oid, oid, strlen( oid ) );
245 strncpy( cur->name, s, c - s );
246
247 s = c + 1;
248 in_tag = 1;
249 }
250 c++;
251 }
252
253 /*
254 * 1.1. Load the key
255 */
256 printf( "\n . Loading the private key ..." );
257 fflush( stdout );
258
259 ret = x509parse_keyfile( &rsa, opt.filename, NULL );
260
261 if( ret != 0 )
262 {
263#ifdef POLARSSL_ERROR_C
264 error_strerror( ret, buf, 1024 );
265#endif
266 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
267 rsa_free( &rsa );
268 goto exit;
269 }
270
271 printf( " ok\n" );
272
273 write_certificate_request( &rsa, req_name, opt.output_file );
274
275exit:
276
277 rsa_free( &rsa );
278
279#if defined(_WIN32)
280 printf( " + Press Enter to exit this program.\n" );
281 fflush( stdout ); getchar();
282#endif
283
284 return( ret );
285}
286#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
287 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */