blob: 2f2b32c51e49812d0c7d1e83648e11220051c6fe [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 Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerbdb912d2012-02-13 23:11:30 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/error.h"
18#include "mbedtls/pk.h"
19#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <stdio.h>
22#include <string.h>
23#endif
Paul Bakkered27a042013-04-18 22:46:23 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000026#define USAGE_OUT \
27 " output_file=%%s default: keyfile.pem\n" \
28 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020029#else
Rich Evans18b78c72015-02-11 14:06:19 +000030#define USAGE_OUT \
31 " output_file=%%s default: keyfile.der\n" \
32 " output_format=der default: der\n"
33#endif
34
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 DFL_OUTPUT_FILENAME "keyfile.pem"
37#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
38#else
39#define DFL_OUTPUT_FILENAME "keyfile.der"
40#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
41#endif
42
43#define DFL_MODE MODE_NONE
44#define DFL_FILENAME "keyfile.key"
45#define DFL_DEBUG_LEVEL 0
46#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020047
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048#define MODE_NONE 0
49#define MODE_PRIVATE 1
50#define MODE_PUBLIC 2
51
52#define OUTPUT_MODE_NONE 0
53#define OUTPUT_MODE_PRIVATE 1
54#define OUTPUT_MODE_PUBLIC 2
55
Paul Bakkerf3df61a2013-08-26 17:22:23 +020056#define OUTPUT_FORMAT_PEM 0
57#define OUTPUT_FORMAT_DER 1
58
Rich Evans18b78c72015-02-11 14:06:19 +000059#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010060 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000061 "\n acceptable parameters:\n" \
62 " mode=private|public default: none\n" \
63 " filename=%%s default: keyfile.key\n" \
64 " output_mode=private|public default: none\n" \
65 USAGE_OUT \
66 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000067
Hanno Beckerb14c3312018-10-16 13:45:22 +010068#if !defined(MBEDTLS_PK_PARSE_C) || \
69 !defined(MBEDTLS_PK_WRITE_C) || \
70 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +000072{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_printf(
74 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n");
75 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +000076}
77#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000078
Simon Butcher63cb97e2018-12-06 17:43:31 +000079
Paul Bakkerbdb912d2012-02-13 23:11:30 +000080/*
81 * global options
82 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +000084 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020085 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000086 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020087 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +020088 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000089} opt;
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092{
Paul Bakkerf3df61a2013-08-26 17:22:23 +020093 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000094 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +000095 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +020096 unsigned char *c = output_buf;
97 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098
Paul Bakker1d569582012-10-03 20:35:44 +000099 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if (opt.output_format == OUTPUT_FORMAT_PEM) {
103 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
104 return ret;
105 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 len = strlen((char *) output_buf);
108 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200109#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200110 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
112 return ret;
113 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200114
115 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200116 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200117 }
118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 if ((f = fopen(output_file, "w")) == NULL) {
120 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200121 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 if (fwrite(c, 1, len, f) != len) {
124 fclose(f);
125 return -1;
126 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 fclose(f);
129
130 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131}
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200135 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000137 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200138 unsigned char *c = output_buf;
139 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
Paul Bakker1d569582012-10-03 20:35:44 +0000141 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (opt.output_format == OUTPUT_FORMAT_PEM) {
145 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
146 return ret;
147 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 len = strlen((char *) output_buf);
150 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200151#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200152 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
154 return ret;
155 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200156
157 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100158 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200159 }
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if ((f = fopen(output_file, "w")) == NULL) {
162 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200163 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if (fwrite(c, 1, len, f) != len) {
166 fclose(f);
167 return -1;
168 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 fclose(f);
171
172 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173}
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100177 int ret = 1;
178 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200179#if defined(MBEDTLS_ERROR_C)
180 char buf[200];
181#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000182 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183 char *p, *q;
184
Hanno Becker40371ec2017-08-23 06:46:17 +0100185 mbedtls_pk_context key;
186 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
187
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 /*
189 * Set to sane values
190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 mbedtls_pk_init(&key);
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200192#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 memset(buf, 0, sizeof(buf));
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200194#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200196#if defined(MBEDTLS_USE_PSA_CRYPTO)
197 psa_status_t status = psa_crypto_init();
198 if (status != PSA_SUCCESS) {
199 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
200 (int) status);
201 goto exit;
202 }
203#endif /* MBEDTLS_USE_PSA_CRYPTO */
204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
206 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
207 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100208
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000209 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210usage:
211 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212 goto exit;
213 }
214
215 opt.mode = DFL_MODE;
216 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000217 opt.output_mode = DFL_OUTPUT_MODE;
218 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200219 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000222 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000226 *q++ = '\0';
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if (strcmp(p, "mode") == 0) {
229 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000230 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232 opt.mode = MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 }
236 } else if (strcmp(p, "output_mode") == 0) {
237 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 }
244 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200247 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200249#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200251 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200253 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 }
255 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262 }
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
265 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266 goto exit;
267 }
268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
270 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271 goto exit;
272 }
273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275 /*
276 * 1.1. Load the key
277 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 mbedtls_printf("\n . Loading the private key ...");
279 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if (ret != 0) {
284 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
285 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000286 goto exit;
287 }
288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000290
291 /*
292 * 1.2 Print the key
293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
298 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
301 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
302 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100303 goto exit;
304 }
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
307 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
308 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
309 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
310 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
311 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
312 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
313 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
314 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
318 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
319 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
320 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
321 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
322 mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL);
323 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200324#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000328 /*
329 * 1.1. Load the key
330 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 mbedtls_printf("\n . Loading the public key ...");
332 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (ret != 0) {
337 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
338 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000339 goto exit;
340 }
341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000343
344 /*
345 * 1.2 Print the key
346 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
351 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
354 NULL, &E)) != 0) {
355 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100356 goto exit;
357 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
359 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
360 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200361#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
364 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
365 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
366 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
367 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
368 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200369#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 mbedtls_printf("key type not supported yet\n");
371 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000372 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000373 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374
375 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
376 write_public_key(&key, opt.output_file);
377 }
378 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
379 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000380 }
381
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100382 exit_code = MBEDTLS_EXIT_SUCCESS;
383
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384exit:
385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 mbedtls_strerror(ret, buf, sizeof(buf));
389 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200390#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200392#endif
393 }
394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
396 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
397 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 mbedtls_pk_free(&key);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200400#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200401 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200402#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403
404#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 mbedtls_printf(" + Press Enter to exit this program.\n");
406 fflush(stdout); getchar();
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000407#endif
408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000410}
Hanno Beckerb14c3312018-10-16 13:45:22 +0100411#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */