blob: 96c6b70e881cceaa93ad62534cce9a5244fc9df6 [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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000088 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000089 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000090 char *p, *q;
91
92 /*
93 * Set to sane values
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020096 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000097
98 if( argc == 0 )
99 {
100 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( USAGE );
Ron Eldor45d23d62017-08-24 14:20:17 +0300102 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000103 }
104
105 opt.mode = DFL_MODE;
106 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000107 opt.password = DFL_PASSWORD;
108 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000109
110 for( i = 1; i < argc; i++ )
111 {
Paul Bakkered56b222011-07-13 11:26:43 +0000112 p = argv[i];
113 if( ( q = strchr( p, '=' ) ) == NULL )
114 goto usage;
115 *q++ = '\0';
116
117 if( strcmp( p, "mode" ) == 0 )
118 {
119 if( strcmp( q, "private" ) == 0 )
120 opt.mode = MODE_PRIVATE;
121 else if( strcmp( q, "public" ) == 0 )
122 opt.mode = MODE_PUBLIC;
123 else
124 goto usage;
125 }
126 else if( strcmp( p, "filename" ) == 0 )
127 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000128 else if( strcmp( p, "password" ) == 0 )
129 opt.password = q;
130 else if( strcmp( p, "password_file" ) == 0 )
131 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000132 else
133 goto usage;
134 }
135
136 if( opt.mode == MODE_PRIVATE )
137 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 if( strlen( opt.password ) && strlen( opt.password_file ) )
139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000141 goto usage;
142 }
143
144 if( strlen( opt.password_file ) )
145 {
146 FILE *f;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000149 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Ron Eldor45d23d62017-08-24 14:20:17 +0300152 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000153 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200154 if( fgets( buf, sizeof(buf), f ) == NULL )
155 {
156 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Ron Eldor45d23d62017-08-24 14:20:17 +0300158 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200159 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160 fclose( f );
161
Paul Bakker840ab202013-11-30 15:14:38 +0100162 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000163 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
164 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
165 opt.password = buf;
166 }
167
Paul Bakkered56b222011-07-13 11:26:43 +0000168 /*
169 * 1.1. Load the key
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000172 fflush( stdout );
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000175
176 if( ret != 0 )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Ron Eldor45d23d62017-08-24 14:20:17 +0300179 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000180 }
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000183
184 /*
185 * 1.2 Print the key
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " . Key information ...\n" );
188#if defined(MBEDTLS_RSA_C)
189 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Ron Eldor45d23d62017-08-24 14:20:17 +0300192
193 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) );
194 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) );
195 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL ) );
196 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL ) );
197 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL ) );
198 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL ) );
199 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ) );
200 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200201 }
202 else
203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_ECP_C)
205 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
Ron Eldor45d23d62017-08-24 14:20:17 +0300208 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
209 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
210 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
211 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
Paul Bakker15254952013-09-17 11:24:56 +0200212 }
213 else
214#endif
215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf("Do not know how to print key information for this type\n" );
Ron Eldor45d23d62017-08-24 14:20:17 +0300217 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200218 }
Paul Bakkered56b222011-07-13 11:26:43 +0000219 }
220 else if( opt.mode == MODE_PUBLIC )
221 {
222 /*
223 * 1.1. Load the key
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000226 fflush( stdout );
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000229
230 if( ret != 0 )
231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Ron Eldor45d23d62017-08-24 14:20:17 +0300233 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000234 }
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " . Key information ...\n" );
239#if defined(MBEDTLS_RSA_C)
240 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
Ron Eldor45d23d62017-08-24 14:20:17 +0300243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 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 Eldor45d23d62017-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 ) );
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 Eldor45d23d62017-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
264 goto usage;
265
Ron Eldor45d23d62017-08-24 14:20:17 +0300266cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_ERROR_C)
Ron Eldorc24108a2018-06-24 16:34:15 +0300269 if( ret != 0 )
270 {
271 mbedtls_strerror( ret, buf, sizeof( buf ) );
272 mbedtls_printf( " ! Last error was: %s\n", buf );
273 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200274#endif
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000277
Paul Bakkercce9d772011-11-18 14:26:47 +0000278#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000280 fflush( stdout ); getchar();
281#endif
282
283 return( ret );
284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */