Convert circuituse, command, config, connection, relay, router, test to new logging...
[tor.git] / src / or / rendmid.c
blobfdd70bdc45bca5b8720f0bebd96421fedd1da06d
1 /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
4 const char rendmid_c_id[] = "$Id$";
6 /**
7 * \file rendmid.c
8 * \brief Implement introductions points and rendezvous points.
9 **/
11 #define NEW_LOG_INTERFACE
12 #include "or.h"
14 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
15 * setting the circuit's purpose and service pk digest.
17 int
18 rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len)
20 crypto_pk_env_t *pk = NULL;
21 char buf[DIGEST_LEN+9];
22 char expected_digest[DIGEST_LEN];
23 char pk_digest[DIGEST_LEN];
24 size_t asn1len;
25 circuit_t *c;
26 char serviceid[REND_SERVICE_ID_LEN+1];
28 info(LD_REND,
29 "Received an ESTABLISH_INTRO request on circuit %d", circ->p_circ_id);
31 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
32 warn(LD_PROTOCOL, "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
33 goto err;
35 if (request_len < 2+DIGEST_LEN)
36 goto truncated;
37 /* First 2 bytes: length of asn1-encoded key. */
38 asn1len = ntohs(get_uint16(request));
40 /* Next asn1len bytes: asn1-encoded key. */
41 if (request_len < 2+DIGEST_LEN+asn1len)
42 goto truncated;
43 pk = crypto_pk_asn1_decode(request+2, asn1len);
44 if (!pk) {
45 warn(LD_PROTOCOL, "Couldn't decode public key.");
46 goto err;
49 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
50 memcpy(buf, circ->handshake_digest, DIGEST_LEN);
51 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
52 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
53 warn(LD_BUG, "Internal error computing digest.");
54 goto err;
56 if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
57 warn(LD_PROTOCOL, "Hash of session info was not as expected.");
58 goto err;
60 /* Rest of body: signature of previous data */
61 if (crypto_pk_public_checksig_digest(pk, request, 2+asn1len+DIGEST_LEN,
62 request+2+DIGEST_LEN+asn1len,
63 request_len-(2+DIGEST_LEN+asn1len))<0) {
64 warn(LD_PROTOCOL, "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
65 goto err;
68 /* The request is valid. First, compute the hash of Bob's PK.*/
69 if (crypto_pk_get_digest(pk, pk_digest)<0) {
70 warn(LD_BUG, "Internal error: couldn't hash public key.");
71 goto err;
74 crypto_free_pk_env(pk); /* don't need it anymore */
75 pk = NULL; /* so we don't free it again if err */
77 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, pk_digest,10);
79 /* Close any other intro circuits with the same pk. */
80 c = NULL;
81 while ((c = circuit_get_next_by_pk_and_purpose(
82 c,pk_digest,CIRCUIT_PURPOSE_INTRO_POINT))) {
83 info(LD_REND, "Replacing old circuit %d for service %s",
84 c->p_circ_id, safe_str(serviceid));
85 circuit_mark_for_close(c);
88 /* Acknowledge the request. */
89 if (connection_edge_send_command(NULL,circ,
90 RELAY_COMMAND_INTRO_ESTABLISHED,
91 "", 0, NULL)<0) {
92 info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
93 goto err;
96 /* Now, set up this circuit. */
97 circ->purpose = CIRCUIT_PURPOSE_INTRO_POINT;
98 memcpy(circ->rend_pk_digest, pk_digest, DIGEST_LEN);
100 info(LD_REND,
101 "Established introduction point on circuit %d for service %s",
102 circ->p_circ_id, safe_str(serviceid));
104 return 0;
105 truncated:
106 warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
107 err:
108 if (pk) crypto_free_pk_env(pk);
109 circuit_mark_for_close(circ);
110 return -1;
113 /** Process an INTRODUCE1 cell by finding the corresponding introduction
114 * circuit, and relaying the body of the INTRODUCE1 cell inside an
115 * INTRODUCE2 cell.
118 rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len)
120 circuit_t *intro_circ;
121 char serviceid[REND_SERVICE_ID_LEN+1];
122 char nak_body[1];
124 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
125 warn(LD_PROTOCOL, "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
126 circ->p_circ_id);
127 goto err;
130 /* change to MAX_HEX_NICKNAME_LEN once 0.0.9.x is obsolete */
131 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
132 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
133 warn(LD_PROTOCOL,
134 "Impossibly short INTRODUCE1 cell on circuit %d; responding with nack.",
135 circ->p_circ_id);
136 goto err;
139 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
141 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
142 intro_circ = circuit_get_next_by_pk_and_purpose(
143 NULL, request, CIRCUIT_PURPOSE_INTRO_POINT);
144 if (!intro_circ) {
145 info(LD_REND,
146 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; responding with nack.",
147 safe_str(serviceid), circ->p_circ_id);
148 goto err;
151 info(LD_REND,
152 "Sending introduction request for service %s from circ %d to circ %d",
153 safe_str(serviceid), circ->p_circ_id, intro_circ->p_circ_id);
155 /* Great. Now we just relay the cell down the circuit. */
156 if (connection_edge_send_command(NULL, intro_circ,
157 RELAY_COMMAND_INTRODUCE2,
158 request, request_len, NULL)) {
159 warn(LD_GENERAL,
160 "Unable to send INTRODUCE2 cell to Tor client.");
161 goto err;
163 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
164 if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
165 NULL,0,NULL)) {
166 warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
167 circuit_mark_for_close(circ);
168 return -1;
171 return 0;
172 err:
173 /* Send the client an NACK */
174 nak_body[0] = 1;
175 if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
176 nak_body, 1, NULL)) {
177 warn(LD_GENERAL, "Unable to send NAK to Tor client.");
178 circuit_mark_for_close(circ); /* Is this right? */
180 return -1;
183 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
184 * rendezvous cookie.
187 rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len)
189 char hexid[9];
191 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
192 warn(LD_PROTOCOL, "Tried to establish rendezvous on non-OR or non-edge circuit.");
193 goto err;
196 if (request_len != REND_COOKIE_LEN) {
197 warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
198 goto err;
201 if (circuit_get_rendezvous(request)) {
202 warn(LD_PROTOCOL, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
203 goto err;
206 /* Acknowledge the request. */
207 if (connection_edge_send_command(NULL,circ,
208 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
209 "", 0, NULL)<0) {
210 warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
211 goto err;
214 circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
215 memcpy(circ->rend_cookie, request, REND_COOKIE_LEN);
217 base16_encode(hexid,9,request,4);
219 info(LD_REND, "Established rendezvous point on circuit %d for cookie %s",
220 circ->p_circ_id, hexid);
222 return 0;
223 err:
224 circuit_mark_for_close(circ);
225 return -1;
228 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
229 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
230 * connecting the two circuits.
233 rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len)
235 circuit_t *rend_circ;
236 char hexid[9];
238 base16_encode(hexid,9,request,request_len<4?request_len:4);
240 if (request_len>=4) {
241 info(LD_REND, "Got request for rendezvous from circuit %d to cookie %s.",
242 circ->p_circ_id, hexid);
245 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
246 info(LD_REND,
247 "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
248 circ->p_circ_id);
249 goto err;
252 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
253 warn(LD_PROTOCOL,
254 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
255 (int)request_len, circ->p_circ_id);
256 goto err;
259 rend_circ = circuit_get_rendezvous(request);
260 if (!rend_circ) {
261 warn(LD_PROTOCOL,
262 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
263 hexid);
264 goto err;
267 /* Send the RENDEZVOUS2 cell to Alice. */
268 if (connection_edge_send_command(NULL, rend_circ,
269 RELAY_COMMAND_RENDEZVOUS2,
270 request+REND_COOKIE_LEN,
271 request_len-REND_COOKIE_LEN, NULL)) {
272 warn(LD_GENERAL,
273 "Unable to send RENDEZVOUS2 cell to OP on circuit %d.",
274 rend_circ->p_circ_id);
275 goto err;
278 /* Join the circuits. */
279 info(LD_REND,
280 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
281 circ->p_circ_id, rend_circ->p_circ_id, hexid);
283 circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
284 rend_circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
285 memset(circ->rend_cookie, 0, REND_COOKIE_LEN);
287 rend_circ->rend_splice = circ;
288 circ->rend_splice = rend_circ;
290 return 0;
291 err:
292 circuit_mark_for_close(circ);
293 return -1;