blob: b6b84464d232e1e495cba4f0d41d96f6168e9b0d [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkered56b222011-07-13 11:26:43 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered56b222011-07-13 11:26:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkered56b222011-07-13 11:26:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_BIGNUM_C) && \
36 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/error.h"
38#include "mbedtls/rsa.h"
39#include "mbedtls/x509.h"
Paul Bakkered56b222011-07-13 11:26:43 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <string.h>
42#endif
43
44#define MODE_NONE 0
45#define MODE_PRIVATE 1
46#define MODE_PUBLIC 2
47
48#define DFL_MODE MODE_NONE
49#define DFL_FILENAME "keyfile.key"
50#define DFL_PASSWORD ""
51#define DFL_PASSWORD_FILE ""
52#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000053
Rich Evans18b78c72015-02-11 14:06:19 +000054#define USAGE \
55 "\n usage: key_app param=<>...\n" \
56 "\n acceptable parameters:\n" \
57 " mode=private|public default: none\n" \
58 " filename=%%s default: keyfile.key\n" \
59 " password=%%s default: \"\"\n" \
60 " password_file=%%s default: \"\"\n" \
61 "\n"
62
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_BIGNUM_C) || \
65 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000066int main( void )
Paul Bakker15254952013-09-17 11:24:56 +020067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
69 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker15254952013-09-17 11:24:56 +020070 return( 0 );
71}
72#else
Paul Bakkered56b222011-07-13 11:26:43 +000073/*
74 * global options
75 */
76struct options
77{
78 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *filename; /* filename of the key file */
80 const char *password; /* password for the private key */
81 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +000082} opt;
83
Paul Bakkered56b222011-07-13 11:26:43 +000084int main( int argc, char *argv[] )
85{
86 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_pk_context pk;
Paul Bakkered56b222011-07-13 11:26:43 +000088 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +000089 int i;
Paul Bakkered56b222011-07-13 11:26:43 +000090 char *p, *q;
91
92 /*
93 * Set to sane values
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_pk_init( &pk );
Paul Bakker2e24ca72013-09-18 15:21:27 +020096 memset( buf, 0, sizeof(buf) );
Paul Bakkered56b222011-07-13 11:26:43 +000097
98 if( argc == 0 )
99 {
100 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( USAGE );
Paul Bakkered56b222011-07-13 11:26:43 +0000102 goto exit;
103 }
104
105 opt.mode = DFL_MODE;
106 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000107 opt.password = DFL_PASSWORD;
108 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000109
110 for( i = 1; i < argc; i++ )
111 {
Paul Bakkered56b222011-07-13 11:26:43 +0000112 p = argv[i];
113 if( ( q = strchr( p, '=' ) ) == NULL )
114 goto usage;
115 *q++ = '\0';
116
117 if( strcmp( p, "mode" ) == 0 )
118 {
119 if( strcmp( q, "private" ) == 0 )
120 opt.mode = MODE_PRIVATE;
121 else if( strcmp( q, "public" ) == 0 )
122 opt.mode = MODE_PUBLIC;
123 else
124 goto usage;
125 }
126 else if( strcmp( p, "filename" ) == 0 )
127 opt.filename = q;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000128 else if( strcmp( p, "password" ) == 0 )
129 opt.password = q;
130 else if( strcmp( p, "password_file" ) == 0 )
131 opt.password_file = q;
Paul Bakkered56b222011-07-13 11:26:43 +0000132 else
133 goto usage;
134 }
135
136 if( opt.mode == MODE_PRIVATE )
137 {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000138 if( strlen( opt.password ) && strlen( opt.password_file ) )
139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( "Error: cannot have both password and password_file\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000141 goto usage;
142 }
143
144 if( strlen( opt.password_file ) )
145 {
146 FILE *f;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( "\n . Loading the password file ..." );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000149 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000152 goto exit;
153 }
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200154 if( fgets( buf, sizeof(buf), f ) == NULL )
155 {
156 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200158 goto exit;
159 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000160 fclose( f );
161
Paul Bakker840ab202013-11-30 15:14:38 +0100162 i = (int) strlen( buf );
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000163 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
164 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
165 opt.password = buf;
166 }
167
Paul Bakkered56b222011-07-13 11:26:43 +0000168 /*
169 * 1.1. Load the key
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( "\n . Loading the private key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000172 fflush( stdout );
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
Paul Bakkered56b222011-07-13 11:26:43 +0000175
176 if( ret != 0 )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000179 goto exit;
180 }
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000183
184 /*
185 * 1.2 Print the key
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " . Key information ...\n" );
188#if defined(MBEDTLS_RSA_C)
189 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
192 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
193 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
194 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
195 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
196 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
197 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
198 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
199 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Paul Bakker15254952013-09-17 11:24:56 +0200200 }
201 else
202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_ECP_C)
204 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
207 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
208 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
209 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
210 mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
Paul Bakker15254952013-09-17 11:24:56 +0200211 }
212 else
213#endif
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200216 goto exit;
217 }
Paul Bakkered56b222011-07-13 11:26:43 +0000218 }
219 else if( opt.mode == MODE_PUBLIC )
220 {
221 /*
222 * 1.1. Load the key
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( "\n . Loading the public key ..." );
Paul Bakkered56b222011-07-13 11:26:43 +0000225 fflush( stdout );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
Paul Bakkered56b222011-07-13 11:26:43 +0000228
229 if( ret != 0 )
230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakkered56b222011-07-13 11:26:43 +0000232 goto exit;
233 }
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " ok\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " . Key information ...\n" );
238#if defined(MBEDTLS_RSA_C)
239 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
Paul Bakker15254952013-09-17 11:24:56 +0200240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
242 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
243 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
Paul Bakker15254952013-09-17 11:24:56 +0200244 }
245 else
246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_ECP_C)
248 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
Paul Bakker15254952013-09-17 11:24:56 +0200249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
251 mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
252 mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
253 mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
Paul Bakker15254952013-09-17 11:24:56 +0200254 }
255 else
256#endif
257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf("Do not know how to print key information for this type\n" );
Paul Bakker15254952013-09-17 11:24:56 +0200259 goto exit;
260 }
Paul Bakkered56b222011-07-13 11:26:43 +0000261 }
262 else
263 goto usage;
264
265exit:
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_ERROR_C)
268 mbedtls_strerror( ret, buf, sizeof(buf) );
269 mbedtls_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200270#endif
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_pk_free( &pk );
Paul Bakkered56b222011-07-13 11:26:43 +0000273
Paul Bakkercce9d772011-11-18 14:26:47 +0000274#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkered56b222011-07-13 11:26:43 +0000276 fflush( stdout ); getchar();
277#endif
278
279 return( ret );
280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */