blob: 05224f47398c7de142cf12d2718e58f39ccdc4dd [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>
Andres Amaya Garciaed684882018-04-29 20:07:30 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaed684882018-04-29 20:07:30 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/error.h"
41#include "mbedtls/pk.h"
Manuel Pégourié-Gonnardf4afbf92019-09-19 10:45:14 +020042#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <string.h>
47#endif
Paul Bakkered27a042013-04-18 22:46:23 +020048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000050#define USAGE_OUT \
51 " output_file=%%s default: keyfile.pem\n" \
52 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020053#else
Rich Evans18b78c72015-02-11 14:06:19 +000054#define USAGE_OUT \
55 " output_file=%%s default: keyfile.der\n" \
56 " output_format=der default: der\n"
57#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000060#define DFL_OUTPUT_FILENAME "keyfile.pem"
61#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
62#else
63#define DFL_OUTPUT_FILENAME "keyfile.der"
64#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
65#endif
66
67#define DFL_MODE MODE_NONE
68#define DFL_FILENAME "keyfile.key"
69#define DFL_DEBUG_LEVEL 0
70#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020071
Paul Bakkerbdb912d2012-02-13 23:11:30 +000072#define MODE_NONE 0
73#define MODE_PRIVATE 1
74#define MODE_PUBLIC 2
75
76#define OUTPUT_MODE_NONE 0
77#define OUTPUT_MODE_PRIVATE 1
78#define OUTPUT_MODE_PUBLIC 2
79
Paul Bakkerf3df61a2013-08-26 17:22:23 +020080#define OUTPUT_FORMAT_PEM 0
81#define OUTPUT_FORMAT_DER 1
82
Rich Evans18b78c72015-02-11 14:06:19 +000083#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010084 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000085 "\n acceptable parameters:\n" \
86 " mode=private|public default: none\n" \
87 " filename=%%s default: keyfile.key\n" \
88 " output_mode=private|public default: none\n" \
89 USAGE_OUT \
90 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000091
Hanno Beckerb14c3312018-10-16 13:45:22 +010092#if !defined(MBEDTLS_PK_PARSE_C) || \
93 !defined(MBEDTLS_PK_WRITE_C) || \
94 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000095int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000096{
Hanno Beckerb14c3312018-10-16 13:45:22 +010097 mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Andrzej Kurek825ebd42020-05-18 11:47:25 -040098 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +000099}
100#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000101
Jarno Lamsa642596e2019-10-04 12:52:42 +0300102#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
103int mbedtls_hardware_poll( void *data, unsigned char *output,
104 size_t len, size_t *olen )
105{
106 size_t i;
107 (void) data;
108 for( i = 0; i < len; ++i )
109 output[i] = rand();
110 *olen = len;
111 return( 0 );
112}
113#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +0000114
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115/*
116 * global options
117 */
118struct options
119{
120 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200121 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200123 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200124 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125} opt;
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000131 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200132 unsigned char *c = output_buf;
133 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134
Paul Bakker1d569582012-10-03 20:35:44 +0000135 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200138 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200141 return( ret );
142
143 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200145 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200146#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200149 return( ret );
150
151 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200152 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200153 }
154
155 if( ( f = fopen( output_file, "w" ) ) == NULL )
156 return( -1 );
157
158 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200159 {
160 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200161 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200162 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163
Paul Bakker0c226102014-04-17 16:02:36 +0200164 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200165
166 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000173 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200174 unsigned char *c = output_buf;
175 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176
Paul Bakker1d569582012-10-03 20:35:44 +0000177 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200180 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200183 return( ret );
184
185 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200187 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200188#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200191 return( ret );
192
193 len = ret;
Christian Walther38c919b2018-11-28 13:32:27 +0100194 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200195 }
196
197 if( ( f = fopen( output_file, "w" ) ) == NULL )
198 return( -1 );
199
200 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200201 {
202 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200203 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200204 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200205
Paul Bakker0c226102014-04-17 16:02:36 +0200206 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200207
208 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209}
210
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211int main( int argc, char *argv[] )
212{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100213 int ret = 1;
214 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000216 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217 char *p, *q;
218
Hanno Becker40371ec2017-08-23 06:46:17 +0100219 mbedtls_pk_context key;
220 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
221
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222 /*
223 * Set to sane values
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200226 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227
Hanno Becker40371ec2017-08-23 06:46:17 +0100228 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
229 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
230 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
231
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 if( argc == 0 )
233 {
234 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236 goto exit;
237 }
238
239 opt.mode = DFL_MODE;
240 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241 opt.output_mode = DFL_OUTPUT_MODE;
242 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200243 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244
245 for( i = 1; i < argc; i++ )
246 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 p = argv[i];
248 if( ( q = strchr( p, '=' ) ) == NULL )
249 goto usage;
250 *q++ = '\0';
251
252 if( strcmp( p, "mode" ) == 0 )
253 {
254 if( strcmp( q, "private" ) == 0 )
255 opt.mode = MODE_PRIVATE;
256 else if( strcmp( q, "public" ) == 0 )
257 opt.mode = MODE_PUBLIC;
258 else
259 goto usage;
260 }
261 else if( strcmp( p, "output_mode" ) == 0 )
262 {
263 if( strcmp( q, "private" ) == 0 )
264 opt.output_mode = OUTPUT_MODE_PRIVATE;
265 else if( strcmp( q, "public" ) == 0 )
266 opt.output_mode = OUTPUT_MODE_PUBLIC;
267 else
268 goto usage;
269 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200270 else if( strcmp( p, "output_format" ) == 0 )
271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200273 if( strcmp( q, "pem" ) == 0 )
274 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200275 else
276#endif
277 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200278 opt.output_format = OUTPUT_FORMAT_DER;
279 else
280 goto usage;
281 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282 else if( strcmp( p, "filename" ) == 0 )
283 opt.filename = q;
284 else if( strcmp( p, "output_file" ) == 0 )
285 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286 else
287 goto usage;
288 }
289
290 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293 goto exit;
294 }
295
296 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299 goto exit;
300 }
301
302 if( opt.mode == MODE_PRIVATE )
303 {
304 /*
305 * 1.1. Load the key
306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000308 fflush( stdout );
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000311
312 if( ret != 0 )
313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
315 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000316 goto exit;
317 }
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000320
321 /*
322 * 1.2 Print the key
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_RSA_C)
327 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100330
331 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
332 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
333 {
334 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
335 goto exit;
336 }
337
338 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
339 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
340 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
341 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
342 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
343 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
344 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
345 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200346 }
347 else
348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_ECP_C)
350 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
353 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
354 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
355 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
356 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200357 }
358 else
359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000361
362 }
363 else if( opt.mode == MODE_PUBLIC )
364 {
365 /*
366 * 1.1. Load the key
367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000369 fflush( stdout );
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000372
373 if( ret != 0 )
374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
376 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000377 goto exit;
378 }
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000381
382 /*
383 * 1.2 Print the key
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_RSA_C)
388 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100391
392 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
393 NULL, &E ) ) != 0 )
394 {
395 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
396 goto exit;
397 }
398 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
399 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200400 }
401 else
402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_ECP_C)
404 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
407 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
408 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
409 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200410 }
411 else
412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000414 }
415 else
416 goto usage;
417
418 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
419 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200420 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000421 }
422 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
423 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200424 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000425 }
426
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100427 exit_code = MBEDTLS_EXIT_SUCCESS;
428
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000429exit:
430
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100431 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#ifdef MBEDTLS_ERROR_C
434 mbedtls_strerror( ret, buf, sizeof( buf ) );
435 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200436#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200438#endif
439 }
440
Hanno Becker40371ec2017-08-23 06:46:17 +0100441 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
442 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
443 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000446
447#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000449 fflush( stdout ); getchar();
450#endif
451
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400452 mbedtls_exit( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000453}
Hanno Beckerb14c3312018-10-16 13:45:22 +0100454#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */