PcapPlusPlus  Next
WireGuardLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "IpAddress.h"
5 #include "MacAddress.h"
6 
8 
11 namespace pcpp
12 {
15  class WireGuardLayer : public Layer
16  {
17  protected:
18 #pragma pack(push, 1)
22  {
24  uint8_t messageType;
26  uint8_t reserved[3];
27  };
28 #pragma pack(pop)
29  static_assert(sizeof(wg_common_header) == 4, "wg_common_header size is not 4 bytes");
30 
31  wg_common_header* getBasicHeader() const
32  {
33  return reinterpret_cast<wg_common_header*>(m_Data);
34  }
35 
36  WireGuardLayer() = default;
37 
38  public:
41  {
43  Unknown = 0,
49  CookieReply = 3,
51  TransportData = 4
52  };
53 
59  WireGuardLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
60  : Layer(data, dataLen, prevLayer, packet, WireGuard)
61  {}
62 
67  static bool isWireGuardPorts(uint16_t portSrc, uint16_t portDst)
68  {
69  return (portSrc == 51820 || portDst == 51820);
70  }
71 
76  static bool isDataValid(const uint8_t* data, size_t dataLen);
77 
84  static WireGuardLayer* parseWireGuardLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
85 
87  std::string getMessageTypeAsString() const;
88 
90  uint8_t getMessageType() const;
91 
93  uint32_t getReserved() const;
94 
96  void setReserved(const std::array<uint8_t, 3>& reserved);
97 
99  void parseNextLayer() override
100  {}
101 
103  size_t getHeaderLen() const override;
104 
106  void computeCalculateFields() override
107  {}
108 
111  std::string toString() const override;
112 
115  {
116  return OsiModelNetworkLayer;
117  }
118 
121  {
123  }
124  };
125 
129  {
130  private:
131 #pragma pack(push, 1)
134  typedef struct wg_handshake_initiation : wg_common_header
135  {
137  uint32_t senderIndex;
139  uint8_t initiatorEphemeral[32];
141  uint8_t encryptedInitiatorStatic[48];
143  uint8_t encryptedTimestamp[28];
145  uint8_t mac1[16];
147  uint8_t mac2[16];
148  } wg_handshake_initiation;
149 #pragma pack(pop)
150  wg_handshake_initiation* getHandshakeInitiationHeader() const
151  {
152  return reinterpret_cast<wg_handshake_initiation*>(getBasicHeader());
153  }
154 
155  public:
161  WireGuardHandshakeInitiationLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
162  : WireGuardLayer(data, dataLen, prevLayer, packet)
163  {}
164 
172  WireGuardHandshakeInitiationLayer(uint32_t senderIndex, const uint8_t initiatorEphemeral[32],
173  const uint8_t encryptedInitiatorStatic[48],
174  const uint8_t encryptedTimestamp[28], const uint8_t mac1[16],
175  const uint8_t mac2[16]);
176 
178  uint32_t getSenderIndex() const;
179 
181  std::array<uint8_t, 32> getInitiatorEphemeral() const;
182 
184  std::array<uint8_t, 48> getEncryptedInitiatorStatic() const;
185 
187  std::array<uint8_t, 28> getEncryptedTimestamp() const;
188 
190  std::array<uint8_t, 16> getMac1() const;
191 
193  std::array<uint8_t, 16> getMac2() const;
194 
196  void setSenderIndex(uint32_t senderIndex);
197 
199  void setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral);
200 
202  void setEncryptedInitiatorStatic(const std::array<uint8_t, 48>& encryptedInitiatorStatic);
203 
205  void setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp);
206 
208  void setMac1(const std::array<uint8_t, 16>& mac1);
209 
211  void setMac2(const std::array<uint8_t, 16>& mac2);
212 
213  // implement abstract methods
214 
217  {
219  }
220  };
221 
225  {
226  private:
227 #pragma pack(push, 1)
230  typedef struct wg_handshake_response : wg_common_header
231  {
233  uint32_t senderIndex;
235  uint32_t receiverIndex;
237  uint8_t responderEphemeral[32];
239  uint8_t encryptedEmpty[16];
241  uint8_t mac1[16];
243  uint8_t mac2[16];
244  } wg_handshake_response;
245 #pragma pack(pop)
246 
247  wg_handshake_response* getHandshakeResponseHeader() const
248  {
249  return reinterpret_cast<wg_handshake_response*>(getBasicHeader());
250  }
251 
252  public:
258  WireGuardHandshakeResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
259  : WireGuardLayer(data, dataLen, prevLayer, packet)
260  {}
261 
269  WireGuardHandshakeResponseLayer(uint32_t senderIndex, uint32_t receiverIndex,
270  const uint8_t responderEphemeral[32], const uint8_t encryptedEmpty[16],
271  const uint8_t mac1[16], const uint8_t mac2[16]);
272 
274  uint32_t getSenderIndex() const;
275 
277  uint32_t getReceiverIndex() const;
278 
280  std::array<uint8_t, 32> getResponderEphemeral() const;
281 
283  std::array<uint8_t, 16> getEncryptedEmpty() const;
284 
286  std::array<uint8_t, 16> getMac1() const;
287 
289  std::array<uint8_t, 16> getMac2() const;
290 
292  void setSenderIndex(uint32_t senderIndex);
293 
295  void setReceiverIndex(uint32_t receiverIndex);
296 
298  void setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral);
299 
301  void setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty);
302 
304  void setMac1(const std::array<uint8_t, 16>& mac1);
305 
307  void setMac2(const std::array<uint8_t, 16>& mac2);
308 
309  // implement abstract methods
310 
313  {
315  }
316  };
317 
321  {
322  private:
323 #pragma pack(push, 1)
326  typedef struct wg_cookie_reply : wg_common_header
327  {
329  uint32_t receiverIndex;
331  uint8_t nonce[24];
333  uint8_t encryptedCookie[32];
334  } wg_cookie_reply;
335 #pragma pack(pop)
336 
337  wg_cookie_reply* getCookieReplyHeader() const
338  {
339  return reinterpret_cast<wg_cookie_reply*>(getBasicHeader());
340  }
341 
342  public:
348  WireGuardCookieReplyLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
349  : WireGuardLayer(data, dataLen, prevLayer, packet)
350  {}
351 
356  WireGuardCookieReplyLayer(uint32_t receiverIndex, const uint8_t nonce[24], const uint8_t encryptedCookie[32]);
357 
359  uint32_t getReceiverIndex() const;
360 
362  std::array<uint8_t, 24> getNonce() const;
363 
365  std::array<uint8_t, 32> getEncryptedCookie() const;
366 
368  void setReceiverIndex(uint32_t receiverIndex);
369 
371  void setNonce(const std::array<uint8_t, 24>& nonce);
372 
374  void setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie);
375 
376  // implement abstract methods
377 
380  {
382  }
383  };
384 
388  {
389  private:
390 #pragma pack(push, 1)
393  typedef struct wg_transport_data : wg_common_header
394  {
396  uint32_t receiverIndex;
398  uint64_t counter;
400  uint8_t encryptedData[0];
401  } wg_transport_data;
402 #pragma pack(pop)
403 
404  wg_transport_data* getTransportHeader() const
405  {
406  return reinterpret_cast<wg_transport_data*>(getBasicHeader());
407  }
408 
409  public:
415  WireGuardTransportDataLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
416  : WireGuardLayer(data, dataLen, prevLayer, packet)
417  {}
418 
424  WireGuardTransportDataLayer(uint32_t receiverIndex, uint64_t counter, const uint8_t* encryptedData,
425  size_t encryptedDataLen);
426 
428  uint32_t getReceiverIndex() const;
429 
431  uint64_t getCounter() const;
432 
434  const uint8_t* getEncryptedData() const;
435 
437  void setReceiverIndex(uint32_t receiverIndex);
438 
440  void setCounter(uint64_t counter);
441 
444  void setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen);
445 
446  // implement abstract methods
447 
450  {
452  }
453  };
454 } // namespace pcpp
Definition: Layer.h:60
Definition: Packet.h:22
Definition: WireGuardLayer.h:129
void setMac2(const std::array< uint8_t, 16 > &mac2)
WireGuardMessageType getWireGuardMessageType() const override
Definition: WireGuardLayer.h:216
void setMac1(const std::array< uint8_t, 16 > &mac1)
std::array< uint8_t, 48 > getEncryptedInitiatorStatic() const
std::array< uint8_t, 16 > getMac1() const
WireGuardHandshakeInitiationLayer(uint32_t senderIndex, const uint8_t initiatorEphemeral[32], const uint8_t encryptedInitiatorStatic[48], const uint8_t encryptedTimestamp[28], const uint8_t mac1[16], const uint8_t mac2[16])
std::array< uint8_t, 16 > getMac2() const
std::array< uint8_t, 28 > getEncryptedTimestamp() const
void setEncryptedInitiatorStatic(const std::array< uint8_t, 48 > &encryptedInitiatorStatic)
WireGuardHandshakeInitiationLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:161
void setEncryptedTimestamp(const std::array< uint8_t, 28 > &encryptedTimestamp)
std::array< uint8_t, 32 > getInitiatorEphemeral() const
void setInitiatorEphemeral(const std::array< uint8_t, 32 > &initiatorEphemeral)
void setSenderIndex(uint32_t senderIndex)
Definition: WireGuardLayer.h:225
std::array< uint8_t, 16 > getMac1() const
WireGuardHandshakeResponseLayer(uint32_t senderIndex, uint32_t receiverIndex, const uint8_t responderEphemeral[32], const uint8_t encryptedEmpty[16], const uint8_t mac1[16], const uint8_t mac2[16])
void setResponderEphemeral(const std::array< uint8_t, 32 > &responderEphemeral)
void setMac2(const std::array< uint8_t, 16 > &mac2)
void setReceiverIndex(uint32_t receiverIndex)
WireGuardMessageType getWireGuardMessageType() const override
Definition: WireGuardLayer.h:312
void setEncryptedEmpty(const std::array< uint8_t, 16 > &encryptedEmpty)
WireGuardHandshakeResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:258
void setMac1(const std::array< uint8_t, 16 > &mac1)
std::array< uint8_t, 16 > getMac2() const
void setSenderIndex(uint32_t senderIndex)
std::array< uint8_t, 32 > getResponderEphemeral() const
std::array< uint8_t, 16 > getEncryptedEmpty() const
Definition: WireGuardLayer.h:16
void setReserved(const std::array< uint8_t, 3 > &reserved)
OsiModelLayer getOsiModelLayer() const override
Definition: WireGuardLayer.h:114
size_t getHeaderLen() const override
static bool isWireGuardPorts(uint16_t portSrc, uint16_t portDst)
Definition: WireGuardLayer.h:67
virtual WireGuardMessageType getWireGuardMessageType() const
Definition: WireGuardLayer.h:120
WireGuardMessageType
WireGuard message types.
Definition: WireGuardLayer.h:41
@ HandshakeInitiation
Handshake Initiation message.
@ HandshakeResponse
Handshake Response message.
@ TransportData
Transport Data message.
@ Unknown
Unknown Initiation message.
static bool isDataValid(const uint8_t *data, size_t dataLen)
std::string toString() const override
uint32_t getReserved() const
std::string getMessageTypeAsString() const
static WireGuardLayer * parseWireGuardLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
void parseNextLayer() override
Does nothing for this layer (WireGuard layer is always last)
Definition: WireGuardLayer.h:99
void computeCalculateFields() override
No fields to compute or update, so this method is left empty.
Definition: WireGuardLayer.h:106
WireGuardLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:59
uint8_t getMessageType() const
Definition: WireGuardLayer.h:388
void setEncryptedData(const uint8_t *encryptedData, size_t encryptedDataLen)
void setCounter(uint64_t counter)
WireGuardTransportDataLayer(uint32_t receiverIndex, uint64_t counter, const uint8_t *encryptedData, size_t encryptedDataLen)
WireGuardTransportDataLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:415
const uint8_t * getEncryptedData() const
void setReceiverIndex(uint32_t receiverIndex)
WireGuardMessageType getWireGuardMessageType() const override
Definition: WireGuardLayer.h:449
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelNetworkLayer
Network layer (layer 3)
Definition: ProtocolType.h:231
const ProtocolType WireGuard
WireGuard protocol.
Definition: ProtocolType.h:215
Definition: WireGuardLayer.h:22
uint8_t reserved[3]
Reserved field (3 bytes)
Definition: WireGuardLayer.h:26
uint8_t messageType
Message type field.
Definition: WireGuardLayer.h:24