tor  0.4.1.0-alpha-dev
rendcommon.c
Go to the documentation of this file.
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2  * Copyright (c) 2007-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
11 #define RENDCOMMON_PRIVATE
12 
13 #include "core/or/or.h"
14 #include "core/or/circuitbuild.h"
15 #include "core/or/circuitlist.h"
16 #include "core/or/circuituse.h"
17 #include "app/config/config.h"
21 #include "feature/hs/hs_client.h"
22 #include "feature/hs/hs_common.h"
27 #include "feature/rend/rendmid.h"
28 #include "feature/rend/rendparse.h"
30 #include "feature/stats/rephist.h"
32 #include "feature/relay/router.h"
35 
36 #include "core/or/cpath_build_state_st.h"
37 #include "core/or/crypt_path_st.h"
38 #include "core/or/extend_info_st.h"
39 #include "feature/nodelist/networkstatus_st.h"
40 #include "core/or/origin_circuit_st.h"
41 #include "feature/rend/rend_encoded_v2_service_descriptor_st.h"
42 #include "feature/rend/rend_intro_point_st.h"
43 #include "feature/rend/rend_service_descriptor_st.h"
44 #include "feature/nodelist/routerstatus_st.h"
45 
47 int
48 rend_cmp_service_ids(const char *one, const char *two)
49 {
50  return strcasecmp(one,two);
51 }
52 
55 void
57 {
58  if (!desc)
59  return;
60  if (desc->pk)
61  crypto_pk_free(desc->pk);
62  if (desc->intro_nodes) {
64  rend_intro_point_free(intro););
65  smartlist_free(desc->intro_nodes);
66  }
67  if (desc->successful_uploads) {
68  SMARTLIST_FOREACH(desc->successful_uploads, char *, c, tor_free(c););
69  smartlist_free(desc->successful_uploads);
70  }
71  tor_free(desc);
72 }
73 
76 #define REND_DESC_COOKIE_LEN 16
77 
80 #define REND_REPLICA_LEN 1
81 
86 void
87 rend_get_descriptor_id_bytes(char *descriptor_id_out,
88  const char *service_id,
89  const char *secret_id_part)
90 {
92  crypto_digest_add_bytes(digest, service_id, REND_SERVICE_ID_LEN);
93  crypto_digest_add_bytes(digest, secret_id_part, DIGEST_LEN);
94  crypto_digest_get_digest(digest, descriptor_id_out, DIGEST_LEN);
95  crypto_digest_free(digest);
96 }
97 
103 static void
104 get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period,
105  const char *descriptor_cookie, uint8_t replica)
106 {
108  time_period = htonl(time_period);
109  crypto_digest_add_bytes(digest, (char*)&time_period, sizeof(uint32_t));
110  if (descriptor_cookie) {
111  crypto_digest_add_bytes(digest, descriptor_cookie,
113  }
114  crypto_digest_add_bytes(digest, (const char *)&replica, REND_REPLICA_LEN);
115  crypto_digest_get_digest(digest, secret_id_part, DIGEST_LEN);
116  crypto_digest_free(digest);
117 }
118 
122 static uint32_t
123 get_time_period(time_t now, uint8_t deviation, const char *service_id)
124 {
125  /* The time period is the number of REND_TIME_PERIOD_V2_DESC_VALIDITY
126  * intervals that have passed since the epoch, offset slightly so that
127  * each service's time periods start and end at a fraction of that
128  * period based on their first byte. */
129  return (uint32_t)
130  (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
131  / REND_TIME_PERIOD_V2_DESC_VALIDITY + deviation;
132 }
133 
136 static uint32_t
137 get_seconds_valid(time_t now, const char *service_id)
138 {
139  uint32_t result = REND_TIME_PERIOD_V2_DESC_VALIDITY -
140  ((uint32_t)
141  (now + ((uint8_t) *service_id) * REND_TIME_PERIOD_V2_DESC_VALIDITY / 256)
143  return result;
144 }
145 
152 int
153 rend_compute_v2_desc_id(char *desc_id_out, const char *service_id,
154  const char *descriptor_cookie, time_t now,
155  uint8_t replica)
156 {
157  char service_id_binary[REND_SERVICE_ID_LEN];
158  char secret_id_part[DIGEST_LEN];
159  uint32_t time_period;
160  if (!service_id ||
161  strlen(service_id) != REND_SERVICE_ID_LEN_BASE32) {
162  log_warn(LD_REND, "Could not compute v2 descriptor ID: "
163  "Illegal service ID: %s",
164  safe_str(service_id));
165  return -1;
166  }
168  log_warn(LD_REND, "Could not compute v2 descriptor ID: "
169  "Replica number out of range: %d", replica);
170  return -1;
171  }
172  /* Convert service ID to binary. */
173  if (base32_decode(service_id_binary, REND_SERVICE_ID_LEN,
174  service_id, REND_SERVICE_ID_LEN_BASE32) !=
176  log_warn(LD_REND, "Could not compute v2 descriptor ID: "
177  "Illegal characters or wrong length for service ID: %s",
178  safe_str_client(service_id));
179  return -1;
180  }
181  /* Calculate current time-period. */
182  time_period = get_time_period(now, 0, service_id_binary);
183  /* Calculate secret-id-part = h(time-period | desc-cookie | replica). */
184  get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
185  replica);
186  /* Calculate descriptor ID: H(permanent-id | secret-id-part) */
187  rend_get_descriptor_id_bytes(desc_id_out, service_id_binary, secret_id_part);
188  return 0;
189 }
190 
194 static int
196 {
197  size_t unenc_len;
198  char *unenc = NULL;
199  size_t unenc_written = 0;
200  int i;
201  int r = -1;
202  /* Assemble unencrypted list of introduction points. */
203  unenc_len = smartlist_len(desc->intro_nodes) * 1000; /* too long, but ok. */
204  unenc = tor_malloc_zero(unenc_len);
205  for (i = 0; i < smartlist_len(desc->intro_nodes); i++) {
206  char id_base32[REND_INTRO_POINT_ID_LEN_BASE32 + 1];
207  char *onion_key = NULL;
208  size_t onion_key_len;
209  crypto_pk_t *intro_key;
210  char *service_key = NULL;
211  char *address = NULL;
212  size_t service_key_len;
213  int res;
214  rend_intro_point_t *intro = smartlist_get(desc->intro_nodes, i);
215  /* Obtain extend info with introduction point details. */
216  extend_info_t *info = intro->extend_info;
217  /* Encode introduction point ID. */
218  base32_encode(id_base32, sizeof(id_base32),
219  info->identity_digest, DIGEST_LEN);
220  /* Encode onion key. */
221  if (crypto_pk_write_public_key_to_string(info->onion_key, &onion_key,
222  &onion_key_len) < 0) {
223  log_warn(LD_REND, "Could not write onion key.");
224  goto done;
225  }
226  /* Encode intro key. */
227  intro_key = intro->intro_key;
228  if (!intro_key ||
229  crypto_pk_write_public_key_to_string(intro_key, &service_key,
230  &service_key_len) < 0) {
231  log_warn(LD_REND, "Could not write intro key.");
232  tor_free(onion_key);
233  goto done;
234  }
235  /* Assemble everything for this introduction point. */
236  address = tor_addr_to_str_dup(&info->addr);
237  res = tor_snprintf(unenc + unenc_written, unenc_len - unenc_written,
238  "introduction-point %s\n"
239  "ip-address %s\n"
240  "onion-port %d\n"
241  "onion-key\n%s"
242  "service-key\n%s",
243  id_base32,
244  address,
245  info->port,
246  onion_key,
247  service_key);
248  tor_free(address);
249  tor_free(onion_key);
250  tor_free(service_key);
251  if (res < 0) {
252  log_warn(LD_REND, "Not enough space for writing introduction point "
253  "string.");
254  goto done;
255  }
256  /* Update total number of written bytes for unencrypted intro points. */
257  unenc_written += res;
258  }
259  /* Finalize unencrypted introduction points. */
260  if (unenc_len < unenc_written + 2) {
261  log_warn(LD_REND, "Not enough space for finalizing introduction point "
262  "string.");
263  goto done;
264  }
265  unenc[unenc_written++] = '\n';
266  unenc[unenc_written++] = 0;
267  *encoded = unenc;
268  r = 0;
269  done:
270  if (r<0)
271  tor_free(unenc);
272  return r;
273 }
274 
279 static int
281  size_t *encrypted_len_out,
282  const char *encoded,
283  smartlist_t *client_cookies)
284 {
285  int r = -1, i, pos, enclen, client_blocks;
286  size_t len, client_entries_len;
287  char *enc = NULL, iv[CIPHER_IV_LEN], *client_part = NULL,
288  session_key[CIPHER_KEY_LEN];
289  smartlist_t *encrypted_session_keys = NULL;
290  crypto_digest_t *digest;
291  crypto_cipher_t *cipher;
292  tor_assert(encoded);
293  tor_assert(client_cookies && smartlist_len(client_cookies) > 0);
294 
295  /* Generate session key. */
296  crypto_rand(session_key, CIPHER_KEY_LEN);
297 
298  /* Determine length of encrypted introduction points including session
299  * keys. */
300  client_blocks = 1 + ((smartlist_len(client_cookies) - 1) /
302  client_entries_len = client_blocks * REND_BASIC_AUTH_CLIENT_MULTIPLE *
304  len = 2 + client_entries_len + CIPHER_IV_LEN + strlen(encoded);
305  if (client_blocks >= 256) {
306  log_warn(LD_REND, "Too many clients in introduction point string.");
307  goto done;
308  }
309  enc = tor_malloc_zero(len);
310  enc[0] = 0x01; /* type of authorization. */
311  enc[1] = (uint8_t)client_blocks;
312 
313  /* Encrypt with random session key. */
314  enclen = crypto_cipher_encrypt_with_iv(session_key,
315  enc + 2 + client_entries_len,
316  CIPHER_IV_LEN + strlen(encoded), encoded, strlen(encoded));
317 
318  if (enclen < 0) {
319  log_warn(LD_REND, "Could not encrypt introduction point string.");
320  goto done;
321  }
322  memcpy(iv, enc + 2 + client_entries_len, CIPHER_IV_LEN);
323 
324  /* Encrypt session key for cookies, determine client IDs, and put both
325  * in a smartlist. */
326  encrypted_session_keys = smartlist_new();
327  SMARTLIST_FOREACH_BEGIN(client_cookies, const char *, cookie) {
328  client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
329  /* Encrypt session key. */
330  cipher = crypto_cipher_new(cookie);
331  if (crypto_cipher_encrypt(cipher, client_part +
333  session_key, CIPHER_KEY_LEN) < 0) {
334  log_warn(LD_REND, "Could not encrypt session key for client.");
335  crypto_cipher_free(cipher);
336  tor_free(client_part);
337  goto done;
338  }
339  crypto_cipher_free(cipher);
340 
341  /* Determine client ID. */
342  digest = crypto_digest_new();
345  crypto_digest_get_digest(digest, client_part,
347  crypto_digest_free(digest);
348 
349  /* Put both together. */
350  smartlist_add(encrypted_session_keys, client_part);
351  } SMARTLIST_FOREACH_END(cookie);
352 
353  /* Add some fake client IDs and encrypted session keys. */
354  for (i = (smartlist_len(client_cookies) - 1) %
356  i < REND_BASIC_AUTH_CLIENT_MULTIPLE - 1; i++) {
357  client_part = tor_malloc_zero(REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
358  crypto_rand(client_part, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
359  smartlist_add(encrypted_session_keys, client_part);
360  }
361  /* Sort smartlist and put elements in result in order. */
362  smartlist_sort_digests(encrypted_session_keys);
363  pos = 2;
364  SMARTLIST_FOREACH(encrypted_session_keys, const char *, entry, {
365  memcpy(enc + pos, entry, REND_BASIC_AUTH_CLIENT_ENTRY_LEN);
367  });
368  *encrypted_out = enc;
369  *encrypted_len_out = len;
370  enc = NULL; /* prevent free. */
371  r = 0;
372  done:
373  tor_free(enc);
374  if (encrypted_session_keys) {
375  SMARTLIST_FOREACH(encrypted_session_keys, char *, d, tor_free(d););
376  smartlist_free(encrypted_session_keys);
377  }
378  return r;
379 }
380 
386 static int
388  size_t *encrypted_len_out,
389  const char *encoded,
390  const char *descriptor_cookie)
391 {
392  int r = -1, enclen;
393  char *enc;
394  tor_assert(encoded);
395  tor_assert(descriptor_cookie);
396 
397  enc = tor_malloc_zero(1 + CIPHER_IV_LEN + strlen(encoded));
398  enc[0] = 0x02; /* Auth type */
399  enclen = crypto_cipher_encrypt_with_iv(descriptor_cookie,
400  enc + 1,
401  CIPHER_IV_LEN+strlen(encoded),
402  encoded, strlen(encoded));
403  if (enclen < 0) {
404  log_warn(LD_REND, "Could not encrypt introduction point string.");
405  goto done;
406  }
407  *encrypted_out = enc;
408  *encrypted_len_out = enclen;
409  enc = NULL; /* prevent free */
410  r = 0;
411  done:
412  tor_free(enc);
413  return r;
414 }
415 
418 STATIC int
420 {
421  rend_service_descriptor_t *test_parsed = NULL;
422  char test_desc_id[DIGEST_LEN];
423  char *test_intro_content = NULL;
424  size_t test_intro_size;
425  size_t test_encoded_size;
426  const char *test_next;
427  int res = rend_parse_v2_service_descriptor(&test_parsed, test_desc_id,
428  &test_intro_content,
429  &test_intro_size,
430  &test_encoded_size,
431  &test_next, desc->desc_str, 1);
432  rend_service_descriptor_free(test_parsed);
433  tor_free(test_intro_content);
434  return (res >= 0);
435 }
436 
438 void
441 {
442  if (!desc)
443  return;
444  tor_free(desc->desc_str);
445  tor_free(desc);
446 }
447 
449 void
451 {
452  if (!intro)
453  return;
454 
455  extend_info_free(intro->extend_info);
456  crypto_pk_free(intro->intro_key);
457 
458  if (intro->accepted_intro_rsa_parts != NULL) {
459  replaycache_free(intro->accepted_intro_rsa_parts);
460  }
461 
462  tor_free(intro);
463 }
464 
473 int
475  rend_service_descriptor_t *desc, time_t now,
476  uint8_t period, rend_auth_type_t auth_type,
477  crypto_pk_t *client_key,
478  smartlist_t *client_cookies)
479 {
480  char service_id[DIGEST_LEN];
481  char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
482  uint32_t time_period;
483  char *ipos_base64 = NULL, *ipos = NULL, *ipos_encrypted = NULL,
484  *descriptor_cookie = NULL;
485  size_t ipos_len = 0, ipos_encrypted_len = 0;
486  int k;
487  uint32_t seconds_valid;
488  crypto_pk_t *service_key;
489  if (!desc) {
490  log_warn(LD_BUG, "Could not encode v2 descriptor: No desc given.");
491  return -1;
492  }
493  service_key = (auth_type == REND_STEALTH_AUTH) ? client_key : desc->pk;
494  tor_assert(service_key);
495  if (auth_type == REND_STEALTH_AUTH) {
496  descriptor_cookie = smartlist_get(client_cookies, 0);
497  tor_assert(descriptor_cookie);
498  }
499  /* Obtain service_id from public key. */
500  if (crypto_pk_get_digest(service_key, service_id) < 0) {
501  log_warn(LD_BUG, "Couldn't compute service key digest.");
502  return -1;
503  }
504  /* Calculate current time-period. */
505  time_period = get_time_period(now, period, service_id);
506  /* Determine how many seconds the descriptor will be valid. */
507  seconds_valid = period * REND_TIME_PERIOD_V2_DESC_VALIDITY +
508  get_seconds_valid(now, service_id);
509  /* Assemble, possibly encrypt, and encode introduction points. */
510  if (smartlist_len(desc->intro_nodes) > 0) {
511  if (rend_encode_v2_intro_points(&ipos, desc) < 0) {
512  log_warn(LD_REND, "Encoding of introduction points did not succeed.");
513  return -1;
514  }
515  switch (auth_type) {
516  case REND_NO_AUTH:
517  ipos_len = strlen(ipos);
518  break;
519  case REND_BASIC_AUTH:
520  if (rend_encrypt_v2_intro_points_basic(&ipos_encrypted,
521  &ipos_encrypted_len, ipos,
522  client_cookies) < 0) {
523  log_warn(LD_REND, "Encrypting of introduction points did not "
524  "succeed.");
525  tor_free(ipos);
526  return -1;
527  }
528  tor_free(ipos);
529  ipos = ipos_encrypted;
530  ipos_len = ipos_encrypted_len;
531  break;
532  case REND_STEALTH_AUTH:
533  if (rend_encrypt_v2_intro_points_stealth(&ipos_encrypted,
534  &ipos_encrypted_len, ipos,
535  descriptor_cookie) < 0) {
536  log_warn(LD_REND, "Encrypting of introduction points did not "
537  "succeed.");
538  tor_free(ipos);
539  return -1;
540  }
541  tor_free(ipos);
542  ipos = ipos_encrypted;
543  ipos_len = ipos_encrypted_len;
544  break;
545  default:
546  log_warn(LD_REND|LD_BUG, "Unrecognized authorization type %d",
547  (int)auth_type);
548  tor_free(ipos);
549  return -1;
550  }
551  /* Base64-encode introduction points. */
552  ipos_base64 = tor_calloc(ipos_len, 2);
553  if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len,
554  BASE64_ENCODE_MULTILINE)<0) {
555  log_warn(LD_REND, "Could not encode introduction point string to "
556  "base64. length=%d", (int)ipos_len);
557  tor_free(ipos_base64);
558  tor_free(ipos);
559  return -1;
560  }
561  tor_free(ipos);
562  }
563  /* Encode REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS descriptors. */
564  for (k = 0; k < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; k++) {
565  char secret_id_part[DIGEST_LEN];
566  char secret_id_part_base32[REND_SECRET_ID_PART_LEN_BASE32 + 1];
567  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
568  char *permanent_key = NULL;
569  size_t permanent_key_len;
570  char published[ISO_TIME_LEN+1];
571  int i;
572  char protocol_versions_string[16]; /* max len: "0,1,2,3,4,5,6,7\0" */
573  size_t protocol_versions_written;
574  size_t desc_len;
575  char *desc_str = NULL;
576  int result = 0;
577  size_t written = 0;
578  char desc_digest[DIGEST_LEN];
580  tor_malloc_zero(sizeof(rend_encoded_v2_service_descriptor_t));
581  /* Calculate secret-id-part = h(time-period | cookie | replica). */
582  get_secret_id_part_bytes(secret_id_part, time_period, descriptor_cookie,
583  k);
584  base32_encode(secret_id_part_base32, sizeof(secret_id_part_base32),
585  secret_id_part, DIGEST_LEN);
586  /* Calculate descriptor ID. */
587  rend_get_descriptor_id_bytes(enc->desc_id, service_id, secret_id_part);
588  base32_encode(desc_id_base32, sizeof(desc_id_base32),
589  enc->desc_id, DIGEST_LEN);
590  /* PEM-encode the public key */
591  if (crypto_pk_write_public_key_to_string(service_key, &permanent_key,
592  &permanent_key_len) < 0) {
593  log_warn(LD_BUG, "Could not write public key to string.");
594  rend_encoded_v2_service_descriptor_free(enc);
595  goto err;
596  }
597  /* Encode timestamp. */
598  format_iso_time(published, desc->timestamp);
599  /* Write protocol-versions bitmask to comma-separated value string. */
600  protocol_versions_written = 0;
601  for (i = 0; i < 8; i++) {
602  if (desc->protocols & 1 << i) {
603  tor_snprintf(protocol_versions_string + protocol_versions_written,
604  16 - protocol_versions_written, "%d,", i);
605  protocol_versions_written += 2;
606  }
607  }
608  if (protocol_versions_written)
609  protocol_versions_string[protocol_versions_written - 1] = '\0';
610  else
611  protocol_versions_string[0]= '\0';
612  /* Assemble complete descriptor. */
613  desc_len = 2000 + smartlist_len(desc->intro_nodes) * 1000; /* far too long,
614  but okay.*/
615  enc->desc_str = desc_str = tor_malloc_zero(desc_len);
616  result = tor_snprintf(desc_str, desc_len,
617  "rendezvous-service-descriptor %s\n"
618  "version 2\n"
619  "permanent-key\n%s"
620  "secret-id-part %s\n"
621  "publication-time %s\n"
622  "protocol-versions %s\n",
623  desc_id_base32,
624  permanent_key,
625  secret_id_part_base32,
626  published,
627  protocol_versions_string);
628  tor_free(permanent_key);
629  if (result < 0) {
630  log_warn(LD_BUG, "Descriptor ran out of room.");
631  rend_encoded_v2_service_descriptor_free(enc);
632  goto err;
633  }
634  written = result;
635  /* Add introduction points. */
636  if (ipos_base64) {
637  result = tor_snprintf(desc_str + written, desc_len - written,
638  "introduction-points\n"
639  "-----BEGIN MESSAGE-----\n%s"
640  "-----END MESSAGE-----\n",
641  ipos_base64);
642  if (result < 0) {
643  log_warn(LD_BUG, "could not write introduction points.");
644  rend_encoded_v2_service_descriptor_free(enc);
645  goto err;
646  }
647  written += result;
648  }
649  /* Add signature. */
650  strlcpy(desc_str + written, "signature\n", desc_len - written);
651  written += strlen(desc_str + written);
652  if (crypto_digest(desc_digest, desc_str, written) < 0) {
653  log_warn(LD_BUG, "could not create digest.");
654  rend_encoded_v2_service_descriptor_free(enc);
655  goto err;
656  }
657  if (router_append_dirobj_signature(desc_str + written,
658  desc_len - written,
659  desc_digest, DIGEST_LEN,
660  service_key) < 0) {
661  log_warn(LD_BUG, "Couldn't sign desc.");
662  rend_encoded_v2_service_descriptor_free(enc);
663  goto err;
664  }
665  written += strlen(desc_str+written);
666  if (written+2 > desc_len) {
667  log_warn(LD_BUG, "Could not finish desc.");
668  rend_encoded_v2_service_descriptor_free(enc);
669  goto err;
670  }
671  desc_str[written++] = 0;
672  /* Check if we can parse our own descriptor. */
673  if (!rend_desc_v2_is_parsable(enc)) {
674  log_warn(LD_BUG, "Could not parse my own descriptor: %s", desc_str);
675  rend_encoded_v2_service_descriptor_free(enc);
676  goto err;
677  }
678  smartlist_add(descs_out, enc);
679  /* Add the uploaded descriptor to the local service's descriptor cache */
681  base32_encode(service_id_base32, sizeof(service_id_base32),
682  service_id, REND_SERVICE_ID_LEN);
683  control_event_hs_descriptor_created(service_id_base32, desc_id_base32, k);
684  }
685 
686  log_info(LD_REND, "Successfully encoded a v2 descriptor and "
687  "confirmed that it is parsable.");
688  goto done;
689 
690  err:
692  rend_encoded_v2_service_descriptor_free(d););
693  smartlist_clear(descs_out);
694  seconds_valid = -1;
695 
696  done:
697  tor_free(ipos_base64);
698  return seconds_valid;
699 }
700 
705 int
707 {
708  char buf[DIGEST_LEN];
709  tor_assert(pk);
710  if (crypto_pk_get_digest(pk, buf) < 0)
711  return -1;
713  return 0;
714 }
715 
718 int
719 rend_valid_v2_service_id(const char *query)
720 {
721  if (strlen(query) != REND_SERVICE_ID_LEN_BASE32)
722  return 0;
723 
724  if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN_BASE32)
725  return 0;
726 
727  return 1;
728 }
729 
732 int
733 rend_valid_descriptor_id(const char *query)
734 {
735  if (strlen(query) != REND_DESC_ID_V2_LEN_BASE32) {
736  goto invalid;
737  }
738  if (strspn(query, BASE32_CHARS) != REND_DESC_ID_V2_LEN_BASE32) {
739  goto invalid;
740  }
741 
742  return 1;
743 
744  invalid:
745  return 0;
746 }
747 
750 int
751 rend_valid_client_name(const char *client_name)
752 {
753  size_t len = strlen(client_name);
754  if (len < 1 || len > REND_CLIENTNAME_MAX_LEN) {
755  return 0;
756  }
757  if (strspn(client_name, REND_LEGAL_CLIENTNAME_CHARACTERS) != len) {
758  return 0;
759  }
760 
761  return 1;
762 }
763 
766 void
768  int command, size_t length,
769  const uint8_t *payload)
770 {
771  or_circuit_t *or_circ = NULL;
772  origin_circuit_t *origin_circ = NULL;
773  int r = -2;
774  if (CIRCUIT_IS_ORIGIN(circ)) {
775  origin_circ = TO_ORIGIN_CIRCUIT(circ);
776  if (!layer_hint || layer_hint != origin_circ->cpath->prev) {
777  log_fn(LOG_PROTOCOL_WARN, LD_APP,
778  "Relay cell (rend purpose %d) from wrong hop on origin circ",
779  command);
780  origin_circ = NULL;
781  }
782  } else {
783  or_circ = TO_OR_CIRCUIT(circ);
784  }
785 
786  switch (command) {
787  case RELAY_COMMAND_ESTABLISH_INTRO:
788  if (or_circ)
789  r = hs_intro_received_establish_intro(or_circ,payload,length);
790  break;
791  case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
792  if (or_circ)
793  r = rend_mid_establish_rendezvous(or_circ,payload,length);
794  break;
795  case RELAY_COMMAND_INTRODUCE1:
796  if (or_circ)
797  r = hs_intro_received_introduce1(or_circ,payload,length);
798  break;
799  case RELAY_COMMAND_INTRODUCE2:
800  if (origin_circ)
801  r = hs_service_receive_introduce2(origin_circ,payload,length);
802  break;
803  case RELAY_COMMAND_INTRODUCE_ACK:
804  if (origin_circ)
805  r = hs_client_receive_introduce_ack(origin_circ,payload,length);
806  break;
807  case RELAY_COMMAND_RENDEZVOUS1:
808  if (or_circ)
809  r = rend_mid_rendezvous(or_circ,payload,length);
810  break;
811  case RELAY_COMMAND_RENDEZVOUS2:
812  if (origin_circ)
813  r = hs_client_receive_rendezvous2(origin_circ,payload,length);
814  break;
815  case RELAY_COMMAND_INTRO_ESTABLISHED:
816  if (origin_circ)
817  r = hs_service_receive_intro_established(origin_circ,payload,length);
818  break;
819  case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
820  if (origin_circ)
821  r = hs_client_receive_rendezvous_acked(origin_circ,payload,length);
822  break;
823  default:
825  }
826 
827  if (r == 0 && origin_circ) {
828  /* This was a valid cell. Count it as delivered + overhead. */
829  circuit_read_valid_data(origin_circ, length);
830  }
831 
832  if (r == -2)
833  log_info(LD_PROTOCOL, "Dropping cell (type %d) for wrong circuit type.",
834  command);
835 }
836 
841 int
843  const char *id)
844 {
845  int start, found, n_added = 0, i;
846  networkstatus_t *c = networkstatus_get_latest_consensus();
847  if (!c || !smartlist_len(c->routerstatus_list)) {
848  log_info(LD_REND, "We don't have a consensus, so we can't perform v2 "
849  "rendezvous operations.");
850  return -1;
851  }
852  tor_assert(id);
853  start = networkstatus_vote_find_entry_idx(c, id, &found);
854  if (start == smartlist_len(c->routerstatus_list)) start = 0;
855  i = start;
856  do {
857  routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
858  if (r->is_hs_dir) {
859  smartlist_add(responsible_dirs, r);
860  if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
861  return 0;
862  }
863  if (++i == smartlist_len(c->routerstatus_list))
864  i = 0;
865  } while (i != start);
866 
867  /* Even though we don't have the desired number of hidden service
868  * directories, be happy if we got any. */
869  return smartlist_len(responsible_dirs) ? 0 : -1;
870 }
871 
872 /* Length of the 'extended' auth cookie used to encode auth type before
873  * base64 encoding. */
874 #define REND_DESC_COOKIE_LEN_EXT (REND_DESC_COOKIE_LEN + 1)
875 /* Length of the zero-padded auth cookie when base64 encoded. These two
876  * padding bytes always (A=) are stripped off of the returned cookie. */
877 #define REND_DESC_COOKIE_LEN_EXT_BASE64 (REND_DESC_COOKIE_LEN_BASE64 + 2)
878 
887 char *
888 rend_auth_encode_cookie(const uint8_t *cookie_in, rend_auth_type_t auth_type)
889 {
890  uint8_t extended_cookie[REND_DESC_COOKIE_LEN_EXT];
891  char *cookie_out = tor_malloc_zero(REND_DESC_COOKIE_LEN_EXT_BASE64 + 1);
892  int re;
893 
894  tor_assert(cookie_in);
895 
896  memcpy(extended_cookie, cookie_in, REND_DESC_COOKIE_LEN);
897  extended_cookie[REND_DESC_COOKIE_LEN] = ((int)auth_type - 1) << 4;
898  re = base64_encode(cookie_out, REND_DESC_COOKIE_LEN_EXT_BASE64 + 1,
899  (const char *) extended_cookie, REND_DESC_COOKIE_LEN_EXT,
900  0);
901  tor_assert(re == REND_DESC_COOKIE_LEN_EXT_BASE64);
902 
903  /* Remove the trailing 'A='. Auth type is encoded in the high bits
904  * of the last byte, so the last base64 character will always be zero
905  * (A). This is subtly different behavior from base64_encode_nopad. */
906  cookie_out[REND_DESC_COOKIE_LEN_BASE64] = '\0';
907  memwipe(extended_cookie, 0, sizeof(extended_cookie));
908  return cookie_out;
909 }
910 
923 int
924 rend_auth_decode_cookie(const char *cookie_in, uint8_t *cookie_out,
925  rend_auth_type_t *auth_type_out, char **err_msg_out)
926 {
927  uint8_t descriptor_cookie_decoded[REND_DESC_COOKIE_LEN_EXT + 1] = { 0 };
928  char descriptor_cookie_base64ext[REND_DESC_COOKIE_LEN_EXT_BASE64 + 1];
929  const char *descriptor_cookie = cookie_in;
930  char *err_msg = NULL;
931  int auth_type_val = 0;
932  int res = -1;
933  int decoded_len;
934 
935  size_t len = strlen(descriptor_cookie);
936  if (len == REND_DESC_COOKIE_LEN_BASE64) {
937  /* Add a trailing zero byte to make base64-decoding happy. */
938  tor_snprintf(descriptor_cookie_base64ext,
939  sizeof(descriptor_cookie_base64ext),
940  "%sA=", descriptor_cookie);
941  descriptor_cookie = descriptor_cookie_base64ext;
942  } else if (len != REND_DESC_COOKIE_LEN_EXT_BASE64) {
943  tor_asprintf(&err_msg, "Authorization cookie has wrong length: %s",
944  escaped(cookie_in));
945  goto err;
946  }
947 
948  decoded_len = base64_decode((char *) descriptor_cookie_decoded,
949  sizeof(descriptor_cookie_decoded),
950  descriptor_cookie,
951  REND_DESC_COOKIE_LEN_EXT_BASE64);
952  if (decoded_len != REND_DESC_COOKIE_LEN &&
953  decoded_len != REND_DESC_COOKIE_LEN_EXT) {
954  tor_asprintf(&err_msg, "Authorization cookie has invalid characters: %s",
955  escaped(cookie_in));
956  goto err;
957  }
958 
959  if (auth_type_out) {
960  auth_type_val = (descriptor_cookie_decoded[REND_DESC_COOKIE_LEN] >> 4) + 1;
961  if (auth_type_val < 1 || auth_type_val > 2) {
962  tor_asprintf(&err_msg, "Authorization cookie type is unknown: %s",
963  escaped(cookie_in));
964  goto err;
965  }
966  *auth_type_out = auth_type_val == 1 ? REND_BASIC_AUTH : REND_STEALTH_AUTH;
967  }
968 
969  memcpy(cookie_out, descriptor_cookie_decoded, REND_DESC_COOKIE_LEN);
970  res = 0;
971  err:
972  if (err_msg_out) {
973  *err_msg_out = err_msg;
974  } else {
975  tor_free(err_msg);
976  }
977  memwipe(descriptor_cookie_decoded, 0, sizeof(descriptor_cookie_decoded));
978  memwipe(descriptor_cookie_base64ext, 0, sizeof(descriptor_cookie_base64ext));
979  return res;
980 }
981 
982 /* Is this a rend client or server that allows direct (non-anonymous)
983  * connections?
984  * Onion services can be configured to start in this mode for single onion. */
985 int
986 rend_allow_non_anonymous_connection(const or_options_t* options)
987 {
988  return rend_service_allow_non_anonymous_connection(options);
989 }
990 
991 /* Is this a rend client or server in non-anonymous mode?
992  * Onion services can be configured to start in this mode for single onion. */
993 int
994 rend_non_anonymous_mode_enabled(const or_options_t *options)
995 {
996  return rend_service_non_anonymous_mode_enabled(options);
997 }
998 
999 /* Make sure that tor only builds one-hop circuits when they would not
1000  * compromise user anonymity.
1001  *
1002  * One-hop circuits are permitted in Single Onion modes.
1003  *
1004  * Single Onion modes are also allowed to make multi-hop circuits.
1005  * For example, single onion HSDir circuits are 3-hop to prevent denial of
1006  * service.
1007  */
1008 void
1009 assert_circ_anonymity_ok(const origin_circuit_t *circ,
1010  const or_options_t *options)
1011 {
1012  tor_assert(options);
1013  tor_assert(circ);
1014  tor_assert(circ->build_state);
1015 
1016  if (circ->build_state->onehop_tunnel) {
1017  tor_assert(rend_allow_non_anonymous_connection(options));
1018  }
1019 }
1020 
1021 /* Return 1 iff the given <b>digest</b> of a permenanent hidden service key is
1022  * equal to the digest in the origin circuit <b>ocirc</b> of its rend data .
1023  * If the rend data doesn't exist, 0 is returned. This function is agnostic to
1024  * the rend data version. */
1025 int
1026 rend_circuit_pk_digest_eq(const origin_circuit_t *ocirc,
1027  const uint8_t *digest)
1028 {
1029  size_t rend_pk_digest_len;
1030  const uint8_t *rend_pk_digest;
1031 
1032  tor_assert(ocirc);
1033  tor_assert(digest);
1034 
1035  if (ocirc->rend_data == NULL) {
1036  goto no_match;
1037  }
1038 
1039  rend_pk_digest = rend_data_get_pk_digest(ocirc->rend_data,
1040  &rend_pk_digest_len);
1041  if (tor_memeq(rend_pk_digest, digest, rend_pk_digest_len)) {
1042  goto match;
1043  }
1044  no_match:
1045  return 0;
1046  match:
1047  return 1;
1048 }
Header file for rendcommon.c.
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
rend_data_t * rend_data
Header file containing common data for the whole HS subsytem.
#define REND_DESC_COOKIE_LEN
Definition: rendcommon.c:76
static int rend_encrypt_v2_intro_points_basic(char **encrypted_out, size_t *encrypted_len_out, const char *encoded, smartlist_t *client_cookies)
Definition: rendcommon.c:280
void control_event_hs_descriptor_created(const char *onion_address, const char *desc_id, int replica)
Definition: control.c:7236
int rend_auth_decode_cookie(const char *cookie_in, uint8_t *cookie_out, rend_auth_type_t *auth_type_out, char **err_msg_out)
Definition: rendcommon.c:924
Header file for signing.c.
Common functions for using (pseudo-)random number generators.
unsigned int is_hs_dir
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
int rend_valid_descriptor_id(const char *query)
Definition: rendcommon.c:733
static int rend_encrypt_v2_intro_points_stealth(char **encrypted_out, size_t *encrypted_len_out, const char *encoded, const char *descriptor_cookie)
Definition: rendcommon.c:387
Header file containing client data for the HS subsytem.
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:143
int rend_cmp_service_ids(const char *one, const char *two)
Definition: rendcommon.c:48
int crypto_cipher_encrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
Definition: crypto_cipher.c:88
crypt_path_t * cpath
void smartlist_add(smartlist_t *sl, void *element)
crypto_digest_t * crypto_digest_new(void)
#define REND_DESC_COOKIE_LEN_BASE64
Definition: or.h:370
#define REND_SERVICE_ID_LEN
Definition: or.h:337
int rend_compute_v2_desc_id(char *desc_id_out, const char *service_id, const char *descriptor_cookie, time_t now, uint8_t replica)
Definition: rendcommon.c:153
#define REND_DESC_ID_V2_LEN_BASE32
Definition: or.h:354
Header file for config.c.
static uint32_t get_seconds_valid(time_t now, const char *service_id)
Definition: rendcommon.c:137
int crypto_pk_write_public_key_to_string(crypto_pk_t *env, char **dest, size_t *len)
Definition: crypto_rsa.c:466
#define REND_BASIC_AUTH_CLIENT_ID_LEN
Definition: or.h:374
Header file for replaycache.c.
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:60
#define tor_free(p)
Definition: malloc.h:52
#define tor_fragile_assert()
Definition: util_bug.h:221
#define CIPHER_KEY_LEN
Definition: crypto_cipher.h:22
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
int rend_mid_establish_rendezvous(or_circuit_t *circ, const uint8_t *request, size_t request_len)
Definition: rendmid.c:223
#define REND_CLIENTNAME_MAX_LEN
Definition: or.h:395
rend_auth_type_t
Definition: or.h:402
crypto_pk_t * onion_key
static int rend_encode_v2_intro_points(char **encoded, rend_service_descriptor_t *desc)
Definition: rendcommon.c:195
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags)
Definition: binascii.c:215
Header file for rendmid.c.
#define REND_NUMBER_OF_CONSECUTIVE_REPLICAS
Definition: or.h:351
#define CIPHER_IV_LEN
Definition: crypto_cipher.h:24
#define LD_APP
Definition: log.h:74
extend_info_t * extend_info
int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:90
Common functions for cryptographic routines.
tor_assert(buffer)
static uint32_t get_time_period(time_t now, uint8_t deviation, const char *service_id)
Definition: rendcommon.c:123
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
#define DIGEST_LEN
Definition: digest_sizes.h:20
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
Master header file for Tor-specific functionality.
#define REND_BASIC_AUTH_CLIENT_ENTRY_LEN
Definition: or.h:383
Header file for circuitbuild.c.
#define REND_SECRET_ID_PART_LEN_BASE32
Definition: or.h:358
struct crypto_pk_t * intro_key
Header file for rephist.c.
int rend_valid_v2_service_id(const char *query)
Definition: rendcommon.c:719
static void get_secret_id_part_bytes(char *secret_id_part, uint32_t time_period, const char *descriptor_cookie, uint8_t replica)
Definition: rendcommon.c:104
void rend_get_descriptor_id_bytes(char *descriptor_id_out, const char *service_id, const char *secret_id_part)
Definition: rendcommon.c:87
Header file for circuituse.c.
Header file for hs_intropoint.c.
char * rend_auth_encode_cookie(const uint8_t *cookie_in, rend_auth_type_t auth_type)
Definition: rendcommon.c:888
#define LD_REND
Definition: log.h:80
Header file for circuitlist.c.
Header file for rendservice.c.
#define REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
Definition: or.h:348
int rend_valid_client_name(const char *client_name)
Definition: rendcommon.c:751
#define REND_INTRO_POINT_ID_LEN_BASE32
Definition: or.h:362
smartlist_t * routerstatus_list
STATIC int rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
Definition: rendcommon.c:419
int rend_encode_v2_descriptors(smartlist_t *descs_out, rend_service_descriptor_t *desc, time_t now, uint8_t period, rend_auth_type_t auth_type, crypto_pk_t *client_key, smartlist_t *client_cookies)
Definition: rendcommon.c:474
struct crypt_path_t * prev
Definition: crypt_path_st.h:61
cpath_build_state_t * build_state
void rend_encoded_v2_service_descriptor_free_(rend_encoded_v2_service_descriptor_t *desc)
Definition: rendcommon.c:439
#define REND_SERVICE_ID_LEN_BASE32
Definition: or.h:331
#define REND_BASIC_AUTH_CLIENT_MULTIPLE
Definition: or.h:379
crypto_cipher_t * crypto_cipher_new(const char *key)
Definition: crypto_cipher.c:65
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:163
#define BASE32_CHARS
Definition: binascii.h:53
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1133
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
int crypto_cipher_encrypt_with_iv(const char *key, char *to, size_t tolen, const char *from, size_t fromlen)
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:394
char identity_digest[DIGEST_LEN]
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Header file for router.c.
const char * escaped(const char *s)
Definition: escape.c:126
int networkstatus_vote_find_entry_idx(networkstatus_t *ns, const char *digest, int *found_out)
#define log_fn(severity, domain, args,...)
Definition: log.h:255
int rend_get_service_id(crypto_pk_t *pk, char *out)
Definition: rendcommon.c:706
#define REND_TIME_PERIOD_V2_DESC_VALIDITY
Definition: or.h:340
#define REND_REPLICA_LEN
Definition: rendcommon.c:80
int hid_serv_get_responsible_directories(smartlist_t *responsible_dirs, const char *id)
Definition: rendcommon.c:842
void rend_intro_point_free_(rend_intro_point_t *intro)
Definition: rendcommon.c:450
void smartlist_sort_digests(smartlist_t *sl)
Definition: smartlist.c:824
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:151
void rend_service_descriptor_free_(rend_service_descriptor_t *desc)
Definition: rendcommon.c:56
#define REND_LEGAL_CLIENTNAME_CHARACTERS
Definition: or.h:391
Header file for control.c.
void rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint, int command, size_t length, const uint8_t *payload)
Definition: rendcommon.c:767
struct replaycache_t * accepted_intro_rsa_parts
int router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest, size_t digest_len, crypto_pk_t *private_key)
Definition: signing.c:79
int rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out, char *desc_id_out, char **intro_points_encrypted_out, size_t *intro_points_encrypted_size_out, size_t *encoded_size_out, const char **next_out, const char *desc, int as_hsdir)
Definition: rendparse.c:72
void circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
Definition: circuituse.c:3139
int rend_cache_store_v2_desc_as_service(const char *desc)
Definition: rendcache.c:734
tor_addr_t addr
int rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request, size_t request_len)
Definition: rendmid.c:296
Header file for rendclient.c.
#define LD_PROTOCOL
Definition: log.h:68
void smartlist_clear(smartlist_t *sl)
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:82
Header file for routerlist.c.