crypto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. free(output);
  193. EVP_MD_CTX_cleanup(&ctx);
  194. return 0;
  195. }
  196. /** Computes the TLS master secret from the decoy server's
  197. * public key parameters and the leaked secret from the
  198. * extracted Slitheen tag
  199. *
  200. * Input:
  201. * f: the tagged flow
  202. *
  203. * Output:
  204. * 0 on success, 1 on failure
  205. */
  206. int compute_master_secret(flow *f){
  207. 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);
  208. DH *dh_srvr = NULL;
  209. DH *dh_clnt = NULL;
  210. BN_CTX *ctx;
  211. BN_MONT_CTX *mont = NULL;
  212. BIGNUM *pub_key = NULL, *priv_key = NULL;
  213. ctx = BN_CTX_new();
  214. dh_srvr = f->dh;
  215. dh_clnt = DHparams_dup(dh_srvr);
  216. uint32_t l = dh_clnt->length ? dh_clnt->length : BN_num_bits(dh_clnt->p) - 1;
  217. int32_t bytes = (l+7) / 8;
  218. printf("length of dh param: %d\n", bytes);
  219. uint8_t *buf = (uint8_t *)OPENSSL_malloc(bytes);
  220. if (buf == NULL){
  221. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  222. goto err;
  223. }
  224. pub_key = BN_new();
  225. priv_key = BN_new();
  226. printf("key =");
  227. for(int i=0; i< 16; i++)
  228. printf(" %02x", f->key[i]);
  229. printf("\n");
  230. PRF(f->key, 16,
  231. (uint8_t *) SLITHEEN_KEYGEN_CONST, SLITHEEN_KEYGEN_CONST_SIZE,
  232. NULL, 0, NULL, 0, NULL, 0,
  233. buf, bytes);
  234. //#ifdef DEBUG
  235. printf("Generated the following rand bytes: ");
  236. for(int i=0; i< bytes; i++){
  237. printf(" %02x ", buf[i]);
  238. }
  239. printf("\n");
  240. //#endif
  241. if (!BN_bin2bn(buf, bytes, priv_key))
  242. goto err;
  243. {
  244. BIGNUM *prk;
  245. prk = priv_key;
  246. if (!dh_clnt->meth->bn_mod_exp(dh_clnt, pub_key, dh_clnt->g, prk, dh_clnt->p, ctx, mont)){
  247. goto err;
  248. }
  249. }
  250. dh_clnt->pub_key = pub_key;
  251. dh_clnt->priv_key = priv_key;
  252. // Compute master key
  253. uint8_t *pre_master_secret = calloc(1, PRE_MASTER_MAX_LEN);//TODO: find right length
  254. int32_t pre_master_len = DH_compute_key(pre_master_secret, dh_srvr->pub_key, dh_clnt);
  255. 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);
  256. if(f->current_session != NULL)
  257. memcpy(f->current_session, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  258. #ifdef DEBUG
  259. fprintf(stdout, "Premaster Secret:\n");
  260. BIO_dump_fp(stdout, (char *)pre_master_secret, pre_master_len);
  261. fprintf(stdout, "Client Random:\n");
  262. BIO_dump_fp(stdout, (char *)f->client_random, SSL3_RANDOM_SIZE);
  263. fprintf(stdout, "Server Random:\n");
  264. BIO_dump_fp(stdout, (char *)f->server_random, SSL3_RANDOM_SIZE);
  265. fprintf(stdout, "Master Secret:\n");
  266. BIO_dump_fp(stdout, (char *)f->master_secret, SSL3_MASTER_SECRET_SIZE);
  267. #endif
  268. //remove pre_master_secret from memory
  269. memset(pre_master_secret, 0, PRE_MASTER_MAX_LEN);
  270. err:
  271. if((pub_key != NULL) && (dh_srvr == NULL)){
  272. BN_free(pub_key);
  273. }
  274. if((priv_key != NULL) && (dh_clnt == NULL)){
  275. BN_free(priv_key);
  276. }
  277. BN_CTX_free(ctx);
  278. OPENSSL_free(buf);
  279. free(pre_master_secret);
  280. DH_free(dh_srvr);
  281. DH_free(dh_clnt);
  282. return 0;
  283. }
  284. /** Saves the random none from the server hello message
  285. *
  286. * Inputs:
  287. * f: the tagged flow
  288. * hs: a pointer to the beginning of the server hello msg
  289. *
  290. * Output:
  291. * none
  292. */
  293. void extract_server_random(flow *f, uint8_t *hs){
  294. uint8_t *p;
  295. p = hs + HANDSHAKE_HEADER_LEN;
  296. p+=2; //skip version
  297. memcpy(f->server_random, p, SSL3_RANDOM_SIZE);
  298. }
  299. /** PRF using sha384, as defined in RFC 5246
  300. *
  301. * Inputs:
  302. * secret: the master secret used to sign the hash
  303. * secret_len: the length of the master secret
  304. * seed{1, ..., 4}: seed values that are virtually
  305. * concatenated
  306. * seed{1,...4}_len: length of the seeds
  307. * output: a pointer to the output of the PRF
  308. * output_len: the number of desired bytes
  309. *
  310. * Output:
  311. * 0 on success, 1 on failure
  312. */
  313. int PRF(uint8_t *secret, int32_t secret_len,
  314. uint8_t *seed1, int32_t seed1_len,
  315. uint8_t *seed2, int32_t seed2_len,
  316. uint8_t *seed3, int32_t seed3_len,
  317. uint8_t *seed4, int32_t seed4_len,
  318. uint8_t *output, int32_t output_len){
  319. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  320. EVP_PKEY *mac_key;
  321. const EVP_MD *md = EVP_sha384();
  322. uint8_t A[EVP_MAX_MD_SIZE];
  323. size_t len, A_len;
  324. int chunk = EVP_MD_size(md);
  325. int remaining = output_len;
  326. uint8_t *out = output;
  327. EVP_MD_CTX_init(&ctx);
  328. EVP_MD_CTX_init(&ctx_tmp);
  329. EVP_MD_CTX_init(&ctx_init);
  330. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  331. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  332. /* Calculate first A value */
  333. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  334. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  335. if(seed1 != NULL && seed1_len > 0){
  336. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  337. }
  338. if(seed2 != NULL && seed2_len > 0){
  339. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  340. }
  341. if(seed3 != NULL && seed3_len > 0){
  342. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  343. }
  344. if(seed4 != NULL && seed4_len > 0){
  345. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  346. }
  347. EVP_DigestSignFinal(&ctx, A, &A_len);
  348. //iterate until desired length is achieved
  349. while(remaining > 0){
  350. /* Now compute SHA384(secret, A+seed) */
  351. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  352. EVP_DigestSignUpdate(&ctx, A, A_len);
  353. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  354. if(seed1 != NULL && seed1_len > 0){
  355. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  356. }
  357. if(seed2 != NULL && seed2_len > 0){
  358. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  359. }
  360. if(seed3 != NULL && seed3_len > 0){
  361. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  362. }
  363. if(seed4 != NULL && seed4_len > 0){
  364. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  365. }
  366. if(remaining > chunk){
  367. EVP_DigestSignFinal(&ctx, out, &len);
  368. out += len;
  369. remaining -= len;
  370. /* Next A value */
  371. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  372. } else {
  373. EVP_DigestSignFinal(&ctx, A, &A_len);
  374. memcpy(out, A, remaining);
  375. remaining -= remaining;
  376. }
  377. }
  378. EVP_PKEY_free(mac_key);
  379. EVP_MD_CTX_cleanup(&ctx);
  380. EVP_MD_CTX_cleanup(&ctx_tmp);
  381. EVP_MD_CTX_cleanup(&ctx_init);
  382. OPENSSL_cleanse(A, sizeof(A));
  383. return 0;
  384. }
  385. /** After receiving change cipher spec, calculate keys from master secret
  386. *
  387. * Input:
  388. * f: the tagged flow
  389. *
  390. * Output:
  391. * 0 on success, 1 on failure
  392. */
  393. int init_ciphers(flow *f){
  394. EVP_CIPHER_CTX *r_ctx;
  395. EVP_CIPHER_CTX *w_ctx;
  396. EVP_CIPHER_CTX *w_ctx_srvr;
  397. EVP_CIPHER_CTX *r_ctx_srvr;
  398. const EVP_CIPHER *c = EVP_aes_256_gcm();
  399. /* Generate Keys */
  400. uint8_t *write_key, *write_iv;
  401. uint8_t *read_key, *read_iv;
  402. int32_t mac_len, key_len, iv_len;
  403. key_len = EVP_CIPHER_key_length(c);
  404. iv_len = EVP_CIPHER_iv_length(c); //EVP_GCM_TLS_FIXED_IV_LEN;
  405. mac_len = EVP_MD_size(EVP_sha384());
  406. int32_t total_len = key_len + iv_len + mac_len;
  407. total_len *= 2;
  408. uint8_t *key_block = calloc(1, total_len);
  409. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE,
  410. (uint8_t *) TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  411. f->server_random, SSL3_RANDOM_SIZE,
  412. f->client_random, SSL3_RANDOM_SIZE,
  413. NULL, 0,
  414. key_block, total_len);
  415. #ifdef DEBUG
  416. printf("master secret: (%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< SSL3_MASTER_SECRET_SIZE; i++){
  418. printf("%02x ", f->master_secret[i]);
  419. }
  420. printf("\n");
  421. printf("client random: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  422. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  423. printf("%02x ", f->client_random[i]);
  424. }
  425. printf("\n");
  426. printf("server random: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  427. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  428. printf("%02x ", f->server_random[i]);
  429. }
  430. printf("\n");
  431. printf("keyblock: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  432. for(int i=0; i< total_len; i++){
  433. printf("%02x ", key_block[i]);
  434. }
  435. printf("\n");
  436. #endif
  437. iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  438. write_key = key_block;
  439. read_key = key_block + key_len;
  440. write_iv = key_block + 2*key_len;
  441. read_iv = key_block + 2*key_len + iv_len;
  442. /* Initialize Cipher Contexts */
  443. r_ctx = EVP_CIPHER_CTX_new();
  444. w_ctx = EVP_CIPHER_CTX_new();
  445. EVP_CIPHER_CTX_init(r_ctx);
  446. EVP_CIPHER_CTX_init(w_ctx);
  447. w_ctx_srvr = EVP_CIPHER_CTX_new();
  448. r_ctx_srvr = EVP_CIPHER_CTX_new();
  449. EVP_CIPHER_CTX_init(w_ctx_srvr);
  450. EVP_CIPHER_CTX_init(r_ctx_srvr);
  451. /* Initialize MACs --- not needed for aes_256_gcm
  452. write_mac = key_block + 2*key_len + 2*iv_len;
  453. read_mac = key_block + 2*key_len + 2*iv_len + mac_len;
  454. read_mac_ctx = EVP_MD_CTX_create();
  455. write_mac_ctx = EVP_MD_CTX_create();
  456. read_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, read_mac, mac_len);
  457. write_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, write_mac, mac_len);
  458. EVP_DigestSignInit(read_mac_ctx, NULL, EVP_sha384(), NULL, read_mac_key);
  459. EVP_DigestSignInit(write_mac_ctx, NULL, EVP_sha384(), NULL, write_mac_key);
  460. EVP_PKEY_free(read_mac_key);
  461. EVP_PKEY_free(write_mac_key);*/
  462. #ifdef DEBUG
  463. {
  464. int i;
  465. fprintf(stderr, "EVP_CipherInit_ex(r_ctx,c,key=,iv=,which)\n");
  466. fprintf(stderr, "\tkey= ");
  467. for (i = 0; i < c->key_len; i++)
  468. fprintf(stderr, "%02x", read_key[i]);
  469. fprintf(stderr, "\n");
  470. fprintf(stderr, "\t iv= ");
  471. for (i = 0; i < c->iv_len; i++)
  472. fprintf(stderr, "%02x", read_iv[i]);
  473. fprintf(stderr, "\n");
  474. }
  475. {
  476. int i;
  477. fprintf(stderr, "EVP_CipherInit_ex(w_ctx,c,key=,iv=,which)\n");
  478. fprintf(stderr, "\tkey= ");
  479. for (i = 0; i < c->key_len; i++)
  480. fprintf(stderr, "%02x", write_key[i]);
  481. fprintf(stderr, "\n");
  482. fprintf(stderr, "\t iv= ");
  483. for (i = 0; i < c->iv_len; i++)
  484. fprintf(stderr, "%02x", write_iv[i]);
  485. fprintf(stderr, "\n");
  486. }
  487. #endif
  488. EVP_CipherInit_ex(r_ctx, c, NULL, read_key, NULL, 0);
  489. EVP_CipherInit_ex(w_ctx, c, NULL, write_key, NULL, 1);
  490. EVP_CipherInit_ex(w_ctx_srvr, c, NULL, read_key, NULL, 1);
  491. EVP_CipherInit_ex(r_ctx_srvr, c, NULL, write_key, NULL, 0);
  492. EVP_CIPHER_CTX_ctrl(r_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  493. EVP_CIPHER_CTX_ctrl(w_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  494. EVP_CIPHER_CTX_ctrl(w_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  495. EVP_CIPHER_CTX_ctrl(r_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  496. f->clnt_read_ctx = r_ctx;
  497. f->clnt_write_ctx = w_ctx;
  498. f->srvr_read_ctx = r_ctx_srvr;
  499. f->srvr_write_ctx = w_ctx_srvr;
  500. free(key_block);
  501. return 0;
  502. }
  503. // To avoid warnings about MAC paddings, use this to update contexts
  504. void update_context(flow *f, uint8_t *input, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  505. uint8_t *output = calloc(1, len+16+8);
  506. memcpy(output + EVP_GCM_TLS_EXPLICIT_IV_LEN, input, len);
  507. //If the original message was a decryption, this will be an necryption.
  508. //Incoming field stays the same
  509. encrypt(f, output, output, len+8, incoming, type, !enc);
  510. //revert the sequence number
  511. uint8_t *seq = incoming ? f->read_seq : f->write_seq;
  512. for(int i=7; i>=0; i--){
  513. --seq[i];
  514. if(seq[i] >= 0)
  515. break;
  516. else
  517. seq[i] = 0;
  518. }
  519. free(output);
  520. }
  521. /** Checks a handshake message to see if it is tagged or a
  522. * recognized flow. If the client random nonce is tagged,
  523. * adds the flow to the flow table to be tracked.
  524. *
  525. * Inputs:
  526. * info: the processed packet
  527. * f: the tagged flow
  528. *
  529. * Output:
  530. * none
  531. */
  532. void check_handshake(struct packet_info *info, flow f){
  533. FILE *fp;
  534. int res, i, code;
  535. uint8_t *hello_rand;
  536. const struct handshake_header *handshake_hdr;
  537. byte privkey[PTWIST_BYTES];
  538. byte key[16];
  539. uint8_t *p = info->app_data + RECORD_HEADER_LEN;
  540. handshake_hdr = (struct handshake_header*) p;
  541. code = handshake_hdr->type;
  542. if (code == 0x01){
  543. p += CLIENT_HELLO_HEADER_LEN;
  544. //now pointing to hello random :D
  545. hello_rand = p;
  546. p += 4; //skipping time bytes
  547. /* Load the private key */
  548. fp = fopen("privkey", "rb");
  549. if (fp == NULL) {
  550. perror("fopen");
  551. exit(1);
  552. }
  553. res = fread(privkey, PTWIST_BYTES, 1, fp);
  554. if (res < 1) {
  555. perror("fread");
  556. exit(1);
  557. }
  558. fclose(fp);
  559. /* check tag*/
  560. res = check_tag(key, privkey, p, (const byte *)"context", 7);
  561. if (res) {
  562. printf("Untagged\n");
  563. } else {
  564. fp = fopen("tags", "ab");
  565. if (fp == NULL) {
  566. perror("fopen");
  567. exit(1);
  568. }
  569. //Write tag to file
  570. for(i=0; i< 28; i++){
  571. fprintf(fp, "%02x ", p[i]);
  572. }
  573. fprintf(fp, "\n");
  574. fclose(fp);
  575. //Write key to file
  576. fp = fopen("sharedkey", "ab");
  577. if (fp == NULL) {
  578. perror("fopen");
  579. exit(1);
  580. }
  581. for(i=0; i<16;i++){
  582. fprintf(fp, "%02x", key[i]);
  583. }
  584. fprintf(fp, "\n");
  585. fclose(fp);
  586. printf("Received tagged flow! (key =");
  587. for(i=0; i<16;i++){
  588. printf(" %02x", key[i]);
  589. }
  590. printf(")\n");
  591. /* Save flow in table */
  592. flow *flow_ptr = add_flow(f);
  593. for(int i=0; i<16; i++){
  594. flow_ptr->key[i] = key[i];
  595. }
  596. memcpy(flow_ptr->client_random, hello_rand, SSL3_RANDOM_SIZE);
  597. for(int i=0; i< SSL3_RANDOM_SIZE; i++){
  598. printf("%02x ", hello_rand[i]);
  599. }
  600. printf("\n");
  601. printf("Saved new flow\n");
  602. }
  603. }
  604. }