blob: d09755bb84276ce92ee17d44eaf05584a87b48c9 [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úti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkerbdb912d2012-02-13 23:11:30 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/error.h"
66#include "mbedtls/pk.h"
67#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
Rich Evans18b78c72015-02-11 14:06:19 +000069#include <stdio.h>
70#include <string.h>
71#endif
Paul Bakkered27a042013-04-18 22:46:23 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000074#define USAGE_OUT \
75 " output_file=%%s default: keyfile.pem\n" \
76 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020077#else
Rich Evans18b78c72015-02-11 14:06:19 +000078#define USAGE_OUT \
79 " output_file=%%s default: keyfile.der\n" \
80 " output_format=der default: der\n"
81#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000084#define DFL_OUTPUT_FILENAME "keyfile.pem"
85#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
86#else
87#define DFL_OUTPUT_FILENAME "keyfile.der"
88#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
89#endif
90
91#define DFL_MODE MODE_NONE
92#define DFL_FILENAME "keyfile.key"
93#define DFL_DEBUG_LEVEL 0
94#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020095
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096#define MODE_NONE 0
97#define MODE_PRIVATE 1
98#define MODE_PUBLIC 2
99
100#define OUTPUT_MODE_NONE 0
101#define OUTPUT_MODE_PRIVATE 1
102#define OUTPUT_MODE_PUBLIC 2
103
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200104#define OUTPUT_FORMAT_PEM 0
105#define OUTPUT_FORMAT_DER 1
106
Rich Evans18b78c72015-02-11 14:06:19 +0000107#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +0100108 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n acceptable parameters:\n" \
110 " mode=private|public default: none\n" \
111 " filename=%%s default: keyfile.key\n" \
112 " output_mode=private|public default: none\n" \
113 USAGE_OUT \
114 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000115
Hanno Becker3a3f1aa2018-10-16 13:45:22 +0100116#if !defined(MBEDTLS_PK_PARSE_C) || \
117 !defined(MBEDTLS_PK_WRITE_C) || \
118 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000119int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000120{
Hanno Becker3a3f1aa2018-10-16 13:45:22 +0100121 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 +0200122 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000123}
124#else
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125/*
126 * global options
127 */
128struct options
129{
130 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200131 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200133 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135} opt;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000141 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200142 unsigned char *c = output_buf;
143 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
Paul Bakker1d569582012-10-03 20:35:44 +0000145 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200151 return( ret );
152
153 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200155 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200156#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 return( ret );
160
161 len = ret;
Ron Eldor9aff65a2018-01-07 18:10:43 +0200162 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163 }
164
165 if( ( f = fopen( output_file, "w" ) ) == NULL )
166 return( -1 );
167
168 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200169 {
170 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200172 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200173
Paul Bakker0c226102014-04-17 16:02:36 +0200174 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200175
176 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200181 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000183 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200184 unsigned char *c = output_buf;
185 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186
Paul Bakker1d569582012-10-03 20:35:44 +0000187 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200190 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200193 return( ret );
194
195 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200197 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200198#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200201 return( ret );
202
203 len = ret;
Christian Walther42aa4532018-11-28 13:32:27 +0100204 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200205 }
206
207 if( ( f = fopen( output_file, "w" ) ) == NULL )
208 return( -1 );
209
210 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200211 {
212 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200213 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200214 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200215
Paul Bakker0c226102014-04-17 16:02:36 +0200216 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200217
218 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000219}
220
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000221int main( int argc, char *argv[] )
222{
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100223 int ret = 1;
224 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000225 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000226 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227 char *p, *q;
228
Hanno Becker40371ec2017-08-23 06:46:17 +0100229 mbedtls_pk_context key;
230 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
231
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 /*
233 * Set to sane values
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200236 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237
Hanno Becker40371ec2017-08-23 06:46:17 +0100238 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
239 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
240 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
241
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 if( argc == 0 )
243 {
244 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000246 goto exit;
247 }
248
249 opt.mode = DFL_MODE;
250 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000251 opt.output_mode = DFL_OUTPUT_MODE;
252 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200253 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254
255 for( i = 1; i < argc; i++ )
256 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257 p = argv[i];
258 if( ( q = strchr( p, '=' ) ) == NULL )
259 goto usage;
260 *q++ = '\0';
261
262 if( strcmp( p, "mode" ) == 0 )
263 {
264 if( strcmp( q, "private" ) == 0 )
265 opt.mode = MODE_PRIVATE;
266 else if( strcmp( q, "public" ) == 0 )
267 opt.mode = MODE_PUBLIC;
268 else
269 goto usage;
270 }
271 else if( strcmp( p, "output_mode" ) == 0 )
272 {
273 if( strcmp( q, "private" ) == 0 )
274 opt.output_mode = OUTPUT_MODE_PRIVATE;
275 else if( strcmp( q, "public" ) == 0 )
276 opt.output_mode = OUTPUT_MODE_PUBLIC;
277 else
278 goto usage;
279 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200280 else if( strcmp( p, "output_format" ) == 0 )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200283 if( strcmp( q, "pem" ) == 0 )
284 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200285 else
286#endif
287 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200288 opt.output_format = OUTPUT_FORMAT_DER;
289 else
290 goto usage;
291 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 else if( strcmp( p, "filename" ) == 0 )
293 opt.filename = q;
294 else if( strcmp( p, "output_file" ) == 0 )
295 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296 else
297 goto usage;
298 }
299
300 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000303 goto exit;
304 }
305
306 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 goto exit;
310 }
311
312 if( opt.mode == MODE_PRIVATE )
313 {
314 /*
315 * 1.1. Load the key
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000318 fflush( stdout );
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000321
322 if( ret != 0 )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
325 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000326 goto exit;
327 }
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000330
331 /*
332 * 1.2 Print the key
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_RSA_C)
337 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100340
341 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
342 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
343 {
344 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
345 goto exit;
346 }
347
348 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
349 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
350 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
351 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
352 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
353 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
354 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
355 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200356 }
357 else
358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_ECP_C)
360 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
363 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
364 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
365 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
366 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200367 }
368 else
369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000371
372 }
373 else if( opt.mode == MODE_PUBLIC )
374 {
375 /*
376 * 1.1. Load the key
377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000379 fflush( stdout );
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000382
383 if( ret != 0 )
384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
386 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000387 goto exit;
388 }
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000391
392 /*
393 * 1.2 Print the key
394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_RSA_C)
398 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100401
402 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
403 NULL, &E ) ) != 0 )
404 {
405 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
406 goto exit;
407 }
408 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
409 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200410 }
411 else
412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_ECP_C)
414 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
417 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
418 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
419 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200420 }
421 else
422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000424 }
425 else
426 goto usage;
427
428 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
429 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200430 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000431 }
432 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
433 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200434 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000435 }
436
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100437 exit_code = MBEDTLS_EXIT_SUCCESS;
438
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000439exit:
440
Andres Amaya Garciaaa3291e2018-04-29 20:07:30 +0100441 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#ifdef MBEDTLS_ERROR_C
444 mbedtls_strerror( ret, buf, sizeof( buf ) );
445 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200446#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200448#endif
449 }
450
Hanno Becker40371ec2017-08-23 06:46:17 +0100451 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
452 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
453 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000456
457#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000459 fflush( stdout ); getchar();
460#endif
461
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200462 mbedtls_exit( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000463}
Hanno Becker3a3f1aa2018-10-16 13:45:22 +0100464#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */