Cache control file
[tor/appveyor.git] / src / or / rendmid.c
blobc4a34ca62c6509bdca31030d0509a07e72245376
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2017, 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 "channel.h"
12 #include "circuitlist.h"
13 #include "circuituse.h"
14 #include "config.h"
15 #include "crypto.h"
16 #include "dos.h"
17 #include "relay.h"
18 #include "rendmid.h"
19 #include "rephist.h"
20 #include "hs_circuitmap.h"
21 #include "hs_intropoint.h"
23 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
24 * setting the circuit's purpose and service pk digest.
26 int
27 rend_mid_establish_intro_legacy(or_circuit_t *circ, const uint8_t *request,
28 size_t request_len)
30 crypto_pk_t *pk = NULL;
31 char buf[DIGEST_LEN+9];
32 char expected_digest[DIGEST_LEN];
33 char pk_digest[DIGEST_LEN];
34 size_t asn1len;
35 or_circuit_t *c;
36 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
37 int reason = END_CIRC_REASON_INTERNAL;
39 log_info(LD_REND,
40 "Received a legacy ESTABLISH_INTRO request on circuit %u",
41 (unsigned) circ->p_circ_id);
43 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
44 reason = END_CIRC_REASON_TORPROTOCOL;
45 goto err;
48 if (request_len < 2+DIGEST_LEN)
49 goto truncated;
50 /* First 2 bytes: length of asn1-encoded key. */
51 asn1len = ntohs(get_uint16(request));
53 /* Next asn1len bytes: asn1-encoded key. */
54 if (request_len < 2+DIGEST_LEN+asn1len)
55 goto truncated;
56 pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
57 if (!pk) {
58 reason = END_CIRC_REASON_TORPROTOCOL;
59 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
60 goto err;
63 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
64 memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
65 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
66 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
67 log_warn(LD_BUG, "Internal error computing digest.");
68 goto err;
70 if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
71 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
72 reason = END_CIRC_REASON_TORPROTOCOL;
73 goto err;
75 /* Rest of body: signature of previous data */
76 if (crypto_pk_public_checksig_digest(pk,
77 (char*)request, 2+asn1len+DIGEST_LEN,
78 (char*)(request+2+DIGEST_LEN+asn1len),
79 request_len-(2+DIGEST_LEN+asn1len))<0) {
80 log_warn(LD_PROTOCOL,
81 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
82 reason = END_CIRC_REASON_TORPROTOCOL;
83 goto err;
86 /* The request is valid. First, compute the hash of the service's PK.*/
87 if (crypto_pk_get_digest(pk, pk_digest)<0) {
88 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
89 goto err;
92 crypto_pk_free(pk); /* don't need it anymore */
93 pk = NULL; /* so we don't free it again if err */
95 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
96 pk_digest, REND_SERVICE_ID_LEN);
98 /* Close any other intro circuits with the same pk. */
99 c = NULL;
100 while ((c = hs_circuitmap_get_intro_circ_v2_relay_side(
101 (const uint8_t *)pk_digest))) {
102 log_info(LD_REND, "Replacing old circuit for service %s",
103 safe_str(serviceid));
104 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
105 /* Now it's marked, and it won't be returned next time. */
108 /* Acknowledge the request. */
109 if (hs_intro_send_intro_established_cell(circ) < 0) {
110 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
111 goto err_no_close;
114 /* Now, set up this circuit. */
115 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
116 hs_circuitmap_register_intro_circ_v2_relay_side(circ, (uint8_t *)pk_digest);
118 log_info(LD_REND,
119 "Established introduction point on circuit %u for service %s",
120 (unsigned) circ->p_circ_id, safe_str(serviceid));
122 return 0;
123 truncated:
124 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
125 reason = END_CIRC_REASON_TORPROTOCOL;
126 err:
127 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
128 err_no_close:
129 if (pk) crypto_pk_free(pk);
130 return -1;
133 /** Process an INTRODUCE1 cell by finding the corresponding introduction
134 * circuit, and relaying the body of the INTRODUCE1 cell inside an
135 * INTRODUCE2 cell.
138 rend_mid_introduce_legacy(or_circuit_t *circ, const uint8_t *request,
139 size_t request_len)
141 or_circuit_t *intro_circ;
142 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
143 char nak_body[1];
145 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
146 (unsigned)circ->p_circ_id);
148 /* At this point, we know that the circuit is valid for an INTRODUCE1
149 * because the validation has been made before calling this function. */
150 tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_OR);
151 tor_assert(!circ->base_.n_chan);
153 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
154 * obsolete; however, there isn't much reason to do so, and we're going
155 * to revise this protocol anyway.
157 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
158 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
159 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
160 "responding with nack.",
161 (unsigned)circ->p_circ_id);
162 goto err;
165 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
166 (char*)request, REND_SERVICE_ID_LEN);
168 /* The first 20 bytes are all we look at: they have a hash of the service's
169 * PK. */
170 intro_circ = hs_circuitmap_get_intro_circ_v2_relay_side(
171 (const uint8_t*)request);
172 if (!intro_circ) {
173 log_info(LD_REND,
174 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
175 "responding with nack.",
176 safe_str(serviceid), (unsigned)circ->p_circ_id);
177 goto err;
180 log_info(LD_REND,
181 "Sending introduction request for service %s "
182 "from circ %u to circ %u",
183 safe_str(serviceid), (unsigned)circ->p_circ_id,
184 (unsigned)intro_circ->p_circ_id);
186 /* Great. Now we just relay the cell down the circuit. */
187 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
188 RELAY_COMMAND_INTRODUCE2,
189 (char*)request, request_len, NULL)) {
190 log_warn(LD_GENERAL,
191 "Unable to send INTRODUCE2 cell to Tor client.");
192 /* Stop right now, the circuit has been closed. */
193 return -1;
195 /* And send an ack down the client's circuit. Empty body means succeeded. */
196 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
197 RELAY_COMMAND_INTRODUCE_ACK,
198 NULL,0,NULL)) {
199 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
200 /* Stop right now, the circuit has been closed. */
201 return -1;
204 return 0;
205 err:
206 /* Send the client a NACK */
207 nak_body[0] = 1;
208 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
209 RELAY_COMMAND_INTRODUCE_ACK,
210 nak_body, 1, NULL)) {
211 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
213 return -1;
216 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
217 * rendezvous cookie.
220 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
221 size_t request_len)
223 char hexid[9];
224 int reason = END_CIRC_REASON_TORPROTOCOL;
226 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
227 (unsigned)circ->p_circ_id);
229 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
230 log_warn(LD_PROTOCOL,
231 "Tried to establish rendezvous on non-OR circuit with purpose %s",
232 circuit_purpose_to_string(circ->base_.purpose));
233 goto err;
236 /* Check if we are configured to accept established rendezvous cells from
237 * client or in other words tor2web clients. */
238 if (channel_is_client(circ->p_chan) &&
239 dos_should_refuse_single_hop_client()) {
240 /* Note it down for the heartbeat log purposes. */
241 dos_note_refuse_single_hop_client();
242 /* Silent drop so the client has to time out before moving on. */
243 return 0;
246 if (circ->base_.n_chan) {
247 log_warn(LD_PROTOCOL,
248 "Tried to establish rendezvous on non-edge circuit");
249 goto err;
252 if (request_len != REND_COOKIE_LEN) {
253 log_fn(LOG_PROTOCOL_WARN,
254 LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
255 goto err;
258 if (hs_circuitmap_get_rend_circ_relay_side(request)) {
259 log_warn(LD_PROTOCOL,
260 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
261 goto err;
264 /* Acknowledge the request. */
265 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
266 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
267 "", 0, NULL)<0) {
268 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
269 /* Stop right now, the circuit has been closed. */
270 return -1;
273 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
274 hs_circuitmap_register_rend_circ_relay_side(circ, request);
276 base16_encode(hexid,9,(char*)request,4);
278 log_info(LD_REND,
279 "Established rendezvous point on circuit %u for cookie %s",
280 (unsigned)circ->p_circ_id, hexid);
282 return 0;
283 err:
284 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
285 return -1;
288 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
289 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
290 * connecting the two circuits.
293 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
294 size_t request_len)
296 const or_options_t *options = get_options();
297 or_circuit_t *rend_circ;
298 char hexid[9];
299 int reason = END_CIRC_REASON_INTERNAL;
301 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
302 log_info(LD_REND,
303 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
304 (unsigned)circ->p_circ_id);
305 reason = END_CIRC_REASON_TORPROTOCOL;
306 goto err;
309 if (request_len < REND_COOKIE_LEN) {
310 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
311 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
312 (int)request_len, (unsigned)circ->p_circ_id);
313 reason = END_CIRC_REASON_TORPROTOCOL;
314 goto err;
317 base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
319 log_info(LD_REND,
320 "Got request for rendezvous from circuit %u to cookie %s.",
321 (unsigned)circ->p_circ_id, hexid);
323 rend_circ = hs_circuitmap_get_rend_circ_relay_side(request);
324 if (!rend_circ) {
325 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
326 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
327 hexid);
328 reason = END_CIRC_REASON_TORPROTOCOL;
329 goto err;
332 /* Statistics: Mark this circuit as an RP circuit so that we collect
333 stats from it. */
334 if (options->HiddenServiceStatistics) {
335 circ->circuit_carries_hs_traffic_stats = 1;
338 /* Send the RENDEZVOUS2 cell to the client. */
339 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
340 RELAY_COMMAND_RENDEZVOUS2,
341 (char*)(request+REND_COOKIE_LEN),
342 request_len-REND_COOKIE_LEN, NULL)) {
343 log_warn(LD_GENERAL,
344 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
345 (unsigned)rend_circ->p_circ_id);
346 /* Stop right now, the circuit has been closed. */
347 return -1;
350 /* Join the circuits. */
351 log_info(LD_REND,
352 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
353 (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
355 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
356 circuit_change_purpose(TO_CIRCUIT(rend_circ),
357 CIRCUIT_PURPOSE_REND_ESTABLISHED);
358 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
360 rend_circ->rend_splice = circ;
361 circ->rend_splice = rend_circ;
363 return 0;
364 err:
365 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
366 return -1;