blob: 89c67ed9e687fc70cf8ac75a5c3945b1dfc3767a [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garciaed684882018-04-29 20:07:30 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaed684882018-04-29 20:07:30 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020037#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C) && \
38 defined(MBEDTLS_FS_IO) && \
39 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
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
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020044#include "mbedtls/entropy.h"
45#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <string.h>
49#endif
Paul Bakkered27a042013-04-18 22:46:23 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000052#define USAGE_OUT \
53 " output_file=%%s default: keyfile.pem\n" \
54 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#define USAGE_OUT \
57 " output_file=%%s default: keyfile.der\n" \
58 " output_format=der default: der\n"
59#endif
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000062#define DFL_OUTPUT_FILENAME "keyfile.pem"
63#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
64#else
65#define DFL_OUTPUT_FILENAME "keyfile.der"
66#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
67#endif
68
69#define DFL_MODE MODE_NONE
70#define DFL_FILENAME "keyfile.key"
71#define DFL_DEBUG_LEVEL 0
72#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020073
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074#define MODE_NONE 0
75#define MODE_PRIVATE 1
76#define MODE_PUBLIC 2
77
78#define OUTPUT_MODE_NONE 0
79#define OUTPUT_MODE_PRIVATE 1
80#define OUTPUT_MODE_PUBLIC 2
81
Paul Bakkerf3df61a2013-08-26 17:22:23 +020082#define OUTPUT_FORMAT_PEM 0
83#define OUTPUT_FORMAT_DER 1
84
Rich Evans18b78c72015-02-11 14:06:19 +000085#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010086 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000087 "\n acceptable parameters:\n" \
88 " mode=private|public default: none\n" \
89 " filename=%%s default: keyfile.key\n" \
90 " output_mode=private|public default: none\n" \
91 USAGE_OUT \
92 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000093
Hanno Beckerb14c3312018-10-16 13:45:22 +010094#if !defined(MBEDTLS_PK_PARSE_C) || \
95 !defined(MBEDTLS_PK_WRITE_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020096 !defined(MBEDTLS_FS_IO) || \
97 !defined(MBEDTLS_ENTROPY_C) || \
98 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000099int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000100{
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200101 mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or "
102 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
103 "MBEDTLS_FS_IO not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200104 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000105}
106#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000107
Simon Butcher63cb97e2018-12-06 17:43:31 +0000108
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000109/*
110 * global options
111 */
112struct options
113{
114 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200115 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200117 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000119} opt;
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121static int write_public_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000122{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200123 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000125 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 unsigned char *c = output_buf;
127 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128
Paul Bakker1d569582012-10-03 20:35:44 +0000129 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200132 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200135 return( ret );
136
137 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200140#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200143 return( ret );
144
145 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200146 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147 }
148
149 if( ( f = fopen( output_file, "w" ) ) == NULL )
150 return( -1 );
151
152 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200153 {
154 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200155 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200156 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200157
Paul Bakker0c226102014-04-17 16:02:36 +0200158 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159
160 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161}
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000164{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200165 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000167 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168 unsigned char *c = output_buf;
169 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000170
Paul Bakker1d569582012-10-03 20:35:44 +0000171 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200174 if( opt.output_format == OUTPUT_FORMAT_PEM )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200177 return( ret );
178
179 len = strlen( (char *) output_buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200181 else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200182#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200185 return( ret );
186
187 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100188 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200189 }
190
191 if( ( f = fopen( output_file, "w" ) ) == NULL )
192 return( -1 );
193
194 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200195 {
196 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200197 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200198 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200199
Paul Bakker0c226102014-04-17 16:02:36 +0200200 fclose( f );
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200201
202 return( 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203}
204
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205int main( int argc, char *argv[] )
206{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100207 int ret = 1;
208 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209 char buf[1024];
Paul Bakker1d569582012-10-03 20:35:44 +0000210 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211 char *p, *q;
212
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200213 const char *pers = "pkey/key_app";
214 mbedtls_entropy_context entropy;
215 mbedtls_ctr_drbg_context ctr_drbg;
216
Hanno Becker40371ec2017-08-23 06:46:17 +0100217 mbedtls_pk_context key;
218 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
219
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 /*
221 * Set to sane values
222 */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200223 mbedtls_entropy_init( &entropy );
224 mbedtls_ctr_drbg_init( &ctr_drbg );
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200227 memset( buf, 0, sizeof( buf ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000228
Hanno Becker40371ec2017-08-23 06:46:17 +0100229 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
230 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
231 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
232
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233 if( argc == 0 )
234 {
235 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( USAGE );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 goto exit;
238 }
239
240 opt.mode = DFL_MODE;
241 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 opt.output_mode = DFL_OUTPUT_MODE;
243 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200244 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
246 for( i = 1; i < argc; i++ )
247 {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000248 p = argv[i];
249 if( ( q = strchr( p, '=' ) ) == NULL )
250 goto usage;
251 *q++ = '\0';
252
253 if( strcmp( p, "mode" ) == 0 )
254 {
255 if( strcmp( q, "private" ) == 0 )
256 opt.mode = MODE_PRIVATE;
257 else if( strcmp( q, "public" ) == 0 )
258 opt.mode = MODE_PUBLIC;
259 else
260 goto usage;
261 }
262 else if( strcmp( p, "output_mode" ) == 0 )
263 {
264 if( strcmp( q, "private" ) == 0 )
265 opt.output_mode = OUTPUT_MODE_PRIVATE;
266 else if( strcmp( q, "public" ) == 0 )
267 opt.output_mode = OUTPUT_MODE_PUBLIC;
268 else
269 goto usage;
270 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200271 else if( strcmp( p, "output_format" ) == 0 )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200274 if( strcmp( q, "pem" ) == 0 )
275 opt.output_format = OUTPUT_FORMAT_PEM;
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200276 else
277#endif
278 if( strcmp( q, "der" ) == 0 )
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200279 opt.output_format = OUTPUT_FORMAT_DER;
280 else
281 goto usage;
282 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 else if( strcmp( p, "filename" ) == 0 )
284 opt.filename = q;
285 else if( strcmp( p, "output_file" ) == 0 )
286 opt.output_file = q;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287 else
288 goto usage;
289 }
290
291 if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( "\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294 goto exit;
295 }
296
297 if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_printf( "\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300 goto exit;
301 }
302
303 if( opt.mode == MODE_PRIVATE )
304 {
305 /*
306 * 1.1. Load the key
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 fflush( stdout );
310
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200311 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
312 (const unsigned char *) pers,
313 strlen( pers ) ) ) != 0 )
314 {
315 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
316 goto exit;
317 }
318
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200319 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL,
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200320 mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000321 if( ret != 0 )
322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200324 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000325 goto exit;
326 }
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000329
330 /*
331 * 1.2 Print the key
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_RSA_C)
336 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100339
340 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
341 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
342 {
343 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
344 goto exit;
345 }
346
347 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
348 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
349 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
350 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
351 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
352 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
353 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
354 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200355 }
356 else
357#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_ECP_C)
359 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200362 mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL );
363 mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL );
364 mbedtls_mpi_write_file( "Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL );
365 mbedtls_mpi_write_file( "D : ", &ecp->MBEDTLS_PRIVATE(d) , 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200366 }
367 else
368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000370
371 }
372 else if( opt.mode == MODE_PUBLIC )
373 {
374 /*
375 * 1.1. Load the key
376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000378 fflush( stdout );
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000381
382 if( ret != 0 )
383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200385 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", (unsigned int) -ret, buf );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000386 goto exit;
387 }
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_printf( " ok\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000390
391 /*
392 * 1.2 Print the key
393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_printf( " . Key information ...\n" );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_RSA_C)
397 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker40371ec2017-08-23 06:46:17 +0100400
401 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
402 NULL, &E ) ) != 0 )
403 {
404 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
405 goto exit;
406 }
407 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
408 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200409 }
410 else
411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_ECP_C)
413 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker2e24ca72013-09-18 15:21:27 +0200414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200416 mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL );
417 mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL );
418 mbedtls_mpi_write_file( "Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200419 }
420 else
421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000423 }
424 else
425 goto usage;
426
427 if( opt.output_mode == OUTPUT_MODE_PUBLIC )
428 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200429 write_public_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000430 }
431 if( opt.output_mode == OUTPUT_MODE_PRIVATE )
432 {
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200433 write_private_key( &key, opt.output_file );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000434 }
435
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100436 exit_code = MBEDTLS_EXIT_SUCCESS;
437
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000438exit:
439
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100440 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#ifdef MBEDTLS_ERROR_C
443 mbedtls_strerror( ret, buf, sizeof( buf ) );
444 mbedtls_printf( " - %s\n", buf );
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200445#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200447#endif
448 }
449
Hanno Becker40371ec2017-08-23 06:46:17 +0100450 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
451 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
452 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_pk_free( &key );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000455
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200456 mbedtls_ctr_drbg_free( &ctr_drbg );
457 mbedtls_entropy_free( &entropy );
458
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000459#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000461 fflush( stdout ); getchar();
462#endif
463
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200464 mbedtls_exit( exit_code );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000465}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200466#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO &&
467 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */