testget.c 8.0 KB

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