key_app_writer.c 12 KB

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