hs_control.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_control.c
  5. * \brief Contains control port event related code.
  6. **/
  7. #include "core/or/or.h"
  8. #include "feature/control/control.h"
  9. #include "lib/crypt_ops/crypto_format.h"
  10. #include "lib/crypt_ops/crypto_util.h"
  11. #include "feature/hs/hs_client.h"
  12. #include "feature/hs/hs_common.h"
  13. #include "feature/hs/hs_control.h"
  14. #include "feature/hs/hs_descriptor.h"
  15. #include "feature/hs/hs_service.h"
  16. #include "feature/nodelist/nodelist.h"
  17. #include "feature/nodelist/node_st.h"
  18. #include "feature/nodelist/routerstatus_st.h"
  19. /* Send on the control port the "HS_DESC REQUESTED [...]" event.
  20. *
  21. * The onion_pk is the onion service public key, base64_blinded_pk is the
  22. * base64 encoded blinded key for the service and hsdir_rs is the routerstatus
  23. * object of the HSDir that this request is for. */
  24. void
  25. hs_control_desc_event_requested(const ed25519_public_key_t *onion_pk,
  26. const char *base64_blinded_pk,
  27. const routerstatus_t *hsdir_rs)
  28. {
  29. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  30. const uint8_t *hsdir_index;
  31. const node_t *hsdir_node;
  32. tor_assert(onion_pk);
  33. tor_assert(base64_blinded_pk);
  34. tor_assert(hsdir_rs);
  35. hs_build_address(onion_pk, HS_VERSION_THREE, onion_address);
  36. /* Get the node from the routerstatus object to get the HSDir index used for
  37. * this request. We can't have a routerstatus entry without a node and we
  38. * can't pick a node without an hsdir_index. */
  39. hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
  40. tor_assert(hsdir_node);
  41. /* This is a fetch event. */
  42. hsdir_index = hsdir_node->hsdir_index.fetch;
  43. /* Trigger the event. */
  44. control_event_hs_descriptor_requested(onion_address, REND_NO_AUTH,
  45. hsdir_rs->identity_digest,
  46. base64_blinded_pk,
  47. hex_str((const char *) hsdir_index,
  48. DIGEST256_LEN));
  49. memwipe(onion_address, 0, sizeof(onion_address));
  50. }
  51. /* Send on the control port the "HS_DESC FAILED [...]" event.
  52. *
  53. * Using a directory connection identifier, the HSDir identity digest and a
  54. * reason for the failure. None can be NULL. */
  55. void
  56. hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident,
  57. const char *hsdir_id_digest,
  58. const char *reason)
  59. {
  60. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  61. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  62. tor_assert(ident);
  63. tor_assert(hsdir_id_digest);
  64. tor_assert(reason);
  65. /* Build onion address and encoded blinded key. */
  66. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
  67. &ident->blinded_pk) < 0) {
  68. return;
  69. }
  70. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  71. control_event_hsv3_descriptor_failed(onion_address, base64_blinded_pk,
  72. hsdir_id_digest, reason);
  73. }
  74. /* Send on the control port the "HS_DESC RECEIVED [...]" event.
  75. *
  76. * Using a directory connection identifier and the HSDir identity digest.
  77. * None can be NULL. */
  78. void
  79. hs_control_desc_event_received(const hs_ident_dir_conn_t *ident,
  80. const char *hsdir_id_digest)
  81. {
  82. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  83. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  84. tor_assert(ident);
  85. tor_assert(hsdir_id_digest);
  86. /* Build onion address and encoded blinded key. */
  87. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
  88. &ident->blinded_pk) < 0) {
  89. return;
  90. }
  91. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  92. control_event_hsv3_descriptor_received(onion_address, base64_blinded_pk,
  93. hsdir_id_digest);
  94. }
  95. /* Send on the control port the "HS_DESC CREATED [...]" event.
  96. *
  97. * Using the onion address of the descriptor's service and the blinded public
  98. * key of the descriptor as a descriptor ID. None can be NULL. */
  99. void
  100. hs_control_desc_event_created(const char *onion_address,
  101. const ed25519_public_key_t *blinded_pk)
  102. {
  103. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  104. tor_assert(onion_address);
  105. tor_assert(blinded_pk);
  106. /* Build base64 encoded blinded key. */
  107. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
  108. return;
  109. }
  110. /* Version 3 doesn't use the replica number in its descriptor ID computation
  111. * so we pass negative value so the control port subsystem can ignore it. */
  112. control_event_hs_descriptor_created(onion_address, base64_blinded_pk, -1);
  113. }
  114. /* Send on the control port the "HS_DESC UPLOAD [...]" event.
  115. *
  116. * Using the onion address of the descriptor's service, the HSDir identity
  117. * digest, the blinded public key of the descriptor as a descriptor ID and the
  118. * HSDir index for this particular request. None can be NULL. */
  119. void
  120. hs_control_desc_event_upload(const char *onion_address,
  121. const char *hsdir_id_digest,
  122. const ed25519_public_key_t *blinded_pk,
  123. const uint8_t *hsdir_index)
  124. {
  125. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  126. tor_assert(onion_address);
  127. tor_assert(hsdir_id_digest);
  128. tor_assert(blinded_pk);
  129. tor_assert(hsdir_index);
  130. /* Build base64 encoded blinded key. */
  131. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
  132. return;
  133. }
  134. control_event_hs_descriptor_upload(onion_address, hsdir_id_digest,
  135. base64_blinded_pk,
  136. hex_str((const char *) hsdir_index,
  137. DIGEST256_LEN));
  138. }
  139. /* Send on the control port the "HS_DESC UPLOADED [...]" event.
  140. *
  141. * Using the directory connection identifier and the HSDir identity digest.
  142. * None can be NULL. */
  143. void
  144. hs_control_desc_event_uploaded(const hs_ident_dir_conn_t *ident,
  145. const char *hsdir_id_digest)
  146. {
  147. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  148. tor_assert(ident);
  149. tor_assert(hsdir_id_digest);
  150. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  151. control_event_hs_descriptor_uploaded(hsdir_id_digest, onion_address);
  152. }
  153. /* Send on the control port the "HS_DESC_CONTENT [...]" event.
  154. *
  155. * Using the directory connection identifier, the HSDir identity digest and
  156. * the body of the descriptor (as it was received from the directory). None
  157. * can be NULL. */
  158. void
  159. hs_control_desc_event_content(const hs_ident_dir_conn_t *ident,
  160. const char *hsdir_id_digest,
  161. const char *body)
  162. {
  163. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  164. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  165. tor_assert(ident);
  166. tor_assert(hsdir_id_digest);
  167. /* Build onion address and encoded blinded key. */
  168. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
  169. &ident->blinded_pk) < 0) {
  170. return;
  171. }
  172. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  173. control_event_hs_descriptor_content(onion_address, base64_blinded_pk,
  174. hsdir_id_digest, body);
  175. }
  176. /* Handle the "HSPOST [...]" command. The body is an encoded descriptor for
  177. * the given onion_address. The descriptor will be uploaded to each directory
  178. * in hsdirs_rs. If NULL, the responsible directories for the current time
  179. * period will be selected.
  180. *
  181. * Return -1 on if the descriptor plaintext section is not decodable. Else, 0
  182. * on success. */
  183. int
  184. hs_control_hspost_command(const char *body, const char *onion_address,
  185. const smartlist_t *hsdirs_rs)
  186. {
  187. int ret = -1;
  188. ed25519_public_key_t identity_pk;
  189. hs_desc_plaintext_data_t plaintext;
  190. smartlist_t *hsdirs = NULL;
  191. tor_assert(body);
  192. tor_assert(onion_address);
  193. /* This can't fail because we require the caller to pass us a valid onion
  194. * address that has passed hs_address_is_valid(). */
  195. if (BUG(hs_parse_address(onion_address, &identity_pk, NULL, NULL) < 0)) {
  196. goto done; // LCOV_EXCL_LINE
  197. }
  198. /* Only decode the plaintext part which is what the directory will do to
  199. * validate before caching. */
  200. if (hs_desc_decode_plaintext(body, &plaintext) < 0) {
  201. goto done;
  202. }
  203. /* No HSDir(s) given, we'll compute what the current ones should be. */
  204. if (hsdirs_rs == NULL) {
  205. hsdirs = smartlist_new();
  206. hs_get_responsible_hsdirs(&plaintext.blinded_pubkey,
  207. hs_get_time_period_num(0),
  208. 0, /* Always the current descriptor which uses
  209. * the first hsdir index. */
  210. 0, /* It is for storing on a directory. */
  211. hsdirs);
  212. hsdirs_rs = hsdirs;
  213. }
  214. SMARTLIST_FOREACH_BEGIN(hsdirs_rs, const routerstatus_t *, rs) {
  215. hs_service_upload_desc_to_dir(body, plaintext.version, &identity_pk,
  216. &plaintext.blinded_pubkey, rs);
  217. } SMARTLIST_FOREACH_END(rs);
  218. ret = 0;
  219. done:
  220. /* We don't have ownership of the objects in this list. */
  221. smartlist_free(hsdirs);
  222. return ret;
  223. }
  224. /* With a given <b>onion_identity_pk</b>, fetch its descriptor, optionally
  225. * using the list of directory servers given in <b>hsdirs</b>, or a random
  226. * server if it is NULL. This function calls hs_client_launch_v3_desc_fetch().
  227. */
  228. void
  229. hs_control_hsfetch_command(const ed25519_public_key_t *onion_identity_pk,
  230. const smartlist_t *hsdirs)
  231. {
  232. tor_assert(onion_identity_pk);
  233. hs_client_launch_v3_desc_fetch(onion_identity_pk, hsdirs);
  234. }