key_app.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Key reading application
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #ifdef PRINTF_STDLIB
  31. #include <stdio.h>
  32. #endif
  33. #ifdef PRINTF_CUSTOM
  34. #include "tinystdio.h"
  35. #endif
  36. #include "polarssl/config.h"
  37. #include "polarssl/error.h"
  38. #include "polarssl/rsa.h"
  39. #include "polarssl/x509.h"
  40. #define MODE_NONE 0
  41. #define MODE_PRIVATE 1
  42. #define MODE_PUBLIC 2
  43. #define DFL_MODE MODE_NONE
  44. #define DFL_FILENAME "keyfile.key"
  45. #define DFL_DEBUG_LEVEL 0
  46. /*
  47. * global options
  48. */
  49. struct options
  50. {
  51. int mode; /* the mode to run the application in */
  52. char *filename; /* filename of the key file */
  53. int debug_level; /* level of debugging */
  54. } opt;
  55. void my_debug( void *ctx, int level, const char *str )
  56. {
  57. if( level < opt.debug_level )
  58. {
  59. fprintf( (FILE *) ctx, "%s", str );
  60. fflush( (FILE *) ctx );
  61. }
  62. }
  63. #define USAGE \
  64. "\n usage: key_app param=<>...\n" \
  65. "\n acceptable parameters:\n" \
  66. " mode=private|public default: none\n" \
  67. " filename=%%s default: keyfile.key\n" \
  68. " debug_level=%%d default: 0 (disabled)\n" \
  69. "\n"
  70. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  71. !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
  72. int main( void )
  73. {
  74. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  75. "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
  76. return( 0 );
  77. }
  78. #else
  79. int main( int argc, char *argv[] )
  80. {
  81. int ret = 0;
  82. rsa_context rsa;
  83. char buf[1024];
  84. int i, j, n;
  85. char *p, *q;
  86. /*
  87. * Set to sane values
  88. */
  89. memset( &rsa, 0, sizeof( rsa_context ) );
  90. memset( buf, 0, 1024 );
  91. if( argc == 0 )
  92. {
  93. usage:
  94. printf( USAGE );
  95. goto exit;
  96. }
  97. opt.mode = DFL_MODE;
  98. opt.filename = DFL_FILENAME;
  99. opt.debug_level = DFL_DEBUG_LEVEL;
  100. for( i = 1; i < argc; i++ )
  101. {
  102. n = strlen( argv[i] );
  103. for( j = 0; j < n; j++ )
  104. {
  105. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  106. argv[i][j] |= 0x20;
  107. }
  108. p = argv[i];
  109. if( ( q = strchr( p, '=' ) ) == NULL )
  110. goto usage;
  111. *q++ = '\0';
  112. if( strcmp( p, "mode" ) == 0 )
  113. {
  114. if( strcmp( q, "private" ) == 0 )
  115. opt.mode = MODE_PRIVATE;
  116. else if( strcmp( q, "public" ) == 0 )
  117. opt.mode = MODE_PUBLIC;
  118. else
  119. goto usage;
  120. }
  121. else if( strcmp( p, "filename" ) == 0 )
  122. opt.filename = q;
  123. else if( strcmp( p, "debug_level" ) == 0 )
  124. {
  125. opt.debug_level = atoi( q );
  126. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  127. goto usage;
  128. }
  129. else
  130. goto usage;
  131. }
  132. if( opt.mode == MODE_PRIVATE )
  133. {
  134. /*
  135. * 1.1. Load the key
  136. */
  137. printf( "\n . Loading the private key ..." );
  138. fflush( stdout );
  139. ret = x509parse_keyfile( &rsa, opt.filename, NULL );
  140. if( ret != 0 )
  141. {
  142. #ifdef POLARSSL_ERROR_C
  143. error_strerror( ret, buf, 1024 );
  144. #endif
  145. printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
  146. rsa_free( &rsa );
  147. goto exit;
  148. }
  149. printf( " ok\n" );
  150. /*
  151. * 1.2 Print the key
  152. */
  153. printf( " . Key information ...\n" );
  154. mpi_write_file( "N: ", &rsa.N, 16, NULL );
  155. mpi_write_file( "E: ", &rsa.E, 16, NULL );
  156. mpi_write_file( "D: ", &rsa.D, 16, NULL );
  157. mpi_write_file( "P: ", &rsa.P, 16, NULL );
  158. mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
  159. mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
  160. mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
  161. mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
  162. }
  163. else if( opt.mode == MODE_PUBLIC )
  164. {
  165. /*
  166. * 1.1. Load the key
  167. */
  168. printf( "\n . Loading the public key ..." );
  169. fflush( stdout );
  170. ret = x509parse_public_keyfile( &rsa, opt.filename );
  171. if( ret != 0 )
  172. {
  173. #ifdef POLARSSL_ERROR_C
  174. error_strerror( ret, buf, 1024 );
  175. #endif
  176. printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
  177. rsa_free( &rsa );
  178. goto exit;
  179. }
  180. printf( " ok\n" );
  181. /*
  182. * 1.2 Print the key
  183. */
  184. printf( " . Key information ...\n" );
  185. mpi_write_file( "N: ", &rsa.N, 16, NULL );
  186. mpi_write_file( "E: ", &rsa.E, 16, NULL );
  187. }
  188. else
  189. goto usage;
  190. exit:
  191. rsa_free( &rsa );
  192. #ifdef WIN32
  193. printf( " + Press Enter to exit this program.\n" );
  194. fflush( stdout ); getchar();
  195. #endif
  196. return( ret );
  197. }
  198. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
  199. POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */