SPProtoDecoder.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. uint16_t seed_id = ltoh16(header_otpd->seed_id);
  97. if (!OTPChecker_CheckOTP(&o->otpchecker, seed_id, header_otpd->otp)) {
  98. DEBUG("packet has wrong OTP");
  99. return 0;
  100. }
  101. }
  102. // check hash
  103. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  104. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  105. // read hash
  106. uint8_t hash[o->hash_size];
  107. memcpy(hash, header_hash, o->hash_size);
  108. // zero hash in packet
  109. memset(header_hash, 0, o->hash_size);
  110. // calculate hash
  111. uint8_t hash_calc[o->hash_size];
  112. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash_calc);
  113. // set hash field to its original value
  114. memcpy(header_hash, hash, o->hash_size);
  115. // compare hashes
  116. if (memcmp(hash, hash_calc, o->hash_size)) {
  117. DEBUG("packet has wrong hash");
  118. return 0;
  119. }
  120. }
  121. // return packet
  122. *out = plaintext + SPPROTO_HEADER_LEN(o->sp_params);
  123. *out_len = plaintext_len - SPPROTO_HEADER_LEN(o->sp_params);
  124. return 1;
  125. }
  126. static void input_handler_send (SPProtoDecoder *o, uint8_t *data, int data_len)
  127. {
  128. ASSERT(data_len >= 0)
  129. ASSERT(data_len <= o->input_mtu)
  130. DebugObject_Access(&o->d_obj);
  131. // attempt to decode packet
  132. uint8_t *out;
  133. int out_len;
  134. if (!decode_packet(o, data, data_len, &out, &out_len)) {
  135. // cannot decode, finish input packet
  136. PacketPassInterface_Done(&o->input);
  137. } else {
  138. // submit decoded packet to output
  139. PacketPassInterface_Sender_Send(o->output, out, out_len);
  140. }
  141. }
  142. static void output_handler_done (SPProtoDecoder *o)
  143. {
  144. DebugObject_Access(&o->d_obj);
  145. // finish input packet
  146. PacketPassInterface_Done(&o->input);
  147. }
  148. int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg)
  149. {
  150. ASSERT(spproto_validate_security_params(sp_params))
  151. ASSERT(!SPPROTO_HAVE_OTP(sp_params) || num_otp_seeds >= 2)
  152. // init arguments
  153. o->output = output;
  154. o->sp_params = sp_params;
  155. // init output
  156. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  157. // remember output MTU
  158. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  159. // calculate hash size
  160. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  161. o->hash_size = BHash_size(o->sp_params.hash_mode);
  162. }
  163. // calculate encryption block and key sizes
  164. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  165. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  166. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  167. }
  168. // calculate input MTU
  169. o->input_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->output_mtu);
  170. // allocate plaintext buffer
  171. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  172. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
  173. if (!(o->buf = malloc(buf_size))) {
  174. goto fail0;
  175. }
  176. }
  177. // init input
  178. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  179. // init OTP checker
  180. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  181. if (!OTPChecker_Init(&o->otpchecker, o->sp_params.otp_num, o->sp_params.otp_mode, num_otp_seeds)) {
  182. goto fail1;
  183. }
  184. }
  185. // have no encryption key
  186. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  187. o->have_encryption_key = 0;
  188. }
  189. DebugObject_Init(&o->d_obj);
  190. return 1;
  191. fail1:
  192. PacketPassInterface_Free(&o->input);
  193. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  194. free(o->buf);
  195. }
  196. fail0:
  197. return 0;
  198. }
  199. void SPProtoDecoder_Free (SPProtoDecoder *o)
  200. {
  201. DebugObject_Free(&o->d_obj);
  202. // free encryptor
  203. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  204. BEncryption_Free(&o->encryptor);
  205. }
  206. // free OTP checker
  207. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  208. OTPChecker_Free(&o->otpchecker);
  209. }
  210. // free input
  211. PacketPassInterface_Free(&o->input);
  212. // free plaintext buffer
  213. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  214. free(o->buf);
  215. }
  216. }
  217. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o)
  218. {
  219. DebugObject_Access(&o->d_obj);
  220. return &o->input;
  221. }
  222. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key)
  223. {
  224. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  225. DebugObject_Access(&o->d_obj);
  226. // free encryptor
  227. if (o->have_encryption_key) {
  228. BEncryption_Free(&o->encryptor);
  229. }
  230. // init encryptor
  231. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_DECRYPT, o->sp_params.encryption_mode, encryption_key);
  232. // have encryption key
  233. o->have_encryption_key = 1;
  234. }
  235. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o)
  236. {
  237. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  238. DebugObject_Access(&o->d_obj);
  239. if (o->have_encryption_key) {
  240. // free encryptor
  241. BEncryption_Free(&o->encryptor);
  242. // have no encryption key
  243. o->have_encryption_key = 0;
  244. }
  245. }
  246. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  247. {
  248. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  249. DebugObject_Access(&o->d_obj);
  250. OTPChecker_AddSeed(&o->otpchecker, seed_id, key, iv);
  251. }
  252. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o)
  253. {
  254. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  255. DebugObject_Access(&o->d_obj);
  256. OTPChecker_RemoveSeeds(&o->otpchecker);
  257. }