1 /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
4 const char rendmid_c_id
[] =
9 * \brief Implement introductions points and rendezvous points.
14 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
15 * setting the circuit's purpose and service pk digest.
18 rend_mid_establish_intro(circuit_t
*circ
, const char *request
,
21 crypto_pk_env_t
*pk
= NULL
;
22 char buf
[DIGEST_LEN
+9];
23 char expected_digest
[DIGEST_LEN
];
24 char pk_digest
[DIGEST_LEN
];
27 char serviceid
[REND_SERVICE_ID_LEN
+1];
28 int reason
= END_CIRC_REASON_INTERNAL
;
31 "Received an ESTABLISH_INTRO request on circuit %d", circ
->p_circ_id
);
33 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
34 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
35 "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
36 reason
= END_CIRC_REASON_TORPROTOCOL
;
39 if (request_len
< 2+DIGEST_LEN
)
41 /* First 2 bytes: length of asn1-encoded key. */
42 asn1len
= ntohs(get_uint16(request
));
44 /* Next asn1len bytes: asn1-encoded key. */
45 if (request_len
< 2+DIGEST_LEN
+asn1len
)
47 pk
= crypto_pk_asn1_decode(request
+2, asn1len
);
49 reason
= END_CIRC_REASON_TORPROTOCOL
;
50 warn(LD_PROTOCOL
, "Couldn't decode public key.");
54 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
55 memcpy(buf
, circ
->handshake_digest
, DIGEST_LEN
);
56 memcpy(buf
+DIGEST_LEN
, "INTRODUCE", 9);
57 if (crypto_digest(expected_digest
, buf
, DIGEST_LEN
+9) < 0) {
58 warn(LD_BUG
, "Internal error computing digest.");
61 if (memcmp(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
62 warn(LD_PROTOCOL
, "Hash of session info was not as expected.");
63 reason
= END_CIRC_REASON_TORPROTOCOL
;
66 /* Rest of body: signature of previous data */
67 if (crypto_pk_public_checksig_digest(pk
, request
, 2+asn1len
+DIGEST_LEN
,
68 request
+2+DIGEST_LEN
+asn1len
,
69 request_len
-(2+DIGEST_LEN
+asn1len
))<0) {
71 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
72 reason
= END_CIRC_REASON_TORPROTOCOL
;
76 /* The request is valid. First, compute the hash of Bob's PK.*/
77 if (crypto_pk_get_digest(pk
, pk_digest
)<0) {
78 warn(LD_BUG
, "Internal error: couldn't hash public key.");
82 crypto_free_pk_env(pk
); /* don't need it anymore */
83 pk
= NULL
; /* so we don't free it again if err */
85 base32_encode(serviceid
, REND_SERVICE_ID_LEN
+1, pk_digest
,10);
87 /* Close any other intro circuits with the same pk. */
89 while ((c
= circuit_get_next_by_pk_and_purpose(
90 c
,pk_digest
,CIRCUIT_PURPOSE_INTRO_POINT
))) {
91 info(LD_REND
, "Replacing old circuit %d for service %s",
92 c
->p_circ_id
, safe_str(serviceid
));
93 circuit_mark_for_close(c
, END_CIRC_REASON_REQUESTED
);
96 /* Acknowledge the request. */
97 if (connection_edge_send_command(NULL
,circ
,
98 RELAY_COMMAND_INTRO_ESTABLISHED
,
100 info(LD_GENERAL
, "Couldn't send INTRO_ESTABLISHED cell.");
104 /* Now, set up this circuit. */
105 circ
->purpose
= CIRCUIT_PURPOSE_INTRO_POINT
;
106 memcpy(circ
->rend_pk_digest
, pk_digest
, DIGEST_LEN
);
109 "Established introduction point on circuit %d for service %s",
110 circ
->p_circ_id
, safe_str(serviceid
));
114 warn(LD_PROTOCOL
, "Rejecting truncated ESTABLISH_INTRO cell.");
115 reason
= END_CIRC_REASON_TORPROTOCOL
;
117 if (pk
) crypto_free_pk_env(pk
);
118 circuit_mark_for_close(circ
, reason
);
122 /** Process an INTRODUCE1 cell by finding the corresponding introduction
123 * circuit, and relaying the body of the INTRODUCE1 cell inside an
127 rend_mid_introduce(circuit_t
*circ
, const char *request
, size_t request_len
)
129 circuit_t
*intro_circ
;
130 char serviceid
[REND_SERVICE_ID_LEN
+1];
133 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
134 warn(LD_PROTOCOL
, "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
139 /* change to MAX_HEX_NICKNAME_LEN once 0.0.9.x is obsolete */
140 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
141 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
142 warn(LD_PROTOCOL
, "Impossibly short INTRODUCE1 cell on circuit %d; "
143 "responding with nack.",
148 base32_encode(serviceid
, REND_SERVICE_ID_LEN
+1, request
,10);
150 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
151 intro_circ
= circuit_get_next_by_pk_and_purpose(
152 NULL
, request
, CIRCUIT_PURPOSE_INTRO_POINT
);
155 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
156 "responding with nack.",
157 safe_str(serviceid
), circ
->p_circ_id
);
162 "Sending introduction request for service %s from circ %d to circ %d",
163 safe_str(serviceid
), circ
->p_circ_id
, intro_circ
->p_circ_id
);
165 /* Great. Now we just relay the cell down the circuit. */
166 if (connection_edge_send_command(NULL
, intro_circ
,
167 RELAY_COMMAND_INTRODUCE2
,
168 request
, request_len
, NULL
)) {
170 "Unable to send INTRODUCE2 cell to Tor client.");
173 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
174 if (connection_edge_send_command(NULL
,circ
,RELAY_COMMAND_INTRODUCE_ACK
,
176 warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
177 circuit_mark_for_close(circ
, END_CIRC_REASON_INTERNAL
);
183 /* Send the client an NACK */
185 if (connection_edge_send_command(NULL
,circ
,RELAY_COMMAND_INTRODUCE_ACK
,
186 nak_body
, 1, NULL
)) {
187 warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
189 circuit_mark_for_close(circ
, END_CIRC_REASON_INTERNAL
);
194 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
198 rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
,
202 int reason
= END_CIRC_REASON_TORPROTOCOL
;
204 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
206 "Tried to establish rendezvous on non-OR or non-edge circuit.");
210 if (request_len
!= REND_COOKIE_LEN
) {
211 warn(LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
215 if (circuit_get_rendezvous(request
)) {
216 warn(LD_PROTOCOL
, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
220 /* Acknowledge the request. */
221 if (connection_edge_send_command(NULL
,circ
,
222 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
224 warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
225 reason
= END_CIRC_REASON_INTERNAL
;
229 circ
->purpose
= CIRCUIT_PURPOSE_REND_POINT_WAITING
;
230 memcpy(circ
->rend_cookie
, request
, REND_COOKIE_LEN
);
232 base16_encode(hexid
,9,request
,4);
234 info(LD_REND
, "Established rendezvous point on circuit %d for cookie %s",
235 circ
->p_circ_id
, hexid
);
239 circuit_mark_for_close(circ
, reason
);
243 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
244 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
245 * connecting the two circuits.
248 rend_mid_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
)
250 circuit_t
*rend_circ
;
252 int reason
= END_CIRC_REASON_INTERNAL
;
253 base16_encode(hexid
,9,request
,request_len
<4?request_len
:4);
255 if (request_len
>=4) {
256 info(LD_REND
, "Got request for rendezvous from circuit %d to cookie %s.",
257 circ
->p_circ_id
, hexid
);
260 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
262 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
264 reason
= END_CIRC_REASON_TORPROTOCOL
;
268 if (request_len
!= REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
) {
269 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
270 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
271 (int)request_len
, circ
->p_circ_id
);
272 reason
= END_CIRC_REASON_TORPROTOCOL
;
276 rend_circ
= circuit_get_rendezvous(request
);
278 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
279 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
281 reason
= END_CIRC_REASON_TORPROTOCOL
;
285 /* Send the RENDEZVOUS2 cell to Alice. */
286 if (connection_edge_send_command(NULL
, rend_circ
,
287 RELAY_COMMAND_RENDEZVOUS2
,
288 request
+REND_COOKIE_LEN
,
289 request_len
-REND_COOKIE_LEN
, NULL
)) {
291 "Unable to send RENDEZVOUS2 cell to OP on circuit %d.",
292 rend_circ
->p_circ_id
);
296 /* Join the circuits. */
298 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
299 circ
->p_circ_id
, rend_circ
->p_circ_id
, hexid
);
301 circ
->purpose
= CIRCUIT_PURPOSE_REND_ESTABLISHED
;
302 rend_circ
->purpose
= CIRCUIT_PURPOSE_REND_ESTABLISHED
;
303 memset(circ
->rend_cookie
, 0, REND_COOKIE_LEN
);
305 rend_circ
->rend_splice
= circ
;
306 circ
->rend_splice
= rend_circ
;
310 circuit_mark_for_close(circ
, reason
);