blob: 8fcd63185c5db79d3b28f6f83f74c2e4a7b00534 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +00007 *
Paul Bakkerbdb912d2012-02-13 23:11:30 +00008 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_FS_IO)
Paul Bakker2e24ca72013-09-18 15:21:27 +020037#include "polarssl/error.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/pk.h"
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020039#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <stdio.h>
42#include <string.h>
43#endif
Paul Bakkered27a042013-04-18 22:46:23 +020044
Rich Evans18b78c72015-02-11 14:06:19 +000045#if defined(POLARSSL_PEM_WRITE_C)
46#define USAGE_OUT \
47 " output_file=%%s default: keyfile.pem\n" \
48 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020049#else
Rich Evans18b78c72015-02-11 14:06:19 +000050#define USAGE_OUT \
51 " output_file=%%s default: keyfile.der\n" \
52 " output_format=der default: der\n"
53#endif
54
55#if defined(POLARSSL_PEM_WRITE_C)
56#define DFL_OUTPUT_FILENAME "keyfile.pem"
57#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
58#else
59#define DFL_OUTPUT_FILENAME "keyfile.der"
60#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
61#endif
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
Paul Bakkered27a042013-04-18 22:46:23 +020067
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068#define MODE_NONE 0
69#define MODE_PRIVATE 1
70#define MODE_PUBLIC 2
71
72#define OUTPUT_MODE_NONE 0
73#define OUTPUT_MODE_PRIVATE 1
74#define OUTPUT_MODE_PUBLIC 2
75
Paul Bakkerf3df61a2013-08-26 17:22:23 +020076#define OUTPUT_FORMAT_PEM 0
77#define OUTPUT_FORMAT_DER 1
78
Rich Evans18b78c72015-02-11 14:06:19 +000079#define USAGE \
80 "\n usage: key_app param=<>...\n" \
81 "\n acceptable parameters:\n" \
82 " mode=private|public default: none\n" \
83 " filename=%%s default: keyfile.key\n" \
84 " output_mode=private|public default: none\n" \
85 USAGE_OUT \
86 "\n"
87
88#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO)
89int main( int argc, char *argv[] )
90{
91 ((void) argc);
92 ((void) argv);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093
Rich Evans18b78c72015-02-11 14:06:19 +000094 polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
95 return( 0 );
96}
97#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098/*
99 * global options
100 */
101struct options
102{
103 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200106 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200107 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108} opt;
109
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200110static int write_public_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200112 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000114 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200115 unsigned char *c = output_buf;
116 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117
Paul Bakker1d569582012-10-03 20:35:44 +0000118 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200120#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200123 if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200124 return( ret );
125
126 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200128 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200129#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131 if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200132 return( ret );
133
134 len = ret;
135 c = output_buf + sizeof(output_buf) - len - 1;
136 }
137
138 if( ( f = fopen( output_file, "w" ) ) == NULL )
139 return( -1 );
140
141 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200142 {
143 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200144 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200145 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200146
Paul Bakker0c226102014-04-17 16:02:36 +0200147 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148
149 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150}
151
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200152static int write_private_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200154 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000156 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200157 unsigned char *c = output_buf;
158 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159
Paul Bakker1d569582012-10-03 20:35:44 +0000160 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200161
162#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200165 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200166 return( ret );
167
168 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200170 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200171#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200172 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200173 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200174 return( ret );
175
176 len = ret;
177 c = output_buf + sizeof(output_buf) - len - 1;
178 }
179
180 if( ( f = fopen( output_file, "w" ) ) == NULL )
181 return( -1 );
182
183 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200184 {
185 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200186 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200187 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200188
Paul Bakker0c226102014-04-17 16:02:36 +0200189 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200190
191 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192}
193
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194int main( int argc, char *argv[] )
195{
196 int ret = 0;
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200197 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000199 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 char *p, *q;
201
202 /*
203 * Set to sane values
204 */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200205 pk_init( &key );
206 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207
208 if( argc == 0 )
209 {
210 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200211 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000212 polarssl_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213 goto exit;
214 }
215
216 opt.mode = DFL_MODE;
217 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218 opt.output_mode = DFL_OUTPUT_MODE;
219 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200220 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221
222 for( i = 1; i < argc; i++ )
223 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224 p = argv[i];
225 if( ( q = strchr( p, '=' ) ) == NULL )
226 goto usage;
227 *q++ = '\0';
228
229 if( strcmp( p, "mode" ) == 0 )
230 {
231 if( strcmp( q, "private" ) == 0 )
232 opt.mode = MODE_PRIVATE;
233 else if( strcmp( q, "public" ) == 0 )
234 opt.mode = MODE_PUBLIC;
235 else
236 goto usage;
237 }
238 else if( strcmp( p, "output_mode" ) == 0 )
239 {
240 if( strcmp( q, "private" ) == 0 )
241 opt.output_mode = OUTPUT_MODE_PRIVATE;
242 else if( strcmp( q, "public" ) == 0 )
243 opt.output_mode = OUTPUT_MODE_PUBLIC;
244 else
245 goto usage;
246 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200247 else if( strcmp( p, "output_format" ) == 0 )
248 {
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200249#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200250 if( strcmp( q, "pem" ) == 0 )
251 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200252 else
253#endif
254 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200255 opt.output_format = OUTPUT_FORMAT_DER;
256 else
257 goto usage;
258 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259 else if( strcmp( p, "filename" ) == 0 )
260 opt.filename = q;
261 else if( strcmp( p, "output_file" ) == 0 )
262 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263 else
264 goto usage;
265 }
266
267 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
268 {
Rich Evansf90016a2015-01-19 14:26:37 +0000269 polarssl_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270 goto exit;
271 }
272
273 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
274 {
Rich Evansf90016a2015-01-19 14:26:37 +0000275 polarssl_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000276 goto exit;
277 }
278
279 if( opt.mode == MODE_PRIVATE )
280 {
281 /*
282 * 1.1. Load the key
283 */
Rich Evansf90016a2015-01-19 14:26:37 +0000284 polarssl_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 fflush( stdout );
286
Paul Bakker1a7550a2013-09-15 13:01:22 +0200287 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288
289 if( ret != 0 )
290 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200291 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000292 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293 goto exit;
294 }
295
Rich Evansf90016a2015-01-19 14:26:37 +0000296 polarssl_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297
298 /*
299 * 1.2 Print the key
300 */
Rich Evansf90016a2015-01-19 14:26:37 +0000301 polarssl_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200302
303#if defined(POLARSSL_RSA_C)
304 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
305 {
306 rsa_context *rsa = pk_rsa( key );
307 mpi_write_file( "N: ", &rsa->N, 16, NULL );
308 mpi_write_file( "E: ", &rsa->E, 16, NULL );
309 mpi_write_file( "D: ", &rsa->D, 16, NULL );
310 mpi_write_file( "P: ", &rsa->P, 16, NULL );
311 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
312 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
313 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
314 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
315 }
316 else
317#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200318#if defined(POLARSSL_ECP_C)
319 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
320 {
321 ecp_keypair *ecp = pk_ec( key );
322 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
323 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
324 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
325 mpi_write_file( "D : ", &ecp->d , 16, NULL );
326 }
327 else
328#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000330
331 }
332 else if( opt.mode == MODE_PUBLIC )
333 {
334 /*
335 * 1.1. Load the key
336 */
Rich Evansf90016a2015-01-19 14:26:37 +0000337 polarssl_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338 fflush( stdout );
339
Paul Bakker1a7550a2013-09-15 13:01:22 +0200340 ret = pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000341
342 if( ret != 0 )
343 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200344 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000345 polarssl_printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000346 goto exit;
347 }
348
Rich Evansf90016a2015-01-19 14:26:37 +0000349 polarssl_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000350
351 /*
352 * 1.2 Print the key
353 */
Rich Evansf90016a2015-01-19 14:26:37 +0000354 polarssl_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200355
356#if defined(POLARSSL_RSA_C)
357 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
358 {
359 rsa_context *rsa = pk_rsa( key );
360 mpi_write_file( "N: ", &rsa->N, 16, NULL );
361 mpi_write_file( "E: ", &rsa->E, 16, NULL );
362 }
363 else
364#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200365#if defined(POLARSSL_ECP_C)
366 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
367 {
368 ecp_keypair *ecp = pk_ec( key );
369 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
370 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
371 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
372 }
373 else
374#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000375 polarssl_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000376 }
377 else
378 goto usage;
379
380 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
381 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200382 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000383 }
384 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
385 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200386 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000387 }
388
389exit:
390
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200391 if( ret != 0 && ret != 1)
392 {
393#ifdef POLARSSL_ERROR_C
394 polarssl_strerror( ret, buf, sizeof( buf ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000395 polarssl_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200396#else
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200398#endif
399 }
400
401 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000402
403#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000404 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000405 fflush( stdout ); getchar();
406#endif
407
408 return( ret );
409}
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200410#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */