blob: b273e7daa1e521b36b4918c3207775a7c8f24490 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/error.h"
37#include "mbedtls/pk.h"
38#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000039
Rich Evans18b78c72015-02-11 14:06:19 +000040#include <stdio.h>
41#include <string.h>
42#endif
Paul Bakkered27a042013-04-18 22:46:23 +020043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000045#define USAGE_OUT \
46 " output_file=%%s default: keyfile.pem\n" \
47 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020048#else
Rich Evans18b78c72015-02-11 14:06:19 +000049#define USAGE_OUT \
50 " output_file=%%s default: keyfile.der\n" \
51 " output_format=der default: der\n"
52#endif
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000055#define DFL_OUTPUT_FILENAME "keyfile.pem"
56#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
57#else
58#define DFL_OUTPUT_FILENAME "keyfile.der"
59#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
60#endif
61
62#define DFL_MODE MODE_NONE
63#define DFL_FILENAME "keyfile.key"
64#define DFL_DEBUG_LEVEL 0
65#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020066
Paul Bakkerbdb912d2012-02-13 23:11:30 +000067#define MODE_NONE 0
68#define MODE_PRIVATE 1
69#define MODE_PUBLIC 2
70
71#define OUTPUT_MODE_NONE 0
72#define OUTPUT_MODE_PRIVATE 1
73#define OUTPUT_MODE_PUBLIC 2
74
Paul Bakkerf3df61a2013-08-26 17:22:23 +020075#define OUTPUT_FORMAT_PEM 0
76#define OUTPUT_FORMAT_DER 1
77
Rich Evans18b78c72015-02-11 14:06:19 +000078#define USAGE \
79 "\n usage: key_app param=<>...\n" \
80 "\n acceptable parameters:\n" \
81 " mode=private|public default: none\n" \
82 " filename=%%s default: keyfile.key\n" \
83 " output_mode=private|public default: none\n" \
84 USAGE_OUT \
85 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000088int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Rich Evans18b78c72015-02-11 14:06:19 +000091 return( 0 );
92}
93#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094/*
95 * global options
96 */
97struct options
98{
99 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200100 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200103 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104} opt;
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200108 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000110 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200111 unsigned char *c = output_buf;
112 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113
Paul Bakker1d569582012-10-03 20:35:44 +0000114 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200117 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200120 return( ret );
121
122 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200124 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200125#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200128 return( ret );
129
130 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200131 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200132 }
133
134 if( ( f = fopen( output_file, "w" ) ) == NULL )
135 return( -1 );
136
137 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200138 {
139 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200140 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200141 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200142
Paul Bakker0c226102014-04-17 16:02:36 +0200143 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200144
145 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146}
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000152 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200153 unsigned char *c = output_buf;
154 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155
Paul Bakker1d569582012-10-03 20:35:44 +0000156 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200162 return( ret );
163
164 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200166 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200167#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200170 return( ret );
171
172 len = ret;
173 c = output_buf + sizeof(output_buf) - len - 1;
174 }
175
176 if( ( f = fopen( output_file, "w" ) ) == NULL )
177 return( -1 );
178
179 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200180 {
181 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200182 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200183 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200184
Paul Bakker0c226102014-04-17 16:02:36 +0200185 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200186
187 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188}
189
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190int main( int argc, char *argv[] )
191{
192 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000195 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 char *p, *q;
197
198 /*
199 * Set to sane values
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200202 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203
204 if( argc == 0 )
205 {
206 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200207 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209 goto exit;
210 }
211
212 opt.mode = DFL_MODE;
213 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000214 opt.output_mode = DFL_OUTPUT_MODE;
215 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200216 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217
218 for( i = 1; i < argc; i++ )
219 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 p = argv[i];
221 if( ( q = strchr( p, '=' ) ) == NULL )
222 goto usage;
223 *q++ = '\0';
224
225 if( strcmp( p, "mode" ) == 0 )
226 {
227 if( strcmp( q, "private" ) == 0 )
228 opt.mode = MODE_PRIVATE;
229 else if( strcmp( q, "public" ) == 0 )
230 opt.mode = MODE_PUBLIC;
231 else
232 goto usage;
233 }
234 else if( strcmp( p, "output_mode" ) == 0 )
235 {
236 if( strcmp( q, "private" ) == 0 )
237 opt.output_mode = OUTPUT_MODE_PRIVATE;
238 else if( strcmp( q, "public" ) == 0 )
239 opt.output_mode = OUTPUT_MODE_PUBLIC;
240 else
241 goto usage;
242 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200243 else if( strcmp( p, "output_format" ) == 0 )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200246 if( strcmp( q, "pem" ) == 0 )
247 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200248 else
249#endif
250 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200251 opt.output_format = OUTPUT_FORMAT_DER;
252 else
253 goto usage;
254 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255 else if( strcmp( p, "filename" ) == 0 )
256 opt.filename = q;
257 else if( strcmp( p, "output_file" ) == 0 )
258 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259 else
260 goto usage;
261 }
262
263 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266 goto exit;
267 }
268
269 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272 goto exit;
273 }
274
275 if( opt.mode == MODE_PRIVATE )
276 {
277 /*
278 * 1.1. Load the key
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 fflush( stdout );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000284
285 if( ret != 0 )
286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
288 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 goto exit;
290 }
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293
294 /*
295 * 1.2 Print the key
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_RSA_C)
300 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
303 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
304 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
305 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
306 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
307 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
308 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
309 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
310 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200311 }
312 else
313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_ECP_C)
315 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
318 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
319 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
320 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
321 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200322 }
323 else
324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000326
327 }
328 else if( opt.mode == MODE_PUBLIC )
329 {
330 /*
331 * 1.1. Load the key
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000334 fflush( stdout );
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000337
338 if( ret != 0 )
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
341 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000342 goto exit;
343 }
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000346
347 /*
348 * 1.2 Print the key
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_RSA_C)
353 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
356 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
357 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200358 }
359 else
360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_ECP_C)
362 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
365 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
366 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
367 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200368 }
369 else
370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000372 }
373 else
374 goto usage;
375
376 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
377 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200378 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000379 }
380 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
381 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200382 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000383 }
384
385exit:
386
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200387 if( ret != 0 && ret != 1)
388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#ifdef MBEDTLS_ERROR_C
390 mbedtls_strerror( ret, buf, sizeof( buf ) );
391 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200392#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200394#endif
395 }
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000398
399#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000401 fflush( stdout ); getchar();
402#endif
403
404 return( ret );
405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */