1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2013, 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 could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
153 * obsolete; however, there isn't much reason to do so, and we're going
154 * to revise this protocol anyway.
156 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
157 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
158 log_warn(LD_PROTOCOL
, "Impossibly short INTRODUCE1 cell on circuit %u; "
159 "responding with nack.",
160 (unsigned)circ
->p_circ_id
);
164 base32_encode(serviceid
, REND_SERVICE_ID_LEN_BASE32
+1,
165 (char*)request
, REND_SERVICE_ID_LEN
);
167 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
168 intro_circ
= circuit_get_intro_point((const uint8_t*)request
);
171 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
172 "responding with nack.",
173 safe_str(serviceid
), (unsigned)circ
->p_circ_id
);
178 "Sending introduction request for service %s "
179 "from circ %u to circ %u",
180 safe_str(serviceid
), (unsigned)circ
->p_circ_id
,
181 (unsigned)intro_circ
->p_circ_id
);
183 /* Great. Now we just relay the cell down the circuit. */
184 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ
),
185 RELAY_COMMAND_INTRODUCE2
,
186 (char*)request
, request_len
, NULL
)) {
188 "Unable to send INTRODUCE2 cell to Tor client.");
191 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
192 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
193 RELAY_COMMAND_INTRODUCE_ACK
,
195 log_warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
196 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
202 /* Send the client an NACK */
204 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ
),
205 RELAY_COMMAND_INTRODUCE_ACK
,
206 nak_body
, 1, NULL
)) {
207 log_warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
209 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
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 (circuit_get_rendezvous(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 reason
= END_CIRC_REASON_INTERNAL
;
261 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_POINT_WAITING
);
262 circuit_set_rendezvous_cookie(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 or_circuit_t
*rend_circ
;
286 int reason
= END_CIRC_REASON_INTERNAL
;
288 if (circ
->base_
.purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->base_
.n_chan
) {
290 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
291 (unsigned)circ
->p_circ_id
);
292 reason
= END_CIRC_REASON_TORPROTOCOL
;
296 if (request_len
!= REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
) {
297 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
298 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
299 (int)request_len
, (unsigned)circ
->p_circ_id
);
300 reason
= END_CIRC_REASON_TORPROTOCOL
;
304 base16_encode(hexid
, sizeof(hexid
), (const char*)request
, 4);
307 "Got request for rendezvous from circuit %u to cookie %s.",
308 (unsigned)circ
->p_circ_id
, hexid
);
310 rend_circ
= circuit_get_rendezvous(request
);
312 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
313 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
315 reason
= END_CIRC_REASON_TORPROTOCOL
;
319 /* Send the RENDEZVOUS2 cell to Alice. */
320 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ
),
321 RELAY_COMMAND_RENDEZVOUS2
,
322 (char*)(request
+REND_COOKIE_LEN
),
323 request_len
-REND_COOKIE_LEN
, NULL
)) {
325 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
326 (unsigned)rend_circ
->p_circ_id
);
330 /* Join the circuits. */
332 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
333 (unsigned)circ
->p_circ_id
, (unsigned)rend_circ
->p_circ_id
, hexid
);
335 circuit_change_purpose(TO_CIRCUIT(circ
), CIRCUIT_PURPOSE_REND_ESTABLISHED
);
336 circuit_change_purpose(TO_CIRCUIT(rend_circ
),
337 CIRCUIT_PURPOSE_REND_ESTABLISHED
);
338 circuit_set_rendezvous_cookie(circ
, NULL
);
340 rend_circ
->rend_splice
= circ
;
341 circ
->rend_splice
= rend_circ
;
345 circuit_mark_for_close(TO_CIRCUIT(circ
), reason
);