blob: e986ada826c0a60eaa03500289a8ddffab135375 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerbdb912d2012-02-13 23:11:30 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/error.h"
30#include "mbedtls/pk.h"
31#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <stdio.h>
34#include <string.h>
35#endif
Paul Bakkered27a042013-04-18 22:46:23 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000038#define USAGE_OUT \
39 " output_file=%%s default: keyfile.pem\n" \
40 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020041#else
Rich Evans18b78c72015-02-11 14:06:19 +000042#define USAGE_OUT \
43 " output_file=%%s default: keyfile.der\n" \
44 " output_format=der default: der\n"
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000048#define DFL_OUTPUT_FILENAME "keyfile.pem"
49#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
50#else
51#define DFL_OUTPUT_FILENAME "keyfile.der"
52#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
53#endif
54
55#define DFL_MODE MODE_NONE
56#define DFL_FILENAME "keyfile.key"
57#define DFL_DEBUG_LEVEL 0
58#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020059
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060#define MODE_NONE 0
61#define MODE_PRIVATE 1
62#define MODE_PUBLIC 2
63
64#define OUTPUT_MODE_NONE 0
65#define OUTPUT_MODE_PRIVATE 1
66#define OUTPUT_MODE_PUBLIC 2
67
Paul Bakkerf3df61a2013-08-26 17:22:23 +020068#define OUTPUT_FORMAT_PEM 0
69#define OUTPUT_FORMAT_DER 1
70
Rich Evans18b78c72015-02-11 14:06:19 +000071#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010072 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000073 "\n acceptable parameters:\n" \
74 " mode=private|public default: none\n" \
75 " filename=%%s default: keyfile.key\n" \
76 " output_mode=private|public default: none\n" \
77 USAGE_OUT \
78 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000079
Hanno Beckerb14c3312018-10-16 13:45:22 +010080#if !defined(MBEDTLS_PK_PARSE_C) || \
81 !defined(MBEDTLS_PK_WRITE_C) || \
82 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +000084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 mbedtls_printf(
86 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n");
87 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +000088}
89#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000090
Simon Butcher63cb97e2018-12-06 17:43:31 +000091
Paul Bakkerbdb912d2012-02-13 23:11:30 +000092/*
93 * global options
94 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020097 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020099 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200100 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101} opt;
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200105 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000106 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000107 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200108 unsigned char *c = output_buf;
109 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110
Paul Bakker1d569582012-10-03 20:35:44 +0000111 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 if (opt.output_format == OUTPUT_FORMAT_PEM) {
115 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
116 return ret;
117 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 len = strlen((char *) output_buf);
120 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200121#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
124 return ret;
125 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126
127 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200128 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200129 }
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if ((f = fopen(output_file, "w")) == NULL) {
132 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200133 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 if (fwrite(c, 1, len, f) != len) {
136 fclose(f);
137 return -1;
138 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 fclose(f);
141
142 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143}
144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200147 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000149 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200150 unsigned char *c = output_buf;
151 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152
Paul Bakker1d569582012-10-03 20:35:44 +0000153 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (opt.output_format == OUTPUT_FORMAT_PEM) {
157 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
158 return ret;
159 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 len = strlen((char *) output_buf);
162 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200163#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
166 return ret;
167 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168
169 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100170 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200171 }
172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 if ((f = fopen(output_file, "w")) == NULL) {
174 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200175 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 if (fwrite(c, 1, len, f) != len) {
178 fclose(f);
179 return -1;
180 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 fclose(f);
183
184 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185}
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100189 int ret = 1;
190 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200191#if defined(MBEDTLS_ERROR_C)
192 char buf[200];
193#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000194 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195 char *p, *q;
196
Hanno Becker40371ec2017-08-23 06:46:17 +0100197 mbedtls_pk_context key;
198 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
199
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 /*
201 * Set to sane values
202 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 mbedtls_pk_init(&key);
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200204#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 memset(buf, 0, sizeof(buf));
Gilles Peskine3f0722c2021-08-06 14:37:01 +0200206#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000207
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200208#if defined(MBEDTLS_USE_PSA_CRYPTO)
209 psa_status_t status = psa_crypto_init();
210 if (status != PSA_SUCCESS) {
211 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
212 (int) status);
213 goto exit;
214 }
215#endif /* MBEDTLS_USE_PSA_CRYPTO */
216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
218 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
219 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100220
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000221 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222usage:
223 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000224 goto exit;
225 }
226
227 opt.mode = DFL_MODE;
228 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229 opt.output_mode = DFL_OUTPUT_MODE;
230 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200231 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238 *q++ = '\0';
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (strcmp(p, "mode") == 0) {
241 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000242 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244 opt.mode = MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000246 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 }
248 } else if (strcmp(p, "output_mode") == 0) {
249 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000252 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 }
256 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200259 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200261#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200263 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200265 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 }
267 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000270 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 }
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
277 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 goto exit;
279 }
280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
282 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 goto exit;
284 }
285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000287 /*
288 * 1.1. Load the key
289 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 mbedtls_printf("\n . Loading the private key ...");
291 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if (ret != 0) {
296 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
297 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000298 goto exit;
299 }
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000302
303 /*
304 * 1.2 Print the key
305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
310 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
313 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
314 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100315 goto exit;
316 }
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
319 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
320 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
321 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
322 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
323 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
324 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
325 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
326 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
330 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
331 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
332 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
333 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
334 mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL);
335 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200336#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000340 /*
341 * 1.1. Load the key
342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 mbedtls_printf("\n . Loading the public key ...");
344 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (ret != 0) {
349 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
350 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000351 goto exit;
352 }
353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000355
356 /*
357 * 1.2 Print the key
358 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
363 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
366 NULL, &E)) != 0) {
367 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100368 goto exit;
369 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
371 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
372 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
376 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
377 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
378 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
379 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
380 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200381#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 mbedtls_printf("key type not supported yet\n");
383 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000385 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386
387 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
388 write_public_key(&key, opt.output_file);
389 }
390 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
391 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000392 }
393
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100394 exit_code = MBEDTLS_EXIT_SUCCESS;
395
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000396exit:
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 mbedtls_strerror(ret, buf, sizeof(buf));
401 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200402#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200404#endif
405 }
406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
408 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
409 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_pk_free(&key);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200413 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200414#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415
416#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 mbedtls_printf(" + Press Enter to exit this program.\n");
418 fflush(stdout); getchar();
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000419#endif
420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000422}
Hanno Beckerb14c3312018-10-16 13:45:22 +0100423#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */