blob: a161829f29873c4ef220784c9b87bc9677573da8 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakkered56b222011-07-13 11:26:43 +000038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41
Paul Bakkered56b222011-07-13 11:26:43 +000042#include "polarssl/error.h"
43#include "polarssl/rsa.h"
44#include "polarssl/x509.h"
45
Paul Bakker15254952013-09-17 11:24:56 +020046#if !defined(POLARSSL_BIGNUM_C) || \
47 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
48int main( int argc, char *argv[] )
49{
50 ((void) argc);
51 ((void) argv);
52
Rich Evansf90016a2015-01-19 14:26:37 +000053 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Paul Bakker15254952013-09-17 11:24:56 +020054 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
55 return( 0 );
56}
57#else
58
Paul Bakkered56b222011-07-13 11:26:43 +000059#define MODE_NONE 0
60#define MODE_PRIVATE 1
61#define MODE_PUBLIC 2
62
63#define DFL_MODE MODE_NONE
64#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000065#define DFL_PASSWORD ""
66#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000067#define DFL_DEBUG_LEVEL 0
68
69/*
70 * global options
71 */
72struct options
73{
74 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 const char *filename; /* filename of the key file */
76 const char *password; /* password for the private key */
77 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000078} opt;
79
Paul Bakkered56b222011-07-13 11:26:43 +000080#define USAGE \
81 "\n usage: key_app param=<>...\n" \
82 "\n acceptable parameters:\n" \
83 " mode=private|public default: none\n" \
84 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000085 " password=%%s default: \"\"\n" \
86 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000087 "\n"
88
Paul Bakkered56b222011-07-13 11:26:43 +000089int main( int argc, char *argv[] )
90{
91 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020092 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000093 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000094 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000095 char *p, *q;
96
97 /*
98 * Set to sane values
99 */
Paul Bakker15254952013-09-17 11:24:56 +0200100 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +0200101 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +0000102
103 if( argc == 0 )
104 {
105 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( USAGE );
Paul Bakkered56b222011-07-13 11:26:43 +0000107 goto exit;
108 }
109
110 opt.mode = DFL_MODE;
111 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000112 opt.password = DFL_PASSWORD;
113 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000114
115 for( i = 1; i < argc; i++ )
116 {
Paul Bakkered56b222011-07-13 11:26:43 +0000117 p = argv[i];
118 if( ( q = strchr( p, '=' ) ) == NULL )
119 goto usage;
120 *q++ = '\0';
121
122 if( strcmp( p, "mode" ) == 0 )
123 {
124 if( strcmp( q, "private" ) == 0 )
125 opt.mode = MODE_PRIVATE;
126 else if( strcmp( q, "public" ) == 0 )
127 opt.mode = MODE_PUBLIC;
128 else
129 goto usage;
130 }
131 else if( strcmp( p, "filename" ) == 0 )
132 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000133 else if( strcmp( p, "password" ) == 0 )
134 opt.password = q;
135 else if( strcmp( p, "password_file" ) == 0 )
136 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000137 else
138 goto usage;
139 }
140
141 if( opt.mode == MODE_PRIVATE )
142 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000143 if( strlen( opt.password ) && strlen( opt.password_file ) )
144 {
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000146 goto usage;
147 }
148
149 if( strlen( opt.password_file ) )
150 {
151 FILE *f;
152
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000154 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
155 {
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " failed\n ! fopen returned NULL\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000157 goto exit;
158 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200159 if( fgets( buf, sizeof(buf), f ) == NULL )
160 {
161 fclose( f );
Rich Evansf90016a2015-01-19 14:26:37 +0000162 polarssl_printf( "Error: fgets() failed to retrieve password\n" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200163 goto exit;
164 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000165 fclose( f );
166
Paul Bakker840ab202013-11-30 15:14:38 +0100167 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000168 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
169 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
170 opt.password = buf;
171 }
172
Paul Bakkered56b222011-07-13 11:26:43 +0000173 /*
174 * 1.1. Load the key
175 */
Rich Evansf90016a2015-01-19 14:26:37 +0000176 polarssl_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000177 fflush( stdout );
178
Paul Bakker15254952013-09-17 11:24:56 +0200179 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000180
181 if( ret != 0 )
182 {
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000184 goto exit;
185 }
186
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000188
189 /*
190 * 1.2 Print the key
191 */
Rich Evansf90016a2015-01-19 14:26:37 +0000192 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200193#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200194 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200195 {
196 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200197 mpi_write_file( "N: ", &rsa->N, 16, NULL );
198 mpi_write_file( "E: ", &rsa->E, 16, NULL );
199 mpi_write_file( "D: ", &rsa->D, 16, NULL );
200 mpi_write_file( "P: ", &rsa->P, 16, NULL );
201 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
202 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
203 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
204 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
205 }
206 else
207#endif
208#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200209 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200210 {
211 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200212 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
213 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
214 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
215 mpi_write_file( "D : ", &ecp->d , 16, NULL );
216 }
217 else
218#endif
219 {
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200221 goto exit;
222 }
Paul Bakkered56b222011-07-13 11:26:43 +0000223 }
224 else if( opt.mode == MODE_PUBLIC )
225 {
226 /*
227 * 1.1. Load the key
228 */
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000230 fflush( stdout );
231
Paul Bakker15254952013-09-17 11:24:56 +0200232 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000233
234 if( ret != 0 )
235 {
Rich Evansf90016a2015-01-19 14:26:37 +0000236 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000237 goto exit;
238 }
239
Rich Evansf90016a2015-01-19 14:26:37 +0000240 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000241
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200243#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200244 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200245 {
246 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200247 mpi_write_file( "N: ", &rsa->N, 16, NULL );
248 mpi_write_file( "E: ", &rsa->E, 16, NULL );
249 }
250 else
251#endif
252#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200253 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200254 {
255 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200256 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
257 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
258 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
259 }
260 else
261#endif
262 {
Rich Evansf90016a2015-01-19 14:26:37 +0000263 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200264 goto exit;
265 }
Paul Bakkered56b222011-07-13 11:26:43 +0000266 }
267 else
268 goto usage;
269
270exit:
271
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200272#if defined(POLARSSL_ERROR_C)
273 polarssl_strerror( ret, buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000274 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200275#endif
276
Paul Bakker15254952013-09-17 11:24:56 +0200277 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000278
Paul Bakkercce9d772011-11-18 14:26:47 +0000279#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000280 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000281 fflush( stdout ); getchar();
282#endif
283
284 return( ret );
285}
Paul Bakker15254952013-09-17 11:24:56 +0200286#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */