PageRenderTime 95ms CodeModel.GetById 19ms RepoModel.GetById 6ms app.codeStats 0ms

/Library/ssrNative/depends/mbedtls/programs/pkey/key_app.c

https://bitbucket.org/frodo_man/vvn.io
C | 308 lines | 235 code | 38 blank | 35 comment | 60 complexity | 8d147902067f889805c22a06f0c96210 MD5 | raw file
Possible License(s): Apache-2.0, 0BSD, ISC, GPL-3.0, CC-BY-SA-3.0, BSD-3-Clause, BSD-2-Clause, JSON, LGPL-3.0
  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. char buf[1024];
  78. int i;
  79. char *p, *q;
  80. mbedtls_pk_context pk;
  81. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  82. /*
  83. * Set to sane values
  84. */
  85. mbedtls_pk_init( &pk );
  86. memset( buf, 0, sizeof(buf) );
  87. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  88. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  89. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  90. if( argc == 0 )
  91. {
  92. usage:
  93. mbedtls_printf( USAGE );
  94. goto exit;
  95. }
  96. opt.mode = DFL_MODE;
  97. opt.filename = DFL_FILENAME;
  98. opt.password = DFL_PASSWORD;
  99. opt.password_file = DFL_PASSWORD_FILE;
  100. for( i = 1; i < argc; i++ )
  101. {
  102. p = argv[i];
  103. if( ( q = strchr( p, '=' ) ) == NULL )
  104. goto usage;
  105. *q++ = '\0';
  106. if( strcmp( p, "mode" ) == 0 )
  107. {
  108. if( strcmp( q, "private" ) == 0 )
  109. opt.mode = MODE_PRIVATE;
  110. else if( strcmp( q, "public" ) == 0 )
  111. opt.mode = MODE_PUBLIC;
  112. else
  113. goto usage;
  114. }
  115. else if( strcmp( p, "filename" ) == 0 )
  116. opt.filename = q;
  117. else if( strcmp( p, "password" ) == 0 )
  118. opt.password = q;
  119. else if( strcmp( p, "password_file" ) == 0 )
  120. opt.password_file = q;
  121. else
  122. goto usage;
  123. }
  124. if( opt.mode == MODE_PRIVATE )
  125. {
  126. if( strlen( opt.password ) && strlen( opt.password_file ) )
  127. {
  128. mbedtls_printf( "Error: cannot have both password and password_file\n" );
  129. goto usage;
  130. }
  131. if( strlen( opt.password_file ) )
  132. {
  133. FILE *f;
  134. mbedtls_printf( "\n . Loading the password file ..." );
  135. if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
  136. {
  137. mbedtls_printf( " failed\n ! fopen returned NULL\n" );
  138. goto exit;
  139. }
  140. if( fgets( buf, sizeof(buf), f ) == NULL )
  141. {
  142. fclose( f );
  143. mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
  144. goto exit;
  145. }
  146. fclose( f );
  147. i = (int) strlen( buf );
  148. if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
  149. if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
  150. opt.password = buf;
  151. }
  152. /*
  153. * 1.1. Load the key
  154. */
  155. mbedtls_printf( "\n . Loading the private key ..." );
  156. fflush( stdout );
  157. ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
  158. if( ret != 0 )
  159. {
  160. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
  161. goto exit;
  162. }
  163. mbedtls_printf( " ok\n" );
  164. /*
  165. * 1.2 Print the key
  166. */
  167. mbedtls_printf( " . Key information ...\n" );
  168. #if defined(MBEDTLS_RSA_C)
  169. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
  170. {
  171. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
  172. if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
  173. ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
  174. {
  175. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  176. goto exit;
  177. }
  178. mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
  179. mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
  180. mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
  181. mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
  182. mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
  183. mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
  184. mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
  185. mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
  186. }
  187. else
  188. #endif
  189. #if defined(MBEDTLS_ECP_C)
  190. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
  191. {
  192. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
  193. mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
  194. mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
  195. mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
  196. mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
  197. }
  198. else
  199. #endif
  200. {
  201. mbedtls_printf("Do not know how to print key information for this type\n" );
  202. goto exit;
  203. }
  204. }
  205. else if( opt.mode == MODE_PUBLIC )
  206. {
  207. /*
  208. * 1.1. Load the key
  209. */
  210. mbedtls_printf( "\n . Loading the public key ..." );
  211. fflush( stdout );
  212. ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
  213. if( ret != 0 )
  214. {
  215. mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
  216. goto exit;
  217. }
  218. mbedtls_printf( " ok\n" );
  219. mbedtls_printf( " . Key information ...\n" );
  220. #if defined(MBEDTLS_RSA_C)
  221. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
  222. {
  223. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
  224. if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
  225. NULL, &E ) ) != 0 )
  226. {
  227. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  228. goto exit;
  229. }
  230. mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
  231. mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
  232. }
  233. else
  234. #endif
  235. #if defined(MBEDTLS_ECP_C)
  236. if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
  237. {
  238. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
  239. mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
  240. mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
  241. mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
  242. }
  243. else
  244. #endif
  245. {
  246. mbedtls_printf("Do not know how to print key information for this type\n" );
  247. goto exit;
  248. }
  249. }
  250. else
  251. goto usage;
  252. exit:
  253. #if defined(MBEDTLS_ERROR_C)
  254. if( ret != 0 )
  255. {
  256. mbedtls_strerror( ret, buf, sizeof(buf) );
  257. mbedtls_printf( " ! Last error was: %s\n", buf );
  258. }
  259. #endif
  260. mbedtls_pk_free( &pk );
  261. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  262. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  263. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  264. #if defined(_WIN32)
  265. mbedtls_printf( " + Press Enter to exit this program.\n" );
  266. fflush( stdout ); getchar();
  267. #endif
  268. return( ret );
  269. }
  270. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */