blob: c07c56464ebe6743748aa94c2171c71e43c16c60 [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 Peskine449bd832023-01-11 14:50:10 +0100179int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100181 int ret = 1;
182 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine680747b2021-08-06 14:37:01 +0200183#if defined(MBEDTLS_ERROR_C)
184 char buf[200];
185#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000186 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187 char *p, *q;
188
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200189 const char *pers = "pkey/key_app";
190 mbedtls_entropy_context entropy;
191 mbedtls_ctr_drbg_context ctr_drbg;
192
Hanno Becker40371ec2017-08-23 06:46:17 +0100193 mbedtls_pk_context key;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200194#if defined(MBEDTLS_RSA_C)
Hanno Becker40371ec2017-08-23 06:46:17 +0100195 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200196#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100197
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 /*
199 * Set to sane values
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_entropy_init(&entropy);
202 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_pk_init(&key);
Gilles Peskine680747b2021-08-06 14:37:01 +0200205#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memset(buf, 0, sizeof(buf));
Gilles Peskine680747b2021-08-06 14:37:01 +0200207#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200209#if defined(MBEDTLS_USE_PSA_CRYPTO)
210 psa_status_t status = psa_crypto_init();
211 if (status != PSA_SUCCESS) {
212 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
213 (int) status);
214 goto exit;
215 }
216#endif /* MBEDTLS_USE_PSA_CRYPTO */
217
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200218#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
220 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
221 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200222#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100223
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000224 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100225usage:
226 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227 goto exit;
228 }
229
230 opt.mode = DFL_MODE;
231 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 opt.output_mode = DFL_OUTPUT_MODE;
233 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200234 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241 *q++ = '\0';
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (strcmp(p, "mode") == 0) {
244 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 }
251 } else if (strcmp(p, "output_mode") == 0) {
252 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
259 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200262 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200264#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200266 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200268 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 }
270 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277 }
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
280 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 goto exit;
282 }
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
285 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286 goto exit;
287 }
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000290 /*
291 * 1.1. Load the key
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_printf("\n . Loading the private key ...");
294 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
297 (const unsigned char *) pers,
298 strlen(pers))) != 0) {
299 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
300 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200301 goto exit;
302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
305 mbedtls_ctr_drbg_random, &ctr_drbg);
306 if (ret != 0) {
307 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
308 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000309 goto exit;
310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000313
314 /*
315 * 1.2 Print the key
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
321 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
324 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
325 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100326 goto exit;
327 }
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
330 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
331 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
332 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
333 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
334 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
335 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
336 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
337 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
341 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
342 mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
343 mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
344 mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
345 mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL);
346 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200347#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000351 /*
352 * 1.1. Load the key
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_printf("\n . Loading the public key ...");
355 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (ret != 0) {
360 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
361 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000362 goto exit;
363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000366
367 /*
368 * 1.2 Print the key
369 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
374 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
377 NULL, &E)) != 0) {
378 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100379 goto exit;
380 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
382 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
383 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
387 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
388 mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
389 mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
390 mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
391 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200392#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_printf("key type not supported yet\n");
394 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000395 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000396 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100397
398 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
399 write_public_key(&key, opt.output_file);
400 }
401 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
402 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403 }
404
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100405 exit_code = MBEDTLS_EXIT_SUCCESS;
406
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000407exit:
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_strerror(ret, buf, sizeof(buf));
412 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200413#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200415#endif
416 }
417
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200418#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
420 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
421 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200422#endif /* MBEDTLS_RSA_C */
Hanno Becker40371ec2017-08-23 06:46:17 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_pk_free(&key);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_ctr_drbg_free(&ctr_drbg);
427 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200428#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200429 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200430#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000433}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200434#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO &&
435 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */