blob: 88cb9071c2d08f1d3c8bcd4025a0f1e27e3ed6b4 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Key reading application
3 *
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
63#define DFL_MODE MODE_NONE
64#define DFL_FILENAME "keyfile.key"
65#define DFL_DEBUG_LEVEL 0
66#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
67#define DFL_OUTPUT_FILENAME "keyfile.pem"
68
69/*
70 * global options
71 */
72struct options
73{
74 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000076 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020077 const char *output_file; /* where to store the constructed key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078} opt;
79
Paul Bakker3c5ef712013-06-25 16:37:45 +020080static void write_public_key( rsa_context *rsa, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081{
82 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000083 unsigned char output_buf[16000];
84 unsigned char base_buf[16000];
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085 unsigned char *c;
86 int ret;
Paul Bakker1d569582012-10-03 20:35:44 +000087 size_t len = 0, olen = 16000;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker1d569582012-10-03 20:35:44 +000089 memset(output_buf, 0, 16000);
90 ret = x509_write_pubkey_der( output_buf, 16000, rsa );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091
92 if( ret < 0 )
93 return;
94
95 len = ret;
Paul Bakker1d569582012-10-03 20:35:44 +000096 c = output_buf + 15999 - len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097
98 base64_encode( base_buf, &olen, c, len );
99
100 c = base_buf;
101
102 f = fopen( output_file, "w" );
103 fprintf(f, "-----BEGIN PUBLIC KEY-----\n");
104 while (olen)
105 {
106 int use_len = olen;
107 if (use_len > 64) use_len = 64;
108 fwrite( c, 1, use_len, f );
109 olen -= use_len;
110 c += use_len;
111 fprintf(f, "\n");
112 }
113 fprintf(f, "-----END PUBLIC KEY-----\n");
114 fclose(f);
115}
116
Paul Bakker3c5ef712013-06-25 16:37:45 +0200117static void write_private_key( rsa_context *rsa, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118{
119 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000120 unsigned char output_buf[16000];
121 unsigned char base_buf[16000];
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122 unsigned char *c;
123 int ret;
Paul Bakker1d569582012-10-03 20:35:44 +0000124 size_t len = 0, olen = 16000;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125
Paul Bakker1d569582012-10-03 20:35:44 +0000126 memset(output_buf, 0, 16000);
127 ret = x509_write_key_der( output_buf, 16000, rsa );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128 if( ret < 0 )
129 return;
130
131 len = ret;
Paul Bakker1d569582012-10-03 20:35:44 +0000132 c = output_buf + 15999 - len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133
134 base64_encode( base_buf, &olen, c, len );
135
136 c = base_buf;
137
138 f = fopen( output_file, "w" );
139 fprintf(f, "-----BEGIN RSA PRIVATE KEY-----\n");
140 while (olen)
141 {
142 int use_len = olen;
143 if (use_len > 64) use_len = 64;
144 fwrite( c, 1, use_len, f );
145 olen -= use_len;
146 c += use_len;
147 fprintf(f, "\n");
148 }
149 fprintf(f, "-----END RSA PRIVATE KEY-----\n");
150 fclose(f);
151}
152
153#define USAGE \
154 "\n usage: key_app param=<>...\n" \
155 "\n acceptable parameters:\n" \
156 " mode=private|public default: none\n" \
157 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158 " output_mode=private|public default: none\n" \
159 " output_file=%%s defeult: keyfile.pem\n" \
160 "\n"
161
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162int main( int argc, char *argv[] )
163{
164 int ret = 0;
165 rsa_context rsa;
166 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000167 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168 char *p, *q;
169
170 /*
171 * Set to sane values
172 */
173 memset( &rsa, 0, sizeof( rsa_context ) );
174 memset( buf, 0, 1024 );
175
176 if( argc == 0 )
177 {
178 usage:
179 printf( USAGE );
180 goto exit;
181 }
182
183 opt.mode = DFL_MODE;
184 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185 opt.output_mode = DFL_OUTPUT_MODE;
186 opt.output_file = DFL_OUTPUT_FILENAME;
187
188 for( i = 1; i < argc; i++ )
189 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190 p = argv[i];
191 if( ( q = strchr( p, '=' ) ) == NULL )
192 goto usage;
193 *q++ = '\0';
194
195 if( strcmp( p, "mode" ) == 0 )
196 {
197 if( strcmp( q, "private" ) == 0 )
198 opt.mode = MODE_PRIVATE;
199 else if( strcmp( q, "public" ) == 0 )
200 opt.mode = MODE_PUBLIC;
201 else
202 goto usage;
203 }
204 else if( strcmp( p, "output_mode" ) == 0 )
205 {
206 if( strcmp( q, "private" ) == 0 )
207 opt.output_mode = OUTPUT_MODE_PRIVATE;
208 else if( strcmp( q, "public" ) == 0 )
209 opt.output_mode = OUTPUT_MODE_PUBLIC;
210 else
211 goto usage;
212 }
213 else if( strcmp( p, "filename" ) == 0 )
214 opt.filename = q;
215 else if( strcmp( p, "output_file" ) == 0 )
216 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217 else
218 goto usage;
219 }
220
221 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
222 {
223 printf( "\nCannot output a key without reading one.\n");
224 goto exit;
225 }
226
227 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
228 {
229 printf( "\nCannot output a private key from a public key.\n");
230 goto exit;
231 }
232
233 if( opt.mode == MODE_PRIVATE )
234 {
235 /*
236 * 1.1. Load the key
237 */
238 printf( "\n . Loading the private key ..." );
239 fflush( stdout );
240
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200241 ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242
243 if( ret != 0 )
244 {
245#ifdef POLARSSL_ERROR_C
Paul Bakker03a8a792013-06-30 12:18:08 +0200246 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200248 printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 rsa_free( &rsa );
250 goto exit;
251 }
252
253 printf( " ok\n" );
254
255 /*
256 * 1.2 Print the key
257 */
258 printf( " . Key information ...\n" );
259 mpi_write_file( "N: ", &rsa.N, 16, NULL );
260 mpi_write_file( "E: ", &rsa.E, 16, NULL );
261 mpi_write_file( "D: ", &rsa.D, 16, NULL );
262 mpi_write_file( "P: ", &rsa.P, 16, NULL );
263 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
264 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
265 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
266 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
267
268 }
269 else if( opt.mode == MODE_PUBLIC )
270 {
271 /*
272 * 1.1. Load the key
273 */
274 printf( "\n . Loading the public key ..." );
275 fflush( stdout );
276
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200277 ret = x509parse_public_keyfile_rsa( &rsa, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278
279 if( ret != 0 )
280 {
281#ifdef POLARSSL_ERROR_C
Paul Bakker03a8a792013-06-30 12:18:08 +0200282 polarssl_strerror( ret, buf, 1024 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283#endif
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +0200284 printf( " failed\n ! x509parse_public_key_rsa returned %d - %s\n\n", ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 rsa_free( &rsa );
286 goto exit;
287 }
288
289 printf( " ok\n" );
290
291 /*
292 * 1.2 Print the key
293 */
294 printf( " . Key information ...\n" );
295 mpi_write_file( "N: ", &rsa.N, 16, NULL );
296 mpi_write_file( "E: ", &rsa.E, 16, NULL );
297 }
298 else
299 goto usage;
300
301 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
302 {
303 write_public_key( &rsa, opt.output_file );
304 }
305 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
306 {
307 write_private_key( &rsa, opt.output_file );
308 }
309
310exit:
311
312 rsa_free( &rsa );
313
314#if defined(_WIN32)
315 printf( " + Press Enter to exit this program.\n" );
316 fflush( stdout ); getchar();
317#endif
318
319 return( ret );
320}
321#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakkered27a042013-04-18 22:46:23 +0200322 POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */