blob: 1a6463d8a230fd4d9d04a96bb3dcf82c77dd0cb5 [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047int dev_random_entropy_poll(void *data, unsigned char *output,
48 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020049{
50 FILE *file;
51 size_t ret, left = len;
52 unsigned char *p = output;
53 ((void) data);
54
55 *olen = 0;
56
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 file = fopen("/dev/random", "rb");
58 if (file == NULL) {
59 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
60 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020063 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 ret = fread(p, 1, left, file);
65 if (ret == 0 && ferror(file)) {
66 fclose(file);
67 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020068 }
69
70 p += ret;
71 left -= ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020073 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020075 *olen = len;
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020078}
Rich Evans18b78c72015-02-11 14:06:19 +000079#endif /* !_WIN32 */
80#endif
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_ECP_C)
83#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000084#else
85#define DFL_EC_CURVE 0
86#endif
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000089#define USAGE_DEV_RANDOM \
90 " use_dev_random=0|1 default: 0\n"
91#else
92#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020094
Rich Evans18b78c72015-02-11 14:06:19 +000095#define FORMAT_PEM 0
96#define FORMAT_DER 1
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000099#define DFL_RSA_KEYSIZE 4096
100#define DFL_FILENAME "keyfile.key"
101#define DFL_FORMAT FORMAT_PEM
102#define DFL_USE_DEV_RANDOM 0
103
104#define USAGE \
105 "\n usage: gen_key param=<>...\n" \
106 "\n acceptable parameters:\n" \
107 " type=rsa|ec default: rsa\n" \
108 " rsa_keysize=%%d default: 4096\n" \
109 " ec_curve=%%s see below\n" \
110 " filename=%%s default: keyfile.key\n" \
111 " format=pem|der default: pem\n" \
112 USAGE_DEV_RANDOM \
113 "\n"
114
Simon Butcher604d3992016-10-06 19:02:49 +0100115#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
116 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
117 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +0000119{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 mbedtls_printf("MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
121 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
122 "MBEDTLS_PEM_WRITE_C"
123 "not defined.\n");
124 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +0000125}
126#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000127
Simon Butcher63cb97e2018-12-06 17:43:31 +0000128
Rich Evans18b78c72015-02-11 14:06:19 +0000129/*
130 * global options
131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000133 int type; /* the type of key to generate */
134 int rsa_keysize; /* length of key in bits */
135 int ec_curve; /* curve identifier for EC keys */
136 const char *filename; /* filename of the key file */
137 int format; /* the output format to use */
138 int use_dev_random; /* use /dev/random as entropy source */
139} opt;
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200142{
143 int ret;
144 FILE *f;
145 unsigned char output_buf[16000];
146 unsigned char *c = output_buf;
147 size_t len = 0;
148
149 memset(output_buf, 0, 16000);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if (opt.format == FORMAT_PEM) {
151 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
152 return ret;
153 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 len = strlen((char *) output_buf);
156 } else {
157 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
158 return ret;
159 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200160
161 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200162 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200163 }
164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if ((f = fopen(output_file, "wb")) == NULL) {
166 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200167 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if (fwrite(c, 1, len, f) != len) {
170 fclose(f);
171 return -1;
172 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 fclose(f);
175
176 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200177}
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200180{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100181 int ret = 1;
182 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200184 char buf[1024];
185 int i;
186 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100187 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_entropy_context entropy;
189 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200190 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_ECP_C)
192 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100193#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200194
195 /*
196 * Set to sane values
197 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
200 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
201 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 mbedtls_pk_init(&key);
204 mbedtls_ctr_drbg_init(&ctr_drbg);
205 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200206
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000207 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208usage:
209 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 mbedtls_printf(" %s (default)\n", curve_info->name);
214 while ((++curve_info)->name != NULL) {
215 mbedtls_printf(" %s\n", curve_info->name);
216 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100217#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200218 goto exit;
219 }
220
221 opt.type = DFL_TYPE;
222 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100223 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200224 opt.filename = DFL_FILENAME;
225 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200226 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200231 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200233 *q++ = '\0';
234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (strcmp(p, "type") == 0) {
236 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200241 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 }
243 } else if (strcmp(p, "format") == 0) {
244 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200245 opt.format = FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200247 opt.format = FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200249 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 }
251 } else if (strcmp(p, "rsa_keysize") == 0) {
252 opt.rsa_keysize = atoi(q);
253 if (opt.rsa_keysize < 1024 ||
254 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200255 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200257 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 else if (strcmp(p, "ec_curve") == 0) {
260 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100261 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100263 opt.ec_curve = curve_info->grp_id;
264 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200265#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200267 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 } else if (strcmp(p, "use_dev_random") == 0) {
269 opt.use_dev_random = atoi(q);
270 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200271 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 }
273 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200274 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200276 }
277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 mbedtls_printf("\n . Seeding the random number generator...");
279 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if (opt.use_dev_random) {
284 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
285 NULL, DEV_RANDOM_THRESHOLD,
286 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
287 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
288 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200289 goto exit;
290 }
291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
293 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
298 (const unsigned char *) pers,
299 strlen(pers))) != 0) {
300 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
301 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 goto exit;
303 }
304
305 /*
306 * 1.1. Generate the key
307 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 mbedtls_printf("\n . Generating the private key ...");
309 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if ((ret = mbedtls_pk_setup(&key,
312 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
313 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100314 goto exit;
315 }
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if (opt.type == MBEDTLS_PK_RSA) {
319 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
320 opt.rsa_keysize, 65537);
321 if (ret != 0) {
322 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
323 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200324 goto exit;
325 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_RSA_C */
328#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (opt.type == MBEDTLS_PK_ECKEY) {
330 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
331 mbedtls_pk_ec(key),
332 mbedtls_ctr_drbg_random, &ctr_drbg);
333 if (ret != 0) {
334 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
335 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100336 goto exit;
337 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100340 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200342 goto exit;
343 }
344
345 /*
346 * 1.2 Print the key
347 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
352 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
355 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
356 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100357 goto exit;
358 }
359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
361 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
362 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
363 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
364 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
365 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
366 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
367 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
368 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
372 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
373 mbedtls_printf("curve: %s\n",
374 mbedtls_ecp_curve_info_from_grp_id(ecp->grp.id)->name);
375 mbedtls_mpi_write_file("X_Q: ", &ecp->Q.X, 16, NULL);
376 mbedtls_mpi_write_file("Y_Q: ", &ecp->Q.Y, 16, NULL);
377 mbedtls_mpi_write_file("D: ", &ecp->d, 16, NULL);
378 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200379#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200381
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200382 /*
383 * 1.3 Export key
384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if ((ret = write_private_key(&key, opt.filename)) != 0) {
388 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200389 goto exit;
390 }
391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200393
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100394 exit_code = MBEDTLS_EXIT_SUCCESS;
395
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200396exit:
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 mbedtls_strerror(ret, buf, sizeof(buf));
401 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200402#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200404#endif
405 }
406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
408 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
409 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_pk_free(&key);
412 mbedtls_ctr_drbg_free(&ctr_drbg);
413 mbedtls_entropy_free(&entropy);
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 */