Send control port events for timeouts.
[tor.git] / src / or / rendmid.c
blobd392f8e53ab08349c8dddd028f6ea3491af0bddd
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2010, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 /**
6 * \file rendmid.c
7 * \brief Implement introductions points and rendezvous points.
8 **/
10 #include "or.h"
11 #include "circuitlist.h"
12 #include "config.h"
13 #include "relay.h"
14 #include "rendmid.h"
15 #include "rephist.h"
17 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
18 * setting the circuit's purpose and service pk digest.
20 int
21 rend_mid_establish_intro(or_circuit_t *circ, const char *request,
22 size_t request_len)
24 crypto_pk_env_t *pk = NULL;
25 char buf[DIGEST_LEN+9];
26 char expected_digest[DIGEST_LEN];
27 char pk_digest[DIGEST_LEN];
28 size_t asn1len;
29 or_circuit_t *c;
30 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
31 int reason = END_CIRC_REASON_INTERNAL;
33 log_info(LD_REND,
34 "Received an ESTABLISH_INTRO request on circuit %d",
35 circ->p_circ_id);
37 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
38 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
39 "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
40 reason = END_CIRC_REASON_TORPROTOCOL;
41 goto err;
43 if (request_len < 2+DIGEST_LEN)
44 goto truncated;
45 /* First 2 bytes: length of asn1-encoded key. */
46 asn1len = ntohs(get_uint16(request));
48 /* Next asn1len bytes: asn1-encoded key. */
49 if (request_len < 2+DIGEST_LEN+asn1len)
50 goto truncated;
51 pk = crypto_pk_asn1_decode(request+2, asn1len);
52 if (!pk) {
53 reason = END_CIRC_REASON_TORPROTOCOL;
54 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
55 goto err;
58 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
59 memcpy(buf, circ->handshake_digest, DIGEST_LEN);
60 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
61 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
62 log_warn(LD_BUG, "Internal error computing digest.");
63 goto err;
65 if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
66 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
67 reason = END_CIRC_REASON_TORPROTOCOL;
68 goto err;
70 /* Rest of body: signature of previous data */
71 note_crypto_pk_op(REND_MID);
72 if (crypto_pk_public_checksig_digest(pk, request, 2+asn1len+DIGEST_LEN,
73 request+2+DIGEST_LEN+asn1len,
74 request_len-(2+DIGEST_LEN+asn1len))<0) {
75 log_warn(LD_PROTOCOL,
76 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
77 reason = END_CIRC_REASON_TORPROTOCOL;
78 goto err;
81 /* The request is valid. First, compute the hash of Bob's PK.*/
82 if (crypto_pk_get_digest(pk, pk_digest)<0) {
83 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
84 goto err;
87 crypto_free_pk_env(pk); /* don't need it anymore */
88 pk = NULL; /* so we don't free it again if err */
90 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
91 pk_digest, REND_SERVICE_ID_LEN);
93 /* Close any other intro circuits with the same pk. */
94 c = NULL;
95 while ((c = circuit_get_intro_point(pk_digest))) {
96 log_info(LD_REND, "Replacing old circuit for service %s",
97 safe_str(serviceid));
98 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
99 /* Now it's marked, and it won't be returned next time. */
102 /* Acknowledge the request. */
103 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
104 RELAY_COMMAND_INTRO_ESTABLISHED,
105 "", 0, NULL)<0) {
106 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
107 goto err;
110 /* Now, set up this circuit. */
111 circ->_base.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
112 memcpy(circ->rend_token, pk_digest, DIGEST_LEN);
114 log_info(LD_REND,
115 "Established introduction point on circuit %d for service %s",
116 circ->p_circ_id, safe_str(serviceid));
118 return 0;
119 truncated:
120 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
121 reason = END_CIRC_REASON_TORPROTOCOL;
122 err:
123 if (pk) crypto_free_pk_env(pk);
124 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
125 return -1;
128 /** Process an INTRODUCE1 cell by finding the corresponding introduction
129 * circuit, and relaying the body of the INTRODUCE1 cell inside an
130 * INTRODUCE2 cell.
133 rend_mid_introduce(or_circuit_t *circ, const char *request, size_t request_len)
135 or_circuit_t *intro_circ;
136 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
137 char nak_body[1];
139 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %d",
140 circ->p_circ_id);
142 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
143 log_warn(LD_PROTOCOL,
144 "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
145 circ->p_circ_id);
146 goto err;
149 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
150 * obsolete; however, there isn't much reason to do so, and we're going
151 * to revise this protocol anyway.
153 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
154 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
155 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
156 "responding with nack.",
157 circ->p_circ_id);
158 goto err;
161 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
162 request, REND_SERVICE_ID_LEN);
164 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
165 intro_circ = circuit_get_intro_point(request);
166 if (!intro_circ) {
167 log_info(LD_REND,
168 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
169 "responding with nack.",
170 safe_str(serviceid), circ->p_circ_id);
171 goto err;
174 log_info(LD_REND,
175 "Sending introduction request for service %s "
176 "from circ %d to circ %d",
177 safe_str(serviceid), circ->p_circ_id,
178 intro_circ->p_circ_id);
180 /* Great. Now we just relay the cell down the circuit. */
181 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
182 RELAY_COMMAND_INTRODUCE2,
183 request, request_len, NULL)) {
184 log_warn(LD_GENERAL,
185 "Unable to send INTRODUCE2 cell to Tor client.");
186 goto err;
188 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
189 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
190 RELAY_COMMAND_INTRODUCE_ACK,
191 NULL,0,NULL)) {
192 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
193 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
194 return -1;
197 return 0;
198 err:
199 /* Send the client an NACK */
200 nak_body[0] = 1;
201 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
202 RELAY_COMMAND_INTRODUCE_ACK,
203 nak_body, 1, NULL)) {
204 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
205 /* Is this right? */
206 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
208 return -1;
211 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
212 * rendezvous cookie.
215 rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
216 size_t request_len)
218 char hexid[9];
219 int reason = END_CIRC_REASON_TORPROTOCOL;
221 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %d",
222 circ->p_circ_id);
224 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
225 log_warn(LD_PROTOCOL,
226 "Tried to establish rendezvous on non-OR or non-edge circuit.");
227 goto err;
230 if (request_len != REND_COOKIE_LEN) {
231 log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
232 goto err;
235 if (circuit_get_rendezvous(request)) {
236 log_warn(LD_PROTOCOL,
237 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
238 goto err;
241 /* Acknowledge the request. */
242 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
243 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
244 "", 0, NULL)<0) {
245 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
246 reason = END_CIRC_REASON_INTERNAL;
247 goto err;
250 circ->_base.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
251 memcpy(circ->rend_token, request, REND_COOKIE_LEN);
253 base16_encode(hexid,9,request,4);
255 log_info(LD_REND,
256 "Established rendezvous point on circuit %d for cookie %s",
257 circ->p_circ_id, hexid);
259 return 0;
260 err:
261 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
262 return -1;
265 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
266 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
267 * connecting the two circuits.
270 rend_mid_rendezvous(or_circuit_t *circ, const char *request,
271 size_t request_len)
273 or_circuit_t *rend_circ;
274 char hexid[9];
275 int reason = END_CIRC_REASON_INTERNAL;
276 base16_encode(hexid,9,request,request_len<4?request_len:4);
278 if (request_len>=4) {
279 log_info(LD_REND,
280 "Got request for rendezvous from circuit %d to cookie %s.",
281 circ->p_circ_id, hexid);
284 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
285 log_info(LD_REND,
286 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
287 circ->p_circ_id);
288 reason = END_CIRC_REASON_TORPROTOCOL;
289 goto err;
292 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
293 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
294 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
295 (int)request_len, circ->p_circ_id);
296 reason = END_CIRC_REASON_TORPROTOCOL;
297 goto err;
300 rend_circ = circuit_get_rendezvous(request);
301 if (!rend_circ) {
302 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
303 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
304 hexid);
305 reason = END_CIRC_REASON_TORPROTOCOL;
306 goto err;
309 /* Send the RENDEZVOUS2 cell to Alice. */
310 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
311 RELAY_COMMAND_RENDEZVOUS2,
312 request+REND_COOKIE_LEN,
313 request_len-REND_COOKIE_LEN, NULL)) {
314 log_warn(LD_GENERAL,
315 "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
316 rend_circ->p_circ_id);
317 goto err;
320 /* Join the circuits. */
321 log_info(LD_REND,
322 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
323 circ->p_circ_id, rend_circ->p_circ_id, hexid);
325 circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
326 rend_circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
327 memset(circ->rend_token, 0, REND_COOKIE_LEN);
329 rend_circ->rend_splice = circ;
330 circ->rend_splice = rend_circ;
332 return 0;
333 err:
334 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
335 return -1;