SPProtoDecoder.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file SPProtoDecoder.h
  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. * @section DESCRIPTION
  23. *
  24. * Object which decodes packets according to SPProto.
  25. */
  26. #ifndef BADVPN_CLIENT_SPPROTODECODER_H
  27. #define BADVPN_CLIENT_SPPROTODECODER_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <base/DebugObject.h>
  31. #include <base/BLog.h>
  32. #include <protocol/spproto.h>
  33. #include <security/BEncryption.h>
  34. #include <security/OTPChecker.h>
  35. #include <flow/PacketPassInterface.h>
  36. /**
  37. * Handler called when OTP generation for a new seed is finished.
  38. *
  39. * @param user as in {@link SPProtoDecoder_Init}
  40. */
  41. typedef void (*SPProtoDecoder_otp_handler) (void *user);
  42. /**
  43. * Object which decodes packets according to SPProto.
  44. * Input is with {@link PacketPassInterface}.
  45. * Output is with {@link PacketPassInterface}.
  46. */
  47. typedef struct {
  48. PacketPassInterface *output;
  49. struct spproto_security_params sp_params;
  50. BThreadWorkDispatcher *twd;
  51. void *user;
  52. BLog_logfunc logfunc;
  53. int output_mtu;
  54. int hash_size;
  55. int enc_block_size;
  56. int enc_key_size;
  57. int input_mtu;
  58. uint8_t *buf;
  59. PacketPassInterface input;
  60. OTPChecker otpchecker;
  61. int have_encryption_key;
  62. BEncryption encryptor;
  63. uint8_t *in;
  64. int in_len;
  65. int tw_have;
  66. BThreadWork tw;
  67. uint16_t tw_out_seed_id;
  68. otp_t tw_out_otp;
  69. uint8_t *tw_out;
  70. int tw_out_len;
  71. DebugObject d_obj;
  72. } SPProtoDecoder;
  73. /**
  74. * Initializes the object.
  75. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  76. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  77. *
  78. * @param o the object
  79. * @param output output interface. Its MTU must not be too large, i.e. this must hold:
  80. * spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU) >= 0
  81. * @param sp_params SPProto parameters
  82. * @param encryption_key if using encryption, the encryption key
  83. * @param num_otp_seeds if using OTPs, how many OTP seeds to keep for checking
  84. * receiving packets. Must be >=2 if using OTPs.
  85. * @param pg pending group
  86. * @param twd thread work dispatcher
  87. * @param user argument to handlers
  88. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  89. * @return 1 on success, 0 on failure
  90. */
  91. int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user, BLog_logfunc logfunc) WARN_UNUSED;
  92. /**
  93. * Frees the object.
  94. *
  95. * @param o the object
  96. */
  97. void SPProtoDecoder_Free (SPProtoDecoder *o);
  98. /**
  99. * Returns the input interface.
  100. * The MTU of the input interface will depend on the output MTU and security parameters,
  101. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU).
  102. *
  103. * @param o the object
  104. * @return input interface
  105. */
  106. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o);
  107. /**
  108. * Sets an encryption key for decrypting packets.
  109. * Encryption must be enabled.
  110. *
  111. * @param o the object
  112. * @param encryption_key key to use
  113. */
  114. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key);
  115. /**
  116. * Removes an encryption key if one is configured.
  117. * Encryption must be enabled.
  118. *
  119. * @param o the object
  120. */
  121. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o);
  122. /**
  123. * Starts generating OTPs for a seed to check received packets against.
  124. * OTPs for this seed will not be recognized until the {@link SPProtoDecoder_otp_handler} handler
  125. * is called.
  126. * If OTPs are still being generated for the previous seed, it will be forgotten.
  127. * OTPs must be enabled.
  128. *
  129. * @param o the object
  130. * @param seed_id seed identifier
  131. * @param key OTP encryption key
  132. * @param iv OTP initialization vector
  133. */
  134. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  135. /**
  136. * Removes all OTP seeds for checking received packets against.
  137. * OTPs must be enabled.
  138. *
  139. * @param o the object
  140. */
  141. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o);
  142. /**
  143. * Sets handlers.
  144. *
  145. * @param o the object
  146. * @param otp_handler handler called when OTP generation is finished
  147. * @param user argument to handler
  148. */
  149. void SPProtoDecoder_SetHandlers (SPProtoDecoder *o, SPProtoDecoder_otp_handler otp_handler, void *user);
  150. #endif