blob: cc4c4dc727c743a6c64889c7209b50cdf3a9da76 [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
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020012#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C) && \
13 defined(MBEDTLS_FS_IO) && \
14 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/error.h"
16#include "mbedtls/pk.h"
17#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020019#include "mbedtls/entropy.h"
20#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020021
Rich Evans18b78c72015-02-11 14:06:19 +000022#include <stdio.h>
23#include <string.h>
24#endif
Paul Bakkered27a042013-04-18 22:46:23 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000027#define USAGE_OUT \
28 " output_file=%%s default: keyfile.pem\n" \
29 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#define USAGE_OUT \
32 " output_file=%%s default: keyfile.der\n" \
33 " output_format=der default: der\n"
34#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000037#define DFL_OUTPUT_FILENAME "keyfile.pem"
38#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
39#else
40#define DFL_OUTPUT_FILENAME "keyfile.der"
41#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
42#endif
43
44#define DFL_MODE MODE_NONE
45#define DFL_FILENAME "keyfile.key"
46#define DFL_DEBUG_LEVEL 0
47#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020048
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049#define MODE_NONE 0
50#define MODE_PRIVATE 1
51#define MODE_PUBLIC 2
52
53#define OUTPUT_MODE_NONE 0
54#define OUTPUT_MODE_PRIVATE 1
55#define OUTPUT_MODE_PUBLIC 2
56
Paul Bakkerf3df61a2013-08-26 17:22:23 +020057#define OUTPUT_FORMAT_PEM 0
58#define OUTPUT_FORMAT_DER 1
59
Rich Evans18b78c72015-02-11 14:06:19 +000060#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010061 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000062 "\n acceptable parameters:\n" \
63 " mode=private|public default: none\n" \
64 " filename=%%s default: keyfile.key\n" \
65 " output_mode=private|public default: none\n" \
66 USAGE_OUT \
67 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000068
Hanno Beckerb14c3312018-10-16 13:45:22 +010069#if !defined(MBEDTLS_PK_PARSE_C) || \
70 !defined(MBEDTLS_PK_WRITE_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020071 !defined(MBEDTLS_FS_IO) || \
72 !defined(MBEDTLS_ENTROPY_C) || \
73 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010074int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +000075{
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_printf("MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or "
77 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
78 "MBEDTLS_FS_IO not defined.\n");
79 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +000080}
81#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000082
Simon Butcher63cb97e2018-12-06 17:43:31 +000083
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084/*
85 * global options
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +000088 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020089 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000090 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020092 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093} opt;
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020097 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000099 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200100 unsigned char *c = output_buf;
101 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102
Paul Bakker1d569582012-10-03 20:35:44 +0000103 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (opt.output_format == OUTPUT_FORMAT_PEM) {
107 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
108 return ret;
109 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 len = strlen((char *) output_buf);
112 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200113#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200114 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
116 return ret;
117 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118
119 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200120 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if ((f = fopen(output_file, "w")) == NULL) {
124 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200125 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (fwrite(c, 1, len, f) != len) {
128 fclose(f);
129 return -1;
130 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 fclose(f);
133
134 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000141 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200142 unsigned char *c = output_buf;
143 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
Paul Bakker1d569582012-10-03 20:35:44 +0000145 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (opt.output_format == OUTPUT_FORMAT_PEM) {
149 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
150 return ret;
151 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 len = strlen((char *) output_buf);
154 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200155#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200156 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
158 return ret;
159 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200160
161 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100162 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200163 }
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if ((f = fopen(output_file, "w")) == NULL) {
166 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200167 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (fwrite(c, 1, len, f) != len) {
170 fclose(f);
171 return -1;
172 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 fclose(f);
175
176 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177}
178
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200179#if defined(MBEDTLS_ECP_C)
180static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
181{
182 int ret = 0;
183
184 const mbedtls_ecp_curve_info *curve_info =
185 mbedtls_ecp_curve_info_from_grp_id(
186 mbedtls_ecp_keypair_get_group_id(ecp));
187 mbedtls_printf("curve: %s\n", curve_info->name);
188
189 mbedtls_ecp_group grp;
190 mbedtls_ecp_group_init(&grp);
191 mbedtls_mpi D;
192 mbedtls_mpi_init(&D);
193 mbedtls_ecp_point pt;
194 mbedtls_ecp_point_init(&pt);
195 mbedtls_mpi X, Y;
196 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
197
198 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
199 (has_private ? &D : NULL),
200 &pt));
201
202 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
203 size_t len = 0;
204 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
205 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
206 &len, point_bin, sizeof(point_bin)));
207 switch (mbedtls_ecp_get_type(&grp)) {
208 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
209 if ((len & 1) == 0 || point_bin[0] != 0x04) {
210 /* Point in an unxepected format. This shouldn't happen. */
211 ret = -1;
212 goto cleanup;
213 }
214 MBEDTLS_MPI_CHK(
215 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
216 MBEDTLS_MPI_CHK(
217 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
218 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
219 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
220 break;
221 case MBEDTLS_ECP_TYPE_MONTGOMERY:
222 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
223 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
224 break;
225 default:
226 mbedtls_printf(
227 "This program does not yet support listing coordinates for this curve type.\n");
228 break;
229 }
230
231 if (has_private) {
232 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
233 }
234
235cleanup:
236 mbedtls_ecp_group_free(&grp);
237 mbedtls_mpi_free(&D);
238 mbedtls_ecp_point_free(&pt);
239 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
240 return ret;
241}
242#endif
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100246 int ret = 1;
247 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine680747b2021-08-06 14:37:01 +0200248#if defined(MBEDTLS_ERROR_C)
249 char buf[200];
250#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000251 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000252 char *p, *q;
253
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200254 const char *pers = "pkey/key_app";
255 mbedtls_entropy_context entropy;
256 mbedtls_ctr_drbg_context ctr_drbg;
257
Hanno Becker40371ec2017-08-23 06:46:17 +0100258 mbedtls_pk_context key;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200259#if defined(MBEDTLS_RSA_C)
Hanno Becker40371ec2017-08-23 06:46:17 +0100260 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200261#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100262
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263 /*
264 * Set to sane values
265 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_entropy_init(&entropy);
267 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 mbedtls_pk_init(&key);
Gilles Peskine680747b2021-08-06 14:37:01 +0200270#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 memset(buf, 0, sizeof(buf));
Gilles Peskine680747b2021-08-06 14:37:01 +0200272#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200274#if defined(MBEDTLS_USE_PSA_CRYPTO)
275 psa_status_t status = psa_crypto_init();
276 if (status != PSA_SUCCESS) {
277 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
278 (int) status);
279 goto exit;
280 }
281#endif /* MBEDTLS_USE_PSA_CRYPTO */
282
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200283#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
285 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
286 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200287#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100288
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000289 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100290usage:
291 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292 goto exit;
293 }
294
295 opt.mode = DFL_MODE;
296 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000297 opt.output_mode = DFL_OUTPUT_MODE;
298 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200299 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000302 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000304 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000306 *q++ = '\0';
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (strcmp(p, "mode") == 0) {
309 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000310 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000312 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000314 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 }
316 } else if (strcmp(p, "output_mode") == 0) {
317 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000318 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000320 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000322 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 }
324 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200327 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200329#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200331 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200333 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
335 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000340 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
345 mbedtls_printf("\nCannot output a key without reading one.\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_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
350 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000351 goto exit;
352 }
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000355 /*
356 * 1.1. Load the key
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 mbedtls_printf("\n . Loading the private key ...");
359 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
362 (const unsigned char *) pers,
363 strlen(pers))) != 0) {
364 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
365 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200366 goto exit;
367 }
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
370 mbedtls_ctr_drbg_random, &ctr_drbg);
371 if (ret != 0) {
372 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
373 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000374 goto exit;
375 }
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000378
379 /*
380 * 1.2 Print the key
381 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
386 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
389 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
390 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100391 goto exit;
392 }
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
395 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
396 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
397 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
398 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
399 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
400 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
401 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
402 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200406 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
407 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
408 goto exit;
409 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415 /*
416 * 1.1. Load the key
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_printf("\n . Loading the public key ...");
419 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (ret != 0) {
424 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
425 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000426 goto exit;
427 }
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000430
431 /*
432 * 1.2 Print the key
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
438 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
441 NULL, &E)) != 0) {
442 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100443 goto exit;
444 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
446 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
447 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200448#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200451 if (show_ecp_key(mbedtls_pk_ec(key), 0) != 0) {
452 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
453 goto exit;
454 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200456#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 mbedtls_printf("key type not supported yet\n");
458 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000459 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000460 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100461
462 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
463 write_public_key(&key, opt.output_file);
464 }
465 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
466 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000467 }
468
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100469 exit_code = MBEDTLS_EXIT_SUCCESS;
470
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000471exit:
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_strerror(ret, buf, sizeof(buf));
476 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200477#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200479#endif
480 }
481
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200482#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
484 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
485 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200486#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_pk_free(&key);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 mbedtls_ctr_drbg_free(&ctr_drbg);
491 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200492#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200493 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200494#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000497}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200498#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO &&
499 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */