Stop trying to generate test scripts via autoconf substitution.
[tor.git] / src / or / rendmid.c
blob2451acb514198f8c20e2789225d11862f72889a0
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2015, 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 have already done an introduction on this circuit but we just
153 received a request for another one. We block it since this might
154 be an attempt to DoS a hidden service (#15515). */
155 if (circ->already_received_introduce1) {
156 log_fn(LOG_PROTOCOL_WARN, LD_REND,
157 "Blocking multiple introductions on the same circuit. "
158 "Someone might be trying to attack a hidden service through "
159 "this relay.");
160 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
161 return -1;
164 circ->already_received_introduce1 = 1;
166 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
167 * obsolete; however, there isn't much reason to do so, and we're going
168 * to revise this protocol anyway.
170 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
171 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
172 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
173 "responding with nack.",
174 (unsigned)circ->p_circ_id);
175 goto err;
178 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
179 (char*)request, REND_SERVICE_ID_LEN);
181 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
182 intro_circ = circuit_get_intro_point((const uint8_t*)request);
183 if (!intro_circ) {
184 log_info(LD_REND,
185 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
186 "responding with nack.",
187 safe_str(serviceid), (unsigned)circ->p_circ_id);
188 goto err;
191 log_info(LD_REND,
192 "Sending introduction request for service %s "
193 "from circ %u to circ %u",
194 safe_str(serviceid), (unsigned)circ->p_circ_id,
195 (unsigned)intro_circ->p_circ_id);
197 /* Great. Now we just relay the cell down the circuit. */
198 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
199 RELAY_COMMAND_INTRODUCE2,
200 (char*)request, request_len, NULL)) {
201 log_warn(LD_GENERAL,
202 "Unable to send INTRODUCE2 cell to Tor client.");
203 goto err;
205 /* And send an ack down Alice's circuit. Empty body means succeeded. */
206 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
207 RELAY_COMMAND_INTRODUCE_ACK,
208 NULL,0,NULL)) {
209 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
210 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
211 return -1;
214 return 0;
215 err:
216 /* Send the client a NACK */
217 nak_body[0] = 1;
218 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
219 RELAY_COMMAND_INTRODUCE_ACK,
220 nak_body, 1, NULL)) {
221 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
222 /* Is this right? */
223 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
225 return -1;
228 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
229 * rendezvous cookie.
232 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
233 size_t request_len)
235 char hexid[9];
236 int reason = END_CIRC_REASON_TORPROTOCOL;
238 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
239 (unsigned)circ->p_circ_id);
241 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
242 log_warn(LD_PROTOCOL,
243 "Tried to establish rendezvous on non-OR circuit with purpose %s",
244 circuit_purpose_to_string(circ->base_.purpose));
245 goto err;
248 if (circ->base_.n_chan) {
249 log_warn(LD_PROTOCOL,
250 "Tried to establish rendezvous on non-edge circuit");
251 goto err;
254 if (request_len != REND_COOKIE_LEN) {
255 log_fn(LOG_PROTOCOL_WARN,
256 LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
257 goto err;
260 if (circuit_get_rendezvous(request)) {
261 log_warn(LD_PROTOCOL,
262 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
263 goto err;
266 /* Acknowledge the request. */
267 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
268 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
269 "", 0, NULL)<0) {
270 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
271 reason = END_CIRC_REASON_INTERNAL;
272 goto err;
275 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
276 circuit_set_rendezvous_cookie(circ, request);
278 base16_encode(hexid,9,(char*)request,4);
280 log_info(LD_REND,
281 "Established rendezvous point on circuit %u for cookie %s",
282 (unsigned)circ->p_circ_id, hexid);
284 return 0;
285 err:
286 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
287 return -1;
290 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
291 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
292 * connecting the two circuits.
295 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
296 size_t request_len)
298 const or_options_t *options = get_options();
299 or_circuit_t *rend_circ;
300 char hexid[9];
301 int reason = END_CIRC_REASON_INTERNAL;
303 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
304 log_info(LD_REND,
305 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
306 (unsigned)circ->p_circ_id);
307 reason = END_CIRC_REASON_TORPROTOCOL;
308 goto err;
311 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
312 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
313 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
314 (int)request_len, (unsigned)circ->p_circ_id);
315 reason = END_CIRC_REASON_TORPROTOCOL;
316 goto err;
319 base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
321 log_info(LD_REND,
322 "Got request for rendezvous from circuit %u to cookie %s.",
323 (unsigned)circ->p_circ_id, hexid);
325 rend_circ = circuit_get_rendezvous(request);
326 if (!rend_circ) {
327 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
328 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
329 hexid);
330 reason = END_CIRC_REASON_TORPROTOCOL;
331 goto err;
334 /* Statistics: Mark this circuit as an RP circuit so that we collect
335 stats from it. */
336 if (options->HiddenServiceStatistics) {
337 circ->circuit_carries_hs_traffic_stats = 1;
340 /* Send the RENDEZVOUS2 cell to Alice. */
341 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
342 RELAY_COMMAND_RENDEZVOUS2,
343 (char*)(request+REND_COOKIE_LEN),
344 request_len-REND_COOKIE_LEN, NULL)) {
345 log_warn(LD_GENERAL,
346 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
347 (unsigned)rend_circ->p_circ_id);
348 goto err;
351 /* Join the circuits. */
352 log_info(LD_REND,
353 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
354 (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
356 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
357 circuit_change_purpose(TO_CIRCUIT(rend_circ),
358 CIRCUIT_PURPOSE_REND_ESTABLISHED);
359 circuit_set_rendezvous_cookie(circ, NULL);
361 rend_circ->rend_splice = circ;
362 circ->rend_splice = rend_circ;
364 return 0;
365 err:
366 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
367 return -1;