SPProtoDecoder.c 11 KB

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