key_app.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <stdio.h>
  31. #include "polarssl/config.h"
  32. #include "polarssl/error.h"
  33. #include "polarssl/rsa.h"
  34. #include "polarssl/x509.h"
  35. #define MODE_NONE 0
  36. #define MODE_PRIVATE 1
  37. #define MODE_PUBLIC 2
  38. #define DFL_MODE MODE_NONE
  39. #define DFL_FILENAME "keyfile.key"
  40. #define DFL_DEBUG_LEVEL 0
  41. /*
  42. * global options
  43. */
  44. struct options
  45. {
  46. int mode; /* the mode to run the application in */
  47. char *filename; /* filename of the key file */
  48. int debug_level; /* level of debugging */
  49. } opt;
  50. void my_debug( void *ctx, int level, const char *str )
  51. {
  52. if( level < opt.debug_level )
  53. {
  54. fprintf( (FILE *) ctx, "%s", str );
  55. fflush( (FILE *) ctx );
  56. }
  57. }
  58. #define USAGE \
  59. "\n usage: key_app param=<>...\n" \
  60. "\n acceptable parameters:\n" \
  61. " mode=private|public default: none\n" \
  62. " filename=%%s default: keyfile.key\n" \
  63. " debug_level=%%d default: 0 (disabled)\n" \
  64. "\n"
  65. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  66. !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
  67. int main( void )
  68. {
  69. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  70. "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
  71. return( 0 );
  72. }
  73. #else
  74. int main( int argc, char *argv[] )
  75. {
  76. int ret = 0;
  77. rsa_context rsa;
  78. char buf[1024];
  79. int i, j, n;
  80. char *p, *q;
  81. /*
  82. * Set to sane values
  83. */
  84. memset( &rsa, 0, sizeof( rsa_context ) );
  85. memset( buf, 0, 1024 );
  86. if( argc == 0 )
  87. {
  88. usage:
  89. printf( USAGE );
  90. goto exit;
  91. }
  92. opt.mode = DFL_MODE;
  93. opt.filename = DFL_FILENAME;
  94. opt.debug_level = DFL_DEBUG_LEVEL;
  95. for( i = 1; i < argc; i++ )
  96. {
  97. n = strlen( argv[i] );
  98. for( j = 0; j < n; j++ )
  99. {
  100. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  101. argv[i][j] |= 0x20;
  102. }
  103. p = argv[i];
  104. if( ( q = strchr( p, '=' ) ) == NULL )
  105. goto usage;
  106. *q++ = '\0';
  107. if( strcmp( p, "mode" ) == 0 )
  108. {
  109. if( strcmp( q, "private" ) == 0 )
  110. opt.mode = MODE_PRIVATE;
  111. else if( strcmp( q, "public" ) == 0 )
  112. opt.mode = MODE_PUBLIC;
  113. else
  114. goto usage;
  115. }
  116. else if( strcmp( p, "filename" ) == 0 )
  117. opt.filename = q;
  118. else if( strcmp( p, "debug_level" ) == 0 )
  119. {
  120. opt.debug_level = atoi( q );
  121. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  122. goto usage;
  123. }
  124. else
  125. goto usage;
  126. }
  127. if( opt.mode == MODE_PRIVATE )
  128. {
  129. /*
  130. * 1.1. Load the key
  131. */
  132. printf( "\n . Loading the private key ..." );
  133. fflush( stdout );
  134. ret = x509parse_keyfile( &rsa, opt.filename, NULL );
  135. if( ret != 0 )
  136. {
  137. #ifdef POLARSSL_ERROR_C
  138. error_strerror( ret, buf, 1024 );
  139. #endif
  140. printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf );
  141. rsa_free( &rsa );
  142. goto exit;
  143. }
  144. printf( " ok\n" );
  145. /*
  146. * 1.2 Print the key
  147. */
  148. printf( " . Key information ...\n" );
  149. mpi_write_file( "N: ", &rsa.N, 16, NULL );
  150. mpi_write_file( "E: ", &rsa.E, 16, NULL );
  151. mpi_write_file( "D: ", &rsa.D, 16, NULL );
  152. mpi_write_file( "P: ", &rsa.P, 16, NULL );
  153. mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
  154. mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
  155. mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
  156. mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
  157. }
  158. else if( opt.mode == MODE_PUBLIC )
  159. {
  160. /*
  161. * 1.1. Load the key
  162. */
  163. printf( "\n . Loading the public key ..." );
  164. fflush( stdout );
  165. ret = x509parse_public_keyfile( &rsa, opt.filename );
  166. if( ret != 0 )
  167. {
  168. #ifdef POLARSSL_ERROR_C
  169. error_strerror( ret, buf, 1024 );
  170. #endif
  171. printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf );
  172. rsa_free( &rsa );
  173. goto exit;
  174. }
  175. printf( " ok\n" );
  176. /*
  177. * 1.2 Print the key
  178. */
  179. printf( " . Key information ...\n" );
  180. mpi_write_file( "N: ", &rsa.N, 16, NULL );
  181. mpi_write_file( "E: ", &rsa.E, 16, NULL );
  182. }
  183. else
  184. goto usage;
  185. exit:
  186. rsa_free( &rsa );
  187. #ifdef WIN32
  188. printf( " + Press Enter to exit this program.\n" );
  189. fflush( stdout ); getchar();
  190. #endif
  191. return( ret );
  192. }
  193. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
  194. POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */