blob: b93ea8f7574c24bf24c70d726deaf47af37ae30d [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered56b222011-07-13 11:26:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkered56b222011-07-13 11:26:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_BIGNUM_C) && \
36 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/error.h"
38#include "mbedtls/rsa.h"
39#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <string.h>
42#endif
43
44#define MODE_NONE 0
45#define MODE_PRIVATE 1
46#define MODE_PUBLIC 2
47
48#define DFL_MODE MODE_NONE
49#define DFL_FILENAME "keyfile.key"
50#define DFL_PASSWORD ""
51#define DFL_PASSWORD_FILE ""
52#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000053
Rich Evans18b78c72015-02-11 14:06:19 +000054#define USAGE \
55 "\n usage: key_app param=<>...\n" \
56 "\n acceptable parameters:\n" \
57 " mode=private|public default: none\n" \
58 " filename=%%s default: keyfile.key\n" \
59 " password=%%s default: \"\"\n" \
60 " password_file=%%s default: \"\"\n" \
61 "\n"
62
63
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)
Rich Evans85b05ec2015-02-12 11:37:29 +000066int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
69 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker15254952013-09-17 11:24:56 +020070 return( 0 );
71}
72#else
Paul Bakkered56b222011-07-13 11:26:43 +000073/*
74 * global options
75 */
76struct options
77{
78 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *filename; /* filename of the key file */
80 const char *password; /* password for the private key */
81 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000082} opt;
83
Paul Bakkered56b222011-07-13 11:26:43 +000084int main( int argc, char *argv[] )
85{
86 int ret = 0;
Paul Bakkered56b222011-07-13 11:26:43 +000087 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000088 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000089 char *p, *q;
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020098 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000099
Hanno Becker54ebf992017-08-23 06:45:38 +0100100 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
101 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
102 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
103
Paul Bakkered56b222011-07-13 11:26:43 +0000104 if( argc == 0 )
105 {
106 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( USAGE );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300108 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000109 }
110
111 opt.mode = DFL_MODE;
112 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000113 opt.password = DFL_PASSWORD;
114 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000115
116 for( i = 1; i < argc; i++ )
117 {
Paul Bakkered56b222011-07-13 11:26:43 +0000118 p = argv[i];
119 if( ( q = strchr( p, '=' ) ) == NULL )
120 goto usage;
121 *q++ = '\0';
122
123 if( strcmp( p, "mode" ) == 0 )
124 {
125 if( strcmp( q, "private" ) == 0 )
126 opt.mode = MODE_PRIVATE;
127 else if( strcmp( q, "public" ) == 0 )
128 opt.mode = MODE_PUBLIC;
129 else
130 goto usage;
131 }
132 else if( strcmp( p, "filename" ) == 0 )
133 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000134 else if( strcmp( p, "password" ) == 0 )
135 opt.password = q;
136 else if( strcmp( p, "password_file" ) == 0 )
137 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000138 else
139 goto usage;
140 }
141
142 if( opt.mode == MODE_PRIVATE )
143 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000144 if( strlen( opt.password ) && strlen( opt.password_file ) )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000147 goto usage;
148 }
149
150 if( strlen( opt.password_file ) )
151 {
152 FILE *f;
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000155 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300158 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000159 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200160 if( fgets( buf, sizeof(buf), f ) == NULL )
161 {
162 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300164 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200165 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000166 fclose( f );
167
Paul Bakker840ab202013-11-30 15:14:38 +0100168 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000169 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
170 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
171 opt.password = buf;
172 }
173
Paul Bakkered56b222011-07-13 11:26:43 +0000174 /*
175 * 1.1. Load the key
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000178 fflush( stdout );
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000181
182 if( ret != 0 )
183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300185 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000186 }
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000189
190 /*
191 * 1.2 Print the key
192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_printf( " . Key information ...\n" );
194#if defined(MBEDTLS_RSA_C)
195 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100198
199 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
200 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
201 {
202 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
203 goto exit;
204 }
205
Ron Eldor6a9257b2017-08-24 14:20:17 +0300206 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) );
207 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) );
208 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL ) );
209 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL ) );
210 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL ) );
211 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL ) );
212 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200214 }
215 else
216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_ECP_C)
218 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300221 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200225 }
226 else
227#endif
228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300230 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200231 }
Paul Bakkered56b222011-07-13 11:26:43 +0000232 }
233 else if( opt.mode == MODE_PUBLIC )
234 {
235 /*
236 * 1.1. Load the key
237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000239 fflush( stdout );
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000242
243 if( ret != 0 )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300246 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000247 }
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " . Key information ...\n" );
252#if defined(MBEDTLS_RSA_C)
253 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100256
257 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
258 NULL, &E ) ) != 0 )
259 {
260 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
261 goto exit;
262 }
Ron Eldor6a9257b2017-08-24 14:20:17 +0300263 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) );
264 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200265 }
266 else
267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_ECP_C)
269 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300272 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
273 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
274 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200275 }
276 else
277#endif
278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor6a9257b2017-08-24 14:20:17 +0300280 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200281 }
Paul Bakkered56b222011-07-13 11:26:43 +0000282 }
283 else
284 goto usage;
285
Ron Eldor6a9257b2017-08-24 14:20:17 +0300286cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_ERROR_C)
Hanno Becker54ebf992017-08-23 06:45:38 +0100289 if( ret != 0 )
290 {
291 mbedtls_strerror( ret, buf, sizeof(buf) );
292 mbedtls_printf( " ! Last error was: %s\n", buf );
293 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200294#endif
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_pk_free( &pk );
Hanno Becker54ebf992017-08-23 06:45:38 +0100297 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
298 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
299 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Paul Bakkered56b222011-07-13 11:26:43 +0000300
Paul Bakkercce9d772011-11-18 14:26:47 +0000301#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000303 fflush( stdout ); getchar();
304#endif
305
306 return( ret );
307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */