blob: 04bad874e7022dcec35cdf094549e39605d300d7 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkered56b222011-07-13 11:26:43 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkered56b222011-07-13 11:26:43 +00007 *
Paul Bakkered56b222011-07-13 11:26:43 +00008 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkered56b222011-07-13 11:26:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakkered56b222011-07-13 11:26:43 +000035#include <string.h>
36#include <stdlib.h>
37#include <stdio.h>
38
Paul Bakkered56b222011-07-13 11:26:43 +000039#include "polarssl/error.h"
40#include "polarssl/rsa.h"
41#include "polarssl/x509.h"
42
Paul Bakker15254952013-09-17 11:24:56 +020043#if !defined(POLARSSL_BIGNUM_C) || \
44 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
45int main( int argc, char *argv[] )
46{
47 ((void) argc);
48 ((void) argv);
49
Rich Evansf90016a2015-01-19 14:26:37 +000050 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Paul Bakker15254952013-09-17 11:24:56 +020051 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
52 return( 0 );
53}
54#else
55
Paul Bakkered56b222011-07-13 11:26:43 +000056#define MODE_NONE 0
57#define MODE_PRIVATE 1
58#define MODE_PUBLIC 2
59
60#define DFL_MODE MODE_NONE
61#define DFL_FILENAME "keyfile.key"
Paul Bakkerdb2509c2012-09-27 12:44:31 +000062#define DFL_PASSWORD ""
63#define DFL_PASSWORD_FILE ""
Paul Bakkered56b222011-07-13 11:26:43 +000064#define DFL_DEBUG_LEVEL 0
65
66/*
67 * global options
68 */
69struct options
70{
71 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072 const char *filename; /* filename of the key file */
73 const char *password; /* password for the private key */
74 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000075} opt;
76
Paul Bakkered56b222011-07-13 11:26:43 +000077#define USAGE \
78 "\n usage: key_app param=<>...\n" \
79 "\n acceptable parameters:\n" \
80 " mode=private|public default: none\n" \
81 " filename=%%s default: keyfile.key\n" \
Paul Bakkerdb2509c2012-09-27 12:44:31 +000082 " password=%%s default: \"\"\n" \
83 " password_file=%%s default: \"\"\n" \
Paul Bakkered56b222011-07-13 11:26:43 +000084 "\n"
85
Paul Bakkered56b222011-07-13 11:26:43 +000086int main( int argc, char *argv[] )
87{
88 int ret = 0;
Paul Bakker15254952013-09-17 11:24:56 +020089 pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000090 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000091 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000092 char *p, *q;
93
94 /*
95 * Set to sane values
96 */
Paul Bakker15254952013-09-17 11:24:56 +020097 pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020098 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000099
100 if( argc == 0 )
101 {
102 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( USAGE );
Paul Bakkered56b222011-07-13 11:26:43 +0000104 goto exit;
105 }
106
107 opt.mode = DFL_MODE;
108 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000109 opt.password = DFL_PASSWORD;
110 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000111
112 for( i = 1; i < argc; i++ )
113 {
Paul Bakkered56b222011-07-13 11:26:43 +0000114 p = argv[i];
115 if( ( q = strchr( p, '=' ) ) == NULL )
116 goto usage;
117 *q++ = '\0';
118
119 if( strcmp( p, "mode" ) == 0 )
120 {
121 if( strcmp( q, "private" ) == 0 )
122 opt.mode = MODE_PRIVATE;
123 else if( strcmp( q, "public" ) == 0 )
124 opt.mode = MODE_PUBLIC;
125 else
126 goto usage;
127 }
128 else if( strcmp( p, "filename" ) == 0 )
129 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000130 else if( strcmp( p, "password" ) == 0 )
131 opt.password = q;
132 else if( strcmp( p, "password_file" ) == 0 )
133 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000134 else
135 goto usage;
136 }
137
138 if( opt.mode == MODE_PRIVATE )
139 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000140 if( strlen( opt.password ) && strlen( opt.password_file ) )
141 {
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000143 goto usage;
144 }
145
146 if( strlen( opt.password_file ) )
147 {
148 FILE *f;
149
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000151 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
152 {
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( " failed\n ! fopen returned NULL\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000154 goto exit;
155 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200156 if( fgets( buf, sizeof(buf), f ) == NULL )
157 {
158 fclose( f );
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( "Error: fgets() failed to retrieve password\n" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200160 goto exit;
161 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000162 fclose( f );
163
Paul Bakker840ab202013-11-30 15:14:38 +0100164 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000165 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
166 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
167 opt.password = buf;
168 }
169
Paul Bakkered56b222011-07-13 11:26:43 +0000170 /*
171 * 1.1. Load the key
172 */
Rich Evansf90016a2015-01-19 14:26:37 +0000173 polarssl_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000174 fflush( stdout );
175
Paul Bakker15254952013-09-17 11:24:56 +0200176 ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000177
178 if( ret != 0 )
179 {
Rich Evansf90016a2015-01-19 14:26:37 +0000180 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000181 goto exit;
182 }
183
Rich Evansf90016a2015-01-19 14:26:37 +0000184 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000185
186 /*
187 * 1.2 Print the key
188 */
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200190#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200191 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200192 {
193 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200194 mpi_write_file( "N: ", &rsa->N, 16, NULL );
195 mpi_write_file( "E: ", &rsa->E, 16, NULL );
196 mpi_write_file( "D: ", &rsa->D, 16, NULL );
197 mpi_write_file( "P: ", &rsa->P, 16, NULL );
198 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
199 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
200 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
201 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
202 }
203 else
204#endif
205#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200206 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200207 {
208 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200209 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
210 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
211 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
212 mpi_write_file( "D : ", &ecp->d , 16, NULL );
213 }
214 else
215#endif
216 {
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200218 goto exit;
219 }
Paul Bakkered56b222011-07-13 11:26:43 +0000220 }
221 else if( opt.mode == MODE_PUBLIC )
222 {
223 /*
224 * 1.1. Load the key
225 */
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000227 fflush( stdout );
228
Paul Bakker15254952013-09-17 11:24:56 +0200229 ret = pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000230
231 if( ret != 0 )
232 {
Rich Evansf90016a2015-01-19 14:26:37 +0000233 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000234 goto exit;
235 }
236
Rich Evansf90016a2015-01-19 14:26:37 +0000237 polarssl_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000238
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_printf( " . Key information ...\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200240#if defined(POLARSSL_RSA_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200241 if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200242 {
243 rsa_context *rsa = pk_rsa( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200244 mpi_write_file( "N: ", &rsa->N, 16, NULL );
245 mpi_write_file( "E: ", &rsa->E, 16, NULL );
246 }
247 else
248#endif
249#if defined(POLARSSL_ECP_C)
Paul Bakker2e24ca72013-09-18 15:21:27 +0200250 if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200251 {
252 ecp_keypair *ecp = pk_ec( pk );
Paul Bakker15254952013-09-17 11:24:56 +0200253 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
254 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
255 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
256 }
257 else
258#endif
259 {
Rich Evansf90016a2015-01-19 14:26:37 +0000260 polarssl_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200261 goto exit;
262 }
Paul Bakkered56b222011-07-13 11:26:43 +0000263 }
264 else
265 goto usage;
266
267exit:
268
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200269#if defined(POLARSSL_ERROR_C)
270 polarssl_strerror( ret, buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000271 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200272#endif
273
Paul Bakker15254952013-09-17 11:24:56 +0200274 pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000275
Paul Bakkercce9d772011-11-18 14:26:47 +0000276#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000277 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000278 fflush( stdout ); getchar();
279#endif
280
281 return( ret );
282}
Paul Bakker15254952013-09-17 11:24:56 +0200283#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */