blob: 1891eb47f7256d2a726cd69b7e29403a036a648a [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
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 Bakker15b9b3a2013-09-23 12:05:44 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker15b9b3a2013-09-23 12:05:44 +020020 */
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 Bakker15b9b3a2013-09-23 12:05:44 +020027
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 Garcia208c2172018-04-29 19:51:56 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia208c2172018-04-29 19:51:56 +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) && \
40 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/error.h"
42#include "mbedtls/pk.h"
43#include "mbedtls/ecdsa.h"
44#include "mbedtls/rsa.h"
45#include "mbedtls/error.h"
46#include "mbedtls/entropy.h"
47#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020048
Rich Evans18b78c72015-02-11 14:06:19 +000049#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020052
Rich Evans18b78c72015-02-11 14:06:19 +000053#if !defined(_WIN32)
54#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020055
56#define DEV_RANDOM_THRESHOLD 32
57
58int dev_random_entropy_poll( void *data, unsigned char *output,
59 size_t len, size_t *olen )
60{
61 FILE *file;
62 size_t ret, left = len;
63 unsigned char *p = output;
64 ((void) data);
65
66 *olen = 0;
67
68 file = fopen( "/dev/random", "rb" );
69 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020071
72 while( left > 0 )
73 {
74 /* /dev/random can return much less than requested. If so, try again */
75 ret = fread( p, 1, left, file );
76 if( ret == 0 && ferror( file ) )
77 {
78 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020080 }
81
82 p += ret;
83 left -= ret;
84 sleep( 1 );
85 }
86 fclose( file );
87 *olen = len;
88
89 return( 0 );
90}
Rich Evans18b78c72015-02-11 14:06:19 +000091#endif /* !_WIN32 */
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_ECP_C)
95#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000096#else
97#define DFL_EC_CURVE 0
98#endif
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +0000101#define USAGE_DEV_RANDOM \
102 " use_dev_random=0|1 default: 0\n"
103#else
104#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200106
Rich Evans18b78c72015-02-11 14:06:19 +0000107#define FORMAT_PEM 0
108#define FORMAT_DER 1
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000111#define DFL_RSA_KEYSIZE 4096
112#define DFL_FILENAME "keyfile.key"
113#define DFL_FORMAT FORMAT_PEM
114#define DFL_USE_DEV_RANDOM 0
115
116#define USAGE \
117 "\n usage: gen_key param=<>...\n" \
118 "\n acceptable parameters:\n" \
119 " type=rsa|ec default: rsa\n" \
120 " rsa_keysize=%%d default: 4096\n" \
121 " ec_curve=%%s see below\n" \
122 " filename=%%s default: keyfile.key\n" \
123 " format=pem|der default: pem\n" \
124 USAGE_DEV_RANDOM \
125 "\n"
126
Simon Butcher604d3992016-10-06 19:02:49 +0100127#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
128 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
129 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000130int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher604d3992016-10-06 19:02:49 +0100133 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
134 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000135 "not defined.\n" );
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400136 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000137}
138#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000139
Jarno Lamsa642596e2019-10-04 12:52:42 +0300140#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
141int mbedtls_hardware_poll( void *data, unsigned char *output,
142 size_t len, size_t *olen )
143{
144 size_t i;
145 (void) data;
146 for( i = 0; i < len; ++i )
147 output[i] = rand();
148 *olen = len;
149 return( 0 );
150}
151#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +0000152
Rich Evans18b78c72015-02-11 14:06:19 +0000153/*
154 * global options
155 */
156struct options
157{
158 int type; /* the type of key to generate */
159 int rsa_keysize; /* length of key in bits */
160 int ec_curve; /* curve identifier for EC keys */
161 const char *filename; /* filename of the key file */
162 int format; /* the output format to use */
163 int use_dev_random; /* use /dev/random as entropy source */
164} opt;
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200167{
168 int ret;
169 FILE *f;
170 unsigned char output_buf[16000];
171 unsigned char *c = output_buf;
172 size_t len = 0;
173
174 memset(output_buf, 0, 16000);
175 if( opt.format == FORMAT_PEM )
176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178 return( ret );
179
180 len = strlen( (char *) output_buf );
181 }
182 else
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200185 return( ret );
186
187 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200188 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200189 }
190
Paul Bakker3966d712014-07-10 12:03:09 +0200191 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200192 return( -1 );
193
194 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200195 {
196 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200197 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200198 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200199
Paul Bakker0c226102014-04-17 16:02:36 +0200200 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200201
202 return( 0 );
203}
204
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200205int main( int argc, char *argv[] )
206{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100207 int ret = 1;
208 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200210 char buf[1024];
211 int i;
212 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100213 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_entropy_context entropy;
215 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200216 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_ECP_C)
218 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100219#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200220
221 /*
222 * Set to sane values
223 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100224
225 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
226 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
227 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200230 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200231 memset( buf, 0, sizeof( buf ) );
232
233 if( argc == 0 )
234 {
235 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( USAGE );
237#if defined(MBEDTLS_ECP_C)
James Cowgill07a92d72015-10-09 00:28:14 +0100238 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 curve_info = mbedtls_ecp_curve_list();
240 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100241 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " %s\n", curve_info->name );
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100243#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200244 goto exit;
245 }
246
247 opt.type = DFL_TYPE;
248 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100249 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200250 opt.filename = DFL_FILENAME;
251 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200252 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253
254 for( i = 1; i < argc; i++ )
255 {
256 p = argv[i];
257 if( ( q = strchr( p, '=' ) ) == NULL )
258 goto usage;
259 *q++ = '\0';
260
261 if( strcmp( p, "type" ) == 0 )
262 {
263 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100265 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200267 else
268 goto usage;
269 }
270 else if( strcmp( p, "format" ) == 0 )
271 {
272 if( strcmp( q, "pem" ) == 0 )
273 opt.format = FORMAT_PEM;
274 else if( strcmp( q, "der" ) == 0 )
275 opt.format = FORMAT_DER;
276 else
277 goto usage;
278 }
279 else if( strcmp( p, "rsa_keysize" ) == 0 )
280 {
281 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200282 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284 goto usage;
285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100287 else if( strcmp( p, "ec_curve" ) == 0 )
288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100290 goto usage;
291 opt.ec_curve = curve_info->grp_id;
292 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200293#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200294 else if( strcmp( p, "filename" ) == 0 )
295 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200296 else if( strcmp( p, "use_dev_random" ) == 0 )
297 {
298 opt.use_dev_random = atoi( q );
299 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
300 goto usage;
301 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 else
303 goto usage;
304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 fflush( stdout );
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_entropy_init( &entropy );
310#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200311 if( opt.use_dev_random )
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200314 NULL, DEV_RANDOM_THRESHOLD,
315 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200318 goto exit;
319 }
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200322 fflush( stdout );
323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200325
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200326 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200327 (const unsigned char *) pers,
328 strlen( pers ) ) ) != 0 )
329 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200330 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200331 goto exit;
332 }
333
334 /*
335 * 1.1. Generate the key
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 fflush( stdout );
339
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000340 if( ( ret = mbedtls_pk_setup( &key,
341 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200342 {
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200343 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100344 goto exit;
345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
348 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker83aad1f2017-08-23 06:45:10 +0100351 opt.rsa_keysize, 65537 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200352 if( ret != 0 )
353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200355 goto exit;
356 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200357 }
358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_RSA_C */
360#if defined(MBEDTLS_ECP_C)
361 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200362 {
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000363 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
364 mbedtls_pk_ec( key ),
Hanno Becker83aad1f2017-08-23 06:45:10 +0100365 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100366 if( ret != 0 )
367 {
Chris Xue9a51c032017-11-05 19:10:51 +0000368 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100369 goto exit;
370 }
371 }
372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200376 goto exit;
377 }
378
379 /*
380 * 1.2 Print the key
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_RSA_C)
385 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker83aad1f2017-08-23 06:45:10 +0100388
389 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
390 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
391 {
392 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
393 goto exit;
394 }
395
396 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
397 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
398 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
399 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
400 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
401 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
402 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
403 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200404 }
405 else
406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_ECP_C)
408 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
411 mbedtls_printf( "curve: %s\n",
412 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
413 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
414 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
415 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200416 }
417 else
418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200420
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200421 /*
422 * 1.3 Export key
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200425
426 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200429 goto exit;
430 }
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200433
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100434 exit_code = MBEDTLS_EXIT_SUCCESS;
435
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200436exit:
437
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100438 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#ifdef MBEDTLS_ERROR_C
441 mbedtls_strerror( ret, buf, sizeof( buf ) );
442 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200443#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200445#endif
446 }
447
Hanno Becker83aad1f2017-08-23 06:45:10 +0100448 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
449 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
450 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_pk_free( &key );
453 mbedtls_ctr_drbg_free( &ctr_drbg );
454 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200455
456#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200458 fflush( stdout ); getchar();
459#endif
460
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400461 mbedtls_exit( exit_code );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200462}
Simon Butcher604d3992016-10-06 19:02:49 +0100463#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
464 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */