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 #include <stdlib.h>
33 #define mbedtls_printf printf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
38
39 #if defined(MBEDTLS_BIGNUM_C) && \
40 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/error.h"
42 #include "mbedtls/rsa.h"
43 #include "mbedtls/x509.h"
44
45 #include <string.h>
46 #endif
47
48 #define MODE_NONE 0
49 #define MODE_PRIVATE 1
50 #define MODE_PUBLIC 2
51
52 #define DFL_MODE MODE_NONE
53 #define DFL_FILENAME "keyfile.key"
54 #define DFL_PASSWORD ""
55 #define DFL_PASSWORD_FILE ""
56 #define DFL_DEBUG_LEVEL 0
57
58 #define USAGE \
59 "\n usage: key_app param=<>...\n" \
60 "\n acceptable parameters:\n" \
61 " mode=private|public default: none\n" \
62 " filename=%%s default: keyfile.key\n" \
63 " password=%%s default: \"\"\n" \
64 " password_file=%%s default: \"\"\n" \
65 "\n"
66
67
68 #if !defined(MBEDTLS_BIGNUM_C) || \
69 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)70 int main( void )
71 {
72 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
73 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
74 return( 0 );
75 }
76 #else
77
78 #if defined(MBEDTLS_CHECK_PARAMS)
79 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)80 void mbedtls_param_failed( const char *failure_condition,
81 const char *file,
82 int line )
83 {
84 mbedtls_printf( "%s:%i: Input param failed - %s\n",
85 file, line, failure_condition );
86 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
87 }
88 #endif
89
90 /*
91 * global options
92 */
93 struct options
94 {
95 int mode; /* the mode to run the application in */
96 const char *filename; /* filename of the key file */
97 const char *password; /* password for the private key */
98 const char *password_file; /* password_file for the private key */
99 } opt;
100
main(int argc,char * argv[])101 int main( int argc, char *argv[] )
102 {
103 int ret = 1;
104 int exit_code = MBEDTLS_EXIT_FAILURE;
105 char buf[1024];
106 int i;
107 char *p, *q;
108
109 mbedtls_pk_context pk;
110 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
111
112 /*
113 * Set to sane values
114 */
115 mbedtls_pk_init( &pk );
116 memset( buf, 0, sizeof(buf) );
117
118 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
119 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
120 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
121
122 if( argc == 0 )
123 {
124 usage:
125 mbedtls_printf( USAGE );
126 goto cleanup;
127 }
128
129 opt.mode = DFL_MODE;
130 opt.filename = DFL_FILENAME;
131 opt.password = DFL_PASSWORD;
132 opt.password_file = DFL_PASSWORD_FILE;
133
134 for( i = 1; i < argc; i++ )
135 {
136 p = argv[i];
137 if( ( q = strchr( p, '=' ) ) == NULL )
138 goto usage;
139 *q++ = '\0';
140
141 if( strcmp( p, "mode" ) == 0 )
142 {
143 if( strcmp( q, "private" ) == 0 )
144 opt.mode = MODE_PRIVATE;
145 else if( strcmp( q, "public" ) == 0 )
146 opt.mode = MODE_PUBLIC;
147 else
148 goto usage;
149 }
150 else if( strcmp( p, "filename" ) == 0 )
151 opt.filename = q;
152 else if( strcmp( p, "password" ) == 0 )
153 opt.password = q;
154 else if( strcmp( p, "password_file" ) == 0 )
155 opt.password_file = q;
156 else
157 goto usage;
158 }
159
160 if( opt.mode == MODE_PRIVATE )
161 {
162 if( strlen( opt.password ) && strlen( opt.password_file ) )
163 {
164 mbedtls_printf( "Error: cannot have both password and password_file\n" );
165 goto usage;
166 }
167
168 if( strlen( opt.password_file ) )
169 {
170 FILE *f;
171
172 mbedtls_printf( "\n . Loading the password file ..." );
173 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
174 {
175 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
176 goto cleanup;
177 }
178 if( fgets( buf, sizeof(buf), f ) == NULL )
179 {
180 fclose( f );
181 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
182 goto cleanup;
183 }
184 fclose( f );
185
186 i = (int) strlen( buf );
187 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
188 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
189 opt.password = buf;
190 }
191
192 /*
193 * 1.1. Load the key
194 */
195 mbedtls_printf( "\n . Loading the private key ..." );
196 fflush( stdout );
197
198 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
199
200 if( ret != 0 )
201 {
202 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
203 goto cleanup;
204 }
205
206 mbedtls_printf( " ok\n" );
207
208 /*
209 * 1.2 Print the key
210 */
211 mbedtls_printf( " . Key information ...\n" );
212 #if defined(MBEDTLS_RSA_C)
213 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
214 {
215 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
216
217 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
218 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
219 {
220 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
221 goto cleanup;
222 }
223
224 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
232 }
233 else
234 #endif
235 #if defined(MBEDTLS_ECP_C)
236 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
237 {
238 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
243 }
244 else
245 #endif
246 {
247 mbedtls_printf("Do not know how to print key information for this type\n" );
248 goto cleanup;
249 }
250 }
251 else if( opt.mode == MODE_PUBLIC )
252 {
253 /*
254 * 1.1. Load the key
255 */
256 mbedtls_printf( "\n . Loading the public key ..." );
257 fflush( stdout );
258
259 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
260
261 if( ret != 0 )
262 {
263 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
264 goto cleanup;
265 }
266
267 mbedtls_printf( " ok\n" );
268
269 mbedtls_printf( " . Key information ...\n" );
270 #if defined(MBEDTLS_RSA_C)
271 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
272 {
273 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
274
275 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
276 NULL, &E ) ) != 0 )
277 {
278 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
279 goto cleanup;
280 }
281 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
282 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
283 }
284 else
285 #endif
286 #if defined(MBEDTLS_ECP_C)
287 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
288 {
289 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
290 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
291 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
292 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
293 }
294 else
295 #endif
296 {
297 mbedtls_printf("Do not know how to print key information for this type\n" );
298 goto cleanup;
299 }
300 }
301 else
302 goto usage;
303
304 exit_code = MBEDTLS_EXIT_SUCCESS;
305
306 cleanup:
307
308 #if defined(MBEDTLS_ERROR_C)
309 if( exit_code != MBEDTLS_EXIT_SUCCESS )
310 {
311 mbedtls_strerror( ret, buf, sizeof( buf ) );
312 mbedtls_printf( " ! Last error was: %s\n", buf );
313 }
314 #endif
315
316 mbedtls_pk_free( &pk );
317 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
318 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
319 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
320
321 #if defined(_WIN32)
322 mbedtls_printf( " + Press Enter to exit this program.\n" );
323 fflush( stdout ); getchar();
324 #endif
325
326 return( exit_code );
327 }
328 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
329