In routerlist_assert_ok(), check r2 before taking &(r2->cache_info)
[tor.git] / src / or / rendmid.c
blobd89cdf6bed9db040ce200184b983f560128253ad
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2013, 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 "circuituse.h"
13 #include "config.h"
14 #include "relay.h"
15 #include "rendmid.h"
16 #include "rephist.h"
18 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
19 * setting the circuit's purpose and service pk digest.
21 int
22 rend_mid_establish_intro(or_circuit_t *circ, const uint8_t *request,
23 size_t request_len)
25 crypto_pk_t *pk = NULL;
26 char buf[DIGEST_LEN+9];
27 char expected_digest[DIGEST_LEN];
28 char pk_digest[DIGEST_LEN];
29 size_t asn1len;
30 or_circuit_t *c;
31 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
32 int reason = END_CIRC_REASON_INTERNAL;
34 log_info(LD_REND,
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;
42 goto err;
44 if (request_len < 2+DIGEST_LEN)
45 goto truncated;
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)
51 goto truncated;
52 pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
53 if (!pk) {
54 reason = END_CIRC_REASON_TORPROTOCOL;
55 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
56 goto err;
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.");
64 goto err;
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;
69 goto err;
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) {
77 log_warn(LD_PROTOCOL,
78 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
79 reason = END_CIRC_REASON_TORPROTOCOL;
80 goto err;
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.");
86 goto err;
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. */
96 c = NULL;
97 while ((c = circuit_get_intro_point((const uint8_t *)pk_digest))) {
98 log_info(LD_REND, "Replacing old circuit for service %s",
99 safe_str(serviceid));
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,
107 "", 0, NULL)<0) {
108 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
109 goto err;
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);
116 log_info(LD_REND,
117 "Established introduction point on circuit %u for service %s",
118 (unsigned) circ->p_circ_id, safe_str(serviceid));
120 return 0;
121 truncated:
122 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
123 reason = END_CIRC_REASON_TORPROTOCOL;
124 err:
125 if (pk) crypto_pk_free(pk);
126 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
127 return -1;
130 /** Process an INTRODUCE1 cell by finding the corresponding introduction
131 * circuit, and relaying the body of the INTRODUCE1 cell inside an
132 * INTRODUCE2 cell.
135 rend_mid_introduce(or_circuit_t *circ, const uint8_t *request,
136 size_t request_len)
138 or_circuit_t *intro_circ;
139 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
140 char nak_body[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);
149 goto err;
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);
161 goto err;
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);
169 if (!intro_circ) {
170 log_info(LD_REND,
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);
174 goto err;
177 log_info(LD_REND,
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)) {
187 log_warn(LD_GENERAL,
188 "Unable to send INTRODUCE2 cell to Tor client.");
189 goto err;
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,
194 NULL,0,NULL)) {
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);
197 return -1;
200 return 0;
201 err:
202 /* Send the client an NACK */
203 nak_body[0] = 1;
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.");
208 /* Is this right? */
209 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
211 return -1;
214 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
215 * rendezvous cookie.
218 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
219 size_t request_len)
221 char hexid[9];
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));
231 goto err;
234 if (circ->base_.n_chan) {
235 log_warn(LD_PROTOCOL,
236 "Tried to establish rendezvous on non-edge circuit");
237 goto err;
240 if (request_len != REND_COOKIE_LEN) {
241 log_fn(LOG_PROTOCOL_WARN,
242 LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
243 goto err;
246 if (circuit_get_rendezvous(request)) {
247 log_warn(LD_PROTOCOL,
248 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
249 goto err;
252 /* Acknowledge the request. */
253 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
254 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
255 "", 0, NULL)<0) {
256 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
257 reason = END_CIRC_REASON_INTERNAL;
258 goto err;
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);
266 log_info(LD_REND,
267 "Established rendezvous point on circuit %u for cookie %s",
268 (unsigned)circ->p_circ_id, hexid);
270 return 0;
271 err:
272 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
273 return -1;
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,
282 size_t request_len)
284 or_circuit_t *rend_circ;
285 char hexid[9];
286 int reason = END_CIRC_REASON_INTERNAL;
288 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
289 log_info(LD_REND,
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;
293 goto err;
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;
301 goto err;
304 base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
306 log_info(LD_REND,
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);
311 if (!rend_circ) {
312 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
313 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
314 hexid);
315 reason = END_CIRC_REASON_TORPROTOCOL;
316 goto err;
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)) {
324 log_warn(LD_GENERAL,
325 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
326 (unsigned)rend_circ->p_circ_id);
327 goto err;
330 /* Join the circuits. */
331 log_info(LD_REND,
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;
343 return 0;
344 err:
345 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
346 return -1;