hs_control.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (c) 2017, 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 "or.h"
  8. #include "control.h"
  9. #include "hs_common.h"
  10. #include "hs_control.h"
  11. #include "nodelist.h"
  12. /* Send on the control port the "HS_DESC REQUESTED [...]" event.
  13. *
  14. * The onion_pk is the onion service public key, base64_blinded_pk is the
  15. * base64 encoded blinded key for the service and hsdir_rs is the routerstatus
  16. * object of the HSDir that this request is for. */
  17. void
  18. hs_control_desc_event_requested(const ed25519_public_key_t *onion_pk,
  19. const char *base64_blinded_pk,
  20. const routerstatus_t *hsdir_rs)
  21. {
  22. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  23. const uint8_t *hsdir_index;
  24. const node_t *hsdir_node;
  25. tor_assert(onion_pk);
  26. tor_assert(base64_blinded_pk);
  27. tor_assert(hsdir_rs);
  28. hs_build_address(onion_pk, HS_VERSION_THREE, onion_address);
  29. /* Get the node from the routerstatus object to get the HSDir index used for
  30. * this request. We can't have a routerstatus entry without a node and we
  31. * can't pick a node without an hsdir_index. */
  32. hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
  33. tor_assert(hsdir_node);
  34. tor_assert(hsdir_node->hsdir_index);
  35. /* This is a fetch event. */
  36. hsdir_index = hsdir_node->hsdir_index->fetch;
  37. /* Trigger the event. */
  38. control_event_hs_descriptor_requested(onion_address, REND_NO_AUTH,
  39. hsdir_rs->identity_digest,
  40. base64_blinded_pk,
  41. hex_str((const char *) hsdir_index,
  42. DIGEST256_LEN));
  43. memwipe(onion_address, 0, sizeof(onion_address));
  44. }
  45. /* Send on the control port the "HS_DESC FAILED [...]" event.
  46. *
  47. * Using a directory connection identifier, the HSDir identity digest and a
  48. * reason for the failure. None can be NULL. */
  49. void
  50. hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident,
  51. const char *hsdir_id_digest,
  52. const char *reason)
  53. {
  54. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  55. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  56. tor_assert(ident);
  57. tor_assert(hsdir_id_digest);
  58. tor_assert(reason);
  59. /* Build onion address and encoded blinded key. */
  60. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
  61. &ident->blinded_pk) < 0) {
  62. return;
  63. }
  64. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  65. control_event_hsv3_descriptor_failed(onion_address, base64_blinded_pk,
  66. hsdir_id_digest, reason);
  67. }
  68. /* Send on the control port the "HS_DESC RECEIVED [...]" event.
  69. *
  70. * Using a directory connection identifier and the HSDir identity digest.
  71. * None can be NULL. */
  72. void
  73. hs_control_desc_event_received(const hs_ident_dir_conn_t *ident,
  74. const char *hsdir_id_digest)
  75. {
  76. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  77. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  78. tor_assert(ident);
  79. tor_assert(hsdir_id_digest);
  80. /* Build onion address and encoded blinded key. */
  81. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
  82. &ident->blinded_pk) < 0) {
  83. return;
  84. }
  85. hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);
  86. control_event_hsv3_descriptor_received(onion_address, base64_blinded_pk,
  87. hsdir_id_digest);
  88. }
  89. /* Send on the control port the "HS_DESC CREATED [...]" event.
  90. *
  91. * Using the onion address of the descriptor's service and the blinded public
  92. * key of the descriptor as a descriptor ID. None can be NULL. */
  93. void
  94. hs_control_desc_event_created(const char *onion_address,
  95. const ed25519_public_key_t *blinded_pk)
  96. {
  97. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  98. tor_assert(onion_address);
  99. tor_assert(blinded_pk);
  100. /* Build base64 encoded blinded key. */
  101. IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
  102. return;
  103. }
  104. /* Version 3 doesn't use the replica number in its descriptor ID computation
  105. * so we pass negative value so the control port subsystem can ignore it. */
  106. control_event_hs_descriptor_created(onion_address, base64_blinded_pk, -1);
  107. }