hs_ident.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* Initialized the allocated ident object with identity_pk and blinded_pk.
  61. * None of them can be NULL since a valid directory connection identifier must
  62. * have all fields set. */
  63. void
  64. hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk,
  65. const ed25519_public_key_t *blinded_pk,
  66. hs_ident_dir_conn_t *ident)
  67. {
  68. tor_assert(identity_pk);
  69. tor_assert(blinded_pk);
  70. tor_assert(ident);
  71. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  72. ed25519_pubkey_copy(&ident->blinded_pk, blinded_pk);
  73. }
  74. /* Return a newly allocated edge connection identifier. The given public key
  75. * identity_pk is copied into the identifier. */
  76. hs_ident_edge_conn_t *
  77. hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
  78. {
  79. hs_ident_edge_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  80. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  81. return ident;
  82. }
  83. /* Free the given edge connection identifier. */
  84. void
  85. hs_ident_edge_conn_free_(hs_ident_edge_conn_t *ident)
  86. {
  87. if (ident == NULL) {
  88. return;
  89. }
  90. memwipe(ident, 0, sizeof(hs_ident_edge_conn_t));
  91. tor_free(ident);
  92. }
  93. /* Return true if the given ident is valid for an introduction circuit. */
  94. int
  95. hs_ident_intro_circ_is_valid(const hs_ident_circuit_t *ident)
  96. {
  97. if (ident == NULL) {
  98. goto invalid;
  99. }
  100. if (ed25519_public_key_is_zero(&ident->identity_pk)) {
  101. goto invalid;
  102. }
  103. if (ed25519_public_key_is_zero(&ident->intro_auth_pk)) {
  104. goto invalid;
  105. }
  106. /* Valid. */
  107. return 1;
  108. invalid:
  109. return 0;
  110. }