blob: fc0269e28a7404f16c31cd0f81092b6140335d33 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Paul Bakkerdb2509c2012-09-27 12:44:31 +00004 * Copyright (C) 2006-2012, 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
40#define MODE_NONE 0
41#define MODE_PRIVATE 1
42#define MODE_PUBLIC 2
43
44#define DFL_MODE MODE_NONE
45#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000046#define DFL_PASSWORD ""
47#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000048#define DFL_DEBUG_LEVEL 0
49
50/*
51 * global options
52 */
53struct options
54{
55 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020056 const char *filename; /* filename of the key file */
57 const char *password; /* password for the private key */
58 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000059 int debug_level; /* level of debugging */
60} opt;
61
62void my_debug( void *ctx, int level, const char *str )
63{
64 if( level < opt.debug_level )
65 {
66 fprintf( (FILE *) ctx, "%s", str );
67 fflush( (FILE *) ctx );
68 }
69}
70
71#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 " debug_level=%%d default: 0 (disabled)\n" \
79 "\n"
80
81#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
82 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000083int main( int argc, char *argv[] )
Paul Bakkered56b222011-07-13 11:26:43 +000084{
Paul Bakkercce9d772011-11-18 14:26:47 +000085 ((void) argc);
86 ((void) argv);
87
Paul Bakkered56b222011-07-13 11:26:43 +000088 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
89 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
90 return( 0 );
91}
92#else
93int main( int argc, char *argv[] )
94{
95 int ret = 0;
96 rsa_context rsa;
97 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000098 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000099 char *p, *q;
100
101 /*
102 * Set to sane values
103 */
104 memset( &rsa, 0, sizeof( rsa_context ) );
105 memset( buf, 0, 1024 );
106
107 if( argc == 0 )
108 {
109 usage:
110 printf( USAGE );
111 goto exit;
112 }
113
114 opt.mode = DFL_MODE;
115 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000116 opt.password = DFL_PASSWORD;
117 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000118 opt.debug_level = DFL_DEBUG_LEVEL;
119
120 for( i = 1; i < argc; i++ )
121 {
Paul Bakkered56b222011-07-13 11:26:43 +0000122 p = argv[i];
123 if( ( q = strchr( p, '=' ) ) == NULL )
124 goto usage;
125 *q++ = '\0';
126
127 if( strcmp( p, "mode" ) == 0 )
128 {
129 if( strcmp( q, "private" ) == 0 )
130 opt.mode = MODE_PRIVATE;
131 else if( strcmp( q, "public" ) == 0 )
132 opt.mode = MODE_PUBLIC;
133 else
134 goto usage;
135 }
136 else if( strcmp( p, "filename" ) == 0 )
137 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 else if( strcmp( p, "password" ) == 0 )
139 opt.password = q;
140 else if( strcmp( p, "password_file" ) == 0 )
141 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000142 else if( strcmp( p, "debug_level" ) == 0 )
143 {
144 opt.debug_level = atoi( q );
145 if( opt.debug_level < 0 || opt.debug_level > 65535 )
146 goto usage;
147 }
148 else
149 goto usage;
150 }
151
152 if( opt.mode == MODE_PRIVATE )
153 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000154 if( strlen( opt.password ) && strlen( opt.password_file ) )
155 {
156 printf( "Error: cannot have both password and password_file\n" );
157 goto usage;
158 }
159
160 if( strlen( opt.password_file ) )
161 {
162 FILE *f;
163
164 printf( "\n . Loading the password file ..." );
165 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
166 {
167 printf( " failed\n ! fopen returned NULL\n" );
168 goto exit;
169 }
170 fgets( buf, 1024, f );
171 fclose( f );
172
173 i = strlen( buf );
174 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
175 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
176 opt.password = buf;
177 }
178
Paul Bakkered56b222011-07-13 11:26:43 +0000179 /*
180 * 1.1. Load the key
181 */
182 printf( "\n . Loading the private key ..." );
183 fflush( stdout );
184
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000185 ret = x509parse_keyfile( &rsa, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000186
187 if( ret != 0 )
188 {
189#ifdef POLARSSL_ERROR_C
190 error_strerror( ret, buf, 1024 );
191#endif
192 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
193 rsa_free( &rsa );
194 goto exit;
195 }
196
197 printf( " ok\n" );
198
199 /*
200 * 1.2 Print the key
201 */
202 printf( " . Key information ...\n" );
203 mpi_write_file( "N: ", &rsa.N, 16, NULL );
204 mpi_write_file( "E: ", &rsa.E, 16, NULL );
205 mpi_write_file( "D: ", &rsa.D, 16, NULL );
206 mpi_write_file( "P: ", &rsa.P, 16, NULL );
207 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
208 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
209 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
210 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
211 }
212 else if( opt.mode == MODE_PUBLIC )
213 {
214 /*
215 * 1.1. Load the key
216 */
217 printf( "\n . Loading the public key ..." );
218 fflush( stdout );
219
220 ret = x509parse_public_keyfile( &rsa, opt.filename );
221
222 if( ret != 0 )
223 {
224#ifdef POLARSSL_ERROR_C
225 error_strerror( ret, buf, 1024 );
226#endif
227 printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
228 rsa_free( &rsa );
229 goto exit;
230 }
231
232 printf( " ok\n" );
233
234 /*
235 * 1.2 Print the key
236 */
237 printf( " . Key information ...\n" );
238 mpi_write_file( "N: ", &rsa.N, 16, NULL );
239 mpi_write_file( "E: ", &rsa.E, 16, NULL );
240 }
241 else
242 goto usage;
243
244exit:
245
246 rsa_free( &rsa );
247
Paul Bakkercce9d772011-11-18 14:26:47 +0000248#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000249 printf( " + Press Enter to exit this program.\n" );
250 fflush( stdout ); getchar();
251#endif
252
253 return( ret );
254}
255#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
256 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */