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