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