SPProtoDecoder.h 4.3 KB

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