1 /*
2 * Key reading application
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * 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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #define mbedtls_printf printf
33 #endif
34
35 #if defined(MBEDTLS_BIGNUM_C) && \
36 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
37 #include "mbedtls/error.h"
38 #include "mbedtls/rsa.h"
39 #include "mbedtls/x509.h"
40
41 #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
53
54 #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
64 #if !defined(MBEDTLS_BIGNUM_C) || \
65 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)66 int main( void )
67 {
68 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
69 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
70 return( 0 );
71 }
72 #else
73 /*
74 * global options
75 */
76 struct options
77 {
78 int mode; /* the mode to run the application in */
79 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 */
82 } opt;
83
main(int argc,char * argv[])84 int main( int argc, char *argv[] )
85 {
86 int ret = 0;
87 mbedtls_pk_context pk;
88 char buf[1024];
89 int i;
90 char *p, *q;
91
92 /*
93 * Set to sane values
94 */
95 mbedtls_pk_init( &pk );
96 memset( buf, 0, sizeof(buf) );
97
98 if( argc == 0 )
99 {
100 usage:
101 mbedtls_printf( USAGE );
102 goto exit;
103 }
104
105 opt.mode = DFL_MODE;
106 opt.filename = DFL_FILENAME;
107 opt.password = DFL_PASSWORD;
108 opt.password_file = DFL_PASSWORD_FILE;
109
110 for( i = 1; i < argc; i++ )
111 {
112 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;
128 else if( strcmp( p, "password" ) == 0 )
129 opt.password = q;
130 else if( strcmp( p, "password_file" ) == 0 )
131 opt.password_file = q;
132 else
133 goto usage;
134 }
135
136 if( opt.mode == MODE_PRIVATE )
137 {
138 if( strlen( opt.password ) && strlen( opt.password_file ) )
139 {
140 mbedtls_printf( "Error: cannot have both password and password_file\n" );
141 goto usage;
142 }
143
144 if( strlen( opt.password_file ) )
145 {
146 FILE *f;
147
148 mbedtls_printf( "\n . Loading the password file ..." );
149 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
150 {
151 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
152 goto exit;
153 }
154 if( fgets( buf, sizeof(buf), f ) == NULL )
155 {
156 fclose( f );
157 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
158 goto exit;
159 }
160 fclose( f );
161
162 i = (int) strlen( buf );
163 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
168 /*
169 * 1.1. Load the key
170 */
171 mbedtls_printf( "\n . Loading the private key ..." );
172 fflush( stdout );
173
174 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
175
176 if( ret != 0 )
177 {
178 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
179 goto exit;
180 }
181
182 mbedtls_printf( " ok\n" );
183
184 /*
185 * 1.2 Print the key
186 */
187 mbedtls_printf( " . Key information ...\n" );
188 #if defined(MBEDTLS_RSA_C)
189 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
190 {
191 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 );
200 }
201 else
202 #endif
203 #if defined(MBEDTLS_ECP_C)
204 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
205 {
206 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 );
211 }
212 else
213 #endif
214 {
215 mbedtls_printf("Do not know how to print key information for this type\n" );
216 goto exit;
217 }
218 }
219 else if( opt.mode == MODE_PUBLIC )
220 {
221 /*
222 * 1.1. Load the key
223 */
224 mbedtls_printf( "\n . Loading the public key ..." );
225 fflush( stdout );
226
227 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
228
229 if( ret != 0 )
230 {
231 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
232 goto exit;
233 }
234
235 mbedtls_printf( " ok\n" );
236
237 mbedtls_printf( " . Key information ...\n" );
238 #if defined(MBEDTLS_RSA_C)
239 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
240 {
241 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 );
244 }
245 else
246 #endif
247 #if defined(MBEDTLS_ECP_C)
248 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
249 {
250 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 );
254 }
255 else
256 #endif
257 {
258 mbedtls_printf("Do not know how to print key information for this type\n" );
259 goto exit;
260 }
261 }
262 else
263 goto usage;
264
265 exit:
266
267 #if defined(MBEDTLS_ERROR_C)
268 mbedtls_strerror( ret, buf, sizeof(buf) );
269 mbedtls_printf( " ! Last error was: %s\n", buf );
270 #endif
271
272 mbedtls_pk_free( &pk );
273
274 #if defined(_WIN32)
275 mbedtls_printf( " + Press Enter to exit this program.\n" );
276 fflush( stdout ); getchar();
277 #endif
278
279 return( ret );
280 }
281 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
282