webm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. //we want to replace this block
  67. printf("Replaced simple block!\n");
  68. p[0] = 0xef;
  69. }
  70. p += header_len;
  71. remaining_len -= header_len;
  72. //parse length of header
  73. f->remaining_element = variable_length(p, &int_len);
  74. p += int_len;
  75. remaining_len -= int_len;
  76. printf("element length: %lu\n", f->remaining_element);
  77. f->webmstate = PARSE_ELEMENT;
  78. break;
  79. case PARSE_ELEMENT:
  80. switch(f->element_header) {
  81. case 0x18538067: //segment
  82. case 0x1f43b675: //cluster
  83. // do nothing. Move on to parsing sub-element
  84. f->webmstate = WEBM_HEADER;
  85. break;
  86. case 0xa3: //simple block
  87. f->webmstate = BLOCK_HEADER;
  88. break;
  89. default:
  90. //we want to skip this element
  91. f->webmstate = MID_ELEMENT;
  92. break;
  93. }
  94. break;
  95. case MID_ELEMENT: {
  96. uint32_t parse_len = (f->remaining_element <= remaining_len) ?
  97. f->remaining_element : remaining_len;
  98. if (f->element_header == 0xa3) {
  99. //replace content
  100. printf("Replaceable data (%d bytes):\n", parse_len);
  101. for(int i=0; i< parse_len; i++){
  102. printf("%02x ", p[i]);
  103. }
  104. printf("\n");
  105. fill_with_downstream(f, p, parse_len);
  106. }
  107. p += parse_len;
  108. remaining_len -= parse_len;
  109. f->remaining_element -= parse_len;
  110. if (f->remaining_element == 0) {
  111. f->webmstate = WEBM_HEADER;
  112. }
  113. break;
  114. }
  115. case BLOCK_HEADER:
  116. //TODO: expand to handle lacing, non-simple blocks
  117. if(remaining_len < 4){
  118. //TODO: fix this somehow
  119. printf("PARSE FAIL: too little len remaining\n");
  120. return 1;
  121. }
  122. p += 4;
  123. f->remaining_element -= 4;
  124. remaining_len -= 4;
  125. f->webmstate = MID_ELEMENT;
  126. break;
  127. }
  128. }
  129. printf("Remaining element: %lu\n", f->remaining_element);
  130. return 0;
  131. }
  132. static uint64_t variable_length(uint8_t *p, uint8_t *int_length){
  133. //first check for length of int
  134. uint8_t count = 1;
  135. uint32_t mask = 1 << 7;
  136. uint64_t len;
  137. while (count < 8) {
  138. if ((p[0] & mask) != 0) {
  139. break;
  140. }
  141. mask >>= 1;
  142. count += 1;
  143. }
  144. *int_length = count;
  145. //now calculate the integer
  146. len = p[0] & ~mask;
  147. for(int i=1; i< count; i++){
  148. len <<= 8;
  149. len |= p[i];
  150. }
  151. return len;
  152. }
  153. static uint32_t variable_header(uint8_t *p, uint8_t *int_length){
  154. //first check for length of int
  155. uint8_t count = 1;
  156. uint32_t mask = 1 << 7;
  157. uint32_t len;
  158. while (count < 4) {
  159. if ((p[0] & mask) != 0) {
  160. break;
  161. }
  162. mask >>= 1;
  163. count += 1;
  164. }
  165. *int_length = count;
  166. //now calculate the integer
  167. len = p[0];
  168. for(int i=1; i< count; i++){
  169. len <<= 8;
  170. len |= p[i];
  171. }
  172. return len;
  173. }