blob: c440932f92a37a762c45e5037a89e13c62fea5b3 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
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 Bakker15b9b3a2013-09-23 12:05:44 +020018 */
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 Bakker15b9b3a2013-09-23 12:05:44 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
29 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/error.h"
31#include "mbedtls/pk.h"
32#include "mbedtls/ecdsa.h"
33#include "mbedtls/rsa.h"
34#include "mbedtls/error.h"
35#include "mbedtls/entropy.h"
36#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020037
Rich Evans18b78c72015-02-11 14:06:19 +000038#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020041
Rich Evans18b78c72015-02-11 14:06:19 +000042#if !defined(_WIN32)
43#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020044
45#define DEV_RANDOM_THRESHOLD 32
46
47int dev_random_entropy_poll( void *data, unsigned char *output,
48 size_t len, size_t *olen )
49{
50 FILE *file;
51 size_t ret, left = len;
52 unsigned char *p = output;
53 ((void) data);
54
55 *olen = 0;
56
57 file = fopen( "/dev/random", "rb" );
58 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020060
61 while( left > 0 )
62 {
63 /* /dev/random can return much less than requested. If so, try again */
64 ret = fread( p, 1, left, file );
65 if( ret == 0 && ferror( file ) )
66 {
67 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020069 }
70
71 p += ret;
72 left -= ret;
73 sleep( 1 );
74 }
75 fclose( file );
76 *olen = len;
77
78 return( 0 );
79}
Rich Evans18b78c72015-02-11 14:06:19 +000080#endif /* !_WIN32 */
81#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_ECP_C)
84#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000085#else
86#define DFL_EC_CURVE 0
87#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000090#define USAGE_DEV_RANDOM \
91 " use_dev_random=0|1 default: 0\n"
92#else
93#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020095
Rich Evans18b78c72015-02-11 14:06:19 +000096#define FORMAT_PEM 0
97#define FORMAT_DER 1
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000100#define DFL_RSA_KEYSIZE 4096
101#define DFL_FILENAME "keyfile.key"
102#define DFL_FORMAT FORMAT_PEM
103#define DFL_USE_DEV_RANDOM 0
104
105#define USAGE \
106 "\n usage: gen_key param=<>...\n" \
107 "\n acceptable parameters:\n" \
108 " type=rsa|ec default: rsa\n" \
109 " rsa_keysize=%%d default: 4096\n" \
110 " ec_curve=%%s see below\n" \
111 " filename=%%s default: keyfile.key\n" \
112 " format=pem|der default: pem\n" \
113 USAGE_DEV_RANDOM \
114 "\n"
115
Simon Butcher604d3992016-10-06 19:02:49 +0100116#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
117 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
118 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000119int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher604d3992016-10-06 19:02:49 +0100122 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
123 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000124 "not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200125 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000126}
127#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000128
Simon Butcher63cb97e2018-12-06 17:43:31 +0000129
Rich Evans18b78c72015-02-11 14:06:19 +0000130/*
131 * global options
132 */
133struct options
134{
135 int type; /* the type of key to generate */
136 int rsa_keysize; /* length of key in bits */
137 int ec_curve; /* curve identifier for EC keys */
138 const char *filename; /* filename of the key file */
139 int format; /* the output format to use */
140 int use_dev_random; /* use /dev/random as entropy source */
141} opt;
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200144{
145 int ret;
146 FILE *f;
147 unsigned char output_buf[16000];
148 unsigned char *c = output_buf;
149 size_t len = 0;
150
151 memset(output_buf, 0, 16000);
152 if( opt.format == FORMAT_PEM )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200155 return( ret );
156
157 len = strlen( (char *) output_buf );
158 }
159 else
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200162 return( ret );
163
164 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200165 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200166 }
167
Paul Bakker3966d712014-07-10 12:03:09 +0200168 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200169 return( -1 );
170
171 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200172 {
173 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200174 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200175 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200176
Paul Bakker0c226102014-04-17 16:02:36 +0200177 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178
179 return( 0 );
180}
181
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200182int main( int argc, char *argv[] )
183{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100184 int ret = 1;
185 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187 char buf[1024];
188 int i;
189 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100190 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_entropy_context entropy;
192 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200193 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_ECP_C)
195 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100196#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200197
198 /*
199 * Set to sane values
200 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100201
202 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
203 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
204 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200207 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200208 memset( buf, 0, sizeof( buf ) );
209
210 if( argc == 0 )
211 {
212 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_printf( USAGE );
214#if defined(MBEDTLS_ECP_C)
James Cowgill07a92d72015-10-09 00:28:14 +0100215 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 curve_info = mbedtls_ecp_curve_list();
217 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100218 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( " %s\n", curve_info->name );
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100220#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200221 goto exit;
222 }
223
224 opt.type = DFL_TYPE;
225 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100226 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 opt.filename = DFL_FILENAME;
228 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200229 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200230
231 for( i = 1; i < argc; i++ )
232 {
233 p = argv[i];
234 if( ( q = strchr( p, '=' ) ) == NULL )
235 goto usage;
236 *q++ = '\0';
237
238 if( strcmp( p, "type" ) == 0 )
239 {
240 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100242 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200244 else
245 goto usage;
246 }
247 else if( strcmp( p, "format" ) == 0 )
248 {
249 if( strcmp( q, "pem" ) == 0 )
250 opt.format = FORMAT_PEM;
251 else if( strcmp( q, "der" ) == 0 )
252 opt.format = FORMAT_DER;
253 else
254 goto usage;
255 }
256 else if( strcmp( p, "rsa_keysize" ) == 0 )
257 {
258 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200259 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200261 goto usage;
262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100264 else if( strcmp( p, "ec_curve" ) == 0 )
265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100267 goto usage;
268 opt.ec_curve = curve_info->grp_id;
269 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200270#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200271 else if( strcmp( p, "filename" ) == 0 )
272 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200273 else if( strcmp( p, "use_dev_random" ) == 0 )
274 {
275 opt.use_dev_random = atoi( q );
276 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
277 goto usage;
278 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200279 else
280 goto usage;
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284 fflush( stdout );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_entropy_init( &entropy );
287#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200288 if( opt.use_dev_random )
289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200291 NULL, DEV_RANDOM_THRESHOLD,
292 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200293 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200294 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200295 goto exit;
296 }
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200299 fflush( stdout );
300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200302
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200303 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200304 (const unsigned char *) pers,
305 strlen( pers ) ) ) != 0 )
306 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200307 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200308 goto exit;
309 }
310
311 /*
312 * 1.1. Generate the key
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200315 fflush( stdout );
316
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000317 if( ( ret = mbedtls_pk_setup( &key,
318 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200319 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200320 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100321 goto exit;
322 }
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
325 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker83aad1f2017-08-23 06:45:10 +0100328 opt.rsa_keysize, 65537 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200329 if( ret != 0 )
330 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200331 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200332 goto exit;
333 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200334 }
335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_RSA_C */
337#if defined(MBEDTLS_ECP_C)
338 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200339 {
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000340 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
341 mbedtls_pk_ec( key ),
Hanno Becker83aad1f2017-08-23 06:45:10 +0100342 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100343 if( ret != 0 )
344 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200345 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100346 goto exit;
347 }
348 }
349 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200353 goto exit;
354 }
355
356 /*
357 * 1.2 Print the key
358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_RSA_C)
362 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker83aad1f2017-08-23 06:45:10 +0100365
366 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
367 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
368 {
369 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
370 goto exit;
371 }
372
373 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
374 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
375 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
376 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
377 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
378 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
379 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
380 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200381 }
382 else
383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ECP_C)
385 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
388 mbedtls_printf( "curve: %s\n",
389 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
390 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
391 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
392 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200393 }
394 else
395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200397
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200398 /*
399 * 1.3 Export key
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200402
403 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200406 goto exit;
407 }
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200410
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100411 exit_code = MBEDTLS_EXIT_SUCCESS;
412
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200413exit:
414
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100415 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +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 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200420#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200422#endif
423 }
424
Hanno Becker83aad1f2017-08-23 06:45:10 +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 );
430 mbedtls_ctr_drbg_free( &ctr_drbg );
431 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200432
433#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200435 fflush( stdout ); getchar();
436#endif
437
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200438 mbedtls_exit( exit_code );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200439}
Simon Butcher604d3992016-10-06 19:02:49 +0100440#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
441 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */