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.
11 #include "circuitlist.h"
12 #include "circuituse.h"
18 #include "hs_circuitmap.h"
19 #include "hs_intropoint.h"
21 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
22 * setting the circuit's purpose and service pk digest.
25 rend_mid_establish_intro_legacy(or_circuit_t
*circ
, const uint8_t *request
,
28 crypto_pk_t
*pk
= NULL
;
29 char buf
[DIGEST_LEN
+9];
30 char expected_digest
[DIGEST_LEN
];
31 char pk_digest
[DIGEST_LEN
];
34 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
35 int reason
= END_CIRC_REASON_INTERNAL
;
38 "Received a legacy ESTABLISH_INTRO request on circuit %u",
39 (unsigned) circ
->p_circ_id
);
41 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ
)) {
42 reason
= END_CIRC_REASON_TORPROTOCOL
;
46 if (request_len
< 2+DIGEST_LEN
)
48 /* First 2 bytes: length of asn1-encoded key. */
49 asn1len
= ntohs(get_uint16(request
));
51 /* Next asn1len bytes: asn1-encoded key. */
52 if (request_len
< 2+DIGEST_LEN
+asn1len
)
54 pk
= crypto_pk_asn1_decode((char*)(request
+2), asn1len
);
56 reason
= END_CIRC_REASON_TORPROTOCOL
;
57 log_warn(LD_PROTOCOL
, "Couldn't decode public key.");
61 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
62 memcpy(buf
, circ
->rend_circ_nonce
, DIGEST_LEN
);
63 memcpy(buf
+DIGEST_LEN
, "INTRODUCE", 9);
64 if (crypto_digest(expected_digest
, buf
, DIGEST_LEN
+9) < 0) {
65 log_warn(LD_BUG
, "Internal error computing digest.");
68 if (tor_memneq(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
69 log_warn(LD_PROTOCOL
, "Hash of session info was not as expected.");
70 reason
= END_CIRC_REASON_TORPROTOCOL
;
73 /* Rest of body: signature of previous data */
74 if (crypto_pk_public_checksig_digest(pk
,
75 (char*)request
, 2+asn1len
+DIGEST_LEN
,
76 (char*)(request
+2+DIGEST_LEN
+asn1len
),
77 request_len
-(2+DIGEST_LEN
+asn1len
))<0) {
79 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
80 reason
= END_CIRC_REASON_TORPROTOCOL
;
84 /* The request is valid. First, compute the hash of the service's PK.*/
85 if (crypto_pk_get_digest(pk
, pk_digest
)<0) {
86 log_warn(LD_BUG
, "Internal error: couldn't hash public key.");
90 crypto_pk_free(pk
); /* don't need it anymore */
91 pk
= NULL
; /* so we don't free it again if err */
93 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
94 pk_digest
, REND_SERVICE_ID_LEN
);
96 /* Close any other intro circuits with the same pk. */
98 while ((c
= hs_circuitmap_get_intro_circ_v2_relay_side(
99 (const uint8_t *)pk_digest
))) {
100 log_info(LD_REND
, "Replacing old circuit for service %s",
101 safe_str(serviceid
));
102 circuit_mark_for_close(TO_CIRCUIT(c
), END_CIRC_REASON_FINISHED
);
103 /* Now it's marked, and it won't be returned next time. */
106 /* Acknowledge the request. */
107 if (hs_intro_send_intro_established_cell(circ
) < 0) {
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 hs_circuitmap_register_intro_circ_v2_relay_side(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 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
127 if (pk
) crypto_pk_free(pk
);
131 /** Process an INTRODUCE1 cell by finding the corresponding introduction
132 * circuit, and relaying the body of the INTRODUCE1 cell inside an
136 rend_mid_introduce_legacy(or_circuit_t
*circ
, const uint8_t *request
,
139 or_circuit_t
*intro_circ
;
140 char serviceid
[REND_SERVICE_ID_LEN_BASE32
+1];
143 log_info(LD_REND
, "Received an INTRODUCE1 request on circuit %u",
144 (unsigned)circ
->p_circ_id
);
146 /* At this point, we know that the circuit is valid for an INTRODUCE1
147 * because the validation has been made before calling this function. */
148 tor_assert(circ
->base_
.purpose
== CIRCUIT_PURPOSE_OR
);
149 tor_assert(!circ
->base_
.n_chan
);
151 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
152 * obsolete; however, there isn't much reason to do so, and we're going
153 * to revise this protocol anyway.
155 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
156 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
157 log_warn(LD_PROTOCOL
, "Impossibly short INTRODUCE1 cell on circuit %u; "
158 "responding with nack.",
159 (unsigned)circ
->p_circ_id
);
163 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
164 (char*)request
, REND_SERVICE_ID_LEN
);
166 /* The first 20 bytes are all we look at: they have a hash of the service's
168 intro_circ
= hs_circuitmap_get_intro_circ_v2_relay_side(
169 (const uint8_t*)request
);
172 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
173 "responding with nack.",
174 safe_str(serviceid
), (unsigned)circ
->p_circ_id
);
179 "Sending introduction request for service %s "
180 "from circ %u to circ %u",
181 safe_str(serviceid
), (unsigned)circ
->p_circ_id
,
182 (unsigned)intro_circ
->p_circ_id
);
184 /* Great. Now we just relay the cell down the circuit. */
185 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ
),
186 RELAY_COMMAND_INTRODUCE2
,
187 (char*)request
, request_len
, NULL
)) {
189 "Unable to send INTRODUCE2 cell to Tor client.");
190 /* Stop right now, the circuit has been closed. */
193 /* And send an ack down the client's circuit. Empty body means succeeded. */
194 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
195 RELAY_COMMAND_INTRODUCE_ACK
,
197 log_warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
198 /* Stop right now, the circuit has been closed. */
204 /* Send the client a NACK */
206 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
207 RELAY_COMMAND_INTRODUCE_ACK
,
208 nak_body
, 1, NULL
)) {
209 log_warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
214 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
218 rend_mid_establish_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
222 int reason
= END_CIRC_REASON_TORPROTOCOL
;
224 log_info(LD_REND
, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
225 (unsigned)circ
->p_circ_id
);
227 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
) {
228 log_warn(LD_PROTOCOL
,
229 "Tried to establish rendezvous on non-OR circuit with purpose %s",
230 circuit_purpose_to_string(circ
->base_
.purpose
));
234 if (circ
->base_
.n_chan
) {
235 log_warn(LD_PROTOCOL
,
236 "Tried to establish rendezvous on non-edge circuit");
240 if (request_len
!= REND_COOKIE_LEN
) {
241 log_fn(LOG_PROTOCOL_WARN
,
242 LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
246 if (hs_circuitmap_get_rend_circ_relay_side(request
)) {
247 log_warn(LD_PROTOCOL
,
248 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
252 /* Acknowledge the request. */
253 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
254 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
256 log_warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
257 /* Stop right now, the circuit has been closed. */
261 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_POINT_WAITING
);
262 hs_circuitmap_register_rend_circ_relay_side(circ
, request
);
264 base16_encode(hexid
,9,(char*)request
,4);
267 "Established rendezvous point on circuit %u for cookie %s",
268 (unsigned)circ
->p_circ_id
, hexid
);
272 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);
276 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
277 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
278 * connecting the two circuits.
281 rend_mid_rendezvous(or_circuit_t
*circ
, const uint8_t *request
,
284 const or_options_t
*options
= get_options();
285 or_circuit_t
*rend_circ
;
287 int reason
= END_CIRC_REASON_INTERNAL
;
289 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
291 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
292 (unsigned)circ
->p_circ_id
);
293 reason
= END_CIRC_REASON_TORPROTOCOL
;
297 if (request_len
< REND_COOKIE_LEN
) {
298 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
299 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
300 (int)request_len
, (unsigned)circ
->p_circ_id
);
301 reason
= END_CIRC_REASON_TORPROTOCOL
;
305 base16_encode(hexid
, sizeof(hexid
), (const char*)request
, 4);
308 "Got request for rendezvous from circuit %u to cookie %s.",
309 (unsigned)circ
->p_circ_id
, hexid
);
311 rend_circ
= hs_circuitmap_get_rend_circ_relay_side(request
);
313 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
314 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
316 reason
= END_CIRC_REASON_TORPROTOCOL
;
320 /* Statistics: Mark this circuit as an RP circuit so that we collect
322 if (options
->HiddenServiceStatistics
) {
323 circ
->circuit_carries_hs_traffic_stats
= 1;
326 /* Send the RENDEZVOUS2 cell to the client. */
327 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ
),
328 RELAY_COMMAND_RENDEZVOUS2
,
329 (char*)(request
+REND_COOKIE_LEN
),
330 request_len
-REND_COOKIE_LEN
, NULL
)) {
332 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
333 (unsigned)rend_circ
->p_circ_id
);
334 /* Stop right now, the circuit has been closed. */
338 /* Join the circuits. */
340 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
341 (unsigned)circ
->p_circ_id
, (unsigned)rend_circ
->p_circ_id
, hexid
);
343 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_ESTABLISHED
);
344 circuit_change_purpose(TO_CIRCUIT(rend_circ
),
345 CIRCUIT_PURPOSE_REND_ESTABLISHED
);
346 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ
));
348 rend_circ
->rend_splice
= circ
;
349 circ
->rend_splice
= rend_circ
;
353 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);