cert_app.c 9.0 KB

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