2 * Key reading application
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.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 */
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"
49 #define MODE_PRIVATE 1
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
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" \
68 #if !defined(MBEDTLS_BIGNUM_C) || \
69 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
72 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
73 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
78 #if defined(MBEDTLS_CHECK_PARAMS)
79 #include "mbedtls/platform_util.h"
80 void mbedtls_param_failed( const char *failure_condition,
84 mbedtls_printf( "%s:%i: Input param failed - %s\n",
85 file, line, failure_condition );
86 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
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 */
101 int main( int argc, char *argv[] )
104 int exit_code = MBEDTLS_EXIT_FAILURE;
109 mbedtls_pk_context pk;
110 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
115 mbedtls_pk_init( &pk );
116 memset( buf, 0, sizeof(buf) );
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 );
125 mbedtls_printf( USAGE );
130 opt.filename = DFL_FILENAME;
131 opt.password = DFL_PASSWORD;
132 opt.password_file = DFL_PASSWORD_FILE;
134 for( i = 1; i < argc; i++ )
137 if( ( q = strchr( p, '=' ) ) == NULL )
141 if( strcmp( p, "mode" ) == 0 )
143 if( strcmp( q, "private" ) == 0 )
144 opt.mode = MODE_PRIVATE;
145 else if( strcmp( q, "public" ) == 0 )
146 opt.mode = MODE_PUBLIC;
150 else if( strcmp( p, "filename" ) == 0 )
152 else if( strcmp( p, "password" ) == 0 )
154 else if( strcmp( p, "password_file" ) == 0 )
155 opt.password_file = q;
160 if( opt.mode == MODE_PRIVATE )
162 if( strlen( opt.password ) && strlen( opt.password_file ) )
164 mbedtls_printf( "Error: cannot have both password and password_file\n" );
168 if( strlen( opt.password_file ) )
172 mbedtls_printf( "\n . Loading the password file ..." );
173 if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
175 mbedtls_printf( " failed\n ! fopen returned NULL\n" );
178 if( fgets( buf, sizeof(buf), f ) == NULL )
181 mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
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';
195 mbedtls_printf( "\n . Loading the private key ..." );
198 ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
202 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
206 mbedtls_printf( " ok\n" );
211 mbedtls_printf( " . Key information ...\n" );
212 #if defined(MBEDTLS_RSA_C)
213 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
215 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
217 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
218 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
220 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
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 ) );
235 #if defined(MBEDTLS_ECP_C)
236 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
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 ) );
247 mbedtls_printf("Do not know how to print key information for this type\n" );
251 else if( opt.mode == MODE_PUBLIC )
256 mbedtls_printf( "\n . Loading the public key ..." );
259 ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
263 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
267 mbedtls_printf( " ok\n" );
269 mbedtls_printf( " . Key information ...\n" );
270 #if defined(MBEDTLS_RSA_C)
271 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
273 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
275 if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
278 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
281 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
282 MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
286 #if defined(MBEDTLS_ECP_C)
287 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
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 ) );
297 mbedtls_printf("Do not know how to print key information for this type\n" );
304 exit_code = MBEDTLS_EXIT_SUCCESS;
308 #if defined(MBEDTLS_ERROR_C)
309 if( exit_code != MBEDTLS_EXIT_SUCCESS )
311 mbedtls_strerror( ret, buf, sizeof( buf ) );
312 mbedtls_printf( " ! Last error was: %s\n", buf );
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 );
322 mbedtls_printf( " + Press Enter to exit this program.\n" );
323 fflush( stdout ); getchar();
328 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */