flow.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.finish_md_ctx = EVP_MD_CTX_create();
  37. const EVP_MD *md = EVP_sha384();
  38. EVP_DigestInit_ex(newFlow.finish_md_ctx, md, NULL);
  39. memset(newFlow.read_seq, 0, 8);
  40. memset(newFlow.write_seq, 0, 8);
  41. *ptr = newFlow;
  42. table->len ++;
  43. return ptr;
  44. }
  45. /* Updates the flow state */
  46. int update_flow(flow *f) {
  47. uint8_t *record;
  48. const struct record_header *record_hdr;
  49. const struct handshake_header *handshake_hdr;
  50. uint8_t *p = f->packet_chain->data;
  51. record_hdr = (struct record_header*) p;
  52. int record_len;
  53. int data_len;
  54. //printf("record version(major): %d.\n", (record_hdr->version&0xFF00)>>8);
  55. //printf("record version(minor): %d.\n", record_hdr->version&0xFF);
  56. //printf("record length: %d.\n", RECORD_LEN(record_hdr));
  57. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  58. data_len = f->packet_chain->data_len;
  59. packet *current = f->packet_chain;
  60. int incoming = current->incoming;
  61. record = calloc(1, record_len);
  62. for(int i=0; (i<data_len) && (i<record_len); i++){
  63. record[i] = p[i];
  64. }
  65. //printf("record len: %d, data_len: %d\n", record_len, data_len);
  66. while(record_len > data_len) {
  67. if(current->next == NULL){
  68. //printf("Don't have enought to reconstruct record\n");
  69. free(record);
  70. return 0;
  71. }
  72. if(current->next->seq_num != current->seq_num + current->len){
  73. printf("Missing packet: seq_num= %d, datalen= %d, nextseq= %d\n", current->seq_num, current->len, current->next->seq_num);
  74. free(record);
  75. return 0;
  76. }
  77. current = current->next;
  78. p = current->data;
  79. int i;
  80. for(i=0; (i<current->data_len) && (i+data_len < record_len); i++){
  81. record[data_len+i] = p[i];
  82. }
  83. //printf("Filled %d\n", i);
  84. data_len += current->data_len;
  85. }
  86. switch(record_hdr->type){
  87. case HS:
  88. p = record;
  89. p += RECORD_HEADER_LEN;
  90. //int size_hs = HANDSHAKE_MESSAGE_LEN(handshake_hdr);
  91. printf("Handshake Message:\n");
  92. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  93. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16);
  94. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  95. if(incoming) f->in_encrypted = 2;
  96. else f->out_encrypted = 2;
  97. }
  98. handshake_hdr = (struct handshake_header*) p;
  99. f->state = handshake_hdr->type;
  100. printf("record length: %d, hanshake length: %d\n", record_len, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  101. /* Now see if there's anything extra to do */
  102. switch(f->state){
  103. /* Checks to see if this is a possibly tagged hello msg */
  104. case TLS_CLNT_HELLO:
  105. /* Expecting server hello msg */
  106. printf("Received client hello!\n");
  107. update_finish_hash(f, p);
  108. break;
  109. case TLS_SERV_HELLO:
  110. extract_server_random(f, p);
  111. update_finish_hash(f, p);
  112. printf("Received server hello!\n");
  113. break;
  114. case TLS_NEW_SESS:
  115. update_finish_hash(f, p);
  116. printf("Received new session ticket!\n");
  117. break;
  118. case TLS_CERT:
  119. update_finish_hash(f, p);
  120. printf("Received certificate!\n");
  121. break;
  122. case TLS_SRVR_KEYEX:
  123. update_finish_hash(f, p);
  124. printf("Received server key exchange!\n");
  125. /* Need to extract server params */
  126. if(extract_parameters(f, p)){
  127. printf("Error extracting params\n");
  128. }
  129. if(compute_master_secret(f)){
  130. printf("Error computing master secret\n");
  131. }
  132. break;
  133. case TLS_CERT_REQ:
  134. update_finish_hash(f, p);
  135. printf("Received certificate request!\n");
  136. break;
  137. case TLS_SRVR_HELLO_DONE:
  138. update_finish_hash(f, p);
  139. printf("Received server hello done!\n");
  140. break;
  141. case TLS_CERT_VERIFY:
  142. update_finish_hash(f, p);
  143. printf("Received certificate verify!\n");
  144. break;
  145. case TLS_CLNT_KEYEX:
  146. update_finish_hash(f, p);
  147. printf("Received client key exchange!\n");
  148. break;
  149. case TLS_FINISHED:
  150. verify_finish_hash(f,p, incoming);
  151. update_finish_hash(f, p);
  152. printf("Received finished message!\n");
  153. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  154. printf("Handshake complete!\n");
  155. f->application = 1;
  156. }
  157. break;
  158. default:
  159. printf("Error? %02x\n",p[0]);
  160. break;
  161. }
  162. break;
  163. case APP:
  164. printf("Application Data\n");
  165. //decrypt this
  166. break;
  167. case CCS:
  168. printf("Change of Cipher Spec\n");
  169. if(incoming){
  170. f->in_encrypted = 1;
  171. } else {
  172. f->out_encrypted = 1;
  173. }
  174. /*Initialize ciphers */
  175. init_ciphers(f);
  176. break;
  177. case ALERT:
  178. p = record;
  179. p += RECORD_HEADER_LEN;
  180. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16);
  181. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  182. printf("Alert: %02x %02x\n", p[0], p[1]);
  183. break;
  184. case HB:
  185. printf("Heartbeat\n");
  186. break;
  187. default:
  188. printf("Error: Not a Record\n");
  189. //TODO: later figure this out, for now delete
  190. f->packet_chain = f->packet_chain->next;
  191. if( f->packet_chain != NULL){
  192. update_flow(f);
  193. }
  194. return 0;
  195. }
  196. f->seq_num = current->seq_num;
  197. if(record_len == data_len){
  198. /* record ended on packet boundary */
  199. f->packet_chain = current->next;
  200. } else {
  201. /* need to update data */
  202. f->packet_chain = current; //TODO: make current
  203. current->data = current->data + (current->data_len - (data_len - record_len));
  204. current->data_len = data_len - record_len;
  205. update_flow(f);
  206. }
  207. free(record);
  208. return 0;
  209. }
  210. int remove_flow(int index) {
  211. int i;
  212. flow *ptr;
  213. if(index){
  214. ptr = table->table + index -1;
  215. for(i=0; i< table->len - index; i++){
  216. ptr += i;
  217. *ptr = *(ptr + 1);
  218. }
  219. table->len --;
  220. } else {
  221. return 1;
  222. }
  223. printf("flow removed!\n");
  224. return 0;
  225. }
  226. int grow_table() {
  227. return 0;
  228. }
  229. /** Returns the index of a flow in the flow table if
  230. * it exists, returns 0 if it is not present.
  231. */
  232. int check_flow(flow observed){
  233. /* Loop through flows in table and see if it exists */
  234. int i;
  235. flow *candidate = table->table;
  236. /* Check first in this direction */
  237. for(i=0; i<table->len; i++){
  238. candidate += i;
  239. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  240. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  241. if(candidate->src_port == observed.src_port){
  242. if(candidate->dst_port == observed.dst_port){
  243. return i+1;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. candidate = table->table;
  250. /* Then in the other direction */
  251. for(i=0; i<table->len; i++){
  252. candidate += i;
  253. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  254. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  255. if(candidate->src_port == observed.dst_port){
  256. if(candidate->dst_port == observed.src_port){
  257. return i+1;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. return 0;
  264. }
  265. flow *get_flow(int index){
  266. if(index < table->len){
  267. return table->table+index;
  268. } else {
  269. return NULL;
  270. }
  271. }
  272. /* Adds a packet the flow's packet chain */
  273. int add_packet(flow *f, uint8_t *p){
  274. const struct ip_header *ip_hdr;
  275. const struct tcp_header *tcp_hdr;
  276. packet *new_packet = malloc(sizeof(packet));
  277. p += ETHER_HEADER_LEN; //skip ethernet header
  278. ip_hdr = (struct ip_header*) p;
  279. int size_ip = IP_HEADER_LEN(ip_hdr);
  280. if (ip_hdr->proto != IPPROTO_TCP){
  281. return 0;
  282. }
  283. p += size_ip; //skip IP header
  284. tcp_hdr = (struct tcp_header*) p;
  285. int size_tcp = TCP_HEADER_LEN(tcp_hdr);
  286. p += size_tcp;
  287. new_packet->seq_num = htonl(tcp_hdr->sequence_num);
  288. new_packet->len = htons(ip_hdr->len) - (size_ip + size_tcp);
  289. new_packet->data = p;
  290. new_packet->data_len = htons(ip_hdr->len) - (size_ip + size_tcp);
  291. new_packet->next = NULL;
  292. new_packet->incoming =
  293. (ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  294. /* Find appropriate place in chain */
  295. if(new_packet->data_len > 0){
  296. packet *previous = NULL;
  297. packet *next = f->packet_chain;
  298. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  299. //printf("next: %u <= new: %u\n", next->seq_num, new_packet->seq_num);
  300. previous = next;
  301. next = next->next;
  302. }
  303. //place packet after current
  304. if(previous == NULL){
  305. //goes at the beginning of chain
  306. new_packet->next = f->packet_chain;
  307. f->packet_chain = new_packet;
  308. } else {
  309. new_packet->next = next;
  310. previous->next = new_packet;
  311. }
  312. /*printf("Flow: %d > %d (%s)\n", ip_hdr->src.s_addr, ip_hdr->dst.s_addr, (new_packet->incoming)? "incoming":"outgoing");
  313. printf("ID number: %u\n", htonl(ip_hdr->id));
  314. printf("Sequence number: %u\n", htonl(tcp_hdr->sequence_num));
  315. printf("Acknowledgement number: %u\n", htonl(tcp_hdr->ack_num));
  316. printf("Length: %d\n", new_packet->data_len);*/
  317. }
  318. return 0;
  319. }