Avoid crashing if we call num_usable_bridges() when bridges are not enabled
[tor/appveyor.git] / src / or / hs_ident.c
blobb0e4e36a9b565210f009979b03aaba6db25b50ae
1 /* Copyright (c) 2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file hs_ident.c
6 * \brief Contains circuit and connection identifier code for the whole HS
7 * subsytem.
8 **/
10 #include "hs_ident.h"
12 /* Return a newly allocated circuit identifier. The given public key is copied
13 * identity_pk into the identifier. */
14 hs_ident_circuit_t *
15 hs_ident_circuit_new(const ed25519_public_key_t *identity_pk,
16 hs_ident_circuit_type_t circuit_type)
18 tor_assert(circuit_type == HS_IDENT_CIRCUIT_INTRO ||
19 circuit_type == HS_IDENT_CIRCUIT_RENDEZVOUS);
20 hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
21 ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
22 ident->circuit_type = circuit_type;
23 return ident;
26 /* Free the given circuit identifier. */
27 void
28 hs_ident_circuit_free(hs_ident_circuit_t *ident)
30 if (ident == NULL) {
31 return;
33 memwipe(ident, 0, sizeof(hs_ident_circuit_t));
34 tor_free(ident);
37 /* For a given circuit identifier src, return a newly allocated copy of it.
38 * This can't fail. */
39 hs_ident_circuit_t *
40 hs_ident_circuit_dup(const hs_ident_circuit_t *src)
42 hs_ident_circuit_t *ident = tor_malloc_zero(sizeof(*ident));
43 memcpy(ident, src, sizeof(*ident));
44 return ident;
47 /* For a given directory connection identifier src, return a newly allocated
48 * copy of it. This can't fail. */
49 hs_ident_dir_conn_t *
50 hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src)
52 hs_ident_dir_conn_t *ident = tor_malloc_zero(sizeof(*ident));
53 memcpy(ident, src, sizeof(*ident));
54 return ident;
57 /* Free the given directory connection identifier. */
58 void
59 hs_ident_dir_conn_free(hs_ident_dir_conn_t *ident)
61 if (ident == NULL) {
62 return;
64 memwipe(ident, 0, sizeof(hs_ident_dir_conn_t));
65 tor_free(ident);
68 /* Initialized the allocated ident object with identity_pk and blinded_pk.
69 * None of them can be NULL since a valid directory connection identifier must
70 * have all fields set. */
71 void
72 hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk,
73 const ed25519_public_key_t *blinded_pk,
74 hs_ident_dir_conn_t *ident)
76 tor_assert(identity_pk);
77 tor_assert(blinded_pk);
78 tor_assert(ident);
80 ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
81 ed25519_pubkey_copy(&ident->blinded_pk, blinded_pk);
84 /* Return a newly allocated edge connection identifier. The given public key
85 * identity_pk is copied into the identifier. */
86 hs_ident_edge_conn_t *
87 hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
89 hs_ident_edge_conn_t *ident = tor_malloc_zero(sizeof(*ident));
90 ed25519_pubkey_copy(&ident->identity_pk, identity_pk);
91 return ident;
94 /* Free the given edge connection identifier. */
95 void
96 hs_ident_edge_conn_free(hs_ident_edge_conn_t *ident)
98 if (ident == NULL) {
99 return;
101 memwipe(ident, 0, sizeof(hs_ident_edge_conn_t));
102 tor_free(ident);
105 /* Return true if the given ident is valid for an introduction circuit. */
107 hs_ident_intro_circ_is_valid(const hs_ident_circuit_t *ident)
109 if (ident == NULL) {
110 goto invalid;
113 if (ed25519_public_key_is_zero(&ident->identity_pk)) {
114 goto invalid;
117 if (ed25519_public_key_is_zero(&ident->intro_auth_pk)) {
118 goto invalid;
121 /* Valid. */
122 return 1;
123 invalid:
124 return 0;