flow.c 18 KB

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