Stupid cut-and-paste bug.
[tor.git] / src / or / rendmid.c
blob73b6c8064d867b6d214f88c185cf18dc4c9ae393
1 /* Copyright 2004-2005 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(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 circuit_t *c;
27 char serviceid[REND_SERVICE_ID_LEN+1];
28 int reason = END_CIRC_REASON_INTERNAL;
30 info(LD_REND,
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;
37 goto err;
39 if (request_len < 2+DIGEST_LEN)
40 goto truncated;
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)
46 goto truncated;
47 pk = crypto_pk_asn1_decode(request+2, asn1len);
48 if (!pk) {
49 reason = END_CIRC_REASON_TORPROTOCOL;
50 warn(LD_PROTOCOL, "Couldn't decode public key.");
51 goto err;
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.");
59 goto err;
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;
64 goto err;
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) {
70 warn(LD_PROTOCOL,
71 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
72 reason = END_CIRC_REASON_TORPROTOCOL;
73 goto err;
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.");
79 goto err;
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. */
88 c = NULL;
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,
99 "", 0, NULL)<0) {
100 info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
101 goto err;
104 /* Now, set up this circuit. */
105 circ->purpose = CIRCUIT_PURPOSE_INTRO_POINT;
106 memcpy(circ->rend_pk_digest, pk_digest, DIGEST_LEN);
108 info(LD_REND,
109 "Established introduction point on circuit %d for service %s",
110 circ->p_circ_id, safe_str(serviceid));
112 return 0;
113 truncated:
114 warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
115 reason = END_CIRC_REASON_TORPROTOCOL;
116 err:
117 if (pk) crypto_free_pk_env(pk);
118 circuit_mark_for_close(circ, reason);
119 return -1;
122 /** Process an INTRODUCE1 cell by finding the corresponding introduction
123 * circuit, and relaying the body of the INTRODUCE1 cell inside an
124 * INTRODUCE2 cell.
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];
131 char nak_body[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.",
135 circ->p_circ_id);
136 goto err;
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.",
144 circ->p_circ_id);
145 goto err;
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);
153 if (!intro_circ) {
154 info(LD_REND,
155 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
156 "responding with nack.",
157 safe_str(serviceid), circ->p_circ_id);
158 goto err;
161 info(LD_REND,
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)) {
169 warn(LD_GENERAL,
170 "Unable to send INTRODUCE2 cell to Tor client.");
171 goto err;
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,
175 NULL,0,NULL)) {
176 warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
177 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
178 return -1;
181 return 0;
182 err:
183 /* Send the client an NACK */
184 nak_body[0] = 1;
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.");
188 /* Is this right? */
189 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
191 return -1;
194 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
195 * rendezvous cookie.
198 rend_mid_establish_rendezvous(circuit_t *circ, const char *request,
199 size_t request_len)
201 char hexid[9];
202 int reason = END_CIRC_REASON_TORPROTOCOL;
204 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
205 warn(LD_PROTOCOL,
206 "Tried to establish rendezvous on non-OR or non-edge circuit.");
207 goto err;
210 if (request_len != REND_COOKIE_LEN) {
211 warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
212 goto err;
215 if (circuit_get_rendezvous(request)) {
216 warn(LD_PROTOCOL, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
217 goto err;
220 /* Acknowledge the request. */
221 if (connection_edge_send_command(NULL,circ,
222 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
223 "", 0, NULL)<0) {
224 warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
225 reason = END_CIRC_REASON_INTERNAL;
226 goto err;
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);
237 return 0;
238 err:
239 circuit_mark_for_close(circ, reason);
240 return -1;
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;
251 char hexid[9];
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) {
261 info(LD_REND,
262 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
263 circ->p_circ_id);
264 reason = END_CIRC_REASON_TORPROTOCOL;
265 goto err;
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;
273 goto err;
276 rend_circ = circuit_get_rendezvous(request);
277 if (!rend_circ) {
278 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
279 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
280 hexid);
281 reason = END_CIRC_REASON_TORPROTOCOL;
282 goto err;
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)) {
290 warn(LD_GENERAL,
291 "Unable to send RENDEZVOUS2 cell to OP on circuit %d.",
292 rend_circ->p_circ_id);
293 goto err;
296 /* Join the circuits. */
297 info(LD_REND,
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;
308 return 0;
309 err:
310 circuit_mark_for_close(circ, reason);
311 return -1;