r13702@catbus: nickm | 2007-07-12 11:35:51 -0400
[tor.git] / src / or / rendmid.c
blobb5dcb4dbf6a415769f7b5e3167ccde2cf4843a11
1 /* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
4 const char rendmid_c_id[] =
5 "$Id$";
7 /**
8 * \file rendmid.c
9 * \brief Implement introductions points and rendezvous points.
10 **/
12 #include "or.h"
14 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
15 * setting the circuit's purpose and service pk digest.
17 int
18 rend_mid_establish_intro(or_circuit_t *circ, const char *request,
19 size_t request_len)
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];
25 size_t asn1len;
26 or_circuit_t *c;
27 char serviceid[REND_SERVICE_ID_LEN+1];
28 int reason = END_CIRC_REASON_INTERNAL;
30 log_info(LD_REND,
31 "Received an ESTABLISH_INTRO request on circuit %d",
32 circ->p_circ_id);
34 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
35 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
36 "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
37 reason = END_CIRC_REASON_TORPROTOCOL;
38 goto err;
40 if (request_len < 2+DIGEST_LEN)
41 goto truncated;
42 /* First 2 bytes: length of asn1-encoded key. */
43 asn1len = ntohs(get_uint16(request));
45 /* Next asn1len bytes: asn1-encoded key. */
46 if (request_len < 2+DIGEST_LEN+asn1len)
47 goto truncated;
48 pk = crypto_pk_asn1_decode(request+2, asn1len);
49 if (!pk) {
50 reason = END_CIRC_REASON_TORPROTOCOL;
51 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
52 goto err;
55 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
56 memcpy(buf, circ->handshake_digest, DIGEST_LEN);
57 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
58 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
59 log_warn(LD_BUG, "Internal error computing digest.");
60 goto err;
62 if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
63 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
64 reason = END_CIRC_REASON_TORPROTOCOL;
65 goto err;
67 /* Rest of body: signature of previous data */
68 note_crypto_pk_op(REND_MID);
69 if (crypto_pk_public_checksig_digest(pk, request, 2+asn1len+DIGEST_LEN,
70 request+2+DIGEST_LEN+asn1len,
71 request_len-(2+DIGEST_LEN+asn1len))<0) {
72 log_warn(LD_PROTOCOL,
73 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
74 reason = END_CIRC_REASON_TORPROTOCOL;
75 goto err;
78 /* The request is valid. First, compute the hash of Bob's PK.*/
79 if (crypto_pk_get_digest(pk, pk_digest)<0) {
80 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
81 goto err;
84 crypto_free_pk_env(pk); /* don't need it anymore */
85 pk = NULL; /* so we don't free it again if err */
87 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, pk_digest,10);
89 /* Close any other intro circuits with the same pk. */
90 c = NULL;
91 while ((c = circuit_get_intro_point(pk_digest))) {
92 log_info(LD_REND, "Replacing old circuit for service %s",
93 safe_str(serviceid));
94 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
95 /* Now it's marked, and it won't be returned next time. */
98 /* Acknowledge the request. */
99 if (connection_edge_send_command(NULL,TO_CIRCUIT(circ),
100 RELAY_COMMAND_INTRO_ESTABLISHED,
101 "", 0, NULL)<0) {
102 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
103 goto err;
106 /* Now, set up this circuit. */
107 circ->_base.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
108 memcpy(circ->rend_token, pk_digest, DIGEST_LEN);
110 log_info(LD_REND,
111 "Established introduction point on circuit %d for service %s",
112 circ->p_circ_id, safe_str(serviceid));
114 return 0;
115 truncated:
116 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
117 reason = END_CIRC_REASON_TORPROTOCOL;
118 err:
119 if (pk) crypto_free_pk_env(pk);
120 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
121 return -1;
124 /** Process an INTRODUCE1 cell by finding the corresponding introduction
125 * circuit, and relaying the body of the INTRODUCE1 cell inside an
126 * INTRODUCE2 cell.
129 rend_mid_introduce(or_circuit_t *circ, const char *request, size_t request_len)
131 or_circuit_t *intro_circ;
132 char serviceid[REND_SERVICE_ID_LEN+1];
133 char nak_body[1];
135 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
136 log_warn(LD_PROTOCOL,
137 "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
138 circ->p_circ_id);
139 goto err;
142 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
143 * obsolete; however, there isn't much reason to do so, and we're going
144 * to revise this protocol anyway.
146 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
147 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
148 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
149 "responding with nack.",
150 circ->p_circ_id);
151 goto err;
154 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
156 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
157 intro_circ = circuit_get_intro_point(request);
158 if (!intro_circ) {
159 log_info(LD_REND,
160 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
161 "responding with nack.",
162 safe_str(serviceid), circ->p_circ_id);
163 goto err;
166 log_info(LD_REND,
167 "Sending introduction request for service %s "
168 "from circ %d to circ %d",
169 safe_str(serviceid), circ->p_circ_id,
170 intro_circ->p_circ_id);
172 /* Great. Now we just relay the cell down the circuit. */
173 if (connection_edge_send_command(NULL, TO_CIRCUIT(intro_circ),
174 RELAY_COMMAND_INTRODUCE2,
175 request, request_len, NULL)) {
176 log_warn(LD_GENERAL,
177 "Unable to send INTRODUCE2 cell to Tor client.");
178 goto err;
180 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
181 if (connection_edge_send_command(NULL,TO_CIRCUIT(circ),
182 RELAY_COMMAND_INTRODUCE_ACK,
183 NULL,0,NULL)) {
184 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
185 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
186 return -1;
189 return 0;
190 err:
191 /* Send the client an NACK */
192 nak_body[0] = 1;
193 if (connection_edge_send_command(NULL,TO_CIRCUIT(circ),
194 RELAY_COMMAND_INTRODUCE_ACK,
195 nak_body, 1, NULL)) {
196 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
197 /* Is this right? */
198 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
200 return -1;
203 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
204 * rendezvous cookie.
207 rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
208 size_t request_len)
210 char hexid[9];
211 int reason = END_CIRC_REASON_TORPROTOCOL;
213 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
214 log_warn(LD_PROTOCOL,
215 "Tried to establish rendezvous on non-OR or non-edge circuit.");
216 goto err;
219 if (request_len != REND_COOKIE_LEN) {
220 log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
221 goto err;
224 if (circuit_get_rendezvous(request)) {
225 log_warn(LD_PROTOCOL,
226 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
227 goto err;
230 /* Acknowledge the request. */
231 if (connection_edge_send_command(NULL,TO_CIRCUIT(circ),
232 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
233 "", 0, NULL)<0) {
234 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
235 reason = END_CIRC_REASON_INTERNAL;
236 goto err;
239 circ->_base.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
240 memcpy(circ->rend_token, request, REND_COOKIE_LEN);
242 base16_encode(hexid,9,request,4);
244 log_info(LD_REND,
245 "Established rendezvous point on circuit %d for cookie %s",
246 circ->p_circ_id, hexid);
248 return 0;
249 err:
250 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
251 return -1;
254 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
255 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
256 * connecting the two circuits.
259 rend_mid_rendezvous(or_circuit_t *circ, const char *request,
260 size_t request_len)
262 or_circuit_t *rend_circ;
263 char hexid[9];
264 int reason = END_CIRC_REASON_INTERNAL;
265 base16_encode(hexid,9,request,request_len<4?request_len:4);
267 if (request_len>=4) {
268 log_info(LD_REND,
269 "Got request for rendezvous from circuit %d to cookie %s.",
270 circ->p_circ_id, hexid);
273 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
274 log_info(LD_REND,
275 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
276 circ->p_circ_id);
277 reason = END_CIRC_REASON_TORPROTOCOL;
278 goto err;
281 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
282 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
283 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
284 (int)request_len, circ->p_circ_id);
285 reason = END_CIRC_REASON_TORPROTOCOL;
286 goto err;
289 rend_circ = circuit_get_rendezvous(request);
290 if (!rend_circ) {
291 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
292 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
293 hexid);
294 reason = END_CIRC_REASON_TORPROTOCOL;
295 goto err;
298 /* Send the RENDEZVOUS2 cell to Alice. */
299 if (connection_edge_send_command(NULL, TO_CIRCUIT(rend_circ),
300 RELAY_COMMAND_RENDEZVOUS2,
301 request+REND_COOKIE_LEN,
302 request_len-REND_COOKIE_LEN, NULL)) {
303 log_warn(LD_GENERAL,
304 "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
305 rend_circ->p_circ_id);
306 goto err;
309 /* Join the circuits. */
310 log_info(LD_REND,
311 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
312 circ->p_circ_id, rend_circ->p_circ_id, hexid);
314 circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
315 rend_circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
316 memset(circ->rend_token, 0, REND_COOKIE_LEN);
318 rend_circ->rend_splice = circ;
319 circ->rend_splice = rend_circ;
321 return 0;
322 err:
323 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
324 return -1;