blob: 82a4de12a58bd603170da5ab28df7e8188912419 [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
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +020094#if defined(MBEDTLS_USE_PSA_CRYPTO)
95 psa_status_t status = psa_crypto_init();
96 if (status != PSA_SUCCESS) {
97 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
98 (int) status);
99 goto cleanup;
100 }
101#endif /* MBEDTLS_USE_PSA_CRYPTO */
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
104 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
105 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100106
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000107 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108usage:
109 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300110 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000111 }
112
113 opt.mode = DFL_MODE;
114 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000115 opt.password = DFL_PASSWORD;
116 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000119 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000121 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 }
Paul Bakkered56b222011-07-13 11:26:43 +0000123 *q++ = '\0';
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 if (strcmp(p, "mode") == 0) {
126 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000127 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000129 opt.mode = MODE_PUBLIC;
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 }
133 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000134 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000136 opt.password = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 opt.password_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000140 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 }
Paul Bakkered56b222011-07-13 11:26:43 +0000142 }
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (opt.mode == MODE_PRIVATE) {
145 if (strlen(opt.password) && strlen(opt.password_file)) {
146 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000147 goto usage;
148 }
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000151 FILE *f;
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 mbedtls_printf("\n . Loading the password file ...");
154 if ((f = fopen(opt.password_file, "rb")) == NULL) {
155 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300156 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000157 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 if (fgets(buf, sizeof(buf), f) == NULL) {
159 fclose(f);
160 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300161 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200162 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 i = (int) strlen(buf);
166 if (buf[i - 1] == '\n') {
167 buf[i - 1] = '\0';
168 }
169 if (buf[i - 2] == '\r') {
170 buf[i - 2] = '\0';
171 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000172 opt.password = buf;
173 }
174
Paul Bakkered56b222011-07-13 11:26:43 +0000175 /*
176 * 1.1. Load the key
177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_printf("\n . Loading the private key ...");
179 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password);
Paul Bakkered56b222011-07-13 11:26:43 +0000182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 if (ret != 0) {
184 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
185 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300186 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000187 }
188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000190
191 /*
192 * 1.2 Print the key
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
197 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
200 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
201 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300202 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100203 }
204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
206 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
207 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
208 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
209 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
210 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
211 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
212 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
213 } else
Paul Bakker15254952013-09-17 11:24:56 +0200214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
217 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
218 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
219 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
220 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
221 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL));
222 } else
Paul Bakker15254952013-09-17 11:24:56 +0200223#endif
224 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300226 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200227 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000229 /*
230 * 1.1. Load the key
231 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 mbedtls_printf("\n . Loading the public key ...");
233 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (ret != 0) {
238 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
239 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300240 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000241 }
242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
248 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
251 NULL, &E)) != 0) {
252 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300253 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100254 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
256 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
257 } else
Paul Bakker15254952013-09-17 11:24:56 +0200258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
261 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
262 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
263 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
264 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
265 } else
Paul Bakker15254952013-09-17 11:24:56 +0200266#endif
267 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300269 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200270 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000272 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 }
Paul Bakkered56b222011-07-13 11:26:43 +0000274
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100275 exit_code = MBEDTLS_EXIT_SUCCESS;
276
Ron Eldor6a9257b2017-08-24 14:20:17 +0300277cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
281 mbedtls_strerror(ret, buf, sizeof(buf));
282 mbedtls_printf(" ! Last error was: %s\n", buf);
Hanno Becker54ebf992017-08-23 06:45:38 +0100283 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200284#endif
285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 mbedtls_pk_free(&pk);
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200287 mbedtls_psa_crypto_free();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
289 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
290 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000291
Paul Bakkercce9d772011-11-18 14:26:47 +0000292#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 mbedtls_printf(" + Press Enter to exit this program.\n");
294 fflush(stdout); getchar();
Paul Bakkered56b222011-07-13 11:26:43 +0000295#endif
296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */