blob: 19dcdfe494757df5634d047d583620a4251d3566 [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
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_BIGNUM_C) && \
40 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/error.h"
42#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010043#include "mbedtls/pk.h"
Paul Bakkered56b222011-07-13 11:26:43 +000044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <string.h>
46#endif
47
48#define MODE_NONE 0
49#define MODE_PRIVATE 1
50#define MODE_PUBLIC 2
51
52#define DFL_MODE MODE_NONE
53#define DFL_FILENAME "keyfile.key"
54#define DFL_PASSWORD ""
55#define DFL_PASSWORD_FILE ""
56#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000057
Rich Evans18b78c72015-02-11 14:06:19 +000058#define USAGE \
59 "\n usage: key_app param=<>...\n" \
60 "\n acceptable parameters:\n" \
61 " mode=private|public default: none\n" \
62 " filename=%%s default: keyfile.key\n" \
63 " password=%%s default: \"\"\n" \
64 " password_file=%%s default: \"\"\n" \
65 "\n"
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
Simon Butcher63cb97e2018-12-06 17:43:31 +000077
Paul Bakkered56b222011-07-13 11:26:43 +000078/*
79 * global options
80 */
81struct options
82{
83 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020084 const char *filename; /* filename of the key file */
85 const char *password; /* password for the private key */
86 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000087} opt;
88
Paul Bakkered56b222011-07-13 11:26:43 +000089int main( int argc, char *argv[] )
90{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010091 int ret = 1;
92 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000093 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000094 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000095 char *p, *q;
96
Hanno Becker54ebf992017-08-23 06:45:38 +010097 mbedtls_pk_context pk;
98 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
99
Paul Bakkered56b222011-07-13 11:26:43 +0000100 /*
101 * Set to sane values
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200104 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000105
Hanno Becker54ebf992017-08-23 06:45:38 +0100106 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
107 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
108 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
109
Paul Bakkered56b222011-07-13 11:26:43 +0000110 if( argc == 0 )
111 {
112 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300114 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000115 }
116
117 opt.mode = DFL_MODE;
118 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000119 opt.password = DFL_PASSWORD;
120 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000121
122 for( i = 1; i < argc; i++ )
123 {
Paul Bakkered56b222011-07-13 11:26:43 +0000124 p = argv[i];
125 if( ( q = strchr( p, '=' ) ) == NULL )
126 goto usage;
127 *q++ = '\0';
128
129 if( strcmp( p, "mode" ) == 0 )
130 {
131 if( strcmp( q, "private" ) == 0 )
132 opt.mode = MODE_PRIVATE;
133 else if( strcmp( q, "public" ) == 0 )
134 opt.mode = MODE_PUBLIC;
135 else
136 goto usage;
137 }
138 else if( strcmp( p, "filename" ) == 0 )
139 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000140 else if( strcmp( p, "password" ) == 0 )
141 opt.password = q;
142 else if( strcmp( p, "password_file" ) == 0 )
143 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000144 else
145 goto usage;
146 }
147
148 if( opt.mode == MODE_PRIVATE )
149 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 if( strlen( opt.password ) && strlen( opt.password_file ) )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000153 goto usage;
154 }
155
156 if( strlen( opt.password_file ) )
157 {
158 FILE *f;
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000161 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300164 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000165 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200166 if( fgets( buf, sizeof(buf), f ) == NULL )
167 {
168 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300170 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200171 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000172 fclose( f );
173
Paul Bakker840ab202013-11-30 15:14:38 +0100174 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000175 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
176 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
177 opt.password = buf;
178 }
179
Paul Bakkered56b222011-07-13 11:26:43 +0000180 /*
181 * 1.1. Load the key
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000184 fflush( stdout );
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000187
188 if( ret != 0 )
189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300191 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000192 }
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000195
196 /*
197 * 1.2 Print the key
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf( " . Key information ...\n" );
200#if defined(MBEDTLS_RSA_C)
201 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100204
205 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
206 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
207 {
208 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300209 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100210 }
211
Ron Eldorbf470992018-06-27 11:51:46 +0300212 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200220 }
221 else
222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_ECP_C)
224 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300227 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200231 }
232 else
233#endif
234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300236 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200237 }
Paul Bakkered56b222011-07-13 11:26:43 +0000238 }
239 else if( opt.mode == MODE_PUBLIC )
240 {
241 /*
242 * 1.1. Load the key
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000245 fflush( stdout );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000248
249 if( ret != 0 )
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300252 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000253 }
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf( " . Key information ...\n" );
258#if defined(MBEDTLS_RSA_C)
259 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100262
263 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
264 NULL, &E ) ) != 0 )
265 {
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 }
Ron Eldorbf470992018-06-27 11:51:46 +0300269 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
270 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200271 }
272 else
273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_ECP_C)
275 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
280 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200281 }
282 else
283#endif
284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 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 }
Paul Bakkered56b222011-07-13 11:26:43 +0000288 }
289 else
290 goto usage;
291
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)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100297 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100298 {
Ron Eldor7a814262018-06-24 16:34:15 +0300299 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100300 mbedtls_printf( " ! Last error was: %s\n", buf );
301 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200302#endif
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100305 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
306 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
307 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000308
Paul Bakkercce9d772011-11-18 14:26:47 +0000309#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000311 fflush( stdout ); getchar();
312#endif
313
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100314 return( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */