Bump copyright date to 2019
[tor.git] / src / feature / rend / rendmid.c
blob7c96f236295d451652b9e42e6d742751653a05e7
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 /**
6 * \file rendmid.c
7 * \brief Implement introductions points and rendezvous points.
8 **/
10 #include "core/or/or.h"
11 #include "core/or/channel.h"
12 #include "core/or/circuitlist.h"
13 #include "core/or/circuituse.h"
14 #include "app/config/config.h"
15 #include "lib/crypt_ops/crypto_cipher.h"
16 #include "core/or/dos.h"
17 #include "core/or/relay.h"
18 #include "feature/rend/rendmid.h"
19 #include "feature/stats/rephist.h"
20 #include "feature/hs/hs_circuitmap.h"
21 #include "feature/hs/hs_intropoint.h"
23 #include "core/or/or_circuit_st.h"
25 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
26 * setting the circuit's purpose and service pk digest.
28 int
29 rend_mid_establish_intro_legacy(or_circuit_t *circ, const uint8_t *request,
30 size_t request_len)
32 crypto_pk_t *pk = NULL;
33 char buf[DIGEST_LEN+9];
34 char expected_digest[DIGEST_LEN];
35 char pk_digest[DIGEST_LEN];
36 size_t asn1len;
37 or_circuit_t *c;
38 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
39 int reason = END_CIRC_REASON_INTERNAL;
41 log_info(LD_REND,
42 "Received a legacy ESTABLISH_INTRO request on circuit %u",
43 (unsigned) circ->p_circ_id);
45 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
46 reason = END_CIRC_REASON_TORPROTOCOL;
47 goto err;
50 if (request_len < 2+DIGEST_LEN)
51 goto truncated;
52 /* First 2 bytes: length of asn1-encoded key. */
53 asn1len = ntohs(get_uint16(request));
55 /* Next asn1len bytes: asn1-encoded key. */
56 if (request_len < 2+DIGEST_LEN+asn1len)
57 goto truncated;
58 pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
59 if (!pk) {
60 reason = END_CIRC_REASON_TORPROTOCOL;
61 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
62 goto err;
65 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
66 memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
67 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
68 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
69 log_warn(LD_BUG, "Internal error computing digest.");
70 goto err;
72 if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
73 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
74 reason = END_CIRC_REASON_TORPROTOCOL;
75 goto err;
77 /* Rest of body: signature of previous data */
78 if (crypto_pk_public_checksig_digest(pk,
79 (char*)request, 2+asn1len+DIGEST_LEN,
80 (char*)(request+2+DIGEST_LEN+asn1len),
81 request_len-(2+DIGEST_LEN+asn1len))<0) {
82 log_warn(LD_PROTOCOL,
83 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
84 reason = END_CIRC_REASON_TORPROTOCOL;
85 goto err;
88 /* The request is valid. First, compute the hash of the service's PK.*/
89 if (crypto_pk_get_digest(pk, pk_digest)<0) {
90 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
91 goto err;
94 crypto_pk_free(pk); /* don't need it anymore */
95 pk = NULL; /* so we don't free it again if err */
97 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
98 pk_digest, REND_SERVICE_ID_LEN);
100 /* Close any other intro circuits with the same pk. */
101 c = NULL;
102 while ((c = hs_circuitmap_get_intro_circ_v2_relay_side(
103 (const uint8_t *)pk_digest))) {
104 log_info(LD_REND, "Replacing old circuit for service %s",
105 safe_str(serviceid));
106 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
107 /* Now it's marked, and it won't be returned next time. */
110 /* Acknowledge the request. */
111 if (hs_intro_send_intro_established_cell(circ) < 0) {
112 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
113 goto err_no_close;
116 /* Now, set up this circuit. */
117 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
118 hs_circuitmap_register_intro_circ_v2_relay_side(circ, (uint8_t *)pk_digest);
120 log_info(LD_REND,
121 "Established introduction point on circuit %u for service %s",
122 (unsigned) circ->p_circ_id, safe_str(serviceid));
124 return 0;
125 truncated:
126 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
127 reason = END_CIRC_REASON_TORPROTOCOL;
128 err:
129 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
130 err_no_close:
131 if (pk) crypto_pk_free(pk);
132 return -1;
135 /** Process an INTRODUCE1 cell by finding the corresponding introduction
136 * circuit, and relaying the body of the INTRODUCE1 cell inside an
137 * INTRODUCE2 cell.
140 rend_mid_introduce_legacy(or_circuit_t *circ, const uint8_t *request,
141 size_t request_len)
143 or_circuit_t *intro_circ;
144 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
145 char nak_body[1];
147 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
148 (unsigned)circ->p_circ_id);
150 /* At this point, we know that the circuit is valid for an INTRODUCE1
151 * because the validation has been made before calling this function. */
152 tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_OR);
153 tor_assert(!circ->base_.n_chan);
155 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
156 * obsolete; however, there isn't much reason to do so, and we're going
157 * to revise this protocol anyway.
159 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
160 DH1024_KEY_LEN+CIPHER_KEY_LEN+
161 PKCS1_OAEP_PADDING_OVERHEAD)) {
162 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
163 "responding with nack.",
164 (unsigned)circ->p_circ_id);
165 goto err;
168 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
169 (char*)request, REND_SERVICE_ID_LEN);
171 /* The first 20 bytes are all we look at: they have a hash of the service's
172 * PK. */
173 intro_circ = hs_circuitmap_get_intro_circ_v2_relay_side(
174 (const uint8_t*)request);
175 if (!intro_circ) {
176 log_info(LD_REND,
177 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
178 "responding with nack.",
179 safe_str(serviceid), (unsigned)circ->p_circ_id);
180 goto err;
183 log_info(LD_REND,
184 "Sending introduction request for service %s "
185 "from circ %u to circ %u",
186 safe_str(serviceid), (unsigned)circ->p_circ_id,
187 (unsigned)intro_circ->p_circ_id);
189 /* Great. Now we just relay the cell down the circuit. */
190 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
191 RELAY_COMMAND_INTRODUCE2,
192 (char*)request, request_len, NULL)) {
193 log_warn(LD_GENERAL,
194 "Unable to send INTRODUCE2 cell to Tor client.");
195 /* Stop right now, the circuit has been closed. */
196 return -1;
198 /* And send an ack down the client's circuit. Empty body means succeeded. */
199 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
200 RELAY_COMMAND_INTRODUCE_ACK,
201 NULL,0,NULL)) {
202 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
203 /* Stop right now, the circuit has been closed. */
204 return -1;
207 return 0;
208 err:
209 /* Send the client a NACK */
210 nak_body[0] = 1;
211 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
212 RELAY_COMMAND_INTRODUCE_ACK,
213 nak_body, 1, NULL)) {
214 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
216 return -1;
219 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
220 * rendezvous cookie.
223 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
224 size_t request_len)
226 char hexid[9];
227 int reason = END_CIRC_REASON_TORPROTOCOL;
229 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
230 (unsigned)circ->p_circ_id);
232 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
233 log_warn(LD_PROTOCOL,
234 "Tried to establish rendezvous on non-OR circuit with purpose %s",
235 circuit_purpose_to_string(circ->base_.purpose));
236 goto err;
239 /* Check if we are configured to defend ourselves from clients that
240 * attempt to establish rendezvous points directly to us. */
241 if (channel_is_client(circ->p_chan) &&
242 dos_should_refuse_single_hop_client()) {
243 /* Note it down for the heartbeat log purposes. */
244 dos_note_refuse_single_hop_client();
245 /* Silent drop so the client has to time out before moving on. */
246 return 0;
249 if (circ->base_.n_chan) {
250 log_warn(LD_PROTOCOL,
251 "Tried to establish rendezvous on non-edge circuit");
252 goto err;
255 if (request_len != REND_COOKIE_LEN) {
256 log_fn(LOG_PROTOCOL_WARN,
257 LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
258 goto err;
261 if (hs_circuitmap_get_rend_circ_relay_side(request)) {
262 log_warn(LD_PROTOCOL,
263 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
264 goto err;
267 /* Acknowledge the request. */
268 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
269 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
270 "", 0, NULL)<0) {
271 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
272 /* Stop right now, the circuit has been closed. */
273 return -1;
276 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
277 hs_circuitmap_register_rend_circ_relay_side(circ, request);
279 base16_encode(hexid,9,(char*)request,4);
281 log_info(LD_REND,
282 "Established rendezvous point on circuit %u for cookie %s",
283 (unsigned)circ->p_circ_id, hexid);
285 return 0;
286 err:
287 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
288 return -1;
291 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
292 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
293 * connecting the two circuits.
296 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
297 size_t request_len)
299 const or_options_t *options = get_options();
300 or_circuit_t *rend_circ;
301 char hexid[9];
302 int reason = END_CIRC_REASON_INTERNAL;
304 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
305 log_info(LD_REND,
306 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
307 (unsigned)circ->p_circ_id);
308 reason = END_CIRC_REASON_TORPROTOCOL;
309 goto err;
312 if (request_len < REND_COOKIE_LEN) {
313 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
314 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
315 (int)request_len, (unsigned)circ->p_circ_id);
316 reason = END_CIRC_REASON_TORPROTOCOL;
317 goto err;
320 base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
322 log_info(LD_REND,
323 "Got request for rendezvous from circuit %u to cookie %s.",
324 (unsigned)circ->p_circ_id, hexid);
326 rend_circ = hs_circuitmap_get_rend_circ_relay_side(request);
327 if (!rend_circ) {
328 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
329 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
330 hexid);
331 reason = END_CIRC_REASON_TORPROTOCOL;
332 goto err;
335 /* Statistics: Mark this circuit as an RP circuit so that we collect
336 stats from it. */
337 if (options->HiddenServiceStatistics) {
338 circ->circuit_carries_hs_traffic_stats = 1;
341 /* Send the RENDEZVOUS2 cell to the client. */
342 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
343 RELAY_COMMAND_RENDEZVOUS2,
344 (char*)(request+REND_COOKIE_LEN),
345 request_len-REND_COOKIE_LEN, NULL)) {
346 log_warn(LD_GENERAL,
347 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
348 (unsigned)rend_circ->p_circ_id);
349 /* Stop right now, the circuit has been closed. */
350 return -1;
353 /* Join the circuits. */
354 log_info(LD_REND,
355 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
356 (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
358 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
359 circuit_change_purpose(TO_CIRCUIT(rend_circ),
360 CIRCUIT_PURPOSE_REND_ESTABLISHED);
361 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
363 rend_circ->rend_splice = circ;
364 circ->rend_splice = rend_circ;
366 return 0;
367 err:
368 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
369 return -1;