blob: a2df832ff817c72947a72271940efe2e77d7adb7 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkered56b222011-07-13 11:26:43 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered56b222011-07-13 11:26:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkered56b222011-07-13 11:26:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_BIGNUM_C) && \
67 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/error.h"
69#include "mbedtls/rsa.h"
70#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000071
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <string.h>
73#endif
74
75#define MODE_NONE 0
76#define MODE_PRIVATE 1
77#define MODE_PUBLIC 2
78
79#define DFL_MODE MODE_NONE
80#define DFL_FILENAME "keyfile.key"
81#define DFL_PASSWORD ""
82#define DFL_PASSWORD_FILE ""
83#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000084
Rich Evans18b78c72015-02-11 14:06:19 +000085#define USAGE \
86 "\n usage: key_app param=<>...\n" \
87 "\n acceptable parameters:\n" \
88 " mode=private|public default: none\n" \
89 " filename=%%s default: keyfile.key\n" \
90 " password=%%s default: \"\"\n" \
91 " password_file=%%s default: \"\"\n" \
92 "\n"
93
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if !defined(MBEDTLS_BIGNUM_C) || \
96 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000097int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
100 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200101 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +0200102}
103#else
Paul Bakkered56b222011-07-13 11:26:43 +0000104/*
105 * global options
106 */
107struct options
108{
109 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200110 const char *filename; /* filename of the key file */
111 const char *password; /* password for the private key */
112 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000113} opt;
114
Paul Bakkered56b222011-07-13 11:26:43 +0000115int main( int argc, char *argv[] )
116{
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100117 int ret = 1;
118 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000119 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000120 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000121 char *p, *q;
122
Hanno Becker54ebf992017-08-23 06:45:38 +0100123 mbedtls_pk_context pk;
124 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
125
Paul Bakkered56b222011-07-13 11:26:43 +0000126 /*
127 * Set to sane values
128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200130 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000131
Hanno Becker54ebf992017-08-23 06:45:38 +0100132 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
133 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
134 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
135
Paul Bakkered56b222011-07-13 11:26:43 +0000136 if( argc == 0 )
137 {
138 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( USAGE );
Ron Eldore1440892017-08-24 14:20:17 +0300140 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000141 }
142
143 opt.mode = DFL_MODE;
144 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000145 opt.password = DFL_PASSWORD;
146 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000147
148 for( i = 1; i < argc; i++ )
149 {
Paul Bakkered56b222011-07-13 11:26:43 +0000150 p = argv[i];
151 if( ( q = strchr( p, '=' ) ) == NULL )
152 goto usage;
153 *q++ = '\0';
154
155 if( strcmp( p, "mode" ) == 0 )
156 {
157 if( strcmp( q, "private" ) == 0 )
158 opt.mode = MODE_PRIVATE;
159 else if( strcmp( q, "public" ) == 0 )
160 opt.mode = MODE_PUBLIC;
161 else
162 goto usage;
163 }
164 else if( strcmp( p, "filename" ) == 0 )
165 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000166 else if( strcmp( p, "password" ) == 0 )
167 opt.password = q;
168 else if( strcmp( p, "password_file" ) == 0 )
169 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000170 else
171 goto usage;
172 }
173
174 if( opt.mode == MODE_PRIVATE )
175 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000176 if( strlen( opt.password ) && strlen( opt.password_file ) )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000179 goto usage;
180 }
181
182 if( strlen( opt.password_file ) )
183 {
184 FILE *f;
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000187 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300190 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000191 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200192 if( fgets( buf, sizeof(buf), f ) == NULL )
193 {
194 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300196 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200197 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000198 fclose( f );
199
Paul Bakker840ab202013-11-30 15:14:38 +0100200 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000201 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
202 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
203 opt.password = buf;
204 }
205
Paul Bakkered56b222011-07-13 11:26:43 +0000206 /*
207 * 1.1. Load the key
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000210 fflush( stdout );
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000213
214 if( ret != 0 )
215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300217 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000218 }
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000221
222 /*
223 * 1.2 Print the key
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( " . Key information ...\n" );
226#if defined(MBEDTLS_RSA_C)
227 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100230
231 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
232 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
233 {
234 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300235 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100236 }
237
Ron Eldor5146ef32018-06-27 11:51:46 +0300238 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
245 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200246 }
247 else
248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_ECP_C)
250 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300253 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 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200257 }
258 else
259#endif
260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300262 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200263 }
Paul Bakkered56b222011-07-13 11:26:43 +0000264 }
265 else if( opt.mode == MODE_PUBLIC )
266 {
267 /*
268 * 1.1. Load the key
269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000271 fflush( stdout );
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000274
275 if( ret != 0 )
276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300278 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( " . Key information ...\n" );
284#if defined(MBEDTLS_RSA_C)
285 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100288
289 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
290 NULL, &E ) ) != 0 )
291 {
292 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300293 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100294 }
Ron Eldor5146ef32018-06-27 11:51:46 +0300295 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
296 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200297 }
298 else
299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_ECP_C)
301 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300304 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200307 }
308 else
309#endif
310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300312 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200313 }
Paul Bakkered56b222011-07-13 11:26:43 +0000314 }
315 else
316 goto usage;
317
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100318 exit_code = MBEDTLS_EXIT_SUCCESS;
319
Ron Eldore1440892017-08-24 14:20:17 +0300320cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100323 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100324 {
Ron Eldor45486b12018-06-24 16:34:15 +0300325 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100326 mbedtls_printf( " ! Last error was: %s\n", buf );
327 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200328#endif
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100331 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
332 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
333 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000334
Paul Bakkercce9d772011-11-18 14:26:47 +0000335#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000337 fflush( stdout ); getchar();
338#endif
339
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200340 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */