blob: c6bd192eca7e030b9cad1c2f6e183ad4fcf6239e [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkered56b222011-07-13 11:26:43 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakkered56b222011-07-13 11:26:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkered56b222011-07-13 11:26:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_BIGNUM_C) && \
65 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/error.h"
67#include "mbedtls/rsa.h"
68#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000069
Rich Evans18b78c72015-02-11 14:06:19 +000070#include <string.h>
71#endif
72
73#define MODE_NONE 0
74#define MODE_PRIVATE 1
75#define MODE_PUBLIC 2
76
77#define DFL_MODE MODE_NONE
78#define DFL_FILENAME "keyfile.key"
79#define DFL_PASSWORD ""
80#define DFL_PASSWORD_FILE ""
81#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000082
Rich Evans18b78c72015-02-11 14:06:19 +000083#define USAGE \
84 "\n usage: key_app param=<>...\n" \
85 "\n acceptable parameters:\n" \
86 " mode=private|public default: none\n" \
87 " filename=%%s default: keyfile.key\n" \
88 " password=%%s default: \"\"\n" \
89 " password_file=%%s default: \"\"\n" \
90 "\n"
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if !defined(MBEDTLS_BIGNUM_C) || \
93 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000094int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
97 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020098 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +020099}
100#else
Simon Butcher63cb97e2018-12-06 17:43:31 +0000101
Simon Butcher63cb97e2018-12-06 17:43:31 +0000102
Paul Bakkered56b222011-07-13 11:26:43 +0000103/*
104 * global options
105 */
106struct options
107{
108 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200109 const char *filename; /* filename of the key file */
110 const char *password; /* password for the private key */
111 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000112} opt;
113
Paul Bakkered56b222011-07-13 11:26:43 +0000114int main( int argc, char *argv[] )
115{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100116 int ret = 1;
117 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000118 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000119 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000120 char *p, *q;
121
Hanno Becker54ebf992017-08-23 06:45:38 +0100122 mbedtls_pk_context pk;
123 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
124
Paul Bakkered56b222011-07-13 11:26:43 +0000125 /*
126 * Set to sane values
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200129 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000130
Hanno Becker54ebf992017-08-23 06:45:38 +0100131 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
132 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
133 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
134
Paul Bakkered56b222011-07-13 11:26:43 +0000135 if( argc == 0 )
136 {
137 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300139 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000140 }
141
142 opt.mode = DFL_MODE;
143 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000144 opt.password = DFL_PASSWORD;
145 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000146
147 for( i = 1; i < argc; i++ )
148 {
Paul Bakkered56b222011-07-13 11:26:43 +0000149 p = argv[i];
150 if( ( q = strchr( p, '=' ) ) == NULL )
151 goto usage;
152 *q++ = '\0';
153
154 if( strcmp( p, "mode" ) == 0 )
155 {
156 if( strcmp( q, "private" ) == 0 )
157 opt.mode = MODE_PRIVATE;
158 else if( strcmp( q, "public" ) == 0 )
159 opt.mode = MODE_PUBLIC;
160 else
161 goto usage;
162 }
163 else if( strcmp( p, "filename" ) == 0 )
164 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000165 else if( strcmp( p, "password" ) == 0 )
166 opt.password = q;
167 else if( strcmp( p, "password_file" ) == 0 )
168 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000169 else
170 goto usage;
171 }
172
173 if( opt.mode == MODE_PRIVATE )
174 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000175 if( strlen( opt.password ) && strlen( opt.password_file ) )
176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000178 goto usage;
179 }
180
181 if( strlen( opt.password_file ) )
182 {
183 FILE *f;
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000186 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300189 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000190 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200191 if( fgets( buf, sizeof(buf), f ) == NULL )
192 {
193 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300195 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200196 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000197 fclose( f );
198
Paul Bakker840ab202013-11-30 15:14:38 +0100199 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000200 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
201 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
202 opt.password = buf;
203 }
204
Paul Bakkered56b222011-07-13 11:26:43 +0000205 /*
206 * 1.1. Load the key
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000209 fflush( stdout );
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000212
213 if( ret != 0 )
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300216 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000217 }
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000220
221 /*
222 * 1.2 Print the key
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " . Key information ...\n" );
225#if defined(MBEDTLS_RSA_C)
226 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100229
230 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
231 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
232 {
233 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300234 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100235 }
236
Ron Eldorbf470992018-06-27 11:51:46 +0300237 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200245 }
246 else
247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_ECP_C)
249 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300252 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
253 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
254 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
255 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200256 }
257 else
258#endif
259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300261 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200262 }
Paul Bakkered56b222011-07-13 11:26:43 +0000263 }
264 else if( opt.mode == MODE_PUBLIC )
265 {
266 /*
267 * 1.1. Load the key
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000270 fflush( stdout );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000273
274 if( ret != 0 )
275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300277 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000278 }
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( " . Key information ...\n" );
283#if defined(MBEDTLS_RSA_C)
284 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100287
288 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
289 NULL, &E ) ) != 0 )
290 {
291 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldora5221472018-06-27 08:49:00 +0300292 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100293 }
Ron Eldorbf470992018-06-27 11:51:46 +0300294 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
295 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200296 }
297 else
298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_ECP_C)
300 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300303 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
304 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200306 }
307 else
308#endif
309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300311 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200312 }
Paul Bakkered56b222011-07-13 11:26:43 +0000313 }
314 else
315 goto usage;
316
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100317 exit_code = MBEDTLS_EXIT_SUCCESS;
318
Ron Eldor6a9257b2017-08-24 14:20:17 +0300319cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100322 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100323 {
Ron Eldor7a814262018-06-24 16:34:15 +0300324 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100325 mbedtls_printf( " ! Last error was: %s\n", buf );
326 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200327#endif
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100330 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
331 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
332 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000333
Paul Bakkercce9d772011-11-18 14:26:47 +0000334#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000336 fflush( stdout ); getchar();
337#endif
338
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200339 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */