hs_common.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright (c) 2016-2017, 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. /* Make sure that the directory for <b>service</b> is private, using the config
  16. * <b>username</b>.
  17. * If <b>create</b> is true:
  18. * - if the directory exists, change permissions if needed,
  19. * - if the directory does not exist, create it with the correct permissions.
  20. * If <b>create</b> is false:
  21. * - if the directory exists, check permissions,
  22. * - if the directory does not exist, check if we think we can create it.
  23. * Return 0 on success, -1 on failure. */
  24. int
  25. hs_check_service_private_dir(const char *username, const char *path,
  26. unsigned int dir_group_readable,
  27. unsigned int create)
  28. {
  29. cpd_check_t check_opts = CPD_NONE;
  30. tor_assert(path);
  31. if (create) {
  32. check_opts |= CPD_CREATE;
  33. } else {
  34. check_opts |= CPD_CHECK_MODE_ONLY;
  35. check_opts |= CPD_CHECK;
  36. }
  37. if (dir_group_readable) {
  38. check_opts |= CPD_GROUP_READ;
  39. }
  40. /* Check/create directory */
  41. if (check_private_dir(path, check_opts, username) < 0) {
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. /* Create a new rend_data_t for a specific given <b>version</b>.
  47. * Return a pointer to the newly allocated data structure. */
  48. static rend_data_t *
  49. rend_data_alloc(uint32_t version)
  50. {
  51. rend_data_t *rend_data = NULL;
  52. switch (version) {
  53. case HS_VERSION_TWO:
  54. {
  55. rend_data_v2_t *v2 = tor_malloc_zero(sizeof(*v2));
  56. v2->base_.version = HS_VERSION_TWO;
  57. v2->base_.hsdirs_fp = smartlist_new();
  58. rend_data = &v2->base_;
  59. break;
  60. }
  61. default:
  62. tor_assert(0);
  63. break;
  64. }
  65. return rend_data;
  66. }
  67. /** Free all storage associated with <b>data</b> */
  68. void
  69. rend_data_free(rend_data_t *data)
  70. {
  71. if (!data) {
  72. return;
  73. }
  74. /* By using our allocation function, this should always be set. */
  75. tor_assert(data->hsdirs_fp);
  76. /* Cleanup the HSDir identity digest. */
  77. SMARTLIST_FOREACH(data->hsdirs_fp, char *, d, tor_free(d));
  78. smartlist_free(data->hsdirs_fp);
  79. /* Depending on the version, cleanup. */
  80. switch (data->version) {
  81. case HS_VERSION_TWO:
  82. {
  83. rend_data_v2_t *v2_data = TO_REND_DATA_V2(data);
  84. tor_free(v2_data);
  85. break;
  86. }
  87. default:
  88. tor_assert(0);
  89. }
  90. }
  91. /* Allocate and return a deep copy of <b>data</b>. */
  92. rend_data_t *
  93. rend_data_dup(const rend_data_t *data)
  94. {
  95. rend_data_t *data_dup = NULL;
  96. smartlist_t *hsdirs_fp = smartlist_new();
  97. tor_assert(data);
  98. tor_assert(data->hsdirs_fp);
  99. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  100. smartlist_add(hsdirs_fp, tor_memdup(fp, DIGEST_LEN)));
  101. switch (data->version) {
  102. case HS_VERSION_TWO:
  103. {
  104. rend_data_v2_t *v2_data = tor_memdup(TO_REND_DATA_V2(data),
  105. sizeof(*v2_data));
  106. data_dup = &v2_data->base_;
  107. data_dup->hsdirs_fp = hsdirs_fp;
  108. break;
  109. }
  110. default:
  111. tor_assert(0);
  112. break;
  113. }
  114. return data_dup;
  115. }
  116. /* Compute the descriptor ID for each HS descriptor replica and save them. A
  117. * valid onion address must be present in the <b>rend_data</b>.
  118. *
  119. * Return 0 on success else -1. */
  120. static int
  121. compute_desc_id(rend_data_t *rend_data)
  122. {
  123. int ret = 0;
  124. unsigned replica;
  125. time_t now = time(NULL);
  126. tor_assert(rend_data);
  127. switch (rend_data->version) {
  128. case HS_VERSION_TWO:
  129. {
  130. rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  131. /* Compute descriptor ID for each replicas. */
  132. for (replica = 0; replica < ARRAY_LENGTH(v2_data->descriptor_id);
  133. replica++) {
  134. ret = rend_compute_v2_desc_id(v2_data->descriptor_id[replica],
  135. v2_data->onion_address,
  136. v2_data->descriptor_cookie,
  137. now, replica);
  138. if (ret < 0) {
  139. goto end;
  140. }
  141. }
  142. break;
  143. }
  144. default:
  145. tor_assert(0);
  146. }
  147. end:
  148. return ret;
  149. }
  150. /* Allocate and initialize a rend_data_t object for a service using the
  151. * provided arguments. All arguments are optional (can be NULL), except from
  152. * <b>onion_address</b> which MUST be set. The <b>pk_digest</b> is the hash of
  153. * the service private key. The <b>cookie</b> is the rendezvous cookie and
  154. * <b>auth_type</b> is which authentiation this service is configured with.
  155. *
  156. * Return a valid rend_data_t pointer. This only returns a version 2 object of
  157. * rend_data_t. */
  158. rend_data_t *
  159. rend_data_service_create(const char *onion_address, const char *pk_digest,
  160. const uint8_t *cookie, rend_auth_type_t auth_type)
  161. {
  162. /* Create a rend_data_t object for version 2. */
  163. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  164. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  165. /* We need at least one else the call is wrong. */
  166. tor_assert(onion_address != NULL);
  167. if (pk_digest) {
  168. memcpy(v2->rend_pk_digest, pk_digest, sizeof(v2->rend_pk_digest));
  169. }
  170. if (cookie) {
  171. memcpy(rend_data->rend_cookie, cookie, sizeof(rend_data->rend_cookie));
  172. }
  173. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  174. v2->auth_type = auth_type;
  175. return rend_data;
  176. }
  177. /* Allocate and initialize a rend_data_t object for a client request using the
  178. * given arguments. Either an onion address or a descriptor ID is needed. Both
  179. * can be given but in this case only the onion address will be used to make
  180. * the descriptor fetch. The <b>cookie</b> is the rendezvous cookie and
  181. * <b>auth_type</b> is which authentiation the service is configured with.
  182. *
  183. * Return a valid rend_data_t pointer or NULL on error meaning the
  184. * descriptor IDs couldn't be computed from the given data. */
  185. rend_data_t *
  186. rend_data_client_create(const char *onion_address, const char *desc_id,
  187. const char *cookie, rend_auth_type_t auth_type)
  188. {
  189. /* Create a rend_data_t object for version 2. */
  190. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  191. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  192. /* We need at least one else the call is wrong. */
  193. tor_assert(onion_address != NULL || desc_id != NULL);
  194. if (cookie) {
  195. memcpy(v2->descriptor_cookie, cookie, sizeof(v2->descriptor_cookie));
  196. }
  197. if (desc_id) {
  198. memcpy(v2->desc_id_fetch, desc_id, sizeof(v2->desc_id_fetch));
  199. }
  200. if (onion_address) {
  201. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  202. if (compute_desc_id(rend_data) < 0) {
  203. goto error;
  204. }
  205. }
  206. v2->auth_type = auth_type;
  207. return rend_data;
  208. error:
  209. rend_data_free(rend_data);
  210. return NULL;
  211. }
  212. /* Return the onion address from the rend data. Depending on the version,
  213. * the size of the address can vary but it's always NUL terminated. */
  214. const char *
  215. rend_data_get_address(const rend_data_t *rend_data)
  216. {
  217. tor_assert(rend_data);
  218. switch (rend_data->version) {
  219. case HS_VERSION_TWO:
  220. return TO_REND_DATA_V2(rend_data)->onion_address;
  221. default:
  222. /* We should always have a supported version. */
  223. tor_assert(0);
  224. }
  225. }
  226. /* Return the descriptor ID for a specific replica number from the rend
  227. * data. The returned data is a binary digest and depending on the version its
  228. * size can vary. The size of the descriptor ID is put in <b>len_out</b> if
  229. * non NULL. */
  230. const char *
  231. rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica,
  232. size_t *len_out)
  233. {
  234. tor_assert(rend_data);
  235. switch (rend_data->version) {
  236. case HS_VERSION_TWO:
  237. tor_assert(replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  238. if (len_out) {
  239. *len_out = DIGEST_LEN;
  240. }
  241. return TO_REND_DATA_V2(rend_data)->descriptor_id[replica];
  242. default:
  243. /* We should always have a supported version. */
  244. tor_assert(0);
  245. }
  246. }
  247. /* Return the public key digest using the given <b>rend_data</b>. The size of
  248. * the digest is put in <b>len_out</b> (if set) which can differ depending on
  249. * the version. */
  250. const uint8_t *
  251. rend_data_get_pk_digest(const rend_data_t *rend_data, size_t *len_out)
  252. {
  253. tor_assert(rend_data);
  254. switch (rend_data->version) {
  255. case HS_VERSION_TWO:
  256. {
  257. const rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  258. if (len_out) {
  259. *len_out = sizeof(v2_data->rend_pk_digest);
  260. }
  261. return (const uint8_t *) v2_data->rend_pk_digest;
  262. }
  263. default:
  264. /* We should always have a supported version. */
  265. tor_assert(0);
  266. }
  267. }