blob: b355069f787dead9cc15d74b5a01cb9f3758d3bb [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkered56b222011-07-13 11:26:43 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkered56b222011-07-13 11:26:43 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakkered56b222011-07-13 11:26:43 +000032#include "polarssl/error.h"
33#include "polarssl/rsa.h"
34#include "polarssl/x509.h"
35
Paul Bakker15254952013-09-17 11:24:56 +020036#if !defined(POLARSSL_BIGNUM_C) || \
37 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
38int main( int argc, char *argv[] )
39{
40 ((void) argc);
41 ((void) argv);
42
43 printf("POLARSSL_BIGNUM_C and/or "
44 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
45 return( 0 );
46}
47#else
48
Paul Bakkered56b222011-07-13 11:26:43 +000049#define MODE_NONE 0
50#define MODE_PRIVATE 1
51#define MODE_PUBLIC 2
52
53#define DFL_MODE MODE_NONE
54#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000055#define DFL_PASSWORD ""
56#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000057#define DFL_DEBUG_LEVEL 0
58
59/*
60 * global options
61 */
62struct options
63{
64 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020065 const char *filename; /* filename of the key file */
66 const char *password; /* password for the private key */
67 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000068} opt;
69
Paul Bakkered56b222011-07-13 11:26:43 +000070#define USAGE \
71 "\n usage: key_app param=<>...\n" \
72 "\n acceptable parameters:\n" \
73 " mode=private|public default: none\n" \
74 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000075 " password=%%s default: \"\"\n" \
76 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000077 "\n"
78
Paul Bakkered56b222011-07-13 11:26:43 +000079int main( int argc, char *argv[] )
80{
81 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020082 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000083 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000084 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000085 char *p, *q;
86
87 /*
88 * Set to sane values
89 */
Paul Bakker15254952013-09-17 11:24:56 +020090 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020091 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000092
93 if( argc == 0 )
94 {
95 usage:
96 printf( USAGE );
97 goto exit;
98 }
99
100 opt.mode = DFL_MODE;
101 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000102 opt.password = DFL_PASSWORD;
103 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000104
105 for( i = 1; i < argc; i++ )
106 {
Paul Bakkered56b222011-07-13 11:26:43 +0000107 p = argv[i];
108 if( ( q = strchr( p, '=' ) ) == NULL )
109 goto usage;
110 *q++ = '\0';
111
112 if( strcmp( p, "mode" ) == 0 )
113 {
114 if( strcmp( q, "private" ) == 0 )
115 opt.mode = MODE_PRIVATE;
116 else if( strcmp( q, "public" ) == 0 )
117 opt.mode = MODE_PUBLIC;
118 else
119 goto usage;
120 }
121 else if( strcmp( p, "filename" ) == 0 )
122 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000123 else if( strcmp( p, "password" ) == 0 )
124 opt.password = q;
125 else if( strcmp( p, "password_file" ) == 0 )
126 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000127 else
128 goto usage;
129 }
130
131 if( opt.mode == MODE_PRIVATE )
132 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000133 if( strlen( opt.password ) && strlen( opt.password_file ) )
134 {
135 printf( "Error: cannot have both password and password_file\n" );
136 goto usage;
137 }
138
139 if( strlen( opt.password_file ) )
140 {
141 FILE *f;
142
143 printf( "\n . Loading the password file ..." );
144 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
145 {
146 printf( " failed\n ! fopen returned NULL\n" );
147 goto exit;
148 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200149 if( fgets( buf, sizeof(buf), f ) == NULL )
150 {
151 fclose( f );
152 printf( "Error: fgets() failed to retrieve password\n" );
153 goto exit;
154 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000155 fclose( f );
156
Paul Bakker840ab202013-11-30 15:14:38 +0100157 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000158 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
159 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
160 opt.password = buf;
161 }
162
Paul Bakkered56b222011-07-13 11:26:43 +0000163 /*
164 * 1.1. Load the key
165 */
166 printf( "\n . Loading the private key ..." );
167 fflush( stdout );
168
Paul Bakker15254952013-09-17 11:24:56 +0200169 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000170
171 if( ret != 0 )
172 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200173 printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000174 goto exit;
175 }
176
177 printf( " ok\n" );
178
179 /*
180 * 1.2 Print the key
181 */
Paul Bakker2e24ca72013-09-18 15:21:27 +0200182 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200183#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200184 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200185 {
186 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200187 mpi_write_file( "N: ", &rsa->N, 16, NULL );
188 mpi_write_file( "E: ", &rsa->E, 16, NULL );
189 mpi_write_file( "D: ", &rsa->D, 16, NULL );
190 mpi_write_file( "P: ", &rsa->P, 16, NULL );
191 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
192 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
193 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
194 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
195 }
196 else
197#endif
198#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200199 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200200 {
201 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200202 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
203 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
204 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
205 mpi_write_file( "D : ", &ecp->d , 16, NULL );
206 }
207 else
208#endif
209 {
210 printf("Do not know how to print key information for this type\n" );
211 goto exit;
212 }
Paul Bakkered56b222011-07-13 11:26:43 +0000213 }
214 else if( opt.mode == MODE_PUBLIC )
215 {
216 /*
217 * 1.1. Load the key
218 */
219 printf( "\n . Loading the public key ..." );
220 fflush( stdout );
221
Paul Bakker15254952013-09-17 11:24:56 +0200222 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000223
224 if( ret != 0 )
225 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200226 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000227 goto exit;
228 }
229
230 printf( " ok\n" );
231
Paul Bakker2e24ca72013-09-18 15:21:27 +0200232 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200233#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200234 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200235 {
236 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200237 mpi_write_file( "N: ", &rsa->N, 16, NULL );
238 mpi_write_file( "E: ", &rsa->E, 16, NULL );
239 }
240 else
241#endif
242#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200243 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200244 {
245 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200246 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
247 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
248 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
249 }
250 else
251#endif
252 {
253 printf("Do not know how to print key information for this type\n" );
254 goto exit;
255 }
Paul Bakkered56b222011-07-13 11:26:43 +0000256 }
257 else
258 goto usage;
259
260exit:
261
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200262#if defined(POLARSSL_ERROR_C)
263 polarssl_strerror( ret, buf, sizeof(buf) );
264 printf( " ! Last error was: %s\n", buf );
265#endif
266
Paul Bakker15254952013-09-17 11:24:56 +0200267 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000268
Paul Bakkercce9d772011-11-18 14:26:47 +0000269#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000270 printf( " + Press Enter to exit this program.\n" );
271 fflush( stdout ); getchar();
272#endif
273
274 return( ret );
275}
Paul Bakker15254952013-09-17 11:24:56 +0200276#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */