/src/PcapPlusPlus/Packet++/src/WireGuardLayer.cpp
Line | Count | Source |
1 | 0 | #define LOG_MODULE PacketLogModuleWireGuardLayer |
2 | | |
3 | | #include "Logger.h" |
4 | | #include "UdpLayer.h" |
5 | | #include "WireGuardLayer.h" |
6 | | #include "EndianPortable.h" |
7 | | #include "GeneralUtils.h" |
8 | | #include <iomanip> |
9 | | |
10 | | namespace pcpp |
11 | | { |
12 | | WireGuardLayer* WireGuardLayer::parseWireGuardLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
13 | 0 | { |
14 | 0 | if (dataLen < sizeof(WireGuardLayer::wg_common_header)) |
15 | 0 | return nullptr; |
16 | 0 | wg_common_header* wgHeader = reinterpret_cast<wg_common_header*>(data); |
17 | |
|
18 | 0 | switch (wgHeader->messageType) |
19 | 0 | { |
20 | 0 | case static_cast<uint8_t>(WireGuardMessageType::HandshakeInitiation): |
21 | 0 | if (!WireGuardHandshakeInitiationLayer::isDataValid(data, dataLen)) |
22 | 0 | return nullptr; |
23 | 0 | return new WireGuardHandshakeInitiationLayer(data, dataLen, prevLayer, packet); |
24 | 0 | case static_cast<uint8_t>(WireGuardMessageType::HandshakeResponse): |
25 | 0 | if (!WireGuardHandshakeResponseLayer::isDataValid(data, dataLen)) |
26 | 0 | return nullptr; |
27 | 0 | return new WireGuardHandshakeResponseLayer(data, dataLen, prevLayer, packet); |
28 | 0 | case static_cast<uint8_t>(WireGuardMessageType::CookieReply): |
29 | 0 | if (!WireGuardCookieReplyLayer::isDataValid(data, dataLen)) |
30 | 0 | return nullptr; |
31 | 0 | return new WireGuardCookieReplyLayer(data, dataLen, prevLayer, packet); |
32 | 0 | case static_cast<uint8_t>(WireGuardMessageType::TransportData): |
33 | 0 | if (!WireGuardTransportDataLayer::isDataValid(data, dataLen)) |
34 | 0 | return nullptr; |
35 | 0 | return new WireGuardTransportDataLayer(data, dataLen, prevLayer, packet); |
36 | 0 | default: |
37 | 0 | return nullptr; |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | | std::string WireGuardLayer::getMessageTypeAsString() const |
42 | 0 | { |
43 | 0 | uint32_t messageType = getMessageType(); |
44 | 0 | switch (messageType) |
45 | 0 | { |
46 | 0 | case static_cast<uint8_t>(WireGuardMessageType::HandshakeInitiation): |
47 | 0 | return "Handshake Initiation"; |
48 | 0 | case static_cast<uint8_t>(WireGuardMessageType::HandshakeResponse): |
49 | 0 | return "Handshake Response"; |
50 | 0 | case static_cast<uint8_t>(WireGuardMessageType::CookieReply): |
51 | 0 | return "Cookie Reply"; |
52 | 0 | case static_cast<uint8_t>(WireGuardMessageType::TransportData): |
53 | 0 | return "Transport Data"; |
54 | 0 | default: |
55 | 0 | return "Unknown"; |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | std::string WireGuardLayer::toString() const |
60 | 0 | { |
61 | 0 | return "WireGuard Layer, " + getMessageTypeAsString() + " message"; |
62 | 0 | } |
63 | | |
64 | | size_t WireGuardLayer::getHeaderLen() const |
65 | 0 | { |
66 | 0 | return m_DataLen; |
67 | 0 | } |
68 | | |
69 | | uint8_t WireGuardLayer::getMessageType() const |
70 | 0 | { |
71 | 0 | return getBasicHeader()->messageType; |
72 | 0 | } |
73 | | |
74 | | uint32_t WireGuardLayer::getReserved() const |
75 | 0 | { |
76 | 0 | uint32_t reservedValue = 0; |
77 | 0 | memcpy(&reservedValue, getBasicHeader()->reserved, 3); |
78 | 0 | return be32toh(reservedValue); |
79 | 0 | } |
80 | | |
81 | | void WireGuardLayer::setReserved(const std::array<uint8_t, 3>& reserved) |
82 | 0 | { |
83 | 0 | wg_common_header* msg = reinterpret_cast<wg_common_header*>(m_Data); |
84 | 0 | memcpy(msg->reserved, reserved.data(), 3); |
85 | 0 | } |
86 | | |
87 | | bool WireGuardLayer::isDataValid(const uint8_t* data, size_t dataLen) |
88 | 0 | { |
89 | 0 | if (dataLen < sizeof(WireGuardLayer::wg_common_header)) |
90 | 0 | return false; |
91 | | |
92 | 0 | uint8_t messageType = data[0]; |
93 | 0 | return messageType >= static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::HandshakeInitiation) && |
94 | 0 | messageType <= static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::TransportData); |
95 | 0 | } |
96 | | |
97 | | static constexpr const char* messageTypeToString(uint8_t messageType) |
98 | 0 | { |
99 | 0 | switch (messageType) |
100 | 0 | { |
101 | 0 | case static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::HandshakeInitiation): |
102 | 0 | return "HandshakeInitiation"; |
103 | 0 | case static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::HandshakeResponse): |
104 | 0 | return "HandshakeResponse"; |
105 | 0 | case static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::CookieReply): |
106 | 0 | return "CookieReply"; |
107 | 0 | case static_cast<uint8_t>(WireGuardLayer::WireGuardMessageType::TransportData): |
108 | 0 | return "TransportData"; |
109 | 0 | default: |
110 | 0 | return "Unknown"; |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | const FieldDescriptor WireGuardLayer::SerializedFields::MessageType{ Layer::SerializedFields::MaxID + 1, |
115 | | "messageType" }; |
116 | | const FieldDescriptor WireGuardLayer::SerializedFields::MessageTypeName{ Layer::SerializedFields::MaxID + 2, |
117 | | "messageTypeName" }; |
118 | | |
119 | | void WireGuardLayer::serializeLayer(ObjectScope& serializer) const |
120 | 0 | { |
121 | 0 | serializer.writeField(SerializedFields::MessageType, getMessageType()); |
122 | 0 | serializer.writeField(SerializedFields::MessageTypeName, messageTypeToString(getMessageType())); |
123 | 0 | } |
124 | | |
125 | | // ~~~~~~~~~~~~~~~~~~~~ |
126 | | // WireGuardHandshakeInitiationLayer |
127 | | // ~~~~~~~~~~~~~~~~~~~~ |
128 | | |
129 | | WireGuardHandshakeInitiationLayer::WireGuardHandshakeInitiationLayer(uint32_t senderIndex, |
130 | | const uint8_t initiatorEphemeral[32], |
131 | | const uint8_t encryptedInitiatorStatic[48], |
132 | | const uint8_t encryptedTimestamp[28], |
133 | | const uint8_t mac1[16], const uint8_t mac2[16]) |
134 | 0 | { |
135 | 0 | allocData(sizeof(wg_handshake_initiation)); |
136 | |
|
137 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
138 | |
|
139 | 0 | msg->messageType = static_cast<uint8_t>(WireGuardMessageType::HandshakeInitiation); |
140 | 0 | memset(msg->reserved, 0, 3); |
141 | 0 | msg->senderIndex = htobe32(senderIndex); |
142 | |
|
143 | 0 | memcpy(msg->initiatorEphemeral, initiatorEphemeral, 32); |
144 | 0 | memcpy(msg->encryptedInitiatorStatic, encryptedInitiatorStatic, 48); |
145 | 0 | memcpy(msg->encryptedTimestamp, encryptedTimestamp, 28); |
146 | 0 | memcpy(msg->mac1, mac1, 16); |
147 | 0 | memcpy(msg->mac2, mac2, 16); |
148 | |
|
149 | 0 | m_Protocol = WireGuard; |
150 | 0 | } |
151 | | |
152 | | uint32_t WireGuardHandshakeInitiationLayer::getSenderIndex() const |
153 | 0 | { |
154 | 0 | return be32toh(getHandshakeInitiationHeader()->senderIndex); |
155 | 0 | } |
156 | | |
157 | | std::array<uint8_t, 32> WireGuardHandshakeInitiationLayer::getInitiatorEphemeral() const |
158 | 0 | { |
159 | 0 | std::array<uint8_t, 32> ephemeralArray; |
160 | 0 | memcpy(ephemeralArray.data(), getHandshakeInitiationHeader()->initiatorEphemeral, 32); |
161 | 0 | return ephemeralArray; |
162 | 0 | } |
163 | | |
164 | | std::array<uint8_t, 48> WireGuardHandshakeInitiationLayer::getEncryptedInitiatorStatic() const |
165 | 0 | { |
166 | 0 | std::array<uint8_t, 48> initArray; |
167 | 0 | memcpy(initArray.data(), getHandshakeInitiationHeader()->encryptedInitiatorStatic, 48); |
168 | 0 | return initArray; |
169 | 0 | } |
170 | | |
171 | | std::array<uint8_t, 28> WireGuardHandshakeInitiationLayer::getEncryptedTimestamp() const |
172 | 0 | { |
173 | 0 | std::array<uint8_t, 28> tsArray; |
174 | 0 | memcpy(tsArray.data(), getHandshakeInitiationHeader()->encryptedTimestamp, 28); |
175 | 0 | return tsArray; |
176 | 0 | } |
177 | | |
178 | | std::array<uint8_t, 16> WireGuardHandshakeInitiationLayer::getMac1() const |
179 | 0 | { |
180 | 0 | std::array<uint8_t, 16> mac1Array; |
181 | 0 | memcpy(mac1Array.data(), getHandshakeInitiationHeader()->mac1, 16); |
182 | 0 | return mac1Array; |
183 | 0 | } |
184 | | |
185 | | std::array<uint8_t, 16> WireGuardHandshakeInitiationLayer::getMac2() const |
186 | 0 | { |
187 | 0 | std::array<uint8_t, 16> mac2Array; |
188 | 0 | memcpy(mac2Array.data(), getHandshakeInitiationHeader()->mac2, 16); |
189 | 0 | return mac2Array; |
190 | 0 | } |
191 | | |
192 | | void WireGuardHandshakeInitiationLayer::setSenderIndex(uint32_t senderIndex) |
193 | 0 | { |
194 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
195 | 0 | msg->senderIndex = htobe32(senderIndex); |
196 | 0 | } |
197 | | |
198 | | void WireGuardHandshakeInitiationLayer::setInitiatorEphemeral(const std::array<uint8_t, 32>& initiatorEphemeral) |
199 | 0 | { |
200 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
201 | 0 | memcpy(msg->initiatorEphemeral, initiatorEphemeral.data(), 32); |
202 | 0 | } |
203 | | |
204 | | void WireGuardHandshakeInitiationLayer::setEncryptedInitiatorStatic( |
205 | | const std::array<uint8_t, 48>& encryptedInitiatorStatic) |
206 | 0 | { |
207 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
208 | 0 | memcpy(msg->encryptedInitiatorStatic, encryptedInitiatorStatic.data(), 48); |
209 | 0 | } |
210 | | |
211 | | void WireGuardHandshakeInitiationLayer::setEncryptedTimestamp(const std::array<uint8_t, 28>& encryptedTimestamp) |
212 | 0 | { |
213 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
214 | 0 | memcpy(msg->encryptedTimestamp, encryptedTimestamp.data(), 28); |
215 | 0 | } |
216 | | |
217 | | void WireGuardHandshakeInitiationLayer::setMac1(const std::array<uint8_t, 16>& mac1) |
218 | 0 | { |
219 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
220 | 0 | memcpy(msg->mac1, mac1.data(), 16); |
221 | 0 | } |
222 | | |
223 | | void WireGuardHandshakeInitiationLayer::setMac2(const std::array<uint8_t, 16>& mac2) |
224 | 0 | { |
225 | 0 | wg_handshake_initiation* msg = reinterpret_cast<wg_handshake_initiation*>(m_Data); |
226 | 0 | memcpy(msg->mac2, mac2.data(), 16); |
227 | 0 | } |
228 | | |
229 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::SenderIndex{ |
230 | | WireGuardLayer::SerializedFields::MaxID + 1, "senderIndex" |
231 | | }; |
232 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::InitiatorEphemeral{ |
233 | | WireGuardLayer::SerializedFields::MaxID + 2, "initiatorEphemeral" |
234 | | }; |
235 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::EncryptedInitiatorStatic{ |
236 | | WireGuardLayer::SerializedFields::MaxID + 3, "encryptedInitiatorStatic" |
237 | | }; |
238 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::EncryptedTimestamp{ |
239 | | WireGuardLayer::SerializedFields::MaxID + 4, "encryptedTimestamp" |
240 | | }; |
241 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::Mac1{ |
242 | | WireGuardLayer::SerializedFields::MaxID + 5, "mac1" |
243 | | }; |
244 | | const FieldDescriptor WireGuardHandshakeInitiationLayer::SerializedFields::Mac2{ |
245 | | WireGuardLayer::SerializedFields::MaxID + 6, "mac2" |
246 | | }; |
247 | | |
248 | | void WireGuardHandshakeInitiationLayer::serializeLayer(ObjectScope& serializer) const |
249 | 0 | { |
250 | 0 | WireGuardLayer::serializeLayer(serializer); |
251 | 0 | serializer.writeHexField(SerializedFields::SenderIndex, getSenderIndex()); |
252 | 0 | { |
253 | 0 | auto value = getInitiatorEphemeral(); |
254 | 0 | serializer.writeField(SerializedFields::InitiatorEphemeral, |
255 | 0 | byteArrayToHexString(value.data(), value.size())); |
256 | 0 | } |
257 | 0 | { |
258 | 0 | auto value = getEncryptedInitiatorStatic(); |
259 | 0 | serializer.writeField(SerializedFields::EncryptedInitiatorStatic, |
260 | 0 | byteArrayToHexString(value.data(), value.size())); |
261 | 0 | } |
262 | 0 | { |
263 | 0 | auto value = getEncryptedTimestamp(); |
264 | 0 | serializer.writeField(SerializedFields::EncryptedTimestamp, |
265 | 0 | byteArrayToHexString(value.data(), value.size())); |
266 | 0 | } |
267 | 0 | { |
268 | 0 | auto value = getMac1(); |
269 | 0 | serializer.writeField(SerializedFields::Mac1, byteArrayToHexString(value.data(), value.size())); |
270 | 0 | } |
271 | 0 | { |
272 | 0 | auto value = getMac2(); |
273 | 0 | serializer.writeField(SerializedFields::Mac2, byteArrayToHexString(value.data(), value.size())); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | // ~~~~~~~~~~~~~~~~~~~~ |
278 | | // WireGuardHandshakeResponseLayer |
279 | | // ~~~~~~~~~~~~~~~~~~~~ |
280 | | |
281 | | WireGuardHandshakeResponseLayer::WireGuardHandshakeResponseLayer(uint32_t senderIndex, uint32_t receiverIndex, |
282 | | const uint8_t responderEphemeral[32], |
283 | | const uint8_t encryptedEmpty[16], |
284 | | const uint8_t mac1[16], const uint8_t mac2[16]) |
285 | 0 | { |
286 | 0 | allocData(sizeof(wg_handshake_response), false); |
287 | |
|
288 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
289 | |
|
290 | 0 | msg->messageType = static_cast<uint8_t>(WireGuardMessageType::HandshakeResponse); |
291 | 0 | memset(msg->reserved, 0, 3); |
292 | 0 | msg->senderIndex = htobe32(senderIndex); |
293 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
294 | 0 | memcpy(msg->responderEphemeral, responderEphemeral, 32); |
295 | 0 | memcpy(msg->encryptedEmpty, encryptedEmpty, 16); |
296 | 0 | memcpy(msg->mac1, mac1, 16); |
297 | 0 | memcpy(msg->mac2, mac2, 16); |
298 | |
|
299 | 0 | m_Protocol = WireGuard; |
300 | 0 | } |
301 | | |
302 | | uint32_t WireGuardHandshakeResponseLayer::getSenderIndex() const |
303 | 0 | { |
304 | 0 | return be32toh(getHandshakeResponseHeader()->senderIndex); |
305 | 0 | } |
306 | | |
307 | | uint32_t WireGuardHandshakeResponseLayer::getReceiverIndex() const |
308 | 0 | { |
309 | 0 | return be32toh(getHandshakeResponseHeader()->receiverIndex); |
310 | 0 | } |
311 | | |
312 | | std::array<uint8_t, 32> WireGuardHandshakeResponseLayer::getResponderEphemeral() const |
313 | 0 | { |
314 | 0 | std::array<uint8_t, 32> responderEphemeralArray; |
315 | 0 | memcpy(responderEphemeralArray.data(), getHandshakeResponseHeader()->responderEphemeral, 32); |
316 | 0 | return responderEphemeralArray; |
317 | 0 | } |
318 | | |
319 | | std::array<uint8_t, 16> WireGuardHandshakeResponseLayer::getEncryptedEmpty() const |
320 | 0 | { |
321 | 0 | std::array<uint8_t, 16> encryptedEmptyArray; |
322 | 0 | memcpy(encryptedEmptyArray.data(), getHandshakeResponseHeader()->encryptedEmpty, 16); |
323 | 0 | return encryptedEmptyArray; |
324 | 0 | } |
325 | | |
326 | | std::array<uint8_t, 16> WireGuardHandshakeResponseLayer::getMac1() const |
327 | 0 | { |
328 | 0 | std::array<uint8_t, 16> mac1Array; |
329 | 0 | memcpy(mac1Array.data(), getHandshakeResponseHeader()->mac1, 16); |
330 | 0 | return mac1Array; |
331 | 0 | } |
332 | | |
333 | | std::array<uint8_t, 16> WireGuardHandshakeResponseLayer::getMac2() const |
334 | 0 | { |
335 | 0 | std::array<uint8_t, 16> mac2Array; |
336 | 0 | memcpy(mac2Array.data(), getHandshakeResponseHeader()->mac2, 16); |
337 | 0 | return mac2Array; |
338 | 0 | } |
339 | | |
340 | | void WireGuardHandshakeResponseLayer::setSenderIndex(uint32_t senderIndex) |
341 | 0 | { |
342 | |
|
343 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
344 | 0 | msg->senderIndex = htobe32(senderIndex); |
345 | 0 | } |
346 | | |
347 | | void WireGuardHandshakeResponseLayer::setReceiverIndex(uint32_t receiverIndex) |
348 | 0 | { |
349 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
350 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
351 | 0 | } |
352 | | |
353 | | void WireGuardHandshakeResponseLayer::setResponderEphemeral(const std::array<uint8_t, 32>& responderEphemeral) |
354 | 0 | { |
355 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
356 | 0 | memcpy(msg->responderEphemeral, responderEphemeral.data(), 32); |
357 | 0 | } |
358 | | |
359 | | void WireGuardHandshakeResponseLayer::setEncryptedEmpty(const std::array<uint8_t, 16>& encryptedEmpty) |
360 | 0 | { |
361 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
362 | 0 | memcpy(msg->encryptedEmpty, encryptedEmpty.data(), 16); |
363 | 0 | } |
364 | | |
365 | | void WireGuardHandshakeResponseLayer::setMac1(const std::array<uint8_t, 16>& mac1) |
366 | 0 | { |
367 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
368 | 0 | memcpy(msg->mac1, mac1.data(), 16); |
369 | 0 | } |
370 | | |
371 | | void WireGuardHandshakeResponseLayer::setMac2(const std::array<uint8_t, 16>& mac2) |
372 | 0 | { |
373 | 0 | wg_handshake_response* msg = reinterpret_cast<wg_handshake_response*>(m_Data); |
374 | 0 | memcpy(msg->mac2, mac2.data(), 16); |
375 | 0 | } |
376 | | |
377 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::SenderIndex{ |
378 | | WireGuardLayer::SerializedFields::MaxID + 1, "senderIndex" |
379 | | }; |
380 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::ReceiverIndex{ |
381 | | WireGuardLayer::SerializedFields::MaxID + 2, "receiverIndex" |
382 | | }; |
383 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::ResponderEphemeral{ |
384 | | WireGuardLayer::SerializedFields::MaxID + 3, "responderEphemeral" |
385 | | }; |
386 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::EncryptedEmpty{ |
387 | | WireGuardLayer::SerializedFields::MaxID + 4, "encryptedEmpty" |
388 | | }; |
389 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::Mac1{ |
390 | | WireGuardLayer::SerializedFields::MaxID + 5, "mac1" |
391 | | }; |
392 | | const FieldDescriptor WireGuardHandshakeResponseLayer::SerializedFields::Mac2{ |
393 | | WireGuardLayer::SerializedFields::MaxID + 6, "mac2" |
394 | | }; |
395 | | |
396 | | void WireGuardHandshakeResponseLayer::serializeLayer(ObjectScope& serializer) const |
397 | 0 | { |
398 | 0 | WireGuardLayer::serializeLayer(serializer); |
399 | 0 | serializer.writeHexField(SerializedFields::SenderIndex, getSenderIndex()); |
400 | 0 | serializer.writeHexField(SerializedFields::ReceiverIndex, getReceiverIndex()); |
401 | 0 | { |
402 | 0 | auto value = getResponderEphemeral(); |
403 | 0 | serializer.writeField(SerializedFields::ResponderEphemeral, |
404 | 0 | byteArrayToHexString(value.data(), value.size())); |
405 | 0 | } |
406 | 0 | { |
407 | 0 | auto value = getEncryptedEmpty(); |
408 | 0 | serializer.writeField(SerializedFields::EncryptedEmpty, byteArrayToHexString(value.data(), value.size())); |
409 | 0 | } |
410 | 0 | { |
411 | 0 | auto value = getMac1(); |
412 | 0 | serializer.writeField(SerializedFields::Mac1, byteArrayToHexString(value.data(), value.size())); |
413 | 0 | } |
414 | 0 | { |
415 | 0 | auto value = getMac2(); |
416 | 0 | serializer.writeField(SerializedFields::Mac2, byteArrayToHexString(value.data(), value.size())); |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | | // ~~~~~~~~~~~~~~~~~~~~ |
421 | | // WireGuardCookieReplyLayer |
422 | | // ~~~~~~~~~~~~~~~~~~~~ |
423 | | |
424 | | WireGuardCookieReplyLayer::WireGuardCookieReplyLayer(uint32_t receiverIndex, const uint8_t nonce[24], |
425 | | const uint8_t encryptedCookie[32]) |
426 | 0 | { |
427 | 0 | allocData(sizeof(wg_cookie_reply)); |
428 | |
|
429 | 0 | wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data); |
430 | |
|
431 | 0 | msg->messageType = static_cast<uint8_t>(WireGuardMessageType::CookieReply); |
432 | 0 | memset(msg->reserved, 0, 3); |
433 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
434 | 0 | memcpy(msg->nonce, nonce, 24); |
435 | 0 | memcpy(msg->encryptedCookie, encryptedCookie, 32); |
436 | |
|
437 | 0 | m_Protocol = WireGuard; |
438 | 0 | } |
439 | | |
440 | | uint32_t WireGuardCookieReplyLayer::getReceiverIndex() const |
441 | 0 | { |
442 | 0 | return be32toh(getCookieReplyHeader()->receiverIndex); |
443 | 0 | } |
444 | | |
445 | | std::array<uint8_t, 24> WireGuardCookieReplyLayer::getNonce() const |
446 | 0 | { |
447 | 0 | std::array<uint8_t, 24> nonceArray; |
448 | 0 | memcpy(nonceArray.data(), getCookieReplyHeader()->nonce, 24); |
449 | 0 | return nonceArray; |
450 | 0 | } |
451 | | |
452 | | std::array<uint8_t, 32> WireGuardCookieReplyLayer::getEncryptedCookie() const |
453 | 0 | { |
454 | 0 | std::array<uint8_t, 32> encryptedCookieArray; |
455 | 0 | memcpy(encryptedCookieArray.data(), getCookieReplyHeader()->encryptedCookie, 32); |
456 | 0 | return encryptedCookieArray; |
457 | 0 | } |
458 | | |
459 | | void WireGuardCookieReplyLayer::setReceiverIndex(uint32_t receiverIndex) |
460 | 0 | { |
461 | 0 | wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data); |
462 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
463 | 0 | } |
464 | | |
465 | | void WireGuardCookieReplyLayer::setNonce(const std::array<uint8_t, 24>& nonce) |
466 | 0 | { |
467 | 0 | wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data); |
468 | 0 | memcpy(msg->nonce, nonce.data(), 24); |
469 | 0 | } |
470 | | |
471 | | void WireGuardCookieReplyLayer::setEncryptedCookie(const std::array<uint8_t, 32>& encryptedCookie) |
472 | 0 | { |
473 | 0 | wg_cookie_reply* msg = reinterpret_cast<wg_cookie_reply*>(m_Data); |
474 | 0 | memcpy(msg->encryptedCookie, encryptedCookie.data(), 32); |
475 | 0 | } |
476 | | |
477 | | const FieldDescriptor WireGuardCookieReplyLayer::SerializedFields::ReceiverIndex{ |
478 | | WireGuardLayer::SerializedFields::MaxID + 1, "receiverIndex" |
479 | | }; |
480 | | const FieldDescriptor WireGuardCookieReplyLayer::SerializedFields::Nonce{ |
481 | | WireGuardLayer::SerializedFields::MaxID + 2, "nonce" |
482 | | }; |
483 | | const FieldDescriptor WireGuardCookieReplyLayer::SerializedFields::EncryptedCookie{ |
484 | | WireGuardLayer::SerializedFields::MaxID + 3, "encryptedCookie" |
485 | | }; |
486 | | |
487 | | void WireGuardCookieReplyLayer::serializeLayer(ObjectScope& serializer) const |
488 | 0 | { |
489 | 0 | WireGuardLayer::serializeLayer(serializer); |
490 | 0 | serializer.writeHexField(SerializedFields::ReceiverIndex, getReceiverIndex()); |
491 | 0 | { |
492 | 0 | auto value = getNonce(); |
493 | 0 | serializer.writeField(SerializedFields::Nonce, byteArrayToHexString(value.data(), value.size())); |
494 | 0 | } |
495 | 0 | { |
496 | 0 | auto value = getEncryptedCookie(); |
497 | 0 | serializer.writeField(SerializedFields::EncryptedCookie, byteArrayToHexString(value.data(), value.size())); |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | | // ~~~~~~~~~~~~~~~~~~~~ |
502 | | // WireGuardTransportDataLayer |
503 | | // ~~~~~~~~~~~~~~~~~~~~ |
504 | | |
505 | | WireGuardTransportDataLayer::WireGuardTransportDataLayer(uint32_t receiverIndex, uint64_t counter, |
506 | | const uint8_t* encryptedData, size_t encryptedDataLen) |
507 | 0 | { |
508 | 0 | const size_t messageLen = sizeof(wg_transport_data) + encryptedDataLen; |
509 | 0 | allocData(messageLen); |
510 | |
|
511 | 0 | wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data); |
512 | |
|
513 | 0 | msg->messageType = static_cast<uint8_t>(WireGuardMessageType::TransportData); |
514 | 0 | memset(msg->reserved, 0, 3); |
515 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
516 | 0 | msg->counter = htobe64(counter); |
517 | 0 | memcpy(m_Data + sizeof(wg_transport_data), encryptedData, encryptedDataLen); |
518 | |
|
519 | 0 | m_Protocol = WireGuard; |
520 | 0 | } |
521 | | |
522 | | uint32_t WireGuardTransportDataLayer::getReceiverIndex() const |
523 | 0 | { |
524 | 0 | return be32toh(getTransportHeader()->receiverIndex); |
525 | 0 | } |
526 | | |
527 | | uint64_t WireGuardTransportDataLayer::getCounter() const |
528 | 0 | { |
529 | 0 | return be64toh(getTransportHeader()->counter); |
530 | 0 | } |
531 | | |
532 | | const uint8_t* WireGuardTransportDataLayer::getEncryptedData() const |
533 | 0 | { |
534 | 0 | return getTransportHeader()->encryptedData; |
535 | 0 | } |
536 | | |
537 | | void WireGuardTransportDataLayer::setReceiverIndex(uint32_t receiverIndex) |
538 | 0 | { |
539 | 0 | wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data); |
540 | 0 | msg->receiverIndex = htobe32(receiverIndex); |
541 | 0 | } |
542 | | |
543 | | void WireGuardTransportDataLayer::setCounter(uint64_t counter) |
544 | 0 | { |
545 | 0 | wg_transport_data* msg = reinterpret_cast<wg_transport_data*>(m_Data); |
546 | 0 | msg->counter = htobe64(counter); |
547 | 0 | } |
548 | | |
549 | | void WireGuardTransportDataLayer::setEncryptedData(const uint8_t* encryptedData, size_t encryptedDataLen) |
550 | 0 | { |
551 | 0 | const size_t headerLen = sizeof(wg_transport_data); |
552 | 0 | const size_t currentLen = m_DataLen - headerLen; |
553 | |
|
554 | 0 | if (encryptedDataLen < currentLen) |
555 | 0 | { |
556 | 0 | if (!shortenLayer(static_cast<int>(headerLen + encryptedDataLen), currentLen - encryptedDataLen)) |
557 | 0 | { |
558 | 0 | PCPP_LOG_ERROR("Couldn't shorten the layer to fit the new encrypted data"); |
559 | 0 | return; |
560 | 0 | } |
561 | 0 | } |
562 | 0 | else if (encryptedDataLen > currentLen) |
563 | 0 | { |
564 | 0 | if (!extendLayer(static_cast<int>(m_DataLen), encryptedDataLen - currentLen)) |
565 | 0 | { |
566 | 0 | PCPP_LOG_ERROR("Couldn't extend the layer to fit the new encrypted data"); |
567 | 0 | return; |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | 0 | memcpy(m_Data + headerLen, encryptedData, encryptedDataLen); |
572 | 0 | } |
573 | | |
574 | | const FieldDescriptor WireGuardTransportDataLayer::SerializedFields::ReceiverIndex{ |
575 | | WireGuardLayer::SerializedFields::MaxID + 1, "receiverIndex" |
576 | | }; |
577 | | const FieldDescriptor WireGuardTransportDataLayer::SerializedFields::Counter{ |
578 | | WireGuardLayer::SerializedFields::MaxID + 2, "counter" |
579 | | }; |
580 | | |
581 | | void WireGuardTransportDataLayer::serializeLayer(ObjectScope& serializer) const |
582 | 0 | { |
583 | 0 | WireGuardLayer::serializeLayer(serializer); |
584 | 0 | serializer.writeHexField(SerializedFields::ReceiverIndex, getReceiverIndex()); |
585 | 0 | serializer.writeField(SerializedFields::Counter, getCounter()); |
586 | 0 | } |
587 | | |
588 | | } // namespace pcpp |