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