blob: 464c38144e5469fc1f7b4f266cdd20c0f0620c37 [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é-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
25 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/error.h"
27#include "mbedtls/pk.h"
28#include "mbedtls/ecdsa.h"
29#include "mbedtls/rsa.h"
30#include "mbedtls/error.h"
31#include "mbedtls/entropy.h"
32#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020037
Rich Evans18b78c72015-02-11 14:06:19 +000038#if !defined(_WIN32)
39#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020040
41#define DEV_RANDOM_THRESHOLD 32
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043int dev_random_entropy_poll(void *data, unsigned char *output,
44 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020045{
46 FILE *file;
47 size_t ret, left = len;
48 unsigned char *p = output;
49 ((void) data);
50
51 *olen = 0;
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 file = fopen("/dev/random", "rb");
54 if (file == NULL) {
55 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
56 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020059 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 ret = fread(p, 1, left, file);
61 if (ret == 0 && ferror(file)) {
62 fclose(file);
63 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020064 }
65
66 p += ret;
67 left -= ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010068 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020069 }
Gilles Peskine449bd832023-01-11 14:50:10 +010070 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020071 *olen = len;
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020074}
Rich Evans18b78c72015-02-11 14:06:19 +000075#endif /* !_WIN32 */
76#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_ECP_C)
Gilles Peskinea73b5772021-07-19 14:36:03 +020079#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000080#else
81#define DFL_EC_CURVE 0
82#endif
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000085#define USAGE_DEV_RANDOM \
86 " use_dev_random=0|1 default: 0\n"
87#else
88#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020090
Rich Evans18b78c72015-02-11 14:06:19 +000091#define FORMAT_PEM 0
92#define FORMAT_DER 1
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000095#define DFL_RSA_KEYSIZE 4096
96#define DFL_FILENAME "keyfile.key"
97#define DFL_FORMAT FORMAT_PEM
98#define DFL_USE_DEV_RANDOM 0
99
100#define USAGE \
101 "\n usage: gen_key param=<>...\n" \
102 "\n acceptable parameters:\n" \
103 " type=rsa|ec default: rsa\n" \
104 " rsa_keysize=%%d default: 4096\n" \
105 " ec_curve=%%s see below\n" \
106 " filename=%%s default: keyfile.key\n" \
107 " format=pem|der default: pem\n" \
108 USAGE_DEV_RANDOM \
109 "\n"
110
Simon Butcher604d3992016-10-06 19:02:49 +0100111#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
112 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
113 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100114int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +0000115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_printf("MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
117 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
118 "MBEDTLS_PEM_WRITE_C"
119 "not defined.\n");
120 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +0000121}
122#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000123
Simon Butcher63cb97e2018-12-06 17:43:31 +0000124
Rich Evans18b78c72015-02-11 14:06:19 +0000125/*
126 * global options
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000129 int type; /* the type of key to generate */
130 int rsa_keysize; /* length of key in bits */
131 int ec_curve; /* curve identifier for EC keys */
132 const char *filename; /* filename of the key file */
133 int format; /* the output format to use */
134 int use_dev_random; /* use /dev/random as entropy source */
135} opt;
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200138{
139 int ret;
140 FILE *f;
141 unsigned char output_buf[16000];
142 unsigned char *c = output_buf;
143 size_t len = 0;
144
145 memset(output_buf, 0, 16000);
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (opt.format == FORMAT_PEM) {
147 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
148 return ret;
149 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 len = strlen((char *) output_buf);
152 } else {
153 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
154 return ret;
155 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200156
157 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200158 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200159 }
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if ((f = fopen(output_file, "wb")) == NULL) {
162 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200163 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (fwrite(c, 1, len, f) != len) {
166 fclose(f);
167 return -1;
168 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 fclose(f);
171
172 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200176{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100177 int ret = 1;
178 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200180 char buf[1024];
181 int i;
182 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100183 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_entropy_context entropy;
185 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200186 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_ECP_C)
188 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100189#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200190
191 /*
192 * Set to sane values
193 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
196 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
197 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_pk_init(&key);
200 mbedtls_ctr_drbg_init(&ctr_drbg);
201 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200202
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200203#if defined(MBEDTLS_USE_PSA_CRYPTO)
204 psa_status_t status = psa_crypto_init();
205 if (status != PSA_SUCCESS) {
206 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
207 (int) status);
208 goto exit;
209 }
210#endif /* MBEDTLS_USE_PSA_CRYPTO */
211
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000212 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100213usage:
214 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_printf(" %s (default)\n", curve_info->name);
219 while ((++curve_info)->name != NULL) {
220 mbedtls_printf(" %s\n", curve_info->name);
221 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100222#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200223 goto exit;
224 }
225
226 opt.type = DFL_TYPE;
227 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100228 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229 opt.filename = DFL_FILENAME;
230 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200231 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200234 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200236 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200238 *q++ = '\0';
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (strcmp(p, "type") == 0) {
241 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200246 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 }
248 } else if (strcmp(p, "format") == 0) {
249 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200250 opt.format = FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200252 opt.format = FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200254 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 }
256 } else if (strcmp(p, "rsa_keysize") == 0) {
257 opt.rsa_keysize = atoi(q);
258 if (opt.rsa_keysize < 1024 ||
259 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200260 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 else if (strcmp(p, "ec_curve") == 0) {
265 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100266 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 }
Gilles Peskinea73b5772021-07-19 14:36:03 +0200268 opt.ec_curve = curve_info->grp_id;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100269 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200270#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200272 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 } else if (strcmp(p, "use_dev_random") == 0) {
274 opt.use_dev_random = atoi(q);
275 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200276 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 }
278 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200279 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_printf("\n . Seeding the random number generator...");
284 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (opt.use_dev_random) {
289 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
290 NULL, DEV_RANDOM_THRESHOLD,
291 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
292 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
293 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200294 goto exit;
295 }
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
298 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200299 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
303 (const unsigned char *) pers,
304 strlen(pers))) != 0) {
305 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
306 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 goto exit;
308 }
309
310 /*
311 * 1.1. Generate the key
312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_printf("\n . Generating the private key ...");
314 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if ((ret = mbedtls_pk_setup(&key,
317 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
318 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100319 goto exit;
320 }
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (opt.type == MBEDTLS_PK_RSA) {
324 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
325 opt.rsa_keysize, 65537);
326 if (ret != 0) {
327 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
328 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200329 goto exit;
330 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* MBEDTLS_RSA_C */
333#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (opt.type == MBEDTLS_PK_ECKEY) {
335 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
336 mbedtls_pk_ec(key),
337 mbedtls_ctr_drbg_random, &ctr_drbg);
338 if (ret != 0) {
339 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
340 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100341 goto exit;
342 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100345 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200347 goto exit;
348 }
349
350 /*
351 * 1.2 Print the key
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
357 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
360 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
361 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100362 goto exit;
363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
366 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
367 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
368 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
369 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
370 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
371 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
372 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
373 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
377 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
378 mbedtls_printf("curve: %s\n",
379 mbedtls_ecp_curve_info_from_grp_id(ecp->MBEDTLS_PRIVATE(grp).id)->name);
380 mbedtls_mpi_write_file("X_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
381 mbedtls_mpi_write_file("Y_Q: ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
382 mbedtls_mpi_write_file("D: ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL);
383 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200384#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200386
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200387 /*
388 * 1.3 Export key
389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if ((ret = write_private_key(&key, opt.filename)) != 0) {
393 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200394 goto exit;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200398
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100399 exit_code = MBEDTLS_EXIT_SUCCESS;
400
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200401exit:
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 mbedtls_strerror(ret, buf, sizeof(buf));
406 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200407#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200409#endif
410 }
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
413 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
414 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_pk_free(&key);
417 mbedtls_ctr_drbg_free(&ctr_drbg);
418 mbedtls_entropy_free(&entropy);
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200419 mbedtls_psa_crypto_free();
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200422}
Simon Butcher604d3992016-10-06 19:02:49 +0100423#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
424 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */