blob: 464d4b7a8959291239fbbaec3b78ba3f96b53003 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakkered56b222011-07-13 11:26:43 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkered56b222011-07-13 11:26:43 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31#include "polarssl/config.h"
32
33#include "polarssl/error.h"
34#include "polarssl/rsa.h"
35#include "polarssl/x509.h"
36
37#define MODE_NONE 0
38#define MODE_PRIVATE 1
39#define MODE_PUBLIC 2
40
41#define DFL_MODE MODE_NONE
42#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000043#define DFL_PASSWORD ""
44#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000045#define DFL_DEBUG_LEVEL 0
46
47/*
48 * global options
49 */
50struct options
51{
52 int mode; /* the mode to run the application in */
Paul Bakkere0225e42013-06-06 12:52:24 +020053 const char *filename; /* filename of the key file */
54 const char *password; /* password for the private key */
55 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000056 int debug_level; /* level of debugging */
57} opt;
58
59void my_debug( void *ctx, int level, const char *str )
60{
61 if( level < opt.debug_level )
62 {
63 fprintf( (FILE *) ctx, "%s", str );
64 fflush( (FILE *) ctx );
65 }
66}
67
68#define USAGE \
69 "\n usage: key_app param=<>...\n" \
70 "\n acceptable parameters:\n" \
71 " mode=private|public default: none\n" \
72 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000073 " password=%%s default: \"\"\n" \
74 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000075 " debug_level=%%d default: 0 (disabled)\n" \
76 "\n"
77
78#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
79 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000080int main( int argc, char *argv[] )
Paul Bakkered56b222011-07-13 11:26:43 +000081{
Paul Bakkercce9d772011-11-18 14:26:47 +000082 ((void) argc);
83 ((void) argv);
84
Paul Bakkered56b222011-07-13 11:26:43 +000085 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
86 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
87 return( 0 );
88}
89#else
90int main( int argc, char *argv[] )
91{
92 int ret = 0;
93 rsa_context rsa;
94 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000095 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000096 char *p, *q;
97
98 /*
99 * Set to sane values
100 */
101 memset( &rsa, 0, sizeof( rsa_context ) );
102 memset( buf, 0, 1024 );
103
104 if( argc == 0 )
105 {
106 usage:
107 printf( USAGE );
108 goto exit;
109 }
110
111 opt.mode = DFL_MODE;
112 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000113 opt.password = DFL_PASSWORD;
114 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000115 opt.debug_level = DFL_DEBUG_LEVEL;
116
117 for( i = 1; i < argc; i++ )
118 {
Paul Bakkered56b222011-07-13 11:26:43 +0000119 p = argv[i];
120 if( ( q = strchr( p, '=' ) ) == NULL )
121 goto usage;
122 *q++ = '\0';
123
124 if( strcmp( p, "mode" ) == 0 )
125 {
126 if( strcmp( q, "private" ) == 0 )
127 opt.mode = MODE_PRIVATE;
128 else if( strcmp( q, "public" ) == 0 )
129 opt.mode = MODE_PUBLIC;
130 else
131 goto usage;
132 }
133 else if( strcmp( p, "filename" ) == 0 )
134 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000135 else if( strcmp( p, "password" ) == 0 )
136 opt.password = q;
137 else if( strcmp( p, "password_file" ) == 0 )
138 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000139 else if( strcmp( p, "debug_level" ) == 0 )
140 {
141 opt.debug_level = atoi( q );
142 if( opt.debug_level < 0 || opt.debug_level > 65535 )
143 goto usage;
144 }
145 else
146 goto usage;
147 }
148
149 if( opt.mode == MODE_PRIVATE )
150 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000151 if( strlen( opt.password ) && strlen( opt.password_file ) )
152 {
153 printf( "Error: cannot have both password and password_file\n" );
154 goto usage;
155 }
156
157 if( strlen( opt.password_file ) )
158 {
159 FILE *f;
160
161 printf( "\n . Loading the password file ..." );
162 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
163 {
164 printf( " failed\n ! fopen returned NULL\n" );
165 goto exit;
166 }
167 fgets( buf, 1024, f );
168 fclose( f );
169
Paul Bakker07488952013-11-30 15:14:38 +0100170 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000171 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
172 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
173 opt.password = buf;
174 }
175
Paul Bakkered56b222011-07-13 11:26:43 +0000176 /*
177 * 1.1. Load the key
178 */
179 printf( "\n . Loading the private key ..." );
180 fflush( stdout );
181
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000182 ret = x509parse_keyfile( &rsa, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000183
184 if( ret != 0 )
185 {
186#ifdef POLARSSL_ERROR_C
187 error_strerror( ret, buf, 1024 );
188#endif
189 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
190 rsa_free( &rsa );
191 goto exit;
192 }
193
194 printf( " ok\n" );
195
196 /*
197 * 1.2 Print the key
198 */
199 printf( " . Key information ...\n" );
200 mpi_write_file( "N: ", &rsa.N, 16, NULL );
201 mpi_write_file( "E: ", &rsa.E, 16, NULL );
202 mpi_write_file( "D: ", &rsa.D, 16, NULL );
203 mpi_write_file( "P: ", &rsa.P, 16, NULL );
204 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
205 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
206 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
207 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
208 }
209 else if( opt.mode == MODE_PUBLIC )
210 {
211 /*
212 * 1.1. Load the key
213 */
214 printf( "\n . Loading the public key ..." );
215 fflush( stdout );
216
217 ret = x509parse_public_keyfile( &rsa, opt.filename );
218
219 if( ret != 0 )
220 {
221#ifdef POLARSSL_ERROR_C
222 error_strerror( ret, buf, 1024 );
223#endif
224 printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
225 rsa_free( &rsa );
226 goto exit;
227 }
228
229 printf( " ok\n" );
230
231 /*
232 * 1.2 Print the key
233 */
234 printf( " . Key information ...\n" );
235 mpi_write_file( "N: ", &rsa.N, 16, NULL );
236 mpi_write_file( "E: ", &rsa.E, 16, NULL );
237 }
238 else
239 goto usage;
240
241exit:
242
243 rsa_free( &rsa );
244
Paul Bakkercce9d772011-11-18 14:26:47 +0000245#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000246 printf( " + Press Enter to exit this program.\n" );
247 fflush( stdout ); getchar();
248#endif
249
250 return( ret );
251}
252#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
253 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */