| 1 |
#include "rist-private.h" |
| 2 |
#include "librist_config.h" |
| 3 |
#include "peer.h" |
| 4 |
#if HAVE_SRP_SUPPORT |
| 5 |
#include "proto/eap.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#if HAVE_SRP_SUPPORT |
| 9 |
void librist_peer_update_rx_passphrase(struct rist_peer *peer, const uint8_t *passphrase, size_t passphrase_len, bool immediate) { |
| 10 |
if (immediate || !peer->supports_otf_passphrase_change) { |
| 11 |
_librist_crypto_psk_set_passphrase(&peer->key_rx, passphrase, passphrase_len); |
| 12 |
_librist_crypto_psk_set_passphrase(&peer->key_rx_odd, passphrase, passphrase_len); |
| 13 |
} else if (peer->supports_otf_passphrase_change) { |
| 14 |
if (!peer->key_rx_odd_active) |
| 15 |
_librist_crypto_psk_set_passphrase(&peer->key_rx, passphrase, passphrase_len); |
| 16 |
else |
| 17 |
_librist_crypto_psk_set_passphrase(&peer->key_rx_odd, passphrase, passphrase_len); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
void librist_peer_get_current_tx_passphrase(struct rist_peer *peer, const uint8_t **passphrase, size_t *passphrase_len) { |
| 22 |
if (peer->key_tx_odd_active) { |
| 23 |
_librist_crypto_psk_get_passphrase(&peer->key_tx_odd, passphrase, passphrase_len); |
| 24 |
return; |
| 25 |
} |
| 26 |
_librist_crypto_psk_get_passphrase(&peer->key_tx, passphrase, passphrase_len); |
| 27 |
} |
| 28 |
|
| 29 |
void librist_peer_update_tx_passphrase(struct rist_peer *peer, const uint8_t *passphrase, size_t passphrase_len, bool immediate) { |
| 30 |
if (immediate || !peer->supports_otf_passphrase_change) { |
| 31 |
_librist_crypto_psk_set_passphrase(&peer->key_tx, passphrase, passphrase_len); |
| 32 |
_librist_crypto_psk_set_passphrase(&peer->key_tx_odd, passphrase, passphrase_len); |
| 33 |
} else if (peer->supports_otf_passphrase_change) { |
| 34 |
if (!peer->key_rx_odd_active) |
| 35 |
_librist_crypto_psk_set_passphrase(&peer->key_tx, passphrase, passphrase_len); |
| 36 |
else |
| 37 |
_librist_crypto_psk_set_passphrase(&peer->key_tx_odd, passphrase, passphrase_len); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
bool librist_peer_should_rollover_passphrase(struct rist_peer *peer) { |
| 42 |
if (!peer->eap_ctx) |
| 43 |
return false; |
| 44 |
if (peer->rolling_over_passphrase) { |
| 45 |
if (!peer->child) { |
| 46 |
return rist_eap_password_sending_done(peer->eap_ctx); |
| 47 |
} |
| 48 |
bool rollover = false; |
| 49 |
struct rist_peer *child = peer->child; |
| 50 |
while (child != NULL) { |
| 51 |
rollover = rist_eap_password_sending_done(peer->eap_ctx); |
| 52 |
if (!rollover) |
| 53 |
break; |
| 54 |
child = child->sibling_next; |
| 55 |
} |
| 56 |
return rollover; |
| 57 |
} |
| 58 |
return rist_eap_may_rollover_tx(peer->eap_ctx); |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
static inline bool _librist_peer_equal_address(uint16_t family, struct sockaddr *A_, struct rist_peer *p) |
| 63 |
{ |
| 64 |
bool result = false; |
| 65 |
|
| 66 |
if (!p) { |
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
if (p->address_family != family) { |
| 71 |
return result; |
| 72 |
} |
| 73 |
|
| 74 |
struct sockaddr *B_ = &p->u.address; |
| 75 |
|
| 76 |
if (family == AF_INET) { |
| 77 |
struct sockaddr_in *a = (struct sockaddr_in *)A_; |
| 78 |
struct sockaddr_in *b = (struct sockaddr_in *)B_; |
| 79 |
result = (a->sin_port == b->sin_port) && |
| 80 |
((!p->receiver_mode && p->listening) || |
| 81 |
(a->sin_addr.s_addr == b->sin_addr.s_addr)); |
| 82 |
if (result && !p->remote_port) |
| 83 |
p->remote_port = a->sin_port; |
| 84 |
} else { |
| 85 |
|
| 86 |
struct sockaddr_in6 *a = (struct sockaddr_in6 *)A_; |
| 87 |
struct sockaddr_in6 *b = (struct sockaddr_in6 *)B_; |
| 88 |
result = a->sin6_port == b->sin6_port && |
| 89 |
((!p->receiver_mode && p->listening) || |
| 90 |
!memcmp(&a->sin6_addr, &b->sin6_addr, sizeof(struct in6_addr))); |
| 91 |
if (result && !p->remote_port) |
| 92 |
p->remote_port = a->sin6_port; |
| 93 |
} |
| 94 |
|
| 95 |
return result; |
| 96 |
} |
| 97 |
|
| 98 |
struct rist_peer * _librist_peer_match_peer_addr(struct rist_peer *p, uint16_t family, struct sockaddr *addr) { |
| 99 |
if (p->listening) { |
| 100 |
if (_librist_peer_equal_address(family, addr, p)) |
| 101 |
return p; |
| 102 |
p = p->child; |
| 103 |
while (p) { |
| 104 |
|
| 105 |
|
| 106 |
if (!p->dead && _librist_peer_equal_address(family, addr, p)) |
| 107 |
return p; |
| 108 |
p = p->sibling_next; |
| 109 |
} |
| 110 |
} else { |
| 111 |
while (p) { |
| 112 |
if (_librist_peer_equal_address(family, addr, p)) |
| 113 |
return p; |
| 114 |
p = p->next; |
| 115 |
} |
| 116 |
} |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|