hs_common.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_common.c
  5. * \brief Contains code shared between different HS protocol version as well
  6. * as useful data structures and accessors used by other subsystems.
  7. * The rendcommon.c should only contains code relating to the v2
  8. * protocol.
  9. **/
  10. #include "or.h"
  11. #include "config.h"
  12. #include "networkstatus.h"
  13. #include "hs_common.h"
  14. #include "rendcommon.h"
  15. /* Create a new rend_data_t for a specific given <b>version</b>.
  16. * Return a pointer to the newly allocated data structure. */
  17. static rend_data_t *
  18. rend_data_alloc(uint32_t version)
  19. {
  20. rend_data_t *rend_data = NULL;
  21. switch (version) {
  22. case HS_VERSION_TWO:
  23. {
  24. rend_data_v2_t *v2 = tor_malloc_zero(sizeof(*v2));
  25. v2->base_.version = HS_VERSION_TWO;
  26. v2->base_.hsdirs_fp = smartlist_new();
  27. rend_data = &v2->base_;
  28. break;
  29. }
  30. default:
  31. tor_assert(0);
  32. break;
  33. }
  34. return rend_data;
  35. }
  36. /** Free all storage associated with <b>data</b> */
  37. void
  38. rend_data_free(rend_data_t *data)
  39. {
  40. if (!data) {
  41. return;
  42. }
  43. /* By using our allocation function, this should always be set. */
  44. tor_assert(data->hsdirs_fp);
  45. /* Cleanup the HSDir identity digest. */
  46. SMARTLIST_FOREACH(data->hsdirs_fp, char *, d, tor_free(d));
  47. smartlist_free(data->hsdirs_fp);
  48. /* Depending on the version, cleanup. */
  49. switch (data->version) {
  50. case HS_VERSION_TWO:
  51. {
  52. rend_data_v2_t *v2_data = TO_REND_DATA_V2(data);
  53. tor_free(v2_data);
  54. break;
  55. }
  56. default:
  57. tor_assert(0);
  58. }
  59. }
  60. /* Allocate and return a deep copy of <b>data</b>. */
  61. rend_data_t *
  62. rend_data_dup(const rend_data_t *data)
  63. {
  64. rend_data_t *data_dup = NULL;
  65. smartlist_t *hsdirs_fp = smartlist_new();
  66. tor_assert(data);
  67. tor_assert(data->hsdirs_fp);
  68. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  69. smartlist_add(hsdirs_fp, tor_memdup(fp, DIGEST_LEN)));
  70. switch (data->version) {
  71. case HS_VERSION_TWO:
  72. {
  73. rend_data_v2_t *v2_data = tor_memdup(TO_REND_DATA_V2(data),
  74. sizeof(*v2_data));
  75. data_dup = &v2_data->base_;
  76. data_dup->hsdirs_fp = hsdirs_fp;
  77. break;
  78. }
  79. default:
  80. tor_assert(0);
  81. break;
  82. }
  83. return data_dup;
  84. }
  85. /* Compute the descriptor ID for each HS descriptor replica and save them. A
  86. * valid onion address must be present in the <b>rend_data</b>.
  87. *
  88. * Return 0 on success else -1. */
  89. static int
  90. compute_desc_id(rend_data_t *rend_data)
  91. {
  92. int ret = 0;
  93. unsigned replica;
  94. time_t now = time(NULL);
  95. tor_assert(rend_data);
  96. switch (rend_data->version) {
  97. case HS_VERSION_TWO:
  98. {
  99. rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  100. /* Compute descriptor ID for each replicas. */
  101. for (replica = 0; replica < ARRAY_LENGTH(v2_data->descriptor_id);
  102. replica++) {
  103. ret = rend_compute_v2_desc_id(v2_data->descriptor_id[replica],
  104. v2_data->onion_address,
  105. v2_data->descriptor_cookie,
  106. now, replica);
  107. if (ret < 0) {
  108. goto end;
  109. }
  110. }
  111. break;
  112. }
  113. default:
  114. tor_assert(0);
  115. }
  116. end:
  117. return ret;
  118. }
  119. /* Allocate and initialize a rend_data_t object for a service using the
  120. * provided arguments. All arguments are optional (can be NULL), except from
  121. * <b>onion_address</b> which MUST be set. The <b>pk_digest</b> is the hash of
  122. * the service private key. The <b>cookie</b> is the rendezvous cookie and
  123. * <b>auth_type</b> is which authentiation this service is configured with.
  124. *
  125. * Return a valid rend_data_t pointer. This only returns a version 2 object of
  126. * rend_data_t. */
  127. rend_data_t *
  128. rend_data_service_create(const char *onion_address, const char *pk_digest,
  129. const uint8_t *cookie, rend_auth_type_t auth_type)
  130. {
  131. /* Create a rend_data_t object for version 2. */
  132. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  133. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  134. /* We need at least one else the call is wrong. */
  135. tor_assert(onion_address != NULL);
  136. if (pk_digest) {
  137. memcpy(v2->rend_pk_digest, pk_digest, sizeof(v2->rend_pk_digest));
  138. }
  139. if (cookie) {
  140. memcpy(rend_data->rend_cookie, cookie, sizeof(rend_data->rend_cookie));
  141. }
  142. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  143. v2->auth_type = auth_type;
  144. return rend_data;
  145. }
  146. /* Allocate and initialize a rend_data_t object for a client request using the
  147. * given arguments. Either an onion address or a descriptor ID is needed. Both
  148. * can be given but in this case only the onion address will be used to make
  149. * the descriptor fetch. The <b>cookie</b> is the rendezvous cookie and
  150. * <b>auth_type</b> is which authentiation the service is configured with.
  151. *
  152. * Return a valid rend_data_t pointer or NULL on error meaning the
  153. * descriptor IDs couldn't be computed from the given data. */
  154. rend_data_t *
  155. rend_data_client_create(const char *onion_address, const char *desc_id,
  156. const char *cookie, rend_auth_type_t auth_type)
  157. {
  158. /* Create a rend_data_t object for version 2. */
  159. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  160. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  161. /* We need at least one else the call is wrong. */
  162. tor_assert(onion_address != NULL || desc_id != NULL);
  163. if (cookie) {
  164. memcpy(v2->descriptor_cookie, cookie, sizeof(v2->descriptor_cookie));
  165. }
  166. if (desc_id) {
  167. memcpy(v2->desc_id_fetch, desc_id, sizeof(v2->desc_id_fetch));
  168. }
  169. if (onion_address) {
  170. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  171. if (compute_desc_id(rend_data) < 0) {
  172. goto error;
  173. }
  174. }
  175. v2->auth_type = auth_type;
  176. return rend_data;
  177. error:
  178. rend_data_free(rend_data);
  179. return NULL;
  180. }
  181. /* Return the onion address from the rend data. Depending on the version,
  182. * the size of the address can vary but it's always NUL terminated. */
  183. const char *
  184. rend_data_get_address(const rend_data_t *rend_data)
  185. {
  186. tor_assert(rend_data);
  187. switch (rend_data->version) {
  188. case HS_VERSION_TWO:
  189. return TO_REND_DATA_V2(rend_data)->onion_address;
  190. default:
  191. /* We should always have a supported version. */
  192. tor_assert(0);
  193. }
  194. }
  195. /* Return the descriptor ID for a specific replica number from the rend
  196. * data. The returned data is a binary digest and depending on the version its
  197. * size can vary. The size of the descriptor ID is put in <b>len_out</b> if
  198. * non NULL. */
  199. const char *
  200. rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica,
  201. size_t *len_out)
  202. {
  203. tor_assert(rend_data);
  204. switch (rend_data->version) {
  205. case HS_VERSION_TWO:
  206. tor_assert(replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  207. if (len_out) {
  208. *len_out = DIGEST_LEN;
  209. }
  210. return TO_REND_DATA_V2(rend_data)->descriptor_id[replica];
  211. default:
  212. /* We should always have a supported version. */
  213. tor_assert(0);
  214. }
  215. }
  216. /* Return the public key digest using the given <b>rend_data</b>. The size of
  217. * the digest is put in <b>len_out</b> (if set) which can differ depending on
  218. * the version. */
  219. const uint8_t *
  220. rend_data_get_pk_digest(const rend_data_t *rend_data, size_t *len_out)
  221. {
  222. tor_assert(rend_data);
  223. switch (rend_data->version) {
  224. case HS_VERSION_TWO:
  225. {
  226. const rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  227. if (len_out) {
  228. *len_out = sizeof(v2_data->rend_pk_digest);
  229. }
  230. return (const uint8_t *) v2_data->rend_pk_digest;
  231. }
  232. default:
  233. /* We should always have a supported version. */
  234. tor_assert(0);
  235. }
  236. }