hs_ident.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2017-2019, 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 "lib/crypt_ops/crypto_util.h"
  9. #include "feature/hs/hs_ident.h"
  10. /* Return a newly allocated circuit identifier. The given public key is copied
  11. * identity_pk into the identifier. */
  12. hs_ident_circuit_t *
  13. hs_ident_circuit_new(const ed25519_public_key_t *identity_pk)
  14. {
  15. hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
  16. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  17. return ident;
  18. }
  19. /* Free the given circuit identifier. */
  20. void
  21. hs_ident_circuit_free_(hs_ident_circuit_t *ident)
  22. {
  23. if (ident == NULL) {
  24. return;
  25. }
  26. memwipe(ident, 0, sizeof(hs_ident_circuit_t));
  27. tor_free(ident);
  28. }
  29. /* For a given circuit identifier src, return a newly allocated copy of it.
  30. * This can't fail. */
  31. hs_ident_circuit_t *
  32. hs_ident_circuit_dup(const hs_ident_circuit_t *src)
  33. {
  34. hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
  35. memcpy(ident, src, sizeof(*ident));
  36. return ident;
  37. }
  38. /* For a given directory connection identifier src, return a newly allocated
  39. * copy of it. This can't fail. */
  40. hs_ident_dir_conn_t *
  41. hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
  42. {
  43. hs_ident_dir_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  44. memcpy(ident, src, sizeof(*ident));
  45. return ident;
  46. }
  47. /* Free the given directory connection identifier. */
  48. void
  49. hs_ident_dir_conn_free_(hs_ident_dir_conn_t *ident)
  50. {
  51. if (ident == NULL) {
  52. return;
  53. }
  54. memwipe(ident, 0, sizeof(hs_ident_dir_conn_t));
  55. tor_free(ident);
  56. }
  57. /* Initialized the allocated ident object with identity_pk and blinded_pk.
  58. * None of them can be NULL since a valid directory connection identifier must
  59. * have all fields set. */
  60. void
  61. hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk,
  62. const ed25519_public_key_t *blinded_pk,
  63. hs_ident_dir_conn_t *ident)
  64. {
  65. tor_assert(identity_pk);
  66. tor_assert(blinded_pk);
  67. tor_assert(ident);
  68. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  69. ed25519_pubkey_copy(&ident->blinded_pk, blinded_pk);
  70. }
  71. /* Return a newly allocated edge connection identifier. The given public key
  72. * identity_pk is copied into the identifier. */
  73. hs_ident_edge_conn_t *
  74. hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
  75. {
  76. hs_ident_edge_conn_t *ident = tor_malloc_zero(sizeof(*ident));
  77. ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
  78. return ident;
  79. }
  80. /* Free the given edge connection identifier. */
  81. void
  82. hs_ident_edge_conn_free_(hs_ident_edge_conn_t *ident)
  83. {
  84. if (ident == NULL) {
  85. return;
  86. }
  87. memwipe(ident, 0, sizeof(hs_ident_edge_conn_t));
  88. tor_free(ident);
  89. }
  90. /* Return true if the given ident is valid for an introduction circuit. */
  91. int
  92. hs_ident_intro_circ_is_valid(const hs_ident_circuit_t *ident)
  93. {
  94. if (ident == NULL) {
  95. goto invalid;
  96. }
  97. if (ed25519_public_key_is_zero(&ident->identity_pk)) {
  98. goto invalid;
  99. }
  100. if (ed25519_public_key_is_zero(&ident->intro_auth_pk)) {
  101. goto invalid;
  102. }
  103. /* Valid. */
  104. return 1;
  105. invalid:
  106. return 0;
  107. }