blob: c72f17d9f1d6c7c733b967b354e6b03c1c7aa900 [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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_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
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkered56b222011-07-13 11:26:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_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>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000036#if defined(POLARSSL_BIGNUM_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000037 defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_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
Paul Bakker15254952013-09-17 11:24:56 +020065#if !defined(POLARSSL_BIGNUM_C) || \
66 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000067int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020068{
Rich Evansf90016a2015-01-19 14:26:37 +000069 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Paul Bakker15254952013-09-17 11:24:56 +020070 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
71 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;
Paul Bakker15254952013-09-17 11:24:56 +020088 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 */
Paul Bakker15254952013-09-17 11:24:56 +020096 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:
Rich Evansf90016a2015-01-19 14:26:37 +0000102 polarssl_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 {
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_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
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000150 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
151 {
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_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 );
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_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 */
Rich Evansf90016a2015-01-19 14:26:37 +0000172 polarssl_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000173 fflush( stdout );
174
Paul Bakker15254952013-09-17 11:24:56 +0200175 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000176
177 if( ret != 0 )
178 {
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000180 goto exit;
181 }
182
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000184
185 /*
186 * 1.2 Print the key
187 */
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200189#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200190 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200191 {
192 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200193 mpi_write_file( "N: ", &rsa->N, 16, NULL );
194 mpi_write_file( "E: ", &rsa->E, 16, NULL );
195 mpi_write_file( "D: ", &rsa->D, 16, NULL );
196 mpi_write_file( "P: ", &rsa->P, 16, NULL );
197 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
198 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
199 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
200 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
201 }
202 else
203#endif
204#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200205 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200206 {
207 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200208 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
209 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
210 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
211 mpi_write_file( "D : ", &ecp->d , 16, NULL );
212 }
213 else
214#endif
215 {
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_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 */
Rich Evansf90016a2015-01-19 14:26:37 +0000225 polarssl_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000226 fflush( stdout );
227
Paul Bakker15254952013-09-17 11:24:56 +0200228 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000229
230 if( ret != 0 )
231 {
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000233 goto exit;
234 }
235
Rich Evansf90016a2015-01-19 14:26:37 +0000236 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000237
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200239#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200240 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200241 {
242 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200243 mpi_write_file( "N: ", &rsa->N, 16, NULL );
244 mpi_write_file( "E: ", &rsa->E, 16, NULL );
245 }
246 else
247#endif
248#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200249 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200250 {
251 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200252 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
253 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
254 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
255 }
256 else
257#endif
258 {
Rich Evansf90016a2015-01-19 14:26:37 +0000259 polarssl_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é-Gonnard92e5b592013-09-18 18:57:10 +0200268#if defined(POLARSSL_ERROR_C)
269 polarssl_strerror( ret, buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000270 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200271#endif
272
Paul Bakker15254952013-09-17 11:24:56 +0200273 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000274
Paul Bakkercce9d772011-11-18 14:26:47 +0000275#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000276 polarssl_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}
Paul Bakker15254952013-09-17 11:24:56 +0200282#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */