testget.c 4.1 KB

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