blob: 63cc11ed5b9566000631794d7cd260dcfa251f82 [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é-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 Garcia208c2172018-04-29 19:51:56 +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 Garcia208c2172018-04-29 19:51:56 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
38 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/error.h"
40#include "mbedtls/pk.h"
41#include "mbedtls/ecdsa.h"
42#include "mbedtls/rsa.h"
43#include "mbedtls/error.h"
44#include "mbedtls/entropy.h"
45#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020050
Rich Evans18b78c72015-02-11 14:06:19 +000051#if !defined(_WIN32)
52#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020053
54#define DEV_RANDOM_THRESHOLD 32
55
56int dev_random_entropy_poll( void *data, unsigned char *output,
57 size_t len, size_t *olen )
58{
59 FILE *file;
60 size_t ret, left = len;
61 unsigned char *p = output;
62 ((void) data);
63
64 *olen = 0;
65
66 file = fopen( "/dev/random", "rb" );
67 if( file == NULL )
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 while( left > 0 )
71 {
72 /* /dev/random can return much less than requested. If so, try again */
73 ret = fread( p, 1, left, file );
74 if( ret == 0 && ferror( file ) )
75 {
76 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020078 }
79
80 p += ret;
81 left -= ret;
82 sleep( 1 );
83 }
84 fclose( file );
85 *olen = len;
86
87 return( 0 );
88}
Rich Evans18b78c72015-02-11 14:06:19 +000089#endif /* !_WIN32 */
90#endif
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_ECP_C)
93#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000094#else
95#define DFL_EC_CURVE 0
96#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000099#define USAGE_DEV_RANDOM \
100 " use_dev_random=0|1 default: 0\n"
101#else
102#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200104
Rich Evans18b78c72015-02-11 14:06:19 +0000105#define FORMAT_PEM 0
106#define FORMAT_DER 1
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000109#define DFL_RSA_KEYSIZE 4096
110#define DFL_FILENAME "keyfile.key"
111#define DFL_FORMAT FORMAT_PEM
112#define DFL_USE_DEV_RANDOM 0
113
114#define USAGE \
115 "\n usage: gen_key param=<>...\n" \
116 "\n acceptable parameters:\n" \
117 " type=rsa|ec default: rsa\n" \
118 " rsa_keysize=%%d default: 4096\n" \
119 " ec_curve=%%s see below\n" \
120 " filename=%%s default: keyfile.key\n" \
121 " format=pem|der default: pem\n" \
122 USAGE_DEV_RANDOM \
123 "\n"
124
Simon Butcher604d3992016-10-06 19:02:49 +0100125#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
126 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
127 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000128int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher604d3992016-10-06 19:02:49 +0100131 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
132 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000133 "not defined.\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200134 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000135}
136#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000137
Simon Butcher63cb97e2018-12-06 17:43:31 +0000138
Rich Evans18b78c72015-02-11 14:06:19 +0000139/*
140 * global options
141 */
142struct options
143{
144 int type; /* the type of key to generate */
145 int rsa_keysize; /* length of key in bits */
146 int ec_curve; /* curve identifier for EC keys */
147 const char *filename; /* filename of the key file */
148 int format; /* the output format to use */
149 int use_dev_random; /* use /dev/random as entropy source */
150} opt;
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200153{
154 int ret;
155 FILE *f;
156 unsigned char output_buf[16000];
157 unsigned char *c = output_buf;
158 size_t len = 0;
159
160 memset(output_buf, 0, 16000);
161 if( opt.format == FORMAT_PEM )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200164 return( ret );
165
166 len = strlen( (char *) output_buf );
167 }
168 else
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200171 return( ret );
172
173 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200174 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200175 }
176
Paul Bakker3966d712014-07-10 12:03:09 +0200177 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178 return( -1 );
179
180 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200181 {
182 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200183 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200184 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200185
Paul Bakker0c226102014-04-17 16:02:36 +0200186 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187
188 return( 0 );
189}
190
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200191int main( int argc, char *argv[] )
192{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100193 int ret = 1;
194 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200196 char buf[1024];
197 int i;
198 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100199 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_entropy_context entropy;
201 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200202 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_ECP_C)
204 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100205#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200206
207 /*
208 * Set to sane values
209 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100210
211 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
212 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
213 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200216 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200217 memset( buf, 0, sizeof( buf ) );
218
219 if( argc == 0 )
220 {
221 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_printf( USAGE );
223#if defined(MBEDTLS_ECP_C)
James Cowgill07a92d72015-10-09 00:28:14 +0100224 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 curve_info = mbedtls_ecp_curve_list();
226 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100227 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " %s\n", curve_info->name );
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100229#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200230 goto exit;
231 }
232
233 opt.type = DFL_TYPE;
234 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100235 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200236 opt.filename = DFL_FILENAME;
237 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200238 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200239
240 for( i = 1; i < argc; i++ )
241 {
242 p = argv[i];
243 if( ( q = strchr( p, '=' ) ) == NULL )
244 goto usage;
245 *q++ = '\0';
246
247 if( strcmp( p, "type" ) == 0 )
248 {
249 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100251 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253 else
254 goto usage;
255 }
256 else if( strcmp( p, "format" ) == 0 )
257 {
258 if( strcmp( q, "pem" ) == 0 )
259 opt.format = FORMAT_PEM;
260 else if( strcmp( q, "der" ) == 0 )
261 opt.format = FORMAT_DER;
262 else
263 goto usage;
264 }
265 else if( strcmp( p, "rsa_keysize" ) == 0 )
266 {
267 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200268 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200270 goto usage;
271 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100273 else if( strcmp( p, "ec_curve" ) == 0 )
274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100276 goto usage;
277 opt.ec_curve = curve_info->grp_id;
278 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200279#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200280 else if( strcmp( p, "filename" ) == 0 )
281 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200282 else if( strcmp( p, "use_dev_random" ) == 0 )
283 {
284 opt.use_dev_random = atoi( q );
285 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
286 goto usage;
287 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200288 else
289 goto usage;
290 }
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200293 fflush( stdout );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_entropy_init( &entropy );
296#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200297 if( opt.use_dev_random )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200300 NULL, DEV_RANDOM_THRESHOLD,
301 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200302 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200303 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200304 goto exit;
305 }
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200308 fflush( stdout );
309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200311
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200312 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200313 (const unsigned char *) pers,
314 strlen( pers ) ) ) != 0 )
315 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200316 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200317 goto exit;
318 }
319
320 /*
321 * 1.1. Generate the key
322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200324 fflush( stdout );
325
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000326 if( ( ret = mbedtls_pk_setup( &key,
327 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200328 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200329 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100330 goto exit;
331 }
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
334 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker83aad1f2017-08-23 06:45:10 +0100337 opt.rsa_keysize, 65537 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 if( ret != 0 )
339 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200340 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", (unsigned int) -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200341 goto exit;
342 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200343 }
344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_RSA_C */
346#if defined(MBEDTLS_ECP_C)
347 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200348 {
Hanno Beckere2dae7e2018-11-05 16:54:40 +0000349 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
350 mbedtls_pk_ec( key ),
Hanno Becker83aad1f2017-08-23 06:45:10 +0100351 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100352 if( ret != 0 )
353 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200354 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", (unsigned int) -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100355 goto exit;
356 }
357 }
358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200362 goto exit;
363 }
364
365 /*
366 * 1.2 Print the key
367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_RSA_C)
371 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker83aad1f2017-08-23 06:45:10 +0100374
375 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
376 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
377 {
378 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
379 goto exit;
380 }
381
382 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
383 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
384 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
385 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
386 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
387 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
388 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
389 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200390 }
391 else
392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_ECP_C)
394 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
397 mbedtls_printf( "curve: %s\n",
398 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
399 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
400 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
401 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200402 }
403 else
404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200406
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200407 /*
408 * 1.3 Export key
409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200411
412 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200415 goto exit;
416 }
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200419
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100420 exit_code = MBEDTLS_EXIT_SUCCESS;
421
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200422exit:
423
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100424 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#ifdef MBEDTLS_ERROR_C
427 mbedtls_strerror( ret, buf, sizeof( buf ) );
428 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200429#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200431#endif
432 }
433
Hanno Becker83aad1f2017-08-23 06:45:10 +0100434 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
435 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
436 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_pk_free( &key );
439 mbedtls_ctr_drbg_free( &ctr_drbg );
440 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200441
442#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200444 fflush( stdout ); getchar();
445#endif
446
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200447 mbedtls_exit( exit_code );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200448}
Simon Butcher604d3992016-10-06 19:02:49 +0100449#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
450 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */