blob: 18e644267a0b03eeb5af186e12e4f60293b13da1 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker2e24ca72013-09-18 15:21:27 +020033#include "polarssl/error.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/pk.h"
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020035#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000036
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020038int main( int argc, char *argv[] )
39{
40 ((void) argc);
41 ((void) argv);
42
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
Paul Bakkered27a042013-04-18 22:46:23 +020044 return( 0 );
45}
46#else
47
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048#define MODE_NONE 0
49#define MODE_PRIVATE 1
50#define MODE_PUBLIC 2
51
52#define OUTPUT_MODE_NONE 0
53#define OUTPUT_MODE_PRIVATE 1
54#define OUTPUT_MODE_PUBLIC 2
55
Paul Bakkerf3df61a2013-08-26 17:22:23 +020056#define OUTPUT_FORMAT_PEM 0
57#define OUTPUT_FORMAT_DER 1
58
Paul Bakkerbdb912d2012-02-13 23:11:30 +000059#define DFL_MODE MODE_NONE
60#define DFL_FILENAME "keyfile.key"
61#define DFL_DEBUG_LEVEL 0
Paul Bakkerf3df61a2013-08-26 17:22:23 +020062#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020063#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064#define DFL_OUTPUT_FILENAME "keyfile.pem"
Paul Bakkerf3df61a2013-08-26 17:22:23 +020065#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020066#else
67#define DFL_OUTPUT_FILENAME "keyfile.der"
68#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
69#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070
71/*
72 * global options
73 */
74struct options
75{
76 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020077 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020080 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081} opt;
82
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020083static int write_public_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020085 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000087 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020088 unsigned char *c = output_buf;
89 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090
Paul Bakker1d569582012-10-03 20:35:44 +000091 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020093#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +020094 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020096 if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 return( ret );
98
99 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200101 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200102#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200103 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200104 if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200105 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 )
Paul Bakker0c226102014-04-17 16:02:36 +0200115 {
116 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200117 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200118 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200119
Paul Bakker0c226102014-04-17 16:02:36 +0200120 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121
122 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123}
124
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200125static int write_private_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200127 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000129 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 unsigned char *c = output_buf;
131 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132
Paul Bakker1d569582012-10-03 20:35:44 +0000133 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200134
135#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200136 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200138 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 return( ret );
140
141 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200143 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200144#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200145 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147 return( ret );
148
149 len = ret;
150 c = output_buf + sizeof(output_buf) - len - 1;
151 }
152
153 if( ( f = fopen( output_file, "w" ) ) == NULL )
154 return( -1 );
155
156 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200157 {
158 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200160 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200161
Paul Bakker0c226102014-04-17 16:02:36 +0200162 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163
164 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165}
166
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200167#if defined(POLARSSL_PEM_WRITE_C)
168#define USAGE_OUT \
169 " output_file=%%s default: keyfile.pem\n" \
170 " output_format=pem|der default: pem\n"
171#else
172#define USAGE_OUT \
173 " output_file=%%s default: keyfile.der\n" \
174 " output_format=der default: der\n"
175#endif
176
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177#define USAGE \
178 "\n usage: key_app param=<>...\n" \
179 "\n acceptable parameters:\n" \
180 " mode=private|public default: none\n" \
181 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 " output_mode=private|public default: none\n" \
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200183 USAGE_OUT \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 "\n"
185
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186int main( int argc, char *argv[] )
187{
188 int ret = 0;
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200189 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000191 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192 char *p, *q;
193
194 /*
195 * Set to sane values
196 */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200197 pk_init( &key );
198 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199
200 if( argc == 0 )
201 {
202 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200203 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204 printf( USAGE );
205 goto exit;
206 }
207
208 opt.mode = DFL_MODE;
209 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210 opt.output_mode = DFL_OUTPUT_MODE;
211 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200212 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213
214 for( i = 1; i < argc; i++ )
215 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 p = argv[i];
217 if( ( q = strchr( p, '=' ) ) == NULL )
218 goto usage;
219 *q++ = '\0';
220
221 if( strcmp( p, "mode" ) == 0 )
222 {
223 if( strcmp( q, "private" ) == 0 )
224 opt.mode = MODE_PRIVATE;
225 else if( strcmp( q, "public" ) == 0 )
226 opt.mode = MODE_PUBLIC;
227 else
228 goto usage;
229 }
230 else if( strcmp( p, "output_mode" ) == 0 )
231 {
232 if( strcmp( q, "private" ) == 0 )
233 opt.output_mode = OUTPUT_MODE_PRIVATE;
234 else if( strcmp( q, "public" ) == 0 )
235 opt.output_mode = OUTPUT_MODE_PUBLIC;
236 else
237 goto usage;
238 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200239 else if( strcmp( p, "output_format" ) == 0 )
240 {
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200241#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200242 if( strcmp( q, "pem" ) == 0 )
243 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200244 else
245#endif
246 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200247 opt.output_format = OUTPUT_FORMAT_DER;
248 else
249 goto usage;
250 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000251 else if( strcmp( p, "filename" ) == 0 )
252 opt.filename = q;
253 else if( strcmp( p, "output_file" ) == 0 )
254 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255 else
256 goto usage;
257 }
258
259 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
260 {
261 printf( "\nCannot output a key without reading one.\n");
262 goto exit;
263 }
264
265 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
266 {
267 printf( "\nCannot output a private key from a public key.\n");
268 goto exit;
269 }
270
271 if( opt.mode == MODE_PRIVATE )
272 {
273 /*
274 * 1.1. Load the key
275 */
276 printf( "\n . Loading the private key ..." );
277 fflush( stdout );
278
Paul Bakker1a7550a2013-09-15 13:01:22 +0200279 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000280
281 if( ret != 0 )
282 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200283 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
284 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 goto exit;
286 }
287
288 printf( " ok\n" );
289
290 /*
291 * 1.2 Print the key
292 */
293 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200294
295#if defined(POLARSSL_RSA_C)
296 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
297 {
298 rsa_context *rsa = pk_rsa( key );
299 mpi_write_file( "N: ", &rsa->N, 16, NULL );
300 mpi_write_file( "E: ", &rsa->E, 16, NULL );
301 mpi_write_file( "D: ", &rsa->D, 16, NULL );
302 mpi_write_file( "P: ", &rsa->P, 16, NULL );
303 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
304 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
305 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
306 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
307 }
308 else
309#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200310#if defined(POLARSSL_ECP_C)
311 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
312 {
313 ecp_keypair *ecp = pk_ec( key );
314 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
315 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
316 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
317 mpi_write_file( "D : ", &ecp->d , 16, NULL );
318 }
319 else
320#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200321 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000322
323 }
324 else if( opt.mode == MODE_PUBLIC )
325 {
326 /*
327 * 1.1. Load the key
328 */
329 printf( "\n . Loading the public key ..." );
330 fflush( stdout );
331
Paul Bakker1a7550a2013-09-15 13:01:22 +0200332 ret = pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333
334 if( ret != 0 )
335 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200336 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
337 printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338 goto exit;
339 }
340
341 printf( " ok\n" );
342
343 /*
344 * 1.2 Print the key
345 */
346 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200347
348#if defined(POLARSSL_RSA_C)
349 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
350 {
351 rsa_context *rsa = pk_rsa( key );
352 mpi_write_file( "N: ", &rsa->N, 16, NULL );
353 mpi_write_file( "E: ", &rsa->E, 16, NULL );
354 }
355 else
356#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200357#if defined(POLARSSL_ECP_C)
358 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
359 {
360 ecp_keypair *ecp = pk_ec( key );
361 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
362 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
363 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
364 }
365 else
366#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200367 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000368 }
369 else
370 goto usage;
371
372 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
373 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200374 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000375 }
376 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
377 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200378 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000379 }
380
381exit:
382
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200383 if( ret != 0 && ret != 1)
384 {
385#ifdef POLARSSL_ERROR_C
386 polarssl_strerror( ret, buf, sizeof( buf ) );
387 printf( " - %s\n", buf );
388#else
389 printf("\n");
390#endif
391 }
392
393 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000394
395#if defined(_WIN32)
396 printf( " + Press Enter to exit this program.\n" );
397 fflush( stdout ); getchar();
398#endif
399
400 return( ret );
401}
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200402#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */