blob: f43112aa226d3698ecb9ff486046acc6b6abd235 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkered56b222011-07-13 11:26:43 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered56b222011-07-13 11:26:43 +00007 *
Paul Bakkered56b222011-07-13 11:26:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakkered56b222011-07-13 11:26:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_BIGNUM_C) && \
37 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/error.h"
39#include "mbedtls/rsa.h"
40#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000041
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <string.h>
43#endif
44
45#define MODE_NONE 0
46#define MODE_PRIVATE 1
47#define MODE_PUBLIC 2
48
49#define DFL_MODE MODE_NONE
50#define DFL_FILENAME "keyfile.key"
51#define DFL_PASSWORD ""
52#define DFL_PASSWORD_FILE ""
53#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000054
Rich Evans18b78c72015-02-11 14:06:19 +000055#define USAGE \
56 "\n usage: key_app param=<>...\n" \
57 "\n acceptable parameters:\n" \
58 " mode=private|public default: none\n" \
59 " filename=%%s default: keyfile.key\n" \
60 " password=%%s default: \"\"\n" \
61 " password_file=%%s default: \"\"\n" \
62 "\n"
63
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");
Paul Bakker15254952013-09-17 11:24:56 +020071 return( 0 );
72}
73#else
Paul Bakkered56b222011-07-13 11:26:43 +000074/*
75 * global options
76 */
77struct options
78{
79 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020080 const char *filename; /* filename of the key file */
81 const char *password; /* password for the private key */
82 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000083} opt;
84
Paul Bakkered56b222011-07-13 11:26:43 +000085int main( int argc, char *argv[] )
86{
87 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000089 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000090 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000091 char *p, *q;
92
93 /*
94 * Set to sane values
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020097 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000098
99 if( argc == 0 )
100 {
101 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( USAGE );
Paul Bakkered56b222011-07-13 11:26:43 +0000103 goto exit;
104 }
105
106 opt.mode = DFL_MODE;
107 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000108 opt.password = DFL_PASSWORD;
109 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000110
111 for( i = 1; i < argc; i++ )
112 {
Paul Bakkered56b222011-07-13 11:26:43 +0000113 p = argv[i];
114 if( ( q = strchr( p, '=' ) ) == NULL )
115 goto usage;
116 *q++ = '\0';
117
118 if( strcmp( p, "mode" ) == 0 )
119 {
120 if( strcmp( q, "private" ) == 0 )
121 opt.mode = MODE_PRIVATE;
122 else if( strcmp( q, "public" ) == 0 )
123 opt.mode = MODE_PUBLIC;
124 else
125 goto usage;
126 }
127 else if( strcmp( p, "filename" ) == 0 )
128 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000129 else if( strcmp( p, "password" ) == 0 )
130 opt.password = q;
131 else if( strcmp( p, "password_file" ) == 0 )
132 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000133 else
134 goto usage;
135 }
136
137 if( opt.mode == MODE_PRIVATE )
138 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000139 if( strlen( opt.password ) && strlen( opt.password_file ) )
140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000142 goto usage;
143 }
144
145 if( strlen( opt.password_file ) )
146 {
147 FILE *f;
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000153 goto exit;
154 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200155 if( fgets( buf, sizeof(buf), f ) == NULL )
156 {
157 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200159 goto exit;
160 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000161 fclose( f );
162
Paul Bakker840ab202013-11-30 15:14:38 +0100163 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
165 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
166 opt.password = buf;
167 }
168
Paul Bakkered56b222011-07-13 11:26:43 +0000169 /*
170 * 1.1. Load the key
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000173 fflush( stdout );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000176
177 if( ret != 0 )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000180 goto exit;
181 }
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000184
185 /*
186 * 1.2 Print the key
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( " . Key information ...\n" );
189#if defined(MBEDTLS_RSA_C)
190 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
193 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
194 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
195 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
196 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
197 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
198 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
199 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
200 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 );
208 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
209 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
210 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
211 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" );
Paul Bakker15254952013-09-17 11:24:56 +0200217 goto exit;
218 }
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 );
Paul Bakkered56b222011-07-13 11:26:43 +0000233 goto exit;
234 }
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 );
243 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
244 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 );
252 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
253 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
254 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" );
Paul Bakker15254952013-09-17 11:24:56 +0200260 goto exit;
261 }
Paul Bakkered56b222011-07-13 11:26:43 +0000262 }
263 else
264 goto usage;
265
266exit:
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_ERROR_C)
269 mbedtls_strerror( ret, buf, sizeof(buf) );
270 mbedtls_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200271#endif
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000274
Paul Bakkercce9d772011-11-18 14:26:47 +0000275#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000277 fflush( stdout ); getchar();
278#endif
279
280 return( ret );
281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */