key_app.c 9.8 KB

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