testget.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <openssl/rand.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/err.h>
  13. #include "ptwist.h"
  14. #include "rclient.h"
  15. /* Copied from ssl_locl.h */
  16. # define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  17. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  18. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  19. *((c)++)=(unsigned char)(((l) )&0xff))
  20. // Simple structure to keep track of the handle, and
  21. // of what needs to be freed later.
  22. typedef struct {
  23. int socket;
  24. SSL *sslHandle;
  25. SSL_CTX *sslContext;
  26. } connection;
  27. // For this example, we'll be testing on openssl.org
  28. #define SERVER "cs.uwaterloo.ca"
  29. #define PORT 443
  30. int tag_flow(SSL *s){
  31. unsigned char *result;
  32. int len;
  33. int send_time = 0;
  34. result = s->s3->client_random;
  35. len = sizeof(s->s3->client_random);
  36. if(len < PTWIST_TAG_BYTES) {
  37. printf("Uhoh\n");
  38. return 1;
  39. }
  40. send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0;
  41. if (send_time) {
  42. unsigned long Time = (unsigned long)time(NULL);
  43. unsigned char *p = result;
  44. l2n(Time, p);
  45. tag_hello((byte *) result+4);
  46. } else {
  47. printf("hmm\n");
  48. tag_hello((byte *) result);
  49. }
  50. return 0;
  51. }
  52. // Establish a regular tcp connection
  53. int tcpConnect ()
  54. {
  55. int error, handle;
  56. struct hostent *host;
  57. struct sockaddr_in server;
  58. host = gethostbyname (SERVER);
  59. handle = socket (AF_INET, SOCK_STREAM, 0);
  60. if (handle == -1)
  61. {
  62. perror ("Socket");
  63. handle = 0;
  64. }
  65. else
  66. {
  67. server.sin_family = AF_INET;
  68. server.sin_port = htons (PORT);
  69. server.sin_addr = *((struct in_addr *) host->h_addr);
  70. bzero (&(server.sin_zero), 8);
  71. error = connect (handle, (struct sockaddr *) &server,
  72. sizeof (struct sockaddr));
  73. if (error == -1)
  74. {
  75. perror ("Connect");
  76. handle = 0;
  77. }
  78. }
  79. return handle;
  80. }
  81. // Establish a connection using an SSL layer
  82. connection *sslConnect (void)
  83. {
  84. connection *c;
  85. c = malloc (sizeof (connection));
  86. c->sslHandle = NULL;
  87. c->sslContext = NULL;
  88. c->socket = tcpConnect ();
  89. if (c->socket)
  90. {
  91. // Register the error strings for libcrypto & libssl
  92. SSL_load_error_strings();
  93. // Register the available ciphers and digests
  94. SSL_library_init();
  95. // New context saying we are a client, and using TLSv1.2
  96. c->sslContext = SSL_CTX_new (TLSv1_2_method());
  97. //Tag the client hello message with Telex tag
  98. SSL_CTX_set_client_hello_callback(c->sslContext, tag_flow);
  99. //Set backdoored DH callback
  100. // SSL_CTX_set_generate_key_callback(c->sslContext, generate_backdoor_key);
  101. //SSL_set_dh_seed
  102. if (c->sslContext == NULL)
  103. ERR_print_errors_fp (stderr);
  104. // Create an SSL struct for the connection
  105. c->sslHandle = SSL_new (c->sslContext);
  106. if (c->sslHandle == NULL)
  107. ERR_print_errors_fp (stderr);
  108. // Connect the SSL struct to our connection
  109. if (!SSL_set_fd (c->sslHandle, c->socket))
  110. ERR_print_errors_fp (stderr);
  111. // Initiate SSL handshake
  112. if (SSL_connect (c->sslHandle) != 1)
  113. ERR_print_errors_fp (stderr);
  114. }
  115. else
  116. {
  117. perror ("Connect failed");
  118. }
  119. return c;
  120. }
  121. // Disconnect & free connection struct
  122. void sslDisconnect (connection *c)
  123. {
  124. if (c->socket)
  125. close (c->socket);
  126. if (c->sslHandle)
  127. {
  128. SSL_shutdown (c->sslHandle);
  129. SSL_free (c->sslHandle);
  130. }
  131. if (c->sslContext)
  132. SSL_CTX_free (c->sslContext);
  133. free (c);
  134. }
  135. // Read all available text from the connection
  136. char *sslRead (connection *c)
  137. {
  138. const int readSize = 1024;
  139. char *rc = NULL;
  140. int received, count = 0;
  141. char buffer[1024];
  142. if (c)
  143. {
  144. while (1)
  145. {
  146. if (!rc)
  147. rc = malloc (readSize * sizeof (char) + 1);
  148. else
  149. rc = realloc (rc, (count + 1) *
  150. readSize * sizeof (char) + 1);
  151. received = SSL_read (c->sslHandle, buffer, readSize);
  152. buffer[received] = '\0';
  153. if (received > 0)
  154. strcat (rc, buffer);
  155. if (received < readSize)
  156. break;
  157. count++;
  158. }
  159. }
  160. return rc;
  161. }
  162. // Write text to the connection
  163. void sslWrite (connection *c, char *text)
  164. {
  165. if (c)
  166. SSL_write (c->sslHandle, text, strlen (text));
  167. printf("Wrote:\n%s\n", text);
  168. }
  169. // Very basic main: we send GET / and print the response.
  170. int main (int argc, char **argv)
  171. {
  172. connection *c;
  173. char *response;
  174. c = sslConnect ();
  175. sslWrite (c, "GET /index.php HTTP/1.1\r\nhost: cs.uwaterloo.ca\r\n\r\n");
  176. response = sslRead (c);
  177. printf ("%s\n", response);
  178. sslDisconnect (c);
  179. free (response);
  180. return 0;
  181. }