blob: 983b64e3f20d7e413e36bbde025948b0af4abfe2 [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker2e24ca72013-09-18 15:21:27 +020036#include "polarssl/error.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#include "polarssl/pk.h"
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020038#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000039
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020041int main( int argc, char *argv[] )
42{
43 ((void) argc);
44 ((void) argv);
45
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
Paul Bakkered27a042013-04-18 22:46:23 +020047 return( 0 );
48}
49#else
50
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051#define MODE_NONE 0
52#define MODE_PRIVATE 1
53#define MODE_PUBLIC 2
54
55#define OUTPUT_MODE_NONE 0
56#define OUTPUT_MODE_PRIVATE 1
57#define OUTPUT_MODE_PUBLIC 2
58
Paul Bakkerf3df61a2013-08-26 17:22:23 +020059#define OUTPUT_FORMAT_PEM 0
60#define OUTPUT_FORMAT_DER 1
61
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062#define DFL_MODE MODE_NONE
63#define DFL_FILENAME "keyfile.key"
64#define DFL_DEBUG_LEVEL 0
Paul Bakkerf3df61a2013-08-26 17:22:23 +020065#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020066#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067#define DFL_OUTPUT_FILENAME "keyfile.pem"
Paul Bakkerf3df61a2013-08-26 17:22:23 +020068#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020069#else
70#define DFL_OUTPUT_FILENAME "keyfile.der"
71#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
72#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000073
74/*
75 * global options
76 */
77struct options
78{
79 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020080 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020082 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020083 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084} opt;
85
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020086static int write_public_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000087{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020088 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000089 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000090 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020091 unsigned char *c = output_buf;
92 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093
Paul Bakker1d569582012-10-03 20:35:44 +000094 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +020096#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020099 if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200100 return( ret );
101
102 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200104 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200105#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200106 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107 if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200108 return( ret );
109
110 len = ret;
111 c = output_buf + sizeof(output_buf) - len - 1;
112 }
113
114 if( ( f = fopen( output_file, "w" ) ) == NULL )
115 return( -1 );
116
117 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200118 {
119 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200120 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200121 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122
Paul Bakker0c226102014-04-17 16:02:36 +0200123 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200124
125 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126}
127
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200128static int write_private_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000132 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200133 unsigned char *c = output_buf;
134 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135
Paul Bakker1d569582012-10-03 20:35:44 +0000136 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200137
138#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200141 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200142 return( ret );
143
144 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200146 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200147#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200149 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150 return( ret );
151
152 len = ret;
153 c = output_buf + sizeof(output_buf) - len - 1;
154 }
155
156 if( ( f = fopen( output_file, "w" ) ) == NULL )
157 return( -1 );
158
159 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200160 {
161 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200162 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200163 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164
Paul Bakker0c226102014-04-17 16:02:36 +0200165 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200166
167 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168}
169
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200170#if defined(POLARSSL_PEM_WRITE_C)
171#define USAGE_OUT \
172 " output_file=%%s default: keyfile.pem\n" \
173 " output_format=pem|der default: pem\n"
174#else
175#define USAGE_OUT \
176 " output_file=%%s default: keyfile.der\n" \
177 " output_format=der default: der\n"
178#endif
179
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180#define USAGE \
181 "\n usage: key_app param=<>...\n" \
182 "\n acceptable parameters:\n" \
183 " mode=private|public default: none\n" \
184 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185 " output_mode=private|public default: none\n" \
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200186 USAGE_OUT \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187 "\n"
188
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189int main( int argc, char *argv[] )
190{
191 int ret = 0;
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200192 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000194 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195 char *p, *q;
196
197 /*
198 * Set to sane values
199 */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200200 pk_init( &key );
201 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202
203 if( argc == 0 )
204 {
205 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200206 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207 printf( USAGE );
208 goto exit;
209 }
210
211 opt.mode = DFL_MODE;
212 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213 opt.output_mode = DFL_OUTPUT_MODE;
214 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200215 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216
217 for( i = 1; i < argc; i++ )
218 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219 p = argv[i];
220 if( ( q = strchr( p, '=' ) ) == NULL )
221 goto usage;
222 *q++ = '\0';
223
224 if( strcmp( p, "mode" ) == 0 )
225 {
226 if( strcmp( q, "private" ) == 0 )
227 opt.mode = MODE_PRIVATE;
228 else if( strcmp( q, "public" ) == 0 )
229 opt.mode = MODE_PUBLIC;
230 else
231 goto usage;
232 }
233 else if( strcmp( p, "output_mode" ) == 0 )
234 {
235 if( strcmp( q, "private" ) == 0 )
236 opt.output_mode = OUTPUT_MODE_PRIVATE;
237 else if( strcmp( q, "public" ) == 0 )
238 opt.output_mode = OUTPUT_MODE_PUBLIC;
239 else
240 goto usage;
241 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200242 else if( strcmp( p, "output_format" ) == 0 )
243 {
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200244#if defined(POLARSSL_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200245 if( strcmp( q, "pem" ) == 0 )
246 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200247 else
248#endif
249 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200250 opt.output_format = OUTPUT_FORMAT_DER;
251 else
252 goto usage;
253 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254 else if( strcmp( p, "filename" ) == 0 )
255 opt.filename = q;
256 else if( strcmp( p, "output_file" ) == 0 )
257 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258 else
259 goto usage;
260 }
261
262 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
263 {
264 printf( "\nCannot output a key without reading one.\n");
265 goto exit;
266 }
267
268 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
269 {
270 printf( "\nCannot output a private key from a public key.\n");
271 goto exit;
272 }
273
274 if( opt.mode == MODE_PRIVATE )
275 {
276 /*
277 * 1.1. Load the key
278 */
279 printf( "\n . Loading the private key ..." );
280 fflush( stdout );
281
Paul Bakker1a7550a2013-09-15 13:01:22 +0200282 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283
284 if( ret != 0 )
285 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200286 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
287 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288 goto exit;
289 }
290
291 printf( " ok\n" );
292
293 /*
294 * 1.2 Print the key
295 */
296 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200297
298#if defined(POLARSSL_RSA_C)
299 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
300 {
301 rsa_context *rsa = pk_rsa( key );
302 mpi_write_file( "N: ", &rsa->N, 16, NULL );
303 mpi_write_file( "E: ", &rsa->E, 16, NULL );
304 mpi_write_file( "D: ", &rsa->D, 16, NULL );
305 mpi_write_file( "P: ", &rsa->P, 16, NULL );
306 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
307 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
308 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
309 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
310 }
311 else
312#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200313#if defined(POLARSSL_ECP_C)
314 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
315 {
316 ecp_keypair *ecp = pk_ec( key );
317 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
318 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
319 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
320 mpi_write_file( "D : ", &ecp->d , 16, NULL );
321 }
322 else
323#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200324 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000325
326 }
327 else if( opt.mode == MODE_PUBLIC )
328 {
329 /*
330 * 1.1. Load the key
331 */
332 printf( "\n . Loading the public key ..." );
333 fflush( stdout );
334
Paul Bakker1a7550a2013-09-15 13:01:22 +0200335 ret = pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336
337 if( ret != 0 )
338 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200339 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
340 printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000341 goto exit;
342 }
343
344 printf( " ok\n" );
345
346 /*
347 * 1.2 Print the key
348 */
349 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200350
351#if defined(POLARSSL_RSA_C)
352 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
353 {
354 rsa_context *rsa = pk_rsa( key );
355 mpi_write_file( "N: ", &rsa->N, 16, NULL );
356 mpi_write_file( "E: ", &rsa->E, 16, NULL );
357 }
358 else
359#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200360#if defined(POLARSSL_ECP_C)
361 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
362 {
363 ecp_keypair *ecp = pk_ec( key );
364 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
365 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
366 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
367 }
368 else
369#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200370 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000371 }
372 else
373 goto usage;
374
375 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
376 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200377 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000378 }
379 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
380 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200381 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000382 }
383
384exit:
385
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200386 if( ret != 0 && ret != 1)
387 {
388#ifdef POLARSSL_ERROR_C
389 polarssl_strerror( ret, buf, sizeof( buf ) );
390 printf( " - %s\n", buf );
391#else
392 printf("\n");
393#endif
394 }
395
396 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000397
398#if defined(_WIN32)
399 printf( " + Press Enter to exit this program.\n" );
400 fflush( stdout ); getchar();
401#endif
402
403 return( ret );
404}
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200405#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */