blob: a757cb3e7c7163c3d40a33cf2362622a448cdef3 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
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 Bakkered56b222011-07-13 11:26:43 +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 Bakkered56b222011-07-13 11:26:43 +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_BIGNUM_C) && \
29 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/error.h"
31#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010032#include "mbedtls/pk.h"
Paul Bakkered56b222011-07-13 11:26:43 +000033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <string.h>
35#endif
36
37#define MODE_NONE 0
38#define MODE_PRIVATE 1
39#define MODE_PUBLIC 2
40
41#define DFL_MODE MODE_NONE
42#define DFL_FILENAME "keyfile.key"
43#define DFL_PASSWORD ""
44#define DFL_PASSWORD_FILE ""
45#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000046
Rich Evans18b78c72015-02-11 14:06:19 +000047#define USAGE \
48 "\n usage: key_app param=<>...\n" \
49 "\n acceptable parameters:\n" \
50 " mode=private|public default: none\n" \
51 " filename=%%s default: keyfile.key\n" \
52 " password=%%s default: \"\"\n" \
53 " password_file=%%s default: \"\"\n" \
54 "\n"
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_BIGNUM_C) || \
57 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058int main(void)
Paul Bakker15254952013-09-17 11:24:56 +020059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
62 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020063}
64#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000065
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Paul Bakkered56b222011-07-13 11:26:43 +000067/*
68 * global options
69 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070struct options {
Paul Bakkered56b222011-07-13 11:26:43 +000071 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072 const char *filename; /* filename of the key file */
73 const char *password; /* password for the private key */
74 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000075} opt;
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +000078{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010079 int ret = 1;
80 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000081 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000082 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000083 char *p, *q;
84
Hanno Becker54ebf992017-08-23 06:45:38 +010085 mbedtls_pk_context pk;
86 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
87
Paul Bakkered56b222011-07-13 11:26:43 +000088 /*
89 * Set to sane values
90 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_pk_init(&pk);
92 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +000093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
95 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
96 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +010097
Aditya Deshpande0504ac22023-01-30 15:58:50 +000098 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099usage:
100 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300101 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000102 }
103
104 opt.mode = DFL_MODE;
105 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000106 opt.password = DFL_PASSWORD;
107 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000110 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000112 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 }
Paul Bakkered56b222011-07-13 11:26:43 +0000114 *q++ = '\0';
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if (strcmp(p, "mode") == 0) {
117 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000118 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000120 opt.mode = MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000122 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 }
124 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000125 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000127 opt.password = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000129 opt.password_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000131 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 }
Paul Bakkered56b222011-07-13 11:26:43 +0000133 }
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 if (opt.mode == MODE_PRIVATE) {
136 if (strlen(opt.password) && strlen(opt.password_file)) {
137 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 goto usage;
139 }
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000142 FILE *f;
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 mbedtls_printf("\n . Loading the password file ...");
145 if ((f = fopen(opt.password_file, "rb")) == NULL) {
146 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300147 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000148 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (fgets(buf, sizeof(buf), f) == NULL) {
150 fclose(f);
151 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300152 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200153 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 i = (int) strlen(buf);
157 if (buf[i - 1] == '\n') {
158 buf[i - 1] = '\0';
159 }
160 if (buf[i - 2] == '\r') {
161 buf[i - 2] = '\0';
162 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000163 opt.password = buf;
164 }
165
Paul Bakkered56b222011-07-13 11:26:43 +0000166 /*
167 * 1.1. Load the key
168 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 mbedtls_printf("\n . Loading the private key ...");
170 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password);
Paul Bakkered56b222011-07-13 11:26:43 +0000173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if (ret != 0) {
175 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
176 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300177 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000178 }
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000181
182 /*
183 * 1.2 Print the key
184 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
188 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
191 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
192 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300193 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100194 }
195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
197 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
198 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
199 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
200 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
201 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
202 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
203 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
204 } else
Paul Bakker15254952013-09-17 11:24:56 +0200205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
208 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
209 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
210 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
211 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
212 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL));
213 } else
Paul Bakker15254952013-09-17 11:24:56 +0200214#endif
215 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300217 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200218 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000220 /*
221 * 1.1. Load the key
222 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 mbedtls_printf("\n . Loading the public key ...");
224 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if (ret != 0) {
229 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
230 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300231 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000232 }
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
239 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
242 NULL, &E)) != 0) {
243 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300244 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100245 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
247 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
248 } else
Paul Bakker15254952013-09-17 11:24:56 +0200249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
252 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
253 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
254 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
255 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
256 } else
Paul Bakker15254952013-09-17 11:24:56 +0200257#endif
258 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300260 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200261 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000263 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 }
Paul Bakkered56b222011-07-13 11:26:43 +0000265
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100266 exit_code = MBEDTLS_EXIT_SUCCESS;
267
Ron Eldor6a9257b2017-08-24 14:20:17 +0300268cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
272 mbedtls_strerror(ret, buf, sizeof(buf));
273 mbedtls_printf(" ! Last error was: %s\n", buf);
Hanno Becker54ebf992017-08-23 06:45:38 +0100274 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200275#endif
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 mbedtls_pk_free(&pk);
278 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
279 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
280 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000281
Paul Bakkercce9d772011-11-18 14:26:47 +0000282#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 mbedtls_printf(" + Press Enter to exit this program.\n");
284 fflush(stdout); getchar();
Paul Bakkered56b222011-07-13 11:26:43 +0000285#endif
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */