blob: f89f436ca4d4a12602679877f08cdd2c23788ca5 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/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)
30#include "polarssl/platform.h"
31#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
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) &&\
37 defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO)
Paul Bakkered56b222011-07-13 11:26:43 +000038#include "polarssl/error.h"
39#include "polarssl/rsa.h"
40#include "polarssl/x509.h"
41
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
54
55#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)
67int main( int argc, char *argv[] )
68{
69 ((void) argc);
70 ((void) argv);
71
Rich Evansf90016a2015-01-19 14:26:37 +000072 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Paul Bakker15254952013-09-17 11:24:56 +020073 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
74 return( 0 );
75}
76#else
Paul Bakkered56b222011-07-13 11:26:43 +000077/*
78 * global options
79 */
80struct options
81{
82 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 const char *filename; /* filename of the key file */
84 const char *password; /* password for the private key */
85 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000086} opt;
87
Paul Bakkered56b222011-07-13 11:26:43 +000088int main( int argc, char *argv[] )
89{
90 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020091 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000092 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000093 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000094 char *p, *q;
95
96 /*
97 * Set to sane values
98 */
Paul Bakker15254952013-09-17 11:24:56 +020099 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200100 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000101
102 if( argc == 0 )
103 {
104 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000105 polarssl_printf( USAGE );
Paul Bakkered56b222011-07-13 11:26:43 +0000106 goto exit;
107 }
108
109 opt.mode = DFL_MODE;
110 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000111 opt.password = DFL_PASSWORD;
112 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000113
114 for( i = 1; i < argc; i++ )
115 {
Paul Bakkered56b222011-07-13 11:26:43 +0000116 p = argv[i];
117 if( ( q = strchr( p, '=' ) ) == NULL )
118 goto usage;
119 *q++ = '\0';
120
121 if( strcmp( p, "mode" ) == 0 )
122 {
123 if( strcmp( q, "private" ) == 0 )
124 opt.mode = MODE_PRIVATE;
125 else if( strcmp( q, "public" ) == 0 )
126 opt.mode = MODE_PUBLIC;
127 else
128 goto usage;
129 }
130 else if( strcmp( p, "filename" ) == 0 )
131 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000132 else if( strcmp( p, "password" ) == 0 )
133 opt.password = q;
134 else if( strcmp( p, "password_file" ) == 0 )
135 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000136 else
137 goto usage;
138 }
139
140 if( opt.mode == MODE_PRIVATE )
141 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000142 if( strlen( opt.password ) && strlen( opt.password_file ) )
143 {
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000145 goto usage;
146 }
147
148 if( strlen( opt.password_file ) )
149 {
150 FILE *f;
151
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000153 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
154 {
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_printf( " failed\n ! fopen returned NULL\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000156 goto exit;
157 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200158 if( fgets( buf, sizeof(buf), f ) == NULL )
159 {
160 fclose( f );
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( "Error: fgets() failed to retrieve password\n" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200162 goto exit;
163 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000164 fclose( f );
165
Paul Bakker840ab202013-11-30 15:14:38 +0100166 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000167 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
168 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
169 opt.password = buf;
170 }
171
Paul Bakkered56b222011-07-13 11:26:43 +0000172 /*
173 * 1.1. Load the key
174 */
Rich Evansf90016a2015-01-19 14:26:37 +0000175 polarssl_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000176 fflush( stdout );
177
Paul Bakker15254952013-09-17 11:24:56 +0200178 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000179
180 if( ret != 0 )
181 {
Rich Evansf90016a2015-01-19 14:26:37 +0000182 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000183 goto exit;
184 }
185
Rich Evansf90016a2015-01-19 14:26:37 +0000186 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000187
188 /*
189 * 1.2 Print the key
190 */
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200192#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200193 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200194 {
195 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200196 mpi_write_file( "N: ", &rsa->N, 16, NULL );
197 mpi_write_file( "E: ", &rsa->E, 16, NULL );
198 mpi_write_file( "D: ", &rsa->D, 16, NULL );
199 mpi_write_file( "P: ", &rsa->P, 16, NULL );
200 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
201 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
202 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
203 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
204 }
205 else
206#endif
207#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200208 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200209 {
210 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200211 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
212 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
213 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
214 mpi_write_file( "D : ", &ecp->d , 16, NULL );
215 }
216 else
217#endif
218 {
Rich Evansf90016a2015-01-19 14:26:37 +0000219 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200220 goto exit;
221 }
Paul Bakkered56b222011-07-13 11:26:43 +0000222 }
223 else if( opt.mode == MODE_PUBLIC )
224 {
225 /*
226 * 1.1. Load the key
227 */
Rich Evansf90016a2015-01-19 14:26:37 +0000228 polarssl_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000229 fflush( stdout );
230
Paul Bakker15254952013-09-17 11:24:56 +0200231 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000232
233 if( ret != 0 )
234 {
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000236 goto exit;
237 }
238
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000240
Rich Evansf90016a2015-01-19 14:26:37 +0000241 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200242#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200243 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200244 {
245 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200246 mpi_write_file( "N: ", &rsa->N, 16, NULL );
247 mpi_write_file( "E: ", &rsa->E, 16, NULL );
248 }
249 else
250#endif
251#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200252 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200253 {
254 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200255 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
256 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
257 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
258 }
259 else
260#endif
261 {
Rich Evansf90016a2015-01-19 14:26:37 +0000262 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200263 goto exit;
264 }
Paul Bakkered56b222011-07-13 11:26:43 +0000265 }
266 else
267 goto usage;
268
269exit:
270
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200271#if defined(POLARSSL_ERROR_C)
272 polarssl_strerror( ret, buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000273 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200274#endif
275
Paul Bakker15254952013-09-17 11:24:56 +0200276 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000277
Paul Bakkercce9d772011-11-18 14:26:47 +0000278#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000279 polarssl_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}
Paul Bakker15254952013-09-17 11:24:56 +0200285#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */