blob: 845d600b51deb2834d831c05a7ea9a31a9b9872d [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered56b222011-07-13 11:26:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkered56b222011-07-13 11:26:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BIGNUM_C) && \
39 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/error.h"
41#include "mbedtls/rsa.h"
42#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000043
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <string.h>
45#endif
46
47#define MODE_NONE 0
48#define MODE_PRIVATE 1
49#define MODE_PUBLIC 2
50
51#define DFL_MODE MODE_NONE
52#define DFL_FILENAME "keyfile.key"
53#define DFL_PASSWORD ""
54#define DFL_PASSWORD_FILE ""
55#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000056
Rich Evans18b78c72015-02-11 14:06:19 +000057#define USAGE \
58 "\n usage: key_app param=<>...\n" \
59 "\n acceptable parameters:\n" \
60 " mode=private|public default: none\n" \
61 " filename=%%s default: keyfile.key\n" \
62 " password=%%s default: \"\"\n" \
63 " password_file=%%s default: \"\"\n" \
64 "\n"
65
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_BIGNUM_C) || \
68 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000069int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
72 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker15254952013-09-17 11:24:56 +020073 return( 0 );
74}
75#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000076
77#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
78void mbedtls_param_failed( char* failure_condition, char* file, int line )
79{
80 mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition );
81 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
82}
83#endif
84
Paul Bakkered56b222011-07-13 11:26:43 +000085/*
86 * global options
87 */
88struct options
89{
90 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 const char *filename; /* filename of the key file */
92 const char *password; /* password for the private key */
93 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000094} opt;
95
Paul Bakkered56b222011-07-13 11:26:43 +000096int main( int argc, char *argv[] )
97{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010098 int ret = 1;
99 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000100 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000101 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000102 char *p, *q;
103
Hanno Becker54ebf992017-08-23 06:45:38 +0100104 mbedtls_pk_context pk;
105 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
106
Paul Bakkered56b222011-07-13 11:26:43 +0000107 /*
108 * Set to sane values
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200111 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000112
Hanno Becker54ebf992017-08-23 06:45:38 +0100113 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
114 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
115 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
116
Paul Bakkered56b222011-07-13 11:26:43 +0000117 if( argc == 0 )
118 {
119 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300121 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000122 }
123
124 opt.mode = DFL_MODE;
125 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000126 opt.password = DFL_PASSWORD;
127 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000128
129 for( i = 1; i < argc; i++ )
130 {
Paul Bakkered56b222011-07-13 11:26:43 +0000131 p = argv[i];
132 if( ( q = strchr( p, '=' ) ) == NULL )
133 goto usage;
134 *q++ = '\0';
135
136 if( strcmp( p, "mode" ) == 0 )
137 {
138 if( strcmp( q, "private" ) == 0 )
139 opt.mode = MODE_PRIVATE;
140 else if( strcmp( q, "public" ) == 0 )
141 opt.mode = MODE_PUBLIC;
142 else
143 goto usage;
144 }
145 else if( strcmp( p, "filename" ) == 0 )
146 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000147 else if( strcmp( p, "password" ) == 0 )
148 opt.password = q;
149 else if( strcmp( p, "password_file" ) == 0 )
150 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000151 else
152 goto usage;
153 }
154
155 if( opt.mode == MODE_PRIVATE )
156 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000157 if( strlen( opt.password ) && strlen( opt.password_file ) )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160 goto usage;
161 }
162
163 if( strlen( opt.password_file ) )
164 {
165 FILE *f;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000168 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300171 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000172 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200173 if( fgets( buf, sizeof(buf), f ) == NULL )
174 {
175 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300177 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200178 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000179 fclose( f );
180
Paul Bakker840ab202013-11-30 15:14:38 +0100181 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000182 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
183 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
184 opt.password = buf;
185 }
186
Paul Bakkered56b222011-07-13 11:26:43 +0000187 /*
188 * 1.1. Load the key
189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000191 fflush( stdout );
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000194
195 if( ret != 0 )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300198 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000199 }
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000202
203 /*
204 * 1.2 Print the key
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " . Key information ...\n" );
207#if defined(MBEDTLS_RSA_C)
208 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100211
212 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
213 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
214 {
215 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300216 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100217 }
218
Ron Eldorbf470992018-06-27 11:51:46 +0300219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200227 }
228 else
229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_ECP_C)
231 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300234 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
236 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200238 }
239 else
240#endif
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300243 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200244 }
Paul Bakkered56b222011-07-13 11:26:43 +0000245 }
246 else if( opt.mode == MODE_PUBLIC )
247 {
248 /*
249 * 1.1. Load the key
250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000252 fflush( stdout );
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000255
256 if( ret != 0 )
257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300259 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000260 }
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_printf( " . Key information ...\n" );
265#if defined(MBEDTLS_RSA_C)
266 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100269
270 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
271 NULL, &E ) ) != 0 )
272 {
273 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300274 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100275 }
Ron Eldorbf470992018-06-27 11:51:46 +0300276 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
277 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200278 }
279 else
280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#if defined(MBEDTLS_ECP_C)
282 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300285 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
286 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
287 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200288 }
289 else
290#endif
291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300293 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200294 }
Paul Bakkered56b222011-07-13 11:26:43 +0000295 }
296 else
297 goto usage;
298
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100299 exit_code = MBEDTLS_EXIT_SUCCESS;
300
Ron Eldor6a9257b2017-08-24 14:20:17 +0300301cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100304 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100305 {
Ron Eldor7a814262018-06-24 16:34:15 +0300306 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100307 mbedtls_printf( " ! Last error was: %s\n", buf );
308 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200309#endif
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100312 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
313 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
314 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000315
Paul Bakkercce9d772011-11-18 14:26:47 +0000316#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000318 fflush( stdout ); getchar();
319#endif
320
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100321 return( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */