key_app.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Key reading 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_BIGNUM_C) && \
  35. defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
  36. #include "mbedtls/error.h"
  37. #include "mbedtls/rsa.h"
  38. #include "mbedtls/pk.h"
  39. #include <string.h>
  40. #endif
  41. #define MODE_NONE 0
  42. #define MODE_PRIVATE 1
  43. #define MODE_PUBLIC 2
  44. #define DFL_MODE MODE_NONE
  45. #define DFL_FILENAME "keyfile.key"
  46. #define DFL_PASSWORD ""
  47. #define DFL_PASSWORD_FILE ""
  48. #define DFL_DEBUG_LEVEL 0
  49. #define USAGE \
  50. "\n usage: key_app param=<>...\n" \
  51. "\n acceptable parameters:\n" \
  52. " mode=private|public default: none\n" \
  53. " filename=%%s default: keyfile.key\n" \
  54. " password=%%s default: \"\"\n" \
  55. " password_file=%%s default: \"\"\n" \
  56. "\n"
  57. #if !defined(MBEDTLS_BIGNUM_C) || \
  58. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
  59. int main( void )
  60. {
  61. mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
  62. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
  63. mbedtls_exit( 0 );
  64. }
  65. #else
  66. /*
  67. * global options
  68. */
  69. struct options
  70. {
  71. int mode; /* the mode to run the application in */
  72. const char *filename; /* filename of the key file */
  73. const char *password; /* password for the private key */
  74. const char *password_file; /* password_file for the private key */
  75. } opt;
  76. int main( int argc, char *argv[] )
  77. {
  78. int ret = 1;
  79. int exit_code = MBEDTLS_EXIT_FAILURE;
  80. char buf[1024];
  81. int i;
  82. char *p, *q;
  83. mbedtls_pk_context pk;
  84. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  85. /*
  86. * Set to sane values
  87. */
  88. mbedtls_pk_init( &pk );
  89. memset( buf, 0, sizeof(buf) );
  90. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  91. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  92. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  93. if( argc == 0 )
  94. {
  95. usage:
  96. mbedtls_printf( USAGE );
  97. goto cleanup;
  98. }
  99. opt.mode = DFL_MODE;
  100. opt.filename = DFL_FILENAME;
  101. opt.password = DFL_PASSWORD;
  102. opt.password_file = DFL_PASSWORD_FILE;
  103. for( i = 1; i < argc; i++ )
  104. {
  105. p = argv[i];
  106. if( ( q = strchr( p, '=' ) ) == NULL )
  107. goto usage;
  108. *q++ = '\0';
  109. if( strcmp( p, "mode" ) == 0 )
  110. {
  111. if( strcmp( q, "private" ) == 0 )
  112. opt.mode = MODE_PRIVATE;
  113. else if( strcmp( q, "public" ) == 0 )
  114. opt.mode = MODE_PUBLIC;
  115. else
  116. goto usage;
  117. }
  118. else if( strcmp( p, "filename" ) == 0 )
  119. opt.filename = q;
  120. else if( strcmp( p, "password" ) == 0 )
  121. opt.password = q;
  122. else if( strcmp( p, "password_file" ) == 0 )
  123. opt.password_file = q;
  124. else
  125. goto usage;
  126. }
  127. if( opt.mode == MODE_PRIVATE )
  128. {
  129. if( strlen( opt.password ) && strlen( opt.password_file ) )
  130. {
  131. mbedtls_printf( "Error: cannot have both password and password_file\n" );
  132. goto usage;
  133. }
  134. if( strlen( opt.password_file ) )
  135. {
  136. FILE *f;
  137. mbedtls_printf( "\n . Loading the password file ..." );
  138. if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
  139. {
  140. mbedtls_printf( " failed\n ! fopen returned NULL\n" );
  141. goto cleanup;
  142. }
  143. if( fgets( buf, sizeof(buf), f ) == NULL )
  144. {
  145. fclose( f );
  146. mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
  147. goto cleanup;
  148. }
  149. fclose( f );
  150. i = (int) strlen( buf );
  151. if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
  152. if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
  153. opt.password = buf;
  154. }
  155. /*
  156. * 1.1. Load the key
  157. */
  158. mbedtls_printf( "\n . Loading the private key ..." );
  159. fflush( stdout );
  160. ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
  161. if( ret != 0 )
  162. {
  163. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
  164. goto cleanup;
  165. }
  166. mbedtls_printf( " ok\n" );
  167. /*
  168. * 1.2 Print the key
  169. */
  170. mbedtls_printf( " . Key information ...\n" );
  171. #if defined(MBEDTLS_RSA_C)
  172. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
  173. {
  174. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
  175. if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
  176. ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
  177. {
  178. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  179. goto cleanup;
  180. }
  181. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
  182. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
  183. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
  184. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
  185. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
  186. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
  187. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
  188. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
  189. }
  190. else
  191. #endif
  192. #if defined(MBEDTLS_ECP_C)
  193. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
  194. {
  195. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
  196. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
  197. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
  198. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
  199. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
  200. }
  201. else
  202. #endif
  203. {
  204. mbedtls_printf("Do not know how to print key information for this type\n" );
  205. goto cleanup;
  206. }
  207. }
  208. else if( opt.mode == MODE_PUBLIC )
  209. {
  210. /*
  211. * 1.1. Load the key
  212. */
  213. mbedtls_printf( "\n . Loading the public key ..." );
  214. fflush( stdout );
  215. ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
  216. if( ret != 0 )
  217. {
  218. mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
  219. goto cleanup;
  220. }
  221. mbedtls_printf( " ok\n" );
  222. mbedtls_printf( " . Key information ...\n" );
  223. #if defined(MBEDTLS_RSA_C)
  224. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
  225. {
  226. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
  227. if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
  228. NULL, &E ) ) != 0 )
  229. {
  230. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  231. goto cleanup;
  232. }
  233. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
  234. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
  235. }
  236. else
  237. #endif
  238. #if defined(MBEDTLS_ECP_C)
  239. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
  240. {
  241. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
  242. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
  243. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
  244. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
  245. }
  246. else
  247. #endif
  248. {
  249. mbedtls_printf("Do not know how to print key information for this type\n" );
  250. goto cleanup;
  251. }
  252. }
  253. else
  254. goto usage;
  255. exit_code = MBEDTLS_EXIT_SUCCESS;
  256. cleanup:
  257. #if defined(MBEDTLS_ERROR_C)
  258. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  259. {
  260. mbedtls_strerror( ret, buf, sizeof( buf ) );
  261. mbedtls_printf( " ! Last error was: %s\n", buf );
  262. }
  263. #endif
  264. mbedtls_pk_free( &pk );
  265. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  266. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  267. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  268. #if defined(_WIN32)
  269. mbedtls_printf( " + Press Enter to exit this program.\n" );
  270. fflush( stdout ); getchar();
  271. #endif
  272. mbedtls_exit( exit_code );
  273. }
  274. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */