blob: 3a3bd2f35958cfe11329bfd928abb579fd6cdccf [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/error.h"
38#include "mbedtls/pk.h"
39#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000046#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000056#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"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000089int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Rich Evans18b78c72015-02-11 14:06:19 +000092 return( 0 );
93}
94#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095/*
96 * global options
97 */
98struct options
99{
100 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200104 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105} opt;
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200109 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000111 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200112 unsigned char *c = output_buf;
113 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114
Paul Bakker1d569582012-10-03 20:35:44 +0000115 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121 return( ret );
122
123 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200125 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200126#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 return( ret );
130
131 len = ret;
132 c = output_buf + sizeof(output_buf) - len - 1;
133 }
134
135 if( ( f = fopen( output_file, "w" ) ) == NULL )
136 return( -1 );
137
138 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200139 {
140 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200141 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200142 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200143
Paul Bakker0c226102014-04-17 16:02:36 +0200144 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200145
146 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200151 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000153 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200154 unsigned char *c = output_buf;
155 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156
Paul Bakker1d569582012-10-03 20:35:44 +0000157 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200160 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163 return( ret );
164
165 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200167 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200168#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 return( ret );
172
173 len = ret;
174 c = output_buf + sizeof(output_buf) - len - 1;
175 }
176
177 if( ( f = fopen( output_file, "w" ) ) == NULL )
178 return( -1 );
179
180 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200181 {
182 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200183 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200184 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200185
Paul Bakker0c226102014-04-17 16:02:36 +0200186 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200187
188 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189}
190
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191int main( int argc, char *argv[] )
192{
193 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000196 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197 char *p, *q;
198
199 /*
200 * Set to sane values
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200203 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204
205 if( argc == 0 )
206 {
207 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200208 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210 goto exit;
211 }
212
213 opt.mode = DFL_MODE;
214 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215 opt.output_mode = DFL_OUTPUT_MODE;
216 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200217 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218
219 for( i = 1; i < argc; i++ )
220 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221 p = argv[i];
222 if( ( q = strchr( p, '=' ) ) == NULL )
223 goto usage;
224 *q++ = '\0';
225
226 if( strcmp( p, "mode" ) == 0 )
227 {
228 if( strcmp( q, "private" ) == 0 )
229 opt.mode = MODE_PRIVATE;
230 else if( strcmp( q, "public" ) == 0 )
231 opt.mode = MODE_PUBLIC;
232 else
233 goto usage;
234 }
235 else if( strcmp( p, "output_mode" ) == 0 )
236 {
237 if( strcmp( q, "private" ) == 0 )
238 opt.output_mode = OUTPUT_MODE_PRIVATE;
239 else if( strcmp( q, "public" ) == 0 )
240 opt.output_mode = OUTPUT_MODE_PUBLIC;
241 else
242 goto usage;
243 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200244 else if( strcmp( p, "output_format" ) == 0 )
245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200247 if( strcmp( q, "pem" ) == 0 )
248 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200249 else
250#endif
251 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200252 opt.output_format = OUTPUT_FORMAT_DER;
253 else
254 goto usage;
255 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256 else if( strcmp( p, "filename" ) == 0 )
257 opt.filename = q;
258 else if( strcmp( p, "output_file" ) == 0 )
259 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260 else
261 goto usage;
262 }
263
264 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000267 goto exit;
268 }
269
270 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 goto exit;
274 }
275
276 if( opt.mode == MODE_PRIVATE )
277 {
278 /*
279 * 1.1. Load the key
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282 fflush( stdout );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285
286 if( ret != 0 )
287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
289 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000290 goto exit;
291 }
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294
295 /*
296 * 1.2 Print the key
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_RSA_C)
301 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
304 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
305 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
306 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
307 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
308 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
309 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
310 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
311 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200312 }
313 else
314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_ECP_C)
316 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
319 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
320 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
321 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
322 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200323 }
324 else
325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000327
328 }
329 else if( opt.mode == MODE_PUBLIC )
330 {
331 /*
332 * 1.1. Load the key
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000335 fflush( stdout );
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338
339 if( ret != 0 )
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
342 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000343 goto exit;
344 }
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000347
348 /*
349 * 1.2 Print the key
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_RSA_C)
354 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
357 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
358 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200359 }
360 else
361#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_ECP_C)
363 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
366 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
367 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
368 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200369 }
370 else
371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000373 }
374 else
375 goto usage;
376
377 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
378 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200379 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000380 }
381 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
382 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200383 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384 }
385
386exit:
387
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200388 if( ret != 0 && ret != 1)
389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#ifdef MBEDTLS_ERROR_C
391 mbedtls_strerror( ret, buf, sizeof( buf ) );
392 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200393#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200395#endif
396 }
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000399
400#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000402 fflush( stdout ); getchar();
403#endif
404
405 return( ret );
406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */