blob: 0e30be4b202a126d0dd8552c896bcce3caafb2f4 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkered56b222011-07-13 11:26:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_BIGNUM_C) && \
38 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/error.h"
40#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010041#include "mbedtls/pk.h"
Paul Bakkered56b222011-07-13 11:26:43 +000042
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020043#include "test/random.h"
44
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");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020073 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020074}
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é-Gonnard84dea012021-06-15 11:29:26 +0200186 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password,
187 mbedtls_test_rnd_std_rand, NULL );
Paul Bakkered56b222011-07-13 11:26:43 +0000188
189 if( ret != 0 )
190 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200191 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300192 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000193 }
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000196
197 /*
198 * 1.2 Print the key
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " . Key information ...\n" );
201#if defined(MBEDTLS_RSA_C)
202 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100205
206 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
207 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
208 {
209 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300210 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100211 }
212
Ron Eldorbf470992018-06-27 11:51:46 +0300213 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200221 }
222 else
223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_ECP_C)
225 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200228 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->MBEDTLS_PRIVATE(d) , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200232 }
233 else
234#endif
235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300237 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200238 }
Paul Bakkered56b222011-07-13 11:26:43 +0000239 }
240 else if( opt.mode == MODE_PUBLIC )
241 {
242 /*
243 * 1.1. Load the key
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000246 fflush( stdout );
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000249
250 if( ret != 0 )
251 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200252 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300253 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000254 }
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf( " . Key information ...\n" );
259#if defined(MBEDTLS_RSA_C)
260 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100263
264 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
265 NULL, &E ) ) != 0 )
266 {
267 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300268 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100269 }
Ron Eldorbf470992018-06-27 11:51:46 +0300270 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
271 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200272 }
273 else
274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_ECP_C)
276 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200279 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL ) );
280 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL ) );
281 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200282 }
283 else
284#endif
285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300287 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200288 }
Paul Bakkered56b222011-07-13 11:26:43 +0000289 }
290 else
291 goto usage;
292
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100293 exit_code = MBEDTLS_EXIT_SUCCESS;
294
Ron Eldor6a9257b2017-08-24 14:20:17 +0300295cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100298 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100299 {
Ron Eldor7a814262018-06-24 16:34:15 +0300300 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100301 mbedtls_printf( " ! Last error was: %s\n", buf );
302 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200303#endif
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100306 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
307 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
308 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000309
Paul Bakkercce9d772011-11-18 14:26:47 +0000310#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000312 fflush( stdout ); getchar();
313#endif
314
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200315 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */