hs_control.c 9.1 KB

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