Forward port changelog
[tor.git] / src / or / rendmid.c
blob302a6f011f60ed2299f87c741e0feb56cd99dc86
1 /* Copyright 2004 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 #include "or.h"
13 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
14 * setting the circuit's purpose and service pk digest.
16 int
17 rend_mid_establish_intro(circuit_t *circ, const char *request, 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 circuit_t *c;
25 char serviceid[REND_SERVICE_ID_LEN+1];
27 log_fn(LOG_INFO,
28 "Received an ESTABLISH_INTRO request on circuit %d", circ->p_circ_id);
30 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
31 log_fn(LOG_WARN, "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit");
32 goto err;
34 if (request_len < 2+DIGEST_LEN)
35 goto truncated;
36 /* First 2 bytes: length of asn1-encoded key. */
37 asn1len = ntohs(get_uint16(request));
39 /* Next asn1len bytes: asn1-encoded key. */
40 if (request_len < 2+DIGEST_LEN+asn1len)
41 goto truncated;
42 pk = crypto_pk_asn1_decode(request+2, asn1len);
43 if (!pk) {
44 log_fn(LOG_WARN, "Couldn't decode public key");
45 goto err;
48 /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
49 memcpy(buf, circ->handshake_digest, DIGEST_LEN);
50 memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
51 if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
52 log_fn(LOG_WARN, "Error computing digest");
53 goto err;
55 if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
56 log_fn(LOG_WARN, "Hash of session info was not as expected");
57 goto err;
59 /* Rest of body: signature of previous data */
60 if (crypto_pk_public_checksig_digest(pk, request, 2+asn1len+DIGEST_LEN,
61 request+2+DIGEST_LEN+asn1len,
62 request_len-(2+DIGEST_LEN+asn1len))<0) {
63 log_fn(LOG_WARN, "Incorrect signature on ESTABLISH_INTRO cell; rejecting");
64 goto err;
67 /* The request is valid. First, compute the hash of Bob's PK.*/
68 if (crypto_pk_get_digest(pk, pk_digest)<0) {
69 log_fn(LOG_WARN, "Couldn't hash public key.");
70 goto err;
73 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, pk_digest,10);
75 /* Close any other intro circuits with the same pk. */
76 c = NULL;
77 while ((c = circuit_get_next_by_pk_and_purpose(
78 c,pk_digest,CIRCUIT_PURPOSE_INTRO_POINT))) {
79 log_fn(LOG_INFO, "Replacing old circuit %d for service %s",
80 c->p_circ_id, serviceid);
81 circuit_mark_for_close(c);
84 /* Acknowledge the request. */
85 if (connection_edge_send_command(NULL,circ,
86 RELAY_COMMAND_INTRO_ESTABLISHED,
87 "", 0, NULL)<0) {
88 log_fn(LOG_WARN, "Couldn't send INTRO_ESTABLISHED cell");
89 goto err;
92 /* Now, set up this circuit. */
93 circ->purpose = CIRCUIT_PURPOSE_INTRO_POINT;
94 memcpy(circ->rend_pk_digest, pk_digest, DIGEST_LEN);
96 log_fn(LOG_INFO,
97 "Established introduction point on circuit %d for service %s",
98 circ->p_circ_id, serviceid);
100 return 0;
101 truncated:
102 log_fn(LOG_WARN, "Rejecting truncated ESTABLISH_INTRO cell");
103 err:
104 if (pk) crypto_free_pk_env(pk);
105 circuit_mark_for_close(circ);
106 return -1;
109 /** Process an INTRODUCE1 cell by finding the corresponding introduction
110 * circuit, and relaying the body of the INTRODUCE1 cell inside an
111 * INTRODUCE2 cell.
114 rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len)
116 circuit_t *intro_circ;
117 char serviceid[REND_SERVICE_ID_LEN+1];
118 char nak_body[1];
120 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
121 log_fn(LOG_WARN, "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d",
122 circ->p_circ_id);
123 goto err;
126 /* change to MAX_HEX_NICKNAME_LEN once 0.0.9.x is obsolete */
127 if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
128 DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
129 log_fn(LOG_WARN,
130 "Impossibly short INTRODUCE1 cell on circuit %d; responding with nack.",
131 circ->p_circ_id);
132 goto err;
135 base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
137 /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
138 intro_circ = circuit_get_next_by_pk_and_purpose(
139 NULL, request, CIRCUIT_PURPOSE_INTRO_POINT);
140 if (!intro_circ) {
141 log_fn(LOG_WARN,
142 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; responding with nack",
143 serviceid, circ->p_circ_id);
144 goto err;
147 log_fn(LOG_INFO,
148 "Sending introduction request for service %s from circ %d to circ %d",
149 serviceid, circ->p_circ_id, intro_circ->p_circ_id);
151 /* Great. Now we just relay the cell down the circuit. */
152 if (connection_edge_send_command(NULL, intro_circ,
153 RELAY_COMMAND_INTRODUCE2,
154 request, request_len, NULL)) {
155 log_fn(LOG_WARN, "Unable to send INTRODUCE2 cell to OP.");
156 goto err;
158 /* And sent an ack down Alice's circuit. Empty body means succeeded. */
159 if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
160 NULL,0,NULL)) {
161 log_fn(LOG_WARN, "Unable to send INTRODUCE_ACK cell to OP.");
162 circuit_mark_for_close(circ);
163 return -1;
166 return 0;
167 err:
168 /* Send the client an NACK */
169 nak_body[0] = 1;
170 if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
171 nak_body, 1, NULL)) {
172 log_fn(LOG_WARN, "Unable to send NAK to OP");
173 circuit_mark_for_close(circ); /* Is this right? */
175 return -1;
178 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
179 * rendezvous cookie.
182 rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len)
184 char hexid[9];
186 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
187 log_fn(LOG_WARN, "Tried to establish rendezvous on non-OR or non-edge circuit");
188 goto err;
191 if (request_len != REND_COOKIE_LEN) {
192 log_fn(LOG_WARN, "Invalid length on ESTABLISH_RENDEZVOUS");
193 goto err;
196 if (circuit_get_rendezvous(request)) {
197 log_fn(LOG_WARN, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS");
198 goto err;
201 /* Acknowledge the request. */
202 if (connection_edge_send_command(NULL,circ,
203 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
204 "", 0, NULL)<0) {
205 log_fn(LOG_WARN, "Couldn't send RENDEZVOUS_ESTABLISHED cell");
206 goto err;
209 circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
210 memcpy(circ->rend_cookie, request, REND_COOKIE_LEN);
212 base16_encode(hexid,9,request,4);
214 log_fn(LOG_INFO, "Established rendezvous point on circuit %d for cookie %s",
215 circ->p_circ_id, hexid);
217 return 0;
218 err:
219 circuit_mark_for_close(circ);
220 return -1;
223 /** Process a RENDEZVOUS1 cell by looking up the correct rendezvous
224 * circuit by its relaying the cell's body in a RENDEZVOUS2 cell, and
225 * connecting the two circuits.
228 rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len)
230 circuit_t *rend_circ;
231 char hexid[9];
233 base16_encode(hexid,9,request,request_len<4?request_len:4);
235 if (request_len>=4) {
236 log_fn(LOG_INFO, "Got request for rendezvous from circuit %d to cookie %s",
237 circ->p_circ_id, hexid);
240 if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
241 log_fn(LOG_WARN,
242 "Tried to complete rendezvous on non-OR or non-edge circuit %d",
243 circ->p_circ_id);
244 goto err;
247 if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
248 log_fn(LOG_WARN,
249 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d",
250 (int)request_len, circ->p_circ_id);
251 goto err;
254 rend_circ = circuit_get_rendezvous(request);
255 if (!rend_circ) {
256 log_fn(LOG_WARN,
257 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s",
258 hexid);
259 goto err;
262 /* Send the RENDEZVOUS2 cell to Alice. */
263 if (connection_edge_send_command(NULL, rend_circ,
264 RELAY_COMMAND_RENDEZVOUS2,
265 request+REND_COOKIE_LEN,
266 request_len-REND_COOKIE_LEN, NULL)) {
267 log_fn(LOG_WARN, "Unable to send RENDEZVOUS2 cell to OP on circuit %d",
268 rend_circ->p_circ_id);
269 goto err;
272 /* Join the circuits. */
273 log_fn(LOG_INFO,
274 "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
275 circ->p_circ_id, rend_circ->p_circ_id, hexid);
277 circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
278 rend_circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
279 memset(circ->rend_cookie, 0, REND_COOKIE_LEN);
281 rend_circ->rend_splice = circ;
282 circ->rend_splice = rend_circ;
284 return 0;
285 err:
286 circuit_mark_for_close(circ);
287 return -1;