Skip to content

Commit 44972d0

Browse files
authored
Add setters and tests for WireGuard protocol message fields (#1601)
1 parent 742bc3f commit 44972d0

File tree

5 files changed

+382
-1
lines changed

5 files changed

+382
-1
lines changed

Packet++/header/WireGuardLayer.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ namespace pcpp
117117
*/
118118
uint32_t getReserved() const;
119119

120+
/**
121+
* @param reserved The reserved field to set as a An array containing the 3-byte.
122+
*/
123+
void setReserved(const std::array<uint8_t, 3>& reserved);
124+
120125
/**
121126
* Does nothing for this layer (WireGuard layer is always last)
122127
*/
@@ -247,6 +252,36 @@ namespace pcpp
247252
*/
248253
std::array<uint8_t, 16> getMac2() const;
249254

255+
/**
256+
* @param senderIndex A 32-bit integer representing the sender index.
257+
*/
258+
void setSenderIndex(uint32_t senderIndex);
259+
260+
/**
261+
* @param initiatorEphemeral An array containing the 32-byte initiator ephemeral public key.
262+
*/
263+
void setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral);
264+
265+
/**
266+
* @param encryptedInitiatorStatic An array containing the 48-byte encrypted initiator's static key.
267+
*/
268+
void setEncryptedInitiatorStatic(const std::array<uint8_t, 48>& encryptedInitiatorStatic);
269+
270+
/**
271+
* @param encryptedTimestamp An array containing the 28-byte encrypted timestamp.
272+
*/
273+
void setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp);
274+
275+
/**
276+
* @param mac1 An array containing the 16-byte MAC1 field.
277+
*/
278+
void setMac1(const std::array<uint8_t, 16>& mac1);
279+
280+
/**
281+
* @param mac2 An array containing the 16-byte MAC2 field.
282+
*/
283+
void setMac2(const std::array<uint8_t, 16>& mac2);
284+
250285
// implement abstract methods
251286

252287
/**
@@ -347,6 +382,36 @@ namespace pcpp
347382
*/
348383
std::array<uint8_t, 16> getMac2() const;
349384

385+
/**
386+
* @param senderIndex A 32-bit unsigned integer representing the sender index.
387+
*/
388+
void setSenderIndex(uint32_t senderIndex);
389+
390+
/**
391+
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
392+
*/
393+
void setReceiverIndex(uint32_t receiverIndex);
394+
395+
/**
396+
* @param responderEphemeral An array containing the 32-byte responder ephemeral public key.
397+
*/
398+
void setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral);
399+
400+
/**
401+
* @param encryptedEmpty An array containing the 16-byte encrypted empty field.
402+
*/
403+
void setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty);
404+
405+
/**
406+
* @param mac1 An array containing the 16-byte MAC1 field.
407+
*/
408+
void setMac1(const std::array<uint8_t, 16>& mac1);
409+
410+
/**
411+
* @param mac2 An array containing the 16-byte MAC2 field.
412+
*/
413+
void setMac2(const std::array<uint8_t, 16>& mac2);
414+
350415
// implement abstract methods
351416

352417
/**
@@ -421,6 +486,21 @@ namespace pcpp
421486
*/
422487
std::array<uint8_t, 32> getEncryptedCookie() const;
423488

489+
/**
490+
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
491+
*/
492+
void setReceiverIndex(uint32_t receiverIndex);
493+
494+
/**
495+
* @param nonce An array containing the 24-byte nonce field.
496+
*/
497+
void setNonce(const std::array<uint8_t, 24>& nonce);
498+
499+
/**
500+
* @param encryptedCookie An array containing the 32-byte encrypted cookie.
501+
*/
502+
void setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie);
503+
424504
// implement abstract methods
425505

426506
/**
@@ -497,6 +577,22 @@ namespace pcpp
497577
*/
498578
const uint8_t* getEncryptedData() const;
499579

580+
/**
581+
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
582+
*/
583+
void setReceiverIndex(uint32_t receiverIndex);
584+
585+
/**
586+
* @param counter A 64-bit unsigned integer representing the counter field.
587+
*/
588+
void setCounter(uint64_t counter);
589+
590+
/**
591+
* @param encryptedData A pointer to the encrypted data.
592+
* @param encryptedDataLen The length of the encrypted data.
593+
*/
594+
void setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen);
595+
500596
// implement abstract methods
501597

502598
/**

Packet++/src/WireGuardLayer.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ namespace pcpp
6969
return be32toh(reservedValue);
7070
}
7171

72+
void WireGuardLayer::setReserved(const std::array<uint8_t, 3>& reserved)
73+
{
74+
wg_common_header* msg = reinterpret_cast<wg_common_header*>(m_Data);
75+
memcpy(msg->reserved, reserved.data(), 3);
76+
}
77+
7278
bool WireGuardLayer::isDataValid(const uint8_t* data, size_t dataLen)
7379
{
7480
if (dataLen < sizeof(WireGuardLayer::wg_common_header))
@@ -149,6 +155,43 @@ namespace pcpp
149155
return mac2Array;
150156
}
151157

158+
void WireGuardHandshakeInitiationLayer::setSenderIndex(uint32_t senderIndex)
159+
{
160+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
161+
msg->senderIndex = htobe32(senderIndex);
162+
}
163+
164+
void WireGuardHandshakeInitiationLayer::setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral)
165+
{
166+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
167+
memcpy(msg->initiatorEphemeral, initiatorEphemeral.data(), 32);
168+
}
169+
170+
void WireGuardHandshakeInitiationLayer::setEncryptedInitiatorStatic(
171+
const std::array<uint8_t, 48>& encryptedInitiatorStatic)
172+
{
173+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
174+
memcpy(msg->encryptedInitiatorStatic, encryptedInitiatorStatic.data(), 48);
175+
}
176+
177+
void WireGuardHandshakeInitiationLayer::setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp)
178+
{
179+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
180+
memcpy(msg->encryptedTimestamp, encryptedTimestamp.data(), 28);
181+
}
182+
183+
void WireGuardHandshakeInitiationLayer::setMac1(const std::array<uint8_t, 16>& mac1)
184+
{
185+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
186+
memcpy(msg->mac1, mac1.data(), 16);
187+
}
188+
189+
void WireGuardHandshakeInitiationLayer::setMac2(const std::array<uint8_t, 16>& mac2)
190+
{
191+
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
192+
memcpy(msg->mac2, mac2.data(), 16);
193+
}
194+
152195
// ~~~~~~~~~~~~~~~~~~~~
153196
// WireGuardHandshakeResponseLayer
154197
// ~~~~~~~~~~~~~~~~~~~~
@@ -213,6 +256,43 @@ namespace pcpp
213256
return mac2Array;
214257
}
215258

259+
void WireGuardHandshakeResponseLayer::setSenderIndex(uint32_t senderIndex)
260+
{
261+
262+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
263+
msg->senderIndex = htobe32(senderIndex);
264+
}
265+
266+
void WireGuardHandshakeResponseLayer::setReceiverIndex(uint32_t receiverIndex)
267+
{
268+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
269+
msg->receiverIndex = htobe32(receiverIndex);
270+
}
271+
272+
void WireGuardHandshakeResponseLayer::setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral)
273+
{
274+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
275+
memcpy(msg->responderEphemeral, responderEphemeral.data(), 32);
276+
}
277+
278+
void WireGuardHandshakeResponseLayer::setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty)
279+
{
280+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
281+
memcpy(msg->encryptedEmpty, encryptedEmpty.data(), 16);
282+
}
283+
284+
void WireGuardHandshakeResponseLayer::setMac1(const std::array<uint8_t, 16>& mac1)
285+
{
286+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
287+
memcpy(msg->mac1, mac1.data(), 16);
288+
}
289+
290+
void WireGuardHandshakeResponseLayer::setMac2(const std::array<uint8_t, 16>& mac2)
291+
{
292+
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
293+
memcpy(msg->mac2, mac2.data(), 16);
294+
}
295+
216296
// ~~~~~~~~~~~~~~~~~~~~
217297
// WireGuardCookieReplyLayer
218298
// ~~~~~~~~~~~~~~~~~~~~
@@ -255,6 +335,24 @@ namespace pcpp
255335
return encryptedCookieArray;
256336
}
257337

338+
void WireGuardCookieReplyLayer::setReceiverIndex(uint32_t receiverIndex)
339+
{
340+
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
341+
msg->receiverIndex = htobe32(receiverIndex);
342+
}
343+
344+
void WireGuardCookieReplyLayer::setNonce(const std::array<uint8_t, 24>& nonce)
345+
{
346+
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
347+
memcpy(msg->nonce, nonce.data(), 24);
348+
}
349+
350+
void WireGuardCookieReplyLayer::setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie)
351+
{
352+
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
353+
memcpy(msg->encryptedCookie, encryptedCookie.data(), 32);
354+
}
355+
258356
// ~~~~~~~~~~~~~~~~~~~~
259357
// WireGuardTransportDataLayer
260358
// ~~~~~~~~~~~~~~~~~~~~
@@ -293,4 +391,22 @@ namespace pcpp
293391
return getTransportHeader()->encryptedData;
294392
}
295393

394+
void WireGuardTransportDataLayer::setReceiverIndex(uint32_t receiverIndex)
395+
{
396+
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
397+
msg->receiverIndex = htobe32(receiverIndex);
398+
}
399+
400+
void WireGuardTransportDataLayer::setCounter(uint64_t counter)
401+
{
402+
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
403+
msg->counter = htobe64(counter);
404+
}
405+
406+
void WireGuardTransportDataLayer::setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen)
407+
{
408+
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
409+
memcpy(msg->encryptedData, encryptedData, encryptedDataLen);
410+
}
411+
296412
} // namespace pcpp

Tests/Packet++Test/TestDefinition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,4 @@ PTF_TEST_CASE(WireGuardHandshakeRespParsingTest);
270270
PTF_TEST_CASE(WireGuardCookieReplyParsingTest);
271271
PTF_TEST_CASE(WireGuardTransportDataParsingTest);
272272
PTF_TEST_CASE(WireGuardCreationTest);
273+
PTF_TEST_CASE(WireGuardEditTest);

0 commit comments

Comments
 (0)