hs_common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.
  122. *
  123. * Return a valid rend_data_t pointer. This only returns a version 2 object of
  124. * rend_data_t. */
  125. rend_data_t *
  126. rend_data_service_create(const char *onion_address, const char *pk_digest,
  127. const uint8_t *cookie, rend_auth_type_t auth_type)
  128. {
  129. /* Create a rend_data_t object for version 2. */
  130. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  131. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  132. /* We need at least one else the call is wrong. */
  133. tor_assert(onion_address != NULL);
  134. if (pk_digest) {
  135. memcpy(v2->rend_pk_digest, pk_digest, sizeof(v2->rend_pk_digest));
  136. }
  137. if (cookie) {
  138. memcpy(rend_data->rend_cookie, cookie, sizeof(rend_data->rend_cookie));
  139. }
  140. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  141. v2->auth_type = auth_type;
  142. return rend_data;
  143. }
  144. /* Allocate and initialize a rend_data_t object for a client request using
  145. * the given arguments. Either an onion address or a descriptor ID is
  146. * needed. Both can be given but only the onion address will be used to make
  147. * the descriptor fetch.
  148. *
  149. * Return a valid rend_data_t pointer or NULL on error meaning the
  150. * descriptor IDs couldn't be computed from the given data. */
  151. rend_data_t *
  152. rend_data_client_create(const char *onion_address, const char *desc_id,
  153. const char *cookie, rend_auth_type_t auth_type)
  154. {
  155. /* Create a rend_data_t object for version 2. */
  156. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  157. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  158. /* We need at least one else the call is wrong. */
  159. tor_assert(onion_address != NULL || desc_id != NULL);
  160. if (cookie) {
  161. memcpy(v2->descriptor_cookie, cookie, sizeof(v2->descriptor_cookie));
  162. }
  163. if (desc_id) {
  164. memcpy(v2->desc_id_fetch, desc_id, sizeof(v2->desc_id_fetch));
  165. }
  166. if (onion_address) {
  167. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  168. if (compute_desc_id(rend_data) < 0) {
  169. goto error;
  170. }
  171. }
  172. v2->auth_type = auth_type;
  173. return rend_data;
  174. error:
  175. rend_data_free(rend_data);
  176. return NULL;
  177. }
  178. /* Return the onion address from the rend data. Depending on the version,
  179. * the size of the address can vary but it's always NUL terminated. */
  180. const char *
  181. rend_data_get_address(const rend_data_t *rend_data)
  182. {
  183. tor_assert(rend_data);
  184. switch (rend_data->version) {
  185. case HS_VERSION_TWO:
  186. return TO_REND_DATA_V2(rend_data)->onion_address;
  187. default:
  188. /* We should always have a supported version. */
  189. tor_assert(0);
  190. }
  191. }
  192. /* Return the descriptor ID for a specific replica number from the rend
  193. * data. The returned data is a binary digest and depending on the version its
  194. * size can vary. The size of the descriptor ID is put in <b>len_out</b> if
  195. * non NULL. */
  196. const char *
  197. rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica,
  198. size_t *len_out)
  199. {
  200. tor_assert(rend_data);
  201. switch (rend_data->version) {
  202. case HS_VERSION_TWO:
  203. tor_assert(replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  204. if (len_out) {
  205. *len_out = DIGEST_LEN;
  206. }
  207. return TO_REND_DATA_V2(rend_data)->descriptor_id[replica];
  208. default:
  209. /* We should always have a supported version. */
  210. tor_assert(0);
  211. }
  212. }
  213. /* Return the public key digest using the given <b>rend_data</b>. The size of
  214. * the digest is put in <b>len_out</b> (if set) which can differ depending on
  215. * the version. */
  216. const uint8_t *
  217. rend_data_get_pk_digest(const rend_data_t *rend_data, size_t *len_out)
  218. {
  219. tor_assert(rend_data);
  220. switch (rend_data->version) {
  221. case HS_VERSION_TWO:
  222. {
  223. const rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  224. if (len_out) {
  225. *len_out = sizeof(v2_data->rend_pk_digest);
  226. }
  227. return (const uint8_t *) v2_data->rend_pk_digest;
  228. }
  229. default:
  230. /* We should always have a supported version. */
  231. tor_assert(0);
  232. }
  233. }
  234. /* Return true iff the Onion Services protocol version 3 is enabled. This only
  235. * considers the consensus parameter. If the parameter is not found, the
  236. * default is that it's enabled. */
  237. int
  238. hs_v3_protocol_is_enabled(void)
  239. {
  240. /* This consensus param controls if the the onion services version 3 is
  241. * enabled or not which is the first version of the next generation
  242. * (proposal 224). If this option is set to 0, the tor daemon won't support
  243. * the protocol as either a relay, directory, service or client. By default,
  244. * it's enabled if the parameter is not found. */
  245. return networkstatus_get_param(NULL, "EnableOnionServicesV3", 1, 0, 1);
  246. }