blob: 60f992e431132600aa1365a84ddd879e5009b83f [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
Paul Bakkerf3df61a2013-08-26 17:22:23 +02002 * Key writing application
Paul Bakkerbdb912d2012-02-13 23:11:30 +00003 *
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 Bakkerbdb912d2012-02-13 23:11:30 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Gilles Peskine9552a522023-12-23 18:44:20 +010012#if !defined(MBEDTLS_PK_PARSE_C) || \
13 !defined(MBEDTLS_PK_WRITE_C) || \
14 !defined(MBEDTLS_FS_IO) || \
15 !defined(MBEDTLS_ENTROPY_C) || \
16 !defined(MBEDTLS_CTR_DRBG_C) || \
17 !defined(MBEDTLS_BIGNUM_C)
18int main(void)
19{
20 mbedtls_printf("MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or "
21 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
22 "MBEDTLS_FS_IO and/or MBEDTLS_BIGNUM_C not defined.\n");
23 mbedtls_exit(0);
24}
25#else
26
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/error.h"
28#include "mbedtls/pk.h"
29#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000030
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020031#include "mbedtls/entropy.h"
32#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <stdio.h>
35#include <string.h>
Paul Bakkered27a042013-04-18 22:46:23 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000038#define USAGE_OUT \
39 " output_file=%%s default: keyfile.pem\n" \
40 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020041#else
Rich Evans18b78c72015-02-11 14:06:19 +000042#define USAGE_OUT \
43 " output_file=%%s default: keyfile.der\n" \
44 " output_format=der default: der\n"
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000048#define DFL_OUTPUT_FILENAME "keyfile.pem"
49#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
50#else
51#define DFL_OUTPUT_FILENAME "keyfile.der"
52#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
53#endif
54
55#define DFL_MODE MODE_NONE
56#define DFL_FILENAME "keyfile.key"
57#define DFL_DEBUG_LEVEL 0
58#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020059
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060#define MODE_NONE 0
61#define MODE_PRIVATE 1
62#define MODE_PUBLIC 2
63
64#define OUTPUT_MODE_NONE 0
65#define OUTPUT_MODE_PRIVATE 1
66#define OUTPUT_MODE_PUBLIC 2
67
Paul Bakkerf3df61a2013-08-26 17:22:23 +020068#define OUTPUT_FORMAT_PEM 0
69#define OUTPUT_FORMAT_DER 1
70
Rich Evans18b78c72015-02-11 14:06:19 +000071#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010072 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000073 "\n acceptable parameters:\n" \
74 " mode=private|public default: none\n" \
75 " filename=%%s default: keyfile.key\n" \
76 " output_mode=private|public default: none\n" \
77 USAGE_OUT \
78 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000079
Simon Butcher63cb97e2018-12-06 17:43:31 +000080
Paul Bakkerbdb912d2012-02-13 23:11:30 +000081/*
82 * global options
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020086 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000087 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020088 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020089 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090} opt;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020094 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000096 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 unsigned char *c = output_buf;
98 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099
Paul Bakker1d569582012-10-03 20:35:44 +0000100 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (opt.output_format == OUTPUT_FORMAT_PEM) {
104 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
105 return ret;
106 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 len = strlen((char *) output_buf);
109 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200110#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200111 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
113 return ret;
114 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200115
116 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200117 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if ((f = fopen(output_file, "w")) == NULL) {
121 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200122 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (fwrite(c, 1, len, f) != len) {
125 fclose(f);
126 return -1;
127 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 fclose(f);
130
131 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200136 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000138 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 unsigned char *c = output_buf;
140 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
Paul Bakker1d569582012-10-03 20:35:44 +0000142 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (opt.output_format == OUTPUT_FORMAT_PEM) {
146 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
147 return ret;
148 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 len = strlen((char *) output_buf);
151 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200152#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200153 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
155 return ret;
156 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200157
158 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100159 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if ((f = fopen(output_file, "w")) == NULL) {
163 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200164 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (fwrite(c, 1, len, f) != len) {
167 fclose(f);
168 return -1;
169 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 fclose(f);
172
173 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174}
175
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200176#if defined(MBEDTLS_ECP_C)
177static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
178{
179 int ret = 0;
180
181 const mbedtls_ecp_curve_info *curve_info =
182 mbedtls_ecp_curve_info_from_grp_id(
183 mbedtls_ecp_keypair_get_group_id(ecp));
184 mbedtls_printf("curve: %s\n", curve_info->name);
185
186 mbedtls_ecp_group grp;
187 mbedtls_ecp_group_init(&grp);
188 mbedtls_mpi D;
189 mbedtls_mpi_init(&D);
190 mbedtls_ecp_point pt;
191 mbedtls_ecp_point_init(&pt);
192 mbedtls_mpi X, Y;
193 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
194
195 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
196 (has_private ? &D : NULL),
197 &pt));
198
199 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
200 size_t len = 0;
201 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
202 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
203 &len, point_bin, sizeof(point_bin)));
204 switch (mbedtls_ecp_get_type(&grp)) {
205 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
206 if ((len & 1) == 0 || point_bin[0] != 0x04) {
207 /* Point in an unxepected format. This shouldn't happen. */
208 ret = -1;
209 goto cleanup;
210 }
211 MBEDTLS_MPI_CHK(
212 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
213 MBEDTLS_MPI_CHK(
214 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
215 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
216 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
217 break;
218 case MBEDTLS_ECP_TYPE_MONTGOMERY:
219 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
220 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
221 break;
222 default:
223 mbedtls_printf(
224 "This program does not yet support listing coordinates for this curve type.\n");
225 break;
226 }
227
228 if (has_private) {
229 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
230 }
231
232cleanup:
233 mbedtls_ecp_group_free(&grp);
234 mbedtls_mpi_free(&D);
235 mbedtls_ecp_point_free(&pt);
236 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
237 return ret;
238}
239#endif
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100243 int ret = 1;
244 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine680747b2021-08-06 14:37:01 +0200245#if defined(MBEDTLS_ERROR_C)
246 char buf[200];
247#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000248 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 char *p, *q;
250
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200251 const char *pers = "pkey/key_app";
252 mbedtls_entropy_context entropy;
253 mbedtls_ctr_drbg_context ctr_drbg;
254
Hanno Becker40371ec2017-08-23 06:46:17 +0100255 mbedtls_pk_context key;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200256#if defined(MBEDTLS_RSA_C)
Hanno Becker40371ec2017-08-23 06:46:17 +0100257 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200258#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100259
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260 /*
261 * Set to sane values
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_entropy_init(&entropy);
264 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_pk_init(&key);
Gilles Peskine680747b2021-08-06 14:37:01 +0200267#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 memset(buf, 0, sizeof(buf));
Gilles Peskine680747b2021-08-06 14:37:01 +0200269#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200271#if defined(MBEDTLS_USE_PSA_CRYPTO)
272 psa_status_t status = psa_crypto_init();
273 if (status != PSA_SUCCESS) {
274 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
275 (int) status);
276 goto exit;
277 }
278#endif /* MBEDTLS_USE_PSA_CRYPTO */
279
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200280#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
282 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
283 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200284#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100285
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000286 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100287usage:
288 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 goto exit;
290 }
291
292 opt.mode = DFL_MODE;
293 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294 opt.output_mode = DFL_OUTPUT_MODE;
295 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200296 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000301 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000303 *q++ = '\0';
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (strcmp(p, "mode") == 0) {
306 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000307 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000311 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
313 } else if (strcmp(p, "output_mode") == 0) {
314 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000315 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000317 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000319 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 }
321 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200324 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200326#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200328 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200330 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
332 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000335 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000337 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
342 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000343 goto exit;
344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
347 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000348 goto exit;
349 }
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000352 /*
353 * 1.1. Load the key
354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_printf("\n . Loading the private key ...");
356 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
359 (const unsigned char *) pers,
360 strlen(pers))) != 0) {
361 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
362 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200363 goto exit;
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
367 mbedtls_ctr_drbg_random, &ctr_drbg);
368 if (ret != 0) {
369 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
370 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000371 goto exit;
372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000375
376 /*
377 * 1.2 Print the key
378 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
383 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
386 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
387 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100388 goto exit;
389 }
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
392 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
393 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
394 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
395 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
396 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
397 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
398 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
399 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200403 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
404 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
405 goto exit;
406 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200408#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000412 /*
413 * 1.1. Load the key
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_printf("\n . Loading the public key ...");
416 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (ret != 0) {
421 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
422 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000423 goto exit;
424 }
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000427
428 /*
429 * 1.2 Print the key
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
435 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
438 NULL, &E)) != 0) {
439 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100440 goto exit;
441 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
443 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
444 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200448 if (show_ecp_key(mbedtls_pk_ec(key), 0) != 0) {
449 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
450 goto exit;
451 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200453#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_printf("key type not supported yet\n");
455 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000456 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000457 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100458
459 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
460 write_public_key(&key, opt.output_file);
461 }
462 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
463 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000464 }
465
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100466 exit_code = MBEDTLS_EXIT_SUCCESS;
467
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000468exit:
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 mbedtls_strerror(ret, buf, sizeof(buf));
473 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200474#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200476#endif
477 }
478
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200479#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
481 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
482 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200483#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 mbedtls_pk_free(&key);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 mbedtls_ctr_drbg_free(&ctr_drbg);
488 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200490 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200491#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000494}
Gilles Peskine9552a522023-12-23 18:44:20 +0100495#endif /* program viability conditions */