hs_ident.c 3.3 KB

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