blob: 3153eed00ca1991405564012096b9a0e939ba869 [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 Garciaf57bccf2018-04-29 20:02:18 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020034#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf57bccf2018-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"
43#include "mbedtls/x509.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
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_BIGNUM_C) || \
69 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000070int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
73 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020074 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020075}
76#else
Paul Bakkered56b222011-07-13 11:26:43 +000077/*
78 * global options
79 */
80struct options
81{
82 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 const char *filename; /* filename of the key file */
84 const char *password; /* password for the private key */
85 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000086} opt;
87
Paul Bakkered56b222011-07-13 11:26:43 +000088int main( int argc, char *argv[] )
89{
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +010090 int ret = 1;
91 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000092 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000093 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000094 char *p, *q;
95
Hanno Becker54ebf992017-08-23 06:45:38 +010096 mbedtls_pk_context pk;
97 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
98
Paul Bakkered56b222011-07-13 11:26:43 +000099 /*
100 * Set to sane values
101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200103 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000104
Hanno Becker54ebf992017-08-23 06:45:38 +0100105 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
106 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
107 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
108
Paul Bakkered56b222011-07-13 11:26:43 +0000109 if( argc == 0 )
110 {
111 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( USAGE );
Ron Eldore1440892017-08-24 14:20:17 +0300113 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000114 }
115
116 opt.mode = DFL_MODE;
117 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000118 opt.password = DFL_PASSWORD;
119 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000120
121 for( i = 1; i < argc; i++ )
122 {
Paul Bakkered56b222011-07-13 11:26:43 +0000123 p = argv[i];
124 if( ( q = strchr( p, '=' ) ) == NULL )
125 goto usage;
126 *q++ = '\0';
127
128 if( strcmp( p, "mode" ) == 0 )
129 {
130 if( strcmp( q, "private" ) == 0 )
131 opt.mode = MODE_PRIVATE;
132 else if( strcmp( q, "public" ) == 0 )
133 opt.mode = MODE_PUBLIC;
134 else
135 goto usage;
136 }
137 else if( strcmp( p, "filename" ) == 0 )
138 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000139 else if( strcmp( p, "password" ) == 0 )
140 opt.password = q;
141 else if( strcmp( p, "password_file" ) == 0 )
142 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000143 else
144 goto usage;
145 }
146
147 if( opt.mode == MODE_PRIVATE )
148 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000149 if( strlen( opt.password ) && strlen( opt.password_file ) )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000152 goto usage;
153 }
154
155 if( strlen( opt.password_file ) )
156 {
157 FILE *f;
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300163 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200165 if( fgets( buf, sizeof(buf), f ) == NULL )
166 {
167 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300169 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200170 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000171 fclose( f );
172
Paul Bakker840ab202013-11-30 15:14:38 +0100173 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000174 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
175 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
176 opt.password = buf;
177 }
178
Paul Bakkered56b222011-07-13 11:26:43 +0000179 /*
180 * 1.1. Load the key
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000183 fflush( stdout );
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000186
187 if( ret != 0 )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300190 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000191 }
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000194
195 /*
196 * 1.2 Print the key
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_printf( " . Key information ...\n" );
199#if defined(MBEDTLS_RSA_C)
200 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100203
204 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
205 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
206 {
207 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300208 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100209 }
210
Ron Eldor5146ef32018-06-27 11:51:46 +0300211 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
212 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200219 }
220 else
221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_ECP_C)
223 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300226 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200230 }
231 else
232#endif
233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300235 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200236 }
Paul Bakkered56b222011-07-13 11:26:43 +0000237 }
238 else if( opt.mode == MODE_PUBLIC )
239 {
240 /*
241 * 1.1. Load the key
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000244 fflush( stdout );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000247
248 if( ret != 0 )
249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300251 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000252 }
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " . Key information ...\n" );
257#if defined(MBEDTLS_RSA_C)
258 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100261
262 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
263 NULL, &E ) ) != 0 )
264 {
265 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300266 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100267 }
Ron Eldor5146ef32018-06-27 11:51:46 +0300268 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
269 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200270 }
271 else
272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_ECP_C)
274 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300277 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
279 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200280 }
281 else
282#endif
283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300285 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200286 }
Paul Bakkered56b222011-07-13 11:26:43 +0000287 }
288 else
289 goto usage;
290
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100291 exit_code = MBEDTLS_EXIT_SUCCESS;
292
Ron Eldore1440892017-08-24 14:20:17 +0300293cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100296 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100297 {
Ron Eldor45486b12018-06-24 16:34:15 +0300298 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100299 mbedtls_printf( " ! Last error was: %s\n", buf );
300 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100304 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
305 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
306 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000307
Paul Bakkercce9d772011-11-18 14:26:47 +0000308#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000310 fflush( stdout ); getchar();
311#endif
312
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200313 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */