blob: 051ceb511d03bbbef66b7969a371907e0592add7 [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
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} opt;
60
Paul Bakkered56b222011-07-13 11:26:43 +000061#define USAGE \
62 "\n usage: key_app param=<>...\n" \
63 "\n acceptable parameters:\n" \
64 " mode=private|public default: none\n" \
65 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000066 " password=%%s default: \"\"\n" \
67 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000068 "\n"
69
70#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
71 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000072int main( int argc, char *argv[] )
Paul Bakkered56b222011-07-13 11:26:43 +000073{
Paul Bakkercce9d772011-11-18 14:26:47 +000074 ((void) argc);
75 ((void) argv);
76
Paul Bakkered56b222011-07-13 11:26:43 +000077 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
78 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
79 return( 0 );
80}
81#else
82int main( int argc, char *argv[] )
83{
84 int ret = 0;
85 rsa_context rsa;
86 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000087 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000088 char *p, *q;
89
90 /*
91 * Set to sane values
92 */
93 memset( &rsa, 0, sizeof( rsa_context ) );
94 memset( buf, 0, 1024 );
95
96 if( argc == 0 )
97 {
98 usage:
99 printf( USAGE );
100 goto exit;
101 }
102
103 opt.mode = DFL_MODE;
104 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000105 opt.password = DFL_PASSWORD;
106 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000107
108 for( i = 1; i < argc; i++ )
109 {
Paul Bakkered56b222011-07-13 11:26:43 +0000110 p = argv[i];
111 if( ( q = strchr( p, '=' ) ) == NULL )
112 goto usage;
113 *q++ = '\0';
114
115 if( strcmp( p, "mode" ) == 0 )
116 {
117 if( strcmp( q, "private" ) == 0 )
118 opt.mode = MODE_PRIVATE;
119 else if( strcmp( q, "public" ) == 0 )
120 opt.mode = MODE_PUBLIC;
121 else
122 goto usage;
123 }
124 else if( strcmp( p, "filename" ) == 0 )
125 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000126 else if( strcmp( p, "password" ) == 0 )
127 opt.password = q;
128 else if( strcmp( p, "password_file" ) == 0 )
129 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000130 else
131 goto usage;
132 }
133
134 if( opt.mode == MODE_PRIVATE )
135 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000136 if( strlen( opt.password ) && strlen( opt.password_file ) )
137 {
138 printf( "Error: cannot have both password and password_file\n" );
139 goto usage;
140 }
141
142 if( strlen( opt.password_file ) )
143 {
144 FILE *f;
145
146 printf( "\n . Loading the password file ..." );
147 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
148 {
149 printf( " failed\n ! fopen returned NULL\n" );
150 goto exit;
151 }
152 fgets( buf, 1024, f );
153 fclose( f );
154
155 i = strlen( buf );
156 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
157 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
158 opt.password = buf;
159 }
160
Paul Bakkered56b222011-07-13 11:26:43 +0000161 /*
162 * 1.1. Load the key
163 */
164 printf( "\n . Loading the private key ..." );
165 fflush( stdout );
166
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000167 ret = x509parse_keyfile( &rsa, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000168
169 if( ret != 0 )
170 {
171#ifdef POLARSSL_ERROR_C
172 error_strerror( ret, buf, 1024 );
173#endif
174 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
175 rsa_free( &rsa );
176 goto exit;
177 }
178
179 printf( " ok\n" );
180
181 /*
182 * 1.2 Print the key
183 */
184 printf( " . Key information ...\n" );
185 mpi_write_file( "N: ", &rsa.N, 16, NULL );
186 mpi_write_file( "E: ", &rsa.E, 16, NULL );
187 mpi_write_file( "D: ", &rsa.D, 16, NULL );
188 mpi_write_file( "P: ", &rsa.P, 16, NULL );
189 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
190 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
191 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
192 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
193 }
194 else if( opt.mode == MODE_PUBLIC )
195 {
196 /*
197 * 1.1. Load the key
198 */
199 printf( "\n . Loading the public key ..." );
200 fflush( stdout );
201
202 ret = x509parse_public_keyfile( &rsa, opt.filename );
203
204 if( ret != 0 )
205 {
206#ifdef POLARSSL_ERROR_C
207 error_strerror( ret, buf, 1024 );
208#endif
209 printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
210 rsa_free( &rsa );
211 goto exit;
212 }
213
214 printf( " ok\n" );
215
216 /*
217 * 1.2 Print the key
218 */
219 printf( " . Key information ...\n" );
220 mpi_write_file( "N: ", &rsa.N, 16, NULL );
221 mpi_write_file( "E: ", &rsa.E, 16, NULL );
222 }
223 else
224 goto usage;
225
226exit:
227
228 rsa_free( &rsa );
229
Paul Bakkercce9d772011-11-18 14:26:47 +0000230#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000231 printf( " + Press Enter to exit this program.\n" );
232 fflush( stdout ); getchar();
233#endif
234
235 return( ret );
236}
237#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
238 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */