key_app.c 8.2 KB

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