webm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. static uint64_t variable_length(uint8_t *p, uint8_t *int_length);
  36. static uint32_t variable_header(uint8_t *p, uint8_t *int_length);
  37. /**
  38. * Parses the webm content type
  39. *
  40. * Returns 0 on success 1 on failure
  41. */
  42. int32_t parse_webm(flow *f, uint8_t *ptr, uint32_t len) {
  43. if(!f->webmstate){
  44. //make sure this is a webm resource
  45. return 1;
  46. }
  47. uint8_t *p = ptr;
  48. uint32_t remaining_len = len;
  49. while (remaining_len){
  50. switch (f->webmstate){
  51. case BEGIN_ELEMENT:
  52. if(remaining_len < 8){
  53. //this will be difficult to parse
  54. //TODO: make this easier to deal with
  55. printf("PARSE FAIL: too little len remaining\n");
  56. return 1;
  57. }
  58. //The only elements we care about are:
  59. // the segment header (0x18538067), and
  60. // the cluster header (0x1f43b675).
  61. //Parse header:
  62. uint8_t header_len;
  63. uint32_t header = variable_header(p, &header_len);
  64. printf("Received header: %x\n", header);
  65. if (header == 0x18538067) {
  66. // do nothing. Move on to parsing sub-element
  67. } else if (header == 0x1f43b675) {
  68. f->webmstate = MEDIA;
  69. //replace with slitheen header
  70. p[0] = 0x16; //'SYN'
  71. p[1] = 0x73; //'s'
  72. p[2] = 0x6c; //'l'
  73. p[3] = 0x69; //'i'
  74. printf("Replaced cluster with slitheen segment!\n");
  75. } else {
  76. //we want to skip this element
  77. f->webmstate = MID_ELEMENT;
  78. }
  79. p += header_len;
  80. remaining_len -= header_len;
  81. //parse length of header
  82. uint8_t int_len;
  83. uint64_t element_len = variable_length(p, &int_len);
  84. p += int_len;
  85. remaining_len -= int_len;
  86. printf("element length: %lu\n", element_len);
  87. f->remaining_element = element_len;
  88. break;
  89. case MID_ELEMENT:
  90. //The initial sequence of bytes contains everything up to the media
  91. //segments
  92. if(f->remaining_element <= remaining_len){
  93. //we have the entire element in this packet
  94. p += f->remaining_element;
  95. remaining_len -= f->remaining_element;
  96. f->remaining_element = 0;
  97. f->webmstate = BEGIN_ELEMENT;
  98. } else {
  99. //still have more of this element to process
  100. p += remaining_len;
  101. f->remaining_element -= remaining_len;
  102. remaining_len = 0;
  103. }
  104. break;
  105. case MEDIA:
  106. //We're replacing all of this element
  107. if(f->remaining_element <= remaining_len){
  108. //we have the entire element in this packet
  109. p += f->remaining_element;
  110. remaining_len -= f->remaining_element;
  111. f->remaining_element = 0;
  112. f->webmstate = BEGIN_ELEMENT;
  113. } else {
  114. //still have more of this element to process
  115. p += remaining_len;
  116. f->remaining_element -= remaining_len;
  117. remaining_len = 0;
  118. }
  119. break;
  120. }
  121. }
  122. printf("Remaining element: %lu\n", f->remaining_element);
  123. return 0;
  124. }
  125. static uint64_t variable_length(uint8_t *p, uint8_t *int_length){
  126. //first check for length of int
  127. uint8_t count = 1;
  128. uint32_t mask = 1 << 7;
  129. uint64_t len;
  130. while (count < 8) {
  131. if ((p[0] & mask) != 0) {
  132. break;
  133. }
  134. mask >>= 1;
  135. count += 1;
  136. }
  137. *int_length = count;
  138. //now calculate the integer
  139. len = p[0] & ~mask;
  140. for(int i=1; i< count; i++){
  141. len <<= 8;
  142. len |= p[i];
  143. }
  144. return len;
  145. }
  146. static uint32_t variable_header(uint8_t *p, uint8_t *int_length){
  147. //first check for length of int
  148. uint8_t count = 1;
  149. uint32_t mask = 1 << 7;
  150. uint32_t len;
  151. while (count < 4) {
  152. if ((p[0] & mask) != 0) {
  153. break;
  154. }
  155. mask >>= 1;
  156. count += 1;
  157. }
  158. *int_length = count;
  159. //now calculate the integer
  160. len = p[0];
  161. for(int i=1; i< count; i++){
  162. len <<= 8;
  163. len |= p[i];
  164. }
  165. return len;
  166. }