blob: e8f3e85a9e6d0a59db21f31c18da35ace790de9d [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020024#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C) && \
25 defined(MBEDTLS_FS_IO) && \
26 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/error.h"
28#include "mbedtls/pk.h"
29#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000030
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020031#include "mbedtls/entropy.h"
32#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <stdio.h>
35#include <string.h>
36#endif
Paul Bakkered27a042013-04-18 22:46:23 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000039#define USAGE_OUT \
40 " output_file=%%s default: keyfile.pem\n" \
41 " output_format=pem|der default: pem\n"
Paul Bakkered27a042013-04-18 22:46:23 +020042#else
Rich Evans18b78c72015-02-11 14:06:19 +000043#define USAGE_OUT \
44 " output_file=%%s default: keyfile.der\n" \
45 " output_format=der default: der\n"
46#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_WRITE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000049#define DFL_OUTPUT_FILENAME "keyfile.pem"
50#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
51#else
52#define DFL_OUTPUT_FILENAME "keyfile.der"
53#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
54#endif
55
56#define DFL_MODE MODE_NONE
57#define DFL_FILENAME "keyfile.key"
58#define DFL_DEBUG_LEVEL 0
59#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
Paul Bakkered27a042013-04-18 22:46:23 +020060
Paul Bakkerbdb912d2012-02-13 23:11:30 +000061#define MODE_NONE 0
62#define MODE_PRIVATE 1
63#define MODE_PUBLIC 2
64
65#define OUTPUT_MODE_NONE 0
66#define OUTPUT_MODE_PRIVATE 1
67#define OUTPUT_MODE_PUBLIC 2
68
Paul Bakkerf3df61a2013-08-26 17:22:23 +020069#define OUTPUT_FORMAT_PEM 0
70#define OUTPUT_FORMAT_DER 1
71
Rich Evans18b78c72015-02-11 14:06:19 +000072#define USAGE \
Hanno Becker40371ec2017-08-23 06:46:17 +010073 "\n usage: key_app_writer param=<>...\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000074 "\n acceptable parameters:\n" \
75 " mode=private|public default: none\n" \
76 " filename=%%s default: keyfile.key\n" \
77 " output_mode=private|public default: none\n" \
78 USAGE_OUT \
79 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000080
Hanno Beckerb14c3312018-10-16 13:45:22 +010081#if !defined(MBEDTLS_PK_PARSE_C) || \
82 !defined(MBEDTLS_PK_WRITE_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020083 !defined(MBEDTLS_FS_IO) || \
84 !defined(MBEDTLS_ENTROPY_C) || \
85 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010086int main(void)
Rich Evans18b78c72015-02-11 14:06:19 +000087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf("MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or "
89 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
90 "MBEDTLS_FS_IO not defined.\n");
91 mbedtls_exit(0);
Rich Evans18b78c72015-02-11 14:06:19 +000092}
93#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000094
Simon Butcher63cb97e2018-12-06 17:43:31 +000095
Paul Bakkerbdb912d2012-02-13 23:11:30 +000096/*
97 * global options
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099struct options {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000100 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 const char *filename; /* filename of the key file */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000102 int output_mode; /* the output mode to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 const char *output_file; /* where to store the constructed key file */
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200104 int output_format; /* the output format to use */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105} opt;
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int write_public_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200109 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000111 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200112 unsigned char *c = output_buf;
113 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114
Paul Bakker1d569582012-10-03 20:35:44 +0000115 memset(output_buf, 0, 16000);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (opt.output_format == OUTPUT_FORMAT_PEM) {
119 if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
120 return ret;
121 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 len = strlen((char *) output_buf);
124 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200125#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200126 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
128 return ret;
129 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200130
131 len = ret;
Ron Eldorbb51cb32018-01-07 18:10:43 +0200132 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200133 }
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if ((f = fopen(output_file, "w")) == NULL) {
136 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200137 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (fwrite(c, 1, len, f) != len) {
140 fclose(f);
141 return -1;
142 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 fclose(f);
145
146 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150{
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200151 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152 FILE *f;
Paul Bakker1d569582012-10-03 20:35:44 +0000153 unsigned char output_buf[16000];
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200154 unsigned char *c = output_buf;
155 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000156
Paul Bakker1d569582012-10-03 20:35:44 +0000157 memset(output_buf, 0, 16000);
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (opt.output_format == OUTPUT_FORMAT_PEM) {
161 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
162 return ret;
163 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 len = strlen((char *) output_buf);
166 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200167#endif
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200168 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
170 return ret;
171 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200172
173 len = ret;
Christian Walthera92c5452018-11-28 13:32:27 +0100174 c = output_buf + sizeof(output_buf) - len;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if ((f = fopen(output_file, "w")) == NULL) {
178 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200179 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (fwrite(c, 1, len, f) != len) {
182 fclose(f);
183 return -1;
184 }
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 fclose(f);
187
188 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189}
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192{
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100193 int ret = 1;
194 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskine680747b2021-08-06 14:37:01 +0200195#if defined(MBEDTLS_ERROR_C)
196 char buf[200];
197#endif
Paul Bakker1d569582012-10-03 20:35:44 +0000198 int i;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199 char *p, *q;
200
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200201 const char *pers = "pkey/key_app";
202 mbedtls_entropy_context entropy;
203 mbedtls_ctr_drbg_context ctr_drbg;
204
Hanno Becker40371ec2017-08-23 06:46:17 +0100205 mbedtls_pk_context key;
206 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
207
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208 /*
209 * Set to sane values
210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_entropy_init(&entropy);
212 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_pk_init(&key);
Gilles Peskine680747b2021-08-06 14:37:01 +0200215#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memset(buf, 0, sizeof(buf));
Gilles Peskine680747b2021-08-06 14:37:01 +0200217#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000218
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200219#if defined(MBEDTLS_USE_PSA_CRYPTO)
220 psa_status_t status = psa_crypto_init();
221 if (status != PSA_SUCCESS) {
222 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
223 (int) status);
224 goto exit;
225 }
226#endif /* MBEDTLS_USE_PSA_CRYPTO */
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
229 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
230 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100231
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000232 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100233usage:
234 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235 goto exit;
236 }
237
238 opt.mode = DFL_MODE;
239 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000240 opt.output_mode = DFL_OUTPUT_MODE;
241 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200242 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000247 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 *q++ = '\0';
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (strcmp(p, "mode") == 0) {
252 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000253 opt.mode = 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.mode = 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_mode") == 0) {
260 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 }
267 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200270 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200272#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200274 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200276 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 }
278 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000279 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 }
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
288 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 goto exit;
290 }
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
293 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000294 goto exit;
295 }
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000298 /*
299 * 1.1. Load the key
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_printf("\n . Loading the private key ...");
302 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
305 (const unsigned char *) pers,
306 strlen(pers))) != 0) {
307 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
308 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200309 goto exit;
310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
313 mbedtls_ctr_drbg_random, &ctr_drbg);
314 if (ret != 0) {
315 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
316 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000317 goto exit;
318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000321
322 /*
323 * 1.2 Print the key
324 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
329 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
332 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
333 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100334 goto exit;
335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
338 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
339 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
340 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
341 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
342 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
343 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
344 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
345 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
349 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
350 mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
351 mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
352 mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
353 mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL);
354 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000359 /*
360 * 1.1. Load the key
361 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_printf("\n . Loading the public key ...");
363 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (ret != 0) {
368 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
369 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000370 goto exit;
371 }
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000374
375 /*
376 * 1.2 Print the key
377 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
382 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
385 NULL, &E)) != 0) {
386 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100387 goto exit;
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 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
395 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
396 mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
397 mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
398 mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
399 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_printf("key type not supported yet\n");
402 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000403 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000404 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100405
406 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
407 write_public_key(&key, opt.output_file);
408 }
409 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
410 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000411 }
412
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100413 exit_code = MBEDTLS_EXIT_SUCCESS;
414
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415exit:
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_strerror(ret, buf, sizeof(buf));
420 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200421#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200423#endif
424 }
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
427 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
428 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_pk_free(&key);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_ctr_drbg_free(&ctr_drbg);
433 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200434#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200435 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200436#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000439}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200440#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO &&
441 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */