blob: 7f39e5ce37eba36eb7d531218e8cb64d5c72736d [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
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <string.h>
44#endif
45
46#define MODE_NONE 0
47#define MODE_PRIVATE 1
48#define MODE_PUBLIC 2
49
50#define DFL_MODE MODE_NONE
51#define DFL_FILENAME "keyfile.key"
52#define DFL_PASSWORD ""
53#define DFL_PASSWORD_FILE ""
54#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000055
Rich Evans18b78c72015-02-11 14:06:19 +000056#define USAGE \
57 "\n usage: key_app param=<>...\n" \
58 "\n acceptable parameters:\n" \
59 " mode=private|public default: none\n" \
60 " filename=%%s default: keyfile.key\n" \
61 " password=%%s default: \"\"\n" \
62 " password_file=%%s default: \"\"\n" \
63 "\n"
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_BIGNUM_C) || \
66 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000067int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
70 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020071 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020072}
73#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000074
Simon Butcher63cb97e2018-12-06 17:43:31 +000075
Paul Bakkered56b222011-07-13 11:26:43 +000076/*
77 * global options
78 */
79struct options
80{
81 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020082 const char *filename; /* filename of the key file */
83 const char *password; /* password for the private key */
84 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000085} opt;
86
Paul Bakkered56b222011-07-13 11:26:43 +000087int main( int argc, char *argv[] )
88{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010089 int ret = 1;
90 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +000091 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000092 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000093 char *p, *q;
94
Hanno Becker54ebf992017-08-23 06:45:38 +010095 mbedtls_pk_context pk;
96 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
97
Paul Bakkered56b222011-07-13 11:26:43 +000098 /*
99 * Set to sane values
100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200102 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000103
Hanno Becker54ebf992017-08-23 06:45:38 +0100104 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
105 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
106 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
107
Paul Bakkered56b222011-07-13 11:26:43 +0000108 if( argc == 0 )
109 {
110 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300112 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000113 }
114
115 opt.mode = DFL_MODE;
116 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000117 opt.password = DFL_PASSWORD;
118 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000119
120 for( i = 1; i < argc; i++ )
121 {
Paul Bakkered56b222011-07-13 11:26:43 +0000122 p = argv[i];
123 if( ( q = strchr( p, '=' ) ) == NULL )
124 goto usage;
125 *q++ = '\0';
126
127 if( strcmp( p, "mode" ) == 0 )
128 {
129 if( strcmp( q, "private" ) == 0 )
130 opt.mode = MODE_PRIVATE;
131 else if( strcmp( q, "public" ) == 0 )
132 opt.mode = MODE_PUBLIC;
133 else
134 goto usage;
135 }
136 else if( strcmp( p, "filename" ) == 0 )
137 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 else if( strcmp( p, "password" ) == 0 )
139 opt.password = q;
140 else if( strcmp( p, "password_file" ) == 0 )
141 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000142 else
143 goto usage;
144 }
145
146 if( opt.mode == MODE_PRIVATE )
147 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000148 if( strlen( opt.password ) && strlen( opt.password_file ) )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000151 goto usage;
152 }
153
154 if( strlen( opt.password_file ) )
155 {
156 FILE *f;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000159 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300162 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000163 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200164 if( fgets( buf, sizeof(buf), f ) == NULL )
165 {
166 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300168 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200169 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000170 fclose( f );
171
Paul Bakker840ab202013-11-30 15:14:38 +0100172 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000173 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
174 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
175 opt.password = buf;
176 }
177
Paul Bakkered56b222011-07-13 11:26:43 +0000178 /*
179 * 1.1. Load the key
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000182 fflush( stdout );
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000185
186 if( ret != 0 )
187 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200188 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300189 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000190 }
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000193
194 /*
195 * 1.2 Print the key
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_printf( " . Key information ...\n" );
198#if defined(MBEDTLS_RSA_C)
199 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100202
203 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
204 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
205 {
206 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300207 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100208 }
209
Ron Eldorbf470992018-06-27 11:51:46 +0300210 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
211 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
212 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200218 }
219 else
220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_ECP_C)
222 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300225 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200229 }
230 else
231#endif
232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300234 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200235 }
Paul Bakkered56b222011-07-13 11:26:43 +0000236 }
237 else if( opt.mode == MODE_PUBLIC )
238 {
239 /*
240 * 1.1. Load the key
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000243 fflush( stdout );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000246
247 if( ret != 0 )
248 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200249 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300250 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000251 }
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " . Key information ...\n" );
256#if defined(MBEDTLS_RSA_C)
257 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100260
261 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
262 NULL, &E ) ) != 0 )
263 {
264 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300265 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100266 }
Ron Eldorbf470992018-06-27 11:51:46 +0300267 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
268 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200269 }
270 else
271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_ECP_C)
273 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300276 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
277 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200279 }
280 else
281#endif
282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300284 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200285 }
Paul Bakkered56b222011-07-13 11:26:43 +0000286 }
287 else
288 goto usage;
289
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100290 exit_code = MBEDTLS_EXIT_SUCCESS;
291
Ron Eldor6a9257b2017-08-24 14:20:17 +0300292cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100295 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100296 {
Ron Eldor7a814262018-06-24 16:34:15 +0300297 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100298 mbedtls_printf( " ! Last error was: %s\n", buf );
299 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200300#endif
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100303 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
304 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
305 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000306
Paul Bakkercce9d772011-11-18 14:26:47 +0000307#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000309 fflush( stdout ); getchar();
310#endif
311
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200312 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */