1 /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
4 const char rendmid_c_id
[] = "$Id$";
8 * \brief Implement introductions points and rendezvous points.
11 #define NEW_LOG_INTERFACE
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
, size_t request_len
)
20 crypto_pk_env_t
*pk
= NULL
;
21 char buf
[DIGEST_LEN
+9];
22 char expected_digest
[DIGEST_LEN
];
23 char pk_digest
[DIGEST_LEN
];
26 char serviceid
[REND_SERVICE_ID_LEN
+1];
29 "Received an ESTABLISH_INTRO request on circuit %d", circ
->p_circ_id
);
31 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
32 warn(LD_PROTOCOL
, "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
35 if (request_len
< 2+DIGEST_LEN
)
37 /* First 2 bytes: length of asn1-encoded key. */
38 asn1len
= ntohs(get_uint16(request
));
40 /* Next asn1len bytes: asn1-encoded key. */
41 if (request_len
< 2+DIGEST_LEN
+asn1len
)
43 pk
= crypto_pk_asn1_decode(request
+2, asn1len
);
45 warn(LD_PROTOCOL
, "Couldn't decode public key.");
49 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
50 memcpy(buf
, circ
->handshake_digest
, DIGEST_LEN
);
51 memcpy(buf
+DIGEST_LEN
, "INTRODUCE", 9);
52 if (crypto_digest(expected_digest
, buf
, DIGEST_LEN
+9) < 0) {
53 warn(LD_BUG
, "Internal error computing digest.");
56 if (memcmp(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
57 warn(LD_PROTOCOL
, "Hash of session info was not as expected.");
60 /* Rest of body: signature of previous data */
61 if (crypto_pk_public_checksig_digest(pk
, request
, 2+asn1len
+DIGEST_LEN
,
62 request
+2+DIGEST_LEN
+asn1len
,
63 request_len
-(2+DIGEST_LEN
+asn1len
))<0) {
64 warn(LD_PROTOCOL
, "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
68 /* The request is valid. First, compute the hash of Bob's PK.*/
69 if (crypto_pk_get_digest(pk
, pk_digest
)<0) {
70 warn(LD_BUG
, "Internal error: couldn't hash public key.");
74 crypto_free_pk_env(pk
); /* don't need it anymore */
75 pk
= NULL
; /* so we don't free it again if err */
77 base32_encode(serviceid
, REND_SERVICE_ID_LEN
+1, pk_digest
,10);
79 /* Close any other intro circuits with the same pk. */
81 while ((c
= circuit_get_next_by_pk_and_purpose(
82 c
,pk_digest
,CIRCUIT_PURPOSE_INTRO_POINT
))) {
83 info(LD_REND
, "Replacing old circuit %d for service %s",
84 c
->p_circ_id
, safe_str(serviceid
));
85 circuit_mark_for_close(c
);
88 /* Acknowledge the request. */
89 if (connection_edge_send_command(NULL
,circ
,
90 RELAY_COMMAND_INTRO_ESTABLISHED
,
92 info(LD_GENERAL
, "Couldn't send INTRO_ESTABLISHED cell.");
96 /* Now, set up this circuit. */
97 circ
->purpose
= CIRCUIT_PURPOSE_INTRO_POINT
;
98 memcpy(circ
->rend_pk_digest
, pk_digest
, DIGEST_LEN
);
101 "Established introduction point on circuit %d for service %s",
102 circ
->p_circ_id
, safe_str(serviceid
));
106 warn(LD_PROTOCOL
, "Rejecting truncated ESTABLISH_INTRO cell.");
108 if (pk
) crypto_free_pk_env(pk
);
109 circuit_mark_for_close(circ
);
113 /** Process an INTRODUCE1 cell by finding the corresponding introduction
114 * circuit, and relaying the body of the INTRODUCE1 cell inside an
118 rend_mid_introduce(circuit_t
*circ
, const char *request
, size_t request_len
)
120 circuit_t
*intro_circ
;
121 char serviceid
[REND_SERVICE_ID_LEN
+1];
124 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
125 warn(LD_PROTOCOL
, "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
130 /* change to MAX_HEX_NICKNAME_LEN once 0.0.9.x is obsolete */
131 if (request_len
< (DIGEST_LEN
+(MAX_NICKNAME_LEN
+1)+REND_COOKIE_LEN
+
132 DH_KEY_LEN
+CIPHER_KEY_LEN
+PKCS1_OAEP_PADDING_OVERHEAD
)) {
134 "Impossibly short INTRODUCE1 cell on circuit %d; responding with nack.",
139 base32_encode(serviceid
, REND_SERVICE_ID_LEN
+1, request
,10);
141 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
142 intro_circ
= circuit_get_next_by_pk_and_purpose(
143 NULL
, request
, CIRCUIT_PURPOSE_INTRO_POINT
);
146 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; responding with nack.",
147 safe_str(serviceid
), circ
->p_circ_id
);
152 "Sending introduction request for service %s from circ %d to circ %d",
153 safe_str(serviceid
), circ
->p_circ_id
, intro_circ
->p_circ_id
);
155 /* Great. Now we just relay the cell down the circuit. */
156 if (connection_edge_send_command(NULL
, intro_circ
,
157 RELAY_COMMAND_INTRODUCE2
,
158 request
, request_len
, NULL
)) {
160 "Unable to send INTRODUCE2 cell to Tor client.");
163 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
164 if (connection_edge_send_command(NULL
,circ
,RELAY_COMMAND_INTRODUCE_ACK
,
166 warn(LD_GENERAL
, "Unable to send INTRODUCE_ACK cell to Tor client.");
167 circuit_mark_for_close(circ
);
173 /* Send the client an NACK */
175 if (connection_edge_send_command(NULL
,circ
,RELAY_COMMAND_INTRODUCE_ACK
,
176 nak_body
, 1, NULL
)) {
177 warn(LD_GENERAL
, "Unable to send NAK to Tor client.");
178 circuit_mark_for_close(circ
); /* Is this right? */
183 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
187 rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
)
191 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
192 warn(LD_PROTOCOL
, "Tried to establish rendezvous on non-OR or non-edge circuit.");
196 if (request_len
!= REND_COOKIE_LEN
) {
197 warn(LD_PROTOCOL
, "Invalid length on ESTABLISH_RENDEZVOUS.");
201 if (circuit_get_rendezvous(request
)) {
202 warn(LD_PROTOCOL
, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
206 /* Acknowledge the request. */
207 if (connection_edge_send_command(NULL
,circ
,
208 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
210 warn(LD_PROTOCOL
, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
214 circ
->purpose
= CIRCUIT_PURPOSE_REND_POINT_WAITING
;
215 memcpy(circ
->rend_cookie
, request
, REND_COOKIE_LEN
);
217 base16_encode(hexid
,9,request
,4);
219 info(LD_REND
, "Established rendezvous point on circuit %d for cookie %s",
220 circ
->p_circ_id
, hexid
);
224 circuit_mark_for_close(circ
);
228 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
229 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
230 * connecting the two circuits.
233 rend_mid_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
)
235 circuit_t
*rend_circ
;
238 base16_encode(hexid
,9,request
,request_len
<4?request_len
:4);
240 if (request_len
>=4) {
241 info(LD_REND
, "Got request for rendezvous from circuit %d to cookie %s.",
242 circ
->p_circ_id
, hexid
);
245 if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
|| circ
->n_conn
) {
247 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
252 if (request_len
!= REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
) {
254 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
255 (int)request_len
, circ
->p_circ_id
);
259 rend_circ
= circuit_get_rendezvous(request
);
262 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
267 /* Send the RENDEZVOUS2 cell to Alice. */
268 if (connection_edge_send_command(NULL
, rend_circ
,
269 RELAY_COMMAND_RENDEZVOUS2
,
270 request
+REND_COOKIE_LEN
,
271 request_len
-REND_COOKIE_LEN
, NULL
)) {
273 "Unable to send RENDEZVOUS2 cell to OP on circuit %d.",
274 rend_circ
->p_circ_id
);
278 /* Join the circuits. */
280 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
281 circ
->p_circ_id
, rend_circ
->p_circ_id
, hexid
);
283 circ
->purpose
= CIRCUIT_PURPOSE_REND_ESTABLISHED
;
284 rend_circ
->purpose
= CIRCUIT_PURPOSE_REND_ESTABLISHED
;
285 memset(circ
->rend_cookie
, 0, REND_COOKIE_LEN
);
287 rend_circ
->rend_splice
= circ
;
288 circ
->rend_splice
= rend_circ
;
292 circuit_mark_for_close(circ
);