socks5proxy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 [name of library's license],
  22. * the licensors of this Program grant you additional permission to convey
  23. * the resulting work. {Corresponding Source for a non-source form of such
  24. * a combination shall include the source code for the parts of the OpenSSL
  25. * library used as well as that of the covered work.}
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <stdint.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <sys/socket.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <netinet/in.h>
  37. #include <netdb.h>
  38. #include <pthread.h>
  39. #include <fcntl.h>
  40. #include <arpa/inet.h>
  41. #include <openssl/bio.h>
  42. #include <openssl/evp.h>
  43. #include <openssl/buffer.h>
  44. #include <openssl/rand.h>
  45. #include "socks5proxy.h"
  46. #include "crypto.h"
  47. #include "tagging.h"
  48. static connection_table *connections;
  49. int main(void){
  50. int listen_socket;
  51. struct sockaddr_in address;
  52. struct sockaddr_in remote_addr;
  53. socklen_t addr_size;
  54. mkfifo("OUS_out", 0666);
  55. //generate Slitheen ID using Telex tagging method
  56. uint8_t slitheen_id[SLITHEEN_ID_LEN];
  57. uint8_t shared_secret[16];
  58. generate_slitheen_id(slitheen_id, shared_secret);
  59. //RAND_bytes(slitheen_id, SLITHEEN_ID_LEN);
  60. printf("Randomly generated slitheen id: ");
  61. int i;
  62. for(i=0; i< SLITHEEN_ID_LEN; i++){
  63. printf("%02x ", slitheen_id[i]);
  64. }
  65. printf("\n");
  66. // Calculate super encryption keys
  67. generate_super_keys(shared_secret);
  68. //b64 encode slitheen ID
  69. char *encoded_bytes;
  70. BUF_MEM *buffer_ptr;
  71. BIO *bio, *b64;
  72. b64 = BIO_new(BIO_f_base64());
  73. bio = BIO_new(BIO_s_mem());
  74. bio = BIO_push(b64, bio);
  75. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  76. BIO_write(bio, slitheen_id, SLITHEEN_ID_LEN);
  77. BIO_flush(bio);
  78. BIO_get_mem_ptr(bio, &buffer_ptr);
  79. BIO_set_close(bio, BIO_NOCLOSE);
  80. BIO_free_all(bio);
  81. encoded_bytes = (*buffer_ptr).data;
  82. encoded_bytes[(*buffer_ptr).length] = '\0';
  83. //give encoded slitheen ID to ous
  84. struct sockaddr_in ous_addr;
  85. ous_addr.sin_family = AF_INET;
  86. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  87. ous_addr.sin_port = htons(8888);
  88. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  89. if(ous_in < 0){
  90. printf("Failed to make ous_in socket\n");
  91. return 1;
  92. }
  93. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  94. if(error < 0){
  95. printf("Error connecting\n");
  96. return 1;
  97. }
  98. char *message = calloc(1, BUFSIZ);
  99. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes), encoded_bytes);
  100. int32_t bytes_sent = send(ous_in, message, strlen(message), 0);
  101. printf("Wrote %d bytes to OUS_in:\n %s\n", bytes_sent, message);
  102. free(message);
  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. char *message = calloc(1, BUFSIZ);
  299. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  300. bytes_sent = send(ous_in, message, strlen(message), 0);
  301. #ifdef DEBUG_UPSTREAM
  302. printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
  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. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  383. strlen( (char *)ebytes)+1, ebytes);
  384. free(ebytes);
  385. bytes_sent = send(ous_in, message, strlen(message), 0);
  386. printf("Closing message: %s\n", message);
  387. close(ous_in);
  388. goto err;
  389. }
  390. if(bytes_read > 0){
  391. #ifdef DEBUG_UPSTREAM
  392. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  393. for(i=0; i< bytes_read; i++){
  394. printf("%02x ", buffer[i]);
  395. }
  396. printf("\n");
  397. printf("%s\n", buffer);
  398. fflush(stdout);
  399. #endif
  400. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  401. up_hdr = (struct slitheen_up_hdr *) buffer;
  402. up_hdr->stream_id = stream_id;
  403. up_hdr->len = htons(bytes_read);
  404. bytes_read+= sizeof(struct slitheen_up_hdr);
  405. bio = BIO_new(BIO_s_mem());
  406. b64 = BIO_new(BIO_f_base64());
  407. bio = BIO_push(b64, bio);
  408. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  409. BIO_write(bio, buffer, bytes_read);
  410. BIO_flush(bio);
  411. BIO_get_mem_ptr(bio, &buffer_ptr);
  412. BIO_set_close(bio, BIO_NOCLOSE);
  413. BIO_free_all(bio);
  414. encoded_bytes = (*buffer_ptr).data;
  415. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  416. if(ous_in < 0){
  417. printf("Failed to make ous_in socket\n");
  418. return 1;
  419. }
  420. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  421. if(error < 0){
  422. printf("Error connecting\n");
  423. return 1;
  424. }
  425. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  426. strlen(encoded_bytes)+1, encoded_bytes);
  427. bytes_sent = send(ous_in, message, strlen(message), 0);
  428. #ifdef DEBUG_UPSTREAM
  429. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, message);
  430. #endif
  431. close(ous_in);
  432. }
  433. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  434. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  435. if (bytes_read <= 0){
  436. printf("Error reading from ous_out (in for loop)\n");
  437. fflush(stdout);
  438. goto err;
  439. }
  440. if(bytes_read > 0){
  441. #ifdef DEBUG_DOWNSTREAM
  442. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  443. for(i=0; i< bytes_read; i++){
  444. printf("%02x ", buffer[i]);
  445. }
  446. printf("\n");
  447. printf("%s\n", buffer);
  448. fflush(stdout);
  449. #endif
  450. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  451. if(bytes_sent <= 0){
  452. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  453. }
  454. #ifdef DEBUG_DOWNSTREAM
  455. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  456. for(i=0; i< bytes_sent; i++){
  457. printf("%02x ", buffer[i]);
  458. }
  459. printf("\n");
  460. fflush(stdout);
  461. #endif
  462. }
  463. }
  464. }
  465. err:
  466. //should also remove stream from table
  467. close(sockfd);
  468. free(buffer);
  469. free(response);
  470. exit(0);
  471. }
  472. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  473. * the block and then write the data to the correct thread to be passed to the browser
  474. */
  475. void *demultiplex_data(){
  476. int32_t buffer_len = BUFSIZ;
  477. uint8_t *buffer = calloc(1, buffer_len);
  478. uint8_t *p;
  479. printf("Opening OUS_out... ");
  480. int32_t ous_fd = open("OUS_out", O_RDONLY);
  481. printf("done.\n");
  482. uint8_t *partial_block = NULL;
  483. uint32_t partial_block_len = 0;
  484. uint32_t resource_remaining = 0;
  485. uint64_t expected_next_count = 1;
  486. data_block *saved_data = NULL;
  487. for(;;){
  488. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  489. if(bytes_read > 0){
  490. int32_t bytes_remaining = bytes_read;
  491. p = buffer;
  492. //didn't read a full slitheen block last time
  493. if(partial_block_len > 0){
  494. //process first part of slitheen info
  495. memmove(buffer+partial_block_len, buffer, bytes_read);
  496. memcpy(buffer, partial_block, partial_block_len);
  497. bytes_remaining += partial_block_len;
  498. free(partial_block);
  499. partial_block = NULL;
  500. partial_block_len = 0;
  501. }
  502. while(bytes_remaining > 0){
  503. if(resource_remaining <= 0){//we're at a new resource
  504. //the first value for a new resource will be the resource length,
  505. //followed by a newline
  506. uint8_t *end_ptr;
  507. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  508. #ifdef DEBUG_PARSE
  509. printf("Starting new resource of len %d bytes\n", resource_remaining);
  510. printf("Resource len bytes:\n");
  511. int i;
  512. for(i=0; i< (end_ptr - p) + 1; i++){
  513. printf("%02x ", ((const char *) p)[i]);
  514. }
  515. printf("\n");
  516. #endif
  517. if(resource_remaining == 0){
  518. bytes_remaining -= (end_ptr - p) + 1;
  519. p += (end_ptr - p) + 1;
  520. } else {
  521. bytes_remaining -= (end_ptr - p) + 1;
  522. p += (end_ptr - p) + 1;
  523. }
  524. continue;
  525. }
  526. if(resource_remaining < SLITHEEN_HEADER_LEN){
  527. printf("ERROR: Resource remaining doesn't fit header len.\n");
  528. resource_remaining = 0;
  529. bytes_remaining = 0;
  530. break;
  531. }
  532. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  533. #ifdef DEBUG_PARSE
  534. printf("Partial header: ");
  535. int i;
  536. for(i = 0; i< bytes_remaining; i++){
  537. printf("%02x ", p[i]);
  538. }
  539. printf("\n");
  540. #endif
  541. if(partial_block != NULL) printf("UH OH (PB)\n");
  542. partial_block = calloc(1, bytes_remaining);
  543. memcpy(partial_block, p, bytes_remaining);
  544. partial_block_len = bytes_remaining;
  545. bytes_remaining = 0;
  546. break;
  547. }
  548. //decrypt header to see if we have entire block
  549. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  550. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  551. peek_header(tmp_header);
  552. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  553. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  554. #ifdef DEBUG_PARSE
  555. printf("Slitheen header:\n");
  556. int i;
  557. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  558. printf("%02x ", tmp_header[i]);
  559. }
  560. printf("\n");
  561. #endif
  562. if(ntohs(sl_hdr->len) > resource_remaining){
  563. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  564. resource_remaining = 0;
  565. bytes_remaining = 0;
  566. break;
  567. }
  568. if(ntohs(sl_hdr->len) > bytes_remaining){
  569. if(partial_block != NULL) printf("UH OH (PB)\n");
  570. partial_block = calloc(1, ntohs(sl_hdr->len));
  571. memcpy(partial_block, p, bytes_remaining);
  572. partial_block_len = bytes_remaining;
  573. bytes_remaining = 0;
  574. free(tmp_header);
  575. break;
  576. }
  577. super_decrypt(p);
  578. sl_hdr = (struct slitheen_hdr *) p;
  579. free(tmp_header);
  580. p += SLITHEEN_HEADER_LEN;
  581. bytes_remaining -= SLITHEEN_HEADER_LEN;
  582. resource_remaining -= SLITHEEN_HEADER_LEN;
  583. if((!sl_hdr->len) && (sl_hdr->garbage)){
  584. #ifdef DEBUG_PARSE
  585. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  586. #endif
  587. p += ntohs(sl_hdr->garbage);
  588. bytes_remaining -= ntohs(sl_hdr->garbage);
  589. resource_remaining -= ntohs(sl_hdr->garbage);
  590. continue;
  591. }
  592. int32_t pipe_fd =-1;
  593. if(connections->first == NULL){
  594. printf("Error: there are no connections\n");
  595. } else {
  596. connection *last = connections->first;
  597. if (last->stream_id == sl_hdr->stream_id){
  598. pipe_fd = last->pipe_fd;
  599. }
  600. while(last->next != NULL){
  601. last = last->next;
  602. if (last->stream_id == sl_hdr->stream_id){
  603. pipe_fd = last->pipe_fd;
  604. }
  605. }
  606. }
  607. if(pipe_fd == -1){
  608. printf("No stream id exists. Possibly invalid header\n");
  609. break;
  610. }
  611. #ifdef DEBUG_PARSE
  612. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  613. #endif
  614. //figure out how much to skip
  615. int32_t padding = 0;
  616. if(ntohs(sl_hdr->len) %16){
  617. padding = 16 - ntohs(sl_hdr->len)%16;
  618. }
  619. p += 16; //IV
  620. //check counter to see if we are missing data
  621. if(sl_hdr->counter > expected_next_count){
  622. //save any future data
  623. printf("Received header with count %lu. Expected count %lu.\n",
  624. sl_hdr->counter, expected_next_count);
  625. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  626. data_block *new_block = malloc(sizeof(data_block));
  627. new_block->count = sl_hdr->counter;
  628. new_block->len = ntohs(sl_hdr->len);
  629. new_block->data = malloc(ntohs(sl_hdr->len));
  630. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  631. new_block->pipe_fd = pipe_fd;
  632. new_block->next = saved_data;
  633. saved_data = new_block;
  634. } else {
  635. data_block *last = saved_data;
  636. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  637. last = last->next;
  638. }
  639. data_block *new_block = malloc(sizeof(data_block));
  640. new_block->count = sl_hdr->counter;
  641. new_block->len = ntohs(sl_hdr->len);
  642. new_block->data = malloc(ntohs(sl_hdr->len));
  643. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  644. new_block->pipe_fd = pipe_fd;
  645. new_block->next = last->next;
  646. last->next = new_block;
  647. }
  648. } else {
  649. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  650. if(bytes_sent <= 0){
  651. printf("Error reading to pipe for stream id %d\n",
  652. sl_hdr->stream_id);
  653. }
  654. //increment expected counter
  655. expected_next_count++;
  656. }
  657. //now check to see if there is saved data to write out
  658. if(saved_data != NULL){
  659. data_block *current_block = saved_data;
  660. while((current_block != NULL) &&
  661. (expected_next_count == current_block->count)){
  662. int32_t bytes_sent = write(current_block->pipe_fd,
  663. current_block->data, current_block->len);
  664. if(bytes_sent <= 0){
  665. printf("Error reading to pipe for stream id %d\n",
  666. sl_hdr->stream_id);
  667. }
  668. expected_next_count++;
  669. saved_data = current_block->next;
  670. free(current_block->data);
  671. free(current_block);
  672. current_block = saved_data;
  673. }
  674. }
  675. p += ntohs(sl_hdr->len); //encrypted data
  676. p += 16; //mac
  677. p += padding;
  678. p += ntohs(sl_hdr->garbage);
  679. bytes_remaining -=
  680. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  681. resource_remaining -=
  682. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  683. }
  684. } else {
  685. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  686. printf("Re-opening OUS_out... ");
  687. close(ous_fd);
  688. ous_fd = open("OUS_out", O_RDONLY);
  689. printf("done.\n");
  690. }
  691. }
  692. free(buffer);
  693. close(ous_fd);
  694. }