1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2015, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Implement introductions points and rendezvous points.
11 #include "circuitlist.h"
12 #include "circuituse.h"
18 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
19 * setting the circuit's purpose and service pk digest.
22 rend_mid_establish_intro(or_circuit_t
*circ
, const uint8_t *request
,
25 crypto_pk_t
*pk
= NULL
;
26 char buf
[DIGEST_LEN
+9];
27 char expected_digest
[DIGEST_LEN
];
28 char pk_digest
[DIGEST_LEN
];
31 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
32 int reason
= END_CIRC_REASON_INTERNAL
;
35 "Received an ESTABLISH_INTRO request on circuit %u",
36 (unsigned) circ
->p_circ_id
);
38 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
39 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
40 "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
41 reason
= END_CIRC_REASON_TORPROTOCOL
;
44 if (request_len
< 2+DIGEST_LEN
)
46 /* First 2 bytes: length of asn1-encoded key. */
47 asn1len
= ntohs(get_uint16(request
));
49 /* Next asn1len bytes: asn1-encoded key. */
50 if (request_len
< 2+DIGEST_LEN
+asn1len
)
52 pk
= crypto_pk_asn1_decode((char*)(request
+2), asn1len
);
54 reason
= END_CIRC_REASON_TORPROTOCOL
;
55 log_warn(LD_PROTOCOL
, "Couldn't decode public key.");
59 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
60 memcpy(buf
, circ
->rend_circ_nonce
, DIGEST_LEN
);
61 memcpy(buf
+DIGEST_LEN
, "INTRODUCE", 9);
62 if (crypto_digest(expected_digest
, buf
, DIGEST_LEN
+9) < 0) {
63 log_warn(LD_BUG
, "Internal error computing digest.");
66 if (tor_memneq(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
67 log_warn(LD_PROTOCOL
, "Hash of session info was not as expected.");
68 reason
= END_CIRC_REASON_TORPROTOCOL
;
71 /* Rest of body: signature of previous data */
72 note_crypto_pk_op(REND_MID
);
73 if (crypto_pk_public_checksig_digest(pk
,
74 (char*)request
, 2+asn1len
+DIGEST_LEN
,
75 (char*)(request
+2+DIGEST_LEN
+asn1len
),
76 request_len
-(2+DIGEST_LEN
+asn1len
))<0) {
78 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
79 reason
= END_CIRC_REASON_TORPROTOCOL
;
83 /* The request is valid. First, compute the hash of Bob's PK.*/
84 if (crypto_pk_get_digest(pk
, pk_digest
)<0) {
85 log_warn(LD_BUG
, "Internal error: couldn't hash public key.");
89 crypto_pk_free(pk
); /* don't need it anymore */
90 pk
= NULL
; /* so we don't free it again if err */
92 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
93 pk_digest
, REND_SERVICE_ID_LEN
);
95 /* Close any other intro circuits with the same pk. */
97 while ((c
= circuit_get_intro_point((const uint8_t *)pk_digest
))) {
98 log_info(LD_REND
, "Replacing old circuit for service %s",
100 circuit_mark_for_close(TO_CIRCUIT(c
), END_CIRC_REASON_FINISHED
);
101 /* Now it's marked, and it won't be returned next time. */
104 /* Acknowledge the request. */
105 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ
),
106 RELAY_COMMAND_INTRO_ESTABLISHED
,
108 log_info(LD_GENERAL
, "Couldn't send INTRO_ESTABLISHED cell.");
112 /* Now, set up this circuit. */
113 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_INTRO_POINT
);
114 circuit_set_intro_point_digest(circ
, (uint8_t *)pk_digest
);
117 "Established introduction point on circuit %u for service %s",
118 (unsigned) circ
->p_circ_id
, safe_str(serviceid
));
122 log_warn(LD_PROTOCOL
, "Rejecting truncated ESTABLISH_INTRO cell.");
123 reason
= END_CIRC_REASON_TORPROTOCOL
;
125 if (pk
) crypto_pk_free(pk
);
126 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
130 /** Process an INTRODUCE1 cell by finding the corresponding introduction
131 * circuit, and relaying the body of the INTRODUCE1 cell inside an
135 rend_mid_introduce(or_circuit_t
*circ
, const uint8_t *request
,
138 or_circuit_t
*intro_circ
;
139 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
142 log_info(LD_REND
, "Received an INTRODUCE1 request on circuit %u",
143 (unsigned)circ
->p_circ_id
);
145 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
146 log_warn(LD_PROTOCOL
,
147 "Rejecting INTRODUCE1 on non-OR or non-edge circuit %u.",
148 (unsigned)circ
->p_circ_id
);
152 /* We have already done an introduction on this circuit but we just
153 received a request for another one. We block it since this might
154 be an attempt to DoS a hidden service (#15515). */
155 if (circ
->already_received_introduce1
) {
156 log_fn(LOG_PROTOCOL_WARN
, LD_REND
,
157 "Blocking multiple introductions on the same circuit. "
158 "Someone might be trying to attack a hidden service through "
160 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
164 circ
->already_received_introduce1
= 1;
166 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
167 * obsolete; however, there isn't much reason to do so, and we're going
168 * to revise this protocol anyway.
170 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
171 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
172 log_warn(LD_PROTOCOL
, "Impossibly short INTRODUCE1 cell on circuit %u; "
173 "responding with nack.",
174 (unsigned)circ
->p_circ_id
);
178 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
179 (char*)request
, REND_SERVICE_ID_LEN
);
181 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
182 intro_circ
= circuit_get_intro_point((const uint8_t*)request
);
185 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
186 "responding with nack.",
187 safe_str(serviceid
), (unsigned)circ
->p_circ_id
);
192 "Sending introduction request for service %s "
193 "from circ %u to circ %u",
194 safe_str(serviceid
), (unsigned)circ
->p_circ_id
,
195 (unsigned)intro_circ
->p_circ_id
);
197 /* Great. Now we just relay the cell down the circuit. */
198 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ
),
199 RELAY_COMMAND_INTRODUCE2
,
200 (char*)request
, request_len
, NULL
)) {
202 "Unable to send INTRODUCE2 cell to Tor client.");
205 /* And send an ack down Alice's circuit. Empty body means succeeded. */
206 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
207 RELAY_COMMAND_INTRODUCE_ACK
,
209 log_warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
210 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
216 /* Send the client a NACK */
218 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
219 RELAY_COMMAND_INTRODUCE_ACK
,
220 nak_body
, 1, NULL
)) {
221 log_warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
223 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
228 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
232 rend_mid_establish_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
236 int reason
= END_CIRC_REASON_TORPROTOCOL
;
238 log_info(LD_REND
, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
239 (unsigned)circ
->p_circ_id
);
241 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
) {
242 log_warn(LD_PROTOCOL
,
243 "Tried to establish rendezvous on non-OR circuit with purpose %s",
244 circuit_purpose_to_string(circ
->base_
.purpose
));
248 if (circ
->base_
.n_chan
) {
249 log_warn(LD_PROTOCOL
,
250 "Tried to establish rendezvous on non-edge circuit");
254 if (request_len
!= REND_COOKIE_LEN
) {
255 log_fn(LOG_PROTOCOL_WARN
,
256 LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
260 if (circuit_get_rendezvous(request
)) {
261 log_warn(LD_PROTOCOL
,
262 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
266 /* Acknowledge the request. */
267 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
268 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
270 log_warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
271 reason
= END_CIRC_REASON_INTERNAL
;
275 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_POINT_WAITING
);
276 circuit_set_rendezvous_cookie(circ
, request
);
278 base16_encode(hexid
,9,(char*)request
,4);
281 "Established rendezvous point on circuit %u for cookie %s",
282 (unsigned)circ
->p_circ_id
, hexid
);
286 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
290 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
291 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
292 * connecting the two circuits.
295 rend_mid_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
298 const or_options_t
*options
= get_options();
299 or_circuit_t
*rend_circ
;
301 int reason
= END_CIRC_REASON_INTERNAL
;
303 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
305 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
306 (unsigned)circ
->p_circ_id
);
307 reason
= END_CIRC_REASON_TORPROTOCOL
;
311 if (request_len
!= REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
) {
312 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
313 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
314 (int)request_len
, (unsigned)circ
->p_circ_id
);
315 reason
= END_CIRC_REASON_TORPROTOCOL
;
319 base16_encode(hexid
, sizeof(hexid
), (const char*)request
, 4);
322 "Got request for rendezvous from circuit %u to cookie %s.",
323 (unsigned)circ
->p_circ_id
, hexid
);
325 rend_circ
= circuit_get_rendezvous(request
);
327 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
328 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
330 reason
= END_CIRC_REASON_TORPROTOCOL
;
334 /* Statistics: Mark this circuit as an RP circuit so that we collect
336 if (options
->HiddenServiceStatistics
) {
337 circ
->circuit_carries_hs_traffic_stats
= 1;
340 /* Send the RENDEZVOUS2 cell to Alice. */
341 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ
),
342 RELAY_COMMAND_RENDEZVOUS2
,
343 (char*)(request
+REND_COOKIE_LEN
),
344 request_len
-REND_COOKIE_LEN
, NULL
)) {
346 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
347 (unsigned)rend_circ
->p_circ_id
);
351 /* Join the circuits. */
353 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
354 (unsigned)circ
->p_circ_id
, (unsigned)rend_circ
->p_circ_id
, hexid
);
356 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_ESTABLISHED
);
357 circuit_change_purpose(TO_CIRCUIT(rend_circ
),
358 CIRCUIT_PURPOSE_REND_ESTABLISHED
);
359 circuit_set_rendezvous_cookie(circ
, NULL
);
361 rend_circ
->rend_splice
= circ
;
362 circ
->rend_splice
= rend_circ
;
366 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);