blob: d34cbe1fb0a2a8f6ef26688ba00065c0a4330178 [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/pk.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020029#include "mbedtls/entropy.h"
30#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020031
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
33#include <string.h>
Paul Bakkered27a042013-04-18 22:46:23 +020034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000036#define USAGE_OUT \
37 " output_file=%%s default: keyfile.pem\n" \
38 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020039#else
Rich Evans18b78c72015-02-11 14:06:19 +000040#define USAGE_OUT \
41 " output_file=%%s default: keyfile.der\n" \
42 " output_format=der default: der\n"
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000046#define DFL_OUTPUT_FILENAME "keyfile.pem"
47#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
48#else
49#define DFL_OUTPUT_FILENAME "keyfile.der"
50#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
51#endif
52
53#define DFL_MODE MODE_NONE
54#define DFL_FILENAME "keyfile.key"
55#define DFL_DEBUG_LEVEL 0
56#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020057
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058#define MODE_NONE 0
59#define MODE_PRIVATE 1
60#define MODE_PUBLIC 2
61
62#define OUTPUT_MODE_NONE 0
63#define OUTPUT_MODE_PRIVATE 1
64#define OUTPUT_MODE_PUBLIC 2
65
Paul Bakkerf3df61a2013-08-26 17:22:23 +020066#define OUTPUT_FORMAT_PEM 0
67#define OUTPUT_FORMAT_DER 1
68
Rich Evans18b78c72015-02-11 14:06:19 +000069#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010070 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000071 "\n acceptable parameters:\n" \
72 " mode=private|public default: none\n" \
73 " filename=%%s default: keyfile.key\n" \
74 " output_mode=private|public default: none\n" \
75 USAGE_OUT \
76 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000077
Simon Butcher63cb97e2018-12-06 17:43:31 +000078
Paul Bakkerbdb912d2012-02-13 23:11:30 +000079/*
80 * global options
81 */
Gilles Peskine449bd832023-01-11 14:50:10 +010082struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +000083 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020084 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000085 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020086 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020087 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088} opt;
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000091{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020092 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000094 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020095 unsigned char *c = output_buf;
96 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000097
Paul Bakker1d569582012-10-03 20:35:44 +000098 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (opt.output_format == OUTPUT_FORMAT_PEM) {
102 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
103 return ret;
104 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 len = strlen((char *) output_buf);
107 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200108#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200109 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
111 return ret;
112 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200113
114 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200115 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if ((f = fopen(output_file, "w")) == NULL) {
119 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200120 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (fwrite(c, 1, len, f) != len) {
123 fclose(f);
124 return -1;
125 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 fclose(f);
128
129 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130}
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000136 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200137 unsigned char *c = output_buf;
138 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
Paul Bakker1d569582012-10-03 20:35:44 +0000140 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (opt.output_format == OUTPUT_FORMAT_PEM) {
144 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
145 return ret;
146 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 len = strlen((char *) output_buf);
149 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200150#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200151 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
153 return ret;
154 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200155
156 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100157 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if ((f = fopen(output_file, "w")) == NULL) {
161 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200162 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (fwrite(c, 1, len, f) != len) {
165 fclose(f);
166 return -1;
167 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 fclose(f);
170
171 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172}
173
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200174#if defined(MBEDTLS_ECP_C)
175static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
176{
177 int ret = 0;
178
179 const mbedtls_ecp_curve_info *curve_info =
180 mbedtls_ecp_curve_info_from_grp_id(
181 mbedtls_ecp_keypair_get_group_id(ecp));
182 mbedtls_printf("curve: %s\n", curve_info->name);
183
184 mbedtls_ecp_group grp;
185 mbedtls_ecp_group_init(&grp);
186 mbedtls_mpi D;
187 mbedtls_mpi_init(&D);
188 mbedtls_ecp_point pt;
189 mbedtls_ecp_point_init(&pt);
190 mbedtls_mpi X, Y;
191 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
192
193 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
194 (has_private ? &D : NULL),
195 &pt));
196
197 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
198 size_t len = 0;
199 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
200 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
201 &len, point_bin, sizeof(point_bin)));
202 switch (mbedtls_ecp_get_type(&grp)) {
203 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
204 if ((len & 1) == 0 || point_bin[0] != 0x04) {
205 /* Point in an unxepected format. This shouldn't happen. */
206 ret = -1;
207 goto cleanup;
208 }
209 MBEDTLS_MPI_CHK(
210 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
211 MBEDTLS_MPI_CHK(
212 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
213 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
214 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
215 break;
216 case MBEDTLS_ECP_TYPE_MONTGOMERY:
217 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
218 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
219 break;
220 default:
221 mbedtls_printf(
222 "This program does not yet support listing coordinates for this curve type.\n");
223 break;
224 }
225
226 if (has_private) {
227 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
228 }
229
230cleanup:
231 mbedtls_ecp_group_free(&grp);
232 mbedtls_mpi_free(&D);
233 mbedtls_ecp_point_free(&pt);
234 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
235 return ret;
236}
237#endif
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100241 int ret = 1;
242 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine680747b2021-08-06 14:37:01 +0200243#if defined(MBEDTLS_ERROR_C)
244 char buf[200];
245#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000246 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 char *p, *q;
248
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200249 const char *pers = "pkey/key_app";
250 mbedtls_entropy_context entropy;
251 mbedtls_ctr_drbg_context ctr_drbg;
252
Hanno Becker40371ec2017-08-23 06:46:17 +0100253 mbedtls_pk_context key;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200254#if defined(MBEDTLS_RSA_C)
Hanno Becker40371ec2017-08-23 06:46:17 +0100255 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200256#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100257
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258 /*
259 * Set to sane values
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_entropy_init(&entropy);
262 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_pk_init(&key);
Gilles Peskine680747b2021-08-06 14:37:01 +0200265#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 memset(buf, 0, sizeof(buf));
Gilles Peskine680747b2021-08-06 14:37:01 +0200267#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200269#if defined(MBEDTLS_USE_PSA_CRYPTO)
270 psa_status_t status = psa_crypto_init();
271 if (status != PSA_SUCCESS) {
272 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
273 (int) status);
274 goto exit;
275 }
276#endif /* MBEDTLS_USE_PSA_CRYPTO */
277
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200278#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
280 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
281 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200282#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100283
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000284 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100285usage:
286 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287 goto exit;
288 }
289
290 opt.mode = DFL_MODE;
291 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 opt.output_mode = DFL_OUTPUT_MODE;
293 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200294 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000299 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000301 *q++ = '\0';
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (strcmp(p, "mode") == 0) {
304 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000305 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000307 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 }
311 } else if (strcmp(p, "output_mode") == 0) {
312 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000313 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000315 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000317 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
319 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200322 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200324#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200326 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200328 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
330 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000331 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000335 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000337 }
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
340 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000341 goto exit;
342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
345 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000346 goto exit;
347 }
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000350 /*
351 * 1.1. Load the key
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_printf("\n . Loading the private key ...");
354 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
357 (const unsigned char *) pers,
358 strlen(pers))) != 0) {
359 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
360 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200361 goto exit;
362 }
363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
365 mbedtls_ctr_drbg_random, &ctr_drbg);
366 if (ret != 0) {
367 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
368 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000369 goto exit;
370 }
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000373
374 /*
375 * 1.2 Print the key
376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
381 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
384 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
385 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100386 goto exit;
387 }
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
390 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
391 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
392 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
393 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
394 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
395 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
396 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
397 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200401 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
402 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
403 goto exit;
404 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200406#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000410 /*
411 * 1.1. Load the key
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_printf("\n . Loading the public key ...");
414 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ret != 0) {
419 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
420 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000421 goto exit;
422 }
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000425
426 /*
427 * 1.2 Print the key
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
433 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
436 NULL, &E)) != 0) {
437 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100438 goto exit;
439 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
441 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
442 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200446 if (show_ecp_key(mbedtls_pk_ec(key), 0) != 0) {
447 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
448 goto exit;
449 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200451#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_printf("key type not supported yet\n");
453 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000454 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000455 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100456
457 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
458 write_public_key(&key, opt.output_file);
459 }
460 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
461 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000462 }
463
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100464 exit_code = MBEDTLS_EXIT_SUCCESS;
465
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000466exit:
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#ifdef MBEDTLS_ERROR_C
Harry Ramsey9c664052024-10-16 14:08:19 +0100470 mbedtls_printf("Error code: %d", ret);
471 /* mbedtls_strerror(ret, buf, sizeof(buf));
472 mbedtls_printf(" - %s\n", buf); */
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200473#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200475#endif
476 }
477
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200478#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
480 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
481 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200482#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 mbedtls_pk_free(&key);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 mbedtls_ctr_drbg_free(&ctr_drbg);
487 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200488#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200489 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200490#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000493}
Gilles Peskine9552a522023-12-23 18:44:20 +0100494#endif /* program viability conditions */