blob: 4043dfa6e038d575fe92c58c3656cc9bc113c1c1 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garcia208c2172018-04-29 19:51:56 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia208c2172018-04-29 19:51:56 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
34 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/error.h"
36#include "mbedtls/pk.h"
37#include "mbedtls/ecdsa.h"
38#include "mbedtls/rsa.h"
39#include "mbedtls/error.h"
40#include "mbedtls/entropy.h"
41#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020042
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020046
Rich Evans18b78c72015-02-11 14:06:19 +000047#if !defined(_WIN32)
48#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020049
50#define DEV_RANDOM_THRESHOLD 32
51
52int dev_random_entropy_poll( void *data, unsigned char *output,
53 size_t len, size_t *olen )
54{
55 FILE *file;
56 size_t ret, left = len;
57 unsigned char *p = output;
58 ((void) data);
59
60 *olen = 0;
61
62 file = fopen( "/dev/random", "rb" );
63 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020065
66 while( left > 0 )
67 {
68 /* /dev/random can return much less than requested. If so, try again */
69 ret = fread( p, 1, left, file );
70 if( ret == 0 && ferror( file ) )
71 {
72 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020074 }
75
76 p += ret;
77 left -= ret;
78 sleep( 1 );
79 }
80 fclose( file );
81 *olen = len;
82
83 return( 0 );
84}
Rich Evans18b78c72015-02-11 14:06:19 +000085#endif /* !_WIN32 */
86#endif
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_ECP_C)
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +020089#define DFL_EC_CURVE mbedtls_ecp_curve_list()->MBEDTLS_PRIVATE(grp_id)
Rich Evans18b78c72015-02-11 14:06:19 +000090#else
91#define DFL_EC_CURVE 0
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000095#define USAGE_DEV_RANDOM \
96 " use_dev_random=0|1 default: 0\n"
97#else
98#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200100
Rich Evans18b78c72015-02-11 14:06:19 +0000101#define FORMAT_PEM 0
102#define FORMAT_DER 1
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000105#define DFL_RSA_KEYSIZE 4096
106#define DFL_FILENAME "keyfile.key"
107#define DFL_FORMAT FORMAT_PEM
108#define DFL_USE_DEV_RANDOM 0
109
110#define USAGE \
111 "\n usage: gen_key param=<>...\n" \
112 "\n acceptable parameters:\n" \
113 " type=rsa|ec default: rsa\n" \
114 " rsa_keysize=%%d default: 4096\n" \
115 " ec_curve=%%s see below\n" \
116 " filename=%%s default: keyfile.key\n" \
117 " format=pem|der default: pem\n" \
118 USAGE_DEV_RANDOM \
119 "\n"
120
Simon Butcher604d3992016-10-06 19:02:49 +0100121#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
122 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
123 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000124int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher604d3992016-10-06 19:02:49 +0100127 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
128 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000129 "not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200130 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000131}
132#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000133
Simon Butcher63cb97e2018-12-06 17:43:31 +0000134
Rich Evans18b78c72015-02-11 14:06:19 +0000135/*
136 * global options
137 */
138struct options
139{
140 int type; /* the type of key to generate */
141 int rsa_keysize; /* length of key in bits */
142 int ec_curve; /* curve identifier for EC keys */
143 const char *filename; /* filename of the key file */
144 int format; /* the output format to use */
145 int use_dev_random; /* use /dev/random as entropy source */
146} opt;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200149{
150 int ret;
151 FILE *f;
152 unsigned char output_buf[16000];
153 unsigned char *c = output_buf;
154 size_t len = 0;
155
156 memset(output_buf, 0, 16000);
157 if( opt.format == FORMAT_PEM )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200160 return( ret );
161
162 len = strlen( (char *) output_buf );
163 }
164 else
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200167 return( ret );
168
169 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200170 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200171 }
172
Paul Bakker3966d712014-07-10 12:03:09 +0200173 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200174 return( -1 );
175
176 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200177 {
178 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200179 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200180 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200181
Paul Bakker0c226102014-04-17 16:02:36 +0200182 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200183
184 return( 0 );
185}
186
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187int main( int argc, char *argv[] )
188{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100189 int ret = 1;
190 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200192 char buf[1024];
193 int i;
194 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100195 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_entropy_context entropy;
197 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200198 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_ECP_C)
200 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100201#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200202
203 /*
204 * Set to sane values
205 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100206
207 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
208 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
209 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200212 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200213 memset( buf, 0, sizeof( buf ) );
214
215 if( argc == 0 )
216 {
217 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( USAGE );
219#if defined(MBEDTLS_ECP_C)
James Cowgill07a92d72015-10-09 00:28:14 +0100220 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 curve_info = mbedtls_ecp_curve_list();
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200222 mbedtls_printf( " %s (default)\n", curve_info->MBEDTLS_PRIVATE(name) );
223 while( ( ++curve_info )->MBEDTLS_PRIVATE(name) != NULL )
224 mbedtls_printf( " %s\n", curve_info->MBEDTLS_PRIVATE(name) );
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100225#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200226 goto exit;
227 }
228
229 opt.type = DFL_TYPE;
230 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100231 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200232 opt.filename = DFL_FILENAME;
233 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200234 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200235
236 for( i = 1; i < argc; i++ )
237 {
238 p = argv[i];
239 if( ( q = strchr( p, '=' ) ) == NULL )
240 goto usage;
241 *q++ = '\0';
242
243 if( strcmp( p, "type" ) == 0 )
244 {
245 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100247 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200249 else
250 goto usage;
251 }
252 else if( strcmp( p, "format" ) == 0 )
253 {
254 if( strcmp( q, "pem" ) == 0 )
255 opt.format = FORMAT_PEM;
256 else if( strcmp( q, "der" ) == 0 )
257 opt.format = FORMAT_DER;
258 else
259 goto usage;
260 }
261 else if( strcmp( p, "rsa_keysize" ) == 0 )
262 {
263 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200264 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200266 goto usage;
267 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100269 else if( strcmp( p, "ec_curve" ) == 0 )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100272 goto usage;
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200273 opt.ec_curve = curve_info->MBEDTLS_PRIVATE(grp_id);
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100274 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200275#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200276 else if( strcmp( p, "filename" ) == 0 )
277 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200278 else if( strcmp( p, "use_dev_random" ) == 0 )
279 {
280 opt.use_dev_random = atoi( q );
281 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
282 goto usage;
283 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284 else
285 goto usage;
286 }
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200289 fflush( stdout );
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_entropy_init( &entropy );
292#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200293 if( opt.use_dev_random )
294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200296 NULL, DEV_RANDOM_THRESHOLD,
297 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200298 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200299 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200300 goto exit;
301 }
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200304 fflush( stdout );
305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200307
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200308 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200309 (const unsigned char *) pers,
310 strlen( pers ) ) ) != 0 )
311 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200312 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200313 goto exit;
314 }
315
316 /*
317 * 1.1. Generate the key
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200320 fflush( stdout );
321
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000322 if( ( ret = mbedtls_pk_setup( &key,
323 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200324 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200325 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100326 goto exit;
327 }
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
330 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker83aad1f2017-08-23 06:45:10 +0100333 opt.rsa_keysize, 65537 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200334 if( ret != 0 )
335 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200336 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200337 goto exit;
338 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200339 }
340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#endif /* MBEDTLS_RSA_C */
342#if defined(MBEDTLS_ECP_C)
343 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200344 {
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000345 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
346 mbedtls_pk_ec( key ),
Hanno Becker83aad1f2017-08-23 06:45:10 +0100347 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100348 if( ret != 0 )
349 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200350 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100351 goto exit;
352 }
353 }
354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200358 goto exit;
359 }
360
361 /*
362 * 1.2 Print the key
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_RSA_C)
367 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker83aad1f2017-08-23 06:45:10 +0100370
371 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
372 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
373 {
374 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
375 goto exit;
376 }
377
378 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
379 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
380 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
381 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
382 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
383 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
384 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
385 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200386 }
387 else
388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#if defined(MBEDTLS_ECP_C)
390 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
393 mbedtls_printf( "curve: %s\n",
Gilles Peskinebf69ea52021-05-27 23:53:07 +0200394 mbedtls_ecp_curve_info_from_grp_id( ecp->MBEDTLS_PRIVATE(grp).id )->MBEDTLS_PRIVATE(name) );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200395 mbedtls_mpi_write_file( "X_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL );
396 mbedtls_mpi_write_file( "Y_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL );
397 mbedtls_mpi_write_file( "D: ", &ecp->MBEDTLS_PRIVATE(d) , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200398 }
399 else
400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200402
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200403 /*
404 * 1.3 Export key
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200407
408 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200411 goto exit;
412 }
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200415
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100416 exit_code = MBEDTLS_EXIT_SUCCESS;
417
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200418exit:
419
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100420 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#ifdef MBEDTLS_ERROR_C
423 mbedtls_strerror( ret, buf, sizeof( buf ) );
424 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200425#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200427#endif
428 }
429
Hanno Becker83aad1f2017-08-23 06:45:10 +0100430 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
431 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
432 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_pk_free( &key );
435 mbedtls_ctr_drbg_free( &ctr_drbg );
436 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200437
438#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200440 fflush( stdout ); getchar();
441#endif
442
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200443 mbedtls_exit( exit_code );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200444}
Simon Butcher604d3992016-10-06 19:02:49 +0100445#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
446 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */