SPProtoDecoder.c 11 KB

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