blob: b3b235eff36ac3bd4cbb9644b19f3ef333848154 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/config.h"
35
36#include "polarssl/error.h"
37#include "polarssl/rsa.h"
38#include "polarssl/x509.h"
39
Paul Bakker15254952013-09-17 11:24:56 +020040#if !defined(POLARSSL_BIGNUM_C) || \
41 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
42int main( int argc, char *argv[] )
43{
44 ((void) argc);
45 ((void) argv);
46
47 printf("POLARSSL_BIGNUM_C and/or "
48 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
49 return( 0 );
50}
51#else
52
Paul Bakkered56b222011-07-13 11:26:43 +000053#define MODE_NONE 0
54#define MODE_PRIVATE 1
55#define MODE_PUBLIC 2
56
57#define DFL_MODE MODE_NONE
58#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000059#define DFL_PASSWORD ""
60#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000061#define DFL_DEBUG_LEVEL 0
62
63/*
64 * global options
65 */
66struct options
67{
68 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020069 const char *filename; /* filename of the key file */
70 const char *password; /* password for the private key */
71 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000072} opt;
73
Paul Bakkered56b222011-07-13 11:26:43 +000074#define USAGE \
75 "\n usage: key_app param=<>...\n" \
76 "\n acceptable parameters:\n" \
77 " mode=private|public default: none\n" \
78 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000079 " password=%%s default: \"\"\n" \
80 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000081 "\n"
82
Paul Bakkered56b222011-07-13 11:26:43 +000083int main( int argc, char *argv[] )
84{
85 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020086 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000087 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000088 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000089 char *p, *q;
90
91 /*
92 * Set to sane values
93 */
Paul Bakker15254952013-09-17 11:24:56 +020094 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020095 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000096
97 if( argc == 0 )
98 {
99 usage:
100 printf( USAGE );
101 goto exit;
102 }
103
104 opt.mode = DFL_MODE;
105 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000106 opt.password = DFL_PASSWORD;
107 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000108
109 for( i = 1; i < argc; i++ )
110 {
Paul Bakkered56b222011-07-13 11:26:43 +0000111 p = argv[i];
112 if( ( q = strchr( p, '=' ) ) == NULL )
113 goto usage;
114 *q++ = '\0';
115
116 if( strcmp( p, "mode" ) == 0 )
117 {
118 if( strcmp( q, "private" ) == 0 )
119 opt.mode = MODE_PRIVATE;
120 else if( strcmp( q, "public" ) == 0 )
121 opt.mode = MODE_PUBLIC;
122 else
123 goto usage;
124 }
125 else if( strcmp( p, "filename" ) == 0 )
126 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000127 else if( strcmp( p, "password" ) == 0 )
128 opt.password = q;
129 else if( strcmp( p, "password_file" ) == 0 )
130 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000131 else
132 goto usage;
133 }
134
135 if( opt.mode == MODE_PRIVATE )
136 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000137 if( strlen( opt.password ) && strlen( opt.password_file ) )
138 {
139 printf( "Error: cannot have both password and password_file\n" );
140 goto usage;
141 }
142
143 if( strlen( opt.password_file ) )
144 {
145 FILE *f;
146
147 printf( "\n . Loading the password file ..." );
148 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
149 {
150 printf( " failed\n ! fopen returned NULL\n" );
151 goto exit;
152 }
Paul Bakker2e24ca72013-09-18 15:21:27 +0200153 fgets( buf, sizeof(buf), f );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000154 fclose( f );
155
156 i = strlen( buf );
157 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
158 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
159 opt.password = buf;
160 }
161
Paul Bakkered56b222011-07-13 11:26:43 +0000162 /*
163 * 1.1. Load the key
164 */
165 printf( "\n . Loading the private key ..." );
166 fflush( stdout );
167
Paul Bakker15254952013-09-17 11:24:56 +0200168 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000169
170 if( ret != 0 )
171 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200172 printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000173 goto exit;
174 }
175
176 printf( " ok\n" );
177
178 /*
179 * 1.2 Print the key
180 */
Paul Bakker2e24ca72013-09-18 15:21:27 +0200181 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200182#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200183 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200184 {
185 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200186 mpi_write_file( "N: ", &rsa->N, 16, NULL );
187 mpi_write_file( "E: ", &rsa->E, 16, NULL );
188 mpi_write_file( "D: ", &rsa->D, 16, NULL );
189 mpi_write_file( "P: ", &rsa->P, 16, NULL );
190 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
191 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
192 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
193 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
194 }
195 else
196#endif
197#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200198 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200199 {
200 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200201 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
202 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
203 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
204 mpi_write_file( "D : ", &ecp->d , 16, NULL );
205 }
206 else
207#endif
208 {
209 printf("Do not know how to print key information for this type\n" );
210 goto exit;
211 }
Paul Bakkered56b222011-07-13 11:26:43 +0000212 }
213 else if( opt.mode == MODE_PUBLIC )
214 {
215 /*
216 * 1.1. Load the key
217 */
218 printf( "\n . Loading the public key ..." );
219 fflush( stdout );
220
Paul Bakker15254952013-09-17 11:24:56 +0200221 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000222
223 if( ret != 0 )
224 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200225 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000226 goto exit;
227 }
228
229 printf( " ok\n" );
230
Paul Bakker2e24ca72013-09-18 15:21:27 +0200231 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200232#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200233 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200234 {
235 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200236 mpi_write_file( "N: ", &rsa->N, 16, NULL );
237 mpi_write_file( "E: ", &rsa->E, 16, NULL );
238 }
239 else
240#endif
241#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200242 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200243 {
244 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200245 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
246 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
247 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
248 }
249 else
250#endif
251 {
252 printf("Do not know how to print key information for this type\n" );
253 goto exit;
254 }
Paul Bakkered56b222011-07-13 11:26:43 +0000255 }
256 else
257 goto usage;
258
259exit:
260
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200261#if defined(POLARSSL_ERROR_C)
262 polarssl_strerror( ret, buf, sizeof(buf) );
263 printf( " ! Last error was: %s\n", buf );
264#endif
265
Paul Bakker15254952013-09-17 11:24:56 +0200266 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000267
Paul Bakkercce9d772011-11-18 14:26:47 +0000268#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000269 printf( " + Press Enter to exit this program.\n" );
270 fflush( stdout ); getchar();
271#endif
272
273 return( ret );
274}
Paul Bakker15254952013-09-17 11:24:56 +0200275#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */