2
0

zrtp_iface_cache.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. #ifndef __ZRTP_IFACE_CACHE_H__
  10. #define __ZRTP_IFACE_CACHE_H__
  11. #include "zrtp_config.h"
  12. #include "zrtp_base.h"
  13. #include "zrtp_string.h"
  14. #include "zrtp_error.h"
  15. #include "zrtp_iface.h"
  16. #if defined(__cplusplus)
  17. extern "C"
  18. {
  19. #endif
  20. #if defined(ZRTP_USE_BUILTIN_CACHE) && (ZRTP_USE_BUILTIN_CACHE == 1)
  21. #define ZRTP_DEF_CACHE_VERSION_STR "libZRTP cache version="
  22. #define ZRTP_DEF_CACHE_VERSION_VAL "1.0"
  23. /**
  24. * @brief Cache element identifier type
  25. * Elements of this type link cache data with a pair of ZIDs.
  26. * (constructed as: [ZID1][ZID2], where ZID1 - ZID with greater binary value)
  27. * This type is used to identify cache elements in the built-in implementation.
  28. */
  29. typedef uint8_t zrtp_cache_id_t[24];
  30. #define ZRTP_MITMCACHE_ELEM_LENGTH ( sizeof(zrtp_cache_id_t) + sizeof(zrtp_string64_t) )
  31. #define ZRTP_CACHE_ELEM_LENGTH ( sizeof(zrtp_cache_elem_t) - sizeof(mlist_t) - (sizeof(uint32_t)*2) )
  32. #define ZFONE_CACHE_NAME_LENGTH 256
  33. /**
  34. * @brief Secret cache element structure
  35. * This structure is used to store cache data in the built-in implementation
  36. * of the caching system.
  37. */
  38. typedef struct zrtp_cache_elem
  39. {
  40. zrtp_cache_id_t id; /** Cache element identifier */
  41. zrtp_string64_t curr_cache; /** Current cache value */
  42. zrtp_string64_t prev_cache; /** Prev cache value */
  43. uint32_t verified; /** Verified flag for the cache value */
  44. uint32_t lastused_at; /** Last usage time-stamp in seconds */
  45. uint32_t ttl; /** Cache TTL since lastused_at in seconds */
  46. uint32_t secure_since; /** Secure since date in seconds. Utility field. Don't required by libzrtp. */
  47. char name[ZFONE_CACHE_NAME_LENGTH]; /** name of the user associated with this cache entry */
  48. uint32_t name_length; /** cache name lengths */
  49. uint32_t presh_counter; /** number of Preshared streams made since last DH exchange */
  50. uint32_t _index; /** cache element index in the cache file */
  51. uint32_t _is_dirty; /** dirty flag means the entry has unsaved changes */
  52. mlist_t _mlist;
  53. } zrtp_cache_elem_t;
  54. #endif /* ZRTP_USE_BUILTIN_CACHE */
  55. zrtp_status_t zrtp_def_cache_init(zrtp_global_t* zrtp);
  56. void zrtp_def_cache_down();
  57. zrtp_status_t zrtp_def_cache_set_verified( const zrtp_stringn_t* one_zid,
  58. const zrtp_stringn_t* another_zid,
  59. uint32_t verified);
  60. zrtp_status_t zrtp_def_cache_get_verified( const zrtp_stringn_t* one_zid,
  61. const zrtp_stringn_t* another_zid,
  62. uint32_t* verified);
  63. zrtp_status_t zrtp_def_cache_put( const zrtp_stringn_t* one_zid,
  64. const zrtp_stringn_t* another_zid,
  65. zrtp_shared_secret_t *rss);
  66. zrtp_status_t zrtp_def_cache_put_mitm( const zrtp_stringn_t* one_zid,
  67. const zrtp_stringn_t* another_zid,
  68. zrtp_shared_secret_t *rss);
  69. zrtp_status_t zrtp_def_cache_get( const zrtp_stringn_t* one_zid,
  70. const zrtp_stringn_t* another_zid,
  71. zrtp_shared_secret_t *rss,
  72. int prev_requested);
  73. zrtp_status_t zrtp_def_cache_get_mitm( const zrtp_stringn_t* one_zid,
  74. const zrtp_stringn_t* another_zid,
  75. zrtp_shared_secret_t *rss);
  76. zrtp_status_t zrtp_def_cache_set_presh_counter( const zrtp_stringn_t* one_zid,
  77. const zrtp_stringn_t* another_zid,
  78. uint32_t counter);
  79. zrtp_status_t zrtp_def_cache_get_presh_counter( const zrtp_stringn_t* one_zid,
  80. const zrtp_stringn_t* another_zid,
  81. uint32_t* counter);
  82. #if defined(ZRTP_USE_BUILTIN_CACHE) && (ZRTP_USE_BUILTIN_CACHE == 1)
  83. /**
  84. * @brief Cache iterator
  85. * zrtp_def_cache_foreach() calls this function for every cache entry.
  86. * @param elem - cache element;
  87. * @param is_mitm - is 1 when callback was called for MiTM for each.
  88. * @param del - callback may return 1 to this to remove cache entry from the list.
  89. * @param data - pointer to some user data from zrtp_def_cache_foreach();
  90. * @return
  91. * - 0 - if element was requested for reading only and wasn't changed;
  92. * - 1 - if element was modified and cache should be updated.
  93. */
  94. typedef int (*zrtp_cache_callback_t)(zrtp_cache_elem_t* elem, int is_mitm, void* data, int* del);
  95. /**
  96. * @brief Iterate over all cache entries.
  97. * Can be used for searching and modifying cache entries. Protected by mutex.
  98. * Can be called in parallel with other cache operations when protocol is
  99. * running.
  100. * @param global - libzrtp global context;
  101. * @param is_mitm - if value of this flag is 1 - fore_each will be applied for MiTM secrets;
  102. * @param callback - function to be called for every cache entry;
  103. * @param data - this pointer will be passed to every \c callback call.
  104. */
  105. void zrtp_def_cache_foreach( zrtp_global_t *global,
  106. int is_mitm,
  107. zrtp_cache_callback_t callback,
  108. void *data);
  109. #endif /* ZRTP_USE_BUILTIN_CACHE */
  110. /**
  111. * @brief Store shared secrets cache to the persistent storage
  112. * May be used in server solutions for periodically flushing the cache to prevent data loss.
  113. *
  114. * @return
  115. * - zrtp_status_ok - if operation completed successfully;
  116. * - zrtp_status_wrong_state - if a call is performed from a routine which
  117. * doesn't use the default cache.
  118. */
  119. zrtp_status_t zrtp_def_cache_store(zrtp_global_t *global);
  120. zrtp_status_t zrtp_def_cache_reset_since( const zrtp_stringn_t* one_zid,
  121. const zrtp_stringn_t* another_zid);
  122. zrtp_status_t zrtp_def_cache_get_since( const zrtp_stringn_t* one_zid,
  123. const zrtp_stringn_t* another_zid,
  124. uint32_t* since);
  125. zrtp_status_t zrtp_def_cache_get_name( const zrtp_stringn_t* one_zid,
  126. const zrtp_stringn_t* another_zid,
  127. zrtp_stringn_t* name);
  128. zrtp_status_t zrtp_def_cache_put_name( const zrtp_stringn_t* one_zid,
  129. const zrtp_stringn_t* another_zid,
  130. const zrtp_stringn_t* name);
  131. #if defined(ZRTP_USE_BUILTIN_CACHE) && (ZRTP_USE_BUILTIN_CACHE == 1)
  132. zrtp_cache_elem_t* zrtp_def_cache_get2(const zrtp_cache_id_t id, int is_mitm);
  133. #endif /* ZRTP_USE_BUILTIN_CACHE */
  134. #if defined(__cplusplus)
  135. }
  136. #endif
  137. #endif /*__ZRTP_IFACE_CACHE_H__*/