Back to index

tor  0.2.3.19-rc
rendmid.c
Go to the documentation of this file.
00001 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00002  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00003 /* See LICENSE for licensing information */
00004 
00010 #include "or.h"
00011 #include "circuitlist.h"
00012 #include "circuituse.h"
00013 #include "config.h"
00014 #include "relay.h"
00015 #include "rendmid.h"
00016 #include "rephist.h"
00017 
00021 int
00022 rend_mid_establish_intro(or_circuit_t *circ, const uint8_t *request,
00023                          size_t request_len)
00024 {
00025   crypto_pk_t *pk = NULL;
00026   char buf[DIGEST_LEN+9];
00027   char expected_digest[DIGEST_LEN];
00028   char pk_digest[DIGEST_LEN];
00029   size_t asn1len;
00030   or_circuit_t *c;
00031   char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
00032   int reason = END_CIRC_REASON_INTERNAL;
00033 
00034   log_info(LD_REND,
00035            "Received an ESTABLISH_INTRO request on circuit %d",
00036            circ->p_circ_id);
00037 
00038   if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
00039     log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
00040          "Rejecting ESTABLISH_INTRO on non-OR or non-edge circuit.");
00041     reason = END_CIRC_REASON_TORPROTOCOL;
00042     goto err;
00043   }
00044   if (request_len < 2+DIGEST_LEN)
00045     goto truncated;
00046   /* First 2 bytes: length of asn1-encoded key. */
00047   asn1len = ntohs(get_uint16(request));
00048 
00049   /* Next asn1len bytes: asn1-encoded key. */
00050   if (request_len < 2+DIGEST_LEN+asn1len)
00051     goto truncated;
00052   pk = crypto_pk_asn1_decode((char*)(request+2), asn1len);
00053   if (!pk) {
00054     reason = END_CIRC_REASON_TORPROTOCOL;
00055     log_warn(LD_PROTOCOL, "Couldn't decode public key.");
00056     goto err;
00057   }
00058 
00059   /* Next 20 bytes: Hash of handshake_digest | "INTRODUCE" */
00060   memcpy(buf, circ->handshake_digest, DIGEST_LEN);
00061   memcpy(buf+DIGEST_LEN, "INTRODUCE", 9);
00062   if (crypto_digest(expected_digest, buf, DIGEST_LEN+9) < 0) {
00063     log_warn(LD_BUG, "Internal error computing digest.");
00064     goto err;
00065   }
00066   if (tor_memneq(expected_digest, request+2+asn1len, DIGEST_LEN)) {
00067     log_warn(LD_PROTOCOL, "Hash of session info was not as expected.");
00068     reason = END_CIRC_REASON_TORPROTOCOL;
00069     goto err;
00070   }
00071   /* Rest of body: signature of previous data */
00072   note_crypto_pk_op(REND_MID);
00073   if (crypto_pk_public_checksig_digest(pk,
00074                                        (char*)request, 2+asn1len+DIGEST_LEN,
00075                                        (char*)(request+2+DIGEST_LEN+asn1len),
00076                                        request_len-(2+DIGEST_LEN+asn1len))<0) {
00077     log_warn(LD_PROTOCOL,
00078              "Incorrect signature on ESTABLISH_INTRO cell; rejecting.");
00079     reason = END_CIRC_REASON_TORPROTOCOL;
00080     goto err;
00081   }
00082 
00083   /* The request is valid.  First, compute the hash of Bob's PK.*/
00084   if (crypto_pk_get_digest(pk, pk_digest)<0) {
00085     log_warn(LD_BUG, "Internal error: couldn't hash public key.");
00086     goto err;
00087   }
00088 
00089   crypto_pk_free(pk); /* don't need it anymore */
00090   pk = NULL; /* so we don't free it again if err */
00091 
00092   base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
00093                 pk_digest, REND_SERVICE_ID_LEN);
00094 
00095   /* Close any other intro circuits with the same pk. */
00096   c = NULL;
00097   while ((c = circuit_get_intro_point(pk_digest))) {
00098     log_info(LD_REND, "Replacing old circuit for service %s",
00099              safe_str(serviceid));
00100     circuit_mark_for_close(TO_CIRCUIT(c), END_CIRC_REASON_FINISHED);
00101     /* Now it's marked, and it won't be returned next time. */
00102   }
00103 
00104   /* Acknowledge the request. */
00105   if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
00106                                    RELAY_COMMAND_INTRO_ESTABLISHED,
00107                                    "", 0, NULL)<0) {
00108     log_info(LD_GENERAL, "Couldn't send INTRO_ESTABLISHED cell.");
00109     goto err;
00110   }
00111 
00112   /* Now, set up this circuit. */
00113   circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
00114   memcpy(circ->rend_token, pk_digest, DIGEST_LEN);
00115 
00116   log_info(LD_REND,
00117            "Established introduction point on circuit %d for service %s",
00118            circ->p_circ_id, safe_str(serviceid));
00119 
00120   return 0;
00121  truncated:
00122   log_warn(LD_PROTOCOL, "Rejecting truncated ESTABLISH_INTRO cell.");
00123   reason = END_CIRC_REASON_TORPROTOCOL;
00124  err:
00125   if (pk) crypto_pk_free(pk);
00126   circuit_mark_for_close(TO_CIRCUIT(circ), reason);
00127   return -1;
00128 }
00129 
00134 int
00135 rend_mid_introduce(or_circuit_t *circ, const uint8_t *request,
00136                    size_t request_len)
00137 {
00138   or_circuit_t *intro_circ;
00139   char serviceid[REND_SERVICE_ID_LEN_BASE32+1];
00140   char nak_body[1];
00141 
00142   log_info(LD_REND, "Received an INTRODUCE1 request on circuit %d",
00143            circ->p_circ_id);
00144 
00145   if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
00146     log_warn(LD_PROTOCOL,
00147              "Rejecting INTRODUCE1 on non-OR or non-edge circuit %d.",
00148              circ->p_circ_id);
00149     goto err;
00150   }
00151 
00152   /* We could change this to MAX_HEX_NICKNAME_LEN now that 0.0.9.x is
00153    * obsolete; however, there isn't much reason to do so, and we're going
00154    * to revise this protocol anyway.
00155    */
00156   if (request_len < (DIGEST_LEN+(MAX_NICKNAME_LEN+1)+REND_COOKIE_LEN+
00157                      DH_KEY_LEN+CIPHER_KEY_LEN+PKCS1_OAEP_PADDING_OVERHEAD)) {
00158     log_warn(LD_PROTOCOL, "Impossibly short INTRODUCE1 cell on circuit %d; "
00159              "responding with nack.",
00160              circ->p_circ_id);
00161     goto err;
00162   }
00163 
00164   base32_encode(serviceid, REND_SERVICE_ID_LEN_BASE32+1,
00165                 (char*)request, REND_SERVICE_ID_LEN);
00166 
00167   /* The first 20 bytes are all we look at: they have a hash of Bob's PK. */
00168   intro_circ = circuit_get_intro_point((char*)request);
00169   if (!intro_circ) {
00170     log_info(LD_REND,
00171              "No intro circ found for INTRODUCE1 cell (%s) from circuit %d; "
00172              "responding with nack.",
00173              safe_str(serviceid), circ->p_circ_id);
00174     goto err;
00175   }
00176 
00177   log_info(LD_REND,
00178            "Sending introduction request for service %s "
00179            "from circ %d to circ %d",
00180            safe_str(serviceid), circ->p_circ_id,
00181            intro_circ->p_circ_id);
00182 
00183   /* Great.  Now we just relay the cell down the circuit. */
00184   if (relay_send_command_from_edge(0, TO_CIRCUIT(intro_circ),
00185                                    RELAY_COMMAND_INTRODUCE2,
00186                                    (char*)request, request_len, NULL)) {
00187     log_warn(LD_GENERAL,
00188              "Unable to send INTRODUCE2 cell to Tor client.");
00189     goto err;
00190   }
00191   /* And sent an ack down Alice's circuit.  Empty body means succeeded. */
00192   if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
00193                                    RELAY_COMMAND_INTRODUCE_ACK,
00194                                    NULL,0,NULL)) {
00195     log_warn(LD_GENERAL, "Unable to send INTRODUCE_ACK cell to Tor client.");
00196     circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
00197     return -1;
00198   }
00199 
00200   return 0;
00201  err:
00202   /* Send the client an NACK */
00203   nak_body[0] = 1;
00204   if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
00205                                    RELAY_COMMAND_INTRODUCE_ACK,
00206                                    nak_body, 1, NULL)) {
00207     log_warn(LD_GENERAL, "Unable to send NAK to Tor client.");
00208     /* Is this right? */
00209     circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
00210   }
00211   return -1;
00212 }
00213 
00217 int
00218 rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request,
00219                               size_t request_len)
00220 {
00221   char hexid[9];
00222   int reason = END_CIRC_REASON_TORPROTOCOL;
00223 
00224   log_info(LD_REND, "Received an ESTABLISH_RENDEZVOUS request on circuit %d",
00225            circ->p_circ_id);
00226 
00227   if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
00228     log_warn(LD_PROTOCOL,
00229              "Tried to establish rendezvous on non-OR or non-edge circuit.");
00230     goto err;
00231   }
00232 
00233   if (request_len != REND_COOKIE_LEN) {
00234     log_warn(LD_PROTOCOL, "Invalid length on ESTABLISH_RENDEZVOUS.");
00235     goto err;
00236   }
00237 
00238   if (circuit_get_rendezvous((char*)request)) {
00239     log_warn(LD_PROTOCOL,
00240              "Duplicate rendezvous cookie in ESTABLISH_RENDEZVOUS.");
00241     goto err;
00242   }
00243 
00244   /* Acknowledge the request. */
00245   if (relay_send_command_from_edge(0,TO_CIRCUIT(circ),
00246                                    RELAY_COMMAND_RENDEZVOUS_ESTABLISHED,
00247                                    "", 0, NULL)<0) {
00248     log_warn(LD_PROTOCOL, "Couldn't send RENDEZVOUS_ESTABLISHED cell.");
00249     reason = END_CIRC_REASON_INTERNAL;
00250     goto err;
00251   }
00252 
00253   circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_POINT_WAITING);
00254   memcpy(circ->rend_token, request, REND_COOKIE_LEN);
00255 
00256   base16_encode(hexid,9,(char*)request,4);
00257 
00258   log_info(LD_REND,
00259            "Established rendezvous point on circuit %d for cookie %s",
00260            circ->p_circ_id, hexid);
00261 
00262   return 0;
00263  err:
00264   circuit_mark_for_close(TO_CIRCUIT(circ), reason);
00265   return -1;
00266 }
00267 
00272 int
00273 rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
00274                     size_t request_len)
00275 {
00276   or_circuit_t *rend_circ;
00277   char hexid[9];
00278   int reason = END_CIRC_REASON_INTERNAL;
00279 
00280   if (circ->_base.purpose != CIRCUIT_PURPOSE_OR || circ->_base.n_conn) {
00281     log_info(LD_REND,
00282              "Tried to complete rendezvous on non-OR or non-edge circuit %d.",
00283              circ->p_circ_id);
00284     reason = END_CIRC_REASON_TORPROTOCOL;
00285     goto err;
00286   }
00287 
00288   if (request_len != REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN) {
00289     log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
00290          "Rejecting RENDEZVOUS1 cell with bad length (%d) on circuit %d.",
00291          (int)request_len, circ->p_circ_id);
00292     reason = END_CIRC_REASON_TORPROTOCOL;
00293     goto err;
00294   }
00295 
00296   base16_encode(hexid, sizeof(hexid), (const char*)request, 4);
00297 
00298   log_info(LD_REND,
00299            "Got request for rendezvous from circuit %d to cookie %s.",
00300            circ->p_circ_id, hexid);
00301 
00302   rend_circ = circuit_get_rendezvous((char*)request);
00303   if (!rend_circ) {
00304     log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
00305          "Rejecting RENDEZVOUS1 cell with unrecognized rendezvous cookie %s.",
00306          hexid);
00307     reason = END_CIRC_REASON_TORPROTOCOL;
00308     goto err;
00309   }
00310 
00311   /* Send the RENDEZVOUS2 cell to Alice. */
00312   if (relay_send_command_from_edge(0, TO_CIRCUIT(rend_circ),
00313                                    RELAY_COMMAND_RENDEZVOUS2,
00314                                    (char*)(request+REND_COOKIE_LEN),
00315                                    request_len-REND_COOKIE_LEN, NULL)) {
00316     log_warn(LD_GENERAL,
00317              "Unable to send RENDEZVOUS2 cell to client on circuit %d.",
00318              rend_circ->p_circ_id);
00319     goto err;
00320   }
00321 
00322   /* Join the circuits. */
00323   log_info(LD_REND,
00324            "Completing rendezvous: circuit %d joins circuit %d (cookie %s)",
00325            circ->p_circ_id, rend_circ->p_circ_id, hexid);
00326 
00327   circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
00328   circuit_change_purpose(TO_CIRCUIT(rend_circ),
00329                          CIRCUIT_PURPOSE_REND_ESTABLISHED);
00330   memset(circ->rend_token, 0, REND_COOKIE_LEN);
00331 
00332   rend_circ->rend_splice = circ;
00333   circ->rend_splice = rend_circ;
00334 
00335   return 0;
00336  err:
00337   circuit_mark_for_close(TO_CIRCUIT(circ), reason);
00338   return -1;
00339 }
00340