blob: cd0c230644e26083bcecb7e6f500a7d8e15398c4 [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 Garciaaa3291e2018-04-29 20:07:30 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/error.h"
40#include "mbedtls/pk.h"
41#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000042
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <stdio.h>
44#include <string.h>
45#endif
Paul Bakkered27a042013-04-18 22:46:23 +020046
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 USAGE_OUT \
49 " output_file=%%s default: keyfile.pem\n" \
50 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020051#else
Rich Evans18b78c72015-02-11 14:06:19 +000052#define USAGE_OUT \
53 " output_file=%%s default: keyfile.der\n" \
54 " output_format=der default: der\n"
55#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000058#define DFL_OUTPUT_FILENAME "keyfile.pem"
59#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
60#else
61#define DFL_OUTPUT_FILENAME "keyfile.der"
62#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
63#endif
64
65#define DFL_MODE MODE_NONE
66#define DFL_FILENAME "keyfile.key"
67#define DFL_DEBUG_LEVEL 0
68#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020069
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070#define MODE_NONE 0
71#define MODE_PRIVATE 1
72#define MODE_PUBLIC 2
73
74#define OUTPUT_MODE_NONE 0
75#define OUTPUT_MODE_PRIVATE 1
76#define OUTPUT_MODE_PUBLIC 2
77
Paul Bakkerf3df61a2013-08-26 17:22:23 +020078#define OUTPUT_FORMAT_PEM 0
79#define OUTPUT_FORMAT_DER 1
80
Rich Evans18b78c72015-02-11 14:06:19 +000081#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010082 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000083 "\n acceptable parameters:\n" \
84 " mode=private|public default: none\n" \
85 " filename=%%s default: keyfile.key\n" \
86 " output_mode=private|public default: none\n" \
87 USAGE_OUT \
88 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000089
Hanno Becker3a3f1aa2018-10-16 13:45:22 +010090#if !defined(MBEDTLS_PK_PARSE_C) || \
91 !defined(MBEDTLS_PK_WRITE_C) || \
92 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000093int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000094{
Hanno Becker3a3f1aa2018-10-16 13:45:22 +010095 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 +000096 return( 0 );
97}
98#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099/*
100 * global options
101 */
102struct options
103{
104 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200105 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200107 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200108 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109} opt;
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200113 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000115 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200116 unsigned char *c = output_buf;
117 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118
Paul Bakker1d569582012-10-03 20:35:44 +0000119 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200125 return( ret );
126
127 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200130#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200133 return( ret );
134
135 len = ret;
Ron Eldor9aff65a2018-01-07 18:10:43 +0200136 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200137 }
138
139 if( ( f = fopen( output_file, "w" ) ) == NULL )
140 return( -1 );
141
142 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200143 {
144 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200145 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200146 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147
Paul Bakker0c226102014-04-17 16:02:36 +0200148 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200149
150 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151}
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200155 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000157 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200158 unsigned char *c = output_buf;
159 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000160
Paul Bakker1d569582012-10-03 20:35:44 +0000161 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200167 return( ret );
168
169 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200172#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200175 return( ret );
176
177 len = ret;
178 c = output_buf + sizeof(output_buf) - len - 1;
179 }
180
181 if( ( f = fopen( output_file, "w" ) ) == NULL )
182 return( -1 );
183
184 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200185 {
186 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200187 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200188 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200189
Paul Bakker0c226102014-04-17 16:02:36 +0200190 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200191
192 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193}
194
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195int main( int argc, char *argv[] )
196{
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100197 int ret = 1;
198 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000200 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000201 char *p, *q;
202
Hanno Becker40371ec2017-08-23 06:46:17 +0100203 mbedtls_pk_context key;
204 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
205
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206 /*
207 * Set to sane values
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200210 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211
Hanno Becker40371ec2017-08-23 06:46:17 +0100212 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
213 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
214 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
215
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216 if( argc == 0 )
217 {
218 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 goto exit;
221 }
222
223 opt.mode = DFL_MODE;
224 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000225 opt.output_mode = DFL_OUTPUT_MODE;
226 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200227 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228
229 for( i = 1; i < argc; i++ )
230 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231 p = argv[i];
232 if( ( q = strchr( p, '=' ) ) == NULL )
233 goto usage;
234 *q++ = '\0';
235
236 if( strcmp( p, "mode" ) == 0 )
237 {
238 if( strcmp( q, "private" ) == 0 )
239 opt.mode = MODE_PRIVATE;
240 else if( strcmp( q, "public" ) == 0 )
241 opt.mode = MODE_PUBLIC;
242 else
243 goto usage;
244 }
245 else if( strcmp( p, "output_mode" ) == 0 )
246 {
247 if( strcmp( q, "private" ) == 0 )
248 opt.output_mode = OUTPUT_MODE_PRIVATE;
249 else if( strcmp( q, "public" ) == 0 )
250 opt.output_mode = OUTPUT_MODE_PUBLIC;
251 else
252 goto usage;
253 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200254 else if( strcmp( p, "output_format" ) == 0 )
255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200257 if( strcmp( q, "pem" ) == 0 )
258 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200259 else
260#endif
261 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200262 opt.output_format = OUTPUT_FORMAT_DER;
263 else
264 goto usage;
265 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266 else if( strcmp( p, "filename" ) == 0 )
267 opt.filename = q;
268 else if( strcmp( p, "output_file" ) == 0 )
269 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270 else
271 goto usage;
272 }
273
274 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277 goto exit;
278 }
279
280 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 goto exit;
284 }
285
286 if( opt.mode == MODE_PRIVATE )
287 {
288 /*
289 * 1.1. Load the key
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 fflush( stdout );
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
296 if( ret != 0 )
297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
299 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300 goto exit;
301 }
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000304
305 /*
306 * 1.2 Print the key
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_RSA_C)
311 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100314
315 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
316 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
317 {
318 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
319 goto exit;
320 }
321
322 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
323 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
324 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
325 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
326 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
327 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
328 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
329 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200330 }
331 else
332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_ECP_C)
334 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
337 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
338 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
339 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
340 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200341 }
342 else
343#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000345
346 }
347 else if( opt.mode == MODE_PUBLIC )
348 {
349 /*
350 * 1.1. Load the key
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000353 fflush( stdout );
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000356
357 if( ret != 0 )
358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
360 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000361 goto exit;
362 }
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000365
366 /*
367 * 1.2 Print the key
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_RSA_C)
372 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100375
376 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
377 NULL, &E ) ) != 0 )
378 {
379 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
380 goto exit;
381 }
382 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
383 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200384 }
385 else
386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_ECP_C)
388 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
391 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
392 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
393 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200394 }
395 else
396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000398 }
399 else
400 goto usage;
401
402 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
403 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200404 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000405 }
406 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
407 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200408 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000409 }
410
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100411 exit_code = MBEDTLS_EXIT_SUCCESS;
412
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000413exit:
414
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100415 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#ifdef MBEDTLS_ERROR_C
418 mbedtls_strerror( ret, buf, sizeof( buf ) );
419 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200420#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200422#endif
423 }
424
Hanno Becker40371ec2017-08-23 06:46:17 +0100425 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
426 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
427 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000430
431#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000433 fflush( stdout ); getchar();
434#endif
435
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100436 return( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000437}
Hanno Becker3a3f1aa2018-10-16 13:45:22 +0100438#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */