socks5proxy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. char *message = calloc(1, BUFSIZ);
  100. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes), encoded_bytes);
  101. int32_t bytes_sent = send(ous_in, message, strlen(message), 0);
  102. printf("Wrote %d bytes to OUS_in:\n %s\n", bytes_sent, message);
  103. free(message);
  104. /* Spawn process to listen for incoming data from OUS
  105. int32_t demux_pipe[2];
  106. if(pipe(demux_pipe) < 0){
  107. printf("Failed to create pipe for new thread\n");
  108. return 1;
  109. }*/
  110. connections = calloc(1, sizeof(connection_table));
  111. connections->first = NULL;
  112. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  113. pthread_create(demux_thread, NULL, demultiplex_data, NULL);
  114. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  115. printf("Error creating socket\n");
  116. fflush(stdout);
  117. return 1;
  118. }
  119. address.sin_family = AF_INET;
  120. address.sin_addr.s_addr = INADDR_ANY;
  121. address.sin_port = htons(1080);
  122. int enable = 1;
  123. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  124. printf("Error setting sockopt\n");
  125. return 1;
  126. }
  127. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  128. printf("Error binding socket\n");
  129. fflush(stdout);
  130. return 1;
  131. }
  132. if(listen(listen_socket, 10) < 0){
  133. printf("Error listening\n");
  134. fflush(stdout);
  135. close(listen_socket);
  136. exit(1);
  137. }
  138. uint8_t last_id = 1;
  139. printf("Ready for listening\n");
  140. for(;;){
  141. addr_size = sizeof(remote_addr);
  142. int new_socket;
  143. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  144. &addr_size);
  145. if(new_socket < 0){
  146. perror("accept");
  147. exit(1);
  148. }
  149. printf("New connection\n");
  150. //assign a new stream_id and create a pipe for the session
  151. connection *new_conn = calloc(1, sizeof(connection));
  152. new_conn->stream_id = last_id++;
  153. int32_t pipefd[2];
  154. if(pipe(pipefd) < 0){
  155. printf("Failed to create pipe\n");
  156. continue;
  157. }
  158. new_conn->pipe_fd = pipefd[1];
  159. new_conn->next = NULL;
  160. if(connections->first == NULL){
  161. connections->first = new_conn;
  162. printf("Added first connection with id: %d\n", new_conn->stream_id);
  163. fflush(stdout);
  164. } else {
  165. connection *last = connections->first;
  166. while(last->next != NULL){
  167. last = last->next;
  168. }
  169. last->next = new_conn;
  170. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  171. fflush(stdout);
  172. }
  173. int pid = fork();
  174. if(pid == 0){ //child
  175. close(listen_socket);
  176. proxy_data(new_socket, new_conn->stream_id, pipefd[0]);
  177. exit(0);
  178. }
  179. close(new_socket);
  180. }
  181. return 0;
  182. }
  183. //continuously read from the socket and look for a CONNECT message
  184. int proxy_data(int sockfd, uint16_t stream_id, int32_t ous_out){
  185. uint8_t *buffer = calloc(1, BUFSIZ);
  186. uint8_t *response = calloc(1, BUFSIZ);
  187. int32_t i;
  188. int bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  189. if (bytes_read < 0){
  190. printf("Error reading from socket (fd = %d)\n", sockfd);
  191. fflush(stdout);
  192. goto err;
  193. }
  194. #ifdef DEBUG
  195. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  196. for(i=0; i< bytes_read; i++){
  197. printf("%02x ", buffer[i]);
  198. }
  199. printf("\n");
  200. fflush(stdout);
  201. #endif
  202. //Respond to methods negotiation
  203. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  204. uint8_t *p = buffer + 2;
  205. if(clnt_meth->version != 0x05){
  206. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  207. fflush(stdout);
  208. goto err;
  209. }
  210. int responded = 0;
  211. int bytes_sent;
  212. for(i=0; i< clnt_meth->num_methods; i++){
  213. if(p[0] == 0x00){//send response with METH= 0x00
  214. response[0] = 0x05;
  215. response[1] = 0x00;
  216. send(sockfd, response, 2, 0);
  217. responded = 1;
  218. }
  219. p++;
  220. }
  221. if(!responded){//respond with METH= 0xFF
  222. response[0] = 0x05;
  223. response[1] = 0xFF;
  224. send(sockfd, response, 2, 0);
  225. goto err;
  226. }
  227. //Now wait for a connect request
  228. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  229. if (bytes_read < 0){
  230. printf("Error reading from socket\n");
  231. fflush(stdout);
  232. goto err;
  233. }
  234. #ifdef DEBUG
  235. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  236. for(i=0; i< bytes_read; i++){
  237. printf("%02x ", buffer[i]);
  238. }
  239. printf("\n");
  240. fflush(stdout);
  241. #endif
  242. //Now respond
  243. response[0] = 0x05;
  244. response[1] = 0x00;
  245. response[2] = 0x00;
  246. response[3] = 0x01;
  247. *((uint32_t *) (response + 4)) = 0;
  248. *((uint16_t *) (response + 8)) = 0;
  249. send(sockfd, response, 10, 0);
  250. //wait for first upstream bytes
  251. bytes_read += recv(sockfd, buffer+bytes_read, BUFSIZ-bytes_read-3, 0);
  252. if (bytes_read < 0){
  253. printf("Error reading from socket\n");
  254. fflush(stdout);
  255. goto err;
  256. }
  257. #ifdef DEBUG_UPSTREAM
  258. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  259. for(i=0; i< bytes_read; i++){
  260. printf("%02x ", buffer[i]);
  261. }
  262. printf("\n");
  263. fflush(stdout);
  264. #endif
  265. //pre-pend stream_id and length
  266. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read+1);
  267. struct slitheen_up_hdr *up_hdr = (struct slitheen_up_hdr *) buffer;
  268. up_hdr->stream_id = stream_id;
  269. up_hdr->len = htons(bytes_read);
  270. bytes_read+= sizeof(struct slitheen_up_hdr);
  271. //encode bytes for safe transport (b64)
  272. const char *encoded_bytes;
  273. BUF_MEM *buffer_ptr;
  274. BIO *bio, *b64;
  275. b64 = BIO_new(BIO_f_base64());
  276. bio = BIO_new(BIO_s_mem());
  277. bio = BIO_push(b64, bio);
  278. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  279. BIO_write(bio, buffer, bytes_read);
  280. BIO_flush(bio);
  281. BIO_get_mem_ptr(bio, &buffer_ptr);
  282. BIO_set_close(bio, BIO_NOCLOSE);
  283. BIO_free_all(bio);
  284. encoded_bytes = (*buffer_ptr).data;
  285. struct sockaddr_in ous_addr;
  286. ous_addr.sin_family = AF_INET;
  287. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  288. ous_addr.sin_port = htons(8888);
  289. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  290. if(ous_in < 0){
  291. printf("Failed to make ous_in socket\n");
  292. goto err;
  293. }
  294. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  295. if(error < 0){
  296. printf("Error connecting\n");
  297. goto err;
  298. }
  299. char *message = calloc(1, BUFSIZ);
  300. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  301. bytes_sent = send(ous_in, message, strlen(message), 0);
  302. #ifdef DEBUG_UPSTREAM
  303. printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
  304. #endif
  305. if(bytes_sent < 0){
  306. printf("Error writing to websocket\n");
  307. fflush(stdout);
  308. goto err;
  309. } else {
  310. close(ous_in);
  311. }
  312. p = buffer+sizeof(struct slitheen_up_hdr);
  313. #ifdef DEBUG_UPSTREAM
  314. for(i=0; i< bytes_read; i++){
  315. printf("%02x ", p[i]);
  316. }
  317. printf("\n");
  318. fflush(stdout);
  319. #endif
  320. struct socks_req *clnt_req = (struct socks_req *) p;
  321. p += 4;
  322. //see if it's a connect request
  323. if(clnt_req->cmd != 0x01){
  324. printf("Error: issued a non-connect command\n");
  325. fflush(stdout);
  326. goto err;
  327. }
  328. //now select on pipe (for downstream data) and the socket (for upstream data)
  329. for(;;){
  330. fd_set readfds;
  331. fd_set writefds;
  332. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  333. FD_ZERO(&readfds);
  334. FD_ZERO(&writefds);
  335. FD_SET(sockfd, &readfds);
  336. FD_SET(ous_out, &readfds);
  337. FD_SET(sockfd, &writefds);
  338. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  339. printf("Select error\n");
  340. fflush(stdout);
  341. continue;
  342. }
  343. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  344. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  345. if (bytes_read < 0){
  346. printf("Error reading from socket (in for loop)\n");
  347. fflush(stdout);
  348. goto err;
  349. }
  350. if(bytes_read == 0){
  351. //socket is closed
  352. printf("Closing connection for stream %d sockfd.\n", stream_id);
  353. fflush(stdout);
  354. //Send close message to slitheen proxy
  355. up_hdr = (struct slitheen_up_hdr *) buffer;
  356. up_hdr->stream_id = stream_id;
  357. up_hdr->len = 0;
  358. bio = BIO_new(BIO_s_mem());
  359. b64 = BIO_new(BIO_f_base64());
  360. bio = BIO_push(b64, bio);
  361. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  362. BIO_write(bio, buffer, 20);
  363. BIO_flush(bio);
  364. BIO_get_mem_ptr(bio, &buffer_ptr);
  365. encoded_bytes = (*buffer_ptr).data;
  366. BIO_set_close(bio, BIO_NOCLOSE);
  367. BIO_free_all(bio);
  368. uint8_t *ebytes = calloc(1, (*buffer_ptr).length+1);
  369. memcpy(ebytes, (*buffer_ptr).data, (*buffer_ptr).length);
  370. ebytes[(*buffer_ptr).length] = '\0';
  371. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  372. if(ous_in < 0){
  373. printf("Failed to make ous_in socket\n");
  374. fflush(stdout);
  375. goto err;
  376. }
  377. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  378. if(error < 0){
  379. printf("Error connecting\n");
  380. fflush(stdout);
  381. goto err;
  382. }
  383. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  384. strlen( (char *)ebytes)+1, ebytes);
  385. free(ebytes);
  386. bytes_sent = send(ous_in, message, strlen(message), 0);
  387. printf("Closing message: %s\n", message);
  388. close(ous_in);
  389. goto err;
  390. }
  391. if(bytes_read > 0){
  392. #ifdef DEBUG_UPSTREAM
  393. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  394. for(i=0; i< bytes_read; i++){
  395. printf("%02x ", buffer[i]);
  396. }
  397. printf("\n");
  398. printf("%s\n", buffer);
  399. fflush(stdout);
  400. #endif
  401. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  402. up_hdr = (struct slitheen_up_hdr *) buffer;
  403. up_hdr->stream_id = stream_id;
  404. up_hdr->len = htons(bytes_read);
  405. bytes_read+= sizeof(struct slitheen_up_hdr);
  406. bio = BIO_new(BIO_s_mem());
  407. b64 = BIO_new(BIO_f_base64());
  408. bio = BIO_push(b64, bio);
  409. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  410. BIO_write(bio, buffer, bytes_read);
  411. BIO_flush(bio);
  412. BIO_get_mem_ptr(bio, &buffer_ptr);
  413. BIO_set_close(bio, BIO_NOCLOSE);
  414. BIO_free_all(bio);
  415. encoded_bytes = (*buffer_ptr).data;
  416. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  417. if(ous_in < 0){
  418. printf("Failed to make ous_in socket\n");
  419. return 1;
  420. }
  421. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  422. if(error < 0){
  423. printf("Error connecting\n");
  424. return 1;
  425. }
  426. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  427. strlen(encoded_bytes)+1, encoded_bytes);
  428. bytes_sent = send(ous_in, message, strlen(message), 0);
  429. #ifdef DEBUG_UPSTREAM
  430. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, message);
  431. #endif
  432. close(ous_in);
  433. }
  434. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  435. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  436. if (bytes_read <= 0){
  437. printf("Error reading from ous_out (in for loop)\n");
  438. fflush(stdout);
  439. goto err;
  440. }
  441. if(bytes_read > 0){
  442. #ifdef DEBUG_DOWNSTREAM
  443. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  444. for(i=0; i< bytes_read; i++){
  445. printf("%02x ", buffer[i]);
  446. }
  447. printf("\n");
  448. printf("%s\n", buffer);
  449. fflush(stdout);
  450. #endif
  451. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  452. if(bytes_sent <= 0){
  453. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  454. }
  455. #ifdef DEBUG_DOWNSTREAM
  456. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  457. for(i=0; i< bytes_sent; i++){
  458. printf("%02x ", buffer[i]);
  459. }
  460. printf("\n");
  461. fflush(stdout);
  462. #endif
  463. }
  464. }
  465. }
  466. err:
  467. //should also remove stream from table
  468. close(sockfd);
  469. free(buffer);
  470. free(response);
  471. exit(0);
  472. }
  473. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  474. * the block and then write the data to the correct thread to be passed to the browser
  475. */
  476. void *demultiplex_data(){
  477. int32_t buffer_len = BUFSIZ;
  478. uint8_t *buffer = calloc(1, buffer_len);
  479. uint8_t *p;
  480. printf("Opening OUS_out... ");
  481. int32_t ous_fd = open("OUS_out", O_RDONLY);
  482. printf("done.\n");
  483. uint8_t *partial_block = NULL;
  484. uint32_t partial_block_len = 0;
  485. uint32_t resource_remaining = 0;
  486. uint64_t expected_next_count = 1;
  487. data_block *saved_data = NULL;
  488. for(;;){
  489. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  490. if(bytes_read > 0){
  491. int32_t bytes_remaining = bytes_read;
  492. p = buffer;
  493. //didn't read a full slitheen block last time
  494. if(partial_block_len > 0){
  495. //process first part of slitheen info
  496. memmove(buffer+partial_block_len, buffer, bytes_read);
  497. memcpy(buffer, partial_block, partial_block_len);
  498. bytes_remaining += partial_block_len;
  499. free(partial_block);
  500. partial_block = NULL;
  501. partial_block_len = 0;
  502. }
  503. while(bytes_remaining > 0){
  504. if(resource_remaining <= 0){//we're at a new resource
  505. //the first value for a new resource will be the resource length,
  506. //followed by a newline
  507. uint8_t *end_ptr;
  508. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  509. #ifdef DEBUG_PARSE
  510. printf("Starting new resource of len %d bytes\n", resource_remaining);
  511. printf("Resource len bytes:\n");
  512. int i;
  513. for(i=0; i< (end_ptr - p) + 1; i++){
  514. printf("%02x ", ((const char *) p)[i]);
  515. }
  516. printf("\n");
  517. #endif
  518. if(resource_remaining == 0){
  519. bytes_remaining -= (end_ptr - p) + 1;
  520. p += (end_ptr - p) + 1;
  521. } else {
  522. bytes_remaining -= (end_ptr - p) + 1;
  523. p += (end_ptr - p) + 1;
  524. }
  525. continue;
  526. }
  527. if(resource_remaining < SLITHEEN_HEADER_LEN){
  528. printf("ERROR: Resource remaining doesn't fit header len.\n");
  529. resource_remaining = 0;
  530. bytes_remaining = 0;
  531. break;
  532. }
  533. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  534. #ifdef DEBUG_PARSE
  535. printf("Partial header: ");
  536. int i;
  537. for(i = 0; i< bytes_remaining; i++){
  538. printf("%02x ", p[i]);
  539. }
  540. printf("\n");
  541. #endif
  542. if(partial_block != NULL) printf("UH OH (PB)\n");
  543. partial_block = calloc(1, bytes_remaining);
  544. memcpy(partial_block, p, bytes_remaining);
  545. partial_block_len = bytes_remaining;
  546. bytes_remaining = 0;
  547. break;
  548. }
  549. //decrypt header to see if we have entire block
  550. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  551. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  552. peek_header(tmp_header);
  553. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  554. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  555. #ifdef DEBUG_PARSE
  556. printf("Slitheen header:\n");
  557. int i;
  558. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  559. printf("%02x ", tmp_header[i]);
  560. }
  561. printf("\n");
  562. #endif
  563. if(ntohs(sl_hdr->len) > resource_remaining){
  564. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  565. resource_remaining = 0;
  566. bytes_remaining = 0;
  567. break;
  568. }
  569. if(ntohs(sl_hdr->len) > bytes_remaining){
  570. if(partial_block != NULL) printf("UH OH (PB)\n");
  571. partial_block = calloc(1, ntohs(sl_hdr->len));
  572. memcpy(partial_block, p, bytes_remaining);
  573. partial_block_len = bytes_remaining;
  574. bytes_remaining = 0;
  575. free(tmp_header);
  576. break;
  577. }
  578. super_decrypt(p);
  579. sl_hdr = (struct slitheen_hdr *) p;
  580. free(tmp_header);
  581. p += SLITHEEN_HEADER_LEN;
  582. bytes_remaining -= SLITHEEN_HEADER_LEN;
  583. resource_remaining -= SLITHEEN_HEADER_LEN;
  584. if((!sl_hdr->len) && (sl_hdr->garbage)){
  585. #ifdef DEBUG_PARSE
  586. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  587. #endif
  588. p += ntohs(sl_hdr->garbage);
  589. bytes_remaining -= ntohs(sl_hdr->garbage);
  590. resource_remaining -= ntohs(sl_hdr->garbage);
  591. continue;
  592. }
  593. int32_t pipe_fd =-1;
  594. if(connections->first == NULL){
  595. printf("Error: there are no connections\n");
  596. } else {
  597. connection *last = connections->first;
  598. if (last->stream_id == sl_hdr->stream_id){
  599. pipe_fd = last->pipe_fd;
  600. }
  601. while(last->next != NULL){
  602. last = last->next;
  603. if (last->stream_id == sl_hdr->stream_id){
  604. pipe_fd = last->pipe_fd;
  605. }
  606. }
  607. }
  608. if(pipe_fd == -1){
  609. printf("No stream id exists. Possibly invalid header\n");
  610. break;
  611. }
  612. #ifdef DEBUG_PARSE
  613. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  614. #endif
  615. //figure out how much to skip
  616. int32_t padding = 0;
  617. if(ntohs(sl_hdr->len) %16){
  618. padding = 16 - ntohs(sl_hdr->len)%16;
  619. }
  620. p += 16; //IV
  621. //check counter to see if we are missing data
  622. if(sl_hdr->counter > expected_next_count){
  623. //save any future data
  624. printf("Received header with count %lu. Expected count %lu.\n",
  625. sl_hdr->counter, expected_next_count);
  626. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  627. data_block *new_block = malloc(sizeof(data_block));
  628. new_block->count = sl_hdr->counter;
  629. new_block->len = ntohs(sl_hdr->len);
  630. new_block->data = malloc(ntohs(sl_hdr->len));
  631. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  632. new_block->pipe_fd = pipe_fd;
  633. new_block->next = saved_data;
  634. saved_data = new_block;
  635. } else {
  636. data_block *last = saved_data;
  637. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  638. last = last->next;
  639. }
  640. data_block *new_block = malloc(sizeof(data_block));
  641. new_block->count = sl_hdr->counter;
  642. new_block->len = ntohs(sl_hdr->len);
  643. new_block->data = malloc(ntohs(sl_hdr->len));
  644. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  645. new_block->pipe_fd = pipe_fd;
  646. new_block->next = last->next;
  647. last->next = new_block;
  648. }
  649. } else {
  650. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  651. if(bytes_sent <= 0){
  652. printf("Error reading to pipe for stream id %d\n",
  653. sl_hdr->stream_id);
  654. }
  655. //increment expected counter
  656. expected_next_count++;
  657. }
  658. //now check to see if there is saved data to write out
  659. if(saved_data != NULL){
  660. data_block *current_block = saved_data;
  661. while((current_block != NULL) &&
  662. (expected_next_count == current_block->count)){
  663. int32_t bytes_sent = write(current_block->pipe_fd,
  664. current_block->data, current_block->len);
  665. if(bytes_sent <= 0){
  666. printf("Error reading to pipe for stream id %d\n",
  667. sl_hdr->stream_id);
  668. }
  669. expected_next_count++;
  670. saved_data = current_block->next;
  671. free(current_block->data);
  672. free(current_block);
  673. current_block = saved_data;
  674. }
  675. }
  676. p += ntohs(sl_hdr->len); //encrypted data
  677. p += 16; //mac
  678. p += padding;
  679. p += ntohs(sl_hdr->garbage);
  680. bytes_remaining -=
  681. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  682. resource_remaining -=
  683. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  684. }
  685. } else {
  686. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  687. printf("Re-opening OUS_out... ");
  688. close(ous_fd);
  689. ous_fd = open("OUS_out", O_RDONLY);
  690. printf("done.\n");
  691. }
  692. }
  693. free(buffer);
  694. close(ous_fd);
  695. }