Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

240 рядки
5.8 KiB

  1. /******************************************************************************/
  2. /**
  3. \Author Krzysztof Kwiatkowski
  4. \File client.cpp
  5. \Description The SSL client which connects to the server.cpp
  6. and initiates renegotitaion after RENEG_INIT_LEN
  7. chars exchanged with the server
  8. *******************************************************************************/
  9. #include "client.h"
  10. #include <unistd.h>
  11. #include "defs.h"
  12. #include <sys/socket.h>
  13. #include <arpa/inet.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <iostream>
  18. #include <netinet/in.h>
  19. #include <boost/thread/thread.hpp>
  20. #include <boost/thread/mutex.hpp>
  21. #include <boost/thread/locks.hpp>
  22. #include <fcntl.h>
  23. #include <openssl/err.h>
  24. using namespace std;
  25. using namespace boost;
  26. SSL* SSLHandler = 0;
  27. int CharsRead = 0;
  28. // with this you can block sender thread during renegotiation
  29. mutex WriteReadMutex;
  30. bool handle_error_code(int& len, SSL* SSLHandler, int code, const char* func)
  31. {
  32. switch( SSL_get_error( SSLHandler, code ) )
  33. {
  34. case SSL_ERROR_NONE:
  35. len+=code;
  36. return false;
  37. case SSL_ERROR_ZERO_RETURN:
  38. cout << "CONNETION CLOSE ON WRITE" << endl;
  39. break;
  40. case SSL_ERROR_WANT_READ:
  41. cout << func << " WANT READ" << endl;
  42. break;
  43. case SSL_ERROR_WANT_WRITE:
  44. cout << func << " WANT WRITE" << endl;
  45. break;
  46. }
  47. return true;
  48. }
  49. void Sender()
  50. {
  51. while(1)
  52. {
  53. string buf(EXCHANGE_STRING);
  54. int len = 0;
  55. do
  56. {
  57. lock_guard<mutex> lock(WriteReadMutex);
  58. bool flag = true;
  59. while( flag )
  60. {
  61. int code = SSL_write(SSLHandler, buf.c_str()+len, buf.size()-len);
  62. flag = handle_error_code(len, SSLHandler, code, "SSL_write");
  63. }
  64. // for debugging re-neg
  65. cout << "SSL STATE: " << SSL_state_string(SSLHandler) << endl;
  66. } while( len != static_cast<int>(buf.size()) );
  67. }
  68. };
  69. void Client::receive()
  70. {
  71. char buf[MAX_PACKET_SIZE];
  72. cout << "R " << endl;
  73. // TODO: this way it takes 100% CPU, some signal would be usefull
  74. memset(buf,'\0',MAX_PACKET_SIZE);
  75. int len_rcv = 0;
  76. {
  77. lock_guard<mutex> lock(WriteReadMutex);
  78. bool flag = true;
  79. while( flag )
  80. {
  81. len_rcv = SSL_read(SSLHandler, buf, MAX_PACKET_SIZE);
  82. flag = handle_error_code(len_rcv, SSLHandler, len_rcv, "SSL_read");
  83. }
  84. }
  85. if( len_rcv != 0 )
  86. {
  87. CharsRead += len_rcv;
  88. // dirty thing - if it has \n on the end - remove it
  89. if( buf[len_rcv-1] == '\n' )
  90. buf[len_rcv-1] = '\0';
  91. cout << buf << endl;
  92. }
  93. else
  94. {
  95. cout << "Closing connection " << _handler << endl;
  96. ::close(_handler);
  97. }
  98. }
  99. void Client::connect()
  100. {
  101. lock_guard<mutex> lock(WriteReadMutex);
  102. struct sockaddr_in echoserver;
  103. _handler = socket(AF_INET, SOCK_STREAM, 0);
  104. memset(&echoserver, 0, sizeof(echoserver));
  105. echoserver.sin_family = AF_INET;
  106. echoserver.sin_addr.s_addr = inet_addr(IP);
  107. echoserver.sin_port = htons(PORT);
  108. /* Establish connection */
  109. if ( 0 > ::connect(_handler, (struct sockaddr *) &echoserver, sizeof(echoserver)) )
  110. {
  111. throw runtime_error("Can't connect to the server");
  112. }
  113. SSLHandler = SSL_new(_ctx);
  114. // if socket is blocking you can set this and forget about looking at SSL_get_error code on I/O calls
  115. // long mode = SSL_CTX_set_mode(_ctx, SSL_MODE_AUTO_RETRY);
  116. // if( ( mode & SSL_MODE_AUTO_RETRY) != SSL_MODE_AUTO_RETRY )
  117. // {
  118. // throw runtime_error("SSL_MODE_AUTO_RETRY couldn't be set");
  119. // }
  120. SSL_set_fd(SSLHandler, _handler);
  121. if( SSL_connect(SSLHandler) <= 0)
  122. {
  123. cerr << "Can't setup SSL session" << endl;
  124. exit(1);
  125. }
  126. // bug reproduces even if call is blocking, so not need to uncommet this line
  127. int opts = fcntl(_handler,F_GETFL);
  128. opts = opts & ( ~O_NONBLOCK );
  129. fcntl(_handler, F_SETFL, opts);
  130. }
  131. void Client::start()
  132. {
  133. // start sender thread first
  134. _sender =new thread( Sender );
  135. struct timeval tv;
  136. // go to select loop
  137. while(1)
  138. {
  139. // wait timer for select
  140. tv.tv_sec = 0;
  141. tv.tv_usec = 10;
  142. fd_set fd_read;
  143. FD_ZERO(&fd_read);
  144. FD_SET(_handler, &fd_read);
  145. select(_handler+1, &fd_read, NULL, NULL, (struct timeval *)&tv);
  146. if( FD_ISSET(_handler, &fd_read ) )
  147. {
  148. // this should be in other thread but... it works
  149. #ifdef _RENEG_ON_
  150. if( CharsRead > RENEG_INIT_LEN )
  151. {
  152. CharsRead = 0;
  153. renegotiate();
  154. }
  155. #endif
  156. receive();
  157. }
  158. }
  159. }
  160. void Client::renegotiate()
  161. {
  162. lock_guard<mutex> lock_reads(WriteReadMutex);
  163. cout << "Starting SSL renegotiation on SSL"
  164. << "client (initiating by SSL client)" << endl;
  165. cout << "SSL State: " << SSL_state_string(SSLHandler) << endl;
  166. if(SSL_renegotiate(SSLHandler) <= 0){
  167. cerr << "SSL_renegotiate() failed. STATE: "
  168. << SSL_state_string(SSLHandler) << endl;
  169. ERR_print_errors_fp(stderr);
  170. exit(1);
  171. }
  172. cout << "SSL State: " << SSL_state_string(SSLHandler) << endl;
  173. if(SSL_do_handshake(SSLHandler) <= 0){
  174. cerr << "SSL_do_handshake() failed. STATE: "
  175. << SSL_state_string(SSLHandler) << endl;
  176. ERR_print_errors_fp(stderr);
  177. exit(1);
  178. }
  179. }
  180. void Client::init()
  181. {
  182. sslInit();
  183. }
  184. // --- MAIN --- //
  185. int main()
  186. {
  187. try
  188. {
  189. Client client;
  190. client.init();
  191. client.connect();
  192. client.start();
  193. }
  194. catch(std::runtime_error& e)
  195. {
  196. cerr << "ERROR " << e.what() << endl;
  197. }
  198. catch(...)
  199. {
  200. cerr << "Unknown exception" << endl;
  201. }
  202. return 0;
  203. }