aboutsummaryrefslogtreecommitdiff
path: root/src/feature/hs/hs_control.c
blob: 9970fdd1230a2b933bac3e832cb12e249db98a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright (c) 2017-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file hs_control.c
 * \brief Contains control port event related code.
 **/

#include "core/or/or.h"
#include "feature/control/control.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/crypt_ops/crypto_util.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_control.h"
#include "feature/hs/hs_descriptor.h"
#include "feature/hs/hs_service.h"
#include "feature/nodelist/nodelist.h"

#include "feature/nodelist/node_st.h"
#include "feature/nodelist/routerstatus_st.h"

/* Send on the control port the "HS_DESC REQUESTED [...]" event.
 *
 * The onion_pk is the onion service public key, base64_blinded_pk is the
 * base64 encoded blinded key for the service and hsdir_rs is the routerstatus
 * object of the HSDir that this request is for. */
void
hs_control_desc_event_requested(const ed25519_public_key_t *onion_pk,
                                const char *base64_blinded_pk,
                                const routerstatus_t *hsdir_rs)
{
  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  const uint8_t *hsdir_index;
  const node_t *hsdir_node;

  tor_assert(onion_pk);
  tor_assert(base64_blinded_pk);
  tor_assert(hsdir_rs);

  hs_build_address(onion_pk, HS_VERSION_THREE, onion_address);

  /* Get the node from the routerstatus object to get the HSDir index used for
   * this request. We can't have a routerstatus entry without a node and we
   * can't pick a node without an hsdir_index. */
  hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
  tor_assert(hsdir_node);
  /* This is a fetch event. */
  hsdir_index = hsdir_node->hsdir_index.fetch;

  /* Trigger the event. */
  control_event_hs_descriptor_requested(onion_address, REND_NO_AUTH,
                                        hsdir_rs->identity_digest,
                                        base64_blinded_pk,
                                        hex_str((const char *) hsdir_index,
                                                DIGEST256_LEN));
  memwipe(onion_address, 0, sizeof(onion_address));
}

/* Send on the control port the "HS_DESC FAILED [...]" event.
 *
 * Using a directory connection identifier, the HSDir identity digest and a
 * reason for the failure. None can be NULL. */
void
hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident,
                             const char *hsdir_id_digest,
                             const char *reason)
{
  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  char base64_blinded_pk[ED25519_BASE64_LEN + 1];

  tor_assert(ident);
  tor_assert(hsdir_id_digest);
  tor_assert(reason);

  /* Build onion address and encoded blinded key. */
  IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
                                       &ident->blinded_pk) < 0) {
    return;
  }
  hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

  control_event_hsv3_descriptor_failed(onion_address, base64_blinded_pk,
                                       hsdir_id_digest, reason);
}

/* Send on the control port the "HS_DESC RECEIVED [...]" event.
 *
 * Using a directory connection identifier and the HSDir identity digest.
 * None can be NULL. */
void
hs_control_desc_event_received(const hs_ident_dir_conn_t *ident,
                               const char *hsdir_id_digest)
{
  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  char base64_blinded_pk[ED25519_BASE64_LEN + 1];

  tor_assert(ident);
  tor_assert(hsdir_id_digest);

  /* Build onion address and encoded blinded key. */
  IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
                                       &ident->blinded_pk) < 0) {
    return;
  }
  hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

  control_event_hsv3_descriptor_received(onion_address, base64_blinded_pk,
                                         hsdir_id_digest);
}

/* Send on the control port the "HS_DESC CREATED [...]" event.
 *
 * Using the onion address of the descriptor's service and the blinded public
 * key of the descriptor as a descriptor ID. None can be NULL. */
void
hs_control_desc_event_created(const char *onion_address,
                              const ed25519_public_key_t *blinded_pk)
{
  char base64_blinded_pk[ED25519_BASE64_LEN + 1];

  tor_assert(onion_address);
  tor_assert(blinded_pk);

  /* Build base64 encoded blinded key. */
  IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
    return;
  }

  /* Version 3 doesn't use the replica number in its descriptor ID computation
   * so we pass negative value so the control port subsystem can ignore it. */
  control_event_hs_descriptor_created(onion_address, base64_blinded_pk, -1);
}

/* Send on the control port the "HS_DESC UPLOAD [...]" event.
 *
 * Using the onion address of the descriptor's service, the HSDir identity
 * digest, the blinded public key of the descriptor as a descriptor ID and the
 * HSDir index for this particular request. None can be NULL. */
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)
{
  char base64_blinded_pk[ED25519_BASE64_LEN + 1];

  tor_assert(onion_address);
  tor_assert(hsdir_id_digest);
  tor_assert(blinded_pk);
  tor_assert(hsdir_index);

  /* Build base64 encoded blinded key. */
  IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
    return;
  }

  control_event_hs_descriptor_upload(onion_address, hsdir_id_digest,
                                     base64_blinded_pk,
                                     hex_str((const char *) hsdir_index,
                                             DIGEST256_LEN));
}

/* Send on the control port the "HS_DESC UPLOADED [...]" event.
 *
 * Using the directory connection identifier and the HSDir identity digest.
 * None can be NULL. */
void
hs_control_desc_event_uploaded(const hs_ident_dir_conn_t *ident,
                               const char *hsdir_id_digest)
{
  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];

  tor_assert(ident);
  tor_assert(hsdir_id_digest);

  hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

  control_event_hs_descriptor_uploaded(hsdir_id_digest, onion_address);
}

/* Send on the control port the "HS_DESC_CONTENT [...]" event.
 *
 * Using the directory connection identifier, the HSDir identity digest and
 * the body of the descriptor (as it was received from the directory). None
 * can be NULL. */
void
hs_control_desc_event_content(const hs_ident_dir_conn_t *ident,
                              const char *hsdir_id_digest,
                              const char *body)
{
  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  char base64_blinded_pk[ED25519_BASE64_LEN + 1];

  tor_assert(ident);
  tor_assert(hsdir_id_digest);

  /* Build onion address and encoded blinded key. */
  IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
                                       &ident->blinded_pk) < 0) {
    return;
  }
  hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

  control_event_hs_descriptor_content(onion_address, base64_blinded_pk,
                                      hsdir_id_digest, body);
}

/* Handle the "HSPOST [...]" command. The body is an encoded descriptor for
 * the given onion_address. The descriptor will be uploaded to each directory
 * in hsdirs_rs. If NULL, the responsible directories for the current time
 * period will be selected.
 *
 * Return -1 on if the descriptor plaintext section is not decodable. Else, 0
 * on success. */
int
hs_control_hspost_command(const char *body, const char *onion_address,
                          const smartlist_t *hsdirs_rs)
{
  int ret = -1;
  ed25519_public_key_t identity_pk;
  hs_desc_plaintext_data_t plaintext;
  smartlist_t *hsdirs = NULL;

  tor_assert(body);
  tor_assert(onion_address);

  /* This can't fail because we require the caller to pass us a valid onion
   * address that has passed hs_address_is_valid(). */
  if (BUG(hs_parse_address(onion_address, &identity_pk, NULL, NULL) < 0)) {
    goto done; // LCOV_EXCL_LINE
  }

  /* Only decode the plaintext part which is what the directory will do to
   * validate before caching. */
  if (hs_desc_decode_plaintext(body, &plaintext) < 0) {
    goto done;
  }

  /* No HSDir(s) given, we'll compute what the current ones should be. */
  if (hsdirs_rs == NULL) {
    hsdirs = smartlist_new();
    hs_get_responsible_hsdirs(&plaintext.blinded_pubkey,
                              hs_get_time_period_num(0),
                              0, /* Always the current descriptor which uses
                                  * the first hsdir index. */
                              0, /* It is for storing on a directory. */
                              hsdirs);
    hsdirs_rs = hsdirs;
  }

  SMARTLIST_FOREACH_BEGIN(hsdirs_rs, const routerstatus_t *, rs) {
    hs_service_upload_desc_to_dir(body, plaintext.version, &identity_pk,
                                  &plaintext.blinded_pubkey, rs);
  } SMARTLIST_FOREACH_END(rs);
  ret = 0;

 done:
  /* We don't have ownership of the objects in this list. */
  smartlist_free(hsdirs);
  return ret;
}