socks5proxy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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(57173);
  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], ous_in);
  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, int32_t ous_in){
  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. uint16_t len = htons(strlen(encoded_bytes));
  285. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  286. bytes_sent += send(ous_in, encoded_bytes, strlen(encoded_bytes), 0);
  287. #ifdef DEBUG_UPSTREAM
  288. printf("Wrote %d bytes to OUS_in: %x %s\n", bytes_sent, len, encoded_bytes);
  289. #endif
  290. if(bytes_sent < 0){
  291. printf("Error writing to websocket\n");
  292. fflush(stdout);
  293. goto err;
  294. }
  295. p = buffer+sizeof(struct slitheen_up_hdr);
  296. #ifdef DEBUG_UPSTREAM
  297. for(i=0; i< bytes_read; i++){
  298. printf("%02x ", p[i]);
  299. }
  300. printf("\n");
  301. fflush(stdout);
  302. #endif
  303. struct socks_req *clnt_req = (struct socks_req *) p;
  304. p += 4;
  305. //see if it's a connect request
  306. if(clnt_req->cmd != 0x01){
  307. printf("Error: issued a non-connect command\n");
  308. fflush(stdout);
  309. goto err;
  310. }
  311. //now select on pipe (for downstream data) and the socket (for upstream data)
  312. for(;;){
  313. fd_set readfds;
  314. fd_set writefds;
  315. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  316. FD_ZERO(&readfds);
  317. FD_ZERO(&writefds);
  318. FD_SET(sockfd, &readfds);
  319. FD_SET(ous_out, &readfds);
  320. FD_SET(sockfd, &writefds);
  321. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  322. printf("Select error\n");
  323. fflush(stdout);
  324. continue;
  325. }
  326. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  327. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  328. if (bytes_read < 0){
  329. printf("Error reading from socket (in for loop)\n");
  330. fflush(stdout);
  331. goto err;
  332. }
  333. if(bytes_read == 0){
  334. //socket is closed
  335. printf("Closing connection for stream %d sockfd.\n", stream_id);
  336. fflush(stdout);
  337. //Send close message to slitheen proxy
  338. up_hdr = (struct slitheen_up_hdr *) buffer;
  339. up_hdr->stream_id = stream_id;
  340. up_hdr->len = 0;
  341. bio = BIO_new(BIO_s_mem());
  342. b64 = BIO_new(BIO_f_base64());
  343. bio = BIO_push(b64, bio);
  344. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  345. BIO_write(bio, buffer, 20);
  346. BIO_flush(bio);
  347. BIO_get_mem_ptr(bio, &buffer_ptr);
  348. encoded_bytes = (*buffer_ptr).data;
  349. BIO_set_close(bio, BIO_NOCLOSE);
  350. BIO_free_all(bio);
  351. uint8_t *ebytes = calloc(1, (*buffer_ptr).length+1);
  352. memcpy(ebytes, (*buffer_ptr).data, (*buffer_ptr).length);
  353. ebytes[(*buffer_ptr).length] = '\0';
  354. len = htons((*buffer_ptr).length);
  355. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  356. bytes_sent += send(ous_in, ebytes, ntohs(len), 0);
  357. printf("Closing message: %s\n", ebytes);
  358. goto err;
  359. }
  360. if(bytes_read > 0){
  361. #ifdef DEBUG_UPSTREAM
  362. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  363. for(i=0; i< bytes_read; i++){
  364. printf("%02x ", buffer[i]);
  365. }
  366. printf("\n");
  367. printf("%s\n", buffer);
  368. fflush(stdout);
  369. #endif
  370. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  371. up_hdr = (struct slitheen_up_hdr *) buffer;
  372. up_hdr->stream_id = stream_id;
  373. up_hdr->len = htons(bytes_read);
  374. bytes_read+= sizeof(struct slitheen_up_hdr);
  375. bio = BIO_new(BIO_s_mem());
  376. b64 = BIO_new(BIO_f_base64());
  377. bio = BIO_push(b64, bio);
  378. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  379. BIO_write(bio, buffer, bytes_read);
  380. BIO_flush(bio);
  381. BIO_get_mem_ptr(bio, &buffer_ptr);
  382. BIO_set_close(bio, BIO_NOCLOSE);
  383. BIO_free_all(bio);
  384. encoded_bytes = (*buffer_ptr).data;
  385. len = htons(strlen(encoded_bytes));
  386. bytes_sent = send(ous_in, (unsigned char *) &len, sizeof(uint16_t), 0);
  387. bytes_sent += send(ous_in, encoded_bytes, ntohs(len), 0);
  388. #ifdef DEBUG_UPSTREAM
  389. printf("Sent to OUS (%d bytes): %x %s\n",bytes_sent, len, message);
  390. #endif
  391. }
  392. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  393. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  394. if (bytes_read <= 0){
  395. printf("Error reading from ous_out (in for loop)\n");
  396. fflush(stdout);
  397. goto err;
  398. }
  399. if(bytes_read > 0){
  400. #ifdef DEBUG_DOWNSTREAM
  401. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  402. for(i=0; i< bytes_read; i++){
  403. printf("%02x ", buffer[i]);
  404. }
  405. printf("\n");
  406. printf("%s\n", buffer);
  407. fflush(stdout);
  408. #endif
  409. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  410. if(bytes_sent <= 0){
  411. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  412. }
  413. #ifdef DEBUG_DOWNSTREAM
  414. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  415. for(i=0; i< bytes_sent; i++){
  416. printf("%02x ", buffer[i]);
  417. }
  418. printf("\n");
  419. fflush(stdout);
  420. #endif
  421. }
  422. }
  423. }
  424. err:
  425. //should also remove stream from table
  426. close(sockfd);
  427. close(ous_in);
  428. free(buffer);
  429. free(response);
  430. exit(0);
  431. }
  432. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  433. * the block and then write the data to the correct thread to be passed to the browser
  434. */
  435. void *demultiplex_data(){
  436. int32_t buffer_len = BUFSIZ;
  437. uint8_t *buffer = calloc(1, buffer_len);
  438. uint8_t *p;
  439. printf("Opening OUS_out... ");
  440. int32_t ous_fd = open("OUS_out", O_RDONLY);
  441. printf("done.\n");
  442. uint8_t *partial_block = NULL;
  443. uint32_t partial_block_len = 0;
  444. uint32_t resource_remaining = 0;
  445. uint64_t expected_next_count = 1;
  446. data_block *saved_data = NULL;
  447. for(;;){
  448. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  449. if(bytes_read > 0){
  450. int32_t bytes_remaining = bytes_read;
  451. p = buffer;
  452. //didn't read a full slitheen block last time
  453. if(partial_block_len > 0){
  454. //process first part of slitheen info
  455. memmove(buffer+partial_block_len, buffer, bytes_read);
  456. memcpy(buffer, partial_block, partial_block_len);
  457. bytes_remaining += partial_block_len;
  458. free(partial_block);
  459. partial_block = NULL;
  460. partial_block_len = 0;
  461. }
  462. while(bytes_remaining > 0){
  463. if(resource_remaining <= 0){//we're at a new resource
  464. //the first value for a new resource will be the resource length,
  465. //followed by a newline
  466. uint8_t *end_ptr;
  467. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  468. #ifdef DEBUG_PARSE
  469. printf("Starting new resource of len %d bytes\n", resource_remaining);
  470. printf("Resource len bytes:\n");
  471. int i;
  472. for(i=0; i< (end_ptr - p) + 1; i++){
  473. printf("%02x ", ((const char *) p)[i]);
  474. }
  475. printf("\n");
  476. #endif
  477. if(resource_remaining == 0){
  478. bytes_remaining -= (end_ptr - p) + 1;
  479. p += (end_ptr - p) + 1;
  480. } else {
  481. bytes_remaining -= (end_ptr - p) + 1;
  482. p += (end_ptr - p) + 1;
  483. }
  484. continue;
  485. }
  486. if(resource_remaining < SLITHEEN_HEADER_LEN){
  487. printf("ERROR: Resource remaining doesn't fit header len.\n");
  488. resource_remaining = 0;
  489. bytes_remaining = 0;
  490. break;
  491. }
  492. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  493. #ifdef DEBUG_PARSE
  494. printf("Partial header: ");
  495. int i;
  496. for(i = 0; i< bytes_remaining; i++){
  497. printf("%02x ", p[i]);
  498. }
  499. printf("\n");
  500. #endif
  501. if(partial_block != NULL) printf("UH OH (PB)\n");
  502. partial_block = calloc(1, bytes_remaining);
  503. memcpy(partial_block, p, bytes_remaining);
  504. partial_block_len = bytes_remaining;
  505. bytes_remaining = 0;
  506. break;
  507. }
  508. //decrypt header to see if we have entire block
  509. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  510. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  511. peek_header(tmp_header);
  512. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  513. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  514. #ifdef DEBUG_PARSE
  515. printf("Slitheen header:\n");
  516. int i;
  517. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  518. printf("%02x ", tmp_header[i]);
  519. }
  520. printf("\n");
  521. #endif
  522. if(ntohs(sl_hdr->len) > resource_remaining){
  523. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  524. resource_remaining = 0;
  525. bytes_remaining = 0;
  526. break;
  527. }
  528. if(ntohs(sl_hdr->len) > bytes_remaining){
  529. if(partial_block != NULL) printf("UH OH (PB)\n");
  530. partial_block = calloc(1, ntohs(sl_hdr->len));
  531. memcpy(partial_block, p, bytes_remaining);
  532. partial_block_len = bytes_remaining;
  533. bytes_remaining = 0;
  534. free(tmp_header);
  535. break;
  536. }
  537. super_decrypt(p);
  538. sl_hdr = (struct slitheen_hdr *) p;
  539. free(tmp_header);
  540. p += SLITHEEN_HEADER_LEN;
  541. bytes_remaining -= SLITHEEN_HEADER_LEN;
  542. resource_remaining -= SLITHEEN_HEADER_LEN;
  543. if((!sl_hdr->len) && (sl_hdr->garbage)){
  544. #ifdef DEBUG_PARSE
  545. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  546. #endif
  547. p += ntohs(sl_hdr->garbage);
  548. bytes_remaining -= ntohs(sl_hdr->garbage);
  549. resource_remaining -= ntohs(sl_hdr->garbage);
  550. continue;
  551. }
  552. int32_t pipe_fd =-1;
  553. if(connections->first == NULL){
  554. printf("Error: there are no connections\n");
  555. } else {
  556. connection *last = connections->first;
  557. if (last->stream_id == sl_hdr->stream_id){
  558. pipe_fd = last->pipe_fd;
  559. }
  560. while(last->next != NULL){
  561. last = last->next;
  562. if (last->stream_id == sl_hdr->stream_id){
  563. pipe_fd = last->pipe_fd;
  564. }
  565. }
  566. }
  567. if(pipe_fd == -1){
  568. printf("No stream id exists. Possibly invalid header\n");
  569. break;
  570. }
  571. #ifdef DEBUG_PARSE
  572. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  573. #endif
  574. //figure out how much to skip
  575. int32_t padding = 0;
  576. if(ntohs(sl_hdr->len) %16){
  577. padding = 16 - ntohs(sl_hdr->len)%16;
  578. }
  579. p += 16; //IV
  580. //check counter to see if we are missing data
  581. if(sl_hdr->counter > expected_next_count){
  582. //save any future data
  583. printf("Received header with count %lu. Expected count %lu.\n",
  584. sl_hdr->counter, expected_next_count);
  585. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  586. data_block *new_block = malloc(sizeof(data_block));
  587. new_block->count = sl_hdr->counter;
  588. new_block->len = ntohs(sl_hdr->len);
  589. new_block->data = malloc(ntohs(sl_hdr->len));
  590. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  591. new_block->pipe_fd = pipe_fd;
  592. new_block->next = saved_data;
  593. saved_data = new_block;
  594. } else {
  595. data_block *last = saved_data;
  596. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  597. last = last->next;
  598. }
  599. data_block *new_block = malloc(sizeof(data_block));
  600. new_block->count = sl_hdr->counter;
  601. new_block->len = ntohs(sl_hdr->len);
  602. new_block->data = malloc(ntohs(sl_hdr->len));
  603. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  604. new_block->pipe_fd = pipe_fd;
  605. new_block->next = last->next;
  606. last->next = new_block;
  607. }
  608. } else {
  609. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  610. if(bytes_sent <= 0){
  611. printf("Error reading to pipe for stream id %d\n",
  612. sl_hdr->stream_id);
  613. }
  614. //increment expected counter
  615. expected_next_count++;
  616. }
  617. //now check to see if there is saved data to write out
  618. if(saved_data != NULL){
  619. data_block *current_block = saved_data;
  620. while((current_block != NULL) &&
  621. (expected_next_count == current_block->count)){
  622. int32_t bytes_sent = write(current_block->pipe_fd,
  623. current_block->data, current_block->len);
  624. if(bytes_sent <= 0){
  625. printf("Error reading to pipe for stream id %d\n",
  626. sl_hdr->stream_id);
  627. }
  628. expected_next_count++;
  629. saved_data = current_block->next;
  630. free(current_block->data);
  631. free(current_block);
  632. current_block = saved_data;
  633. }
  634. }
  635. p += ntohs(sl_hdr->len); //encrypted data
  636. p += 16; //mac
  637. p += padding;
  638. p += ntohs(sl_hdr->garbage);
  639. bytes_remaining -=
  640. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  641. resource_remaining -=
  642. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  643. }
  644. } else {
  645. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  646. printf("Re-opening OUS_out... ");
  647. close(ous_fd);
  648. ous_fd = open("OUS_out", O_RDONLY);
  649. printf("done.\n");
  650. }
  651. }
  652. free(buffer);
  653. close(ous_fd);
  654. }