blob: 93a94d901bbfe99c382bd67f82e55f5624a0ae39 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/error.h"
30#include "mbedtls/pk.h"
31#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <stdio.h>
34#include <string.h>
35#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000038#define USAGE_OUT \
39 " output_file=%%s default: keyfile.pem\n" \
40 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020041#else
Rich Evans18b78c72015-02-11 14:06:19 +000042#define USAGE_OUT \
43 " output_file=%%s default: keyfile.der\n" \
44 " output_format=der default: der\n"
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000048#define DFL_OUTPUT_FILENAME "keyfile.pem"
49#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
50#else
51#define DFL_OUTPUT_FILENAME "keyfile.der"
52#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
53#endif
54
55#define DFL_MODE MODE_NONE
56#define DFL_FILENAME "keyfile.key"
57#define DFL_DEBUG_LEVEL 0
58#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020059
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060#define MODE_NONE 0
61#define MODE_PRIVATE 1
62#define MODE_PUBLIC 2
63
64#define OUTPUT_MODE_NONE 0
65#define OUTPUT_MODE_PRIVATE 1
66#define OUTPUT_MODE_PUBLIC 2
67
Paul Bakkerf3df61a2013-08-26 17:22:23 +020068#define OUTPUT_FORMAT_PEM 0
69#define OUTPUT_FORMAT_DER 1
70
Rich Evans18b78c72015-02-11 14:06:19 +000071#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010072 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000073 "\n acceptable parameters:\n" \
74 " mode=private|public default: none\n" \
75 " filename=%%s default: keyfile.key\n" \
76 " output_mode=private|public default: none\n" \
77 USAGE_OUT \
78 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000079
Hanno Beckerb14c3312018-10-16 13:45:22 +010080#if !defined(MBEDTLS_PK_PARSE_C) || \
81 !defined(MBEDTLS_PK_WRITE_C) || \
82 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000083int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000084{
Hanno Beckerb14c3312018-10-16 13:45:22 +010085 mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020086 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +000087}
88#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000089
Simon Butcher63cb97e2018-12-06 17:43:31 +000090
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091/*
92 * global options
93 */
94struct options
95{
96 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020097 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020099 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200100 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101} opt;
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200105 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000107 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200108 unsigned char *c = output_buf;
109 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110
Paul Bakker1d569582012-10-03 20:35:44 +0000111 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200114 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200117 return( ret );
118
119 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200122#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200125 return( ret );
126
127 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200128 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 }
130
131 if( ( f = fopen( output_file, "w" ) ) == NULL )
132 return( -1 );
133
134 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200135 {
136 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200137 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200138 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139
Paul Bakker0c226102014-04-17 16:02:36 +0200140 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200141
142 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143}
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000149 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150 unsigned char *c = output_buf;
151 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152
Paul Bakker1d569582012-10-03 20:35:44 +0000153 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200156 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 return( ret );
160
161 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200164#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200167 return( ret );
168
169 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100170 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 }
172
173 if( ( f = fopen( output_file, "w" ) ) == NULL )
174 return( -1 );
175
176 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200177 {
178 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200179 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200180 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200181
Paul Bakker0c226102014-04-17 16:02:36 +0200182 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200183
184 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185}
186
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187int main( int argc, char *argv[] )
188{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100189 int ret = 1;
190 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200191#if defined(MBEDTLS_ERROR_C)
192 char buf[200];
193#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000194 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195 char *p, *q;
196
Hanno Becker40371ec2017-08-23 06:46:17 +0100197 mbedtls_pk_context key;
198 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
199
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 /*
201 * Set to sane values
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_pk_init( &key );
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200204#if defined(MBEDTLS_ERROR_C)
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200205 memset( buf, 0, sizeof( buf ) );
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200206#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207
Hanno Becker40371ec2017-08-23 06:46:17 +0100208 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
209 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
210 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
211
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212 if( argc == 0 )
213 {
214 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 goto exit;
217 }
218
219 opt.mode = DFL_MODE;
220 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221 opt.output_mode = DFL_OUTPUT_MODE;
222 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200223 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224
225 for( i = 1; i < argc; i++ )
226 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227 p = argv[i];
228 if( ( q = strchr( p, '=' ) ) == NULL )
229 goto usage;
230 *q++ = '\0';
231
232 if( strcmp( p, "mode" ) == 0 )
233 {
234 if( strcmp( q, "private" ) == 0 )
235 opt.mode = MODE_PRIVATE;
236 else if( strcmp( q, "public" ) == 0 )
237 opt.mode = MODE_PUBLIC;
238 else
239 goto usage;
240 }
241 else if( strcmp( p, "output_mode" ) == 0 )
242 {
243 if( strcmp( q, "private" ) == 0 )
244 opt.output_mode = OUTPUT_MODE_PRIVATE;
245 else if( strcmp( q, "public" ) == 0 )
246 opt.output_mode = OUTPUT_MODE_PUBLIC;
247 else
248 goto usage;
249 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200250 else if( strcmp( p, "output_format" ) == 0 )
251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200253 if( strcmp( q, "pem" ) == 0 )
254 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200255 else
256#endif
257 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200258 opt.output_format = OUTPUT_FORMAT_DER;
259 else
260 goto usage;
261 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262 else if( strcmp( p, "filename" ) == 0 )
263 opt.filename = q;
264 else if( strcmp( p, "output_file" ) == 0 )
265 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266 else
267 goto usage;
268 }
269
270 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 goto exit;
274 }
275
276 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000279 goto exit;
280 }
281
282 if( opt.mode == MODE_PRIVATE )
283 {
284 /*
285 * 1.1. Load the key
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000288 fflush( stdout );
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000291
292 if( ret != 0 )
293 {
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200294 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x", (unsigned int) -ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295 goto exit;
296 }
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299
300 /*
301 * 1.2 Print the key
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_RSA_C)
306 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100309
310 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
311 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
312 {
313 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
314 goto exit;
315 }
316
317 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
318 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
319 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
320 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
321 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
322 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
323 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
324 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200325 }
326 else
327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_ECP_C)
329 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
332 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
333 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
334 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
335 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200336 }
337 else
338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000340
341 }
342 else if( opt.mode == MODE_PUBLIC )
343 {
344 /*
345 * 1.1. Load the key
346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000348 fflush( stdout );
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000351
352 if( ret != 0 )
353 {
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200354 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x", (unsigned int) -ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000355 goto exit;
356 }
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000359
360 /*
361 * 1.2 Print the key
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_RSA_C)
366 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100369
370 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
371 NULL, &E ) ) != 0 )
372 {
373 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
374 goto exit;
375 }
376 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
377 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200378 }
379 else
380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_ECP_C)
382 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
385 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
386 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
387 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200388 }
389 else
390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000392 }
393 else
394 goto usage;
395
396 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
397 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200398 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000399 }
400 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
401 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200402 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403 }
404
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100405 exit_code = MBEDTLS_EXIT_SUCCESS;
406
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000407exit:
408
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100409 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#ifdef MBEDTLS_ERROR_C
412 mbedtls_strerror( ret, buf, sizeof( buf ) );
413 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200414#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200416#endif
417 }
418
Hanno Becker40371ec2017-08-23 06:46:17 +0100419 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
420 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
421 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000424
425#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000427 fflush( stdout ); getchar();
428#endif
429
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200430 mbedtls_exit( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000431}
Hanno Beckerb14c3312018-10-16 13:45:22 +0100432#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */