flow.c 8.3 KB

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