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 
13 namespace pcpp
14 {
15 
20  class WireGuardLayer : public Layer
21  {
22  protected:
23 #pragma pack(push, 1)
29  {
31  uint8_t messageType;
33  uint8_t reserved[3];
34  };
35 #pragma pack(pop)
36  wg_common_header* getBasicHeader() const
37  {
38  return reinterpret_cast<wg_common_header*>(m_Data);
39  }
40 
41  WireGuardLayer() = default;
42 
43  public:
48  {
50  Unknown = 0,
56  CookieReply = 3,
58  TransportData = 4
59  };
60 
69  WireGuardLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
70  : Layer(data, dataLen, prevLayer, packet, WireGuard)
71  {}
72 
80  static bool isWireGuardPorts(uint16_t portSrc, uint16_t portDst)
81  {
82  return (portSrc == 51820 || portDst == 51820);
83  }
84 
92  static bool isDataValid(const uint8_t* data, size_t dataLen);
93 
103  static WireGuardLayer* parseWireGuardLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
104 
108  std::string getMessageTypeAsString() const;
109 
113  uint8_t getMessageType() const;
114 
118  uint32_t getReserved() const;
119 
123  void setReserved(const std::array<uint8_t, 3>& reserved);
124 
128  void parseNextLayer() override
129  {}
130 
134  size_t getHeaderLen() const override;
135 
139  void computeCalculateFields() override
140  {}
141 
147  std::string toString() const override;
148 
153  {
154  return OsiModelNetworkLayer;
155  }
156 
161  {
163  }
164  };
165 
171  {
172  private:
173 #pragma pack(push, 1)
178  typedef struct wg_handshake_initiation : wg_common_header
179  {
181  uint32_t senderIndex;
183  uint8_t initiatorEphemeral[32];
185  uint8_t encryptedInitiatorStatic[48];
187  uint8_t encryptedTimestamp[28];
189  uint8_t mac1[16];
191  uint8_t mac2[16];
192  } wg_handshake_initiation;
193 #pragma pack(pop)
194  wg_handshake_initiation* getHandshakeInitiationHeader() const
195  {
196  return reinterpret_cast<wg_handshake_initiation*>(getBasicHeader());
197  }
198 
199  public:
207  WireGuardHandshakeInitiationLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
208  : WireGuardLayer(data, dataLen, prevLayer, packet)
209  {}
210 
220  WireGuardHandshakeInitiationLayer(uint32_t senderIndex, const uint8_t initiatorEphemeral[32],
221  const uint8_t encryptedInitiatorStatic[48],
222  const uint8_t encryptedTimestamp[28], const uint8_t mac1[16],
223  const uint8_t mac2[16]);
224 
228  uint32_t getSenderIndex() const;
229 
233  std::array<uint8_t, 32> getInitiatorEphemeral() const;
234 
238  std::array<uint8_t, 48> getEncryptedInitiatorStatic() const;
239 
243  std::array<uint8_t, 28> getEncryptedTimestamp() const;
244 
248  std::array<uint8_t, 16> getMac1() const;
249 
253  std::array<uint8_t, 16> getMac2() const;
254 
258  void setSenderIndex(uint32_t senderIndex);
259 
263  void setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral);
264 
268  void setEncryptedInitiatorStatic(const std::array<uint8_t, 48>& encryptedInitiatorStatic);
269 
273  void setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp);
274 
278  void setMac1(const std::array<uint8_t, 16>& mac1);
279 
283  void setMac2(const std::array<uint8_t, 16>& mac2);
284 
285  // implement abstract methods
286 
291  {
293  }
294  };
295 
301  {
302  private:
303 #pragma pack(push, 1)
308  typedef struct wg_handshake_response : wg_common_header
309  {
311  uint32_t senderIndex;
313  uint32_t receiverIndex;
315  uint8_t responderEphemeral[32];
317  uint8_t encryptedEmpty[16];
319  uint8_t mac1[16];
321  uint8_t mac2[16];
322  } wg_handshake_response;
323 #pragma pack(pop)
324 
325  wg_handshake_response* getHandshakeResponseHeader() const
326  {
327  return reinterpret_cast<wg_handshake_response*>(getBasicHeader());
328  }
329 
330  public:
338  WireGuardHandshakeResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
339  : WireGuardLayer(data, dataLen, prevLayer, packet)
340  {}
341 
351  WireGuardHandshakeResponseLayer(uint32_t senderIndex, uint32_t receiverIndex,
352  const uint8_t responderEphemeral[32], const uint8_t encryptedEmpty[16],
353  const uint8_t mac1[16], const uint8_t mac2[16]);
354 
358  uint32_t getSenderIndex() const;
359 
363  uint32_t getReceiverIndex() const;
364 
368  std::array<uint8_t, 32> getResponderEphemeral() const;
369 
373  std::array<uint8_t, 16> getEncryptedEmpty() const;
374 
378  std::array<uint8_t, 16> getMac1() const;
379 
383  std::array<uint8_t, 16> getMac2() const;
384 
388  void setSenderIndex(uint32_t senderIndex);
389 
393  void setReceiverIndex(uint32_t receiverIndex);
394 
398  void setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral);
399 
403  void setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty);
404 
408  void setMac1(const std::array<uint8_t, 16>& mac1);
409 
413  void setMac2(const std::array<uint8_t, 16>& mac2);
414 
415  // implement abstract methods
416 
421  {
423  }
424  };
425 
431  {
432  private:
433 #pragma pack(push, 1)
438  typedef struct wg_cookie_reply : wg_common_header
439  {
441  uint32_t receiverIndex;
443  uint8_t nonce[24];
445  uint8_t encryptedCookie[32];
446  } wg_cookie_reply;
447 #pragma pack(pop)
448 
449  wg_cookie_reply* getCookieReplyHeader() const
450  {
451  return reinterpret_cast<wg_cookie_reply*>(getBasicHeader());
452  }
453 
454  public:
462  WireGuardCookieReplyLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
463  : WireGuardLayer(data, dataLen, prevLayer, packet)
464  {}
465 
472  WireGuardCookieReplyLayer(uint32_t receiverIndex, const uint8_t nonce[24], const uint8_t encryptedCookie[32]);
473 
477  uint32_t getReceiverIndex() const;
478 
482  std::array<uint8_t, 24> getNonce() const;
483 
487  std::array<uint8_t, 32> getEncryptedCookie() const;
488 
492  void setReceiverIndex(uint32_t receiverIndex);
493 
497  void setNonce(const std::array<uint8_t, 24>& nonce);
498 
502  void setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie);
503 
504  // implement abstract methods
505 
510  {
512  }
513  };
514 
520  {
521  private:
522 #pragma pack(push, 1)
527  typedef struct wg_transport_data : wg_common_header
528  {
530  uint32_t receiverIndex;
532  uint64_t counter;
534  uint8_t encryptedData[0];
535  } wg_transport_data;
536 #pragma pack(pop)
537 
538  wg_transport_data* getTransportHeader() const
539  {
540  return reinterpret_cast<wg_transport_data*>(getBasicHeader());
541  }
542 
543  public:
551  WireGuardTransportDataLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
552  : WireGuardLayer(data, dataLen, prevLayer, packet)
553  {}
554 
562  WireGuardTransportDataLayer(uint32_t receiverIndex, uint64_t counter, const uint8_t* encryptedData,
563  size_t encryptedDataLen);
564 
568  uint32_t getReceiverIndex() const;
569 
573  uint64_t getCounter() const;
574 
578  const uint8_t* getEncryptedData() const;
579 
583  void setReceiverIndex(uint32_t receiverIndex);
584 
588  void setCounter(uint64_t counter);
589 
594  void setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen);
595 
596  // implement abstract methods
597 
602  {
604  }
605  };
606 } // namespace pcpp
Definition: Layer.h:69
Definition: Packet.h:27
Definition: WireGuardLayer.h:171
void setMac2(const std::array< uint8_t, 16 > &mac2)
WireGuardMessageType getWireGuardMessageType() const override
Definition: WireGuardLayer.h:290
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:207
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:301
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:420
void setEncryptedEmpty(const std::array< uint8_t, 16 > &encryptedEmpty)
WireGuardHandshakeResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:338
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:21
void setReserved(const std::array< uint8_t, 3 > &reserved)
OsiModelLayer getOsiModelLayer() const override
Definition: WireGuardLayer.h:152
size_t getHeaderLen() const override
static bool isWireGuardPorts(uint16_t portSrc, uint16_t portDst)
Definition: WireGuardLayer.h:80
virtual WireGuardMessageType getWireGuardMessageType() const
Definition: WireGuardLayer.h:160
WireGuardMessageType
Definition: WireGuardLayer.h:48
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
Definition: WireGuardLayer.h:128
void computeCalculateFields() override
Definition: WireGuardLayer.h:139
WireGuardLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: WireGuardLayer.h:69
uint8_t getMessageType() const
Definition: WireGuardLayer.h:520
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:551
const uint8_t * getEncryptedData() const
void setReceiverIndex(uint32_t receiverIndex)
WireGuardMessageType getWireGuardMessageType() const override
Definition: WireGuardLayer.h:601
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelNetworkLayer
Definition: ProtocolType.h:370
Definition: WireGuardLayer.h:29
uint8_t reserved[3]
Definition: WireGuardLayer.h:33
uint8_t messageType
Definition: WireGuardLayer.h:31