blob: 064643a4223dc18c5e83b50d8ca2769e75054351 [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 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkered56b222011-07-13 11:26:43 +00007 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkered56b222011-07-13 11:26:43 +000031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakkered56b222011-07-13 11:26:43 +000036#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 Bakker8a0c0a92014-04-17 16:08:20 +0200153 if( fgets( buf, sizeof(buf), f ) == NULL )
154 {
155 fclose( f );
156 printf( "Error: fgets() failed to retrieve password\n" );
157 goto exit;
158 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000159 fclose( f );
160
Paul Bakker840ab202013-11-30 15:14:38 +0100161 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000162 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
163 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
164 opt.password = buf;
165 }
166
Paul Bakkered56b222011-07-13 11:26:43 +0000167 /*
168 * 1.1. Load the key
169 */
170 printf( "\n . Loading the private key ..." );
171 fflush( stdout );
172
Paul Bakker15254952013-09-17 11:24:56 +0200173 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000174
175 if( ret != 0 )
176 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200177 printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000178 goto exit;
179 }
180
181 printf( " ok\n" );
182
183 /*
184 * 1.2 Print the key
185 */
Paul Bakker2e24ca72013-09-18 15:21:27 +0200186 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200187#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200188 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200189 {
190 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200191 mpi_write_file( "N: ", &rsa->N, 16, NULL );
192 mpi_write_file( "E: ", &rsa->E, 16, NULL );
193 mpi_write_file( "D: ", &rsa->D, 16, NULL );
194 mpi_write_file( "P: ", &rsa->P, 16, NULL );
195 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
196 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
197 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
198 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
199 }
200 else
201#endif
202#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200203 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200204 {
205 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200206 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
207 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
208 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
209 mpi_write_file( "D : ", &ecp->d , 16, NULL );
210 }
211 else
212#endif
213 {
214 printf("Do not know how to print key information for this type\n" );
215 goto exit;
216 }
Paul Bakkered56b222011-07-13 11:26:43 +0000217 }
218 else if( opt.mode == MODE_PUBLIC )
219 {
220 /*
221 * 1.1. Load the key
222 */
223 printf( "\n . Loading the public key ..." );
224 fflush( stdout );
225
Paul Bakker15254952013-09-17 11:24:56 +0200226 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000227
228 if( ret != 0 )
229 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200230 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000231 goto exit;
232 }
233
234 printf( " ok\n" );
235
Paul Bakker2e24ca72013-09-18 15:21:27 +0200236 printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200237#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200238 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200239 {
240 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200241 mpi_write_file( "N: ", &rsa->N, 16, NULL );
242 mpi_write_file( "E: ", &rsa->E, 16, NULL );
243 }
244 else
245#endif
246#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200247 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200248 {
249 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200250 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
251 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
252 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
253 }
254 else
255#endif
256 {
257 printf("Do not know how to print key information for this type\n" );
258 goto exit;
259 }
Paul Bakkered56b222011-07-13 11:26:43 +0000260 }
261 else
262 goto usage;
263
264exit:
265
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200266#if defined(POLARSSL_ERROR_C)
267 polarssl_strerror( ret, buf, sizeof(buf) );
268 printf( " ! Last error was: %s\n", buf );
269#endif
270
Paul Bakker15254952013-09-17 11:24:56 +0200271 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000272
Paul Bakkercce9d772011-11-18 14:26:47 +0000273#if defined(_WIN32)
Paul Bakkered56b222011-07-13 11:26:43 +0000274 printf( " + Press Enter to exit this program.\n" );
275 fflush( stdout ); getchar();
276#endif
277
278 return( ret );
279}
Paul Bakker15254952013-09-17 11:24:56 +0200280#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */