Fix valgrind error when marking a descriptor as never-downloadable.
[tor/rransom.git] / src / or / rendmid.c
blob57f5b6daedd15f554b9a0e666e79dabef0258bd5
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2009, 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"
12 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
13 * setting the circuit's purpose and service pk digest.
15 int
16 rend_mid_establish_intro(or_circuit_t *circ, const char *request,
17 size_t request_len)
19 crypto_pk_env_t *pk = NULL;
20 char buf[DIGEST_LEN+9];
21 char expected_digest[DIGEST_LEN];
22 char pk_digest[DIGEST_LEN];
23 size_t asn1len;
24 or_circuit_t *c;
25 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
26 int reason = END_CIRC_REASON_INTERNAL;
28 log_info(LD_REND,
29 "Received an ESTABLISH_INTRO request on circuit %d",
30 circ->p_circ_id);
32 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
33 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
34 "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
35 reason = END_CIRC_REASON_TORPROTOCOL;
36 goto err;
38 if (request_len < 2+DIGEST_LEN)
39 goto truncated;
40 /* First 2 bytes: length of asn1-encoded key. */
41 asn1len = ntohs(get_uint16(request));
43 /* Next asn1len bytes: asn1-encoded key. */
44 if (request_len < 2+DIGEST_LEN+asn1len)
45 goto truncated;
46 pk = crypto_pk_asn1_decode(request+2, asn1len);
47 if (!pk) {
48 reason = END_CIRC_REASON_TORPROTOCOL;
49 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
50 goto err;
53 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
54 memcpy(buf, circ->handshake_digest, DIGEST_LEN);
55 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
56 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
57 log_warn(LD_BUG, "Internal error computing digest.");
58 goto err;
60 if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
61 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
62 reason = END_CIRC_REASON_TORPROTOCOL;
63 goto err;
65 /* Rest of body: signature of previous data */
66 note_crypto_pk_op(REND_MID);
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 log_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 log_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_BASE32+1,
86 pk_digest, REND_SERVICE_ID_LEN);
88 /* Close any other intro circuits with the same pk. */
89 c = NULL;
90 while ((c = circuit_get_intro_point(pk_digest))) {
91 log_info(LD_REND, "Replacing old circuit for service %s",
92 safe_str(serviceid));
93 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
94 /* Now it's marked, and it won't be returned next time. */
97 /* Acknowledge the request. */
98 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
99 RELAY_COMMAND_INTRO_ESTABLISHED,
100 "", 0, NULL)<0) {
101 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
102 goto err;
105 /* Now, set up this circuit. */
106 circ->_base.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
107 memcpy(circ->rend_token, pk_digest, DIGEST_LEN);
109 log_info(LD_REND,
110 "Established introduction point on circuit %d for service %s",
111 circ->p_circ_id, safe_str(serviceid));
113 return 0;
114 truncated:
115 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
116 reason = END_CIRC_REASON_TORPROTOCOL;
117 err:
118 if (pk) crypto_free_pk_env(pk);
119 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
120 return -1;
123 /** Process an INTRODUCE1 cell by finding the corresponding introduction
124 * circuit, and relaying the body of the INTRODUCE1 cell inside an
125 * INTRODUCE2 cell.
128 rend_mid_introduce(or_circuit_t *circ, const char *request, size_t request_len)
130 or_circuit_t *intro_circ;
131 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
132 char nak_body[1];
134 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %d",
135 circ->p_circ_id);
137 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
138 log_warn(LD_PROTOCOL,
139 "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
140 circ->p_circ_id);
141 goto err;
144 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
145 * obsolete; however, there isn't much reason to do so, and we're going
146 * to revise this protocol anyway.
148 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
149 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
150 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
151 "responding with nack.",
152 circ->p_circ_id);
153 goto err;
156 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
157 request, REND_SERVICE_ID_LEN);
159 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
160 intro_circ = circuit_get_intro_point(request);
161 if (!intro_circ) {
162 log_info(LD_REND,
163 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
164 "responding with nack.",
165 safe_str(serviceid), circ->p_circ_id);
166 goto err;
169 log_info(LD_REND,
170 "Sending introduction request for service %s "
171 "from circ %d to circ %d",
172 safe_str(serviceid), circ->p_circ_id,
173 intro_circ->p_circ_id);
175 /* Great. Now we just relay the cell down the circuit. */
176 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
177 RELAY_COMMAND_INTRODUCE2,
178 request, request_len, NULL)) {
179 log_warn(LD_GENERAL,
180 "Unable to send INTRODUCE2 cell to Tor client.");
181 goto err;
183 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
184 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
185 RELAY_COMMAND_INTRODUCE_ACK,
186 NULL,0,NULL)) {
187 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
188 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
189 return -1;
192 return 0;
193 err:
194 /* Send the client an NACK */
195 nak_body[0] = 1;
196 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
197 RELAY_COMMAND_INTRODUCE_ACK,
198 nak_body, 1, NULL)) {
199 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
200 /* Is this right? */
201 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
203 return -1;
206 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
207 * rendezvous cookie.
210 rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
211 size_t request_len)
213 char hexid[9];
214 int reason = END_CIRC_REASON_TORPROTOCOL;
216 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %d",
217 circ->p_circ_id);
219 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
220 log_warn(LD_PROTOCOL,
221 "Tried to establish rendezvous on non-OR or non-edge circuit.");
222 goto err;
225 if (request_len != REND_COOKIE_LEN) {
226 log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
227 goto err;
230 if (circuit_get_rendezvous(request)) {
231 log_warn(LD_PROTOCOL,
232 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
233 goto err;
236 /* Acknowledge the request. */
237 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
238 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
239 "", 0, NULL)<0) {
240 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
241 reason = END_CIRC_REASON_INTERNAL;
242 goto err;
245 circ->_base.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
246 memcpy(circ->rend_token, request, REND_COOKIE_LEN);
248 base16_encode(hexid,9,request,4);
250 log_info(LD_REND,
251 "Established rendezvous point on circuit %d for cookie %s",
252 circ->p_circ_id, hexid);
254 return 0;
255 err:
256 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
257 return -1;
260 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
261 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
262 * connecting the two circuits.
265 rend_mid_rendezvous(or_circuit_t *circ, const char *request,
266 size_t request_len)
268 or_circuit_t *rend_circ;
269 char hexid[9];
270 int reason = END_CIRC_REASON_INTERNAL;
271 base16_encode(hexid,9,request,request_len<4?request_len:4);
273 if (request_len>=4) {
274 log_info(LD_REND,
275 "Got request for rendezvous from circuit %d to cookie %s.",
276 circ->p_circ_id, hexid);
279 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
280 log_info(LD_REND,
281 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
282 circ->p_circ_id);
283 reason = END_CIRC_REASON_TORPROTOCOL;
284 goto err;
287 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
288 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
289 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
290 (int)request_len, circ->p_circ_id);
291 reason = END_CIRC_REASON_TORPROTOCOL;
292 goto err;
295 rend_circ = circuit_get_rendezvous(request);
296 if (!rend_circ) {
297 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
298 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
299 hexid);
300 reason = END_CIRC_REASON_TORPROTOCOL;
301 goto err;
304 /* Send the RENDEZVOUS2 cell to Alice. */
305 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
306 RELAY_COMMAND_RENDEZVOUS2,
307 request+REND_COOKIE_LEN,
308 request_len-REND_COOKIE_LEN, NULL)) {
309 log_warn(LD_GENERAL,
310 "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
311 rend_circ->p_circ_id);
312 goto err;
315 /* Join the circuits. */
316 log_info(LD_REND,
317 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
318 circ->p_circ_id, rend_circ->p_circ_id, hexid);
320 circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
321 rend_circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
322 memset(circ->rend_token, 0, REND_COOKIE_LEN);
324 rend_circ->rend_splice = circ;
325 circ->rend_splice = rend_circ;
327 return 0;
328 err:
329 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
330 return -1;