blob: f6bb2378779d59e6c5dd32141cecf73302afe32b [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker15b9b3a2013-09-23 12:05:44 +02006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +02009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
13 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/error.h"
15#include "mbedtls/pk.h"
16#include "mbedtls/ecdsa.h"
17#include "mbedtls/rsa.h"
18#include "mbedtls/error.h"
19#include "mbedtls/entropy.h"
20#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020021
Rich Evans18b78c72015-02-11 14:06:19 +000022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020025
Rich Evans18b78c72015-02-11 14:06:19 +000026#if !defined(_WIN32)
27#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020028
29#define DEV_RANDOM_THRESHOLD 32
30
Gilles Peskine449bd832023-01-11 14:50:10 +010031int dev_random_entropy_poll(void *data, unsigned char *output,
32 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020033{
34 FILE *file;
35 size_t ret, left = len;
36 unsigned char *p = output;
37 ((void) data);
38
39 *olen = 0;
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041 file = fopen("/dev/random", "rb");
42 if (file == NULL) {
43 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
44 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020047 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine449bd832023-01-11 14:50:10 +010048 ret = fread(p, 1, left, file);
49 if (ret == 0 && ferror(file)) {
50 fclose(file);
51 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020052 }
53
54 p += ret;
55 left -= ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010056 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020057 }
Gilles Peskine449bd832023-01-11 14:50:10 +010058 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020059 *olen = len;
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020062}
Rich Evans18b78c72015-02-11 14:06:19 +000063#endif /* !_WIN32 */
64#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_ECP_C)
Gilles Peskinea73b5772021-07-19 14:36:03 +020067#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000068#else
69#define DFL_EC_CURVE 0
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000073#define USAGE_DEV_RANDOM \
74 " use_dev_random=0|1 default: 0\n"
75#else
76#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020078
Rich Evans18b78c72015-02-11 14:06:19 +000079#define FORMAT_PEM 0
80#define FORMAT_DER 1
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000083#define DFL_RSA_KEYSIZE 4096
84#define DFL_FILENAME "keyfile.key"
85#define DFL_FORMAT FORMAT_PEM
86#define DFL_USE_DEV_RANDOM 0
87
88#define USAGE \
89 "\n usage: gen_key param=<>...\n" \
90 "\n acceptable parameters:\n" \
91 " type=rsa|ec default: rsa\n" \
92 " rsa_keysize=%%d default: 4096\n" \
93 " ec_curve=%%s see below\n" \
94 " filename=%%s default: keyfile.key\n" \
95 " format=pem|der default: pem\n" \
96 USAGE_DEV_RANDOM \
97 "\n"
98
Simon Butcher604d3992016-10-06 19:02:49 +010099#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
100 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
101 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +0000103{
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_printf("MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
105 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
106 "MBEDTLS_PEM_WRITE_C"
107 "not defined.\n");
108 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +0000109}
110#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000111
Simon Butcher63cb97e2018-12-06 17:43:31 +0000112
Rich Evans18b78c72015-02-11 14:06:19 +0000113/*
114 * global options
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000117 int type; /* the type of key to generate */
118 int rsa_keysize; /* length of key in bits */
119 int ec_curve; /* curve identifier for EC keys */
120 const char *filename; /* filename of the key file */
121 int format; /* the output format to use */
122 int use_dev_random; /* use /dev/random as entropy source */
123} opt;
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200126{
127 int ret;
128 FILE *f;
129 unsigned char output_buf[16000];
130 unsigned char *c = output_buf;
131 size_t len = 0;
132
133 memset(output_buf, 0, 16000);
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (opt.format == FORMAT_PEM) {
135 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
136 return ret;
137 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 len = strlen((char *) output_buf);
140 } else {
141 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
142 return ret;
143 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200144
145 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200146 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200147 }
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if ((f = fopen(output_file, "wb")) == NULL) {
150 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200151 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (fwrite(c, 1, len, f) != len) {
154 fclose(f);
155 return -1;
156 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 fclose(f);
159
160 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200164{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100165 int ret = 1;
166 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200168 char buf[1024];
169 int i;
170 char *p, *q;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200171#if defined(MBEDTLS_RSA_C)
Hanno Becker83aad1f2017-08-23 06:45:10 +0100172 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200173#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_entropy_context entropy;
175 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200176 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_ECP_C)
178 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100179#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200180
181 /*
182 * Set to sane values
183 */
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200184#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
186 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
187 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200188#endif /* MBEDTLS_RSA_C */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_pk_init(&key);
191 mbedtls_ctr_drbg_init(&ctr_drbg);
192 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200193
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200194#if defined(MBEDTLS_USE_PSA_CRYPTO)
195 psa_status_t status = psa_crypto_init();
196 if (status != PSA_SUCCESS) {
197 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
198 (int) status);
199 goto exit;
200 }
201#endif /* MBEDTLS_USE_PSA_CRYPTO */
202
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000203 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204usage:
205 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(" %s (default)\n", curve_info->name);
210 while ((++curve_info)->name != NULL) {
211 mbedtls_printf(" %s\n", curve_info->name);
212 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100213#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200214 goto exit;
215 }
216
217 opt.type = DFL_TYPE;
218 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100219 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200220 opt.filename = DFL_FILENAME;
221 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200222 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200225 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229 *q++ = '\0';
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (strcmp(p, "type") == 0) {
232 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200237 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 }
239 } else if (strcmp(p, "format") == 0) {
240 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200241 opt.format = FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200243 opt.format = FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200245 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
247 } else if (strcmp(p, "rsa_keysize") == 0) {
248 opt.rsa_keysize = atoi(q);
249 if (opt.rsa_keysize < 1024 ||
250 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200251 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 else if (strcmp(p, "ec_curve") == 0) {
256 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100257 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Gilles Peskinea73b5772021-07-19 14:36:03 +0200259 opt.ec_curve = curve_info->grp_id;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100260 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200261#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200263 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 } else if (strcmp(p, "use_dev_random") == 0) {
265 opt.use_dev_random = atoi(q);
266 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200267 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 }
269 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200270 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200272 }
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_printf("\n . Seeding the random number generator...");
275 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (opt.use_dev_random) {
280 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
281 NULL, DEV_RANDOM_THRESHOLD,
282 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
283 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
284 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200285 goto exit;
286 }
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
289 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
294 (const unsigned char *) pers,
295 strlen(pers))) != 0) {
296 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
297 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200298 goto exit;
299 }
300
301 /*
302 * 1.1. Generate the key
303 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_printf("\n . Generating the private key ...");
305 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_pk_setup(&key,
308 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
309 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100310 goto exit;
311 }
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (opt.type == MBEDTLS_PK_RSA) {
315 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
316 opt.rsa_keysize, 65537);
317 if (ret != 0) {
318 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
319 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200320 goto exit;
321 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_RSA_C */
324#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (opt.type == MBEDTLS_PK_ECKEY) {
326 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
327 mbedtls_pk_ec(key),
328 mbedtls_ctr_drbg_random, &ctr_drbg);
329 if (ret != 0) {
330 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
331 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100332 goto exit;
333 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100336 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 goto exit;
339 }
340
341 /*
342 * 1.2 Print the key
343 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
348 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
351 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
352 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100353 goto exit;
354 }
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
357 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
358 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
359 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
360 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
361 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
362 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
363 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
364 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
368 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
369 mbedtls_printf("curve: %s\n",
370 mbedtls_ecp_curve_info_from_grp_id(ecp->MBEDTLS_PRIVATE(grp).id)->name);
371 mbedtls_mpi_write_file("X_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
372 mbedtls_mpi_write_file("Y_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
373 mbedtls_mpi_write_file("D: ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL);
374 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200375#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200377
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200378 /*
379 * 1.3 Export key
380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if ((ret = write_private_key(&key, opt.filename)) != 0) {
384 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200385 goto exit;
386 }
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200389
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100390 exit_code = MBEDTLS_EXIT_SUCCESS;
391
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200392exit:
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 mbedtls_strerror(ret, buf, sizeof(buf));
397 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200398#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200400#endif
401 }
402
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200403#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +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);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200407#endif /* MBEDTLS_RSA_C */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_pk_free(&key);
410 mbedtls_ctr_drbg_free(&ctr_drbg);
411 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200413 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200414#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200417}
Simon Butcher604d3992016-10-06 19:02:49 +0100418#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
419 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */