blob: 6024e0c6dcc3a937a4d8d572c0131e263834c88b [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útif744bd72020-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útif744bd72020-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 Garcia0faf1a52018-04-29 20:02:18 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia0faf1a52018-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(MBEDTLS_BIGNUM_C) || \
95 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000096int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
99 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200100 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +0200101}
102#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000103
Simon Butcher63cb97e2018-12-06 17:43:31 +0000104
Paul Bakkered56b222011-07-13 11:26:43 +0000105/*
106 * global options
107 */
108struct options
109{
110 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200111 const char *filename; /* filename of the key file */
112 const char *password; /* password for the private key */
113 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000114} opt;
115
Paul Bakkered56b222011-07-13 11:26:43 +0000116int main( int argc, char *argv[] )
117{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100118 int ret = 1;
119 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000120 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000121 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000122 char *p, *q;
123
Hanno Becker54ebf992017-08-23 06:45:38 +0100124 mbedtls_pk_context pk;
125 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
126
Paul Bakkered56b222011-07-13 11:26:43 +0000127 /*
128 * Set to sane values
129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200131 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000132
Hanno Becker54ebf992017-08-23 06:45:38 +0100133 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
134 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
135 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
136
Paul Bakkered56b222011-07-13 11:26:43 +0000137 if( argc == 0 )
138 {
139 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300141 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000142 }
143
144 opt.mode = DFL_MODE;
145 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000146 opt.password = DFL_PASSWORD;
147 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000148
149 for( i = 1; i < argc; i++ )
150 {
Paul Bakkered56b222011-07-13 11:26:43 +0000151 p = argv[i];
152 if( ( q = strchr( p, '=' ) ) == NULL )
153 goto usage;
154 *q++ = '\0';
155
156 if( strcmp( p, "mode" ) == 0 )
157 {
158 if( strcmp( q, "private" ) == 0 )
159 opt.mode = MODE_PRIVATE;
160 else if( strcmp( q, "public" ) == 0 )
161 opt.mode = MODE_PUBLIC;
162 else
163 goto usage;
164 }
165 else if( strcmp( p, "filename" ) == 0 )
166 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000167 else if( strcmp( p, "password" ) == 0 )
168 opt.password = q;
169 else if( strcmp( p, "password_file" ) == 0 )
170 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000171 else
172 goto usage;
173 }
174
175 if( opt.mode == MODE_PRIVATE )
176 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000177 if( strlen( opt.password ) && strlen( opt.password_file ) )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000180 goto usage;
181 }
182
183 if( strlen( opt.password_file ) )
184 {
185 FILE *f;
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000188 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300191 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000192 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200193 if( fgets( buf, sizeof(buf), f ) == NULL )
194 {
195 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300197 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200198 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000199 fclose( f );
200
Paul Bakker840ab202013-11-30 15:14:38 +0100201 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000202 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
203 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
204 opt.password = buf;
205 }
206
Paul Bakkered56b222011-07-13 11:26:43 +0000207 /*
208 * 1.1. Load the key
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000211 fflush( stdout );
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000214
215 if( ret != 0 )
216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300218 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000219 }
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000222
223 /*
224 * 1.2 Print the key
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " . Key information ...\n" );
227#if defined(MBEDTLS_RSA_C)
228 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100231
232 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
233 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
234 {
235 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300236 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100237 }
238
Ron Eldorbf470992018-06-27 11:51:46 +0300239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
245 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
246 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200247 }
248 else
249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_ECP_C)
251 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300254 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
255 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
256 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
257 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200258 }
259 else
260#endif
261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300263 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200264 }
Paul Bakkered56b222011-07-13 11:26:43 +0000265 }
266 else if( opt.mode == MODE_PUBLIC )
267 {
268 /*
269 * 1.1. Load the key
270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000272 fflush( stdout );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000275
276 if( ret != 0 )
277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300279 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000280 }
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_printf( " . Key information ...\n" );
285#if defined(MBEDTLS_RSA_C)
286 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100289
290 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
291 NULL, &E ) ) != 0 )
292 {
293 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300294 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100295 }
Ron Eldorbf470992018-06-27 11:51:46 +0300296 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
297 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200298 }
299 else
300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_ECP_C)
302 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
307 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200308 }
309 else
310#endif
311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300313 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200314 }
Paul Bakkered56b222011-07-13 11:26:43 +0000315 }
316 else
317 goto usage;
318
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100319 exit_code = MBEDTLS_EXIT_SUCCESS;
320
Ron Eldor6a9257b2017-08-24 14:20:17 +0300321cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100324 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100325 {
Ron Eldor7a814262018-06-24 16:34:15 +0300326 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100327 mbedtls_printf( " ! Last error was: %s\n", buf );
328 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200329#endif
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100332 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
333 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
334 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000335
Paul Bakkercce9d772011-11-18 14:26:47 +0000336#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000338 fflush( stdout ); getchar();
339#endif
340
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200341 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */