crypto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. #include <openssl/evp.h>
  2. #include <openssl/dh.h>
  3. #include <openssl/bn.h>
  4. #include <openssl/err.h>
  5. #include <openssl/rand.h>
  6. #include <openssl/ssl.h>
  7. #include "rserv.h"
  8. #include "crypto.h"
  9. #include "flow.h"
  10. #include "slitheen.h"
  11. /** Updates the hash of all TLS handshake messages upon the
  12. * receipt of a new message. This hash is eventually used
  13. * to verify the TLS Finished message
  14. *
  15. * Inputs:
  16. * f: the tagged flow
  17. * hs: A pointer to the start of the handshake message
  18. *
  19. * Output:
  20. * 0 on success, 1 on failure
  21. */
  22. int update_finish_hash(flow *f, uint8_t *hs){
  23. //find handshake length
  24. const struct handshake_header *hs_hdr;
  25. uint8_t *p = hs;
  26. hs_hdr = (struct handshake_header*) p;
  27. uint32_t hs_len = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  28. EVP_DigestUpdate(f->finish_md_ctx, hs, hs_len+4);
  29. return 0;
  30. }
  31. /** Extracts the server parameters from the server key
  32. * exchange message
  33. *
  34. * Inputs:
  35. * f: the tagged flow
  36. * hs: the beginning of the server key exchange
  37. * handshake message
  38. *
  39. * Output:
  40. * 0 on success, 1 on failure
  41. */
  42. int extract_parameters(flow *f, uint8_t *hs){
  43. DH *dh;
  44. uint8_t *p;
  45. long i;
  46. p = hs + HANDSHAKE_HEADER_LEN;
  47. if((dh = DH_new()) == NULL){
  48. return 1;
  49. }
  50. /* Extract prime modulus */
  51. n2s(p,i);
  52. if(!(dh->p = BN_bin2bn(p,i,NULL))){
  53. return 1;
  54. }
  55. p += i;
  56. /* Extract generator */
  57. n2s(p,i);
  58. if(!(dh->g = BN_bin2bn(p,i,NULL))){
  59. return 1;
  60. }
  61. p += i;
  62. /* Extract server public value */
  63. n2s(p,i);
  64. if(!(dh->pub_key = BN_bin2bn(p,i,NULL))){
  65. return 1;
  66. }
  67. f->dh = dh;
  68. return 0;
  69. }
  70. /** MAC a message
  71. * TODO: look at tls1_mac in t1_enc.c
  72. * For now, only goes one way (macs message to be written)
  73. *
  74. int32_t mac(flow *f, uint8_t *input, uint8_t *output, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  75. uint8_t header[13];
  76. int32_t md_size;
  77. header[8] = type;
  78. header[9] = 0x03;//TODO: update for different versions
  79. header[10] = 0x03;
  80. header[11] = (len) >> 8;
  81. header[12] = (len) & 0xff;
  82. EVP_DigestSignUpdate(f->read_mac_ctx, header, sizeof(header));
  83. EVP_DigestSignUpdate(f->read_mac_ctx, input, len);
  84. EVP_DigestSignFinal(f->read_mac_ctx, output, &md_size);
  85. return md_size;
  86. }*/
  87. /* Encrypt/Decrypt a TLS record
  88. *
  89. * Inputs:
  90. * f: the tagged flow
  91. * input: a pointer to the data that is to be encrypted/
  92. * decrypted
  93. * output: a pointer to where the data should be written
  94. * after it is encrypted or decrypted
  95. * len: the length of the data
  96. * incoming: the direction of the record
  97. * type: the type of the TLS record
  98. * enc: 1 for encryption, 0 for decryption
  99. *
  100. * Output:
  101. * length of the output data
  102. */
  103. int encrypt(flow *f, uint8_t *input, uint8_t *output, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  104. uint8_t *p = input;
  105. EVP_CIPHER_CTX *ds = (incoming) ? ((enc) ? f->srvr_write_ctx : f->clnt_read_ctx) : ((enc) ? f->clnt_write_ctx : f->srvr_read_ctx) ;
  106. if(ds == NULL){
  107. printf("FAIL\n");
  108. return 1;
  109. }
  110. uint8_t *seq;
  111. seq = (incoming) ? f->read_seq : f->write_seq;
  112. if(f->application && (ds->iv[EVP_GCM_TLS_FIXED_IV_LEN] == 0)){
  113. //fill in rest of iv
  114. for(int i = EVP_GCM_TLS_FIXED_IV_LEN; i< ds->cipher->iv_len; i++){
  115. ds->iv[i] = p[i- EVP_GCM_TLS_FIXED_IV_LEN];
  116. }
  117. }
  118. #ifdef DEBUG
  119. printf("\t\tiv: ");
  120. for(int i=0; i<ds->cipher->iv_len; i++){
  121. printf("%02X ", ds->iv[i]);
  122. }
  123. printf("\n");
  124. #endif
  125. uint8_t buf[13];
  126. memcpy(buf, seq, 8);
  127. for(int i=7; i>=0; i--){
  128. ++seq[i];
  129. if(seq[i] != 0)
  130. break;
  131. }
  132. buf[8] = type;
  133. buf[9] = 0x03;
  134. buf[10] = 0x03;
  135. buf[11] = len >> 8; //len >> 8;
  136. buf[12] = len & 0xff;//len *0xff;
  137. int32_t pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  138. 13, buf); // = int32_t pad?
  139. printf("pad: %d\n", pad);
  140. if(enc)
  141. len += pad;
  142. int32_t n = EVP_Cipher(ds, p, p, len); //decrypt in place
  143. if(n<0) return 0;
  144. #ifdef DEBUG
  145. printf("decrypted data:\n");
  146. for(int i=0; i< len; i++){
  147. printf("%02x ", p[EVP_GCM_TLS_EXPLICIT_IV_LEN+i]);
  148. }
  149. printf("\n");
  150. #endif
  151. if(!enc)
  152. p[EVP_GCM_TLS_EXPLICIT_IV_LEN+n] = '\0';
  153. return n;
  154. }
  155. /** Verifies the hash in a TLS finished message
  156. *
  157. * Inputs:
  158. * f: the tagged flow
  159. * p: a pointer to the TLS Finished handshake message
  160. * incoming: the direction of the flow
  161. *
  162. * Output:
  163. * 0 on success, 1 on failure
  164. */
  165. int verify_finish_hash(flow *f, uint8_t *p, int32_t incoming){
  166. EVP_MD_CTX ctx;
  167. uint8_t hash[EVP_MAX_MD_SIZE];
  168. uint32_t hash_len;
  169. EVP_MD_CTX_init(&ctx);
  170. //get header length
  171. struct handshake_header *hs_hdr;
  172. hs_hdr = (struct handshake_header*) p;
  173. uint32_t fin_length = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  174. p += HANDSHAKE_HEADER_LEN;
  175. //finalize hash of handshake msgs
  176. EVP_MD_CTX_copy_ex(&ctx, f->finish_md_ctx);
  177. EVP_DigestFinal_ex(&ctx, hash, &hash_len);
  178. //now use pseudorandom function
  179. uint8_t *output = calloc(1, fin_length);
  180. if(incoming){
  181. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, (uint8_t *) TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  182. } else {
  183. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, (uint8_t *) TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  184. }
  185. //now compare
  186. if(CRYPTO_memcmp(p, output, fin_length) != 0){
  187. // printf("VERIFY FAILED\n");
  188. return 1;
  189. } else {
  190. printf("VERIFY PASSED\n");
  191. }
  192. return 0;
  193. }
  194. /** Computes the TLS master secret from the decoy server's
  195. * public key parameters and the leaked secret from the
  196. * extracted Slitheen tag
  197. *
  198. * Input:
  199. * f: the tagged flow
  200. *
  201. * Output:
  202. * 0 on success, 1 on failure
  203. */
  204. int compute_master_secret(flow *f){
  205. printf("Computing master secret (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  206. DH *dh_srvr = NULL;
  207. DH *dh_clnt = NULL;
  208. BN_CTX *ctx;
  209. BN_MONT_CTX *mont = NULL;
  210. BIGNUM *pub_key = NULL, *priv_key = NULL;
  211. ctx = BN_CTX_new();
  212. dh_srvr = f->dh;
  213. dh_clnt = DHparams_dup(dh_srvr);
  214. uint32_t l = dh_clnt->length ? dh_clnt->length : BN_num_bits(dh_clnt->p) - 1;
  215. int32_t bytes = (l+7) / 8;
  216. printf("length of dh param: %d\n", bytes);
  217. uint8_t *buf = (uint8_t *)OPENSSL_malloc(bytes);
  218. if (buf == NULL){
  219. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  220. return 1;
  221. }
  222. pub_key = BN_new();
  223. priv_key = BN_new();
  224. printf("key =");
  225. for(int i=0; i< 16; i++)
  226. printf(" %02x", f->key[i]);
  227. printf("\n");
  228. PRF(f->key, 16,
  229. (uint8_t *) SLITHEEN_KEYGEN_CONST, SLITHEEN_KEYGEN_CONST_SIZE,
  230. NULL, 0, NULL, 0, NULL, 0,
  231. buf, bytes);
  232. //#ifdef DEBUG
  233. printf("Generated the following rand bytes: ");
  234. for(int i=0; i< bytes; i++){
  235. printf(" %02x ", buf[i]);
  236. }
  237. printf("\n");
  238. //#endif
  239. if (!BN_bin2bn(buf, bytes, priv_key))
  240. return 1;
  241. {
  242. BIGNUM *prk;
  243. prk = priv_key;
  244. if (!dh_clnt->meth->bn_mod_exp(dh_clnt, pub_key, dh_clnt->g, prk, dh_clnt->p, ctx, mont)){
  245. printf("FAIL\n");
  246. return 1;
  247. }
  248. }
  249. dh_clnt->pub_key = pub_key;
  250. dh_clnt->priv_key = priv_key;
  251. // Compute master key
  252. uint8_t *pre_master_secret = calloc(1, PRE_MASTER_MAX_LEN);//TODO: find right length
  253. int32_t pre_master_len = DH_compute_key(pre_master_secret, dh_srvr->pub_key, dh_clnt);
  254. PRF(pre_master_secret, pre_master_len, (uint8_t *) TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE, f->client_random, SSL3_RANDOM_SIZE, f->server_random, SSL3_RANDOM_SIZE, NULL, 0, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  255. if(f->current_session != NULL)
  256. memcpy(f->current_session, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  257. #ifdef DEBUG
  258. fprintf(stdout, "Premaster Secret:\n");
  259. BIO_dump_fp(stdout, (char *)pre_master_secret, pre_master_len);
  260. fprintf(stdout, "Client Random:\n");
  261. BIO_dump_fp(stdout, (char *)f->client_random, SSL3_RANDOM_SIZE);
  262. fprintf(stdout, "Server Random:\n");
  263. BIO_dump_fp(stdout, (char *)f->server_random, SSL3_RANDOM_SIZE);
  264. fprintf(stdout, "Master Secret:\n");
  265. BIO_dump_fp(stdout, (char *)f->master_secret, SSL3_MASTER_SECRET_SIZE);
  266. #endif
  267. //remove pre_master_secret from memory
  268. memset(pre_master_secret, 0, PRE_MASTER_MAX_LEN);
  269. free(pre_master_secret);
  270. DH_free(dh_srvr);
  271. DH_free(dh_clnt);
  272. return 0;
  273. }
  274. /** Saves the random none from the server hello message
  275. *
  276. * Inputs:
  277. * f: the tagged flow
  278. * hs: a pointer to the beginning of the server hello msg
  279. *
  280. * Output:
  281. * none
  282. */
  283. void extract_server_random(flow *f, uint8_t *hs){
  284. uint8_t *p;
  285. p = hs + HANDSHAKE_HEADER_LEN;
  286. p+=2; //skip version
  287. memcpy(f->server_random, p, SSL3_RANDOM_SIZE);
  288. }
  289. /** PRF using sha384, as defined in RFC 5246
  290. *
  291. * Inputs:
  292. * secret: the master secret used to sign the hash
  293. * secret_len: the length of the master secret
  294. * seed{1, ..., 4}: seed values that are virtually
  295. * concatenated
  296. * seed{1,...4}_len: length of the seeds
  297. * output: a pointer to the output of the PRF
  298. * output_len: the number of desired bytes
  299. *
  300. * Output:
  301. * 0 on success, 1 on failure
  302. */
  303. int PRF(uint8_t *secret, int32_t secret_len,
  304. uint8_t *seed1, int32_t seed1_len,
  305. uint8_t *seed2, int32_t seed2_len,
  306. uint8_t *seed3, int32_t seed3_len,
  307. uint8_t *seed4, int32_t seed4_len,
  308. uint8_t *output, int32_t output_len){
  309. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  310. EVP_PKEY *mac_key;
  311. const EVP_MD *md = EVP_sha384();
  312. uint8_t A[EVP_MAX_MD_SIZE];
  313. size_t len, A_len;
  314. int chunk = EVP_MD_size(md);
  315. int remaining = output_len;
  316. uint8_t *out = output;
  317. EVP_MD_CTX_init(&ctx);
  318. EVP_MD_CTX_init(&ctx_tmp);
  319. EVP_MD_CTX_init(&ctx_init);
  320. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  321. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  322. /* Calculate first A value */
  323. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  324. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  325. if(seed1 != NULL && seed1_len > 0){
  326. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  327. }
  328. if(seed2 != NULL && seed2_len > 0){
  329. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  330. }
  331. if(seed3 != NULL && seed3_len > 0){
  332. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  333. }
  334. if(seed4 != NULL && seed4_len > 0){
  335. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  336. }
  337. EVP_DigestSignFinal(&ctx, A, &A_len);
  338. //iterate until desired length is achieved
  339. while(remaining > 0){
  340. /* Now compute SHA384(secret, A+seed) */
  341. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  342. EVP_DigestSignUpdate(&ctx, A, A_len);
  343. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  344. if(seed1 != NULL && seed1_len > 0){
  345. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  346. }
  347. if(seed2 != NULL && seed2_len > 0){
  348. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  349. }
  350. if(seed3 != NULL && seed3_len > 0){
  351. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  352. }
  353. if(seed4 != NULL && seed4_len > 0){
  354. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  355. }
  356. if(remaining > chunk){
  357. EVP_DigestSignFinal(&ctx, out, &len);
  358. out += len;
  359. remaining -= len;
  360. /* Next A value */
  361. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  362. } else {
  363. EVP_DigestSignFinal(&ctx, A, &A_len);
  364. memcpy(out, A, remaining);
  365. remaining -= remaining;
  366. }
  367. }
  368. return 0;
  369. }
  370. /** After receiving change cipher spec, calculate keys from master secret
  371. *
  372. * Input:
  373. * f: the tagged flow
  374. *
  375. * Output:
  376. * 0 on success, 1 on failure
  377. */
  378. int init_ciphers(flow *f){
  379. EVP_CIPHER_CTX *r_ctx;
  380. EVP_CIPHER_CTX *w_ctx;
  381. EVP_CIPHER_CTX *w_ctx_srvr;
  382. EVP_CIPHER_CTX *r_ctx_srvr;
  383. const EVP_CIPHER *c = EVP_aes_256_gcm();
  384. /* Generate Keys */
  385. uint8_t *write_key, *write_iv;
  386. uint8_t *read_key, *read_iv;
  387. int32_t mac_len, key_len, iv_len;
  388. key_len = EVP_CIPHER_key_length(c);
  389. iv_len = EVP_CIPHER_iv_length(c); //EVP_GCM_TLS_FIXED_IV_LEN;
  390. mac_len = EVP_MD_size(EVP_sha384());
  391. int32_t total_len = key_len + iv_len + mac_len;
  392. total_len *= 2;
  393. uint8_t *key_block = calloc(1, total_len);
  394. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE,
  395. (uint8_t *) TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  396. f->server_random, SSL3_RANDOM_SIZE,
  397. f->client_random, SSL3_RANDOM_SIZE,
  398. NULL, 0,
  399. key_block, total_len);
  400. #ifdef DEBUG
  401. printf("master secret: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  402. for(int i=0; i< SSL3_MASTER_SECRET_SIZE; i++){
  403. printf("%02x ", f->master_secret[i]);
  404. }
  405. printf("\n");
  406. printf("client random: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  407. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  408. printf("%02x ", f->client_random[i]);
  409. }
  410. printf("\n");
  411. printf("server random: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  412. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  413. printf("%02x ", f->server_random[i]);
  414. }
  415. printf("\n");
  416. printf("keyblock: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  417. for(int i=0; i< total_len; i++){
  418. printf("%02x ", key_block[i]);
  419. }
  420. printf("\n");
  421. #endif
  422. iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  423. write_key = key_block;
  424. read_key = key_block + key_len;
  425. write_iv = key_block + 2*key_len;
  426. read_iv = key_block + 2*key_len + iv_len;
  427. /* Initialize Cipher Contexts */
  428. r_ctx = EVP_CIPHER_CTX_new();
  429. w_ctx = EVP_CIPHER_CTX_new();
  430. EVP_CIPHER_CTX_init(r_ctx);
  431. EVP_CIPHER_CTX_init(w_ctx);
  432. w_ctx_srvr = EVP_CIPHER_CTX_new();
  433. r_ctx_srvr = EVP_CIPHER_CTX_new();
  434. EVP_CIPHER_CTX_init(w_ctx_srvr);
  435. EVP_CIPHER_CTX_init(r_ctx_srvr);
  436. /* Initialize MACs --- not needed for aes_256_gcm
  437. write_mac = key_block + 2*key_len + 2*iv_len;
  438. read_mac = key_block + 2*key_len + 2*iv_len + mac_len;
  439. read_mac_ctx = EVP_MD_CTX_create();
  440. write_mac_ctx = EVP_MD_CTX_create();
  441. read_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, read_mac, mac_len);
  442. write_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, write_mac, mac_len);
  443. EVP_DigestSignInit(read_mac_ctx, NULL, EVP_sha384(), NULL, read_mac_key);
  444. EVP_DigestSignInit(write_mac_ctx, NULL, EVP_sha384(), NULL, write_mac_key);
  445. EVP_PKEY_free(read_mac_key);
  446. EVP_PKEY_free(write_mac_key);*/
  447. #ifdef DEBUG
  448. {
  449. int i;
  450. fprintf(stderr, "EVP_CipherInit_ex(r_ctx,c,key=,iv=,which)\n");
  451. fprintf(stderr, "\tkey= ");
  452. for (i = 0; i < c->key_len; i++)
  453. fprintf(stderr, "%02x", read_key[i]);
  454. fprintf(stderr, "\n");
  455. fprintf(stderr, "\t iv= ");
  456. for (i = 0; i < c->iv_len; i++)
  457. fprintf(stderr, "%02x", read_iv[i]);
  458. fprintf(stderr, "\n");
  459. }
  460. {
  461. int i;
  462. fprintf(stderr, "EVP_CipherInit_ex(w_ctx,c,key=,iv=,which)\n");
  463. fprintf(stderr, "\tkey= ");
  464. for (i = 0; i < c->key_len; i++)
  465. fprintf(stderr, "%02x", write_key[i]);
  466. fprintf(stderr, "\n");
  467. fprintf(stderr, "\t iv= ");
  468. for (i = 0; i < c->iv_len; i++)
  469. fprintf(stderr, "%02x", write_iv[i]);
  470. fprintf(stderr, "\n");
  471. }
  472. #endif
  473. EVP_CipherInit_ex(r_ctx, c, NULL, read_key, NULL, 0);
  474. EVP_CipherInit_ex(w_ctx, c, NULL, write_key, NULL, 1);
  475. EVP_CipherInit_ex(w_ctx_srvr, c, NULL, read_key, NULL, 1);
  476. EVP_CipherInit_ex(r_ctx_srvr, c, NULL, write_key, NULL, 0);
  477. EVP_CIPHER_CTX_ctrl(r_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  478. EVP_CIPHER_CTX_ctrl(w_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  479. EVP_CIPHER_CTX_ctrl(w_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  480. EVP_CIPHER_CTX_ctrl(r_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  481. f->clnt_read_ctx = r_ctx;
  482. f->clnt_write_ctx = w_ctx;
  483. f->srvr_read_ctx = r_ctx_srvr;
  484. f->srvr_write_ctx = w_ctx_srvr;
  485. return 0;
  486. }
  487. // To avoid warnings about MAC paddings, use this to update contexts
  488. void update_context(flow *f, uint8_t *input, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  489. uint8_t *output = calloc(1, len+16+8);
  490. memcpy(output + EVP_GCM_TLS_EXPLICIT_IV_LEN, input, len);
  491. //If the original message was a decryption, this will be an necryption.
  492. //Incoming field stays the same
  493. encrypt(f, output, output, len+8, incoming, type, !enc);
  494. //revert the sequence number
  495. uint8_t *seq = incoming ? f->read_seq : f->write_seq;
  496. for(int i=7; i>=0; i--){
  497. --seq[i];
  498. if(seq[i] >= 0)
  499. break;
  500. else
  501. seq[i] = 0;
  502. }
  503. free(output);
  504. }
  505. /** Checks a handshake message to see if it is tagged or a
  506. * recognized flow. If the client random nonce is tagged,
  507. * adds the flow to the flow table to be tracked.
  508. *
  509. * Inputs:
  510. * info: the processed packet
  511. * f: the tagged flow
  512. *
  513. * Output:
  514. * none
  515. */
  516. void check_handshake(struct packet_info *info, flow f){
  517. FILE *fp;
  518. int res, i, code;
  519. uint8_t *hello_rand;
  520. const struct handshake_header *handshake_hdr;
  521. byte privkey[PTWIST_BYTES];
  522. byte key[16];
  523. uint8_t *p = info->app_data + RECORD_HEADER_LEN;
  524. handshake_hdr = (struct handshake_header*) p;
  525. code = handshake_hdr->type;
  526. if (code == 0x01){
  527. p += CLIENT_HELLO_HEADER_LEN;
  528. //now pointing to hello random :D
  529. hello_rand = p;
  530. p += 4; //skipping time bytes
  531. /* Load the private key */
  532. fp = fopen("privkey", "rb");
  533. if (fp == NULL) {
  534. perror("fopen");
  535. exit(1);
  536. }
  537. res = fread(privkey, PTWIST_BYTES, 1, fp);
  538. if (res < 1) {
  539. perror("fread");
  540. exit(1);
  541. }
  542. fclose(fp);
  543. /* check tag*/
  544. res = check_tag(key, privkey, p, (const byte *)"context", 7);
  545. if (res) {
  546. printf("Untagged\n");
  547. } else {
  548. fp = fopen("tags", "ab");
  549. if (fp == NULL) {
  550. perror("fopen");
  551. exit(1);
  552. }
  553. //Write tag to file
  554. for(i=0; i< 28; i++){
  555. fprintf(fp, "%02x ", p[i]);
  556. }
  557. fprintf(fp, "\n");
  558. fclose(fp);
  559. //Write key to file
  560. fp = fopen("sharedkey", "ab");
  561. if (fp == NULL) {
  562. perror("fopen");
  563. exit(1);
  564. }
  565. for(i=0; i<16;i++){
  566. fprintf(fp, "%02x", key[i]);
  567. }
  568. fprintf(fp, "\n");
  569. fclose(fp);
  570. printf("Received tagged flow! (key =");
  571. for(i=0; i<16;i++){
  572. printf(" %02x", key[i]);
  573. }
  574. printf(")\n");
  575. /* Save flow in table */
  576. flow *flow_ptr = add_flow(f);
  577. for(int i=0; i<16; i++){
  578. flow_ptr->key[i] = key[i];
  579. }
  580. memcpy(flow_ptr->client_random, hello_rand, SSL3_RANDOM_SIZE);
  581. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  582. printf("%02x ", hello_rand[i]);
  583. }
  584. printf("\n");
  585. printf("Saved new flow\n");
  586. }
  587. }
  588. }