You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

203 lines
4.9 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. void Sender()
  31. {
  32. while(1)
  33. {
  34. string buf(EXCHANGE_STRING);
  35. int len = 0;
  36. do
  37. {
  38. lock_guard<mutex> lock(WriteReadMutex);
  39. len+=SSL_write(SSLHandler, buf.c_str()+len, buf.size()-len);
  40. // for debugging re-neg
  41. cout << "SSL STATE: " << SSL_state_string(SSLHandler) << endl;
  42. } while( len != static_cast<int>(buf.size()) );
  43. }
  44. };
  45. void Client::receive()
  46. {
  47. char buf[MAX_PACKET_SIZE];
  48. cout << "R " << endl;
  49. // TODO: this way it takes 100% CPU, some signal would be usefull
  50. memset(buf,'\0',MAX_PACKET_SIZE);
  51. int len_rcv = 0;
  52. {
  53. lock_guard<mutex> lock(WriteReadMutex);
  54. len_rcv = SSL_read(SSLHandler, buf, MAX_PACKET_SIZE);
  55. }
  56. if( len_rcv != 0 )
  57. {
  58. CharsRead += len_rcv;
  59. // dirty thing - if it has \n on the end - remove it
  60. if( buf[len_rcv-1] == '\n' )
  61. buf[len_rcv-1] = '\0';
  62. cout << buf << endl;
  63. }
  64. else
  65. {
  66. cout << "Closing connection " << _handler << endl;
  67. ::close(_handler);
  68. }
  69. }
  70. void Client::connect()
  71. {
  72. lock_guard<mutex> lock(WriteReadMutex);
  73. struct sockaddr_in echoserver;
  74. _handler = socket(AF_INET, SOCK_STREAM, 0);
  75. memset(&echoserver, 0, sizeof(echoserver));
  76. echoserver.sin_family = AF_INET;
  77. echoserver.sin_addr.s_addr = inet_addr(IP);
  78. echoserver.sin_port = htons(PORT);
  79. /* Establish connection */
  80. if ( 0 > ::connect(_handler, (struct sockaddr *) &echoserver, sizeof(echoserver)) )
  81. {
  82. throw runtime_error("Can't connect to the server");
  83. }
  84. SSLHandler = SSL_new(_ctx);
  85. long mode = SSL_CTX_set_mode(_ctx, SSL_MODE_AUTO_RETRY);
  86. if( ( mode & SSL_MODE_AUTO_RETRY) != SSL_MODE_AUTO_RETRY )
  87. {
  88. throw runtime_error("SSL_MODE_AUTO_RETRY couldn't be set");
  89. }
  90. SSL_set_fd(SSLHandler, _handler);
  91. if( SSL_connect(SSLHandler) <= 0)
  92. {
  93. cerr << "Can't setup SSL session" << endl;
  94. exit(1);
  95. }
  96. // bug reproduces even if call is blocking, so not need to uncommet this line
  97. int opts = fcntl(_handler,F_GETFL);
  98. opts = opts & ( ~O_NONBLOCK );
  99. fcntl(_handler, F_SETFL, opts);
  100. }
  101. void Client::start()
  102. {
  103. // start sender thread first
  104. _sender =new thread( Sender );
  105. struct timeval tv;
  106. // go to select loop
  107. while(1)
  108. {
  109. // wait timer for select
  110. tv.tv_sec = 0;
  111. tv.tv_usec = 10;
  112. fd_set fd_read;
  113. FD_ZERO(&fd_read);
  114. FD_SET(_handler, &fd_read);
  115. select(_handler+1, &fd_read, NULL, NULL, (struct timeval *)&tv);
  116. if( FD_ISSET(_handler, &fd_read ) )
  117. {
  118. // this should be in other thread but... it works
  119. if( CharsRead > RENEG_INIT_LEN )
  120. {
  121. CharsRead = 0;
  122. renegotiate();
  123. }
  124. receive();
  125. }
  126. }
  127. }
  128. void Client::renegotiate()
  129. {
  130. lock_guard<mutex> lock_reads(WriteReadMutex);
  131. cout << "Starting SSL renegotiation on SSL"
  132. << "client (initiating by SSL client)" << endl;
  133. cout << "SSL State: " << SSL_state_string(SSLHandler) << endl;
  134. if(SSL_renegotiate(SSLHandler) <= 0){
  135. cerr << "SSL_renegotiate() failed. STATE: "
  136. << SSL_state_string(SSLHandler) << endl;
  137. ERR_print_errors_fp(stderr);
  138. exit(1);
  139. }
  140. cout << "SSL State: " << SSL_state_string(SSLHandler) << endl;
  141. if(SSL_do_handshake(SSLHandler) <= 0){
  142. cerr << "SSL_do_handshake() failed. STATE: "
  143. << SSL_state_string(SSLHandler) << endl;
  144. ERR_print_errors_fp(stderr);
  145. exit(1);
  146. }
  147. }
  148. void Client::init()
  149. {
  150. sslInit();
  151. }
  152. // --- MAIN --- //
  153. int main()
  154. {
  155. try
  156. {
  157. Client client;
  158. client.init();
  159. client.connect();
  160. client.start();
  161. }
  162. catch(std::runtime_error& e)
  163. {
  164. cerr << "ERROR " << e.what() << endl;
  165. }
  166. catch(...)
  167. {
  168. cerr << "Unknown exception" << endl;
  169. }
  170. return 0;
  171. }