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