CryptoAuth.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef CryptoAuth_H
  16. #define CryptoAuth_H
  17. #include "benc/Object.h"
  18. #include "crypto/random/Random.h"
  19. #include "interface/Interface.h"
  20. #include "memory/Allocator.h"
  21. #include "util/Endian.h"
  22. #include "util/log/Log.h"
  23. #include "util/events/EventBase.h"
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. #define CryptoAuth_DEFAULT_RESET_AFTER_INACTIVITY_SECONDS 60
  27. struct CryptoAuth
  28. {
  29. uint8_t publicKey[32];
  30. /**
  31. * After this number of seconds of inactivity,
  32. * a connection will be reset to prevent them hanging in a bad state.
  33. */
  34. uint32_t resetAfterInactivitySeconds;
  35. };
  36. /** The internal interface wrapper struct. */
  37. struct CryptoAuth_Wrapper;
  38. /**
  39. * Associate a password:authtype pair with a user object.
  40. * Calling CryptoAuth_getUser() on any interface which has established a connection with
  41. * the same password:authType pair will return the same user object.
  42. *
  43. * @param password This should be a key derived from the password using a good key derivation
  44. * function, using a plaintext password here is NOT recommended.
  45. * @param authType The method of authenticating the user, only option currently is 1 for sha256
  46. * based authentication.
  47. * @param user The thing to associate with this user, will be returned by CryptoAuth_getUser().
  48. * If this is NULL and requireAuthentication is enabled, authentication will fail.
  49. * Duplicate user entires are OK.
  50. * @param context The CryptoAuth context.
  51. * @return 0 if all goes well,
  52. * CryptoAuth_addUser_INVALID_AUTHTYPE if the authentication method is not supported,
  53. * CryptoAuth_addUser_OUT_OF_SPACE if there is not enough space to store the entry,
  54. * CryptoAuth_addUser_DUPLICATE if the same *password* already exists.
  55. */
  56. #define CryptoAuth_addUser_INVALID_AUTHTYPE -1
  57. #define CryptoAuth_addUser_OUT_OF_SPACE -2
  58. #define CryptoAuth_addUser_DUPLICATE -3
  59. int32_t CryptoAuth_addUser(String* password,
  60. uint8_t authType,
  61. String* user,
  62. struct CryptoAuth* context);
  63. /**
  64. * Remove all users registered with this CryptoAuth.
  65. *
  66. * @param context the context to remove users for.
  67. * @param user the identifier which was passed to addUser(), all users with this id will be removed.
  68. * @return the number of users removed.
  69. */
  70. int CryptoAuth_removeUsers(struct CryptoAuth* context, String* user);
  71. /**
  72. * Get a list of all the users added via addUser.
  73. *
  74. * @param context the context used to call addUser.
  75. * @param alloc the Allocator to use to create the usersOut array.
  76. * @returns List* containing the user String's
  77. */
  78. List* CryptoAuth_getUsers(struct CryptoAuth* context, struct Allocator* alloc);
  79. /**
  80. * Get the user object associated with the authenticated session or NULL if there is none.
  81. * Please make sure to only call this on interfaces which were actually returned by
  82. * CryptoAuth_wrapInterface() as strange and interesting bugs will result otherwise.
  83. *
  84. * @param interface an interface as returned by CryptoAuth_wrapInterface().
  85. * @return the user object added by calling CryptoAuth_addUser() or NULL if this session is not
  86. * authenticated.
  87. */
  88. String* CryptoAuth_getUser(struct Interface* iface);
  89. /**
  90. * Create a new crypto authenticator.
  91. *
  92. * @param allocator the means of aquiring memory.
  93. * @param privateKey the private key to use for this CryptoAuth or null if one should be generated.
  94. * @param eventBase the libevent context for handling timeouts.
  95. * @param logger the mechanism for logging output from the CryptoAuth.
  96. * if NULL then no logging will be done.
  97. * @param rand random number generator.
  98. * @return a new CryptoAuth context.
  99. */
  100. struct CryptoAuth* CryptoAuth_new(struct Allocator* allocator,
  101. const uint8_t* privateKey,
  102. struct EventBase* eventBase,
  103. struct Log* logger,
  104. struct Random* rand);
  105. /**
  106. * Wrap an interface with crypto authentication.
  107. *
  108. * NOTE: Sending empty packets during the handshake is not allowed!
  109. * Empty packets are used for signaling during the handshake so they can
  110. * only be used while the session is in state ESTABLISHED.
  111. *
  112. * @param toWarp the interface to wrap
  113. * @param herPublicKey the public key of the other party or NULL if unknown.
  114. * @param requireAuth if the remote end of this interface begins the connection, require
  115. * them to present valid authentication credentials to connect.
  116. * If this end begins the connection, this parameter has no effect.
  117. * @param authenticatePackets if true, all packets will be protected against forgery and replay
  118. * attacks, this is a seperate system from password and authType.
  119. * @param context the CryptoAuth context.
  120. */
  121. struct Interface* CryptoAuth_wrapInterface(struct Interface* toWrap,
  122. const uint8_t herPublicKey[32],
  123. const bool requireAuth,
  124. bool authenticatePackets,
  125. struct CryptoAuth* context);
  126. /**
  127. * Choose the authentication credentials to use.
  128. * WARNING: Even if the remote end begins the connection, these credentials will be presented which
  129. * will cause the connection initiation to fail if the remote end does not know of them.
  130. *
  131. * @param password the password to use for authenticating, this must match the password given to
  132. * CryptoAuth_addUser() at the other end of the connection.
  133. * @param authType this must match CryptoAuth_addUser() at the other end of the connection.
  134. * @param wrappedInterface this MUST be the output from CryptoAuth_wrapInterface().
  135. */
  136. void CryptoAuth_setAuth(const String* password,
  137. const uint8_t authType,
  138. struct Interface* wrappedInterface);
  139. /** @return a pointer to the other party's public key. */
  140. uint8_t* CryptoAuth_getHerPublicKey(struct Interface* iface);
  141. /** Reset the session's state to CryptoAuth_NEW, a new connection will be negotiated. */
  142. void CryptoAuth_reset(struct Interface* iface);
  143. /** New CryptoAuth session, has not sent or received anything. */
  144. #define CryptoAuth_NEW 0
  145. /** Sent a hello message, waiting for reply. */
  146. #define CryptoAuth_HANDSHAKE1 1
  147. /** Received a hello message, sent a key message, waiting for the session to complete. */
  148. #define CryptoAuth_HANDSHAKE2 2
  149. /** Sent a hello message and received a key message but have not gotten a data message yet. */
  150. #define CryptoAuth_HANDSHAKE3 3
  151. /** The CryptoAuth session has successfully done a handshake and received at least one message. */
  152. #define CryptoAuth_ESTABLISHED 4
  153. /**
  154. * Get the state of the CryptoAuth session.
  155. *
  156. * @param interface a CryptoAuth wrapper.
  157. * @return one of CryptoAuth_NEW,
  158. * CryptoAuth_HANDSHAKE1,
  159. * CryptoAuth_HANDSHAKE2 or
  160. * CryptoAuth_ESTABLISHED
  161. */
  162. int CryptoAuth_getState(struct Interface* iface);
  163. /**
  164. * Get the interface on the other side of this CryptoAuth session.
  165. *
  166. * Given a wrapped interface, get the wrapping interface.
  167. * given a wrapping interface, get the one which is wrapped.
  168. *
  169. * @param iface the wrapped or wrapper iface.
  170. * @return the opposite.
  171. */
  172. struct Interface* CryptoAuth_getConnectedInterface(struct Interface* iface);
  173. /**
  174. * Get the structure which is used to protect against packet replay attacks.
  175. */
  176. struct ReplayProtector* CryptoAuth_getReplayProtector(struct Interface* iface);
  177. #endif