1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Implement introductions points and rendezvous points.
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.
29 rend_mid_establish_intro_legacy(or_circuit_t
*circ
, const uint8_t *request
,
32 crypto_pk_t
*pk
= NULL
;
33 char buf
[DIGEST_LEN
+9];
34 char expected_digest
[DIGEST_LEN
];
35 char pk_digest
[DIGEST_LEN
];
38 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
39 int reason
= END_CIRC_REASON_INTERNAL
;
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
;
50 if (request_len
< 2+DIGEST_LEN
)
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
)
58 pk
= crypto_pk_asn1_decode((char*)(request
+2), asn1len
);
60 reason
= END_CIRC_REASON_TORPROTOCOL
;
61 log_warn(LD_PROTOCOL
, "Couldn't decode public key.");
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.");
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
;
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) {
83 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
84 reason
= END_CIRC_REASON_TORPROTOCOL
;
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.");
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. */
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.");
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
);
121 "Established introduction point on circuit %u for service %s",
122 (unsigned) circ
->p_circ_id
, safe_str(serviceid
));
126 log_warn(LD_PROTOCOL
, "Rejecting truncated ESTABLISH_INTRO cell.");
127 reason
= END_CIRC_REASON_TORPROTOCOL
;
129 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
131 if (pk
) crypto_pk_free(pk
);
135 /** Process an INTRODUCE1 cell by finding the corresponding introduction
136 * circuit, and relaying the body of the INTRODUCE1 cell inside an
140 rend_mid_introduce_legacy(or_circuit_t
*circ
, const uint8_t *request
,
143 or_circuit_t
*intro_circ
;
144 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+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
);
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
173 intro_circ
= hs_circuitmap_get_intro_circ_v2_relay_side(
174 (const uint8_t*)request
);
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
);
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
)) {
194 "Unable to send INTRODUCE2 cell to Tor client.");
195 /* Stop right now, the circuit has been closed. */
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
,
202 log_warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
203 /* Stop right now, the circuit has been closed. */
209 /* Send the client a NACK */
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.");
219 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
223 rend_mid_establish_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
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
));
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. */
249 if (circ
->base_
.n_chan
) {
250 log_warn(LD_PROTOCOL
,
251 "Tried to establish rendezvous on non-edge circuit");
255 if (request_len
!= REND_COOKIE_LEN
) {
256 log_fn(LOG_PROTOCOL_WARN
,
257 LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
261 if (hs_circuitmap_get_rend_circ_relay_side(request
)) {
262 log_warn(LD_PROTOCOL
,
263 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
267 /* Acknowledge the request. */
268 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
269 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
271 log_warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
272 /* Stop right now, the circuit has been closed. */
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);
282 "Established rendezvous point on circuit %u for cookie %s",
283 (unsigned)circ
->p_circ_id
, hexid
);
287 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
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
,
299 const or_options_t
*options
= get_options();
300 or_circuit_t
*rend_circ
;
302 int reason
= END_CIRC_REASON_INTERNAL
;
304 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
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
;
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
;
320 base16_encode(hexid
, sizeof(hexid
), (const char*)request
, 4);
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
);
328 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
329 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
331 reason
= END_CIRC_REASON_TORPROTOCOL
;
335 /* Statistics: Mark this circuit as an RP circuit so that we collect
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
)) {
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. */
353 /* Join the circuits. */
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
;
368 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);