123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- const char rendmid_c_id[] = "$Id$";
- #include "or.h"
- int
- rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len)
- {
- crypto_pk_env_t *pk = NULL;
- char buf[DIGEST_LEN+9];
- char expected_digest[DIGEST_LEN];
- char pk_digest[DIGEST_LEN];
- size_t asn1len;
- circuit_t *c;
- char serviceid[REND_SERVICE_ID_LEN+1];
- log_fn(LOG_INFO,
- "Received an ESTABLISH_INTRO request on circuit %d", circ->p_circ_id);
- if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
- log_fn(LOG_WARN, "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit");
- goto err;
- }
- if (request_len < 2+DIGEST_LEN)
- goto truncated;
-
- asn1len = ntohs(get_uint16(request));
-
- if (request_len < 2+DIGEST_LEN+asn1len)
- goto truncated;
- pk = crypto_pk_asn1_decode(request+2, asn1len);
- if (!pk) {
- log_fn(LOG_WARN, "Couldn't decode public key");
- goto err;
- }
-
- memcpy(buf, circ->handshake_digest, DIGEST_LEN);
- memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
- if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
- log_fn(LOG_WARN, "Error computing digest");
- goto err;
- }
- if (memcmp(expected_digest, request+2+asn1len, DIGEST_LEN)) {
- log_fn(LOG_WARN, "Hash of session info was not as expected");
- goto err;
- }
-
- if (crypto_pk_public_checksig_digest(pk, request, 2+asn1len+DIGEST_LEN,
- request+2+DIGEST_LEN+asn1len,
- request_len-(2+DIGEST_LEN+asn1len))<0) {
- log_fn(LOG_WARN, "Incorrect signature on ESTABLISH_INTRO cell; rejecting");
- goto err;
- }
-
- if (crypto_pk_get_digest(pk, pk_digest)<0) {
- log_fn(LOG_WARN, "Couldn't hash public key.");
- goto err;
- }
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1, pk_digest,10);
-
- c = NULL;
- while ((c = circuit_get_next_by_pk_and_purpose(
- c,pk_digest,CIRCUIT_PURPOSE_INTRO_POINT))) {
- log_fn(LOG_INFO, "Replacing old circuit %d for service %s",
- c->p_circ_id, serviceid);
- circuit_mark_for_close(c);
- }
-
- if (connection_edge_send_command(NULL,circ,
- RELAY_COMMAND_INTRO_ESTABLISHED,
- "", 0, NULL)<0) {
- log_fn(LOG_WARN, "Couldn't send INTRO_ESTABLISHED cell");
- goto err;
- }
-
- circ->purpose = CIRCUIT_PURPOSE_INTRO_POINT;
- memcpy(circ->rend_pk_digest, pk_digest, DIGEST_LEN);
- log_fn(LOG_INFO,
- "Established introduction point on circuit %d for service %s",
- circ->p_circ_id, serviceid);
- return 0;
- truncated:
- log_fn(LOG_WARN, "Rejecting truncated ESTABLISH_INTRO cell");
- err:
- if (pk) crypto_free_pk_env(pk);
- circuit_mark_for_close(circ);
- return -1;
- }
- int
- rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len)
- {
- circuit_t *intro_circ;
- char serviceid[REND_SERVICE_ID_LEN+1];
- char nak_body[1];
- if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
- log_fn(LOG_WARN, "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d",
- circ->p_circ_id);
- goto err;
- }
-
- if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
- DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
- log_fn(LOG_WARN,
- "Impossibly short INTRODUCE1 cell on circuit %d; responding with nack.",
- circ->p_circ_id);
- goto err;
- }
- base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request,10);
-
- intro_circ = circuit_get_next_by_pk_and_purpose(
- NULL, request, CIRCUIT_PURPOSE_INTRO_POINT);
- if (!intro_circ) {
- log_fn(LOG_WARN,
- "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; responding with nack",
- serviceid, circ->p_circ_id);
- goto err;
- }
- log_fn(LOG_INFO,
- "Sending introduction request for service %s from circ %d to circ %d",
- serviceid, circ->p_circ_id, intro_circ->p_circ_id);
-
- if (connection_edge_send_command(NULL, intro_circ,
- RELAY_COMMAND_INTRODUCE2,
- request, request_len, NULL)) {
- log_fn(LOG_WARN, "Unable to send INTRODUCE2 cell to OP.");
- goto err;
- }
-
- if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
- NULL,0,NULL)) {
- log_fn(LOG_WARN, "Unable to send INTRODUCE_ACK cell to OP.");
- circuit_mark_for_close(circ);
- return -1;
- }
- return 0;
- err:
-
- nak_body[0] = 1;
- if (connection_edge_send_command(NULL,circ,RELAY_COMMAND_INTRODUCE_ACK,
- nak_body, 1, NULL)) {
- log_fn(LOG_WARN, "Unable to send NAK to OP");
- circuit_mark_for_close(circ);
- }
- return -1;
- }
- int
- rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len)
- {
- char hexid[9];
- if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
- log_fn(LOG_WARN, "Tried to establish rendezvous on non-OR or non-edge circuit");
- goto err;
- }
- if (request_len != REND_COOKIE_LEN) {
- log_fn(LOG_WARN, "Invalid length on ESTABLISH_RENDEZVOUS");
- goto err;
- }
- if (circuit_get_rendezvous(request)) {
- log_fn(LOG_WARN, "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS");
- goto err;
- }
-
- if (connection_edge_send_command(NULL,circ,
- RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
- "", 0, NULL)<0) {
- log_fn(LOG_WARN, "Couldn't send RENDEZVOUS_ESTABLISHED cell");
- goto err;
- }
- circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
- memcpy(circ->rend_cookie, request, REND_COOKIE_LEN);
- base16_encode(hexid,9,request,4);
- log_fn(LOG_INFO, "Established rendezvous point on circuit %d for cookie %s",
- circ->p_circ_id, hexid);
- return 0;
- err:
- circuit_mark_for_close(circ);
- return -1;
- }
- int
- rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len)
- {
- circuit_t *rend_circ;
- char hexid[9];
- base16_encode(hexid,9,request,request_len<4?request_len:4);
- if (request_len>=4) {
- log_fn(LOG_INFO, "Got request for rendezvous from circuit %d to cookie %s",
- circ->p_circ_id, hexid);
- }
- if (circ->purpose != CIRCUIT_PURPOSE_OR || circ->n_conn) {
- log_fn(LOG_WARN,
- "Tried to complete rendezvous on non-OR or non-edge circuit %d",
- circ->p_circ_id);
- goto err;
- }
- if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
- log_fn(LOG_WARN,
- "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d",
- (int)request_len, circ->p_circ_id);
- goto err;
- }
- rend_circ = circuit_get_rendezvous(request);
- if (!rend_circ) {
- log_fn(LOG_WARN,
- "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s",
- hexid);
- goto err;
- }
-
- if (connection_edge_send_command(NULL, rend_circ,
- RELAY_COMMAND_RENDEZVOUS2,
- request+REND_COOKIE_LEN,
- request_len-REND_COOKIE_LEN, NULL)) {
- log_fn(LOG_WARN, "Unable to send RENDEZVOUS2 cell to OP on circuit %d",
- rend_circ->p_circ_id);
- goto err;
- }
-
- log_fn(LOG_INFO,
- "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
- circ->p_circ_id, rend_circ->p_circ_id, hexid);
- circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
- rend_circ->purpose = CIRCUIT_PURPOSE_REND_ESTABLISHED;
- memset(circ->rend_cookie, 0, REND_COOKIE_LEN);
- rend_circ->rend_splice = circ;
- circ->rend_splice = rend_circ;
- return 0;
- err:
- circuit_mark_for_close(circ);
- return -1;
- }
|