testget.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. if (c->sslContext == NULL)
  100. ERR_print_errors_fp (stderr);
  101. // Create an SSL struct for the connection
  102. c->sslHandle = SSL_new (c->sslContext);
  103. if (c->sslHandle == NULL)
  104. ERR_print_errors_fp (stderr);
  105. // Connect the SSL struct to our connection
  106. if (!SSL_set_fd (c->sslHandle, c->socket))
  107. ERR_print_errors_fp (stderr);
  108. // Initiate SSL handshake
  109. if (SSL_connect (c->sslHandle) != 1)
  110. ERR_print_errors_fp (stderr);
  111. }
  112. else
  113. {
  114. perror ("Connect failed");
  115. }
  116. return c;
  117. }
  118. // Disconnect & free connection struct
  119. void sslDisconnect (connection *c)
  120. {
  121. if (c->socket)
  122. close (c->socket);
  123. if (c->sslHandle)
  124. {
  125. SSL_shutdown (c->sslHandle);
  126. SSL_free (c->sslHandle);
  127. }
  128. if (c->sslContext)
  129. SSL_CTX_free (c->sslContext);
  130. free (c);
  131. }
  132. // Read all available text from the connection
  133. char *sslRead (connection *c)
  134. {
  135. const int readSize = 1024;
  136. char *rc = NULL;
  137. int received, count = 0;
  138. char buffer[1024];
  139. if (c)
  140. {
  141. while (1)
  142. {
  143. if (!rc)
  144. rc = malloc (readSize * sizeof (char) + 1);
  145. else
  146. rc = realloc (rc, (count + 1) *
  147. readSize * sizeof (char) + 1);
  148. received = SSL_read (c->sslHandle, buffer, readSize);
  149. buffer[received] = '\0';
  150. if (received > 0)
  151. strcat (rc, buffer);
  152. if (received < readSize)
  153. break;
  154. count++;
  155. }
  156. }
  157. return rc;
  158. }
  159. // Write text to the connection
  160. void sslWrite (connection *c, char *text)
  161. {
  162. if (c)
  163. SSL_write (c->sslHandle, text, strlen (text));
  164. printf("Wrote:\n%s\n", text);
  165. }
  166. // Very basic main: we send GET / and print the response.
  167. int main (int argc, char **argv)
  168. {
  169. connection *c;
  170. char *response;
  171. c = sslConnect ();
  172. sslWrite (c, "GET /index.php HTTP/1.1\r\nhost: cs.uwaterloo.ca\r\n\r\n");
  173. response = sslRead (c);
  174. printf ("%s\n", response);
  175. sslDisconnect (c);
  176. free (response);
  177. return 0;
  178. }