webm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. } else {
  75. //we want to skip this element
  76. f->webmstate = MID_ELEMENT;
  77. }
  78. p += header_len;
  79. remaining_len -= header_len;
  80. //parse length of header
  81. uint8_t int_len;
  82. uint64_t element_len = variable_length(p, &int_len);
  83. p += int_len;
  84. remaining_len -= int_len;
  85. printf("element length: %lu\n", element_len);
  86. f->remaining_element = element_len;
  87. break;
  88. case MID_ELEMENT:
  89. //The initial sequence of bytes contains everything up to the media
  90. //segments
  91. if(f->remaining_element <= remaining_len){
  92. //we have the entire element in this packet
  93. p += f->remaining_element;
  94. remaining_len -= f->remaining_element;
  95. f->remaining_element = 0;
  96. f->webmstate = BEGIN_ELEMENT;
  97. } else {
  98. //still have more of this element to process
  99. p += remaining_len;
  100. f->remaining_element -= remaining_len;
  101. remaining_len = 0;
  102. }
  103. break;
  104. case MEDIA:
  105. //We're replacing all of this element
  106. if(f->remaining_element <= remaining_len){
  107. //we have the entire element in this packet
  108. p += f->remaining_element;
  109. remaining_len -= f->remaining_element;
  110. f->remaining_element = 0;
  111. f->webmstate = BEGIN_ELEMENT;
  112. } else {
  113. //still have more of this element to process
  114. p += remaining_len;
  115. f->remaining_element -= remaining_len;
  116. remaining_len = 0;
  117. }
  118. break;
  119. }
  120. }
  121. printf("Remaining element: %lu\n", f->remaining_element);
  122. return 0;
  123. }
  124. static uint64_t variable_length(uint8_t *p, uint8_t *int_length){
  125. //first check for length of int
  126. uint8_t count = 1;
  127. uint32_t mask = 1 << 7;
  128. uint64_t len;
  129. while (count < 8) {
  130. if ((p[0] & mask) != 0) {
  131. break;
  132. }
  133. mask >>= 1;
  134. count += 1;
  135. }
  136. *int_length = count;
  137. //now calculate the integer
  138. len = p[0] & ~mask;
  139. for(int i=1; i< count; i++){
  140. len <<= 8;
  141. len |= p[i];
  142. }
  143. return len;
  144. }
  145. static uint32_t variable_header(uint8_t *p, uint8_t *int_length){
  146. //first check for length of int
  147. uint8_t count = 1;
  148. uint32_t mask = 1 << 7;
  149. uint32_t len;
  150. while (count < 4) {
  151. if ((p[0] & mask) != 0) {
  152. break;
  153. }
  154. mask >>= 1;
  155. count += 1;
  156. }
  157. *int_length = count;
  158. //now calculate the integer
  159. len = p[0];
  160. for(int i=1; i< count; i++){
  161. len <<= 8;
  162. len |= p[i];
  163. }
  164. return len;
  165. }