key_app_writer.c 13 KB

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