SessionManager.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "interface/SessionManager.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "crypto/AddressCalc.h"
  18. #include "interface/Interface.h"
  19. #include "memory/Allocator.h"
  20. #include "util/Bits.h"
  21. #include "util/events/Time.h"
  22. #include "util/events/Timeout.h"
  23. #include "util/version/Version.h"
  24. #include "wire/Error.h"
  25. #include "wire/Headers.h"
  26. #include "wire/Message.h"
  27. #include <stdint.h>
  28. /** The number of seconds of inactivity before a session should expire. */
  29. #define SESSION_TIMEOUT_SECONDS 600
  30. /** The number of seconds between cleanup cycles. */
  31. #define CLEANUP_CYCLE_SECONDS 20
  32. /** Handle numbers 0-3 are reserved for CryptoAuth nonces. */
  33. #define FIRST_HANDLE 4
  34. struct Ip6
  35. {
  36. uint8_t bytes[16];
  37. };
  38. #define Map_NAME OfSessionsByIp6
  39. #define Map_KEY_TYPE struct Ip6
  40. #define Map_VALUE_TYPE struct SessionManager_Session
  41. #define Map_ENABLE_HANDLES
  42. #include "util/Map.h"
  43. /**
  44. * A SessionManager is a mechanism for getting a crypto session based on a given key.
  45. */
  46. struct SessionManager
  47. {
  48. Interface_CONST_CALLBACK(decryptedIncoming);
  49. Interface_CONST_CALLBACK(encryptedOutgoing);
  50. void* const interfaceContext;
  51. struct EventBase* const eventBase;
  52. struct Map_OfSessionsByIp6 ifaceMap;
  53. struct Allocator* const allocator;
  54. struct Timeout* cleanupInterval;
  55. struct CryptoAuth* cryptoAuth;
  56. };
  57. static void cleanup(void* vsm)
  58. {
  59. struct SessionManager* sm = (struct SessionManager*) vsm;
  60. uint64_t nowSecs = Time_currentTimeSeconds(sm->eventBase);
  61. for (uint32_t i = 0; i < sm->ifaceMap.count; i++) {
  62. if (sm->ifaceMap.values[i].lastMessageTime < (nowSecs - SESSION_TIMEOUT_SECONDS)) {
  63. struct Allocator* ifAllocator = sm->ifaceMap.values[i].iface.allocator;
  64. Allocator_free(ifAllocator);
  65. Map_OfSessionsByIp6_remove(i, &sm->ifaceMap);
  66. i--;
  67. }
  68. }
  69. }
  70. static void check(struct SessionManager* sm, int mapIndex)
  71. {
  72. uint8_t* herPubKey = CryptoAuth_getHerPublicKey(&sm->ifaceMap.values[mapIndex].iface);
  73. if (!Bits_isZero(herPubKey, 32)) {
  74. uint8_t ip6[16];
  75. AddressCalc_addressForPublicKey(ip6, herPubKey);
  76. Assert_always(!Bits_memcmp(&sm->ifaceMap.keys[mapIndex], ip6, 16));
  77. }
  78. }
  79. struct SessionManager_Session* SessionManager_getSession(uint8_t* lookupKey,
  80. uint8_t cryptoKey[32],
  81. struct SessionManager* sm)
  82. {
  83. int ifaceIndex = Map_OfSessionsByIp6_indexForKey((struct Ip6*)lookupKey, &sm->ifaceMap);
  84. if (ifaceIndex == -1) {
  85. // Make sure cleanup() doesn't get behind.
  86. cleanup(sm);
  87. struct Allocator* ifAllocator = Allocator_child(sm->allocator);
  88. struct Interface* outsideIf = Allocator_clone(ifAllocator, (&(struct Interface) {
  89. .sendMessage = sm->encryptedOutgoing,
  90. .senderContext = sm->interfaceContext,
  91. .allocator = ifAllocator
  92. }));
  93. struct Interface* insideIf =
  94. CryptoAuth_wrapInterface(outsideIf, cryptoKey, false, true, sm->cryptoAuth);
  95. insideIf->receiveMessage = sm->decryptedIncoming;
  96. insideIf->receiverContext = sm->interfaceContext;
  97. struct SessionManager_Session s = {
  98. .lastMessageTime = Time_currentTimeSeconds(sm->eventBase),
  99. .version = Version_DEFAULT_ASSUMPTION,
  100. // Create a trick interface which pretends to be on both sides of the crypto.
  101. .iface = {
  102. .sendMessage = insideIf->sendMessage,
  103. .senderContext = insideIf->senderContext,
  104. .receiveMessage = outsideIf->receiveMessage,
  105. .receiverContext = outsideIf->receiverContext,
  106. .allocator = ifAllocator
  107. }
  108. };
  109. int index = Map_OfSessionsByIp6_put((struct Ip6*)lookupKey, &s, &sm->ifaceMap);
  110. struct SessionManager_Session* sp = &sm->ifaceMap.values[index];
  111. sp->receiveHandle_be = Endian_hostToBigEndian32(sm->ifaceMap.handles[index] + FIRST_HANDLE);
  112. Bits_memcpyConst(sp->ip6, lookupKey, 16);
  113. return sp;
  114. } else {
  115. // Interface already exists, set the time of last message to "now".
  116. sm->ifaceMap.values[ifaceIndex].lastMessageTime = Time_currentTimeSeconds(sm->eventBase);
  117. uint8_t* herPubKey = CryptoAuth_getHerPublicKey(&sm->ifaceMap.values[ifaceIndex].iface);
  118. if (Bits_isZero(herPubKey, 32) && cryptoKey) {
  119. Bits_memcpyConst(herPubKey, cryptoKey, 32);
  120. }
  121. }
  122. check(sm, ifaceIndex);
  123. return &sm->ifaceMap.values[ifaceIndex];
  124. }
  125. struct SessionManager_Session* SessionManager_sessionForHandle(uint32_t handle,
  126. struct SessionManager* sm)
  127. {
  128. int index = Map_OfSessionsByIp6_indexForHandle(handle - FIRST_HANDLE, &sm->ifaceMap);
  129. if (index < 0) {
  130. return NULL;
  131. }
  132. check(sm, index);
  133. return &sm->ifaceMap.values[index];
  134. }
  135. struct SessionManager_HandleList* SessionManager_getHandleList(struct SessionManager* sm,
  136. struct Allocator* alloc)
  137. {
  138. struct SessionManager_HandleList* out =
  139. Allocator_malloc(alloc, sizeof(struct SessionManager_HandleList));
  140. uint32_t* buff = Allocator_malloc(alloc, 4 * sm->ifaceMap.count);
  141. Bits_memcpy(buff, sm->ifaceMap.handles, 4 * sm->ifaceMap.count);
  142. out->handles = buff;
  143. out->count = sm->ifaceMap.count;
  144. for (int i = 0; i < (int)out->count; i++) {
  145. buff[i] += FIRST_HANDLE;
  146. }
  147. return out;
  148. }
  149. struct SessionManager* SessionManager_new(Interface_CALLBACK(decryptedIncoming),
  150. Interface_CALLBACK(encryptedOutgoing),
  151. void* interfaceContext,
  152. struct EventBase* eventBase,
  153. struct CryptoAuth* cryptoAuth,
  154. struct Allocator* allocator)
  155. {
  156. struct SessionManager* sm = Allocator_malloc(allocator, sizeof(struct SessionManager));
  157. Bits_memcpyConst(sm, (&(struct SessionManager) {
  158. .decryptedIncoming = decryptedIncoming,
  159. .encryptedOutgoing = encryptedOutgoing,
  160. .interfaceContext = interfaceContext,
  161. .eventBase = eventBase,
  162. .ifaceMap = {
  163. .allocator = allocator
  164. },
  165. .cryptoAuth = cryptoAuth,
  166. .allocator = allocator,
  167. .cleanupInterval =
  168. Timeout_setInterval(cleanup, sm, 1000 * CLEANUP_CYCLE_SECONDS, eventBase, allocator)
  169. }), sizeof(struct SessionManager));
  170. return sm;
  171. }