blob: e556ba86cdab9e77cb0d9461728637779b094ce6 [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
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020034#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaaa3291e2018-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"
42#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000043
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <stdio.h>
45#include <string.h>
46#endif
Paul Bakkered27a042013-04-18 22:46:23 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000049#define USAGE_OUT \
50 " output_file=%%s default: keyfile.pem\n" \
51 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020052#else
Rich Evans18b78c72015-02-11 14:06:19 +000053#define USAGE_OUT \
54 " output_file=%%s default: keyfile.der\n" \
55 " output_format=der default: der\n"
56#endif
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000059#define DFL_OUTPUT_FILENAME "keyfile.pem"
60#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
61#else
62#define DFL_OUTPUT_FILENAME "keyfile.der"
63#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
64#endif
65
66#define DFL_MODE MODE_NONE
67#define DFL_FILENAME "keyfile.key"
68#define DFL_DEBUG_LEVEL 0
69#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020070
Paul Bakkerbdb912d2012-02-13 23:11:30 +000071#define MODE_NONE 0
72#define MODE_PRIVATE 1
73#define MODE_PUBLIC 2
74
75#define OUTPUT_MODE_NONE 0
76#define OUTPUT_MODE_PRIVATE 1
77#define OUTPUT_MODE_PUBLIC 2
78
Paul Bakkerf3df61a2013-08-26 17:22:23 +020079#define OUTPUT_FORMAT_PEM 0
80#define OUTPUT_FORMAT_DER 1
81
Rich Evans18b78c72015-02-11 14:06:19 +000082#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010083 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000084 "\n acceptable parameters:\n" \
85 " mode=private|public default: none\n" \
86 " filename=%%s default: keyfile.key\n" \
87 " output_mode=private|public default: none\n" \
88 USAGE_OUT \
89 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000090
Hanno Becker3a3f1aa2018-10-16 13:45:22 +010091#if !defined(MBEDTLS_PK_PARSE_C) || \
92 !defined(MBEDTLS_PK_WRITE_C) || \
93 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000094int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +000095{
Hanno Becker3a3f1aa2018-10-16 13:45:22 +010096 mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020097 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +000098}
99#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100/*
101 * global options
102 */
103struct options
104{
105 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200106 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200108 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200109 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110} opt;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200114 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000116 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200117 unsigned char *c = output_buf;
118 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119
Paul Bakker1d569582012-10-03 20:35:44 +0000120 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200123 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 return( ret );
127
128 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200131#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134 return( ret );
135
136 len = ret;
Ron Eldor9aff65a2018-01-07 18:10:43 +0200137 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200138 }
139
140 if( ( f = fopen( output_file, "w" ) ) == NULL )
141 return( -1 );
142
143 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200144 {
145 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200146 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200147 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148
Paul Bakker0c226102014-04-17 16:02:36 +0200149 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150
151 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200156 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000158 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 unsigned char *c = output_buf;
160 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161
Paul Bakker1d569582012-10-03 20:35:44 +0000162 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200165 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168 return( ret );
169
170 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200172 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200173#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200176 return( ret );
177
178 len = ret;
Christian Walther42aa4532018-11-28 13:32:27 +0100179 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200180 }
181
182 if( ( f = fopen( output_file, "w" ) ) == NULL )
183 return( -1 );
184
185 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200186 {
187 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200188 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200189 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200190
Paul Bakker0c226102014-04-17 16:02:36 +0200191 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200192
193 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194}
195
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196int main( int argc, char *argv[] )
197{
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100198 int ret = 1;
199 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000201 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 char *p, *q;
203
Hanno Becker40371ec2017-08-23 06:46:17 +0100204 mbedtls_pk_context key;
205 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
206
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207 /*
208 * Set to sane values
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200211 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212
Hanno Becker40371ec2017-08-23 06:46:17 +0100213 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
214 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
215 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
216
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217 if( argc == 0 )
218 {
219 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221 goto exit;
222 }
223
224 opt.mode = DFL_MODE;
225 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226 opt.output_mode = DFL_OUTPUT_MODE;
227 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200228 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229
230 for( i = 1; i < argc; i++ )
231 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 p = argv[i];
233 if( ( q = strchr( p, '=' ) ) == NULL )
234 goto usage;
235 *q++ = '\0';
236
237 if( strcmp( p, "mode" ) == 0 )
238 {
239 if( strcmp( q, "private" ) == 0 )
240 opt.mode = MODE_PRIVATE;
241 else if( strcmp( q, "public" ) == 0 )
242 opt.mode = MODE_PUBLIC;
243 else
244 goto usage;
245 }
246 else if( strcmp( p, "output_mode" ) == 0 )
247 {
248 if( strcmp( q, "private" ) == 0 )
249 opt.output_mode = OUTPUT_MODE_PRIVATE;
250 else if( strcmp( q, "public" ) == 0 )
251 opt.output_mode = OUTPUT_MODE_PUBLIC;
252 else
253 goto usage;
254 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200255 else if( strcmp( p, "output_format" ) == 0 )
256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200258 if( strcmp( q, "pem" ) == 0 )
259 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200260 else
261#endif
262 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200263 opt.output_format = OUTPUT_FORMAT_DER;
264 else
265 goto usage;
266 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000267 else if( strcmp( p, "filename" ) == 0 )
268 opt.filename = q;
269 else if( strcmp( p, "output_file" ) == 0 )
270 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271 else
272 goto usage;
273 }
274
275 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 goto exit;
279 }
280
281 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000284 goto exit;
285 }
286
287 if( opt.mode == MODE_PRIVATE )
288 {
289 /*
290 * 1.1. Load the key
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293 fflush( stdout );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296
297 if( ret != 0 )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
300 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000301 goto exit;
302 }
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000305
306 /*
307 * 1.2 Print the key
308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_RSA_C)
312 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100315
316 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
317 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
318 {
319 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
320 goto exit;
321 }
322
323 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
324 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
325 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
326 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
327 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
328 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
329 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
330 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200331 }
332 else
333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_ECP_C)
335 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
338 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
339 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
340 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
341 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200342 }
343 else
344#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000346
347 }
348 else if( opt.mode == MODE_PUBLIC )
349 {
350 /*
351 * 1.1. Load the key
352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000354 fflush( stdout );
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000357
358 if( ret != 0 )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
361 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000362 goto exit;
363 }
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000366
367 /*
368 * 1.2 Print the key
369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_RSA_C)
373 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100376
377 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
378 NULL, &E ) ) != 0 )
379 {
380 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
381 goto exit;
382 }
383 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
384 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200385 }
386 else
387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_ECP_C)
389 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
392 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
393 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
394 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200395 }
396 else
397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000399 }
400 else
401 goto usage;
402
403 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
404 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200405 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000406 }
407 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
408 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200409 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000410 }
411
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100412 exit_code = MBEDTLS_EXIT_SUCCESS;
413
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000414exit:
415
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100416 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#ifdef MBEDTLS_ERROR_C
419 mbedtls_strerror( ret, buf, sizeof( buf ) );
420 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200421#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200423#endif
424 }
425
Hanno Becker40371ec2017-08-23 06:46:17 +0100426 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
427 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
428 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000431
432#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000434 fflush( stdout ); getchar();
435#endif
436
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200437 mbedtls_exit( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000438}
Hanno Becker3a3f1aa2018-10-16 13:45:22 +0100439#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */