webm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Name: webm.c
  2. *
  3. * This file contains functions for manipulating tagged flows.
  4. *
  5. * Slitheen - a decoy routing system for censorship resistance
  6. * Copyright (C) 2018 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 3.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Additional permission under GNU GPL version 3 section 7
  21. *
  22. * If you modify this Program, or any covered work, by linking or combining
  23. * it with the OpenSSL library (or a modified version of that library),
  24. * containing parts covered by the terms of the OpenSSL Licence and the
  25. * SSLeay license, the licensors of this Program grant you additional
  26. * permission to convey the resulting work. Corresponding Source for a
  27. * non-source form of such a combination shall include the source code
  28. * for the parts of the OpenSSL library used as well as that of the covered
  29. * work.
  30. */
  31. #include <stdio.h>
  32. #include <stdint.h>
  33. #include "webm.h"
  34. #include "flow.h"
  35. #include "relay.h"
  36. static uint64_t variable_length(uint8_t *p, uint8_t *int_length);
  37. static uint32_t variable_header(uint8_t *p, uint8_t *int_length);
  38. /**
  39. * Parses the webm content type
  40. *
  41. * Returns 0 on success 1 on failure
  42. */
  43. int32_t parse_webm(flow *f, uint8_t *ptr, uint32_t len) {
  44. if(!f->webmstate){
  45. //make sure this is a webm resource
  46. return 1;
  47. }
  48. uint8_t *p = ptr;
  49. uint32_t remaining_len = len;
  50. while (remaining_len){
  51. switch (f->webmstate){
  52. uint8_t header_len, int_len;
  53. case WEBM_HEADER:
  54. if(remaining_len < 8){
  55. //TODO:right now this assumes we'll have the header + size
  56. // but later we should make it work with just the header
  57. // also the size should be 8 bytes max
  58. //this will be difficult to parse
  59. printf("PARSE FAIL: too little len remaining\n");
  60. return 1;
  61. }
  62. //Parse header:
  63. f->element_header = variable_header(p, &header_len);
  64. printf("Received header: %x\n", f->element_header);
  65. if((f->element_header == 0xa3) &&
  66. (remaining_len >= (SLITHEEN_HEADER_LEN + 9))){
  67. //we want to replace this block
  68. printf("Replaced simple block!\n");
  69. p[0] = 0xef;
  70. }
  71. p += header_len;
  72. remaining_len -= header_len;
  73. //parse length of header
  74. f->remaining_element = variable_length(p, &int_len);
  75. p += int_len;
  76. remaining_len -= int_len;
  77. printf("element length: %lu\n", f->remaining_element);
  78. f->webmstate = PARSE_ELEMENT;
  79. break;
  80. case PARSE_ELEMENT:
  81. switch(f->element_header) {
  82. case 0x18538067: //segment
  83. case 0x1f43b675: //cluster
  84. // do nothing. Move on to parsing sub-element
  85. f->webmstate = WEBM_HEADER;
  86. break;
  87. case 0xa3: //simple block
  88. f->webmstate = BLOCK_HEADER;
  89. break;
  90. default:
  91. //we want to skip this element
  92. f->webmstate = MID_ELEMENT;
  93. break;
  94. }
  95. break;
  96. case MID_ELEMENT: {
  97. uint32_t parse_len = (f->remaining_element <= remaining_len) ?
  98. f->remaining_element : remaining_len;
  99. if (f->element_header == 0xa3) {
  100. //replace content
  101. fill_with_downstream(f, p, parse_len);
  102. printf("Replaced data (%d bytes):\n", parse_len);
  103. for(int i=0; i< parse_len; i++){
  104. printf("%02x ", p[i]);
  105. }
  106. printf("\n");
  107. }
  108. p += parse_len;
  109. remaining_len -= parse_len;
  110. f->remaining_element -= parse_len;
  111. if (f->remaining_element == 0) {
  112. f->webmstate = WEBM_HEADER;
  113. }
  114. break;
  115. }
  116. case BLOCK_HEADER:
  117. //TODO: expand to handle lacing, non-simple blocks
  118. if(remaining_len < 4){
  119. //TODO: fix this somehow
  120. printf("PARSE FAIL: too little len remaining\n");
  121. return 1;
  122. }
  123. p += 4;
  124. f->remaining_element -= 4;
  125. remaining_len -= 4;
  126. f->webmstate = MID_ELEMENT;
  127. break;
  128. }
  129. }
  130. printf("Remaining element: %lu\n", f->remaining_element);
  131. return 0;
  132. }
  133. static uint64_t variable_length(uint8_t *p, uint8_t *int_length){
  134. //first check for length of int
  135. uint8_t count = 1;
  136. uint32_t mask = 1 << 7;
  137. uint64_t len;
  138. while (count < 8) {
  139. if ((p[0] & mask) != 0) {
  140. break;
  141. }
  142. mask >>= 1;
  143. count += 1;
  144. }
  145. *int_length = count;
  146. //now calculate the integer
  147. len = p[0] & ~mask;
  148. for(int i=1; i< count; i++){
  149. len <<= 8;
  150. len |= p[i];
  151. }
  152. return len;
  153. }
  154. static uint32_t variable_header(uint8_t *p, uint8_t *int_length){
  155. //first check for length of int
  156. uint8_t count = 1;
  157. uint32_t mask = 1 << 7;
  158. uint32_t len;
  159. while (count < 4) {
  160. if ((p[0] & mask) != 0) {
  161. break;
  162. }
  163. mask >>= 1;
  164. count += 1;
  165. }
  166. *int_length = count;
  167. //now calculate the integer
  168. len = p[0];
  169. for(int i=1; i< count; i++){
  170. len <<= 8;
  171. len |= p[i];
  172. }
  173. return len;
  174. }