1 /* Copyright 2004 Roger Dingledine, Nick Mathewson. */
2 /* See LICENSE for licensing information */
4 const char rendmid_c_id
[] = "$Id$";
8 * \brief Implement introductions points and rendezvous points.
13 /** Respond to an ESTABLISH_INTRO cell by checking the signed data and
14 * setting the circuit's purpose and service pk digest.
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
];
25 char serviceid
[REND_SERVICE_ID_LEN
+1];
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");
34 if (request_len
< 2+DIGEST_LEN
)
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
)
42 pk
= crypto_pk_asn1_decode(request
+2, asn1len
);
44 log_fn(LOG_WARN
, "Couldn't decode public key");
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");
55 if (memcmp(expected_digest
, request
+2+asn1len
, DIGEST_LEN
)) {
56 log_fn(LOG_WARN
, "Hash of session info was not as expected");
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");
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.");
73 base32_encode(serviceid
, REND_SERVICE_ID_LEN
+1, pk_digest
,10);
75 /* Close any other intro circuits with the same pk. */
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
,
88 log_fn(LOG_WARN
, "Couldn't send INTRO_ESTABLISHED cell");
92 /* Now, set up this circuit. */
93 circ
->purpose
= CIRCUIT_PURPOSE_INTRO_POINT
;
94 memcpy(circ
->rend_pk_digest
, pk_digest
, DIGEST_LEN
);
97 "Established introduction point on circuit %d for service %s",
98 circ
->p_circ_id
, serviceid
);
102 log_fn(LOG_WARN
, "Rejecting truncated ESTABLISH_INTRO cell");
104 if (pk
) crypto_free_pk_env(pk
);
105 circuit_mark_for_close(circ
);
109 /** Process an INTRODUCE1 cell by finding the corresponding introduction
110 * circuit, and relaying the body of the INTRODUCE1 cell inside an
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];
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",
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
)) {
130 "Impossibly short INTRODUCE1 cell on circuit %d; responding with nack.",
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
);
142 "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; responding with nack",
143 serviceid
, circ
->p_circ_id
);
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.");
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
,
161 log_fn(LOG_WARN
, "Unable to send INTRODUCE_ACK cell to OP.");
162 circuit_mark_for_close(circ
);
168 /* Send the client an NACK */
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? */
178 /** Process an ESTABLISH_RENDEZVOUS cell by setting the circuit's purpose and
182 rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
)
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");
191 if (request_len
!= REND_COOKIE_LEN
) {
192 log_fn(LOG_WARN
, "Invalid length on ESTABLISH_RENDEZVOUS");
196 if (circuit_get_rendezvous(request
)) {
197 log_fn(LOG_WARN
, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS");
201 /* Acknowledge the request. */
202 if (connection_edge_send_command(NULL
,circ
,
203 RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
,
205 log_fn(LOG_WARN
, "Couldn't send RENDEZVOUS_ESTABLISHED cell");
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
);
219 circuit_mark_for_close(circ
);
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
;
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
) {
242 "Tried to complete rendezvous on non-OR or non-edge circuit %d",
247 if (request_len
!= REND_COOKIE_LEN
+DH_KEY_LEN
+DIGEST_LEN
) {
249 "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d",
250 (int)request_len
, circ
->p_circ_id
);
254 rend_circ
= circuit_get_rendezvous(request
);
257 "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s",
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
);
272 /* Join the circuits. */
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
;
286 circuit_mark_for_close(circ
);