key_app_writer.c 12 KB

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