blob: ba926e31b20bc705f941cff76eb0afe48521fcf7 [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
209 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
210 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 if (argc == 0) {
213usage:
214 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000215 goto exit;
216 }
217
218 opt.mode = DFL_MODE;
219 opt.filename = DFL_FILENAME;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000220 opt.output_mode = DFL_OUTPUT_MODE;
221 opt.output_file = DFL_OUTPUT_FILENAME;
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200222 opt.output_format = DFL_OUTPUT_FORMAT;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000225 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000227 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000229 *q++ = '\0';
230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 if (strcmp(p, "mode") == 0) {
232 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000235 opt.mode = MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 }
239 } else if (strcmp(p, "output_mode") == 0) {
240 if (strcmp(q, "private") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241 opt.output_mode = OUTPUT_MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 } else if (strcmp(q, "public") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000243 opt.output_mode = OUTPUT_MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 }
247 } else if (strcmp(p, "output_format") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 if (strcmp(q, "pem") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200250 opt.output_format = OUTPUT_FORMAT_PEM;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 } else
Manuel Pégourié-Gonnardf9378d82014-06-24 13:11:25 +0200252#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 if (strcmp(q, "der") == 0) {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200254 opt.output_format = OUTPUT_FORMAT_DER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 } else {
Paul Bakkerf3df61a2013-08-26 17:22:23 +0200256 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 }
258 } else if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000259 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000261 opt.output_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000263 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000265 }
266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
268 mbedtls_printf("\nCannot output a key without reading one.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000269 goto exit;
270 }
271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
273 mbedtls_printf("\nCannot output a private key from a public key.\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 goto exit;
275 }
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (opt.mode == MODE_PRIVATE) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278 /*
279 * 1.1. Load the key
280 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 mbedtls_printf("\n . Loading the private key ...");
282 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (ret != 0) {
287 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
288 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000289 goto exit;
290 }
291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000293
294 /*
295 * 1.2 Print the key
296 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
301 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
304 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
305 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100306 goto exit;
307 }
308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
310 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
311 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
312 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
313 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
314 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
315 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
316 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
317 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
321 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
322 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
323 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
324 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
325 mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL);
326 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200327#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 mbedtls_printf("key type not supported yet\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000331 /*
332 * 1.1. Load the key
333 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 mbedtls_printf("\n . Loading the public key ...");
335 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (ret != 0) {
340 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
341 (unsigned int) -ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000342 goto exit;
343 }
344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000346
347 /*
348 * 1.2 Print the key
349 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
354 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker40371ec2017-08-23 06:46:17 +0100355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
357 NULL, &E)) != 0) {
358 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker40371ec2017-08-23 06:46:17 +0100359 goto exit;
360 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
362 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
363 } else
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
367 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
368 mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL);
369 mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL);
370 mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL);
371 } else
Paul Bakker2e24ca72013-09-18 15:21:27 +0200372#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 mbedtls_printf("key type not supported yet\n");
374 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000375 goto usage;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000376 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377
378 if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
379 write_public_key(&key, opt.output_file);
380 }
381 if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
382 write_private_key(&key, opt.output_file);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000383 }
384
Andres Amaya Garciaed684882018-04-29 20:07:30 +0100385 exit_code = MBEDTLS_EXIT_SUCCESS;
386
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000387exit:
388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 mbedtls_strerror(ret, buf, sizeof(buf));
392 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200393#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200395#endif
396 }
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
399 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
400 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Hanno Becker40371ec2017-08-23 06:46:17 +0100401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 mbedtls_pk_free(&key);
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 */