blob: c80dcd0a17c96522b447207e7e49d022ca2a9f98 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkered56b222011-07-13 11:26:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_BIGNUM_C) && \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020025 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
26 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/error.h"
28#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020030#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020032
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <string.h>
34#endif
35
36#define MODE_NONE 0
37#define MODE_PRIVATE 1
38#define MODE_PUBLIC 2
39
40#define DFL_MODE MODE_NONE
41#define DFL_FILENAME "keyfile.key"
42#define DFL_PASSWORD ""
43#define DFL_PASSWORD_FILE ""
44#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000045
Rich Evans18b78c72015-02-11 14:06:19 +000046#define USAGE \
47 "\n usage: key_app param=<>...\n" \
48 "\n acceptable parameters:\n" \
49 " mode=private|public default: none\n" \
50 " filename=%%s default: keyfile.key\n" \
51 " password=%%s default: \"\"\n" \
52 " password_file=%%s default: \"\"\n" \
53 "\n"
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020056 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
57 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010061 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
62 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
63 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020064}
65#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Simon Butcher63cb97e2018-12-06 17:43:31 +000067
Paul Bakkered56b222011-07-13 11:26:43 +000068/*
69 * global options
70 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071struct options {
Paul Bakkered56b222011-07-13 11:26:43 +000072 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *filename; /* filename of the key file */
74 const char *password; /* password for the private key */
75 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000076} opt;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +000079{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010080 int ret = 1;
81 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000082 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000083 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000084 char *p, *q;
85
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020086 const char *pers = "pkey/key_app";
87 mbedtls_entropy_context entropy;
88 mbedtls_ctr_drbg_context ctr_drbg;
89
Hanno Becker54ebf992017-08-23 06:45:38 +010090 mbedtls_pk_context pk;
91 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
92
Paul Bakkered56b222011-07-13 11:26:43 +000093 /*
94 * Set to sane values
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_entropy_init(&entropy);
97 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_pk_init(&pk);
100 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +0000101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
103 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
104 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100105
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000106 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100107usage:
108 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300109 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000110 }
111
112 opt.mode = DFL_MODE;
113 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000114 opt.password = DFL_PASSWORD;
115 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000118 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000120 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Paul Bakkered56b222011-07-13 11:26:43 +0000122 *q++ = '\0';
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (strcmp(p, "mode") == 0) {
125 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000126 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000128 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000130 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 }
132 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000133 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000135 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000137 opt.password_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000139 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 }
Paul Bakkered56b222011-07-13 11:26:43 +0000141 }
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (opt.mode == MODE_PRIVATE) {
144 if (strlen(opt.password) && strlen(opt.password_file)) {
145 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000146 goto usage;
147 }
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 FILE *f;
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_printf("\n . Loading the password file ...");
153 if ((f = fopen(opt.password_file, "rb")) == NULL) {
154 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300155 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000156 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (fgets(buf, sizeof(buf), f) == NULL) {
158 fclose(f);
159 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300160 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200161 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 i = (int) strlen(buf);
165 if (buf[i - 1] == '\n') {
166 buf[i - 1] = '\0';
167 }
168 if (buf[i - 2] == '\r') {
169 buf[i - 2] = '\0';
170 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000171 opt.password = buf;
172 }
173
Paul Bakkered56b222011-07-13 11:26:43 +0000174 /*
175 * 1.1. Load the key
176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_printf("\n . Loading the private key ...");
178 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
181 (const unsigned char *) pers,
182 strlen(pers))) != 0) {
183 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
184 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200185 goto cleanup;
186 }
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
189 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkered56b222011-07-13 11:26:43 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (ret != 0) {
192 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
193 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300194 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000198
199 /*
200 * 1.2 Print the key
201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
205 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
208 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
209 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300210 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100211 }
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
214 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
215 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
216 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
217 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
218 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
219 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
220 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
221 } else
Paul Bakker15254952013-09-17 11:24:56 +0200222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
225 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
226 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
227 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
228 NULL));
229 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
230 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
231 NULL));
232 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
233 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
234 NULL));
235 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL));
236 } else
Paul Bakker15254952013-09-17 11:24:56 +0200237#endif
238 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300240 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200241 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000243 /*
244 * 1.1. Load the key
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_printf("\n . Loading the public key ...");
247 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ret != 0) {
252 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
253 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300254 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
262 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
265 NULL, &E)) != 0) {
266 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300267 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100268 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
270 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
271 } else
Paul Bakker15254952013-09-17 11:24:56 +0200272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
275 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
276 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
277 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
278 NULL));
279 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
280 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
281 NULL));
282 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
283 &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
284 NULL));
285 } else
Paul Bakker15254952013-09-17 11:24:56 +0200286#endif
287 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300289 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200290 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000292 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 }
Paul Bakkered56b222011-07-13 11:26:43 +0000294
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100295 exit_code = MBEDTLS_EXIT_SUCCESS;
296
Ron Eldor6a9257b2017-08-24 14:20:17 +0300297cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
301 mbedtls_strerror(ret, buf, sizeof(buf));
302 mbedtls_printf(" ! Last error was: %s\n", buf);
Hanno Becker54ebf992017-08-23 06:45:38 +0100303 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200304#endif
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_ctr_drbg_free(&ctr_drbg);
307 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_pk_free(&pk);
310 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
311 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
312 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000315}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200316#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
317 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */