blob: 3637d6d5b13127b2a91cae8ac399b7580b4dc66f [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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakkered56b222011-07-13 11:26:43 +000033#include "polarssl/error.h"
34#include "polarssl/rsa.h"
35#include "polarssl/x509.h"
36
Paul Bakker15254952013-09-17 11:24:56 +020037#if !defined(POLARSSL_BIGNUM_C) || \
38 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
39int main( int argc, char *argv[] )
40{
41 ((void) argc);
42 ((void) argv);
43
44 printf("POLARSSL_BIGNUM_C and/or "
45 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
46 return( 0 );
47}
48#else
49
Paul Bakkered56b222011-07-13 11:26:43 +000050#define MODE_NONE 0
51#define MODE_PRIVATE 1
52#define MODE_PUBLIC 2
53
54#define DFL_MODE MODE_NONE
55#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000056#define DFL_PASSWORD ""
57#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000058#define DFL_DEBUG_LEVEL 0
59
60/*
61 * global options
62 */
63struct options
64{
65 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020066 const char *filename; /* filename of the key file */
67 const char *password; /* password for the private key */
68 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000069} opt;
70
Paul Bakkered56b222011-07-13 11:26:43 +000071#define USAGE \
72 "\n usage: key_app param=<>...\n" \
73 "\n acceptable parameters:\n" \
74 " mode=private|public default: none\n" \
75 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000076 " password=%%s default: \"\"\n" \
77 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000078 "\n"
79
Paul Bakkered56b222011-07-13 11:26:43 +000080int main( int argc, char *argv[] )
81{
82 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020083 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000084 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000085 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000086 char *p, *q;
87
88 /*
89 * Set to sane values
90 */
Paul Bakker15254952013-09-17 11:24:56 +020091 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020092 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000093
94 if( argc == 0 )
95 {
96 usage:
97 printf( USAGE );
98 goto exit;
99 }
100
101 opt.mode = DFL_MODE;
102 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000103 opt.password = DFL_PASSWORD;
104 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000105
106 for( i = 1; i < argc; i++ )
107 {
Paul Bakkered56b222011-07-13 11:26:43 +0000108 p = argv[i];
109 if( ( q = strchr( p, '=' ) ) == NULL )
110 goto usage;
111 *q++ = '\0';
112
113 if( strcmp( p, "mode" ) == 0 )
114 {
115 if( strcmp( q, "private" ) == 0 )
116 opt.mode = MODE_PRIVATE;
117 else if( strcmp( q, "public" ) == 0 )
118 opt.mode = MODE_PUBLIC;
119 else
120 goto usage;
121 }
122 else if( strcmp( p, "filename" ) == 0 )
123 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000124 else if( strcmp( p, "password" ) == 0 )
125 opt.password = q;
126 else if( strcmp( p, "password_file" ) == 0 )
127 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000128 else
129 goto usage;
130 }
131
132 if( opt.mode == MODE_PRIVATE )
133 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000134 if( strlen( opt.password ) && strlen( opt.password_file ) )
135 {
136 printf( "Error: cannot have both password and password_file\n" );
137 goto usage;
138 }
139
140 if( strlen( opt.password_file ) )
141 {
142 FILE *f;
143
144 printf( "\n . Loading the password file ..." );
145 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
146 {
147 printf( " failed\n ! fopen returned NULL\n" );
148 goto exit;
149 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200150 if( fgets( buf, sizeof(buf), f ) == NULL )
151 {
152 fclose( f );
153 printf( "Error: fgets() failed to retrieve password\n" );
154 goto exit;
155 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000156 fclose( f );
157
Paul Bakker840ab202013-11-30 15:14:38 +0100158 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000159 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
160 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
161 opt.password = buf;
162 }
163
Paul Bakkered56b222011-07-13 11:26:43 +0000164 /*
165 * 1.1. Load the key
166 */
167 printf( "\n . Loading the private key ..." );
168 fflush( stdout );
169
Paul Bakker15254952013-09-17 11:24:56 +0200170 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000171
172 if( ret != 0 )
173 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200174 printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000175 goto exit;
176 }
177
178 printf( " ok\n" );
179
180 /*
181 * 1.2 Print the key
182 */
Paul Bakker2e24ca72013-09-18 15:21:27 +0200183 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200184#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200185 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200186 {
187 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200188 mpi_write_file( "N: ", &rsa->N, 16, NULL );
189 mpi_write_file( "E: ", &rsa->E, 16, NULL );
190 mpi_write_file( "D: ", &rsa->D, 16, NULL );
191 mpi_write_file( "P: ", &rsa->P, 16, NULL );
192 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
193 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
194 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
195 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
196 }
197 else
198#endif
199#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200200 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200201 {
202 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200203 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
204 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
205 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
206 mpi_write_file( "D : ", &ecp->d , 16, NULL );
207 }
208 else
209#endif
210 {
211 printf("Do not know how to print key information for this type\n" );
212 goto exit;
213 }
Paul Bakkered56b222011-07-13 11:26:43 +0000214 }
215 else if( opt.mode == MODE_PUBLIC )
216 {
217 /*
218 * 1.1. Load the key
219 */
220 printf( "\n . Loading the public key ..." );
221 fflush( stdout );
222
Paul Bakker15254952013-09-17 11:24:56 +0200223 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000224
225 if( ret != 0 )
226 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200227 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000228 goto exit;
229 }
230
231 printf( " ok\n" );
232
Paul Bakker2e24ca72013-09-18 15:21:27 +0200233 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200234#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200235 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200236 {
237 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200238 mpi_write_file( "N: ", &rsa->N, 16, NULL );
239 mpi_write_file( "E: ", &rsa->E, 16, NULL );
240 }
241 else
242#endif
243#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200244 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200245 {
246 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200247 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
248 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
249 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
250 }
251 else
252#endif
253 {
254 printf("Do not know how to print key information for this type\n" );
255 goto exit;
256 }
Paul Bakkered56b222011-07-13 11:26:43 +0000257 }
258 else
259 goto usage;
260
261exit:
262
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200263#if defined(POLARSSL_ERROR_C)
264 polarssl_strerror( ret, buf, sizeof(buf) );
265 printf( " ! Last error was: %s\n", buf );
266#endif
267
Paul Bakker15254952013-09-17 11:24:56 +0200268 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000269
Paul Bakkercce9d772011-11-18 14:26:47 +0000270#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000271 printf( " + Press Enter to exit this program.\n" );
272 fflush( stdout ); getchar();
273#endif
274
275 return( ret );
276}
Paul Bakker15254952013-09-17 11:24:56 +0200277#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */