SPProtoDecoder.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @file SPProtoDecoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <misc/debug.h>
  24. #include <misc/balign.h>
  25. #include <misc/byteorder.h>
  26. #include <security/bhash.h>
  27. #include <flow/SPProtoDecoder.h>
  28. static int decode_packet (SPProtoDecoder *o, uint8_t *in, int in_len, uint8_t **out, int *out_len)
  29. {
  30. ASSERT(in_len >= 0)
  31. ASSERT(in_len <= o->input_mtu)
  32. uint8_t *plaintext;
  33. int plaintext_len;
  34. // decrypt if needed
  35. if (!SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  36. plaintext = in;
  37. plaintext_len = in_len;
  38. } else {
  39. // check length
  40. if (in_len % o->enc_block_size != 0) {
  41. DEBUG("packet size not a multiple of block size");
  42. return 0;
  43. }
  44. if (in_len < o->enc_block_size) {
  45. DEBUG("packet does not have an IV");
  46. return 0;
  47. }
  48. // check if we have encryption key
  49. if (!o->have_encryption_key) {
  50. DEBUG("have no encryption key");
  51. return 0;
  52. }
  53. // copy IV as BEncryption_Decrypt changes the IV
  54. uint8_t iv[o->enc_block_size];
  55. memcpy(iv, in, o->enc_block_size);
  56. // decrypt
  57. uint8_t *ciphertext = in + o->enc_block_size;
  58. int ciphertext_len = in_len - o->enc_block_size;
  59. plaintext = o->buf;
  60. BEncryption_Decrypt(&o->encryptor, ciphertext, plaintext, ciphertext_len, iv);
  61. // read padding
  62. if (ciphertext_len < o->enc_block_size) {
  63. DEBUG("packet does not have a padding block");
  64. return 0;
  65. }
  66. int i;
  67. for (i = ciphertext_len - 1; i >= ciphertext_len - o->enc_block_size; i--) {
  68. if (plaintext[i] == 1) {
  69. break;
  70. }
  71. if (plaintext[i] != 0) {
  72. DEBUG("packet padding wrong (nonzero byte)");
  73. return 0;
  74. }
  75. }
  76. if (i < ciphertext_len - o->enc_block_size) {
  77. DEBUG("packet padding wrong (all zeroes)");
  78. return 0;
  79. }
  80. plaintext_len = i;
  81. }
  82. // check for header
  83. if (plaintext_len < SPPROTO_HEADER_LEN(o->sp_params)) {
  84. DEBUG("packet has no header");
  85. return 0;
  86. }
  87. uint8_t *header = plaintext;
  88. // check data length
  89. if (plaintext_len - SPPROTO_HEADER_LEN(o->sp_params) > o->output_mtu) {
  90. DEBUG("packet too long");
  91. return 0;
  92. }
  93. // check OTP
  94. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  95. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  96. if (!OTPChecker_CheckOTP(&o->otpchecker, ltoh16(header_otpd->seed_id), header_otpd->otp)) {
  97. DEBUG("packet has wrong OTP");
  98. return 0;
  99. }
  100. }
  101. // check hash
  102. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  103. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  104. // read hash
  105. uint8_t hash[o->hash_size];
  106. memcpy(hash, header_hash, o->hash_size);
  107. // zero hash in packet
  108. memset(header_hash, 0, o->hash_size);
  109. // calculate hash
  110. uint8_t hash_calc[o->hash_size];
  111. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash_calc);
  112. // set hash field to its original value
  113. memcpy(header_hash, hash, o->hash_size);
  114. // compare hashes
  115. if (memcmp(hash, hash_calc, o->hash_size)) {
  116. DEBUG("packet has wrong hash");
  117. return 0;
  118. }
  119. }
  120. // return packet
  121. *out = plaintext + SPPROTO_HEADER_LEN(o->sp_params);
  122. *out_len = plaintext_len - SPPROTO_HEADER_LEN(o->sp_params);
  123. return 1;
  124. }
  125. static int input_handler_send (SPProtoDecoder *o, uint8_t *data, int data_len)
  126. {
  127. ASSERT(data_len >= 0)
  128. ASSERT(data_len <= o->input_mtu)
  129. // attempt to decode packet
  130. uint8_t *out;
  131. int out_len;
  132. if (!decode_packet(o, data, data_len, &out, &out_len)) {
  133. return 1;
  134. }
  135. // submit decoded packet to output
  136. DEAD_ENTER(o->dead)
  137. int res = PacketPassInterface_Sender_Send(o->output, out, out_len);
  138. if (DEAD_LEAVE(o->dead)) {
  139. return -1;
  140. }
  141. ASSERT(res == 0 || res == 1)
  142. return res;
  143. }
  144. static void output_handler_done (SPProtoDecoder *o)
  145. {
  146. PacketPassInterface_Done(&o->input);
  147. return;
  148. }
  149. int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds)
  150. {
  151. ASSERT(spproto_validate_security_params(sp_params))
  152. ASSERT(!SPPROTO_HAVE_OTP(sp_params) || num_otp_seeds >= 2)
  153. // init arguments
  154. o->output = output;
  155. o->sp_params = sp_params;
  156. // init dead var
  157. DEAD_INIT(o->dead);
  158. // init output
  159. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  160. // remember output MTU
  161. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  162. // calculate hash size
  163. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  164. o->hash_size = BHash_size(o->sp_params.hash_mode);
  165. }
  166. // calculate encryption block and key sizes
  167. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  168. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  169. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  170. }
  171. // calculate input MTU
  172. o->input_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->output_mtu);
  173. // allocate plaintext buffer
  174. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  175. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
  176. if (!(o->buf = malloc(buf_size))) {
  177. goto fail0;
  178. }
  179. }
  180. // init input
  181. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o);
  182. // init OTP checker
  183. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  184. if (!OTPChecker_Init(&o->otpchecker, o->sp_params.otp_num, o->sp_params.otp_mode, num_otp_seeds)) {
  185. goto fail1;
  186. }
  187. }
  188. // have no encryption key
  189. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  190. o->have_encryption_key = 0;
  191. }
  192. // init debug object
  193. DebugObject_Init(&o->d_obj);
  194. return 1;
  195. fail1:
  196. PacketPassInterface_Free(&o->input);
  197. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  198. free(o->buf);
  199. }
  200. fail0:
  201. return 0;
  202. }
  203. void SPProtoDecoder_Free (SPProtoDecoder *o)
  204. {
  205. // free debug object
  206. DebugObject_Free(&o->d_obj);
  207. // free encryptor
  208. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  209. BEncryption_Free(&o->encryptor);
  210. }
  211. // free OTP checker
  212. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  213. OTPChecker_Free(&o->otpchecker);
  214. }
  215. // free input
  216. PacketPassInterface_Free(&o->input);
  217. // free plaintext buffer
  218. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  219. free(o->buf);
  220. }
  221. // kill dead var
  222. DEAD_KILL(o->dead);
  223. }
  224. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o)
  225. {
  226. return &o->input;
  227. }
  228. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key)
  229. {
  230. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  231. // free encryptor
  232. if (o->have_encryption_key) {
  233. BEncryption_Free(&o->encryptor);
  234. }
  235. // init encryptor
  236. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_DECRYPT, o->sp_params.encryption_mode, encryption_key);
  237. // have encryption key
  238. o->have_encryption_key = 1;
  239. }
  240. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o)
  241. {
  242. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  243. if (o->have_encryption_key) {
  244. // free encryptor
  245. BEncryption_Free(&o->encryptor);
  246. // have no encryption key
  247. o->have_encryption_key = 0;
  248. }
  249. }
  250. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  251. {
  252. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  253. OTPChecker_AddSeed(&o->otpchecker, seed_id, key, iv);
  254. }
  255. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o)
  256. {
  257. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  258. OTPChecker_RemoveSeeds(&o->otpchecker);
  259. }