blob: 08aba60126c582da1ff39dbe32376cbb5735bbee [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
Hanno Beckerc2eba852018-10-16 13:45:22 +010087#if !defined(MBEDTLS_PK_PARSE_C) || \
88 !defined(MBEDTLS_PK_WRITE_C) || \
89 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000090int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000091{
Hanno Beckerc2eba852018-10-16 13:45:22 +010092 mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Rich Evans18b78c72015-02-11 14:06:19 +000093 return( 0 );
94}
95#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096/*
97 * global options
98 */
99struct options
100{
101 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200105 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106} opt;
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200110 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000112 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200113 unsigned char *c = output_buf;
114 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115
Paul Bakker1d569582012-10-03 20:35:44 +0000116 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200119 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122 return( ret );
123
124 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200127#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 return( ret );
131
132 len = ret;
Ron Eldoreec77b52018-01-07 18:10:43 +0200133 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134 }
135
136 if( ( f = fopen( output_file, "w" ) ) == NULL )
137 return( -1 );
138
139 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200140 {
141 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200142 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200143 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200144
Paul Bakker0c226102014-04-17 16:02:36 +0200145 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200146
147 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200152 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000153 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000154 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200155 unsigned char *c = output_buf;
156 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157
Paul Bakker1d569582012-10-03 20:35:44 +0000158 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200161 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164 return( ret );
165
166 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200169#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200172 return( ret );
173
174 len = ret;
175 c = output_buf + sizeof(output_buf) - len - 1;
176 }
177
178 if( ( f = fopen( output_file, "w" ) ) == NULL )
179 return( -1 );
180
181 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200182 {
183 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200184 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200185 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200186
Paul Bakker0c226102014-04-17 16:02:36 +0200187 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200188
189 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190}
191
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192int main( int argc, char *argv[] )
193{
194 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000197 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 char *p, *q;
199
200 /*
201 * Set to sane values
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200204 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205
206 if( argc == 0 )
207 {
208 usage:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200209 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211 goto exit;
212 }
213
214 opt.mode = DFL_MODE;
215 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 opt.output_mode = DFL_OUTPUT_MODE;
217 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200218 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219
220 for( i = 1; i < argc; i++ )
221 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222 p = argv[i];
223 if( ( q = strchr( p, '=' ) ) == NULL )
224 goto usage;
225 *q++ = '\0';
226
227 if( strcmp( p, "mode" ) == 0 )
228 {
229 if( strcmp( q, "private" ) == 0 )
230 opt.mode = MODE_PRIVATE;
231 else if( strcmp( q, "public" ) == 0 )
232 opt.mode = MODE_PUBLIC;
233 else
234 goto usage;
235 }
236 else if( strcmp( p, "output_mode" ) == 0 )
237 {
238 if( strcmp( q, "private" ) == 0 )
239 opt.output_mode = OUTPUT_MODE_PRIVATE;
240 else if( strcmp( q, "public" ) == 0 )
241 opt.output_mode = OUTPUT_MODE_PUBLIC;
242 else
243 goto usage;
244 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200245 else if( strcmp( p, "output_format" ) == 0 )
246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200248 if( strcmp( q, "pem" ) == 0 )
249 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200250 else
251#endif
252 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200253 opt.output_format = OUTPUT_FORMAT_DER;
254 else
255 goto usage;
256 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257 else if( strcmp( p, "filename" ) == 0 )
258 opt.filename = q;
259 else if( strcmp( p, "output_file" ) == 0 )
260 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261 else
262 goto usage;
263 }
264
265 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268 goto exit;
269 }
270
271 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 goto exit;
275 }
276
277 if( opt.mode == MODE_PRIVATE )
278 {
279 /*
280 * 1.1. Load the key
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 fflush( stdout );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286
287 if( ret != 0 )
288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
290 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000291 goto exit;
292 }
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
296 /*
297 * 1.2 Print the key
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_RSA_C)
302 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
305 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
306 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
307 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
308 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
309 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
310 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
311 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
312 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200313 }
314 else
315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_ECP_C)
317 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
320 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
321 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
322 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
323 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200324 }
325 else
326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000328
329 }
330 else if( opt.mode == MODE_PUBLIC )
331 {
332 /*
333 * 1.1. Load the key
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336 fflush( stdout );
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000339
340 if( ret != 0 )
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
343 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000344 goto exit;
345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000348
349 /*
350 * 1.2 Print the key
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_RSA_C)
355 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
358 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
359 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200360 }
361 else
362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_ECP_C)
364 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
367 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
368 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
369 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200370 }
371 else
372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000374 }
375 else
376 goto usage;
377
378 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
379 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200380 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000381 }
382 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
383 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200384 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000385 }
386
387exit:
388
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200389 if( ret != 0 && ret != 1)
390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#ifdef MBEDTLS_ERROR_C
392 mbedtls_strerror( ret, buf, sizeof( buf ) );
393 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200394#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200396#endif
397 }
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000400
401#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403 fflush( stdout ); getchar();
404#endif
405
406 return( ret );
407}
Hanno Beckerc2eba852018-10-16 13:45:22 +0100408#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */