Skip to content

Commit 77240d3

Browse files
WireGuard::begin(): add support for pre-shared key (PSK)
Pre-shared keys allow using an additional layer of authentication to establish more secure WireGuard tunnels. Update the `WireGuard::begin` methods to accept an optional `presharedKey` parameter and populate the underlying `wireguardif_peer` struct with the provided PSK.
1 parent 887949e commit 77240d3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/WireGuard-ESP32.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class WireGuard
1010
private:
1111
bool _is_initialized = false;
1212
public:
13-
bool begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort);
14-
bool begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort);
13+
bool begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort, const char* presharedKey = NULL);
14+
bool begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort, const char* presharedKey = NULL);
1515
void end();
1616
bool is_initialized() const { return this->_is_initialized; }
1717
};

src/WireGuard.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static uint8_t wireguard_peer_index = WIREGUARDIF_INVALID_INDEX;
4040
UNLOCK_TCPIP_CORE(); \
4141
}
4242

43-
bool WireGuard::begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) {
43+
bool WireGuard::begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort, const char* presharedKey) {
4444
struct wireguardif_init_data wg;
4545
struct wireguardif_peer peer;
4646
ip_addr_t ipaddr = IPADDR4_INIT(static_cast<uint32_t>(localIP));
@@ -105,7 +105,7 @@ bool WireGuard::begin(const IPAddress& localIP, const IPAddress& Subnet, const I
105105
WG_MUTEX_UNLOCK();
106106

107107
peer.public_key = remotePeerPublicKey;
108-
peer.preshared_key = NULL;
108+
peer.preshared_key = presharedKey;
109109
// Allow all IPs through tunnel
110110
{
111111
ip_addr_t allowed_ip = IPADDR4_INIT_BYTES(0, 0, 0, 0);
@@ -138,11 +138,11 @@ bool WireGuard::begin(const IPAddress& localIP, const IPAddress& Subnet, const I
138138
return true;
139139
}
140140

141-
bool WireGuard::begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) {
141+
bool WireGuard::begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort, const char* presharedKey) {
142142
// Maintain compatiblity with old begin
143143
auto subnet = IPAddress(255,255,255,255);
144144
auto gateway = IPAddress(0,0,0,0);
145-
return WireGuard::begin(localIP, subnet, gateway, privateKey, remotePeerAddress, remotePeerPublicKey, remotePeerPort);
145+
return WireGuard::begin(localIP, subnet, gateway, privateKey, remotePeerAddress, remotePeerPublicKey, remotePeerPort, presharedKey);
146146
}
147147

148148
void WireGuard::end() {

0 commit comments

Comments
 (0)