SPProtoDecoder.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // input must be a multiple of blocks size
  40. if (in_len % o->enc_block_size != 0) {
  41. DEBUG("packet size not a multiple of block size");
  42. return 0;
  43. }
  44. // input must have an IV block
  45. if (in_len < o->enc_block_size) {
  46. DEBUG("packet does not have an IV");
  47. return 0;
  48. }
  49. // check if we have encryption key
  50. if (!o->have_encryption_key) {
  51. DEBUG("have no encryption key");
  52. return 0;
  53. }
  54. // copy IV as BEncryption_Decrypt changes the IV
  55. uint8_t iv[o->enc_block_size];
  56. memcpy(iv, in, o->enc_block_size);
  57. // decrypt
  58. uint8_t *ciphertext = in + o->enc_block_size;
  59. int ciphertext_len = in_len - o->enc_block_size;
  60. plaintext = o->buf;
  61. BEncryption_Decrypt(&o->encryptor, ciphertext, plaintext, ciphertext_len, iv);
  62. // read padding
  63. if (ciphertext_len < o->enc_block_size) {
  64. DEBUG("packet does not have a padding block");
  65. return 0;
  66. }
  67. int i;
  68. for (i = ciphertext_len - 1; i >= ciphertext_len - o->enc_block_size; i--) {
  69. if (plaintext[i] == 1) {
  70. break;
  71. }
  72. if (plaintext[i] != 0) {
  73. DEBUG("packet padding wrong (nonzero byte)");
  74. return 0;
  75. }
  76. }
  77. if (i < ciphertext_len - o->enc_block_size) {
  78. DEBUG("packet padding wrong (all zeroes)");
  79. return 0;
  80. }
  81. plaintext_len = i;
  82. }
  83. // check for header
  84. if (plaintext_len < SPPROTO_HEADER_LEN(o->sp_params)) {
  85. DEBUG("packet has no header");
  86. return 0;
  87. }
  88. uint8_t *header = plaintext;
  89. // check data length
  90. if (plaintext_len - SPPROTO_HEADER_LEN(o->sp_params) > o->output_mtu) {
  91. DEBUG("packet too long");
  92. return 0;
  93. }
  94. // check OTP
  95. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  96. struct spproto_otpdata *header_otpd = (struct spproto_otpdata *)(header + SPPROTO_HEADER_OTPDATA_OFF(o->sp_params));
  97. uint16_t seed_id = ltoh16(header_otpd->seed_id);
  98. if (!OTPChecker_CheckOTP(&o->otpchecker, seed_id, header_otpd->otp)) {
  99. DEBUG("packet has wrong OTP");
  100. return 0;
  101. }
  102. }
  103. // check hash
  104. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  105. uint8_t *header_hash = header + SPPROTO_HEADER_HASH_OFF(o->sp_params);
  106. // read hash
  107. uint8_t hash[o->hash_size];
  108. memcpy(hash, header_hash, o->hash_size);
  109. // zero hash in packet
  110. memset(header_hash, 0, o->hash_size);
  111. // calculate hash
  112. uint8_t hash_calc[o->hash_size];
  113. BHash_calculate(o->sp_params.hash_mode, plaintext, plaintext_len, hash_calc);
  114. // set hash field to its original value
  115. memcpy(header_hash, hash, o->hash_size);
  116. // compare hashes
  117. if (memcmp(hash, hash_calc, o->hash_size)) {
  118. DEBUG("packet has wrong hash");
  119. return 0;
  120. }
  121. }
  122. // return packet
  123. *out = plaintext + SPPROTO_HEADER_LEN(o->sp_params);
  124. *out_len = plaintext_len - SPPROTO_HEADER_LEN(o->sp_params);
  125. return 1;
  126. }
  127. static void input_handler_send (SPProtoDecoder *o, uint8_t *data, int data_len)
  128. {
  129. ASSERT(data_len >= 0)
  130. ASSERT(data_len <= o->input_mtu)
  131. DebugObject_Access(&o->d_obj);
  132. // attempt to decode packet
  133. uint8_t *out;
  134. int out_len;
  135. if (!decode_packet(o, data, data_len, &out, &out_len)) {
  136. // cannot decode, finish input packet
  137. PacketPassInterface_Done(&o->input);
  138. } else {
  139. // submit decoded packet to output
  140. PacketPassInterface_Sender_Send(o->output, out, out_len);
  141. }
  142. }
  143. static void output_handler_done (SPProtoDecoder *o)
  144. {
  145. DebugObject_Access(&o->d_obj);
  146. // finish input packet
  147. PacketPassInterface_Done(&o->input);
  148. }
  149. int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg)
  150. {
  151. spproto_assert_security_params(sp_params);
  152. ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketPassInterface_GetMTU(output)) >= 0)
  153. ASSERT(!SPPROTO_HAVE_OTP(sp_params) || num_otp_seeds >= 2)
  154. // init arguments
  155. o->output = output;
  156. o->sp_params = sp_params;
  157. // init output
  158. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  159. // remember output MTU
  160. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  161. // calculate hash size
  162. if (SPPROTO_HAVE_HASH(o->sp_params)) {
  163. o->hash_size = BHash_size(o->sp_params.hash_mode);
  164. }
  165. // calculate encryption block and key sizes
  166. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  167. o->enc_block_size = BEncryption_cipher_block_size(o->sp_params.encryption_mode);
  168. o->enc_key_size = BEncryption_cipher_key_size(o->sp_params.encryption_mode);
  169. }
  170. // calculate input MTU
  171. o->input_mtu = spproto_carrier_mtu_for_payload_mtu(o->sp_params, o->output_mtu);
  172. // allocate plaintext buffer
  173. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  174. int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
  175. if (!(o->buf = malloc(buf_size))) {
  176. goto fail0;
  177. }
  178. }
  179. // init input
  180. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  181. // init OTP checker
  182. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  183. if (!OTPChecker_Init(&o->otpchecker, o->sp_params.otp_num, o->sp_params.otp_mode, num_otp_seeds)) {
  184. goto fail1;
  185. }
  186. }
  187. // have no encryption key
  188. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  189. o->have_encryption_key = 0;
  190. }
  191. DebugObject_Init(&o->d_obj);
  192. return 1;
  193. fail1:
  194. PacketPassInterface_Free(&o->input);
  195. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  196. free(o->buf);
  197. }
  198. fail0:
  199. return 0;
  200. }
  201. void SPProtoDecoder_Free (SPProtoDecoder *o)
  202. {
  203. DebugObject_Free(&o->d_obj);
  204. // free encryptor
  205. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params) && o->have_encryption_key) {
  206. BEncryption_Free(&o->encryptor);
  207. }
  208. // free OTP checker
  209. if (SPPROTO_HAVE_OTP(o->sp_params)) {
  210. OTPChecker_Free(&o->otpchecker);
  211. }
  212. // free input
  213. PacketPassInterface_Free(&o->input);
  214. // free plaintext buffer
  215. if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
  216. free(o->buf);
  217. }
  218. }
  219. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o)
  220. {
  221. DebugObject_Access(&o->d_obj);
  222. return &o->input;
  223. }
  224. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key)
  225. {
  226. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  227. DebugObject_Access(&o->d_obj);
  228. // free encryptor
  229. if (o->have_encryption_key) {
  230. BEncryption_Free(&o->encryptor);
  231. }
  232. // init encryptor
  233. BEncryption_Init(&o->encryptor, BENCRYPTION_MODE_DECRYPT, o->sp_params.encryption_mode, encryption_key);
  234. // have encryption key
  235. o->have_encryption_key = 1;
  236. }
  237. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o)
  238. {
  239. ASSERT(SPPROTO_HAVE_ENCRYPTION(o->sp_params))
  240. DebugObject_Access(&o->d_obj);
  241. if (o->have_encryption_key) {
  242. // free encryptor
  243. BEncryption_Free(&o->encryptor);
  244. // have no encryption key
  245. o->have_encryption_key = 0;
  246. }
  247. }
  248. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv)
  249. {
  250. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  251. DebugObject_Access(&o->d_obj);
  252. OTPChecker_AddSeed(&o->otpchecker, seed_id, key, iv);
  253. }
  254. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o)
  255. {
  256. ASSERT(SPPROTO_HAVE_OTP(o->sp_params))
  257. DebugObject_Access(&o->d_obj);
  258. OTPChecker_RemoveSeeds(&o->otpchecker);
  259. }