flow.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include "flow.h"
  5. #include <openssl/evp.h>
  6. #include <openssl/dh.h>
  7. #include <openssl/bn.h>
  8. #include <openssl/err.h>
  9. #include <openssl/rand.h>
  10. #include <openssl/ssl.h>
  11. #include "slitheen.h"
  12. #define PRE_MASTER_LEN 256
  13. static flow_table *table;
  14. /* Initialize the table of tagged flows */
  15. int init_flow_table(void) {
  16. table = malloc(sizeof(flow_table));
  17. table->table = (flow *) malloc(sizeof(flow)*MAX_FLOWS);
  18. if( table->table == NULL){
  19. fprintf(stderr, "malloc failed.\n");
  20. return 1;
  21. }
  22. table->len = 0;
  23. table->max_len = MAX_FLOWS;
  24. return 0;
  25. }
  26. /* Add a new flow to the tagged flow table */
  27. flow *add_flow(flow newFlow) {
  28. flow *ptr;
  29. if(table->len == table->max_len){
  30. //grow_table();
  31. NULL;
  32. }
  33. printf("there are %d flows in the table\n", table->len);
  34. ptr = table->table + table->len;
  35. newFlow.state = TLS_CLNT_HELLO;
  36. newFlow.in_encrypted = 0;
  37. newFlow.out_encrypted = 0;
  38. newFlow.packet_chain = NULL;
  39. newFlow.finish_md_ctx = EVP_MD_CTX_create();
  40. const EVP_MD *md = EVP_sha384();
  41. EVP_DigestInit_ex(newFlow.finish_md_ctx, md, NULL);
  42. *ptr = newFlow;
  43. table->len ++;
  44. return ptr;
  45. }
  46. /* Updates the flow state */
  47. int update_flow(flow *f) {
  48. uint8_t *record;
  49. const struct record_header *record_hdr;
  50. const struct handshake_header *handshake_hdr;
  51. uint8_t *p = f->packet_chain->data;
  52. record_hdr = (struct record_header*) p;
  53. int record_len;
  54. int data_len;
  55. //printf("record version(major): %d.\n", (record_hdr->version&0xFF00)>>8);
  56. //printf("record version(minor): %d.\n", record_hdr->version&0xFF);
  57. //printf("record length: %d.\n", RECORD_LEN(record_hdr));
  58. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  59. data_len = f->packet_chain->data_len;
  60. packet *current = f->packet_chain;
  61. int incoming = current->incoming;
  62. record = calloc(1, record_len);
  63. for(int i=0; (i<data_len) && (i<record_len); i++){
  64. record[i] = p[i];
  65. }
  66. //printf("record len: %d, data_len: %d\n", record_len, data_len);
  67. while(record_len > data_len) {
  68. if(current->next == NULL){
  69. //printf("Don't have enought to reconstruct record\n");
  70. free(record);
  71. return 0;
  72. }
  73. if(current->next->seq_num != current->seq_num + current->len){
  74. printf("Missing packet: seq_num= %d, datalen= %d, nextseq= %d\n", current->seq_num, current->len, current->next->seq_num);
  75. free(record);
  76. return 0;
  77. }
  78. current = current->next;
  79. p = current->data;
  80. int i;
  81. for(i=0; (i<current->data_len) && (i+data_len < record_len); i++){
  82. record[data_len+i] = p[i];
  83. }
  84. //printf("Filled %d\n", i);
  85. data_len += current->data_len;
  86. }
  87. switch(record_hdr->type){
  88. case HS:
  89. p = record;
  90. p += RECORD_HEADER_LEN;
  91. //int size_hs = HANDSHAKE_MESSAGE_LEN(handshake_hdr);
  92. printf("Handshake Message:\n");
  93. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  94. decrypt_fin(f, p, record_len - RECORD_HEADER_LEN, incoming);
  95. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  96. }
  97. handshake_hdr = (struct handshake_header*) p;
  98. f->state = handshake_hdr->type;
  99. printf("record length: %d, hanshake length: %d\n", record_len, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  100. /* Now see if there's anything extra to do */
  101. switch(f->state){
  102. /* Checks to see if this is a possibly tagged hello msg */
  103. case TLS_CLNT_HELLO:
  104. /* Expecting server hello msg */
  105. printf("Received client hello!\n");
  106. update_finish_hash(f, p);
  107. break;
  108. case TLS_SERV_HELLO:
  109. extract_server_random(f, p);
  110. update_finish_hash(f, p);
  111. printf("Received server hello!\n");
  112. break;
  113. case TLS_NEW_SESS:
  114. update_finish_hash(f, p);
  115. printf("Received new session ticket!\n");
  116. break;
  117. case TLS_CERT:
  118. update_finish_hash(f, p);
  119. printf("Received certificate!\n");
  120. break;
  121. case TLS_SRVR_KEYEX:
  122. update_finish_hash(f, p);
  123. printf("Received server key exchange!\n");
  124. /* Need to extract server params */
  125. if(extract_parameters(f, p)){
  126. printf("Error extracting params\n");
  127. }
  128. if(compute_master_secret(f)){
  129. printf("Error computing master secret\n");
  130. }
  131. break;
  132. case TLS_CERT_REQ:
  133. update_finish_hash(f, p);
  134. printf("Received certificate request!\n");
  135. break;
  136. case TLS_SRVR_HELLO_DONE:
  137. update_finish_hash(f, p);
  138. printf("Received server hello done!\n");
  139. break;
  140. case TLS_CERT_VERIFY:
  141. update_finish_hash(f, p);
  142. printf("Received certificate verify!\n");
  143. break;
  144. case TLS_CLNT_KEYEX:
  145. update_finish_hash(f, p);
  146. printf("Received client key exchange!\n");
  147. break;
  148. case TLS_FINISHED:
  149. verify_finish_hash(f,p, incoming);
  150. update_finish_hash(f, p);
  151. printf("Received finished message!\n");
  152. break;
  153. default:
  154. printf("Error? %02x\n",p[0]);
  155. break;
  156. }
  157. break;
  158. case APP:
  159. printf("Application Data\n");
  160. break;
  161. case CCS:
  162. printf("Change of Cipher Spec\n");
  163. if(incoming){
  164. f->in_encrypted = 1;
  165. } else {
  166. f->out_encrypted = 1;
  167. }
  168. /*Initialize ciphers */
  169. init_ciphers(f);
  170. break;
  171. case ALERT:
  172. printf("Alert\n");
  173. break;
  174. case HB:
  175. printf("Heartbeat\n");
  176. break;
  177. default:
  178. printf("Error: Not a Record\n");
  179. //TODO: later figure this out, for now delete
  180. f->packet_chain = f->packet_chain->next;
  181. if( f->packet_chain != NULL){
  182. update_flow(f);
  183. }
  184. return 0;
  185. }
  186. if(record_len == data_len){
  187. /* record ended on packet boundary */
  188. //printf("record consumed packet\n");
  189. f->packet_chain = current->next;
  190. } else {
  191. /* need to update data */
  192. f->packet_chain = current; //TODO: make current
  193. current->data = current->data + (current->data_len - (data_len - record_len));
  194. current->data_len = data_len - record_len;
  195. update_flow(f);
  196. }
  197. free(record);
  198. return 0;
  199. }
  200. int remove_flow(int index) {
  201. int i;
  202. flow *ptr;
  203. if(index){
  204. ptr = table->table + index -1;
  205. for(i=0; i< table->len - index; i++){
  206. ptr += i;
  207. *ptr = *(ptr + 1);
  208. }
  209. table->len --;
  210. } else {
  211. return 1;
  212. }
  213. printf("flow removed!\n");
  214. return 0;
  215. }
  216. int grow_table() {
  217. return 0;
  218. }
  219. /** Returns the index of a flow in the flow table if
  220. * it exists, returns 0 if it is not present.
  221. */
  222. int check_flow(flow observed){
  223. /* Loop through flows in table and see if it exists */
  224. int i;
  225. flow *candidate = table->table;
  226. /* Check first in this direction */
  227. for(i=0; i<table->len; i++){
  228. candidate += i;
  229. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  230. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  231. if(candidate->src_port == observed.src_port){
  232. if(candidate->dst_port == observed.dst_port){
  233. return i+1;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. candidate = table->table;
  240. /* Then in the other direction */
  241. for(i=0; i<table->len; i++){
  242. candidate += i;
  243. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  244. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  245. if(candidate->src_port == observed.dst_port){
  246. if(candidate->dst_port == observed.src_port){
  247. return i+1;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. flow *get_flow(int index){
  256. if(index < table->len){
  257. return table->table+index;
  258. } else {
  259. return NULL;
  260. }
  261. }
  262. /* Adds a packet the flow's packet chain */
  263. int add_packet(flow *f, uint8_t *p){
  264. const struct ip_header *ip_hdr;
  265. const struct tcp_header *tcp_hdr;
  266. packet *new_packet = malloc(sizeof(packet));
  267. p += ETHER_HEADER_LEN; //skip ethernet header
  268. ip_hdr = (struct ip_header*) p;
  269. int size_ip = IP_HEADER_LEN(ip_hdr);
  270. if (ip_hdr->proto != IPPROTO_TCP){
  271. return 0;
  272. }
  273. p += size_ip; //skip IP header
  274. tcp_hdr = (struct tcp_header*) p;
  275. int size_tcp = TCP_HEADER_LEN(tcp_hdr);
  276. p += size_tcp;
  277. new_packet->seq_num = htonl(tcp_hdr->sequence_num);
  278. new_packet->len = htons(ip_hdr->len) - (size_ip + size_tcp);
  279. new_packet->data = p;
  280. new_packet->data_len = htons(ip_hdr->len) - (size_ip + size_tcp);
  281. new_packet->next = NULL;
  282. new_packet->incoming =
  283. (ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  284. /* Find appropriate place in chain */
  285. if(new_packet->data_len > 0){
  286. packet *previous = NULL;
  287. packet *next = f->packet_chain;
  288. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  289. //printf("next: %u <= new: %u\n", next->seq_num, new_packet->seq_num);
  290. previous = next;
  291. next = next->next;
  292. }
  293. //place packet after current
  294. if(previous == NULL){
  295. //goes at the beginning of chain
  296. new_packet->next = f->packet_chain;
  297. f->packet_chain = new_packet;
  298. } else {
  299. new_packet->next = next;
  300. previous->next = new_packet;
  301. }
  302. printf("Flow: %d > %d (%s)\n", ip_hdr->src.s_addr, ip_hdr->dst.s_addr, (new_packet->incoming)? "incoming":"outgoing");
  303. printf("ID number: %u\n", htonl(ip_hdr->id));
  304. printf("Sequence number: %u\n", htonl(tcp_hdr->sequence_num));
  305. printf("Acknowledgement number: %u\n", htonl(tcp_hdr->ack_num));
  306. printf("Length: %d\n", new_packet->data_len);
  307. }
  308. return 0;
  309. }
  310. /** UTILITY **/
  311. int update_finish_hash(flow *f, uint8_t *hs){
  312. //find handshake length
  313. const struct handshake_header *hs_hdr;
  314. uint8_t *p = hs;
  315. hs_hdr = (struct handshake_header*) p;
  316. uint32_t hs_len = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  317. EVP_DigestUpdate(f->finish_md_ctx, hs, hs_len+4);
  318. return 1;
  319. }
  320. int extract_parameters(flow *f, uint8_t *hs){
  321. DH *dh;
  322. uint8_t *p;
  323. long i;
  324. p = hs + HANDSHAKE_HEADER_LEN;
  325. if((dh = DH_new()) == NULL){
  326. return 1;
  327. }
  328. /* Extract prime modulus */
  329. n2s(p,i);
  330. if(!(dh->p = BN_bin2bn(p,i,NULL))){
  331. return 1;
  332. }
  333. p += i;
  334. /* Extract generator */
  335. n2s(p,i);
  336. if(!(dh->g = BN_bin2bn(p,i,NULL))){
  337. return 1;
  338. }
  339. p += i;
  340. /* Extract server public value */
  341. n2s(p,i);
  342. if(!(dh->pub_key = BN_bin2bn(p,i,NULL))){
  343. return 1;
  344. }
  345. f->dh = dh;
  346. printf("Param extraction: success!\n");
  347. return 0;
  348. }
  349. /* Decrypt the TLS FINISHED message
  350. * Verify that the data is:
  351. * PRF(master_secret, finished_label, Hash(handshake_messages))*/
  352. int decrypt_fin(flow *f, uint8_t *hs, int32_t len, int32_t incoming){
  353. //const struct handshake_header *hs_hdr;
  354. uint8_t *p = hs;
  355. //EVP_MD_CTX ctx;
  356. //uint8_t hash[EVP_MAX_MD_SIZE];
  357. //int32_t hash_len;
  358. //EVP_MD_CTX_init(&ctx);
  359. //decrypt
  360. EVP_CIPHER_CTX *ds = (incoming) ? f->read_ctx : f->write_ctx;
  361. if(ds == NULL){
  362. printf("FAIL\n");
  363. return 1;
  364. }
  365. if(ds->iv[EVP_GCM_TLS_FIXED_IV_LEN] == 0){
  366. //fill in rest of iv
  367. for(int i = EVP_GCM_TLS_FIXED_IV_LEN; i< ds->cipher->iv_len; i++){
  368. ds->iv[i] = p[i- EVP_GCM_TLS_FIXED_IV_LEN];
  369. }
  370. }
  371. //#ifdef KSSL_DEBUG
  372. {
  373. fprintf(stderr, "\t\tIV: ");
  374. for (int i = 0; i < ds->cipher->iv_len; i++)
  375. fprintf(stderr, "%02X", ds->iv[i]);
  376. fprintf(stderr, "\n");
  377. }
  378. //#endif /* KSSL_DEBUG */
  379. int32_t bs = EVP_CIPHER_block_size(ds->cipher);
  380. //padding stuff? TODO: understand this
  381. uint8_t buf[13];
  382. memset(buf, 0, 8);
  383. buf[8] = 0x16;
  384. buf[9] = 0x03;
  385. buf[10] = 0x03;
  386. buf[11] = 0x00; //len >> 8;
  387. buf[12] = 0x28; //len *0xff;
  388. printf("buf: \n");
  389. for(int z=0; z< 13; z++){
  390. printf("%02x ", buf[z]);
  391. }
  392. printf("\n");
  393. int32_t pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  394. 13, buf);
  395. printf("pad: %d\n", pad);
  396. int32_t n = EVP_Cipher(ds, p, p, len); //decrypt in place
  397. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  398. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  399. //print out decrypted record
  400. return 1;
  401. }
  402. int verify_finish_hash(flow *f, uint8_t *p, int32_t incoming){
  403. EVP_MD_CTX ctx;
  404. uint8_t hash[EVP_MAX_MD_SIZE];
  405. int32_t hash_len;
  406. EVP_MD_CTX_init(&ctx);
  407. //get header length
  408. struct handshake_header *hs_hdr;
  409. hs_hdr = (struct handshake_header*) p;
  410. uint32_t fin_length = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  411. p += HANDSHAKE_HEADER_LEN;
  412. //finalize hash of handshake msgs
  413. EVP_MD_CTX_copy_ex(&ctx, f->finish_md_ctx);
  414. EVP_DigestFinal_ex(&ctx, hash, &hash_len);
  415. //now use pseudorandom function
  416. uint8_t *output = calloc(1, fin_length);
  417. if(incoming){
  418. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  419. } else {
  420. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  421. }
  422. //now compare
  423. if(CRYPTO_memcmp(p, output, fin_length) != 0){
  424. printf("VERIFY FAILED\n");
  425. return 0;
  426. } else {
  427. printf("VERIFY PASSED\n");
  428. }
  429. return 1;
  430. }
  431. int compute_master_secret(flow *f){
  432. DH *dh_srvr = NULL;
  433. DH *dh_clnt = NULL;
  434. BN_CTX *ctx;
  435. BN_MONT_CTX *mont = NULL;
  436. BIGNUM *pub_key = NULL, *priv_key = NULL;
  437. ctx = BN_CTX_new();
  438. dh_srvr = f->dh;
  439. dh_clnt = DHparams_dup(dh_srvr);
  440. uint32_t l = dh_clnt->length ? dh_clnt->length : BN_num_bits(dh_clnt->p) - 1;
  441. int32_t bytes = (l+7) / 8;
  442. uint8_t *buf = (uint8_t *)OPENSSL_malloc(bytes);
  443. if (buf == NULL){
  444. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  445. return 1;
  446. }
  447. pub_key = BN_new();
  448. priv_key = BN_new();
  449. for(int i=0; i<bytes; i++){
  450. buf[i] = f->key[i%16];
  451. }
  452. if (!BN_bin2bn(buf, bytes, priv_key))
  453. return 1;
  454. {
  455. BIGNUM *prk;
  456. prk = priv_key;
  457. if (!dh_clnt->meth->bn_mod_exp(dh_clnt, pub_key, dh_clnt->g, prk, dh_clnt->p, ctx, mont)){
  458. printf("FAIL\n");
  459. return 1;
  460. }
  461. printf("here\n");
  462. }
  463. dh_clnt->pub_key = pub_key;
  464. dh_clnt->priv_key = priv_key;
  465. // Compute master key
  466. uint8_t *pre_master_secret = calloc(1, 256);//TODO: find right length
  467. DH_compute_key(pre_master_secret, dh_srvr->pub_key, dh_clnt);
  468. PRF(pre_master_secret, PRE_MASTER_LEN, 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);
  469. //remove pre_master_secret from memory
  470. memset(pre_master_secret, 0, PRE_MASTER_LEN);
  471. printf("master secret:\n");
  472. for(int i=0; i< 48; i++){
  473. printf("%02x ", f->master_secret[i]);
  474. }
  475. printf("\n");
  476. free(pre_master_secret);
  477. DH_free(dh_srvr);
  478. DH_free(dh_clnt);
  479. return 0;
  480. }
  481. void extract_server_random(flow *f, uint8_t *hs){
  482. uint8_t *p;
  483. p = hs + HANDSHAKE_HEADER_LEN;
  484. p+=2; //skip version
  485. memcpy(f->server_random, p, SSL3_RANDOM_SIZE);
  486. printf("got server random\n");
  487. }
  488. /* PRF using sha384, as defined in RFC 5246 */
  489. int PRF(uint8_t *secret, int32_t secret_len,
  490. uint8_t *seed1, int32_t seed1_len,
  491. uint8_t *seed2, int32_t seed2_len,
  492. uint8_t *seed3, int32_t seed3_len,
  493. uint8_t *seed4, int32_t seed4_len,
  494. uint8_t *output, int32_t output_len){
  495. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  496. EVP_PKEY *mac_key;
  497. const EVP_MD *md = EVP_sha384();
  498. uint8_t A[EVP_MAX_MD_SIZE];
  499. size_t len, A_len;
  500. int chunk = EVP_MD_size(md);
  501. int remaining = output_len;
  502. uint8_t *out = output;
  503. EVP_MD_CTX_init(&ctx);
  504. EVP_MD_CTX_init(&ctx_tmp);
  505. EVP_MD_CTX_init(&ctx_init);
  506. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  507. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  508. /* Calculate first A value */
  509. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  510. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  511. if(seed1 != NULL && seed1_len > 0){
  512. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  513. }
  514. if(seed2 != NULL && seed2_len > 0){
  515. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  516. }
  517. if(seed3 != NULL && seed3_len > 0){
  518. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  519. }
  520. if(seed4 != NULL && seed4_len > 0){
  521. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  522. }
  523. EVP_DigestSignFinal(&ctx, A, &A_len);
  524. //iterate until desired length is achieved
  525. while(remaining > 0){
  526. /* Now compute SHA384(secret, A+seed) */
  527. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  528. EVP_DigestSignUpdate(&ctx, A, A_len);
  529. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  530. if(seed1 != NULL && seed1_len > 0){
  531. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  532. }
  533. if(seed2 != NULL && seed2_len > 0){
  534. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  535. }
  536. if(seed3 != NULL && seed3_len > 0){
  537. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  538. }
  539. if(seed4 != NULL && seed4_len > 0){
  540. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  541. }
  542. if(remaining > chunk){
  543. EVP_DigestSignFinal(&ctx, out, &len);
  544. out += len;
  545. remaining -= len;
  546. /* Next A value */
  547. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  548. } else {
  549. EVP_DigestSignFinal(&ctx, A, &A_len);
  550. memcpy(out, A, remaining);
  551. remaining -= remaining;
  552. }
  553. }
  554. return 1;
  555. }
  556. /* After receiving change cipher spec, calculate keys from master secret */
  557. int init_ciphers(flow *f){
  558. EVP_CIPHER_CTX *r_ctx;
  559. EVP_CIPHER_CTX *w_ctx;
  560. const EVP_CIPHER *c = EVP_aes_256_gcm();
  561. /* Generate Keys */
  562. uint8_t *write_mac, *write_key, *write_iv;
  563. uint8_t *read_mac, *read_key, *read_iv;
  564. int32_t mac_len, key_len, iv_len;
  565. key_len = EVP_CIPHER_key_length(c);
  566. iv_len = EVP_CIPHER_iv_length(c); //EVP_GCM_TLS_FIXED_IV_LEN;
  567. mac_len = EVP_MD_size(EVP_get_digestbyname(SN_sha384));
  568. int32_t total_len = key_len + iv_len + mac_len;
  569. total_len *= 2;
  570. uint8_t *key_block = calloc(1, total_len);
  571. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE,
  572. TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  573. f->server_random, SSL3_RANDOM_SIZE,
  574. f->client_random, SSL3_RANDOM_SIZE,
  575. NULL, 0,
  576. key_block, total_len);
  577. printf("keyblock:\n");
  578. for(int i=0; i< total_len; i++){
  579. printf("%02x ", key_block[i]);
  580. }
  581. printf("\n");
  582. iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  583. write_key = key_block;
  584. read_key = key_block + key_len;
  585. write_iv = key_block + 2*key_len;
  586. read_iv = key_block + 2*key_len + iv_len;
  587. write_mac = key_block + 2*key_len + 2*iv_len;
  588. read_mac = key_block + 2*key_len + 2*iv_len + mac_len;
  589. /* Initialize Cipher Contexts */
  590. r_ctx = EVP_CIPHER_CTX_new();
  591. w_ctx = EVP_CIPHER_CTX_new();
  592. EVP_CIPHER_CTX_init(r_ctx);
  593. EVP_CIPHER_CTX_init(w_ctx);
  594. /*#ifdef KSSL_DEBUG
  595. {
  596. int i;
  597. fprintf(stderr, "EVP_CipherInit_ex(r_ctx,c,key=,iv=,which)\n");
  598. fprintf(stderr, "\tkey= ");
  599. for (i = 0; i < c->key_len; i++)
  600. fprintf(stderr, "%02x", read_key[i]);
  601. fprintf(stderr, "\n");
  602. fprintf(stderr, "\t iv= ");
  603. for (i = 0; i < c->iv_len; i++)
  604. fprintf(stderr, "%02x", read_iv[i]);
  605. fprintf(stderr, "\n");
  606. }
  607. //#endif KSSL_DEBUG_
  608. {
  609. int i;
  610. fprintf(stderr, "EVP_CipherInit_ex(w_ctx,c,key=,iv=,which)\n");
  611. fprintf(stderr, "\tkey= ");
  612. for (i = 0; i < c->key_len; i++)
  613. fprintf(stderr, "%02x", write_key[i]);
  614. fprintf(stderr, "\n");
  615. fprintf(stderr, "\t iv= ");
  616. for (i = 0; i < c->iv_len; i++)
  617. fprintf(stderr, "%02x", write_iv[i]);
  618. fprintf(stderr, "\n");
  619. }
  620. //#endif KSSL_DEBUG */
  621. EVP_CipherInit_ex(r_ctx, c, NULL, read_key, NULL, 0);
  622. EVP_CipherInit_ex(w_ctx, c, NULL, write_key, NULL, 0);
  623. EVP_CIPHER_CTX_ctrl(r_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  624. EVP_CIPHER_CTX_ctrl(w_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  625. f->read_ctx = r_ctx;
  626. f->write_ctx = w_ctx;
  627. }