blob: 02a19e95a43a343f03d9877b6076cae7a0442029 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkered56b222011-07-13 11:26:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_BIGNUM_C) && \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020025 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
26 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/error.h"
28#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020030#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020032
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <string.h>
34#endif
35
36#define MODE_NONE 0
37#define MODE_PRIVATE 1
38#define MODE_PUBLIC 2
39
40#define DFL_MODE MODE_NONE
41#define DFL_FILENAME "keyfile.key"
42#define DFL_PASSWORD ""
43#define DFL_PASSWORD_FILE ""
44#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000045
Rich Evans18b78c72015-02-11 14:06:19 +000046#define USAGE \
47 "\n usage: key_app param=<>...\n" \
48 "\n acceptable parameters:\n" \
49 " mode=private|public default: none\n" \
50 " filename=%%s default: keyfile.key\n" \
51 " password=%%s default: \"\"\n" \
52 " password_file=%%s default: \"\"\n" \
53 "\n"
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020056 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
57 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
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 "
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020061 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
62 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020063 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020064}
65#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Simon Butcher63cb97e2018-12-06 17:43:31 +000067
Paul Bakkered56b222011-07-13 11:26:43 +000068/*
69 * global options
70 */
71struct options
72{
73 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020074 const char *filename; /* filename of the key file */
75 const char *password; /* password for the private key */
76 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000077} opt;
78
Paul Bakkered56b222011-07-13 11:26:43 +000079int main( int argc, char *argv[] )
80{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010081 int ret = 1;
82 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000083 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000084 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000085 char *p, *q;
86
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020087 const char *pers = "pkey/key_app";
88 mbedtls_entropy_context entropy;
89 mbedtls_ctr_drbg_context ctr_drbg;
90
Hanno Becker54ebf992017-08-23 06:45:38 +010091 mbedtls_pk_context pk;
92 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
93
Paul Bakkered56b222011-07-13 11:26:43 +000094 /*
95 * Set to sane values
96 */
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020097 mbedtls_entropy_init( &entropy );
98 mbedtls_ctr_drbg_init( &ctr_drbg );
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200101 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000102
Hanno Becker54ebf992017-08-23 06:45:38 +0100103 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
104 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
105 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
106
Paul Bakkered56b222011-07-13 11:26:43 +0000107 if( argc == 0 )
108 {
109 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300111 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000112 }
113
114 opt.mode = DFL_MODE;
115 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000116 opt.password = DFL_PASSWORD;
117 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000118
119 for( i = 1; i < argc; i++ )
120 {
Paul Bakkered56b222011-07-13 11:26:43 +0000121 p = argv[i];
122 if( ( q = strchr( p, '=' ) ) == NULL )
123 goto usage;
124 *q++ = '\0';
125
126 if( strcmp( p, "mode" ) == 0 )
127 {
128 if( strcmp( q, "private" ) == 0 )
129 opt.mode = MODE_PRIVATE;
130 else if( strcmp( q, "public" ) == 0 )
131 opt.mode = MODE_PUBLIC;
132 else
133 goto usage;
134 }
135 else if( strcmp( p, "filename" ) == 0 )
136 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000137 else if( strcmp( p, "password" ) == 0 )
138 opt.password = q;
139 else if( strcmp( p, "password_file" ) == 0 )
140 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000141 else
142 goto usage;
143 }
144
145 if( opt.mode == MODE_PRIVATE )
146 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000147 if( strlen( opt.password ) && strlen( opt.password_file ) )
148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 goto usage;
151 }
152
153 if( strlen( opt.password_file ) )
154 {
155 FILE *f;
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000158 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300161 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000162 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200163 if( fgets( buf, sizeof(buf), f ) == NULL )
164 {
165 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300167 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200168 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000169 fclose( f );
170
Paul Bakker840ab202013-11-30 15:14:38 +0100171 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000172 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
173 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
174 opt.password = buf;
175 }
176
Paul Bakkered56b222011-07-13 11:26:43 +0000177 /*
178 * 1.1. Load the key
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000181 fflush( stdout );
182
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200183 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
184 (const unsigned char *) pers,
185 strlen( pers ) ) ) != 0 )
186 {
187 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
188 goto cleanup;
189 }
190
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200191 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password,
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200192 mbedtls_ctr_drbg_random, &ctr_drbg );
Paul Bakkered56b222011-07-13 11:26:43 +0000193
194 if( ret != 0 )
195 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200196 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300197 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000198 }
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000201
202 /*
203 * 1.2 Print the key
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " . Key information ...\n" );
206#if defined(MBEDTLS_RSA_C)
207 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100210
211 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
212 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
213 {
214 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300215 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100216 }
217
Ron Eldorbf470992018-06-27 11:51:46 +0300218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200226 }
227 else
228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_ECP_C)
230 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200233 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL ) );
236 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->MBEDTLS_PRIVATE(d) , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200237 }
238 else
239#endif
240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300242 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200243 }
Paul Bakkered56b222011-07-13 11:26:43 +0000244 }
245 else if( opt.mode == MODE_PUBLIC )
246 {
247 /*
248 * 1.1. Load the key
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000251 fflush( stdout );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000254
255 if( ret != 0 )
256 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200257 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300258 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000259 }
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_printf( " . Key information ...\n" );
264#if defined(MBEDTLS_RSA_C)
265 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100268
269 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
270 NULL, &E ) ) != 0 )
271 {
272 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300273 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100274 }
Ron Eldorbf470992018-06-27 11:51:46 +0300275 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
276 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200277 }
278 else
279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_ECP_C)
281 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200284 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL ) );
285 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL ) );
286 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 +0200287 }
288 else
289#endif
290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300292 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200293 }
Paul Bakkered56b222011-07-13 11:26:43 +0000294 }
295 else
296 goto usage;
297
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100298 exit_code = MBEDTLS_EXIT_SUCCESS;
299
Ron Eldor6a9257b2017-08-24 14:20:17 +0300300cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100303 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100304 {
Ron Eldor7a814262018-06-24 16:34:15 +0300305 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100306 mbedtls_printf( " ! Last error was: %s\n", buf );
307 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200308#endif
309
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200310 mbedtls_ctr_drbg_free( &ctr_drbg );
311 mbedtls_entropy_free( &entropy );
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100314 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
315 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
316 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000317
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200318 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000319}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200320#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
321 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */