testget.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. //from Slitheen header
  21. struct slitheen_header {
  22. u_char marker; /* 0x01 means censored data, 0x02 means dummy data */
  23. u_char version; /* For now 0x01 */
  24. u_short len;
  25. };
  26. #define SLITHEEN_HEADER_LEN 4
  27. // Simple structure to keep track of the handle, and
  28. // of what needs to be freed later.
  29. typedef struct {
  30. int socket;
  31. SSL *sslHandle;
  32. SSL_CTX *sslContext;
  33. } connection;
  34. int generate_backdoor_key(SSL *s, DH *dh);
  35. // For this example, we'll be testing on openssl.org
  36. #define SERVER "cs.uwaterloo.ca"
  37. #define PORT 443
  38. byte key[16];
  39. //Client hello callback
  40. int tag_flow(SSL *s){
  41. unsigned char *result;
  42. int len, i;
  43. result = s->s3->client_random;
  44. len = sizeof(s->s3->client_random);
  45. if(len < PTWIST_TAG_BYTES) {
  46. printf("Uhoh\n");
  47. return 1;
  48. }
  49. unsigned long Time = (unsigned long)time(NULL);
  50. unsigned char *p = result;
  51. l2n(Time, p);
  52. tag_hello((byte *) result+4, key);
  53. printf("Hello tagged.\n");
  54. return 0;
  55. }
  56. // Establish a regular tcp connection
  57. int tcpConnect ()
  58. {
  59. int error, handle;
  60. struct hostent *host;
  61. struct sockaddr_in server;
  62. host = gethostbyname (SERVER);
  63. handle = socket (AF_INET, SOCK_STREAM, 0);
  64. if (handle == -1)
  65. {
  66. perror ("Socket");
  67. handle = 0;
  68. }
  69. else
  70. {
  71. server.sin_family = AF_INET;
  72. server.sin_port = htons (PORT);
  73. server.sin_addr = *((struct in_addr *) host->h_addr);
  74. bzero (&(server.sin_zero), 8);
  75. error = connect (handle, (struct sockaddr *) &server,
  76. sizeof (struct sockaddr));
  77. if (error == -1)
  78. {
  79. perror ("Connect");
  80. handle = 0;
  81. }
  82. }
  83. return handle;
  84. }
  85. // Establish a connection using an SSL layer
  86. connection *sslConnect (void)
  87. {
  88. connection *c;
  89. FILE *fp;
  90. c = malloc (sizeof (connection));
  91. c->sslHandle = NULL;
  92. c->sslContext = NULL;
  93. c->socket = tcpConnect ();
  94. if (c->socket)
  95. {
  96. // Register the error strings for libcrypto & libssl
  97. SSL_load_error_strings();
  98. // Register the available ciphers and digests
  99. SSL_library_init();
  100. // New context saying we are a client, and using TLSv1.2
  101. c->sslContext = SSL_CTX_new (TLSv1_2_method());
  102. //Tag the client hello message with Telex tag
  103. SSL_CTX_set_client_hello_callback(c->sslContext, tag_flow);
  104. //Set backdoored DH callback
  105. SSL_CTX_set_generate_key_callback(c->sslContext, generate_backdoor_key);
  106. if (c->sslContext == NULL)
  107. ERR_print_errors_fp (stderr);
  108. //make sure DH is in the cipher list
  109. const char *ciphers = "DHE";
  110. if(!SSL_CTX_set_cipher_list(c->sslContext, ciphers))
  111. printf("Failed to set cipher.\n");
  112. // Create an SSL struct for the connection
  113. c->sslHandle = SSL_new (c->sslContext);
  114. if (c->sslHandle == NULL)
  115. ERR_print_errors_fp (stderr);
  116. const unsigned char *list = SSL_get_cipher_list(c->sslHandle, 1);
  117. printf("List of ciphers: %s", list);
  118. // Connect the SSL struct to our connection
  119. if (!SSL_set_fd (c->sslHandle, c->socket))
  120. ERR_print_errors_fp (stderr);
  121. // Initiate SSL handshake
  122. if (SSL_connect (c->sslHandle) != 1)
  123. ERR_print_errors_fp (stderr);
  124. const unsigned char *cipher = SSL_get_cipher_name(c->sslHandle);
  125. printf("CIPHER: %s\n", cipher);
  126. }
  127. else
  128. {
  129. perror ("Connect failed");
  130. }
  131. return c;
  132. }
  133. // Disconnect & free connection struct
  134. void sslDisconnect (connection *c)
  135. {
  136. if (c->socket)
  137. close (c->socket);
  138. if (c->sslHandle)
  139. {
  140. SSL_shutdown (c->sslHandle);
  141. SSL_free (c->sslHandle);
  142. }
  143. if (c->sslContext)
  144. SSL_CTX_free (c->sslContext);
  145. free (c);
  146. }
  147. // Read all available text from the connection
  148. char *sslRead (connection *c)
  149. {
  150. const int readSize = 16384;
  151. char *rc = NULL;
  152. int received, count = 0;
  153. char buffer[16384];
  154. if (c)
  155. {
  156. while (1)
  157. {
  158. if (!rc)
  159. rc = malloc (readSize * sizeof (char) + 1);
  160. else
  161. rc = realloc (rc, count + readSize * sizeof (char)
  162. + 1);
  163. received = SSL_read (c->sslHandle, buffer, readSize);
  164. buffer[received] = '\0';
  165. if (received > 0){
  166. printf("read %d bytes\n", received);
  167. printf("Slitheen header:\n");
  168. int i;
  169. for(i=0; (i<SLITHEEN_HEADER_LEN) && (i<received); i++)
  170. printf("%02x ", buffer[i]);
  171. printf("\n");
  172. struct slitheen_header *sl_hdr = (struct slitheen_header *) buffer;
  173. printf("recieved %d bytes censored data\n", ntohs(sl_hdr->len));
  174. buffer[SLITHEEN_HEADER_LEN+ ntohs(sl_hdr->len)] = '\0';
  175. if(received > SLITHEEN_HEADER_LEN)
  176. strcat (rc, buffer+SLITHEEN_HEADER_LEN);
  177. }
  178. else if (received < 0){
  179. fprintf(stdout, "Error: %d\n", SSL_get_error(c->sslHandle, received));
  180. unsigned long err = ERR_get_error();
  181. uint8_t errbuf[256];
  182. while(err != 0){
  183. ERR_error_string(err, errbuf);
  184. fprintf(stdout, "%s\n", errbuf);
  185. err = ERR_get_error();
  186. fflush(stdout);
  187. }
  188. break;
  189. }
  190. else{
  191. printf("read %d bytes\n", received);
  192. break;
  193. }
  194. count+=received;
  195. }
  196. }
  197. return rc;
  198. }
  199. // Write text to the connection
  200. void sslWrite (connection *c, char *text)
  201. {
  202. if (c)
  203. SSL_write (c->sslHandle, text, strlen (text));
  204. printf("Wrote:\n%s\n", text);
  205. }
  206. // Very basic main: we send GET / and print the response.
  207. int main (int argc, char **argv)
  208. {
  209. connection *c;
  210. char *response;
  211. char *response2;
  212. FILE *fp;
  213. c = sslConnect ();
  214. sslWrite (c, "GET /about/ HTTP/1.1\nhost: cs.uwaterloo.ca\nx-ignore: GET /index.html HTTP/1.1\\nhost: xkcd.com\n\n");
  215. response = sslRead (c);
  216. printf ("%s\n", response);
  217. fflush(stdout);
  218. //Now manually request image
  219. sslWrite (c, "GET /about/ HTTP/1.1\r\nhost: cs.uwaterloo.ca\r\n\r\n");
  220. response2 = sslRead (c);
  221. printf ("%s\n", response2);
  222. fflush(stdout);
  223. sslDisconnect (c);
  224. free (response);
  225. free (response2);
  226. return 0;
  227. }
  228. //dh callback
  229. int generate_backdoor_key(SSL *s, DH *dh)
  230. {
  231. int ok = 0;
  232. int generate_new_key = 0;
  233. unsigned l;
  234. BN_CTX *ctx;
  235. BN_MONT_CTX *mont = NULL;
  236. BIGNUM *pub_key = NULL, *priv_key= NULL;
  237. unsigned char *buf, *seed;
  238. int bytes, i;
  239. FILE *fp;
  240. seed = (unsigned char *) key;
  241. fp = fopen("seed", "wb");
  242. if (fp == NULL) {
  243. perror("fopen");
  244. exit(1);
  245. }
  246. for(i=0; i< 16; i++){
  247. fprintf(fp, "%02x", key[i]);
  248. }
  249. fclose(fp);
  250. printf("In backdoor callback.\n");
  251. ctx = BN_CTX_new();
  252. if (ctx == NULL)
  253. goto err;
  254. if (dh->priv_key == NULL) {
  255. priv_key = BN_new();
  256. if (priv_key == NULL)
  257. goto err;
  258. generate_new_key = 1;
  259. } else
  260. priv_key = dh->priv_key;
  261. if (dh->pub_key == NULL) {
  262. pub_key = BN_new();
  263. if (pub_key == NULL)
  264. goto err;
  265. } else
  266. pub_key = dh->pub_key;
  267. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  268. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  269. CRYPTO_LOCK_DH, dh->p, ctx);
  270. if (!mont)
  271. goto err;
  272. }
  273. if (generate_new_key) {
  274. /* secret exponent length */
  275. l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
  276. bytes = (l+7) / 8;
  277. /* set exponent to seeded prg value */
  278. buf = (unsigned char *)OPENSSL_malloc(bytes);
  279. if (buf == NULL){
  280. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  281. goto err;
  282. }
  283. /*int bytes_read = RAND_load_file("/home/sltiheen/Downloads/client/seed", 16);
  284. printf("read %d bytes\n", bytes_read);
  285. //RAND_seed(seed, 16);
  286. printf("Using the seed: ");
  287. for(i=0; i< 16; i++){
  288. printf(" %02x ", seed[i]);
  289. }
  290. printf("\n");
  291. if(RAND_bytes(buf, bytes) <= 0)
  292. goto err;
  293. */
  294. for(i=0; i<bytes; i++){
  295. buf[i] = seed[i%16];
  296. }
  297. printf("Generated the following rand bytes: ");
  298. for(i=0; i< bytes; i++){
  299. printf(" %02x ", buf[i]);
  300. }
  301. printf("\n");
  302. if (!BN_bin2bn(buf, bytes, priv_key))
  303. goto err;
  304. }
  305. {
  306. BIGNUM local_prk;
  307. BIGNUM *prk;
  308. if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) {
  309. BN_init(&local_prk);
  310. prk = &local_prk;
  311. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  312. } else
  313. prk = priv_key;
  314. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont))
  315. goto err;
  316. }
  317. dh->pub_key = pub_key;
  318. dh->priv_key = priv_key;
  319. ok = 1;
  320. err:
  321. if (buf != NULL){
  322. OPENSSL_cleanse(buf, bytes);
  323. OPENSSL_free(buf);
  324. }
  325. if (ok != 1)
  326. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  327. if ((pub_key != NULL) && (dh->pub_key == NULL))
  328. BN_free(pub_key);
  329. if ((priv_key != NULL) && (dh->priv_key == NULL))
  330. BN_free(priv_key);
  331. BN_CTX_free(ctx);
  332. return (ok);
  333. }