socks5proxy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Slitheen - a decoy routing system for censorship resistance
  3. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Additional permission under GNU GPL version 3 section 7
  18. *
  19. * If you modify this Program, or any covered work, by linking or combining
  20. * it with the OpenSSL library (or a modified version of that library),
  21. * containing parts covered by the terms of the OpenSSL Licence and the
  22. * SSLeay license, the licensors of this Program grant you additional
  23. * permission to convey the resulting work. Corresponding Source for a
  24. * non-source form of such a combination shall include the source code
  25. * for the parts of the OpenSSL library used as well as that of the covered
  26. * work.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <stdint.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <sys/socket.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <netinet/in.h>
  38. #include <netdb.h>
  39. #include <pthread.h>
  40. #include <fcntl.h>
  41. #include <arpa/inet.h>
  42. #include <openssl/bio.h>
  43. #include <openssl/evp.h>
  44. #include <openssl/buffer.h>
  45. #include <openssl/rand.h>
  46. #include "socks5proxy.h"
  47. #include "crypto.h"
  48. #include "tagging.h"
  49. static connection_table *connections;
  50. int main(void){
  51. int listen_socket;
  52. struct sockaddr_in address;
  53. struct sockaddr_in remote_addr;
  54. socklen_t addr_size;
  55. mkfifo("OUS_out", 0666);
  56. //generate Slitheen ID using Telex tagging method
  57. uint8_t slitheen_id[SLITHEEN_ID_LEN];
  58. uint8_t shared_secret[16];
  59. generate_slitheen_id(slitheen_id, shared_secret);
  60. //RAND_bytes(slitheen_id, SLITHEEN_ID_LEN);
  61. printf("Randomly generated slitheen id: ");
  62. int i;
  63. for(i=0; i< SLITHEEN_ID_LEN; i++){
  64. printf("%02x ", slitheen_id[i]);
  65. }
  66. printf("\n");
  67. // Calculate super encryption keys
  68. generate_super_keys(shared_secret);
  69. //b64 encode slitheen ID
  70. char *encoded_bytes;
  71. BUF_MEM *buffer_ptr;
  72. BIO *bio, *b64;
  73. b64 = BIO_new(BIO_f_base64());
  74. bio = BIO_new(BIO_s_mem());
  75. bio = BIO_push(b64, bio);
  76. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  77. BIO_write(bio, slitheen_id, SLITHEEN_ID_LEN);
  78. BIO_flush(bio);
  79. BIO_get_mem_ptr(bio, &buffer_ptr);
  80. BIO_set_close(bio, BIO_NOCLOSE);
  81. BIO_free_all(bio);
  82. encoded_bytes = (*buffer_ptr).data;
  83. encoded_bytes[(*buffer_ptr).length] = '\0';
  84. //give encoded slitheen ID to ous
  85. struct sockaddr_in ous_addr;
  86. ous_addr.sin_family = AF_INET;
  87. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  88. ous_addr.sin_port = htons(8888);
  89. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  90. if(ous_in < 0){
  91. printf("Failed to make ous_in socket\n");
  92. return 1;
  93. }
  94. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  95. if(error < 0){
  96. printf("Error connecting\n");
  97. return 1;
  98. }
  99. uint16_t len = htons(strlen(encoded_bytes));
  100. int32_t bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  101. bytes_sent += send(ous_in, encoded_bytes, ntohs(len), 0);
  102. printf("Wrote %d bytes to OUS_in: %x\n %s\n", bytes_sent, len, encoded_bytes);
  103. /* Spawn process to listen for incoming data from OUS
  104. int32_t demux_pipe[2];
  105. if(pipe(demux_pipe) < 0){
  106. printf("Failed to create pipe for new thread\n");
  107. return 1;
  108. }*/
  109. connections = calloc(1, sizeof(connection_table));
  110. connections->first = NULL;
  111. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  112. pthread_create(demux_thread, NULL, demultiplex_data, NULL);
  113. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  114. printf("Error creating socket\n");
  115. fflush(stdout);
  116. return 1;
  117. }
  118. address.sin_family = AF_INET;
  119. address.sin_addr.s_addr = INADDR_ANY;
  120. address.sin_port = htons(1080);
  121. int enable = 1;
  122. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  123. printf("Error setting sockopt\n");
  124. return 1;
  125. }
  126. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  127. printf("Error binding socket\n");
  128. fflush(stdout);
  129. return 1;
  130. }
  131. if(listen(listen_socket, 10) < 0){
  132. printf("Error listening\n");
  133. fflush(stdout);
  134. close(listen_socket);
  135. exit(1);
  136. }
  137. uint8_t last_id = 1;
  138. printf("Ready for listening\n");
  139. for(;;){
  140. addr_size = sizeof(remote_addr);
  141. int new_socket;
  142. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  143. &addr_size);
  144. if(new_socket < 0){
  145. perror("accept");
  146. exit(1);
  147. }
  148. printf("New connection\n");
  149. //assign a new stream_id and create a pipe for the session
  150. connection *new_conn = calloc(1, sizeof(connection));
  151. new_conn->stream_id = last_id++;
  152. int32_t pipefd[2];
  153. if(pipe(pipefd) < 0){
  154. printf("Failed to create pipe\n");
  155. continue;
  156. }
  157. new_conn->pipe_fd = pipefd[1];
  158. new_conn->next = NULL;
  159. if(connections->first == NULL){
  160. connections->first = new_conn;
  161. printf("Added first connection with id: %d\n", new_conn->stream_id);
  162. fflush(stdout);
  163. } else {
  164. connection *last = connections->first;
  165. while(last->next != NULL){
  166. last = last->next;
  167. }
  168. last->next = new_conn;
  169. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  170. fflush(stdout);
  171. }
  172. int pid = fork();
  173. if(pid == 0){ //child
  174. close(listen_socket);
  175. proxy_data(new_socket, new_conn->stream_id, pipefd[0]);
  176. exit(0);
  177. }
  178. close(new_socket);
  179. }
  180. return 0;
  181. }
  182. //continuously read from the socket and look for a CONNECT message
  183. int proxy_data(int sockfd, uint16_t stream_id, int32_t ous_out){
  184. uint8_t *buffer = calloc(1, BUFSIZ);
  185. uint8_t *response = calloc(1, BUFSIZ);
  186. int32_t i;
  187. int bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  188. if (bytes_read < 0){
  189. printf("Error reading from socket (fd = %d)\n", sockfd);
  190. fflush(stdout);
  191. goto err;
  192. }
  193. #ifdef DEBUG
  194. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  195. for(i=0; i< bytes_read; i++){
  196. printf("%02x ", buffer[i]);
  197. }
  198. printf("\n");
  199. fflush(stdout);
  200. #endif
  201. //Respond to methods negotiation
  202. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  203. uint8_t *p = buffer + 2;
  204. if(clnt_meth->version != 0x05){
  205. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  206. fflush(stdout);
  207. goto err;
  208. }
  209. int responded = 0;
  210. int bytes_sent;
  211. for(i=0; i< clnt_meth->num_methods; i++){
  212. if(p[0] == 0x00){//send response with METH= 0x00
  213. response[0] = 0x05;
  214. response[1] = 0x00;
  215. send(sockfd, response, 2, 0);
  216. responded = 1;
  217. }
  218. p++;
  219. }
  220. if(!responded){//respond with METH= 0xFF
  221. response[0] = 0x05;
  222. response[1] = 0xFF;
  223. send(sockfd, response, 2, 0);
  224. goto err;
  225. }
  226. //Now wait for a connect request
  227. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  228. if (bytes_read < 0){
  229. printf("Error reading from socket\n");
  230. fflush(stdout);
  231. goto err;
  232. }
  233. #ifdef DEBUG
  234. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  235. for(i=0; i< bytes_read; i++){
  236. printf("%02x ", buffer[i]);
  237. }
  238. printf("\n");
  239. fflush(stdout);
  240. #endif
  241. //Now respond
  242. response[0] = 0x05;
  243. response[1] = 0x00;
  244. response[2] = 0x00;
  245. response[3] = 0x01;
  246. *((uint32_t *) (response + 4)) = 0;
  247. *((uint16_t *) (response + 8)) = 0;
  248. send(sockfd, response, 10, 0);
  249. //wait for first upstream bytes
  250. bytes_read += recv(sockfd, buffer+bytes_read, BUFSIZ-bytes_read-3, 0);
  251. if (bytes_read < 0){
  252. printf("Error reading from socket\n");
  253. fflush(stdout);
  254. goto err;
  255. }
  256. #ifdef DEBUG_UPSTREAM
  257. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  258. for(i=0; i< bytes_read; i++){
  259. printf("%02x ", buffer[i]);
  260. }
  261. printf("\n");
  262. fflush(stdout);
  263. #endif
  264. //pre-pend stream_id and length
  265. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read+1);
  266. struct slitheen_up_hdr *up_hdr = (struct slitheen_up_hdr *) buffer;
  267. up_hdr->stream_id = stream_id;
  268. up_hdr->len = htons(bytes_read);
  269. bytes_read+= sizeof(struct slitheen_up_hdr);
  270. //encode bytes for safe transport (b64)
  271. const char *encoded_bytes;
  272. BUF_MEM *buffer_ptr;
  273. BIO *bio, *b64;
  274. b64 = BIO_new(BIO_f_base64());
  275. bio = BIO_new(BIO_s_mem());
  276. bio = BIO_push(b64, bio);
  277. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  278. BIO_write(bio, buffer, bytes_read);
  279. BIO_flush(bio);
  280. BIO_get_mem_ptr(bio, &buffer_ptr);
  281. BIO_set_close(bio, BIO_NOCLOSE);
  282. BIO_free_all(bio);
  283. encoded_bytes = (*buffer_ptr).data;
  284. struct sockaddr_in ous_addr;
  285. ous_addr.sin_family = AF_INET;
  286. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  287. ous_addr.sin_port = htons(8888);
  288. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  289. if(ous_in < 0){
  290. printf("Failed to make ous_in socket\n");
  291. goto err;
  292. }
  293. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  294. if(error < 0){
  295. printf("Error connecting\n");
  296. goto err;
  297. }
  298. uint16_t len = htons(strlen(encoded_bytes));
  299. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  300. bytes_sent += send(ous_in, encoded_bytes, strlen(encoded_bytes), 0);
  301. #ifdef DEBUG_UPSTREAM
  302. printf("Wrote %d bytes to OUS_in: %x %s\n", bytes_sent, len, encoded_bytes);
  303. #endif
  304. if(bytes_sent < 0){
  305. printf("Error writing to websocket\n");
  306. fflush(stdout);
  307. goto err;
  308. } else {
  309. close(ous_in);
  310. }
  311. p = buffer+sizeof(struct slitheen_up_hdr);
  312. #ifdef DEBUG_UPSTREAM
  313. for(i=0; i< bytes_read; i++){
  314. printf("%02x ", p[i]);
  315. }
  316. printf("\n");
  317. fflush(stdout);
  318. #endif
  319. struct socks_req *clnt_req = (struct socks_req *) p;
  320. p += 4;
  321. //see if it's a connect request
  322. if(clnt_req->cmd != 0x01){
  323. printf("Error: issued a non-connect command\n");
  324. fflush(stdout);
  325. goto err;
  326. }
  327. //now select on pipe (for downstream data) and the socket (for upstream data)
  328. for(;;){
  329. fd_set readfds;
  330. fd_set writefds;
  331. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  332. FD_ZERO(&readfds);
  333. FD_ZERO(&writefds);
  334. FD_SET(sockfd, &readfds);
  335. FD_SET(ous_out, &readfds);
  336. FD_SET(sockfd, &writefds);
  337. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  338. printf("Select error\n");
  339. fflush(stdout);
  340. continue;
  341. }
  342. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  343. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  344. if (bytes_read < 0){
  345. printf("Error reading from socket (in for loop)\n");
  346. fflush(stdout);
  347. goto err;
  348. }
  349. if(bytes_read == 0){
  350. //socket is closed
  351. printf("Closing connection for stream %d sockfd.\n", stream_id);
  352. fflush(stdout);
  353. //Send close message to slitheen proxy
  354. up_hdr = (struct slitheen_up_hdr *) buffer;
  355. up_hdr->stream_id = stream_id;
  356. up_hdr->len = 0;
  357. bio = BIO_new(BIO_s_mem());
  358. b64 = BIO_new(BIO_f_base64());
  359. bio = BIO_push(b64, bio);
  360. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  361. BIO_write(bio, buffer, 20);
  362. BIO_flush(bio);
  363. BIO_get_mem_ptr(bio, &buffer_ptr);
  364. encoded_bytes = (*buffer_ptr).data;
  365. BIO_set_close(bio, BIO_NOCLOSE);
  366. BIO_free_all(bio);
  367. uint8_t *ebytes = calloc(1, (*buffer_ptr).length+1);
  368. memcpy(ebytes, (*buffer_ptr).data, (*buffer_ptr).length);
  369. ebytes[(*buffer_ptr).length] = '\0';
  370. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  371. if(ous_in < 0){
  372. printf("Failed to make ous_in socket\n");
  373. fflush(stdout);
  374. goto err;
  375. }
  376. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  377. if(error < 0){
  378. printf("Error connecting\n");
  379. fflush(stdout);
  380. goto err;
  381. }
  382. len = htons((*buffer_ptr).length);
  383. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  384. bytes_sent += send(ous_in, ebytes, ntohs(len), 0);
  385. printf("Closing message: %s\n", ebytes);
  386. close(ous_in);
  387. goto err;
  388. }
  389. if(bytes_read > 0){
  390. #ifdef DEBUG_UPSTREAM
  391. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  392. for(i=0; i< bytes_read; i++){
  393. printf("%02x ", buffer[i]);
  394. }
  395. printf("\n");
  396. printf("%s\n", buffer);
  397. fflush(stdout);
  398. #endif
  399. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  400. up_hdr = (struct slitheen_up_hdr *) buffer;
  401. up_hdr->stream_id = stream_id;
  402. up_hdr->len = htons(bytes_read);
  403. bytes_read+= sizeof(struct slitheen_up_hdr);
  404. bio = BIO_new(BIO_s_mem());
  405. b64 = BIO_new(BIO_f_base64());
  406. bio = BIO_push(b64, bio);
  407. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  408. BIO_write(bio, buffer, bytes_read);
  409. BIO_flush(bio);
  410. BIO_get_mem_ptr(bio, &buffer_ptr);
  411. BIO_set_close(bio, BIO_NOCLOSE);
  412. BIO_free_all(bio);
  413. encoded_bytes = (*buffer_ptr).data;
  414. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  415. if(ous_in < 0){
  416. printf("Failed to make ous_in socket\n");
  417. return 1;
  418. }
  419. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  420. if(error < 0){
  421. printf("Error connecting\n");
  422. return 1;
  423. }
  424. len = htons(strlen(encoded_bytes));
  425. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  426. bytes_sent += send(ous_in, encoded_bytes, ntohs(len), 0);
  427. #ifdef DEBUG_UPSTREAM
  428. printf("Sent to OUS (%d bytes): %x %s\n",bytes_sent, len, message);
  429. #endif
  430. close(ous_in);
  431. }
  432. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  433. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  434. if (bytes_read <= 0){
  435. printf("Error reading from ous_out (in for loop)\n");
  436. fflush(stdout);
  437. goto err;
  438. }
  439. if(bytes_read > 0){
  440. #ifdef DEBUG_DOWNSTREAM
  441. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  442. for(i=0; i< bytes_read; i++){
  443. printf("%02x ", buffer[i]);
  444. }
  445. printf("\n");
  446. printf("%s\n", buffer);
  447. fflush(stdout);
  448. #endif
  449. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  450. if(bytes_sent <= 0){
  451. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  452. }
  453. #ifdef DEBUG_DOWNSTREAM
  454. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  455. for(i=0; i< bytes_sent; i++){
  456. printf("%02x ", buffer[i]);
  457. }
  458. printf("\n");
  459. fflush(stdout);
  460. #endif
  461. }
  462. }
  463. }
  464. err:
  465. //should also remove stream from table
  466. close(sockfd);
  467. free(buffer);
  468. free(response);
  469. exit(0);
  470. }
  471. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  472. * the block and then write the data to the correct thread to be passed to the browser
  473. */
  474. void *demultiplex_data(){
  475. int32_t buffer_len = BUFSIZ;
  476. uint8_t *buffer = calloc(1, buffer_len);
  477. uint8_t *p;
  478. printf("Opening OUS_out... ");
  479. int32_t ous_fd = open("OUS_out", O_RDONLY);
  480. printf("done.\n");
  481. uint8_t *partial_block = NULL;
  482. uint32_t partial_block_len = 0;
  483. uint32_t resource_remaining = 0;
  484. uint64_t expected_next_count = 1;
  485. data_block *saved_data = NULL;
  486. for(;;){
  487. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  488. if(bytes_read > 0){
  489. int32_t bytes_remaining = bytes_read;
  490. p = buffer;
  491. //didn't read a full slitheen block last time
  492. if(partial_block_len > 0){
  493. //process first part of slitheen info
  494. memmove(buffer+partial_block_len, buffer, bytes_read);
  495. memcpy(buffer, partial_block, partial_block_len);
  496. bytes_remaining += partial_block_len;
  497. free(partial_block);
  498. partial_block = NULL;
  499. partial_block_len = 0;
  500. }
  501. while(bytes_remaining > 0){
  502. if(resource_remaining <= 0){//we're at a new resource
  503. //the first value for a new resource will be the resource length,
  504. //followed by a newline
  505. uint8_t *end_ptr;
  506. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  507. #ifdef DEBUG_PARSE
  508. printf("Starting new resource of len %d bytes\n", resource_remaining);
  509. printf("Resource len bytes:\n");
  510. int i;
  511. for(i=0; i< (end_ptr - p) + 1; i++){
  512. printf("%02x ", ((const char *) p)[i]);
  513. }
  514. printf("\n");
  515. #endif
  516. if(resource_remaining == 0){
  517. bytes_remaining -= (end_ptr - p) + 1;
  518. p += (end_ptr - p) + 1;
  519. } else {
  520. bytes_remaining -= (end_ptr - p) + 1;
  521. p += (end_ptr - p) + 1;
  522. }
  523. continue;
  524. }
  525. if(resource_remaining < SLITHEEN_HEADER_LEN){
  526. printf("ERROR: Resource remaining doesn't fit header len.\n");
  527. resource_remaining = 0;
  528. bytes_remaining = 0;
  529. break;
  530. }
  531. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  532. #ifdef DEBUG_PARSE
  533. printf("Partial header: ");
  534. int i;
  535. for(i = 0; i< bytes_remaining; i++){
  536. printf("%02x ", p[i]);
  537. }
  538. printf("\n");
  539. #endif
  540. if(partial_block != NULL) printf("UH OH (PB)\n");
  541. partial_block = calloc(1, bytes_remaining);
  542. memcpy(partial_block, p, bytes_remaining);
  543. partial_block_len = bytes_remaining;
  544. bytes_remaining = 0;
  545. break;
  546. }
  547. //decrypt header to see if we have entire block
  548. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  549. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  550. peek_header(tmp_header);
  551. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  552. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  553. #ifdef DEBUG_PARSE
  554. printf("Slitheen header:\n");
  555. int i;
  556. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  557. printf("%02x ", tmp_header[i]);
  558. }
  559. printf("\n");
  560. #endif
  561. if(ntohs(sl_hdr->len) > resource_remaining){
  562. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  563. resource_remaining = 0;
  564. bytes_remaining = 0;
  565. break;
  566. }
  567. if(ntohs(sl_hdr->len) > bytes_remaining){
  568. if(partial_block != NULL) printf("UH OH (PB)\n");
  569. partial_block = calloc(1, ntohs(sl_hdr->len));
  570. memcpy(partial_block, p, bytes_remaining);
  571. partial_block_len = bytes_remaining;
  572. bytes_remaining = 0;
  573. free(tmp_header);
  574. break;
  575. }
  576. super_decrypt(p);
  577. sl_hdr = (struct slitheen_hdr *) p;
  578. free(tmp_header);
  579. p += SLITHEEN_HEADER_LEN;
  580. bytes_remaining -= SLITHEEN_HEADER_LEN;
  581. resource_remaining -= SLITHEEN_HEADER_LEN;
  582. if((!sl_hdr->len) && (sl_hdr->garbage)){
  583. #ifdef DEBUG_PARSE
  584. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  585. #endif
  586. p += ntohs(sl_hdr->garbage);
  587. bytes_remaining -= ntohs(sl_hdr->garbage);
  588. resource_remaining -= ntohs(sl_hdr->garbage);
  589. continue;
  590. }
  591. int32_t pipe_fd =-1;
  592. if(connections->first == NULL){
  593. printf("Error: there are no connections\n");
  594. } else {
  595. connection *last = connections->first;
  596. if (last->stream_id == sl_hdr->stream_id){
  597. pipe_fd = last->pipe_fd;
  598. }
  599. while(last->next != NULL){
  600. last = last->next;
  601. if (last->stream_id == sl_hdr->stream_id){
  602. pipe_fd = last->pipe_fd;
  603. }
  604. }
  605. }
  606. if(pipe_fd == -1){
  607. printf("No stream id exists. Possibly invalid header\n");
  608. break;
  609. }
  610. #ifdef DEBUG_PARSE
  611. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  612. #endif
  613. //figure out how much to skip
  614. int32_t padding = 0;
  615. if(ntohs(sl_hdr->len) %16){
  616. padding = 16 - ntohs(sl_hdr->len)%16;
  617. }
  618. p += 16; //IV
  619. //check counter to see if we are missing data
  620. if(sl_hdr->counter > expected_next_count){
  621. //save any future data
  622. printf("Received header with count %lu. Expected count %lu.\n",
  623. sl_hdr->counter, expected_next_count);
  624. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  625. data_block *new_block = malloc(sizeof(data_block));
  626. new_block->count = sl_hdr->counter;
  627. new_block->len = ntohs(sl_hdr->len);
  628. new_block->data = malloc(ntohs(sl_hdr->len));
  629. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  630. new_block->pipe_fd = pipe_fd;
  631. new_block->next = saved_data;
  632. saved_data = new_block;
  633. } else {
  634. data_block *last = saved_data;
  635. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  636. last = last->next;
  637. }
  638. data_block *new_block = malloc(sizeof(data_block));
  639. new_block->count = sl_hdr->counter;
  640. new_block->len = ntohs(sl_hdr->len);
  641. new_block->data = malloc(ntohs(sl_hdr->len));
  642. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  643. new_block->pipe_fd = pipe_fd;
  644. new_block->next = last->next;
  645. last->next = new_block;
  646. }
  647. } else {
  648. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  649. if(bytes_sent <= 0){
  650. printf("Error reading to pipe for stream id %d\n",
  651. sl_hdr->stream_id);
  652. }
  653. //increment expected counter
  654. expected_next_count++;
  655. }
  656. //now check to see if there is saved data to write out
  657. if(saved_data != NULL){
  658. data_block *current_block = saved_data;
  659. while((current_block != NULL) &&
  660. (expected_next_count == current_block->count)){
  661. int32_t bytes_sent = write(current_block->pipe_fd,
  662. current_block->data, current_block->len);
  663. if(bytes_sent <= 0){
  664. printf("Error reading to pipe for stream id %d\n",
  665. sl_hdr->stream_id);
  666. }
  667. expected_next_count++;
  668. saved_data = current_block->next;
  669. free(current_block->data);
  670. free(current_block);
  671. current_block = saved_data;
  672. }
  673. }
  674. p += ntohs(sl_hdr->len); //encrypted data
  675. p += 16; //mac
  676. p += padding;
  677. p += ntohs(sl_hdr->garbage);
  678. bytes_remaining -=
  679. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  680. resource_remaining -=
  681. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  682. }
  683. } else {
  684. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  685. printf("Re-opening OUS_out... ");
  686. close(ous_fd);
  687. ous_fd = open("OUS_out", O_RDONLY);
  688. printf("done.\n");
  689. }
  690. }
  691. free(buffer);
  692. close(ous_fd);
  693. }