1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2017, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Implement introductions points and rendezvous points.
12 #include "circuitlist.h"
13 #include "circuituse.h"
20 #include "hs_circuitmap.h"
21 #include "hs_intropoint.h"
23 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
24 * setting the circuit's purpose and service pk digest.
27 rend_mid_establish_intro_legacy(or_circuit_t
*circ
, const uint8_t *request
,
30 crypto_pk_t
*pk
= NULL
;
31 char buf
[DIGEST_LEN
+9];
32 char expected_digest
[DIGEST_LEN
];
33 char pk_digest
[DIGEST_LEN
];
36 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
37 int reason
= END_CIRC_REASON_INTERNAL
;
40 "Received a legacy ESTABLISH_INTRO request on circuit %u",
41 (unsigned) circ
->p_circ_id
);
43 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ
)) {
44 reason
= END_CIRC_REASON_TORPROTOCOL
;
48 if (request_len
< 2+DIGEST_LEN
)
50 /* First 2 bytes: length of asn1-encoded key. */
51 asn1len
= ntohs(get_uint16(request
));
53 /* Next asn1len bytes: asn1-encoded key. */
54 if (request_len
< 2+DIGEST_LEN
+asn1len
)
56 pk
= crypto_pk_asn1_decode((char*)(request
+2), asn1len
);
58 reason
= END_CIRC_REASON_TORPROTOCOL
;
59 log_warn(LD_PROTOCOL
, "Couldn't decode public key.");
63 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
64 memcpy(buf
, circ
->rend_circ_nonce
, DIGEST_LEN
);
65 memcpy(buf
+DIGEST_LEN
, "INTRODUCE", 9);
66 if (crypto_digest(expected_digest
, buf
, DIGEST_LEN
+9) < 0) {
67 log_warn(LD_BUG
, "Internal error computing digest.");
70 if (tor_memneq(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
71 log_warn(LD_PROTOCOL
, "Hash of session info was not as expected.");
72 reason
= END_CIRC_REASON_TORPROTOCOL
;
75 /* Rest of body: signature of previous data */
76 if (crypto_pk_public_checksig_digest(pk
,
77 (char*)request
, 2+asn1len
+DIGEST_LEN
,
78 (char*)(request
+2+DIGEST_LEN
+asn1len
),
79 request_len
-(2+DIGEST_LEN
+asn1len
))<0) {
81 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
82 reason
= END_CIRC_REASON_TORPROTOCOL
;
86 /* The request is valid. First, compute the hash of the service's PK.*/
87 if (crypto_pk_get_digest(pk
, pk_digest
)<0) {
88 log_warn(LD_BUG
, "Internal error: couldn't hash public key.");
92 crypto_pk_free(pk
); /* don't need it anymore */
93 pk
= NULL
; /* so we don't free it again if err */
95 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
96 pk_digest
, REND_SERVICE_ID_LEN
);
98 /* Close any other intro circuits with the same pk. */
100 while ((c
= hs_circuitmap_get_intro_circ_v2_relay_side(
101 (const uint8_t *)pk_digest
))) {
102 log_info(LD_REND
, "Replacing old circuit for service %s",
103 safe_str(serviceid
));
104 circuit_mark_for_close(TO_CIRCUIT(c
), END_CIRC_REASON_FINISHED
);
105 /* Now it's marked, and it won't be returned next time. */
108 /* Acknowledge the request. */
109 if (hs_intro_send_intro_established_cell(circ
) < 0) {
110 log_info(LD_GENERAL
, "Couldn't send INTRO_ESTABLISHED cell.");
114 /* Now, set up this circuit. */
115 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_INTRO_POINT
);
116 hs_circuitmap_register_intro_circ_v2_relay_side(circ
, (uint8_t *)pk_digest
);
119 "Established introduction point on circuit %u for service %s",
120 (unsigned) circ
->p_circ_id
, safe_str(serviceid
));
124 log_warn(LD_PROTOCOL
, "Rejecting truncated ESTABLISH_INTRO cell.");
125 reason
= END_CIRC_REASON_TORPROTOCOL
;
127 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
129 if (pk
) crypto_pk_free(pk
);
133 /** Process an INTRODUCE1 cell by finding the corresponding introduction
134 * circuit, and relaying the body of the INTRODUCE1 cell inside an
138 rend_mid_introduce_legacy(or_circuit_t
*circ
, const uint8_t *request
,
141 or_circuit_t
*intro_circ
;
142 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
145 log_info(LD_REND
, "Received an INTRODUCE1 request on circuit %u",
146 (unsigned)circ
->p_circ_id
);
148 /* At this point, we know that the circuit is valid for an INTRODUCE1
149 * because the validation has been made before calling this function. */
150 tor_assert(circ
->base_
.purpose
== CIRCUIT_PURPOSE_OR
);
151 tor_assert(!circ
->base_
.n_chan
);
153 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
154 * obsolete; however, there isn't much reason to do so, and we're going
155 * to revise this protocol anyway.
157 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
158 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
159 log_warn(LD_PROTOCOL
, "Impossibly short INTRODUCE1 cell on circuit %u; "
160 "responding with nack.",
161 (unsigned)circ
->p_circ_id
);
165 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
166 (char*)request
, REND_SERVICE_ID_LEN
);
168 /* The first 20 bytes are all we look at: they have a hash of the service's
170 intro_circ
= hs_circuitmap_get_intro_circ_v2_relay_side(
171 (const uint8_t*)request
);
174 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
175 "responding with nack.",
176 safe_str(serviceid
), (unsigned)circ
->p_circ_id
);
181 "Sending introduction request for service %s "
182 "from circ %u to circ %u",
183 safe_str(serviceid
), (unsigned)circ
->p_circ_id
,
184 (unsigned)intro_circ
->p_circ_id
);
186 /* Great. Now we just relay the cell down the circuit. */
187 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ
),
188 RELAY_COMMAND_INTRODUCE2
,
189 (char*)request
, request_len
, NULL
)) {
191 "Unable to send INTRODUCE2 cell to Tor client.");
192 /* Stop right now, the circuit has been closed. */
195 /* And send an ack down the client's circuit. Empty body means succeeded. */
196 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
197 RELAY_COMMAND_INTRODUCE_ACK
,
199 log_warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
200 /* Stop right now, the circuit has been closed. */
206 /* Send the client a NACK */
208 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
209 RELAY_COMMAND_INTRODUCE_ACK
,
210 nak_body
, 1, NULL
)) {
211 log_warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
216 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
220 rend_mid_establish_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
224 int reason
= END_CIRC_REASON_TORPROTOCOL
;
226 log_info(LD_REND
, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
227 (unsigned)circ
->p_circ_id
);
229 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
) {
230 log_warn(LD_PROTOCOL
,
231 "Tried to establish rendezvous on non-OR circuit with purpose %s",
232 circuit_purpose_to_string(circ
->base_
.purpose
));
236 /* Check if we are configured to accept established rendezvous cells from
237 * client or in other words tor2web clients. */
238 if (channel_is_client(circ
->p_chan
) &&
239 dos_should_refuse_single_hop_client()) {
240 /* Note it down for the heartbeat log purposes. */
241 dos_note_refuse_single_hop_client();
242 /* Silent drop so the client has to time out before moving on. */
246 if (circ
->base_
.n_chan
) {
247 log_warn(LD_PROTOCOL
,
248 "Tried to establish rendezvous on non-edge circuit");
252 if (request_len
!= REND_COOKIE_LEN
) {
253 log_fn(LOG_PROTOCOL_WARN
,
254 LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
258 if (hs_circuitmap_get_rend_circ_relay_side(request
)) {
259 log_warn(LD_PROTOCOL
,
260 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
264 /* Acknowledge the request. */
265 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
266 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
268 log_warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
269 /* Stop right now, the circuit has been closed. */
273 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_POINT_WAITING
);
274 hs_circuitmap_register_rend_circ_relay_side(circ
, request
);
276 base16_encode(hexid
,9,(char*)request
,4);
279 "Established rendezvous point on circuit %u for cookie %s",
280 (unsigned)circ
->p_circ_id
, hexid
);
284 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
288 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
289 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
290 * connecting the two circuits.
293 rend_mid_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
296 const or_options_t
*options
= get_options();
297 or_circuit_t
*rend_circ
;
299 int reason
= END_CIRC_REASON_INTERNAL
;
301 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
303 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
304 (unsigned)circ
->p_circ_id
);
305 reason
= END_CIRC_REASON_TORPROTOCOL
;
309 if (request_len
< REND_COOKIE_LEN
) {
310 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
311 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
312 (int)request_len
, (unsigned)circ
->p_circ_id
);
313 reason
= END_CIRC_REASON_TORPROTOCOL
;
317 base16_encode(hexid
, sizeof(hexid
), (const char*)request
, 4);
320 "Got request for rendezvous from circuit %u to cookie %s.",
321 (unsigned)circ
->p_circ_id
, hexid
);
323 rend_circ
= hs_circuitmap_get_rend_circ_relay_side(request
);
325 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
326 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
328 reason
= END_CIRC_REASON_TORPROTOCOL
;
332 /* Statistics: Mark this circuit as an RP circuit so that we collect
334 if (options
->HiddenServiceStatistics
) {
335 circ
->circuit_carries_hs_traffic_stats
= 1;
338 /* Send the RENDEZVOUS2 cell to the client. */
339 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ
),
340 RELAY_COMMAND_RENDEZVOUS2
,
341 (char*)(request
+REND_COOKIE_LEN
),
342 request_len
-REND_COOKIE_LEN
, NULL
)) {
344 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
345 (unsigned)rend_circ
->p_circ_id
);
346 /* Stop right now, the circuit has been closed. */
350 /* Join the circuits. */
352 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
353 (unsigned)circ
->p_circ_id
, (unsigned)rend_circ
->p_circ_id
, hexid
);
355 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_ESTABLISHED
);
356 circuit_change_purpose(TO_CIRCUIT(rend_circ
),
357 CIRCUIT_PURPOSE_REND_ESTABLISHED
);
358 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ
));
360 rend_circ
->rend_splice
= circ
;
361 circ
->rend_splice
= rend_circ
;
365 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);