Skip to content

Commit

Permalink
Add setters and tests for WireGuard protocol message fields
Browse files Browse the repository at this point in the history
Signed-off-by: Dongjun Na <[email protected]>
  • Loading branch information
nadongjun committed Oct 1, 2024
1 parent 462ccee commit 5d6c131
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 1 deletion.
96 changes: 96 additions & 0 deletions Packet++/header/WireGuardLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ namespace pcpp
*/
uint32_t getReserved() const;

/**
* @param reserved The reserved field to set as a An array containing the 3-byte.
*/
void setReserved(const std::array<uint8_t, 3>& reserved);

/**
* Does nothing for this layer (WireGuard layer is always last)
*/
Expand Down Expand Up @@ -247,6 +252,36 @@ namespace pcpp
*/
std::array<uint8_t, 16> getMac2() const;

/**
* @param senderIndex A 32-bit integer representing the sender index.
*/
void setSenderIndex(uint32_t senderIndex);

/**
* @param initiatorEphemeral An array containing the 32-byte initiator ephemeral public key.
*/
void setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral);

/**
* @param encryptedInitiatorStatic An array containing the 48-byte encrypted initiator's static key.
*/
void setEncryptedInitiatorStatic(const std::array<uint8_t, 48>& encryptedInitiatorStatic);

/**
* @param encryptedTimestamp An array containing the 28-byte encrypted timestamp.
*/
void setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp);

/**
* @param mac1 An array containing the 16-byte MAC1 field.
*/
void setMac1(const std::array<uint8_t, 16>& mac1);

/**
* @param mac2 An array containing the 16-byte MAC2 field.
*/
void setMac2(const std::array<uint8_t, 16>& mac2);

// implement abstract methods

/**
Expand Down Expand Up @@ -347,6 +382,36 @@ namespace pcpp
*/
std::array<uint8_t, 16> getMac2() const;

/**
* @param senderIndex A 32-bit unsigned integer representing the sender index.
*/
void setSenderIndex(uint32_t senderIndex);

/**
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
*/
void setReceiverIndex(uint32_t receiverIndex);

/**
* @param responderEphemeral An array containing the 32-byte responder ephemeral public key.
*/
void setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral);

/**
* @param encryptedEmpty An array containing the 16-byte encrypted empty field.
*/
void setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty);

/**
* @param mac1 An array containing the 16-byte MAC1 field.
*/
void setMac1(const std::array<uint8_t, 16>& mac1);

/**
* @param mac2 An array containing the 16-byte MAC2 field.
*/
void setMac2(const std::array<uint8_t, 16>& mac2);

// implement abstract methods

/**
Expand Down Expand Up @@ -421,6 +486,21 @@ namespace pcpp
*/
std::array<uint8_t, 32> getEncryptedCookie() const;

/**
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
*/
void setReceiverIndex(uint32_t receiverIndex);

/**
* @param nonce An array containing the 24-byte nonce field.
*/
void setNonce(const std::array<uint8_t, 24>& nonce);

/**
* @param encryptedCookie An array containing the 32-byte encrypted cookie.
*/
void setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie);

// implement abstract methods

/**
Expand Down Expand Up @@ -497,6 +577,22 @@ namespace pcpp
*/
const uint8_t* getEncryptedData() const;

/**
* @param receiverIndex A 32-bit unsigned integer representing the receiver index.
*/
void setReceiverIndex(uint32_t receiverIndex);

/**
* @param counter A 64-bit unsigned integer representing the counter field.
*/
void setCounter(uint64_t counter);

/**
* @param encryptedData A pointer to the encrypted data.
* @param encryptedDataLen The length of the encrypted data.
*/
void setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen);

// implement abstract methods

/**
Expand Down
116 changes: 116 additions & 0 deletions Packet++/src/WireGuardLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ namespace pcpp
return be32toh(reservedValue);
}

void WireGuardLayer::setReserved(const std::array<uint8_t, 3>& reserved)
{
wg_common_header* msg = reinterpret_cast<wg_common_header*>(m_Data);
memcpy(msg->reserved, reserved.data(), 3);
}

bool WireGuardLayer::isDataValid(const uint8_t* data, size_t dataLen)
{
if (dataLen < sizeof(WireGuardLayer::wg_common_header))
Expand Down Expand Up @@ -149,6 +155,43 @@ namespace pcpp
return mac2Array;
}

void WireGuardHandshakeInitiationLayer::setSenderIndex(uint32_t senderIndex)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
msg->senderIndex = htobe32(senderIndex);
}

void WireGuardHandshakeInitiationLayer::setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
memcpy(msg->initiatorEphemeral, initiatorEphemeral.data(), 32);
}

void WireGuardHandshakeInitiationLayer::setEncryptedInitiatorStatic(
const std::array<uint8_t, 48>& encryptedInitiatorStatic)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
memcpy(msg->encryptedInitiatorStatic, encryptedInitiatorStatic.data(), 48);
}

void WireGuardHandshakeInitiationLayer::setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
memcpy(msg->encryptedTimestamp, encryptedTimestamp.data(), 28);
}

void WireGuardHandshakeInitiationLayer::setMac1(const std::array<uint8_t, 16>& mac1)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
memcpy(msg->mac1, mac1.data(), 16);
}

void WireGuardHandshakeInitiationLayer::setMac2(const std::array<uint8_t, 16>& mac2)
{
wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data);
memcpy(msg->mac2, mac2.data(), 16);
}

// ~~~~~~~~~~~~~~~~~~~~
// WireGuardHandshakeResponseLayer
// ~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -213,6 +256,43 @@ namespace pcpp
return mac2Array;
}

void WireGuardHandshakeResponseLayer::setSenderIndex(uint32_t senderIndex)
{

wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
msg->senderIndex = htobe32(senderIndex);
}

void WireGuardHandshakeResponseLayer::setReceiverIndex(uint32_t receiverIndex)
{
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
msg->receiverIndex = htobe32(receiverIndex);
}

void WireGuardHandshakeResponseLayer::setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral)
{
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
memcpy(msg->responderEphemeral, responderEphemeral.data(), 32);
}

void WireGuardHandshakeResponseLayer::setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty)
{
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
memcpy(msg->encryptedEmpty, encryptedEmpty.data(), 16);
}

void WireGuardHandshakeResponseLayer::setMac1(const std::array<uint8_t, 16>& mac1)
{
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
memcpy(msg->mac1, mac1.data(), 16);
}

void WireGuardHandshakeResponseLayer::setMac2(const std::array<uint8_t, 16>& mac2)
{
wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data);
memcpy(msg->mac2, mac2.data(), 16);
}

// ~~~~~~~~~~~~~~~~~~~~
// WireGuardCookieReplyLayer
// ~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -255,6 +335,24 @@ namespace pcpp
return encryptedCookieArray;
}

void WireGuardCookieReplyLayer::setReceiverIndex(uint32_t receiverIndex)
{
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
msg->receiverIndex = htobe32(receiverIndex);
}

void WireGuardCookieReplyLayer::setNonce(const std::array<uint8_t, 24>& nonce)
{
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
memcpy(msg->nonce, nonce.data(), 24);
}

void WireGuardCookieReplyLayer::setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie)
{
wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data);
memcpy(msg->encryptedCookie, encryptedCookie.data(), 32);
}

// ~~~~~~~~~~~~~~~~~~~~
// WireGuardTransportDataLayer
// ~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -293,4 +391,22 @@ namespace pcpp
return getTransportHeader()->encryptedData;
}

void WireGuardTransportDataLayer::setReceiverIndex(uint32_t receiverIndex)
{
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
msg->receiverIndex = htobe32(receiverIndex);
}

void WireGuardTransportDataLayer::setCounter(uint64_t counter)
{
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
msg->counter = htobe64(counter);
}

void WireGuardTransportDataLayer::setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen)
{
wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data);
memcpy(msg->encryptedData, encryptedData, encryptedDataLen);
}

} // namespace pcpp
1 change: 1 addition & 0 deletions Tests/Packet++Test/TestDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,4 @@ PTF_TEST_CASE(WireGuardHandshakeRespParsingTest);
PTF_TEST_CASE(WireGuardCookieReplyParsingTest);
PTF_TEST_CASE(WireGuardTransportDataParsingTest);
PTF_TEST_CASE(WireGuardCreationTest);
PTF_TEST_CASE(WireGuardEditTest);
Loading

0 comments on commit 5d6c131

Please sign in to comment.