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