fix whitespace issues
[tor/rransom.git] / src / or / rendmid.c
blob064f1be2e8dbd5197a10e5ea66c43e9305d9e448
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2011, 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 uint8_t *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((char*)(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,
68 (char*)request, 2+asn1len+DIGEST_LEN,
69 (char*)(request+2+DIGEST_LEN+asn1len),
70 request_len-(2+DIGEST_LEN+asn1len))<0) {
71 log_warn(LD_PROTOCOL,
72 "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
73 reason = END_CIRC_REASON_TORPROTOCOL;
74 goto err;
77 /* The request is valid. First, compute the hash of Bob's PK.*/
78 if (crypto_pk_get_digest(pk, pk_digest)<0) {
79 log_warn(LD_BUG, "Internal error: couldn't hash public key.");
80 goto err;
83 crypto_free_pk_env(pk); /* don't need it anymore */
84 pk = NULL; /* so we don't free it again if err */
86 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
87 pk_digest, REND_SERVICE_ID_LEN);
89 /* Close any other intro circuits with the same pk. */
90 c = NULL;
91 while ((c = circuit_get_intro_point(pk_digest))) {
92 log_info(LD_REND, "Replacing old circuit for service %s",
93 safe_str(serviceid));
94 circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
95 /* Now it's marked, and it won't be returned next time. */
98 /* Acknowledge the request. */
99 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
100 RELAY_COMMAND_INTRO_ESTABLISHED,
101 "", 0, NULL)<0) {
102 log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
103 goto err;
106 /* Now, set up this circuit. */
107 circ->_base.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
108 memcpy(circ->rend_token, pk_digest, DIGEST_LEN);
110 log_info(LD_REND,
111 "Established introduction point on circuit %d for service %s",
112 circ->p_circ_id, safe_str(serviceid));
114 return 0;
115 truncated:
116 log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
117 reason = END_CIRC_REASON_TORPROTOCOL;
118 err:
119 if (pk) crypto_free_pk_env(pk);
120 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
121 return -1;
124 /** Process an INTRODUCE1 cell by finding the corresponding introduction
125 * circuit, and relaying the body of the INTRODUCE1 cell inside an
126 * INTRODUCE2 cell.
129 rend_mid_introduce(or_circuit_t *circ, const uint8_t *request,
130 size_t request_len)
132 or_circuit_t *intro_circ;
133 char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
134 char nak_body[1];
136 log_info(LD_REND, "Received an INTRODUCE1 request on circuit %d",
137 circ->p_circ_id);
139 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
140 log_warn(LD_PROTOCOL,
141 "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
142 circ->p_circ_id);
143 goto err;
146 /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
147 * obsolete; however, there isn't much reason to do so, and we're going
148 * to revise this protocol anyway.
150 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
151 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
152 log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
153 "responding with nack.",
154 circ->p_circ_id);
155 goto err;
158 base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
159 (char*)request, REND_SERVICE_ID_LEN);
161 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
162 intro_circ = circuit_get_intro_point((char*)request);
163 if (!intro_circ) {
164 log_info(LD_REND,
165 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
166 "responding with nack.",
167 safe_str(serviceid), circ->p_circ_id);
168 goto err;
171 log_info(LD_REND,
172 "Sending introduction request for service %s "
173 "from circ %d to circ %d",
174 safe_str(serviceid), circ->p_circ_id,
175 intro_circ->p_circ_id);
177 /* Great. Now we just relay the cell down the circuit. */
178 if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
179 RELAY_COMMAND_INTRODUCE2,
180 (char*)request, request_len, NULL)) {
181 log_warn(LD_GENERAL,
182 "Unable to send INTRODUCE2 cell to Tor client.");
183 goto err;
185 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
186 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
187 RELAY_COMMAND_INTRODUCE_ACK,
188 NULL,0,NULL)) {
189 log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
190 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
191 return -1;
194 return 0;
195 err:
196 /* Send the client an NACK */
197 nak_body[0] = 1;
198 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
199 RELAY_COMMAND_INTRODUCE_ACK,
200 nak_body, 1, NULL)) {
201 log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
202 /* Is this right? */
203 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
205 return -1;
208 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
209 * rendezvous cookie.
212 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
213 size_t request_len)
215 char hexid[9];
216 int reason = END_CIRC_REASON_TORPROTOCOL;
218 log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %d",
219 circ->p_circ_id);
221 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
222 log_warn(LD_PROTOCOL,
223 "Tried to establish rendezvous on non-OR or non-edge circuit.");
224 goto err;
227 if (request_len != REND_COOKIE_LEN) {
228 log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
229 goto err;
232 if (circuit_get_rendezvous((char*)request)) {
233 log_warn(LD_PROTOCOL,
234 "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
235 goto err;
238 /* Acknowledge the request. */
239 if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
240 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
241 "", 0, NULL)<0) {
242 log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
243 reason = END_CIRC_REASON_INTERNAL;
244 goto err;
247 circ->_base.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
248 memcpy(circ->rend_token, request, REND_COOKIE_LEN);
250 base16_encode(hexid,9,(char*)request,4);
252 log_info(LD_REND,
253 "Established rendezvous point on circuit %d for cookie %s",
254 circ->p_circ_id, hexid);
256 return 0;
257 err:
258 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
259 return -1;
262 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
263 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
264 * connecting the two circuits.
267 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
268 size_t request_len)
270 or_circuit_t *rend_circ;
271 char hexid[9];
272 int reason = END_CIRC_REASON_INTERNAL;
273 base16_encode(hexid,9,(char*)request,request_len<4?request_len:4);
275 if (request_len>=4) {
276 log_info(LD_REND,
277 "Got request for rendezvous from circuit %d to cookie %s.",
278 circ->p_circ_id, hexid);
281 if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
282 log_info(LD_REND,
283 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
284 circ->p_circ_id);
285 reason = END_CIRC_REASON_TORPROTOCOL;
286 goto err;
289 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
290 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
291 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
292 (int)request_len, circ->p_circ_id);
293 reason = END_CIRC_REASON_TORPROTOCOL;
294 goto err;
297 rend_circ = circuit_get_rendezvous((char*)request);
298 if (!rend_circ) {
299 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
300 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
301 hexid);
302 reason = END_CIRC_REASON_TORPROTOCOL;
303 goto err;
306 /* Send the RENDEZVOUS2 cell to Alice. */
307 if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
308 RELAY_COMMAND_RENDEZVOUS2,
309 (char*)(request+REND_COOKIE_LEN),
310 request_len-REND_COOKIE_LEN, NULL)) {
311 log_warn(LD_GENERAL,
312 "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
313 rend_circ->p_circ_id);
314 goto err;
317 /* Join the circuits. */
318 log_info(LD_REND,
319 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
320 circ->p_circ_id, rend_circ->p_circ_id, hexid);
322 circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
323 rend_circ->_base.purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
324 memset(circ->rend_token, 0, REND_COOKIE_LEN);
326 rend_circ->rend_splice = circ;
327 circ->rend_splice = rend_circ;
329 return 0;
330 err:
331 circuit_mark_for_close(TO_CIRCUIT(circ), reason);
332 return -1;