Add Coccinelle patch for replacing NULL/non-NULL tt_assert().
[tor.git] / src / or / rendmid.c
blob66d2f93113ec1ce1cf1983b0ce40c9051b2885b7
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 "circuitlist.h"
12 #include "circuituse.h"
13 #include "config.h"
14 #include "crypto.h"
15 #include "relay.h"
16 #include "rendmid.h"
17 #include "rephist.h"
18 #include "hs_circuitmap.h"
19 #include "hs_intropoint.h"
21 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
22 * setting the circuit's purpose and service pk digest.
24 int
25 rend_mid_establish_intro_legacy(or_circuit_t *circ, const uint8_t *request,
26 size_t request_len)
28 crypto_pk_t *pk = NULL;
29 char buf[DIGEST_LEN+9];
30 char expected_digest[DIGEST_LEN];
31 char pk_digest[DIGEST_LEN];
32 size_t asn1len;
33 or_circuit_t *c;
34 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
35 int reason = END_CIRC_REASON_INTERNAL;
37 log_info(LD_REND,
38 "Received a legacy ESTABLISH_INTRO request on circuit %u",
39 (unsigned) circ->p_circ_id);
41 if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
42 reason = END_CIRC_REASON_TORPROTOCOL;
43 goto err;
46 if (request_len < 2+DIGEST_LEN)
47 goto truncated;
48 /* First 2 bytes: length of asn1-encoded key. */
49 asn1len = ntohs(get_uint16(request));
51 /* Next asn1len bytes: asn1-encoded key. */
52 if (request_len < 2+DIGEST_LEN+asn1len)
53 goto truncated;
54 pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
55 if (!pk) {
56 reason = END_CIRC_REASON_TORPROTOCOL;
57 log_warn(LD_PROTOCOL, "Couldn't decode public key.");
58 goto err;
61 /* Next 20 bytes: Hash of rend_circ_nonce | "INTRODUCE" */
62 memcpy(buf, circ->rend_circ_nonce, DIGEST_LEN);
63 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
64 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
65 log_warn(LD_BUG, "Internal error computing digest.");
66 goto err;
68 if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
69 log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
70 reason = END_CIRC_REASON_TORPROTOCOL;
71 goto err;
73 /* Rest of body: signature of previous data */
74 if (crypto_pk_public_checksig_digest(pk,
75 (char*)request, 2+asn1len+DIGEST_LEN,
76 (char*)(request+2+DIGEST_LEN+asn1len),
77 request_len-(2+DIGEST_LEN+asn1len))<0) {
78 log_warn(LD_PROTOCOL,
79 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
80 reason = END_CIRC_REASON_TORPROTOCOL;
81 goto err;
84 /* The request is valid. First, compute the hash of the service's PK.*/
85 if (crypto_pk_get_digest(pk, pk_digest)<0) {
86 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
87 goto err;
90 crypto_pk_free(pk); /* don't need it anymore */
91 pk = NULL; /* so we don't free it again if err */
93 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
94 pk_digest, REND_SERVICE_ID_LEN);
96 /* Close any other intro circuits with the same pk. */
97 c = NULL;
98 while ((c = hs_circuitmap_get_intro_circ_v2_relay_side(
99 (const uint8_t *)pk_digest))) {
100 log_info(LD_REND, "Replacing old circuit for service %s",
101 safe_str(serviceid));
102 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
103 /* Now it's marked, and it won't be returned next time. */
106 /* Acknowledge the request. */
107 if (hs_intro_send_intro_established_cell(circ) < 0) {
108 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
109 goto err_no_close;
112 /* Now, set up this circuit. */
113 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
114 hs_circuitmap_register_intro_circ_v2_relay_side(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 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
126 err_no_close:
127 if (pk) crypto_pk_free(pk);
128 return -1;
131 /** Process an INTRODUCE1 cell by finding the corresponding introduction
132 * circuit, and relaying the body of the INTRODUCE1 cell inside an
133 * INTRODUCE2 cell.
136 rend_mid_introduce_legacy(or_circuit_t *circ, const uint8_t *request,
137 size_t request_len)
139 or_circuit_t *intro_circ;
140 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
141 char nak_body[1];
143 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %u",
144 (unsigned)circ->p_circ_id);
146 /* At this point, we know that the circuit is valid for an INTRODUCE1
147 * because the validation has been made before calling this function. */
148 tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_OR);
149 tor_assert(!circ->base_.n_chan);
151 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
152 * obsolete; however, there isn't much reason to do so, and we're going
153 * to revise this protocol anyway.
155 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
156 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
157 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %u; "
158 "responding with nack.",
159 (unsigned)circ->p_circ_id);
160 goto err;
163 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
164 (char*)request, REND_SERVICE_ID_LEN);
166 /* The first 20 bytes are all we look at: they have a hash of the service's
167 * PK. */
168 intro_circ = hs_circuitmap_get_intro_circ_v2_relay_side(
169 (const uint8_t*)request);
170 if (!intro_circ) {
171 log_info(LD_REND,
172 "No intro circ found for INTRODUCE1 cell (%s) from circuit %u; "
173 "responding with nack.",
174 safe_str(serviceid), (unsigned)circ->p_circ_id);
175 goto err;
178 log_info(LD_REND,
179 "Sending introduction request for service %s "
180 "from circ %u to circ %u",
181 safe_str(serviceid), (unsigned)circ->p_circ_id,
182 (unsigned)intro_circ->p_circ_id);
184 /* Great. Now we just relay the cell down the circuit. */
185 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
186 RELAY_COMMAND_INTRODUCE2,
187 (char*)request, request_len, NULL)) {
188 log_warn(LD_GENERAL,
189 "Unable to send INTRODUCE2 cell to Tor client.");
190 /* Stop right now, the circuit has been closed. */
191 return -1;
193 /* And send an ack down the client's circuit. Empty body means succeeded. */
194 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
195 RELAY_COMMAND_INTRODUCE_ACK,
196 NULL,0,NULL)) {
197 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
198 /* Stop right now, the circuit has been closed. */
199 return -1;
202 return 0;
203 err:
204 /* Send the client a NACK */
205 nak_body[0] = 1;
206 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
207 RELAY_COMMAND_INTRODUCE_ACK,
208 nak_body, 1, NULL)) {
209 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
211 return -1;
214 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
215 * rendezvous cookie.
218 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
219 size_t request_len)
221 char hexid[9];
222 int reason = END_CIRC_REASON_TORPROTOCOL;
224 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %u",
225 (unsigned)circ->p_circ_id);
227 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
228 log_warn(LD_PROTOCOL,
229 "Tried to establish rendezvous on non-OR circuit with purpose %s",
230 circuit_purpose_to_string(circ->base_.purpose));
231 goto err;
234 if (circ->base_.n_chan) {
235 log_warn(LD_PROTOCOL,
236 "Tried to establish rendezvous on non-edge circuit");
237 goto err;
240 if (request_len != REND_COOKIE_LEN) {
241 log_fn(LOG_PROTOCOL_WARN,
242 LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
243 goto err;
246 if (hs_circuitmap_get_rend_circ_relay_side(request)) {
247 log_warn(LD_PROTOCOL,
248 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
249 goto err;
252 /* Acknowledge the request. */
253 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
254 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
255 "", 0, NULL)<0) {
256 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
257 /* Stop right now, the circuit has been closed. */
258 return -1;
261 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
262 hs_circuitmap_register_rend_circ_relay_side(circ, request);
264 base16_encode(hexid,9,(char*)request,4);
266 log_info(LD_REND,
267 "Established rendezvous point on circuit %u for cookie %s",
268 (unsigned)circ->p_circ_id, hexid);
270 return 0;
271 err:
272 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
273 return -1;
276 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
277 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
278 * connecting the two circuits.
281 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
282 size_t request_len)
284 const or_options_t *options = get_options();
285 or_circuit_t *rend_circ;
286 char hexid[9];
287 int reason = END_CIRC_REASON_INTERNAL;
289 if (circ->base_.purpose != CIRCUIT_PURPOSE_OR || circ->base_.n_chan) {
290 log_info(LD_REND,
291 "Tried to complete rendezvous on non-OR or non-edge circuit %u.",
292 (unsigned)circ->p_circ_id);
293 reason = END_CIRC_REASON_TORPROTOCOL;
294 goto err;
297 if (request_len < REND_COOKIE_LEN) {
298 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
299 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %u.",
300 (int)request_len, (unsigned)circ->p_circ_id);
301 reason = END_CIRC_REASON_TORPROTOCOL;
302 goto err;
305 base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
307 log_info(LD_REND,
308 "Got request for rendezvous from circuit %u to cookie %s.",
309 (unsigned)circ->p_circ_id, hexid);
311 rend_circ = hs_circuitmap_get_rend_circ_relay_side(request);
312 if (!rend_circ) {
313 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
314 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
315 hexid);
316 reason = END_CIRC_REASON_TORPROTOCOL;
317 goto err;
320 /* Statistics: Mark this circuit as an RP circuit so that we collect
321 stats from it. */
322 if (options->HiddenServiceStatistics) {
323 circ->circuit_carries_hs_traffic_stats = 1;
326 /* Send the RENDEZVOUS2 cell to the client. */
327 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
328 RELAY_COMMAND_RENDEZVOUS2,
329 (char*)(request+REND_COOKIE_LEN),
330 request_len-REND_COOKIE_LEN, NULL)) {
331 log_warn(LD_GENERAL,
332 "Unable to send RENDEZVOUS2 cell to client on circuit %u.",
333 (unsigned)rend_circ->p_circ_id);
334 /* Stop right now, the circuit has been closed. */
335 return -1;
338 /* Join the circuits. */
339 log_info(LD_REND,
340 "Completing rendezvous: circuit %u joins circuit %u (cookie %s)",
341 (unsigned)circ->p_circ_id, (unsigned)rend_circ->p_circ_id, hexid);
343 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
344 circuit_change_purpose(TO_CIRCUIT(rend_circ),
345 CIRCUIT_PURPOSE_REND_ESTABLISHED);
346 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
348 rend_circ->rend_splice = circ;
349 circ->rend_splice = rend_circ;
351 return 0;
352 err:
353 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
354 return -1;