blob: cd21743fb42ef85a718d787e0d1edaed120fc008 [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
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200207#if defined(MBEDTLS_USE_PSA_CRYPTO)
208 psa_status_t status = psa_crypto_init();
209 if (status != PSA_SUCCESS) {
210 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
211 (int) status);
212 goto exit;
213 }
214#endif /* MBEDTLS_USE_PSA_CRYPTO */
215
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000216 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217usage:
218 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 mbedtls_printf(" %s (default)\n", curve_info->name);
223 while ((++curve_info)->name != NULL) {
224 mbedtls_printf(" %s\n", curve_info->name);
225 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100226#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 goto exit;
228 }
229
230 opt.type = DFL_TYPE;
231 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100232 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200233 opt.filename = DFL_FILENAME;
234 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200235 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200238 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200240 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200242 *q++ = '\0';
243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 if (strcmp(p, "type") == 0) {
245 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200250 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 }
252 } else if (strcmp(p, "format") == 0) {
253 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200254 opt.format = FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200256 opt.format = FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200258 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 }
260 } else if (strcmp(p, "rsa_keysize") == 0) {
261 opt.rsa_keysize = atoi(q);
262 if (opt.rsa_keysize < 1024 ||
263 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200264 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 else if (strcmp(p, "ec_curve") == 0) {
269 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100270 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100272 opt.ec_curve = curve_info->grp_id;
273 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200274#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200276 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 } else if (strcmp(p, "use_dev_random") == 0) {
278 opt.use_dev_random = atoi(q);
279 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200280 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 }
282 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200283 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200285 }
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 mbedtls_printf("\n . Seeding the random number generator...");
288 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 if (opt.use_dev_random) {
293 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
294 NULL, DEV_RANDOM_THRESHOLD,
295 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
296 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
297 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200298 goto exit;
299 }
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
302 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
307 (const unsigned char *) pers,
308 strlen(pers))) != 0) {
309 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
310 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200311 goto exit;
312 }
313
314 /*
315 * 1.1. Generate the key
316 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 mbedtls_printf("\n . Generating the private key ...");
318 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if ((ret = mbedtls_pk_setup(&key,
321 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
322 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100323 goto exit;
324 }
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 if (opt.type == MBEDTLS_PK_RSA) {
328 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
329 opt.rsa_keysize, 65537);
330 if (ret != 0) {
331 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
332 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200333 goto exit;
334 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_RSA_C */
337#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 if (opt.type == MBEDTLS_PK_ECKEY) {
339 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
340 mbedtls_pk_ec(key),
341 mbedtls_ctr_drbg_random, &ctr_drbg);
342 if (ret != 0) {
343 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
344 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100345 goto exit;
346 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100349 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200351 goto exit;
352 }
353
354 /*
355 * 1.2 Print the key
356 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
361 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
364 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
365 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100366 goto exit;
367 }
368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
370 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
371 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
372 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
373 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
374 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
375 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
376 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
377 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
381 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
382 mbedtls_printf("curve: %s\n",
383 mbedtls_ecp_curve_info_from_grp_id(ecp->grp.id)->name);
384 mbedtls_mpi_write_file("X_Q: ", &ecp->Q.X, 16, NULL);
385 mbedtls_mpi_write_file("Y_Q: ", &ecp->Q.Y, 16, NULL);
386 mbedtls_mpi_write_file("D: ", &ecp->d, 16, NULL);
387 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200388#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200390
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200391 /*
392 * 1.3 Export key
393 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 if ((ret = write_private_key(&key, opt.filename)) != 0) {
397 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200398 goto exit;
399 }
400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200402
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100403 exit_code = MBEDTLS_EXIT_SUCCESS;
404
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200405exit:
406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 mbedtls_strerror(ret, buf, sizeof(buf));
410 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200411#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200413#endif
414 }
415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
417 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
418 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 mbedtls_pk_free(&key);
421 mbedtls_ctr_drbg_free(&ctr_drbg);
422 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200423#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200424 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200425#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200426
427#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 mbedtls_printf(" + Press Enter to exit this program.\n");
429 fflush(stdout); getchar();
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200430#endif
431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200433}
Simon Butcher604d3992016-10-06 19:02:49 +0100434#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
435 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */