blob: 7d4a1d9cf07e88ed8e5a7229580f4e982f8c017f [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, 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"
46#define DFL_DEBUG_LEVEL 0
47
48/*
49 * global options
50 */
51struct options
52{
53 int mode; /* the mode to run the application in */
54 char *filename; /* filename of the key file */
55 int debug_level; /* level of debugging */
56} opt;
57
58void my_debug( void *ctx, int level, const char *str )
59{
60 if( level < opt.debug_level )
61 {
62 fprintf( (FILE *) ctx, "%s", str );
63 fflush( (FILE *) ctx );
64 }
65}
66
67#define USAGE \
68 "\n usage: key_app param=<>...\n" \
69 "\n acceptable parameters:\n" \
70 " mode=private|public default: none\n" \
71 " filename=%%s default: keyfile.key\n" \
72 " debug_level=%%d default: 0 (disabled)\n" \
73 "\n"
74
75#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
76 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000077int main( int argc, char *argv[] )
Paul Bakkered56b222011-07-13 11:26:43 +000078{
Paul Bakkercce9d772011-11-18 14:26:47 +000079 ((void) argc);
80 ((void) argv);
81
Paul Bakkered56b222011-07-13 11:26:43 +000082 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
83 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
84 return( 0 );
85}
86#else
87int main( int argc, char *argv[] )
88{
89 int ret = 0;
90 rsa_context rsa;
91 char buf[1024];
92 int i, j, n;
93 char *p, *q;
94
95 /*
96 * Set to sane values
97 */
98 memset( &rsa, 0, sizeof( rsa_context ) );
99 memset( buf, 0, 1024 );
100
101 if( argc == 0 )
102 {
103 usage:
104 printf( USAGE );
105 goto exit;
106 }
107
108 opt.mode = DFL_MODE;
109 opt.filename = DFL_FILENAME;
110 opt.debug_level = DFL_DEBUG_LEVEL;
111
112 for( i = 1; i < argc; i++ )
113 {
114 n = strlen( argv[i] );
115
116 for( j = 0; j < n; j++ )
117 {
118 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
119 argv[i][j] |= 0x20;
120 }
121
122 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;
138 else if( strcmp( p, "debug_level" ) == 0 )
139 {
140 opt.debug_level = atoi( q );
141 if( opt.debug_level < 0 || opt.debug_level > 65535 )
142 goto usage;
143 }
144 else
145 goto usage;
146 }
147
148 if( opt.mode == MODE_PRIVATE )
149 {
150 /*
151 * 1.1. Load the key
152 */
153 printf( "\n . Loading the private key ..." );
154 fflush( stdout );
155
156 ret = x509parse_keyfile( &rsa, opt.filename, NULL );
157
158 if( ret != 0 )
159 {
160#ifdef POLARSSL_ERROR_C
161 error_strerror( ret, buf, 1024 );
162#endif
163 printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
164 rsa_free( &rsa );
165 goto exit;
166 }
167
168 printf( " ok\n" );
169
170 /*
171 * 1.2 Print the key
172 */
173 printf( " . Key information ...\n" );
174 mpi_write_file( "N: ", &rsa.N, 16, NULL );
175 mpi_write_file( "E: ", &rsa.E, 16, NULL );
176 mpi_write_file( "D: ", &rsa.D, 16, NULL );
177 mpi_write_file( "P: ", &rsa.P, 16, NULL );
178 mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
179 mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
180 mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
181 mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
182 }
183 else if( opt.mode == MODE_PUBLIC )
184 {
185 /*
186 * 1.1. Load the key
187 */
188 printf( "\n . Loading the public key ..." );
189 fflush( stdout );
190
191 ret = x509parse_public_keyfile( &rsa, opt.filename );
192
193 if( ret != 0 )
194 {
195#ifdef POLARSSL_ERROR_C
196 error_strerror( ret, buf, 1024 );
197#endif
198 printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
199 rsa_free( &rsa );
200 goto exit;
201 }
202
203 printf( " ok\n" );
204
205 /*
206 * 1.2 Print the key
207 */
208 printf( " . Key information ...\n" );
209 mpi_write_file( "N: ", &rsa.N, 16, NULL );
210 mpi_write_file( "E: ", &rsa.E, 16, NULL );
211 }
212 else
213 goto usage;
214
215exit:
216
217 rsa_free( &rsa );
218
Paul Bakkercce9d772011-11-18 14:26:47 +0000219#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000220 printf( " + Press Enter to exit this program.\n" );
221 fflush( stdout ); getchar();
222#endif
223
224 return( ret );
225}
226#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
227 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */