blob: 194c4102dd7c8d479bff8c991a2d400c6bd91069 [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkered56b222011-07-13 11:26:43 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakkered56b222011-07-13 11:26:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_BIGNUM_C) && \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020013 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
14 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/error.h"
16#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010017#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020018#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <string.h>
22#endif
23
24#define MODE_NONE 0
25#define MODE_PRIVATE 1
26#define MODE_PUBLIC 2
27
28#define DFL_MODE MODE_NONE
29#define DFL_FILENAME "keyfile.key"
30#define DFL_PASSWORD ""
31#define DFL_PASSWORD_FILE ""
32#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000033
Rich Evans18b78c72015-02-11 14:06:19 +000034#define USAGE \
35 "\n usage: key_app param=<>...\n" \
36 "\n acceptable parameters:\n" \
37 " mode=private|public default: none\n" \
38 " filename=%%s default: keyfile.key\n" \
39 " password=%%s default: \"\"\n" \
40 " password_file=%%s default: \"\"\n" \
41 "\n"
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020044 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
45 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010049 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
50 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
51 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020052}
53#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Simon Butcher63cb97e2018-12-06 17:43:31 +000055
Paul Bakkered56b222011-07-13 11:26:43 +000056/*
57 * global options
58 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059struct options {
Paul Bakkered56b222011-07-13 11:26:43 +000060 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061 const char *filename; /* filename of the key file */
62 const char *password; /* password for the private key */
63 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000064} opt;
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +000067{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010068 int ret = 1;
69 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000070 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000071 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000072 char *p, *q;
73
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020074 const char *pers = "pkey/key_app";
75 mbedtls_entropy_context entropy;
76 mbedtls_ctr_drbg_context ctr_drbg;
77
Hanno Becker54ebf992017-08-23 06:45:38 +010078 mbedtls_pk_context pk;
79 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
80
Paul Bakkered56b222011-07-13 11:26:43 +000081 /*
82 * Set to sane values
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_entropy_init(&entropy);
85 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_pk_init(&pk);
88 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +000089
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020090#if defined(MBEDTLS_USE_PSA_CRYPTO)
91 psa_status_t status = psa_crypto_init();
92 if (status != PSA_SUCCESS) {
93 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
94 (int) status);
95 goto cleanup;
96 }
97#endif /* MBEDTLS_USE_PSA_CRYPTO */
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
100 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
101 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100102
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000103 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100104usage:
105 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300106 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000107 }
108
109 opt.mode = DFL_MODE;
110 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000111 opt.password = DFL_PASSWORD;
112 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000115 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000117 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 }
Paul Bakkered56b222011-07-13 11:26:43 +0000119 *q++ = '\0';
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (strcmp(p, "mode") == 0) {
122 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000123 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000125 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000127 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
129 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000130 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000132 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000134 opt.password_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000136 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
Paul Bakkered56b222011-07-13 11:26:43 +0000138 }
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (opt.mode == MODE_PRIVATE) {
141 if (strlen(opt.password) && strlen(opt.password_file)) {
142 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000143 goto usage;
144 }
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000147 FILE *f;
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_printf("\n . Loading the password file ...");
150 if ((f = fopen(opt.password_file, "rb")) == NULL) {
151 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300152 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000153 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (fgets(buf, sizeof(buf), f) == NULL) {
155 fclose(f);
156 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300157 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200158 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 i = (int) strlen(buf);
162 if (buf[i - 1] == '\n') {
163 buf[i - 1] = '\0';
164 }
165 if (buf[i - 2] == '\r') {
166 buf[i - 2] = '\0';
167 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000168 opt.password = buf;
169 }
170
Paul Bakkered56b222011-07-13 11:26:43 +0000171 /*
172 * 1.1. Load the key
173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_printf("\n . Loading the private key ...");
175 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
178 (const unsigned char *) pers,
179 strlen(pers))) != 0) {
180 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
181 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200182 goto cleanup;
183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
186 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkered56b222011-07-13 11:26:43 +0000187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (ret != 0) {
189 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
190 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300191 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000192 }
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000195
196 /*
197 * 1.2 Print the key
198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
202 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
205 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
206 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300207 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100208 }
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
211 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
212 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
213 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
214 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
215 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
216 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
217 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
218 } else
Paul Bakker15254952013-09-17 11:24:56 +0200219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
222 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
223 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
224 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
225 NULL));
226 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
227 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
228 NULL));
229 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
230 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
231 NULL));
232 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL));
233 } else
Paul Bakker15254952013-09-17 11:24:56 +0200234#endif
235 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300237 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200238 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000240 /*
241 * 1.1. Load the key
242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_printf("\n . Loading the public key ...");
244 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ret != 0) {
249 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
250 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300251 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000252 }
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
259 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
262 NULL, &E)) != 0) {
263 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300264 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100265 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
267 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
268 } else
Paul Bakker15254952013-09-17 11:24:56 +0200269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
272 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
273 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
274 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
275 NULL));
276 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
277 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
278 NULL));
279 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
280 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
281 NULL));
282 } else
Paul Bakker15254952013-09-17 11:24:56 +0200283#endif
284 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300286 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200287 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000289 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Paul Bakkered56b222011-07-13 11:26:43 +0000291
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100292 exit_code = MBEDTLS_EXIT_SUCCESS;
293
Ron Eldor6a9257b2017-08-24 14:20:17 +0300294cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
298 mbedtls_strerror(ret, buf, sizeof(buf));
299 mbedtls_printf(" ! Last error was: %s\n", buf);
Hanno Becker54ebf992017-08-23 06:45:38 +0100300 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200301#endif
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_ctr_drbg_free(&ctr_drbg);
304 mbedtls_entropy_free(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_pk_free(&pk);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200306#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200307 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200308#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
310 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
311 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000314}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200315#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
316 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */