cert_app.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Certificate 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/havege.h"
  33. #include "polarssl/net.h"
  34. #include "polarssl/ssl.h"
  35. #include "polarssl/x509.h"
  36. #define MODE_NONE 0
  37. #define MODE_FILE 1
  38. #define MODE_SSL 2
  39. #define DFL_MODE MODE_NONE
  40. #define DFL_FILENAME "cert.crt"
  41. #define DFL_SERVER_NAME "localhost"
  42. #define DFL_SERVER_PORT 4433
  43. #define DFL_DEBUG_LEVEL 0
  44. /*
  45. * global options
  46. */
  47. struct options
  48. {
  49. int mode; /* the mode to run the application in */
  50. char *filename; /* filename of the certificate file */
  51. char *server_name; /* hostname of the server (client only) */
  52. int server_port; /* port on which the ssl service runs */
  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: cert_app param=<>...\n" \
  65. "\n acceptable parameters:\n" \
  66. " mode=file|ssl default: none\n" \
  67. " filename=%%s default: cert.crt\n" \
  68. " server_name=%%s default: localhost\n" \
  69. " server_port=%%d default: 4433\n" \
  70. " debug_level=%%d default: 0 (disabled)\n" \
  71. "\n"
  72. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  73. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
  74. !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
  75. !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
  76. int main( void )
  77. {
  78. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  79. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
  80. "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
  81. "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
  82. return( 0 );
  83. }
  84. #else
  85. int main( int argc, char *argv[] )
  86. {
  87. int ret = 0, server_fd;
  88. unsigned char buf[1024];
  89. havege_state hs;
  90. ssl_context ssl;
  91. ssl_session ssn;
  92. x509_cert clicert;
  93. rsa_context rsa;
  94. int i, j, n;
  95. char *p, *q;
  96. /*
  97. * Set to sane values
  98. */
  99. server_fd = 0;
  100. memset( &ssl, 0, sizeof( ssl_context ) );
  101. memset( &ssn, 0, sizeof( ssl_session ) );
  102. memset( &clicert, 0, sizeof( x509_cert ) );
  103. memset( &rsa, 0, sizeof( rsa_context ) );
  104. if( argc == 0 )
  105. {
  106. usage:
  107. printf( USAGE );
  108. goto exit;
  109. }
  110. opt.mode = DFL_MODE;
  111. opt.filename = DFL_FILENAME;
  112. opt.server_name = DFL_SERVER_NAME;
  113. opt.server_port = DFL_SERVER_PORT;
  114. opt.debug_level = DFL_DEBUG_LEVEL;
  115. for( i = 1; i < argc; i++ )
  116. {
  117. n = strlen( argv[i] );
  118. for( j = 0; j < n; j++ )
  119. {
  120. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  121. argv[i][j] |= 0x20;
  122. }
  123. p = argv[i];
  124. if( ( q = strchr( p, '=' ) ) == NULL )
  125. goto usage;
  126. *q++ = '\0';
  127. if( strcmp( p, "mode" ) == 0 )
  128. {
  129. if( strcmp( q, "file" ) == 0 )
  130. opt.mode = MODE_FILE;
  131. else if( strcmp( q, "ssl" ) == 0 )
  132. opt.mode = MODE_SSL;
  133. else
  134. goto usage;
  135. }
  136. else if( strcmp( p, "filename" ) == 0 )
  137. opt.filename = q;
  138. else if( strcmp( p, "server_name" ) == 0 )
  139. opt.server_name = q;
  140. else if( strcmp( p, "server_port" ) == 0 )
  141. {
  142. opt.server_port = atoi( q );
  143. if( opt.server_port < 1 || opt.server_port > 65535 )
  144. goto usage;
  145. }
  146. else if( strcmp( p, "debug_level" ) == 0 )
  147. {
  148. opt.debug_level = atoi( q );
  149. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  150. goto usage;
  151. }
  152. else
  153. goto usage;
  154. }
  155. if( opt.mode == MODE_FILE )
  156. {
  157. x509_cert crt;
  158. memset( &crt, 0, sizeof( x509_cert ) );
  159. /*
  160. * 1.1. Load the certificate
  161. */
  162. printf( "\n . Loading the certificate ..." );
  163. fflush( stdout );
  164. ret = x509parse_crtfile( &crt, opt.filename );
  165. if( ret != 0 )
  166. {
  167. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  168. x509_free( &crt );
  169. goto exit;
  170. }
  171. printf( " ok\n" );
  172. /*
  173. * 1.2 Print the certificate
  174. */
  175. printf( " . Peer certificate information ...\n" );
  176. ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", &crt );
  177. if( ret == -1 )
  178. {
  179. printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
  180. x509_free( &crt );
  181. goto exit;
  182. }
  183. printf( "%s\n", buf );
  184. x509_free( &crt );
  185. }
  186. else if( opt.mode == MODE_SSL )
  187. {
  188. /*
  189. * 1. Initialize the RNG and the session data
  190. */
  191. havege_init( &hs );
  192. /*
  193. * 2. Start the connection
  194. */
  195. printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
  196. opt.server_port );
  197. fflush( stdout );
  198. if( ( ret = net_connect( &server_fd, opt.server_name,
  199. opt.server_port ) ) != 0 )
  200. {
  201. printf( " failed\n ! net_connect returned %d\n\n", ret );
  202. goto exit;
  203. }
  204. /*
  205. * 3. Setup stuff
  206. */
  207. if( ( ret = ssl_init( &ssl ) ) != 0 )
  208. {
  209. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  210. goto exit;
  211. }
  212. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  213. ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
  214. ssl_set_rng( &ssl, havege_rand, &hs );
  215. ssl_set_dbg( &ssl, my_debug, stdout );
  216. ssl_set_bio( &ssl, net_recv, &server_fd,
  217. net_send, &server_fd );
  218. ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
  219. ssl_set_session( &ssl, 1, 600, &ssn );
  220. ssl_set_own_cert( &ssl, &clicert, &rsa );
  221. ssl_set_hostname( &ssl, opt.server_name );
  222. /*
  223. * 4. Handshake
  224. */
  225. while( ( ret = ssl_handshake( &ssl ) ) != 0 )
  226. {
  227. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  228. {
  229. printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
  230. goto exit;
  231. }
  232. }
  233. printf( " ok\n" );
  234. /*
  235. * 5. Print the certificate
  236. */
  237. printf( " . Peer certificate information ...\n" );
  238. ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
  239. if( ret == -1 )
  240. {
  241. printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
  242. goto exit;
  243. }
  244. printf( "%s\n", buf );
  245. ssl_close_notify( &ssl );
  246. }
  247. else
  248. goto usage;
  249. exit:
  250. if( server_fd )
  251. net_close( server_fd );
  252. x509_free( &clicert );
  253. rsa_free( &rsa );
  254. ssl_free( &ssl );
  255. memset( &ssl, 0, sizeof( ssl ) );
  256. #ifdef WIN32
  257. printf( " + Press Enter to exit this program.\n" );
  258. fflush( stdout ); getchar();
  259. #endif
  260. return( ret );
  261. }
  262. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
  263. POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
  264. POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */