hs_control.c 8.9 KB

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