blob: eab5c30ac34ec5587bce8a3b27c424ef2489c69c [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker15b9b3a2013-09-23 12:05:44 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
17 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/error.h"
19#include "mbedtls/pk.h"
20#include "mbedtls/ecdsa.h"
21#include "mbedtls/rsa.h"
22#include "mbedtls/error.h"
23#include "mbedtls/entropy.h"
24#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020025
Rich Evans18b78c72015-02-11 14:06:19 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020029
Rich Evans18b78c72015-02-11 14:06:19 +000030#if !defined(_WIN32)
31#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020032
33#define DEV_RANDOM_THRESHOLD 32
34
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035int dev_random_entropy_poll(void *data, unsigned char *output,
36 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020037{
38 FILE *file;
39 size_t ret, left = len;
40 unsigned char *p = output;
41 ((void) data);
42
43 *olen = 0;
44
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 file = fopen("/dev/random", "rb");
46 if (file == NULL) {
47 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
48 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020051 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 ret = fread(p, 1, left, file);
53 if (ret == 0 && ferror(file)) {
54 fclose(file);
55 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020056 }
57
58 p += ret;
59 left -= ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020061 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020063 *olen = len;
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020066}
Rich Evans18b78c72015-02-11 14:06:19 +000067#endif /* !_WIN32 */
68#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_ECP_C)
71#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000072#else
73#define DFL_EC_CURVE 0
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000077#define USAGE_DEV_RANDOM \
78 " use_dev_random=0|1 default: 0\n"
79#else
80#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020082
Rich Evans18b78c72015-02-11 14:06:19 +000083#define FORMAT_PEM 0
84#define FORMAT_DER 1
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000087#define DFL_RSA_KEYSIZE 4096
88#define DFL_FILENAME "keyfile.key"
89#define DFL_FORMAT FORMAT_PEM
90#define DFL_USE_DEV_RANDOM 0
91
92#define USAGE \
93 "\n usage: gen_key param=<>...\n" \
94 "\n acceptable parameters:\n" \
95 " type=rsa|ec default: rsa\n" \
96 " rsa_keysize=%%d default: 4096\n" \
97 " ec_curve=%%s see below\n" \
98 " filename=%%s default: keyfile.key\n" \
99 " format=pem|der default: pem\n" \
100 USAGE_DEV_RANDOM \
101 "\n"
102
Simon Butcher604d3992016-10-06 19:02:49 +0100103#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
104 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
105 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +0000107{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 mbedtls_printf("MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
109 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
110 "MBEDTLS_PEM_WRITE_C"
111 "not defined.\n");
112 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +0000113}
114#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000115
Simon Butcher63cb97e2018-12-06 17:43:31 +0000116
Rich Evans18b78c72015-02-11 14:06:19 +0000117/*
118 * global options
119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000121 int type; /* the type of key to generate */
122 int rsa_keysize; /* length of key in bits */
123 int ec_curve; /* curve identifier for EC keys */
124 const char *filename; /* filename of the key file */
125 int format; /* the output format to use */
126 int use_dev_random; /* use /dev/random as entropy source */
127} opt;
128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200130{
131 int ret;
132 FILE *f;
133 unsigned char output_buf[16000];
134 unsigned char *c = output_buf;
135 size_t len = 0;
136
137 memset(output_buf, 0, 16000);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if (opt.format == FORMAT_PEM) {
139 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
140 return ret;
141 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 len = strlen((char *) output_buf);
144 } else {
145 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
146 return ret;
147 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200148
149 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200150 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200151 }
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if ((f = fopen(output_file, "wb")) == NULL) {
154 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200155 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 if (fwrite(c, 1, len, f) != len) {
158 fclose(f);
159 return -1;
160 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 fclose(f);
163
164 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200165}
166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200168{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100169 int ret = 1;
170 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200172 char buf[1024];
173 int i;
174 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100175 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_entropy_context entropy;
177 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_ECP_C)
180 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100181#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200182
183 /*
184 * Set to sane values
185 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
188 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
189 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100190
PiotrBzdrega7c1cd5a2024-02-13 16:59:05 +0100191 mbedtls_entropy_init(&entropy);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 mbedtls_pk_init(&key);
193 mbedtls_ctr_drbg_init(&ctr_drbg);
194 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200195
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200196#if defined(MBEDTLS_USE_PSA_CRYPTO)
197 psa_status_t status = psa_crypto_init();
198 if (status != PSA_SUCCESS) {
199 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
200 (int) status);
201 goto exit;
202 }
203#endif /* MBEDTLS_USE_PSA_CRYPTO */
204
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000205 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206usage:
207 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 mbedtls_printf(" %s (default)\n", curve_info->name);
212 while ((++curve_info)->name != NULL) {
213 mbedtls_printf(" %s\n", curve_info->name);
214 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100215#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200216 goto exit;
217 }
218
219 opt.type = DFL_TYPE;
220 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100221 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200222 opt.filename = DFL_FILENAME;
223 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200224 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200231 *q++ = '\0';
232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 if (strcmp(p, "type") == 0) {
234 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200239 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 }
241 } else if (strcmp(p, "format") == 0) {
242 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200243 opt.format = FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200245 opt.format = FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200247 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 }
249 } else if (strcmp(p, "rsa_keysize") == 0) {
250 opt.rsa_keysize = atoi(q);
251 if (opt.rsa_keysize < 1024 ||
252 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 else if (strcmp(p, "ec_curve") == 0) {
258 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100259 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100261 opt.ec_curve = curve_info->grp_id;
262 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200263#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200265 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 } else if (strcmp(p, "use_dev_random") == 0) {
267 opt.use_dev_random = atoi(q);
268 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200269 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 }
271 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200272 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200274 }
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_printf("\n . Seeding the random number generator...");
277 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 if (opt.use_dev_random) {
281 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
282 NULL, DEV_RANDOM_THRESHOLD,
283 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
284 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
285 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200286 goto exit;
287 }
288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
290 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
295 (const unsigned char *) pers,
296 strlen(pers))) != 0) {
297 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
298 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200299 goto exit;
300 }
301
302 /*
303 * 1.1. Generate the key
304 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 mbedtls_printf("\n . Generating the private key ...");
306 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 if ((ret = mbedtls_pk_setup(&key,
309 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
310 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100311 goto exit;
312 }
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 if (opt.type == MBEDTLS_PK_RSA) {
316 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
317 opt.rsa_keysize, 65537);
318 if (ret != 0) {
319 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
320 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200321 goto exit;
322 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_RSA_C */
325#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 if (opt.type == MBEDTLS_PK_ECKEY) {
327 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
328 mbedtls_pk_ec(key),
329 mbedtls_ctr_drbg_random, &ctr_drbg);
330 if (ret != 0) {
331 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
332 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100333 goto exit;
334 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100337 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200339 goto exit;
340 }
341
342 /*
343 * 1.2 Print the key
344 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
349 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
352 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
353 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100354 goto exit;
355 }
356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
358 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
359 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
360 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
361 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
362 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
363 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
364 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
365 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
369 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
370 mbedtls_printf("curve: %s\n",
371 mbedtls_ecp_curve_info_from_grp_id(ecp->grp.id)->name);
372 mbedtls_mpi_write_file("X_Q: ", &ecp->Q.X, 16, NULL);
373 mbedtls_mpi_write_file("Y_Q: ", &ecp->Q.Y, 16, NULL);
374 mbedtls_mpi_write_file("D: ", &ecp->d, 16, NULL);
375 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200376#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200378
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200379 /*
380 * 1.3 Export key
381 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if ((ret = write_private_key(&key, opt.filename)) != 0) {
385 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200386 goto exit;
387 }
388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200390
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100391 exit_code = MBEDTLS_EXIT_SUCCESS;
392
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200393exit:
394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 mbedtls_strerror(ret, buf, sizeof(buf));
398 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200399#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200401#endif
402 }
403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
405 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
406 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 mbedtls_pk_free(&key);
409 mbedtls_ctr_drbg_free(&ctr_drbg);
410 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200411#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200412 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200413#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200414
415#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 mbedtls_printf(" + Press Enter to exit this program.\n");
417 fflush(stdout); getchar();
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200418#endif
419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200421}
Simon Butcher604d3992016-10-06 19:02:49 +0100422#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
423 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */