hs_ident.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_ident.c
  5. * \brief Contains circuit and connection identifier code for the whole HS
  6. * subsytem.
  7. **/
  8. #include "hs_ident.h"
  9. /* Return a newly allocated circuit identifier. The given public key is copied
  10. * identity_pk into the identifier. */
  11. hs_ident_circuit_t *
  12. hs_ident_circuit_new(const ed25519_public_key_t *identity_pk,
  13. hs_ident_circuit_type_t circuit_type)
  14. {
  15. tor_assert(circuit_type == HS_IDENT_CIRCUIT_INTRO ||
  16. circuit_type == HS_IDENT_CIRCUIT_RENDEZVOUS);
  17. hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
  18. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  19. ident->circuit_type = circuit_type;
  20. return ident;
  21. }
  22. /* Free the given circuit identifier. */
  23. void
  24. hs_ident_circuit_free(hs_ident_circuit_t *ident)
  25. {
  26. if (ident == NULL) {
  27. return;
  28. }
  29. if (ident->auth_key_type == HS_AUTH_KEY_TYPE_LEGACY) {
  30. crypto_pk_free(ident->auth_rsa_pk);
  31. }
  32. memwipe(ident, 0, sizeof(hs_ident_circuit_t));
  33. tor_free(ident);
  34. }
  35. /* For a given directory connection identifier src, return a newly allocated
  36. * copy of it. This can't fail. */
  37. hs_ident_dir_conn_t *
  38. hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
  39. {
  40. hs_ident_dir_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  41. memcpy(ident, src, sizeof(*ident));
  42. return ident;
  43. }
  44. /* Free the given directory connection identifier. */
  45. void
  46. hs_ident_dir_conn_free(hs_ident_dir_conn_t *ident)
  47. {
  48. if (ident == NULL) {
  49. return;
  50. }
  51. memwipe(ident, 0, sizeof(hs_ident_dir_conn_t));
  52. tor_free(ident);
  53. }
  54. /* Return a newly allocated edge connection identifier. The given public key
  55. * identity_pk is copied into the identifier. */
  56. hs_ident_edge_conn_t *
  57. hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
  58. {
  59. hs_ident_edge_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  60. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  61. return ident;
  62. }
  63. /* Free the given edge connection identifier. */
  64. void
  65. hs_ident_edge_conn_free(hs_ident_edge_conn_t *ident)
  66. {
  67. if (ident == NULL) {
  68. return;
  69. }
  70. memwipe(ident, 0, sizeof(hs_ident_edge_conn_t));
  71. tor_free(ident);
  72. }