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