SPProtoDecoder.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file SPProtoDecoder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object which decodes packets according to SPProto.
  32. */
  33. #ifndef BADVPN_CLIENT_SPPROTODECODER_H
  34. #define BADVPN_CLIENT_SPPROTODECODER_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. #include <base/DebugObject.h>
  38. #include <base/BLog.h>
  39. #include <protocol/spproto.h>
  40. #include <security/BEncryption.h>
  41. #include <security/OTPChecker.h>
  42. #include <flow/PacketPassInterface.h>
  43. /**
  44. * Handler called when OTP generation for a new seed is finished.
  45. *
  46. * @param user as in {@link SPProtoDecoder_Init}
  47. */
  48. typedef void (*SPProtoDecoder_otp_handler) (void *user);
  49. /**
  50. * Object which decodes packets according to SPProto.
  51. * Input is with {@link PacketPassInterface}.
  52. * Output is with {@link PacketPassInterface}.
  53. */
  54. typedef struct {
  55. PacketPassInterface *output;
  56. struct spproto_security_params sp_params;
  57. BThreadWorkDispatcher *twd;
  58. void *user;
  59. BLog_logfunc logfunc;
  60. int output_mtu;
  61. int hash_size;
  62. int enc_block_size;
  63. int enc_key_size;
  64. int input_mtu;
  65. uint8_t *buf;
  66. PacketPassInterface input;
  67. OTPChecker otpchecker;
  68. int have_encryption_key;
  69. BEncryption encryptor;
  70. uint8_t *in;
  71. int in_len;
  72. int tw_have;
  73. BThreadWork tw;
  74. uint16_t tw_out_seed_id;
  75. otp_t tw_out_otp;
  76. uint8_t *tw_out;
  77. int tw_out_len;
  78. DebugObject d_obj;
  79. } SPProtoDecoder;
  80. /**
  81. * Initializes the object.
  82. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  83. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  84. *
  85. * @param o the object
  86. * @param output output interface. Its MTU must not be too large, i.e. this must hold:
  87. * spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU) >= 0
  88. * @param sp_params SPProto parameters
  89. * @param encryption_key if using encryption, the encryption key
  90. * @param num_otp_seeds if using OTPs, how many OTP seeds to keep for checking
  91. * receiving packets. Must be >=2 if using OTPs.
  92. * @param pg pending group
  93. * @param twd thread work dispatcher
  94. * @param user argument to handlers
  95. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  96. * @return 1 on success, 0 on failure
  97. */
  98. 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;
  99. /**
  100. * Frees the object.
  101. *
  102. * @param o the object
  103. */
  104. void SPProtoDecoder_Free (SPProtoDecoder *o);
  105. /**
  106. * Returns the input interface.
  107. * The MTU of the input interface will depend on the output MTU and security parameters,
  108. * that is spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU).
  109. *
  110. * @param o the object
  111. * @return input interface
  112. */
  113. PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o);
  114. /**
  115. * Sets an encryption key for decrypting packets.
  116. * Encryption must be enabled.
  117. *
  118. * @param o the object
  119. * @param encryption_key key to use
  120. */
  121. void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key);
  122. /**
  123. * Removes an encryption key if one is configured.
  124. * Encryption must be enabled.
  125. *
  126. * @param o the object
  127. */
  128. void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o);
  129. /**
  130. * Starts generating OTPs for a seed to check received packets against.
  131. * OTPs for this seed will not be recognized until the {@link SPProtoDecoder_otp_handler} handler
  132. * is called.
  133. * If OTPs are still being generated for the previous seed, it will be forgotten.
  134. * OTPs must be enabled.
  135. *
  136. * @param o the object
  137. * @param seed_id seed identifier
  138. * @param key OTP encryption key
  139. * @param iv OTP initialization vector
  140. */
  141. void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  142. /**
  143. * Removes all OTP seeds for checking received packets against.
  144. * OTPs must be enabled.
  145. *
  146. * @param o the object
  147. */
  148. void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o);
  149. /**
  150. * Sets handlers.
  151. *
  152. * @param o the object
  153. * @param otp_handler handler called when OTP generation is finished
  154. * @param user argument to handler
  155. */
  156. void SPProtoDecoder_SetHandlers (SPProtoDecoder *o, SPProtoDecoder_otp_handler otp_handler, void *user);
  157. #endif