CryptoAuth_pvt.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_pvt_H
  16. #define CryptoAuth_pvt_H
  17. #include "crypto/CryptoAuth.h"
  18. #include "crypto/ReplayProtector.h"
  19. #include "interface/Interface.h"
  20. #include "benc/Object.h"
  21. #include "util/log/Log.h"
  22. #include "memory/Allocator.h"
  23. #include "util/events/EventBase.h"
  24. #include "wire/Headers.h"
  25. #include "wire/Message.h"
  26. #include "util/Identity.h"
  27. #include <stdint.h>
  28. struct CryptoAuth_Auth {
  29. union Headers_AuthChallenge challenge;
  30. uint8_t secret[32];
  31. String* user;
  32. };
  33. struct CryptoAuth_pvt
  34. {
  35. struct CryptoAuth pub;
  36. uint8_t privateKey[32];
  37. struct CryptoAuth_Auth* passwords;
  38. uint32_t passwordCount;
  39. uint32_t passwordCapacity;
  40. struct Log* logger;
  41. struct EventBase* eventBase;
  42. struct Allocator* allocator;
  43. struct Random* rand;
  44. Identity
  45. };
  46. /**
  47. * What the "secret" and "tempKey" fields hold during different stages of the handshake.
  48. * | secret | tempKey | message | secret | tempKey | encryptedWith
  49. * |+tmpPvtA |+tmpPubA | ---- hello ---->| 0 |+tmpPubA | prmPvtA-prmPubB-passA
  50. * | tmpPvtA | tmpPubA | --dupe hello -->| 0 | tmpPubA | prmPvtA-prmPubB-passA
  51. * | tmpPvtA | tmpPubA | <---- key ----- | +tmpPvtB | tmpPubA | prmPvtB-tmpPubA-passB
  52. * | tmpPvtA | tmpPubA | <--dupe key---- | tmpPvtB | tmpPubA | prmPvtB-tmpPubA-passB
  53. * | finalSec | 0 | ---- data ----->|+finalSec | 0 | tmpPvtA-tmpPubB
  54. */
  55. struct CryptoAuth_Wrapper
  56. {
  57. /** The public key of the other node. */
  58. uint8_t herPerminentPubKey[32];
  59. /**
  60. * If an object was associated with a password and the remote host authed
  61. * with the password this will be the object, otherwise it will be null.
  62. */
  63. String* user;
  64. /** The shared secret. */
  65. uint8_t secret[32];
  66. /** Used during handshake to hold her public key and my private key at different times. */
  67. uint8_t tempKey[32];
  68. /** An outgoing message which is buffered in the event that a reverse handshake is required. */
  69. struct Message* bufferedMessage;
  70. /** A password to use for authing with the other party. */
  71. String* password;
  72. /** Used for preventing replay attacks. */
  73. struct ReplayProtector replayProtector;
  74. /** The next nonce to use. */
  75. uint32_t nextNonce;
  76. /** Used to reset the connection if it's in a bad state (no traffic coming in). */
  77. uint32_t timeOfLastPacket;
  78. /** The method to use for trying to auth with the server. */
  79. uint8_t authType;
  80. /** True if this node began the conversation. */
  81. bool isInitiator : 1;
  82. /** If true then the packets sent through this interface must be authenticated. */
  83. bool authenticatePackets : 1;
  84. /** If true and the other end is connecting, do not respond until a valid password is sent. */
  85. bool requireAuth : 1;
  86. /** A pointer back to the main cryptoauth context. */
  87. struct CryptoAuth_pvt* const context;
  88. /** The internal interface which we are wrapping. */
  89. struct Interface* const wrappedInterface;
  90. /** The interface which this wrapper provides. */
  91. struct Interface externalInterface;
  92. Identity
  93. };
  94. #endif