1 /*
2  *  Key writing 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_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
40 #include "mbedtls/error.h"
41 #include "mbedtls/pk.h"
42 #include "mbedtls/error.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 #endif
47 
48 #if defined(MBEDTLS_PEM_WRITE_C)
49 #define USAGE_OUT \
50     "    output_file=%%s      default: keyfile.pem\n"   \
51     "    output_format=pem|der default: pem\n"
52 #else
53 #define USAGE_OUT \
54     "    output_file=%%s      default: keyfile.der\n"   \
55     "    output_format=der     default: der\n"
56 #endif
57 
58 #if defined(MBEDTLS_PEM_WRITE_C)
59 #define DFL_OUTPUT_FILENAME     "keyfile.pem"
60 #define DFL_OUTPUT_FORMAT       OUTPUT_FORMAT_PEM
61 #else
62 #define DFL_OUTPUT_FILENAME     "keyfile.der"
63 #define DFL_OUTPUT_FORMAT       OUTPUT_FORMAT_DER
64 #endif
65 
66 #define DFL_MODE                MODE_NONE
67 #define DFL_FILENAME            "keyfile.key"
68 #define DFL_DEBUG_LEVEL         0
69 #define DFL_OUTPUT_MODE         OUTPUT_MODE_NONE
70 
71 #define MODE_NONE               0
72 #define MODE_PRIVATE            1
73 #define MODE_PUBLIC             2
74 
75 #define OUTPUT_MODE_NONE               0
76 #define OUTPUT_MODE_PRIVATE            1
77 #define OUTPUT_MODE_PUBLIC             2
78 
79 #define OUTPUT_FORMAT_PEM              0
80 #define OUTPUT_FORMAT_DER              1
81 
82 #define USAGE \
83     "\n usage: key_app_writer param=<>...\n"            \
84     "\n acceptable parameters:\n"                       \
85     "    mode=private|public default: none\n"           \
86     "    filename=%%s         default: keyfile.key\n"   \
87     "    output_mode=private|public default: none\n"    \
88     USAGE_OUT                                           \
89     "\n"
90 
91 #if !defined(MBEDTLS_PK_PARSE_C) || \
92     !defined(MBEDTLS_PK_WRITE_C) || \
93     !defined(MBEDTLS_FS_IO)
main(void)94 int main( void )
95 {
96     mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
97     return( 0 );
98 }
99 #else
100 
101 #if defined(MBEDTLS_CHECK_PARAMS)
102 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)103 void mbedtls_param_failed( const char *failure_condition,
104                            const char *file,
105                            int line )
106 {
107     mbedtls_printf( "%s:%i: Input param failed - %s\n",
108                     file, line, failure_condition );
109     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
110 }
111 #endif
112 
113 /*
114  * global options
115  */
116 struct options
117 {
118     int mode;                   /* the mode to run the application in   */
119     const char *filename;       /* filename of the key file             */
120     int output_mode;            /* the output mode to use               */
121     const char *output_file;    /* where to store the constructed key file  */
122     int output_format;          /* the output format to use             */
123 } opt;
124 
write_public_key(mbedtls_pk_context * key,const char * output_file)125 static int write_public_key( mbedtls_pk_context *key, const char *output_file )
126 {
127     int ret;
128     FILE *f;
129     unsigned char output_buf[16000];
130     unsigned char *c = output_buf;
131     size_t len = 0;
132 
133     memset(output_buf, 0, 16000);
134 
135 #if defined(MBEDTLS_PEM_WRITE_C)
136     if( opt.output_format == OUTPUT_FORMAT_PEM )
137     {
138         if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
139             return( ret );
140 
141         len = strlen( (char *) output_buf );
142     }
143     else
144 #endif
145     {
146         if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
147             return( ret );
148 
149         len = ret;
150         c = output_buf + sizeof(output_buf) - len;
151     }
152 
153     if( ( f = fopen( output_file, "w" ) ) == NULL )
154         return( -1 );
155 
156     if( fwrite( c, 1, len, f ) != len )
157     {
158         fclose( f );
159         return( -1 );
160     }
161 
162     fclose( f );
163 
164     return( 0 );
165 }
166 
write_private_key(mbedtls_pk_context * key,const char * output_file)167 static int write_private_key( mbedtls_pk_context *key, const char *output_file )
168 {
169     int ret;
170     FILE *f;
171     unsigned char output_buf[16000];
172     unsigned char *c = output_buf;
173     size_t len = 0;
174 
175     memset(output_buf, 0, 16000);
176 
177 #if defined(MBEDTLS_PEM_WRITE_C)
178     if( opt.output_format == OUTPUT_FORMAT_PEM )
179     {
180         if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
181             return( ret );
182 
183         len = strlen( (char *) output_buf );
184     }
185     else
186 #endif
187     {
188         if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
189             return( ret );
190 
191         len = ret;
192         c = output_buf + sizeof(output_buf) - len - 1;
193     }
194 
195     if( ( f = fopen( output_file, "w" ) ) == NULL )
196         return( -1 );
197 
198     if( fwrite( c, 1, len, f ) != len )
199     {
200         fclose( f );
201         return( -1 );
202     }
203 
204     fclose( f );
205 
206     return( 0 );
207 }
208 
main(int argc,char * argv[])209 int main( int argc, char *argv[] )
210 {
211     int ret = 1;
212     int exit_code = MBEDTLS_EXIT_FAILURE;
213     char buf[1024];
214     int i;
215     char *p, *q;
216 
217     mbedtls_pk_context key;
218     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
219 
220     /*
221      * Set to sane values
222      */
223     mbedtls_pk_init( &key );
224     memset( buf, 0, sizeof( buf ) );
225 
226     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
227     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
228     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
229 
230     if( argc == 0 )
231     {
232     usage:
233         mbedtls_printf( USAGE );
234         goto exit;
235     }
236 
237     opt.mode                = DFL_MODE;
238     opt.filename            = DFL_FILENAME;
239     opt.output_mode         = DFL_OUTPUT_MODE;
240     opt.output_file         = DFL_OUTPUT_FILENAME;
241     opt.output_format       = DFL_OUTPUT_FORMAT;
242 
243     for( i = 1; i < argc; i++ )
244     {
245         p = argv[i];
246         if( ( q = strchr( p, '=' ) ) == NULL )
247             goto usage;
248         *q++ = '\0';
249 
250         if( strcmp( p, "mode" ) == 0 )
251         {
252             if( strcmp( q, "private" ) == 0 )
253                 opt.mode = MODE_PRIVATE;
254             else if( strcmp( q, "public" ) == 0 )
255                 opt.mode = MODE_PUBLIC;
256             else
257                 goto usage;
258         }
259         else if( strcmp( p, "output_mode" ) == 0 )
260         {
261             if( strcmp( q, "private" ) == 0 )
262                 opt.output_mode = OUTPUT_MODE_PRIVATE;
263             else if( strcmp( q, "public" ) == 0 )
264                 opt.output_mode = OUTPUT_MODE_PUBLIC;
265             else
266                 goto usage;
267         }
268         else if( strcmp( p, "output_format" ) == 0 )
269         {
270 #if defined(MBEDTLS_PEM_WRITE_C)
271             if( strcmp( q, "pem" ) == 0 )
272                 opt.output_format = OUTPUT_FORMAT_PEM;
273             else
274 #endif
275             if( strcmp( q, "der" ) == 0 )
276                 opt.output_format = OUTPUT_FORMAT_DER;
277             else
278                 goto usage;
279         }
280         else if( strcmp( p, "filename" ) == 0 )
281             opt.filename = q;
282         else if( strcmp( p, "output_file" ) == 0 )
283             opt.output_file = q;
284         else
285             goto usage;
286     }
287 
288     if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
289     {
290         mbedtls_printf( "\nCannot output a key without reading one.\n");
291         goto exit;
292     }
293 
294     if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
295     {
296         mbedtls_printf( "\nCannot output a private key from a public key.\n");
297         goto exit;
298     }
299 
300     if( opt.mode == MODE_PRIVATE )
301     {
302         /*
303          * 1.1. Load the key
304          */
305         mbedtls_printf( "\n  . Loading the private key ..." );
306         fflush( stdout );
307 
308         ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
309 
310         if( ret != 0 )
311         {
312             mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
313             mbedtls_printf( " failed\n  !  mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
314             goto exit;
315         }
316 
317         mbedtls_printf( " ok\n" );
318 
319         /*
320          * 1.2 Print the key
321          */
322         mbedtls_printf( "  . Key information    ...\n" );
323 
324 #if defined(MBEDTLS_RSA_C)
325         if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
326         {
327             mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
328 
329             if( ( ret = mbedtls_rsa_export    ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
330                 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) )      != 0 )
331             {
332                 mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" );
333                 goto exit;
334             }
335 
336             mbedtls_mpi_write_file( "N:  ",  &N,  16, NULL );
337             mbedtls_mpi_write_file( "E:  ",  &E,  16, NULL );
338             mbedtls_mpi_write_file( "D:  ",  &D,  16, NULL );
339             mbedtls_mpi_write_file( "P:  ",  &P,  16, NULL );
340             mbedtls_mpi_write_file( "Q:  ",  &Q,  16, NULL );
341             mbedtls_mpi_write_file( "DP: ",  &DP, 16, NULL );
342             mbedtls_mpi_write_file( "DQ:  ", &DQ, 16, NULL );
343             mbedtls_mpi_write_file( "QP:  ", &QP, 16, NULL );
344         }
345         else
346 #endif
347 #if defined(MBEDTLS_ECP_C)
348         if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
349         {
350             mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
351             mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
352             mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
353             mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
354             mbedtls_mpi_write_file( "D   : ", &ecp->d  , 16, NULL );
355         }
356         else
357 #endif
358             mbedtls_printf("key type not supported yet\n");
359 
360     }
361     else if( opt.mode == MODE_PUBLIC )
362     {
363         /*
364          * 1.1. Load the key
365          */
366         mbedtls_printf( "\n  . Loading the public key ..." );
367         fflush( stdout );
368 
369         ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
370 
371         if( ret != 0 )
372         {
373             mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
374             mbedtls_printf( " failed\n  !  mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
375             goto exit;
376         }
377 
378         mbedtls_printf( " ok\n" );
379 
380         /*
381          * 1.2 Print the key
382          */
383         mbedtls_printf( "  . Key information    ...\n" );
384 
385 #if defined(MBEDTLS_RSA_C)
386         if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
387         {
388             mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
389 
390             if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
391                                             NULL, &E ) ) != 0 )
392             {
393                 mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" );
394                 goto exit;
395             }
396             mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
397             mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
398         }
399         else
400 #endif
401 #if defined(MBEDTLS_ECP_C)
402         if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
403         {
404             mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
405             mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
406             mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
407             mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
408         }
409         else
410 #endif
411             mbedtls_printf("key type not supported yet\n");
412     }
413     else
414         goto usage;
415 
416     if( opt.output_mode == OUTPUT_MODE_PUBLIC )
417     {
418         write_public_key( &key, opt.output_file );
419     }
420     if( opt.output_mode == OUTPUT_MODE_PRIVATE )
421     {
422         write_private_key( &key, opt.output_file );
423     }
424 
425     exit_code = MBEDTLS_EXIT_SUCCESS;
426 
427 exit:
428 
429     if( exit_code != MBEDTLS_EXIT_SUCCESS )
430     {
431 #ifdef MBEDTLS_ERROR_C
432         mbedtls_strerror( ret, buf, sizeof( buf ) );
433         mbedtls_printf( " - %s\n", buf );
434 #else
435         mbedtls_printf("\n");
436 #endif
437     }
438 
439     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
440     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
441     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
442 
443     mbedtls_pk_free( &key );
444 
445 #if defined(_WIN32)
446     mbedtls_printf( "  + Press Enter to exit this program.\n" );
447     fflush( stdout ); getchar();
448 #endif
449 
450     return( exit_code );
451 }
452 #endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
453