blob: 8dd22b621b3d61b87679d3732bc0ae367426f9c8 [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é-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_BIGNUM_C) && \
29 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/error.h"
31#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010032#include "mbedtls/pk.h"
Paul Bakkered56b222011-07-13 11:26:43 +000033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <string.h>
35#endif
36
37#define MODE_NONE 0
38#define MODE_PRIVATE 1
39#define MODE_PUBLIC 2
40
41#define DFL_MODE MODE_NONE
42#define DFL_FILENAME "keyfile.key"
43#define DFL_PASSWORD ""
44#define DFL_PASSWORD_FILE ""
45#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000046
Rich Evans18b78c72015-02-11 14:06:19 +000047#define USAGE \
48 "\n usage: key_app param=<>...\n" \
49 "\n acceptable parameters:\n" \
50 " mode=private|public default: none\n" \
51 " filename=%%s default: keyfile.key\n" \
52 " password=%%s default: \"\"\n" \
53 " password_file=%%s default: \"\"\n" \
54 "\n"
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_BIGNUM_C) || \
57 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000058int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
61 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020062 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020063}
64#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000065
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Paul Bakkered56b222011-07-13 11:26:43 +000067/*
68 * global options
69 */
70struct options
71{
72 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *filename; /* filename of the key file */
74 const char *password; /* password for the private key */
75 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000076} opt;
77
Paul Bakkered56b222011-07-13 11:26:43 +000078int main( int argc, char *argv[] )
79{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010080 int ret = 1;
81 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000082 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000083 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000084 char *p, *q;
85
Hanno Becker54ebf992017-08-23 06:45:38 +010086 mbedtls_pk_context pk;
87 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
88
Paul Bakkered56b222011-07-13 11:26:43 +000089 /*
90 * Set to sane values
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020093 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000094
Hanno Becker54ebf992017-08-23 06:45:38 +010095 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
96 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
97 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
98
Paul Bakkered56b222011-07-13 11:26:43 +000099 if( argc == 0 )
100 {
101 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300103 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000104 }
105
106 opt.mode = DFL_MODE;
107 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000108 opt.password = DFL_PASSWORD;
109 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000110
111 for( i = 1; i < argc; i++ )
112 {
Paul Bakkered56b222011-07-13 11:26:43 +0000113 p = argv[i];
114 if( ( q = strchr( p, '=' ) ) == NULL )
115 goto usage;
116 *q++ = '\0';
117
118 if( strcmp( p, "mode" ) == 0 )
119 {
120 if( strcmp( q, "private" ) == 0 )
121 opt.mode = MODE_PRIVATE;
122 else if( strcmp( q, "public" ) == 0 )
123 opt.mode = MODE_PUBLIC;
124 else
125 goto usage;
126 }
127 else if( strcmp( p, "filename" ) == 0 )
128 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000129 else if( strcmp( p, "password" ) == 0 )
130 opt.password = q;
131 else if( strcmp( p, "password_file" ) == 0 )
132 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000133 else
134 goto usage;
135 }
136
137 if( opt.mode == MODE_PRIVATE )
138 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000139 if( strlen( opt.password ) && strlen( opt.password_file ) )
140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000142 goto usage;
143 }
144
145 if( strlen( opt.password_file ) )
146 {
147 FILE *f;
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300153 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000154 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200155 if( fgets( buf, sizeof(buf), f ) == NULL )
156 {
157 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300159 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200160 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000161 fclose( f );
162
Paul Bakker840ab202013-11-30 15:14:38 +0100163 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
165 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
166 opt.password = buf;
167 }
168
Paul Bakkered56b222011-07-13 11:26:43 +0000169 /*
170 * 1.1. Load the key
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000173 fflush( stdout );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000176
177 if( ret != 0 )
178 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200179 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300180 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000181 }
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000184
185 /*
186 * 1.2 Print the key
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( " . Key information ...\n" );
189#if defined(MBEDTLS_RSA_C)
190 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100193
194 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
195 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
196 {
197 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300198 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100199 }
200
Ron Eldorbf470992018-06-27 11:51:46 +0300201 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
202 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
203 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
204 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
205 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
206 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
207 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
208 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200209 }
210 else
211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_ECP_C)
213 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200220 }
221 else
222#endif
223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300225 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200226 }
Paul Bakkered56b222011-07-13 11:26:43 +0000227 }
228 else if( opt.mode == MODE_PUBLIC )
229 {
230 /*
231 * 1.1. Load the key
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000234 fflush( stdout );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000237
238 if( ret != 0 )
239 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200240 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300241 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000242 }
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_printf( " . Key information ...\n" );
247#if defined(MBEDTLS_RSA_C)
248 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100251
252 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
253 NULL, &E ) ) != 0 )
254 {
255 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300256 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100257 }
Ron Eldorbf470992018-06-27 11:51:46 +0300258 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
259 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200260 }
261 else
262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_ECP_C)
264 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300267 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
268 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
269 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200270 }
271 else
272#endif
273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300275 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200276 }
Paul Bakkered56b222011-07-13 11:26:43 +0000277 }
278 else
279 goto usage;
280
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100281 exit_code = MBEDTLS_EXIT_SUCCESS;
282
Ron Eldor6a9257b2017-08-24 14:20:17 +0300283cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100286 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100287 {
Ron Eldor7a814262018-06-24 16:34:15 +0300288 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100289 mbedtls_printf( " ! Last error was: %s\n", buf );
290 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200291#endif
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100294 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
295 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
296 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000297
Paul Bakkercce9d772011-11-18 14:26:47 +0000298#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000300 fflush( stdout ); getchar();
301#endif
302
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200303 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */