blob: d4c30d22bd6b72ae39b286b694f323ca48e9d89a [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 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200172 polarssl_strerror( ret, buf, sizeof(buf) );
173 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
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 {
Paul Bakker2e24ca72013-09-18 15:21:27 +0200226 polarssl_strerror( ret, buf, sizeof(buf) );
227 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x - %s\n\n", -ret, buf );
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
Paul Bakker15254952013-09-17 11:24:56 +0200263 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000264
Paul Bakkercce9d772011-11-18 14:26:47 +0000265#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000266 printf( " + Press Enter to exit this program.\n" );
267 fflush( stdout ); getchar();
268#endif
269
270 return( ret );
271}
Paul Bakker15254952013-09-17 11:24:56 +0200272#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */