blob: 269ddbd438609f7240afd51531bb3f4d45704ea6 [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakker2e24ca72013-09-18 15:21:27 +020032#include "polarssl/error.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "polarssl/pk.h"
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020034#include "polarssl/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000035
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020037int main( int argc, char *argv[] )
38{
39 ((void) argc);
40 ((void) argv);
41
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
Paul Bakkered27a042013-04-18 22:46:23 +020043 return( 0 );
44}
45#else
46
Paul Bakkerbdb912d2012-02-13 23:11:30 +000047#define MODE_NONE 0
48#define MODE_PRIVATE 1
49#define MODE_PUBLIC 2
50
51#define OUTPUT_MODE_NONE 0
52#define OUTPUT_MODE_PRIVATE 1
53#define OUTPUT_MODE_PUBLIC 2
54
Paul Bakkerf3df61a2013-08-26 17:22:23 +020055#define OUTPUT_FORMAT_PEM 0
56#define OUTPUT_FORMAT_DER 1
57
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058#define DFL_MODE MODE_NONE
59#define DFL_FILENAME "keyfile.key"
60#define DFL_DEBUG_LEVEL 0
Paul Bakkerf3df61a2013-08-26 17:22:23 +020061#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062#define DFL_OUTPUT_FILENAME "keyfile.pem"
Paul Bakkerf3df61a2013-08-26 17:22:23 +020063#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064
65/*
66 * global options
67 */
68struct options
69{
70 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020074 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000075} opt;
76
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020077static int write_public_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000078{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020079 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000081 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020082 unsigned char *c = output_buf;
83 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084
Paul Bakker1d569582012-10-03 20:35:44 +000085 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086
Paul Bakkerf3df61a2013-08-26 17:22:23 +020087 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020089 if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +020090 return( ret );
91
92 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +020094 else
95 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020096 if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 return( ret );
98
99 len = ret;
100 c = output_buf + sizeof(output_buf) - len - 1;
101 }
102
103 if( ( f = fopen( output_file, "w" ) ) == NULL )
104 return( -1 );
105
106 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200107 {
108 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200109 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200110 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200111
Paul Bakker0c226102014-04-17 16:02:36 +0200112 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200113
114 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115}
116
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200117static int write_private_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200119 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000121 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122 unsigned char *c = output_buf;
123 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124
Paul Bakker1d569582012-10-03 20:35:44 +0000125 memset(output_buf, 0, 16000);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000127 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200128 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 return( ret );
130
131 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200133 else
134 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200135 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200136 return( ret );
137
138 len = ret;
139 c = output_buf + sizeof(output_buf) - len - 1;
140 }
141
142 if( ( f = fopen( output_file, "w" ) ) == NULL )
143 return( -1 );
144
145 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200146 {
147 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200149 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150
Paul Bakker0c226102014-04-17 16:02:36 +0200151 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200152
153 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154}
155
156#define USAGE \
157 "\n usage: key_app param=<>...\n" \
158 "\n acceptable parameters:\n" \
159 " mode=private|public default: none\n" \
160 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161 " output_mode=private|public default: none\n" \
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200162 " output_file=%%s default: keyfile.pem\n" \
163 " output_format=pem|der default: pem\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164 "\n"
165
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166int main( int argc, char *argv[] )
167{
168 int ret = 0;
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200169 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000171 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 char *p, *q;
173
174 /*
175 * Set to sane values
176 */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200177 pk_init( &key );
178 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000179
180 if( argc == 0 )
181 {
182 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200183 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000184 printf( USAGE );
185 goto exit;
186 }
187
188 opt.mode = DFL_MODE;
189 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190 opt.output_mode = DFL_OUTPUT_MODE;
191 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200192 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193
194 for( i = 1; i < argc; i++ )
195 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 p = argv[i];
197 if( ( q = strchr( p, '=' ) ) == NULL )
198 goto usage;
199 *q++ = '\0';
200
201 if( strcmp( p, "mode" ) == 0 )
202 {
203 if( strcmp( q, "private" ) == 0 )
204 opt.mode = MODE_PRIVATE;
205 else if( strcmp( q, "public" ) == 0 )
206 opt.mode = MODE_PUBLIC;
207 else
208 goto usage;
209 }
210 else if( strcmp( p, "output_mode" ) == 0 )
211 {
212 if( strcmp( q, "private" ) == 0 )
213 opt.output_mode = OUTPUT_MODE_PRIVATE;
214 else if( strcmp( q, "public" ) == 0 )
215 opt.output_mode = OUTPUT_MODE_PUBLIC;
216 else
217 goto usage;
218 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200219 else if( strcmp( p, "output_format" ) == 0 )
220 {
221 if( strcmp( q, "pem" ) == 0 )
222 opt.output_format = OUTPUT_FORMAT_PEM;
223 else if( strcmp( q, "der" ) == 0 )
224 opt.output_format = OUTPUT_FORMAT_DER;
225 else
226 goto usage;
227 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228 else if( strcmp( p, "filename" ) == 0 )
229 opt.filename = q;
230 else if( strcmp( p, "output_file" ) == 0 )
231 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 else
233 goto usage;
234 }
235
236 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
237 {
238 printf( "\nCannot output a key without reading one.\n");
239 goto exit;
240 }
241
242 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
243 {
244 printf( "\nCannot output a private key from a public key.\n");
245 goto exit;
246 }
247
248 if( opt.mode == MODE_PRIVATE )
249 {
250 /*
251 * 1.1. Load the key
252 */
253 printf( "\n . Loading the private key ..." );
254 fflush( stdout );
255
Paul Bakker1a7550a2013-09-15 13:01:22 +0200256 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257
258 if( ret != 0 )
259 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200260 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
261 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262 goto exit;
263 }
264
265 printf( " ok\n" );
266
267 /*
268 * 1.2 Print the key
269 */
270 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200271
272#if defined(POLARSSL_RSA_C)
273 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
274 {
275 rsa_context *rsa = pk_rsa( key );
276 mpi_write_file( "N: ", &rsa->N, 16, NULL );
277 mpi_write_file( "E: ", &rsa->E, 16, NULL );
278 mpi_write_file( "D: ", &rsa->D, 16, NULL );
279 mpi_write_file( "P: ", &rsa->P, 16, NULL );
280 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
281 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
282 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
283 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
284 }
285 else
286#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200287#if defined(POLARSSL_ECP_C)
288 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
289 {
290 ecp_keypair *ecp = pk_ec( key );
291 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
292 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
293 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
294 mpi_write_file( "D : ", &ecp->d , 16, NULL );
295 }
296 else
297#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200298 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299
300 }
301 else if( opt.mode == MODE_PUBLIC )
302 {
303 /*
304 * 1.1. Load the key
305 */
306 printf( "\n . Loading the public key ..." );
307 fflush( stdout );
308
Paul Bakker1a7550a2013-09-15 13:01:22 +0200309 ret = pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000310
311 if( ret != 0 )
312 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200313 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
314 printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000315 goto exit;
316 }
317
318 printf( " ok\n" );
319
320 /*
321 * 1.2 Print the key
322 */
323 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200324
325#if defined(POLARSSL_RSA_C)
326 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
327 {
328 rsa_context *rsa = pk_rsa( key );
329 mpi_write_file( "N: ", &rsa->N, 16, NULL );
330 mpi_write_file( "E: ", &rsa->E, 16, NULL );
331 }
332 else
333#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200334#if defined(POLARSSL_ECP_C)
335 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
336 {
337 ecp_keypair *ecp = pk_ec( key );
338 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
339 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
340 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
341 }
342 else
343#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200344 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000345 }
346 else
347 goto usage;
348
349 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
350 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200351 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000352 }
353 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
354 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200355 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000356 }
357
358exit:
359
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200360 if( ret != 0 && ret != 1)
361 {
362#ifdef POLARSSL_ERROR_C
363 polarssl_strerror( ret, buf, sizeof( buf ) );
364 printf( " - %s\n", buf );
365#else
366 printf("\n");
367#endif
368 }
369
370 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000371
372#if defined(_WIN32)
373 printf( " + Press Enter to exit this program.\n" );
374 fflush( stdout ); getchar();
375#endif
376
377 return( ret );
378}
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200379#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */