blob: d9ab45c941bea487082db22faf9591c65def816d [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
Paul Bakkerf3df61a2013-08-26 17:22:23 +02002 * Key writing application
Paul Bakkerbdb912d2012-02-13 23:11:30 +00003 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
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
Paul Bakkered27a042013-04-18 22:46:23 +020042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
43 !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
44int main( int argc, char *argv[] )
45{
46 ((void) argc);
47 ((void) argv);
48
49 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
50 "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n");
51 return( 0 );
52}
53#else
54
Paul Bakkerbdb912d2012-02-13 23:11:30 +000055#define MODE_NONE 0
56#define MODE_PRIVATE 1
57#define MODE_PUBLIC 2
58
59#define OUTPUT_MODE_NONE 0
60#define OUTPUT_MODE_PRIVATE 1
61#define OUTPUT_MODE_PUBLIC 2
62
Paul Bakkerf3df61a2013-08-26 17:22:23 +020063#define OUTPUT_FORMAT_PEM 0
64#define OUTPUT_FORMAT_DER 1
65
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066#define DFL_MODE MODE_NONE
67#define DFL_FILENAME "keyfile.key"
68#define DFL_DEBUG_LEVEL 0
Paul Bakkerf3df61a2013-08-26 17:22:23 +020069#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070#define DFL_OUTPUT_FILENAME "keyfile.pem"
Paul Bakkerf3df61a2013-08-26 17:22:23 +020071#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072
73/*
74 * global options
75 */
76struct options
77{
78 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020082 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083} opt;
84
Paul Bakkerf3df61a2013-08-26 17:22:23 +020085static int write_public_key( rsa_context *rsa, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020087 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000089 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020090 unsigned char *c = output_buf;
91 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092
Paul Bakker1d569582012-10-03 20:35:44 +000093 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094
Paul Bakkerf3df61a2013-08-26 17:22:23 +020095 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096 {
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 if( ( ret = x509write_pubkey_pem( rsa, output_buf, 16000 ) ) != 0 )
98 return( ret );
99
100 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200102 else
103 {
104 if( ( ret = x509write_pubkey_der( rsa, output_buf, 16000 ) ) < 0 )
105 return( ret );
106
107 len = ret;
108 c = output_buf + sizeof(output_buf) - len - 1;
109 }
110
111 if( ( f = fopen( output_file, "w" ) ) == NULL )
112 return( -1 );
113
114 if( fwrite( c, 1, len, f ) != len )
115 return( -1 );
116
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117 fclose(f);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118
119 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120}
121
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122static int write_private_key( rsa_context *rsa, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200124 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000126 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200127 unsigned char *c = output_buf;
128 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129
Paul Bakker1d569582012-10-03 20:35:44 +0000130 memset(output_buf, 0, 16000);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200131 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132 {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200133 if( ( ret = x509write_key_pem( rsa, output_buf, 16000 ) ) != 0 )
134 return( ret );
135
136 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200138 else
139 {
140 if( ( ret = x509write_key_der( rsa, output_buf, 16000 ) ) < 0 )
141 return( ret );
142
143 len = ret;
144 c = output_buf + sizeof(output_buf) - len - 1;
145 }
146
147 if( ( f = fopen( output_file, "w" ) ) == NULL )
148 return( -1 );
149
150 if( fwrite( c, 1, len, f ) != len )
151 return( -1 );
152
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 fclose(f);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200154
155 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156}
157
158#define USAGE \
159 "\n usage: key_app param=<>...\n" \
160 "\n acceptable parameters:\n" \
161 " mode=private|public default: none\n" \
162 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163 " output_mode=private|public default: none\n" \
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164 " output_file=%%s default: keyfile.pem\n" \
165 " output_format=pem|der default: pem\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166 "\n"
167
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168int main( int argc, char *argv[] )
169{
170 int ret = 0;
171 rsa_context rsa;
172 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000173 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174 char *p, *q;
175
176 /*
177 * Set to sane values
178 */
179 memset( &rsa, 0, sizeof( rsa_context ) );
180 memset( buf, 0, 1024 );
181
182 if( argc == 0 )
183 {
184 usage:
185 printf( USAGE );
186 goto exit;
187 }
188
189 opt.mode = DFL_MODE;
190 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191 opt.output_mode = DFL_OUTPUT_MODE;
192 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200193 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194
195 for( i = 1; i < argc; i++ )
196 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197 p = argv[i];
198 if( ( q = strchr( p, '=' ) ) == NULL )
199 goto usage;
200 *q++ = '\0';
201
202 if( strcmp( p, "mode" ) == 0 )
203 {
204 if( strcmp( q, "private" ) == 0 )
205 opt.mode = MODE_PRIVATE;
206 else if( strcmp( q, "public" ) == 0 )
207 opt.mode = MODE_PUBLIC;
208 else
209 goto usage;
210 }
211 else if( strcmp( p, "output_mode" ) == 0 )
212 {
213 if( strcmp( q, "private" ) == 0 )
214 opt.output_mode = OUTPUT_MODE_PRIVATE;
215 else if( strcmp( q, "public" ) == 0 )
216 opt.output_mode = OUTPUT_MODE_PUBLIC;
217 else
218 goto usage;
219 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200220 else if( strcmp( p, "output_format" ) == 0 )
221 {
222 if( strcmp( q, "pem" ) == 0 )
223 opt.output_format = OUTPUT_FORMAT_PEM;
224 else if( strcmp( q, "der" ) == 0 )
225 opt.output_format = OUTPUT_FORMAT_DER;
226 else
227 goto usage;
228 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229 else if( strcmp( p, "filename" ) == 0 )
230 opt.filename = q;
231 else if( strcmp( p, "output_file" ) == 0 )
232 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233 else
234 goto usage;
235 }
236
237 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
238 {
239 printf( "\nCannot output a key without reading one.\n");
240 goto exit;
241 }
242
243 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
244 {
245 printf( "\nCannot output a private key from a public key.\n");
246 goto exit;
247 }
248
249 if( opt.mode == MODE_PRIVATE )
250 {
251 /*
252 * 1.1. Load the key
253 */
254 printf( "\n . Loading the private key ..." );
255 fflush( stdout );
256
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200257 ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258
259 if( ret != 0 )
260 {
261#ifdef POLARSSL_ERROR_C
Paul Bakker03a8a792013-06-30 12:18:08 +0200262 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200264 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265 rsa_free( &rsa );
266 goto exit;
267 }
268
269 printf( " ok\n" );
270
271 /*
272 * 1.2 Print the key
273 */
274 printf( " . Key information ...\n" );
275 mpi_write_file( "N: ", &rsa.N, 16, NULL );
276 mpi_write_file( "E: ", &rsa.E, 16, NULL );
277 mpi_write_file( "D: ", &rsa.D, 16, NULL );
278 mpi_write_file( "P: ", &rsa.P, 16, NULL );
279 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
280 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
281 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
282 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
283
284 }
285 else if( opt.mode == MODE_PUBLIC )
286 {
287 /*
288 * 1.1. Load the key
289 */
290 printf( "\n . Loading the public key ..." );
291 fflush( stdout );
292
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200293 ret = x509parse_public_keyfile_rsa( &rsa, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294
295 if( ret != 0 )
296 {
297#ifdef POLARSSL_ERROR_C
Paul Bakker03a8a792013-06-30 12:18:08 +0200298 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200300 printf( " failed\n ! x509parse_public_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000301 rsa_free( &rsa );
302 goto exit;
303 }
304
305 printf( " ok\n" );
306
307 /*
308 * 1.2 Print the key
309 */
310 printf( " . Key information ...\n" );
311 mpi_write_file( "N: ", &rsa.N, 16, NULL );
312 mpi_write_file( "E: ", &rsa.E, 16, NULL );
313 }
314 else
315 goto usage;
316
317 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
318 {
319 write_public_key( &rsa, opt.output_file );
320 }
321 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
322 {
323 write_private_key( &rsa, opt.output_file );
324 }
325
326exit:
327
328 rsa_free( &rsa );
329
330#if defined(_WIN32)
331 printf( " + Press Enter to exit this program.\n" );
332 fflush( stdout ); getchar();
333#endif
334
335 return( ret );
336}
337#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakkered27a042013-04-18 22:46:23 +0200338 POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */