blob: ac1b59442e1c4d5963f6bc75d1854f3639f5c8f6 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkered56b222011-07-13 11:26:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakkered56b222011-07-13 11:26:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_BIGNUM_C) && \
17 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/error.h"
19#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010020#include "mbedtls/pk.h"
Paul Bakkered56b222011-07-13 11:26:43 +000021
Rich Evans18b78c72015-02-11 14:06:19 +000022#include <string.h>
23#endif
24
25#define MODE_NONE 0
26#define MODE_PRIVATE 1
27#define MODE_PUBLIC 2
28
29#define DFL_MODE MODE_NONE
30#define DFL_FILENAME "keyfile.key"
31#define DFL_PASSWORD ""
32#define DFL_PASSWORD_FILE ""
33#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000034
Rich Evans18b78c72015-02-11 14:06:19 +000035#define USAGE \
36 "\n usage: key_app param=<>...\n" \
37 "\n acceptable parameters:\n" \
38 " mode=private|public default: none\n" \
39 " filename=%%s default: keyfile.key\n" \
40 " password=%%s default: \"\"\n" \
41 " password_file=%%s default: \"\"\n" \
42 "\n"
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_BIGNUM_C) || \
45 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046int main(void)
Paul Bakker15254952013-09-17 11:24:56 +020047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
50 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020051}
52#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000053
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Paul Bakkered56b222011-07-13 11:26:43 +000055/*
56 * global options
57 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058struct options {
Paul Bakkered56b222011-07-13 11:26:43 +000059 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020060 const char *filename; /* filename of the key file */
61 const char *password; /* password for the private key */
62 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000063} opt;
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +000066{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010067 int ret = 1;
68 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000069 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000070 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000071 char *p, *q;
72
Hanno Becker54ebf992017-08-23 06:45:38 +010073 mbedtls_pk_context pk;
74 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
75
Paul Bakkered56b222011-07-13 11:26:43 +000076 /*
77 * Set to sane values
78 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 mbedtls_pk_init(&pk);
80 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +000081
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +020082#if defined(MBEDTLS_USE_PSA_CRYPTO)
83 psa_status_t status = psa_crypto_init();
84 if (status != PSA_SUCCESS) {
85 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
86 (int) status);
87 goto cleanup;
88 }
89#endif /* MBEDTLS_USE_PSA_CRYPTO */
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
92 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
93 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +010094
Aditya Deshpande0504ac22023-01-30 15:58:50 +000095 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096usage:
97 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +030098 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +000099 }
100
101 opt.mode = DFL_MODE;
102 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000103 opt.password = DFL_PASSWORD;
104 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000107 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000109 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 }
Paul Bakkered56b222011-07-13 11:26:43 +0000111 *q++ = '\0';
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if (strcmp(p, "mode") == 0) {
114 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000115 opt.mode = MODE_PRIVATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000117 opt.mode = MODE_PUBLIC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000119 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 }
121 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000122 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000124 opt.password = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000126 opt.password_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000128 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 }
Paul Bakkered56b222011-07-13 11:26:43 +0000130 }
131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if (opt.mode == MODE_PRIVATE) {
133 if (strlen(opt.password) && strlen(opt.password_file)) {
134 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000135 goto usage;
136 }
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000139 FILE *f;
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_printf("\n . Loading the password file ...");
142 if ((f = fopen(opt.password_file, "rb")) == NULL) {
143 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300144 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000145 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (fgets(buf, sizeof(buf), f) == NULL) {
147 fclose(f);
148 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300149 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200150 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 i = (int) strlen(buf);
154 if (buf[i - 1] == '\n') {
155 buf[i - 1] = '\0';
156 }
157 if (buf[i - 2] == '\r') {
158 buf[i - 2] = '\0';
159 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160 opt.password = buf;
161 }
162
Paul Bakkered56b222011-07-13 11:26:43 +0000163 /*
164 * 1.1. Load the key
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_printf("\n . Loading the private key ...");
167 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password);
Paul Bakkered56b222011-07-13 11:26:43 +0000170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 if (ret != 0) {
172 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
173 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300174 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000175 }
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000178
179 /*
180 * 1.2 Print the key
181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
185 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
188 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
189 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300190 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100191 }
192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
194 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
195 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
196 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
197 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
198 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
199 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
200 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
201 } else
Paul Bakker15254952013-09-17 11:24:56 +0200202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
205 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
206 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
207 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
208 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
209 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL));
210 } else
Paul Bakker15254952013-09-17 11:24:56 +0200211#endif
212 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300214 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200215 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000217 /*
218 * 1.1. Load the key
219 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_printf("\n . Loading the public key ...");
221 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 if (ret != 0) {
226 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
227 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300228 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000229 }
230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
236 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
239 NULL, &E)) != 0) {
240 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300241 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100242 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
244 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
245 } else
Paul Bakker15254952013-09-17 11:24:56 +0200246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
249 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
250 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL));
251 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL));
252 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL));
253 } else
Paul Bakker15254952013-09-17 11:24:56 +0200254#endif
255 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300257 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200258 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000260 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 }
Paul Bakkered56b222011-07-13 11:26:43 +0000262
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100263 exit_code = MBEDTLS_EXIT_SUCCESS;
264
Ron Eldor6a9257b2017-08-24 14:20:17 +0300265cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
269 mbedtls_strerror(ret, buf, sizeof(buf));
270 mbedtls_printf(" ! Last error was: %s\n", buf);
Hanno Becker54ebf992017-08-23 06:45:38 +0100271 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200272#endif
273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_pk_free(&pk);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200275#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200276 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200277#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 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 */