wac_network_ssl_connection.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //#ifdef USE_SSL
  2. //#include "netinc.h"
  3. //#include "util.h"
  4. //#include "connection.h"
  5. #include "wac_network_ssl_connection.h"
  6. SSL_CTX *sslContext = 0;
  7. #ifdef _DEBUG
  8. extern "C" void apps_ssl_info_callback( const SSL * s, int where, int ret )
  9. {
  10. /*
  11. ** DEBUG INFO HERE
  12. */
  13. }
  14. #endif
  15. /*
  16. ** Well, you should probably change this based on like...
  17. ** well, you're level of trust heh
  18. ** For now, this basically trusts all certs :)
  19. **
  20. */
  21. #if 0
  22. extern "C" int verify_callback( int ok, X509_STORE_CTX * ctx )
  23. {
  24. /* For certificate verification */
  25. int verify_depth = 0;
  26. int verify_error = X509_V_OK;
  27. char buf[ 1024 ] = { 0 };
  28. X509 *err_cert = X509_STORE_CTX_get_current_cert( ctx );
  29. int err = X509_STORE_CTX_get_error( ctx );
  30. int depth = X509_STORE_CTX_get_error_depth( ctx );
  31. X509_NAME_oneline( X509_get_subject_name( err_cert ), buf, sizeof( buf ) );
  32. if ( !ok )
  33. {
  34. if ( verify_depth >= depth )
  35. {
  36. ok = 1;
  37. verify_error = X509_V_OK;
  38. }
  39. else
  40. {
  41. ok = 0;
  42. verify_error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  43. }
  44. }
  45. switch ( ctx->error )
  46. {
  47. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  48. X509_NAME_oneline( X509_get_issuer_name( ctx->current_cert ), buf, sizeof( buf ) );
  49. break;
  50. case X509_V_ERR_CERT_NOT_YET_VALID:
  51. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  52. break;
  53. case X509_V_ERR_CERT_HAS_EXPIRED:
  54. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  55. break;
  56. }
  57. return ok;
  58. }
  59. #endif
  60. JNL_SSL_Connection::JNL_SSL_Connection() : forceConnect( false ), m_ssl( 0 ), m_bsslinit( false ), m_bcontextowned( true )
  61. {
  62. m_bsslinit = true;
  63. }
  64. JNL_SSL_Connection::JNL_SSL_Connection( SSL *pssl, api_dns *dns, size_t sendbufsize, size_t recvbufsize ) : WAC_Network_Connection( dns, sendbufsize, recvbufsize ), forceConnect( false )
  65. {
  66. m_ssl = pssl;
  67. m_bsslinit = false;
  68. if ( m_ssl )
  69. {
  70. m_bcontextowned = false;
  71. }
  72. else
  73. {
  74. m_bcontextowned = true;
  75. }
  76. if ( m_bcontextowned == false )
  77. {
  78. return;
  79. }
  80. m_bsslinit = true;
  81. /* See the SSL states in our own callback */
  82. #ifdef _DEBUG
  83. // SSL_CTX_set_info_callback(m_app_ctx, apps_ssl_info_callback);
  84. #endif
  85. /* Set the certificate verification callback */
  86. //SSL_CTX_set_verify(sslContext, SSL_VERIFY_PEER, verify_callback);
  87. /* Not sure what this does */
  88. //SSL_CTX_set_session_cache_mode(sslContext, SSL_SESS_CACHE_CLIENT);
  89. return;
  90. }
  91. int JNL_SSL_Connection::socket_connect()
  92. {
  93. int retval;
  94. if ( m_bcontextowned == false )
  95. {
  96. /*
  97. ** WTF?
  98. */
  99. return -1;
  100. }
  101. if ( m_ssl != NULL )
  102. {
  103. return -1;
  104. }
  105. retval = WAC_Network_Connection::socket_connect();
  106. if ( retval != 0 )
  107. {
  108. if ( ERRNO != EINPROGRESS )
  109. {
  110. return retval; // benski> if the underlying socket hasn't connected yet, then we can't start the SSL connection
  111. /*
  112. ** Joshua Teitelbaum 3/2/2006
  113. ** Fatal error here
  114. */
  115. }
  116. }
  117. // moved from InitSSL() as no need to create this unless
  118. // we're actually going to use it which helps slow loads
  119. if ( !sslContext )
  120. {
  121. sslContext = SSL_CTX_new( SSLv23_client_method() );
  122. if ( sslContext )
  123. {
  124. SSL_CTX_set_verify( sslContext, SSL_VERIFY_NONE, NULL );
  125. }
  126. else
  127. {
  128. return -1;
  129. }
  130. }
  131. m_ssl = SSL_new( sslContext );
  132. if ( m_ssl == NULL )
  133. {
  134. return -1;
  135. }
  136. /* Tell that we are in connect mode */
  137. SSL_set_connect_state( m_ssl );
  138. /* Set socket descriptor with the socket we already have open */
  139. if ( SSL_set_fd( m_ssl, m_socket ) != 0 )
  140. return -1;
  141. return retval;
  142. }
  143. void JNL_SSL_Connection::socket_shutdown()
  144. {
  145. if ( m_ssl )
  146. SSL_shutdown( m_ssl );
  147. WAC_Network_Connection::socket_shutdown();
  148. if ( m_ssl )
  149. {
  150. SSL_free( m_ssl );
  151. m_ssl = NULL;
  152. }
  153. return;
  154. }
  155. void JNL_SSL_Connection::run( size_t max_send_bytes, size_t max_recv_bytes, size_t *bytes_sent, size_t *bytes_rcvd )
  156. {
  157. if ( !m_bsslinit )
  158. {
  159. int rval = SSL_accept( m_ssl );
  160. if ( rval == -1 )
  161. {
  162. int e = SSL_get_error( m_ssl, rval );
  163. if ( !( ( e == SSL_ERROR_WANT_READ ) || ( e == SSL_ERROR_WANT_WRITE ) ) )
  164. {
  165. m_state = STATE_ERROR;
  166. }
  167. return;
  168. }
  169. else
  170. {
  171. m_bsslinit = true;
  172. }
  173. }
  174. /**
  175. ** benski - march 2, 2006
  176. **if the underlying socket didn't connected yet, we need to try the SSL connection again
  177. */
  178. if ( forceConnect )
  179. {
  180. if ( init_ssl_connection() == false )
  181. {
  182. return;
  183. }
  184. }
  185. WAC_Network_Connection::run( max_send_bytes, max_recv_bytes, bytes_sent, bytes_rcvd );
  186. }
  187. /*
  188. ** init_ssl_connection:
  189. ** Returns true, meaning can continue
  190. ** Else, cannot continue with underlying run
  191. ** side effects:
  192. ** sets forceConnect
  193. */
  194. bool JNL_SSL_Connection::init_ssl_connection()
  195. {
  196. if ( m_ssl == NULL )
  197. {
  198. /*
  199. ** WTF?
  200. ** cascade up.
  201. */
  202. return true;
  203. }
  204. int retval = SSL_connect( m_ssl );
  205. if ( retval < 0 )
  206. {
  207. int err = SSL_get_error( m_ssl, retval );
  208. switch ( err )
  209. {
  210. case SSL_ERROR_WANT_READ:
  211. case SSL_ERROR_WANT_WRITE:
  212. case SSL_ERROR_WANT_CONNECT:
  213. forceConnect = true;
  214. break;
  215. // fall through
  216. default: // TODO: benski> MOST other errors are OK, (especially "want read" and "want write", but we need to think through all the scenarios here
  217. forceConnect = false;
  218. }
  219. }
  220. else if ( retval )
  221. {
  222. /*
  223. ** success
  224. */
  225. forceConnect = false;
  226. }
  227. /*
  228. ** If retval == 0
  229. ** socket is closed, or serious error occurred.
  230. ** cascade up.
  231. */
  232. return forceConnect == false;
  233. }
  234. void JNL_SSL_Connection::on_socket_connected( void )
  235. {
  236. init_ssl_connection();
  237. }
  238. void JNL_SSL_Connection::connect( SOCKET s, sockaddr *addr, socklen_t length )
  239. {
  240. /*
  241. ** Joshua Teitelbaum 2/01/2006
  242. ** No need to close
  243. ** This is the reason for divergence as well as setting
  244. ** the connect state
  245. */
  246. m_socket = s;
  247. address = (sockaddr *)malloc( length );
  248. memcpy( address, addr, length );
  249. m_remote_port = 0;
  250. if ( m_socket != -1 )
  251. {
  252. SET_SOCK_BLOCK( m_socket, 0 );
  253. m_state = STATE_CONNECTED;
  254. SSL_set_fd( m_ssl, m_socket );
  255. }
  256. else
  257. {
  258. m_errorstr = _strdup( "invalid socket passed to connect" );
  259. m_state = STATE_ERROR;
  260. }
  261. }
  262. ssize_t JNL_SSL_Connection::socket_recv( char *buf, size_t len, int options )
  263. {
  264. return SSL_read( m_ssl, buf, (int)len );
  265. }
  266. ssize_t JNL_SSL_Connection::socket_send( const char *buf, size_t len, int options )
  267. {
  268. return SSL_write( m_ssl, buf, (int)len );
  269. }
  270. JNL_SSL_Connection::~JNL_SSL_Connection()
  271. {
  272. if ( m_ssl )
  273. {
  274. SSL_free( m_ssl );
  275. m_ssl = NULL;
  276. }
  277. }
  278. //#endif