hs_ident.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. memwipe(ident, 0, sizeof(hs_ident_circuit_t));
  30. tor_free(ident);
  31. }
  32. /* For a given circuit identifier src, return a newly allocated copy of it.
  33. * This can't fail. */
  34. hs_ident_circuit_t *
  35. hs_ident_circuit_dup(const hs_ident_circuit_t *src)
  36. {
  37. hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
  38. memcpy(ident, src, sizeof(*ident));
  39. return ident;
  40. }
  41. /* For a given directory connection identifier src, return a newly allocated
  42. * copy of it. This can't fail. */
  43. hs_ident_dir_conn_t *
  44. hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
  45. {
  46. hs_ident_dir_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  47. memcpy(ident, src, sizeof(*ident));
  48. return ident;
  49. }
  50. /* Free the given directory connection identifier. */
  51. void
  52. hs_ident_dir_conn_free(hs_ident_dir_conn_t *ident)
  53. {
  54. if (ident == NULL) {
  55. return;
  56. }
  57. memwipe(ident, 0, sizeof(hs_ident_dir_conn_t));
  58. tor_free(ident);
  59. }
  60. /* Return a newly allocated edge connection identifier. The given public key
  61. * identity_pk is copied into the identifier. */
  62. hs_ident_edge_conn_t *
  63. hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
  64. {
  65. hs_ident_edge_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  66. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  67. return ident;
  68. }
  69. /* Free the given edge connection identifier. */
  70. void
  71. hs_ident_edge_conn_free(hs_ident_edge_conn_t *ident)
  72. {
  73. if (ident == NULL) {
  74. return;
  75. }
  76. memwipe(ident, 0, sizeof(hs_ident_edge_conn_t));
  77. tor_free(ident);
  78. }