blob: 863c9a26aae5a79d5b3a1ada7d0939967a993ad9 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 Garciaf57bccf2018-04-29 20:02:18 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf57bccf2018-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
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if !defined(MBEDTLS_BIGNUM_C) || \
94 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000095int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
98 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020099 mbedtls_exit( 0 );
Paul Bakker15254952013-09-17 11:24:56 +0200100}
101#else
Paul Bakkered56b222011-07-13 11:26:43 +0000102/*
103 * global options
104 */
105struct options
106{
107 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200108 const char *filename; /* filename of the key file */
109 const char *password; /* password for the private key */
110 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000111} opt;
112
Paul Bakkered56b222011-07-13 11:26:43 +0000113int main( int argc, char *argv[] )
114{
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100115 int ret = 1;
116 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000117 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000118 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000119 char *p, *q;
120
Hanno Becker54ebf992017-08-23 06:45:38 +0100121 mbedtls_pk_context pk;
122 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
123
Paul Bakkered56b222011-07-13 11:26:43 +0000124 /*
125 * Set to sane values
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200128 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000129
Hanno Becker54ebf992017-08-23 06:45:38 +0100130 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
131 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
132 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
133
Paul Bakkered56b222011-07-13 11:26:43 +0000134 if( argc == 0 )
135 {
136 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( USAGE );
Ron Eldore1440892017-08-24 14:20:17 +0300138 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000139 }
140
141 opt.mode = DFL_MODE;
142 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000143 opt.password = DFL_PASSWORD;
144 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000145
146 for( i = 1; i < argc; i++ )
147 {
Paul Bakkered56b222011-07-13 11:26:43 +0000148 p = argv[i];
149 if( ( q = strchr( p, '=' ) ) == NULL )
150 goto usage;
151 *q++ = '\0';
152
153 if( strcmp( p, "mode" ) == 0 )
154 {
155 if( strcmp( q, "private" ) == 0 )
156 opt.mode = MODE_PRIVATE;
157 else if( strcmp( q, "public" ) == 0 )
158 opt.mode = MODE_PUBLIC;
159 else
160 goto usage;
161 }
162 else if( strcmp( p, "filename" ) == 0 )
163 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164 else if( strcmp( p, "password" ) == 0 )
165 opt.password = q;
166 else if( strcmp( p, "password_file" ) == 0 )
167 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000168 else
169 goto usage;
170 }
171
172 if( opt.mode == MODE_PRIVATE )
173 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000174 if( strlen( opt.password ) && strlen( opt.password_file ) )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000177 goto usage;
178 }
179
180 if( strlen( opt.password_file ) )
181 {
182 FILE *f;
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000185 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300188 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000189 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200190 if( fgets( buf, sizeof(buf), f ) == NULL )
191 {
192 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300194 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200195 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000196 fclose( f );
197
Paul Bakker840ab202013-11-30 15:14:38 +0100198 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000199 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
200 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
201 opt.password = buf;
202 }
203
Paul Bakkered56b222011-07-13 11:26:43 +0000204 /*
205 * 1.1. Load the key
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000208 fflush( stdout );
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000211
212 if( ret != 0 )
213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300215 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000216 }
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000219
220 /*
221 * 1.2 Print the key
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_printf( " . Key information ...\n" );
224#if defined(MBEDTLS_RSA_C)
225 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100228
229 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
230 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
231 {
232 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300233 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100234 }
235
Ron Eldor5146ef32018-06-27 11:51:46 +0300236 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200244 }
245 else
246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_ECP_C)
248 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300251 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
252 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
253 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
254 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200255 }
256 else
257#endif
258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300260 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200261 }
Paul Bakkered56b222011-07-13 11:26:43 +0000262 }
263 else if( opt.mode == MODE_PUBLIC )
264 {
265 /*
266 * 1.1. Load the key
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000269 fflush( stdout );
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000272
273 if( ret != 0 )
274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldore1440892017-08-24 14:20:17 +0300276 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " . Key information ...\n" );
282#if defined(MBEDTLS_RSA_C)
283 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100286
287 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
288 NULL, &E ) ) != 0 )
289 {
290 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Ron Eldor0d63e622018-06-27 08:49:00 +0300291 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100292 }
Ron Eldor5146ef32018-06-27 11:51:46 +0300293 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
294 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200295 }
296 else
297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_ECP_C)
299 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldore1440892017-08-24 14:20:17 +0300302 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
303 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
304 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200305 }
306 else
307#endif
308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldore1440892017-08-24 14:20:17 +0300310 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200311 }
Paul Bakkered56b222011-07-13 11:26:43 +0000312 }
313 else
314 goto usage;
315
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100316 exit_code = MBEDTLS_EXIT_SUCCESS;
317
Ron Eldore1440892017-08-24 14:20:17 +0300318cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garciaf57bccf2018-04-29 20:02:18 +0100321 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Hanno Becker54ebf992017-08-23 06:45:38 +0100322 {
Ron Eldor45486b12018-06-24 16:34:15 +0300323 mbedtls_strerror( ret, buf, sizeof( buf ) );
Hanno Becker54ebf992017-08-23 06:45:38 +0100324 mbedtls_printf( " ! Last error was: %s\n", buf );
325 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200326#endif
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100329 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
330 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
331 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000332
Paul Bakkercce9d772011-11-18 14:26:47 +0000333#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000335 fflush( stdout ); getchar();
336#endif
337
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200338 mbedtls_exit( exit_code );
Paul Bakkered56b222011-07-13 11:26:43 +0000339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */