1 /*
2 * Key reading application
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf printf
59 #define mbedtls_exit exit
60 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
61 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62 #endif /* MBEDTLS_PLATFORM_C */
63
64 #if defined(MBEDTLS_BIGNUM_C) && \
65 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
66 #include "mbedtls/error.h"
67 #include "mbedtls/rsa.h"
68 #include "mbedtls/x509.h"
69
70 #include <string.h>
71 #endif
72
73 #define MODE_NONE 0
74 #define MODE_PRIVATE 1
75 #define MODE_PUBLIC 2
76
77 #define DFL_MODE MODE_NONE
78 #define DFL_FILENAME "keyfile.key"
79 #define DFL_PASSWORD ""
80 #define DFL_PASSWORD_FILE ""
81 #define DFL_DEBUG_LEVEL 0
82
83 #define USAGE \
84 "\n usage: key_app param=<>...\n" \
85 "\n acceptable parameters:\n" \
86 " mode=private|public default: none\n" \
87 " filename=%%s default: keyfile.key\n" \
88 " password=%%s default: \"\"\n" \
89 " password_file=%%s default: \"\"\n" \
90 "\n"
91
92 #if !defined(MBEDTLS_BIGNUM_C) || \
93 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)94 int main( void )
95 {
96 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
97 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
98 mbedtls_exit( 0 );
99 }
100 #else
101
102
103 /*
104 * global options
105 */
106 struct options
107 {
108 int mode; /* the mode to run the application in */
109 const char *filename; /* filename of the key file */
110 const char *password; /* password for the private key */
111 const char *password_file; /* password_file for the private key */
112 } opt;
113
main(int argc,char * argv[])114 int main( int argc, char *argv[] )
115 {
116 int ret = 1;
117 int exit_code = MBEDTLS_EXIT_FAILURE;
118 char buf[1024];
119 int i;
120 char *p, *q;
121
122 mbedtls_pk_context pk;
123 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
124
125 /*
126 * Set to sane values
127 */
128 mbedtls_pk_init( &pk );
129 memset( buf, 0, sizeof(buf) );
130
131 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
132 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
133 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
134
135 if( argc == 0 )
136 {
137 usage:
138 mbedtls_printf( USAGE );
139 goto cleanup;
140 }
141
142 opt.mode = DFL_MODE;
143 opt.filename = DFL_FILENAME;
144 opt.password = DFL_PASSWORD;
145 opt.password_file = DFL_PASSWORD_FILE;
146
147 for( i = 1; i < argc; i++ )
148 {
149 p = argv[i];
150 if( ( q = strchr( p, '=' ) ) == NULL )
151 goto usage;
152 *q++ = '\0';
153
154 if( strcmp( p, "mode" ) == 0 )
155 {
156 if( strcmp( q, "private" ) == 0 )
157 opt.mode = MODE_PRIVATE;
158 else if( strcmp( q, "public" ) == 0 )
159 opt.mode = MODE_PUBLIC;
160 else
161 goto usage;
162 }
163 else if( strcmp( p, "filename" ) == 0 )
164 opt.filename = q;
165 else if( strcmp( p, "password" ) == 0 )
166 opt.password = q;
167 else if( strcmp( p, "password_file" ) == 0 )
168 opt.password_file = q;
169 else
170 goto usage;
171 }
172
173 if( opt.mode == MODE_PRIVATE )
174 {
175 if( strlen( opt.password ) && strlen( opt.password_file ) )
176 {
177 mbedtls_printf( "Error: cannot have both password and password_file\n" );
178 goto usage;
179 }
180
181 if( strlen( opt.password_file ) )
182 {
183 FILE *f;
184
185 mbedtls_printf( "\n . Loading the password file ..." );
186 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
187 {
188 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
189 goto cleanup;
190 }
191 if( fgets( buf, sizeof(buf), f ) == NULL )
192 {
193 fclose( f );
194 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
195 goto cleanup;
196 }
197 fclose( f );
198
199 i = (int) strlen( buf );
200 if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
201 if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
202 opt.password = buf;
203 }
204
205 /*
206 * 1.1. Load the key
207 */
208 mbedtls_printf( "\n . Loading the private key ..." );
209 fflush( stdout );
210
211 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
212
213 if( ret != 0 )
214 {
215 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
216 goto cleanup;
217 }
218
219 mbedtls_printf( " ok\n" );
220
221 /*
222 * 1.2 Print the key
223 */
224 mbedtls_printf( " . Key information ...\n" );
225 #if defined(MBEDTLS_RSA_C)
226 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
227 {
228 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
229
230 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
231 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
232 {
233 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
234 goto cleanup;
235 }
236
237 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
239 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
240 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
243 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
244 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
245 }
246 else
247 #endif
248 #if defined(MBEDTLS_ECP_C)
249 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
250 {
251 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
252 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
253 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
254 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
255 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
256 }
257 else
258 #endif
259 {
260 mbedtls_printf("Do not know how to print key information for this type\n" );
261 goto cleanup;
262 }
263 }
264 else if( opt.mode == MODE_PUBLIC )
265 {
266 /*
267 * 1.1. Load the key
268 */
269 mbedtls_printf( "\n . Loading the public key ..." );
270 fflush( stdout );
271
272 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
273
274 if( ret != 0 )
275 {
276 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
277 goto cleanup;
278 }
279
280 mbedtls_printf( " ok\n" );
281
282 mbedtls_printf( " . Key information ...\n" );
283 #if defined(MBEDTLS_RSA_C)
284 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
285 {
286 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
287
288 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
289 NULL, &E ) ) != 0 )
290 {
291 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
292 goto cleanup;
293 }
294 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
295 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
296 }
297 else
298 #endif
299 #if defined(MBEDTLS_ECP_C)
300 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
301 {
302 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
303 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
304 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
306 }
307 else
308 #endif
309 {
310 mbedtls_printf("Do not know how to print key information for this type\n" );
311 goto cleanup;
312 }
313 }
314 else
315 goto usage;
316
317 exit_code = MBEDTLS_EXIT_SUCCESS;
318
319 cleanup:
320
321 #if defined(MBEDTLS_ERROR_C)
322 if( exit_code != MBEDTLS_EXIT_SUCCESS )
323 {
324 mbedtls_strerror( ret, buf, sizeof( buf ) );
325 mbedtls_printf( " ! Last error was: %s\n", buf );
326 }
327 #endif
328
329 mbedtls_pk_free( &pk );
330 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
331 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
332 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
333
334 #if defined(_WIN32)
335 mbedtls_printf( " + Press Enter to exit this program.\n" );
336 fflush( stdout ); getchar();
337 #endif
338
339 mbedtls_exit( exit_code );
340 }
341 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
342