blob: 672ebf1a7bc8d36777df62e7d891c88f15064bf0 [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
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
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
Paul Bakkerbdb912d2012-02-13 23:11:30 +000066#define DFL_OUTPUT_FILENAME "keyfile.pem"
Paul Bakkerf3df61a2013-08-26 17:22:23 +020067#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
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 Bakkerf3df61a2013-08-26 17:22:23 +020078 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079} opt;
80
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +020081static int write_public_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000082{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020083 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000085 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020086 unsigned char *c = output_buf;
87 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088
Paul Bakker1d569582012-10-03 20:35:44 +000089 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090
Paul Bakkerf3df61a2013-08-26 17:22:23 +020091 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020093 if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +020094 return( ret );
95
96 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +020098 else
99 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200100 if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200101 return( ret );
102
103 len = ret;
104 c = output_buf + sizeof(output_buf) - len - 1;
105 }
106
107 if( ( f = fopen( output_file, "w" ) ) == NULL )
108 return( -1 );
109
110 if( fwrite( c, 1, len, f ) != len )
111 return( -1 );
112
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113 fclose(f);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200114
115 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116}
117
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200118static int write_private_key( pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200120 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000122 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200123 unsigned char *c = output_buf;
124 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125
Paul Bakker1d569582012-10-03 20:35:44 +0000126 memset(output_buf, 0, 16000);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200127 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200129 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 return( ret );
131
132 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134 else
135 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200136 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200137 return( ret );
138
139 len = ret;
140 c = output_buf + sizeof(output_buf) - len - 1;
141 }
142
143 if( ( f = fopen( output_file, "w" ) ) == NULL )
144 return( -1 );
145
146 if( fwrite( c, 1, len, f ) != len )
147 return( -1 );
148
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149 fclose(f);
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150
151 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152}
153
154#define USAGE \
155 "\n usage: key_app param=<>...\n" \
156 "\n acceptable parameters:\n" \
157 " mode=private|public default: none\n" \
158 " filename=%%s default: keyfile.key\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000159 " output_mode=private|public default: none\n" \
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200160 " output_file=%%s default: keyfile.pem\n" \
161 " output_format=pem|der default: pem\n" \
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162 "\n"
163
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164int main( int argc, char *argv[] )
165{
166 int ret = 0;
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200167 pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000169 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 char *p, *q;
171
172 /*
173 * Set to sane values
174 */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200175 pk_init( &key );
176 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177
178 if( argc == 0 )
179 {
180 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200181 ret = 1;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 printf( USAGE );
183 goto exit;
184 }
185
186 opt.mode = DFL_MODE;
187 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 opt.output_mode = DFL_OUTPUT_MODE;
189 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200190 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191
192 for( i = 1; i < argc; i++ )
193 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194 p = argv[i];
195 if( ( q = strchr( p, '=' ) ) == NULL )
196 goto usage;
197 *q++ = '\0';
198
199 if( strcmp( p, "mode" ) == 0 )
200 {
201 if( strcmp( q, "private" ) == 0 )
202 opt.mode = MODE_PRIVATE;
203 else if( strcmp( q, "public" ) == 0 )
204 opt.mode = MODE_PUBLIC;
205 else
206 goto usage;
207 }
208 else if( strcmp( p, "output_mode" ) == 0 )
209 {
210 if( strcmp( q, "private" ) == 0 )
211 opt.output_mode = OUTPUT_MODE_PRIVATE;
212 else if( strcmp( q, "public" ) == 0 )
213 opt.output_mode = OUTPUT_MODE_PUBLIC;
214 else
215 goto usage;
216 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200217 else if( strcmp( p, "output_format" ) == 0 )
218 {
219 if( strcmp( q, "pem" ) == 0 )
220 opt.output_format = OUTPUT_FORMAT_PEM;
221 else if( strcmp( q, "der" ) == 0 )
222 opt.output_format = OUTPUT_FORMAT_DER;
223 else
224 goto usage;
225 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226 else if( strcmp( p, "filename" ) == 0 )
227 opt.filename = q;
228 else if( strcmp( p, "output_file" ) == 0 )
229 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230 else
231 goto usage;
232 }
233
234 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
235 {
236 printf( "\nCannot output a key without reading one.\n");
237 goto exit;
238 }
239
240 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
241 {
242 printf( "\nCannot output a private key from a public key.\n");
243 goto exit;
244 }
245
246 if( opt.mode == MODE_PRIVATE )
247 {
248 /*
249 * 1.1. Load the key
250 */
251 printf( "\n . Loading the private key ..." );
252 fflush( stdout );
253
Paul Bakker1a7550a2013-09-15 13:01:22 +0200254 ret = pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255
256 if( ret != 0 )
257 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200258 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
259 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260 goto exit;
261 }
262
263 printf( " ok\n" );
264
265 /*
266 * 1.2 Print the key
267 */
268 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200269
270#if defined(POLARSSL_RSA_C)
271 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
272 {
273 rsa_context *rsa = pk_rsa( key );
274 mpi_write_file( "N: ", &rsa->N, 16, NULL );
275 mpi_write_file( "E: ", &rsa->E, 16, NULL );
276 mpi_write_file( "D: ", &rsa->D, 16, NULL );
277 mpi_write_file( "P: ", &rsa->P, 16, NULL );
278 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
279 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
280 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
281 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
282 }
283 else
284#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200285#if defined(POLARSSL_ECP_C)
286 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
287 {
288 ecp_keypair *ecp = pk_ec( key );
289 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
290 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
291 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
292 mpi_write_file( "D : ", &ecp->d , 16, NULL );
293 }
294 else
295#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200296 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297
298 }
299 else if( opt.mode == MODE_PUBLIC )
300 {
301 /*
302 * 1.1. Load the key
303 */
304 printf( "\n . Loading the public key ..." );
305 fflush( stdout );
306
Paul Bakker1a7550a2013-09-15 13:01:22 +0200307 ret = pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000308
309 if( ret != 0 )
310 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200311 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
312 printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000313 goto exit;
314 }
315
316 printf( " ok\n" );
317
318 /*
319 * 1.2 Print the key
320 */
321 printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200322
323#if defined(POLARSSL_RSA_C)
324 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
325 {
326 rsa_context *rsa = pk_rsa( key );
327 mpi_write_file( "N: ", &rsa->N, 16, NULL );
328 mpi_write_file( "E: ", &rsa->E, 16, NULL );
329 }
330 else
331#endif
Paul Bakker2e24ca72013-09-18 15:21:27 +0200332#if defined(POLARSSL_ECP_C)
333 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
334 {
335 ecp_keypair *ecp = pk_ec( key );
336 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
337 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
338 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
339 }
340 else
341#endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200342 printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000343 }
344 else
345 goto usage;
346
347 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
348 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200349 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000350 }
351 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
352 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200353 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000354 }
355
356exit:
357
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200358 if( ret != 0 && ret != 1)
359 {
360#ifdef POLARSSL_ERROR_C
361 polarssl_strerror( ret, buf, sizeof( buf ) );
362 printf( " - %s\n", buf );
363#else
364 printf("\n");
365#endif
366 }
367
368 pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000369
370#if defined(_WIN32)
371 printf( " + Press Enter to exit this program.\n" );
372 fflush( stdout ); getchar();
373#endif
374
375 return( ret );
376}
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200377#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */