flow.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include "flow.h"
  7. #include "crypto.h"
  8. #include "slitheen.h"
  9. static flow_table *table;
  10. /* Initialize the table of tagged flows */
  11. int init_flow_table(void) {
  12. table = malloc(sizeof(flow_table));
  13. table->table = (flow *) malloc(sizeof(flow)*MAX_FLOWS);
  14. if( table->table == NULL){
  15. fprintf(stderr, "malloc failed.\n");
  16. return 1;
  17. }
  18. table->len = 0;
  19. table->max_len = MAX_FLOWS;
  20. return 0;
  21. }
  22. /* Add a new flow to the tagged flow table */
  23. flow *add_flow(flow newFlow) {
  24. flow *ptr;
  25. if(table->len == table->max_len){
  26. //grow_table();
  27. NULL;
  28. }
  29. printf("there are %d flows in the table\n", table->len);
  30. ptr = table->table + table->len;
  31. newFlow.state = TLS_CLNT_HELLO;
  32. newFlow.in_encrypted = 0;
  33. newFlow.out_encrypted = 0;
  34. newFlow.application = 0;
  35. newFlow.packet_chain = NULL;
  36. newFlow.censored_queue = calloc(1,2048);
  37. newFlow.finish_md_ctx = EVP_MD_CTX_create();
  38. const EVP_MD *md = EVP_sha384();
  39. EVP_DigestInit_ex(newFlow.finish_md_ctx, md, NULL);
  40. memset(newFlow.read_seq, 0, 8);
  41. memset(newFlow.write_seq, 0, 8);
  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. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16);
  95. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  96. if(incoming) f->in_encrypted = 2;
  97. else f->out_encrypted = 2;
  98. }
  99. handshake_hdr = (struct handshake_header*) p;
  100. f->state = handshake_hdr->type;
  101. printf("record length: %d, hanshake length: %d\n", record_len, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  102. /* Now see if there's anything extra to do */
  103. switch(f->state){
  104. /* Checks to see if this is a possibly tagged hello msg */
  105. case TLS_CLNT_HELLO:
  106. /* Expecting server hello msg */
  107. printf("Received client hello!\n");
  108. update_finish_hash(f, p);
  109. break;
  110. case TLS_SERV_HELLO:
  111. extract_server_random(f, p);
  112. update_finish_hash(f, p);
  113. printf("Received server hello!\n");
  114. break;
  115. case TLS_NEW_SESS:
  116. update_finish_hash(f, p);
  117. printf("Received new session ticket!\n");
  118. break;
  119. case TLS_CERT:
  120. update_finish_hash(f, p);
  121. printf("Received certificate!\n");
  122. break;
  123. case TLS_SRVR_KEYEX:
  124. update_finish_hash(f, p);
  125. printf("Received server key exchange!\n");
  126. /* Need to extract server params */
  127. if(extract_parameters(f, p)){
  128. printf("Error extracting params\n");
  129. }
  130. if(compute_master_secret(f)){
  131. printf("Error computing master secret\n");
  132. }
  133. break;
  134. case TLS_CERT_REQ:
  135. update_finish_hash(f, p);
  136. printf("Received certificate request!\n");
  137. break;
  138. case TLS_SRVR_HELLO_DONE:
  139. update_finish_hash(f, p);
  140. printf("Received server hello done!\n");
  141. break;
  142. case TLS_CERT_VERIFY:
  143. update_finish_hash(f, p);
  144. printf("Received certificate verify!\n");
  145. break;
  146. case TLS_CLNT_KEYEX:
  147. update_finish_hash(f, p);
  148. printf("Received client key exchange!\n");
  149. break;
  150. case TLS_FINISHED:
  151. verify_finish_hash(f,p, incoming);
  152. update_finish_hash(f, p);
  153. printf("Received finished message!\n");
  154. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  155. printf("Handshake complete!\n");
  156. f->application = 1;
  157. }
  158. break;
  159. default:
  160. printf("Error? %02x\n",p[0]);
  161. break;
  162. }
  163. break;
  164. case APP:
  165. printf("Application Data\n");
  166. //decrypt this
  167. break;
  168. case CCS:
  169. printf("Change of Cipher Spec\n");
  170. if(incoming){
  171. f->in_encrypted = 1;
  172. } else {
  173. f->out_encrypted = 1;
  174. }
  175. /*Initialize ciphers */
  176. init_ciphers(f);
  177. break;
  178. case ALERT:
  179. p = record;
  180. p += RECORD_HEADER_LEN;
  181. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16);
  182. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  183. printf("Alert: %02x %02x\n", p[0], p[1]);
  184. break;
  185. case HB:
  186. printf("Heartbeat\n");
  187. break;
  188. default:
  189. printf("Error: Not a Record\n");
  190. //TODO: later figure this out, for now delete
  191. f->packet_chain = f->packet_chain->next;
  192. if( f->packet_chain != NULL){
  193. update_flow(f);
  194. }
  195. return 0;
  196. }
  197. f->seq_num = current->seq_num;
  198. if(record_len == data_len){
  199. /* record ended on packet boundary */
  200. f->packet_chain = current->next;
  201. } else {
  202. /* need to update data */
  203. f->packet_chain = current; //TODO: make current
  204. current->data = current->data + (current->data_len - (data_len - record_len));
  205. current->data_len = data_len - record_len;
  206. update_flow(f);
  207. }
  208. free(record);
  209. return 0;
  210. }
  211. int remove_flow(int index) {
  212. int i;
  213. flow *ptr;
  214. if(index){
  215. ptr = table->table + index -1;
  216. for(i=0; i< table->len - index; i++){
  217. ptr += i;
  218. *ptr = *(ptr + 1);
  219. }
  220. table->len --;
  221. } else {
  222. return 1;
  223. }
  224. printf("flow removed!\n");
  225. return 0;
  226. }
  227. int grow_table() {
  228. return 0;
  229. }
  230. /** Returns the index of a flow in the flow table if
  231. * it exists, returns 0 if it is not present.
  232. */
  233. int check_flow(flow observed){
  234. /* Loop through flows in table and see if it exists */
  235. int i;
  236. flow *candidate = table->table;
  237. /* Check first in this direction */
  238. for(i=0; i<table->len; i++){
  239. candidate += i;
  240. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  241. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  242. if(candidate->src_port == observed.src_port){
  243. if(candidate->dst_port == observed.dst_port){
  244. return i+1;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. candidate = table->table;
  251. /* Then in the other direction */
  252. for(i=0; i<table->len; i++){
  253. candidate += i;
  254. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  255. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  256. if(candidate->src_port == observed.dst_port){
  257. if(candidate->dst_port == observed.src_port){
  258. return i+1;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. return 0;
  265. }
  266. flow *get_flow(int index){
  267. if(index < table->len){
  268. return table->table+index;
  269. } else {
  270. return NULL;
  271. }
  272. }
  273. /* Adds a packet the flow's packet chain */
  274. int add_packet(flow *f, uint8_t *p){
  275. const struct ip_header *ip_hdr;
  276. const struct tcp_header *tcp_hdr;
  277. packet *new_packet = malloc(sizeof(packet));
  278. p += ETHER_HEADER_LEN; //skip ethernet header
  279. ip_hdr = (struct ip_header*) p;
  280. int size_ip = IP_HEADER_LEN(ip_hdr);
  281. if (ip_hdr->proto != IPPROTO_TCP){
  282. return 0;
  283. }
  284. p += size_ip; //skip IP header
  285. tcp_hdr = (struct tcp_header*) p;
  286. int size_tcp = TCP_HEADER_LEN(tcp_hdr);
  287. p += size_tcp;
  288. new_packet->seq_num = htonl(tcp_hdr->sequence_num);
  289. new_packet->len = htons(ip_hdr->len) - (size_ip + size_tcp);
  290. new_packet->data = p;
  291. new_packet->data_len = htons(ip_hdr->len) - (size_ip + size_tcp);
  292. new_packet->next = NULL;
  293. new_packet->incoming =
  294. (ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  295. /* Find appropriate place in chain */
  296. if(new_packet->data_len > 0){
  297. packet *previous = NULL;
  298. packet *next = f->packet_chain;
  299. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  300. //printf("next: %u <= new: %u\n", next->seq_num, new_packet->seq_num);
  301. previous = next;
  302. next = next->next;
  303. }
  304. //place packet after current
  305. if(previous == NULL){
  306. //goes at the beginning of chain
  307. new_packet->next = f->packet_chain;
  308. f->packet_chain = new_packet;
  309. } else {
  310. new_packet->next = next;
  311. previous->next = new_packet;
  312. }
  313. /*printf("Flow: %d > %d (%s)\n", ip_hdr->src.s_addr, ip_hdr->dst.s_addr, (new_packet->incoming)? "incoming":"outgoing");
  314. printf("ID number: %u\n", htonl(ip_hdr->id));
  315. printf("Sequence number: %u\n", htonl(tcp_hdr->sequence_num));
  316. printf("Acknowledgement number: %u\n", htonl(tcp_hdr->ack_num));
  317. printf("Length: %d\n", new_packet->data_len);*/
  318. }
  319. return 0;
  320. }