SPProtoDecoder.c 11 KB

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