/src/wireshark/epan/dissectors/packet-wireguard.c
Line | Count | Source |
1 | | /* packet-wireguard.c |
2 | | * Routines for WireGuard dissection |
3 | | * Copyright 2018, Peter Wu <peter@lekensteyn.nl> |
4 | | * |
5 | | * Wireshark - Network traffic analyzer |
6 | | * By Gerald Combs <gerald@wireshark.org> |
7 | | * Copyright 1998 Gerald Combs |
8 | | * |
9 | | * SPDX-License-Identifier: GPL-2.0-or-later |
10 | | */ |
11 | | |
12 | | /* |
13 | | * Protocol details: https://www.wireguard.com/protocol/ |
14 | | */ |
15 | | |
16 | | #include <config.h> |
17 | 0 | #define WS_LOG_DOMAIN "packet-wireguard" |
18 | | |
19 | | #include <errno.h> |
20 | | |
21 | | #include <epan/packet.h> |
22 | | #include <epan/expert.h> |
23 | | #include <epan/prefs.h> |
24 | | #include <epan/proto_data.h> |
25 | | #include <epan/conversation.h> |
26 | | #include <epan/uat.h> |
27 | | #include <wsutil/file_util.h> |
28 | | #include <wsutil/filesystem.h> |
29 | | #include <wsutil/wsgcrypt.h> |
30 | | #include <wsutil/curve25519.h> |
31 | | #include <wsutil/wslog.h> |
32 | | #include <wsutil/array.h> |
33 | | #include <epan/secrets.h> |
34 | | #include <wiretap/secrets-types.h> |
35 | | |
36 | | void proto_reg_handoff_wg(void); |
37 | | void proto_register_wg(void); |
38 | | |
39 | | static int proto_wg; |
40 | | static int hf_wg_type; |
41 | | static int hf_wg_reserved; |
42 | | static int hf_wg_sender; |
43 | | static int hf_wg_ephemeral; |
44 | | static int hf_wg_encrypted_static; |
45 | | static int hf_wg_static; |
46 | | static int hf_wg_encrypted_timestamp; |
47 | | static int hf_wg_timestamp_tai64_label; |
48 | | static int hf_wg_timestamp_nanoseconds; |
49 | | static int hf_wg_timestamp_value; |
50 | | static int hf_wg_mac1; |
51 | | static int hf_wg_mac2; |
52 | | static int hf_wg_receiver; |
53 | | static int hf_wg_encrypted_empty; |
54 | | static int hf_wg_handshake_ok; |
55 | | static int hf_wg_nonce; |
56 | | static int hf_wg_encrypted_cookie; |
57 | | static int hf_wg_counter; |
58 | | static int hf_wg_encrypted_packet; |
59 | | static int hf_wg_stream; |
60 | | static int hf_wg_response_in; |
61 | | static int hf_wg_response_to; |
62 | | static int hf_wg_receiver_pubkey; |
63 | | static int hf_wg_receiver_pubkey_known_privkey; |
64 | | static int hf_wg_ephemeral_known_privkey; |
65 | | static int hf_wg_static_known_pubkey; |
66 | | static int hf_wg_static_known_privkey; |
67 | | |
68 | | static int ett_wg; |
69 | | static int ett_timestamp; |
70 | | static int ett_key_info; |
71 | | |
72 | | static expert_field ei_wg_bad_packet_length; |
73 | | static expert_field ei_wg_keepalive; |
74 | | static expert_field ei_wg_decryption_error; |
75 | | static expert_field ei_wg_decryption_unsupported; |
76 | | |
77 | | static bool pref_dissect_packet = true; |
78 | | static const char *pref_keylog_file; |
79 | | |
80 | | static dissector_handle_t ip_handle; |
81 | | static dissector_handle_t wg_handle; |
82 | | |
83 | | static bool wg_decryption_supported; |
84 | | |
85 | | // Length of AEAD authentication tag |
86 | 12 | #define AUTH_TAG_LENGTH 16 |
87 | | |
88 | | typedef enum { |
89 | | WG_TYPE_HANDSHAKE_INITIATION = 1, |
90 | | WG_TYPE_HANDSHAKE_RESPONSE = 2, |
91 | | WG_TYPE_COOKIE_REPLY = 3, |
92 | | WG_TYPE_TRANSPORT_DATA = 4 |
93 | | } wg_message_type; |
94 | | |
95 | | static const value_string wg_type_names[] = { |
96 | | { 0x01, "Handshake Initiation" }, |
97 | | { 0x02, "Handshake Response" }, |
98 | | { 0x03, "Cookie Reply" }, |
99 | | { 0x04, "Transport Data" }, |
100 | | { 0x00, NULL } |
101 | | }; |
102 | | |
103 | | /* Decryption types. {{{ */ |
104 | | /* |
105 | | * Most operations operate on 32 byte units (keys and hash output). |
106 | | */ |
107 | | typedef struct { |
108 | 0 | #define WG_KEY_LEN 32 |
109 | | unsigned char data[WG_KEY_LEN]; |
110 | | } wg_qqword; |
111 | | |
112 | | /* |
113 | | * Static key with the MAC1 key pre-computed and an optional private key. |
114 | | */ |
115 | | typedef struct wg_skey { |
116 | | wg_qqword pub_key; |
117 | | wg_qqword mac1_key; |
118 | | wg_qqword priv_key; /* Optional, set to all zeroes if missing. */ |
119 | | } wg_skey_t; |
120 | | |
121 | | /* |
122 | | * Pre-shared key, needed while processing the handshake response message. At |
123 | | * that point, ephemeral keys (from either the initiator or responder) should be |
124 | | * known. Thus link the PSK to such ephemeral keys. |
125 | | * |
126 | | * Usually a "wg_ekey_t" contains an empty list (if there is no PSK, i.e. an |
127 | | * all-zeroes PSK) or one item (if a PSK is configured). In the unlikely event |
128 | | * that an ephemeral key is reused, support more than one PSK. |
129 | | */ |
130 | | typedef struct wg_psk { |
131 | | wg_qqword psk_data; |
132 | | struct wg_psk *next; |
133 | | } wg_psk_t; |
134 | | |
135 | | /* |
136 | | * Ephemeral key. |
137 | | */ |
138 | | typedef struct wg_ekey { |
139 | | wg_qqword pub_key; |
140 | | wg_qqword priv_key; /* Optional, set to all zeroes if missing. */ |
141 | | wg_psk_t *psk_list; /* Optional, possible PSKs to try. */ |
142 | | } wg_ekey_t; |
143 | | |
144 | | /* |
145 | | * Set of (long-term) static keys (for guessing the peer based on MAC1). |
146 | | * Maps the public key to the "wg_skey_t" structure. |
147 | | * Keys are populated from the UAT and key log file. |
148 | | */ |
149 | | static GHashTable *wg_static_keys; |
150 | | |
151 | | /* |
152 | | * Set of ephemeral keys (for decryption). Maps the public key to the |
153 | | * "wg_ekey_t" structure. The private key MUST be available. |
154 | | * Keys are populated from the key log file and wmem_file_scope allocated. |
155 | | */ |
156 | | static wmem_map_t *wg_ephemeral_keys; |
157 | | |
158 | | /* |
159 | | * Key log file handle. Opened on demand (when keys are actually looked up), |
160 | | * closed when the capture file closes. |
161 | | */ |
162 | | static FILE *wg_keylog_file; |
163 | | |
164 | | /* |
165 | | * The most recently parsed ephemeral key. If a PSK is configured, the key log |
166 | | * file must have a PSK line after other keys. If not, then it is assumed that |
167 | | * the session does not use a PSK. |
168 | | * |
169 | | * This pointer is cleared when the key log file is reset (i.e. when the capture |
170 | | * file closes). |
171 | | */ |
172 | | static wg_ekey_t *wg_keylog_last_ekey; |
173 | | |
174 | | enum wg_psk_iter_state { |
175 | | WG_PSK_ITER_STATE_ENTER = 0, |
176 | | WG_PSK_ITER_STATE_INITIATOR, |
177 | | WG_PSK_ITER_STATE_RESPONDER, |
178 | | WG_PSK_ITER_STATE_EXIT |
179 | | }; |
180 | | |
181 | | /* See wg_psk_iter_next. */ |
182 | | typedef struct { |
183 | | enum wg_psk_iter_state state; |
184 | | wg_psk_t *next_psk; |
185 | | } wg_psk_iter_context; |
186 | | |
187 | | /* UAT adapter for populating wg_static_keys. */ |
188 | | enum { WG_KEY_UAT_PUBLIC, WG_KEY_UAT_PRIVATE }; |
189 | | static const value_string wg_key_uat_type_vals[] = { |
190 | | { WG_KEY_UAT_PUBLIC, "Public" }, |
191 | | { WG_KEY_UAT_PRIVATE, "Private" }, |
192 | | { 0, NULL } |
193 | | }; |
194 | | |
195 | | typedef struct { |
196 | | unsigned key_type; /* See "wg_key_uat_type_vals". */ |
197 | | char *key; |
198 | | } wg_key_uat_record_t; |
199 | | |
200 | | static wg_key_uat_record_t *wg_key_records; |
201 | | static unsigned num_wg_key_records; |
202 | | |
203 | | /* |
204 | | * Input keying material for key derivation/decryption during the handshake. |
205 | | * For the Initiation message, Spub_r and either Spriv_r or Epriv_i must be set. |
206 | | * For the Response message, Epriv_r + Spriv_r or Epriv_r + Epub_i. |
207 | | * |
208 | | * The static and ephemeral keys are reset upon UAT changes or are invalidated |
209 | | * when the capture file closes. |
210 | | */ |
211 | | typedef struct { |
212 | | const wg_skey_t *initiator_skey; /* Spub_i based on Initiation.static (decrypted, null if decryption failed) */ |
213 | | const wg_skey_t *responder_skey; /* Spub_r based on Initiation.MAC1 (+Spriv_r if available) */ |
214 | | uint8_t timestamp[12]; /* Initiation.timestamp (decrypted) */ |
215 | | bool timestamp_ok : 1; /* Whether the timestamp was successfully decrypted */ |
216 | | bool empty_ok : 1; /* Whether the empty field was successfully decrypted */ |
217 | | |
218 | | /* The following fields are only valid on the initial pass. */ |
219 | | const wg_ekey_t *initiator_ekey; /* Epub_i matching Initiation.Ephemeral (+Epriv_i if available) */ |
220 | | const wg_ekey_t *responder_ekey; /* Epub_r matching Response.Ephemeral (+Epriv_r if available) */ |
221 | | wg_qqword handshake_hash; /* Handshake hash H_i */ |
222 | | wg_qqword chaining_key; /* Chaining key C_i */ |
223 | | |
224 | | /* Transport ciphers. */ |
225 | | gcry_cipher_hd_t initiator_recv_cipher; |
226 | | gcry_cipher_hd_t responder_recv_cipher; |
227 | | } wg_handshake_state_t; |
228 | | |
229 | | /** Hash(CONSTRUCTION), initialized by wg_decrypt_init. */ |
230 | | static wg_qqword hash_of_construction; |
231 | | /** Hash(Hash(CONSTRUCTION) || IDENTIFIER), initialized by wg_decrypt_init. */ |
232 | | static wg_qqword hash_of_c_identifier; |
233 | | /* Decryption types. }}} */ |
234 | | |
235 | | /* |
236 | | * Information required to process and link messages as required on the first |
237 | | * sequential pass. After that it can be erased. |
238 | | */ |
239 | | typedef struct { |
240 | | address initiator_address; |
241 | | address responder_address; |
242 | | uint16_t initiator_port; |
243 | | uint16_t responder_port; |
244 | | } wg_initial_info_t; |
245 | | |
246 | | /* |
247 | | * A "session" between two peer is identified by a "sender" id as independently |
248 | | * chosen by each side. In case both peer IDs collide, the source IP and UDP |
249 | | * port number could be used to distinguish sessions. As IDs can be recycled |
250 | | * over time, lookups should use the most recent initiation (or response). |
251 | | * |
252 | | * XXX record timestamps (time since last message, for validating timers). |
253 | | */ |
254 | | typedef struct { |
255 | | uint32_t stream; /* Session identifier (akin to udp.stream). */ |
256 | | uint32_t initiator_frame; |
257 | | uint32_t response_frame; /* Responder or Cookie Reply message. */ |
258 | | wg_initial_info_t initial; /* Valid only on the first pass. */ |
259 | | wg_handshake_state_t *hs; /* Handshake state to enable decryption. */ |
260 | | } wg_session_t; |
261 | | |
262 | | /* Per-packet state. */ |
263 | | typedef struct { |
264 | | wg_session_t *session; |
265 | | bool receiver_is_initiator; /* Whether this transport data packet is sent to an Initiator. */ |
266 | | } wg_packet_info_t; |
267 | | |
268 | | /* Map from Sender/Receiver IDs to a list of session information. */ |
269 | | static wmem_map_t *sessions; |
270 | | static uint32_t wg_session_count; |
271 | | |
272 | | |
273 | | /* Key conversion routines. {{{ */ |
274 | | /* Import external random data as private key. */ |
275 | | static void |
276 | | set_private_key(wg_qqword *privkey, const wg_qqword *inkey) |
277 | 0 | { |
278 | | // The 254th bit of a Curve25519 secret will always be set in calculations, |
279 | | // use this property to recognize whether a private key is set. |
280 | 0 | *privkey = *inkey; |
281 | 0 | privkey->data[31] |= 64; |
282 | 0 | } |
283 | | |
284 | | /* Whether a private key is initialized (see set_private_key). */ |
285 | | static inline bool |
286 | | has_private_key(const wg_qqword *secret) |
287 | 0 | { |
288 | 0 | return !!(secret->data[31] & 64); |
289 | 0 | } |
290 | | |
291 | | /** |
292 | | * Compute the Curve25519 public key from a private key. |
293 | | */ |
294 | | static void |
295 | | priv_to_pub(wg_qqword *pub, const wg_qqword *priv) |
296 | 0 | { |
297 | 0 | int r = crypto_scalarmult_curve25519_base(pub->data, priv->data); |
298 | | /* The computation should always be possible. */ |
299 | 0 | DISSECTOR_ASSERT(r == 0); |
300 | 0 | } |
301 | | |
302 | | static void |
303 | | dh_x25519(wg_qqword *shared_secret, const wg_qqword *priv, const wg_qqword *pub) |
304 | 0 | { |
305 | | /* |
306 | | * If the point ("pub") is of small order, of if the result is all zeros, -1 |
307 | | * could be returned with Sodium. We are just interpreting the trace, so |
308 | | * just ignore the condition for now. |
309 | | */ |
310 | 0 | (void)crypto_scalarmult_curve25519(shared_secret->data, priv->data, pub->data); |
311 | 0 | } |
312 | | |
313 | | /* |
314 | | * Returns the string representation (base64) of a public key. |
315 | | * The returned value is allocated with wmem_allocator scope. |
316 | | */ |
317 | | static const char * |
318 | | pubkey_to_string(wmem_allocator_t* allocator, const wg_qqword *pubkey) |
319 | 0 | { |
320 | 0 | char *str = g_base64_encode(pubkey->data, WG_KEY_LEN); |
321 | 0 | char *ret = wmem_strdup(allocator, str); |
322 | 0 | g_free(str); |
323 | 0 | return ret; |
324 | 0 | } |
325 | | |
326 | | static bool |
327 | | decode_base64_key(wg_qqword *out, const char *str) |
328 | 0 | { |
329 | 0 | size_t out_len; |
330 | 0 | char tmp[45]; |
331 | |
|
332 | 0 | if (strlen(str) + 1 != sizeof(tmp)) { |
333 | 0 | return false; |
334 | 0 | } |
335 | 0 | memcpy(tmp, str, sizeof(tmp)); |
336 | 0 | g_base64_decode_inplace(tmp, &out_len); |
337 | 0 | if (out_len != WG_KEY_LEN) { |
338 | 0 | return false; |
339 | 0 | } |
340 | 0 | memcpy(out->data, tmp, WG_KEY_LEN); |
341 | 0 | return true; |
342 | 0 | } |
343 | | /* Key conversion routines. }}} */ |
344 | | |
345 | | static gboolean |
346 | | wg_pubkey_equal(const void *v1, const void *v2) |
347 | 0 | { |
348 | 0 | const wg_qqword *pubkey1 = (const wg_qqword *)v1; |
349 | 0 | const wg_qqword *pubkey2 = (const wg_qqword *)v2; |
350 | 0 | return !memcmp(pubkey1->data, pubkey2->data, WG_KEY_LEN); |
351 | 0 | } |
352 | | |
353 | | |
354 | | /* Protocol-specific crypto routines. {{{ */ |
355 | | /** |
356 | | * Computes MAC1. Caller must ensure that GCRY_MD_BLAKE2S_256 is available. |
357 | | */ |
358 | | static void |
359 | | wg_mac1_key(const wg_qqword *static_public, wg_qqword *mac_key_out) |
360 | 0 | { |
361 | 0 | gcry_md_hd_t hd; |
362 | 0 | if (gcry_md_open(&hd, GCRY_MD_BLAKE2S_256, 0) == 0) { |
363 | 0 | static const char wg_label_mac1[] = "mac1----"; |
364 | 0 | gcry_md_write(hd, wg_label_mac1, strlen(wg_label_mac1)); |
365 | 0 | gcry_md_write(hd, static_public->data, sizeof(wg_qqword)); |
366 | 0 | memcpy(mac_key_out->data, gcry_md_read(hd, 0), sizeof(wg_qqword)); |
367 | 0 | gcry_md_close(hd); |
368 | 0 | return; |
369 | 0 | } |
370 | | // caller should have checked this. |
371 | 0 | DISSECTOR_ASSERT_NOT_REACHED(); |
372 | 0 | } |
373 | | |
374 | | /* |
375 | | * Verify that MAC(mac_key, data) matches "mac_output". |
376 | | */ |
377 | | static bool |
378 | | wg_mac_verify(const wg_qqword *mac_key, |
379 | | const unsigned char *data, unsigned data_len, const uint8_t mac_output[16]) |
380 | 0 | { |
381 | 0 | bool ok = false; |
382 | 0 | gcry_md_hd_t hd; |
383 | 0 | if (gcry_md_open(&hd, GCRY_MD_BLAKE2S_128, 0) == 0) { |
384 | 0 | gcry_error_t r; |
385 | | // not documented by Libgcrypt, but required for keyed blake2s |
386 | 0 | r = gcry_md_setkey(hd, mac_key->data, WG_KEY_LEN); |
387 | 0 | DISSECTOR_ASSERT(r == 0); |
388 | 0 | gcry_md_write(hd, data, data_len); |
389 | 0 | ok = memcmp(mac_output, gcry_md_read(hd, 0), 16) == 0; |
390 | 0 | gcry_md_close(hd); |
391 | 0 | } else { |
392 | | // caller should have checked this. |
393 | 0 | DISSECTOR_ASSERT_NOT_REACHED(); |
394 | 0 | } |
395 | 0 | return ok; |
396 | 0 | } |
397 | | |
398 | | /** |
399 | | * Update the new chained hash value: h = Hash(h || data). |
400 | | */ |
401 | | static void |
402 | | wg_mix_hash(wg_qqword *h, const void *data, size_t data_len) |
403 | 15 | { |
404 | 15 | gcry_md_hd_t hd; |
405 | 15 | if (gcry_md_open(&hd, GCRY_MD_BLAKE2S_256, 0)) { |
406 | 0 | DISSECTOR_ASSERT_NOT_REACHED(); |
407 | 0 | } |
408 | 15 | gcry_md_write(hd, h->data, sizeof(wg_qqword)); |
409 | 15 | gcry_md_write(hd, data, data_len); |
410 | 15 | memcpy(h, gcry_md_read(hd, 0), sizeof(wg_qqword)); |
411 | 15 | gcry_md_close(hd); |
412 | 15 | } |
413 | | |
414 | | /** |
415 | | * Computes KDF_n(key, input) where n is the number of derived keys. |
416 | | */ |
417 | | static void |
418 | | wg_kdf(const wg_qqword *key, const uint8_t *input, unsigned input_len, unsigned n, wg_qqword *out) |
419 | 0 | { |
420 | 0 | uint8_t prk[32]; /* Blake2s_256 hash output. */ |
421 | 0 | gcry_error_t err; |
422 | 0 | err = hkdf_extract(GCRY_MD_BLAKE2S_256, key->data, sizeof(wg_qqword), input, input_len, prk); |
423 | 0 | DISSECTOR_ASSERT(err == 0); |
424 | 0 | err = hkdf_expand(GCRY_MD_BLAKE2S_256, prk, sizeof(prk), NULL, 0, out->data, 32 * n); |
425 | 0 | DISSECTOR_ASSERT(err == 0); |
426 | 0 | } |
427 | | |
428 | | /* |
429 | | * Must be called before attempting decryption. |
430 | | */ |
431 | | static bool |
432 | | wg_decrypt_init(void) |
433 | 15 | { |
434 | 15 | if (gcry_md_test_algo(GCRY_MD_BLAKE2S_128) != 0 || |
435 | 15 | gcry_md_test_algo(GCRY_MD_BLAKE2S_256) != 0 || |
436 | 15 | gcry_cipher_test_algo(GCRY_CIPHER_CHACHA20) != 0) { |
437 | 0 | return false; |
438 | 0 | } |
439 | 15 | static const char construction[] = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"; |
440 | 15 | gcry_md_hash_buffer(GCRY_MD_BLAKE2S_256, hash_of_construction.data, construction, strlen(construction)); |
441 | | |
442 | 15 | static const char wg_identifier[] = "WireGuard v1 zx2c4 Jason@zx2c4.com"; |
443 | 15 | memcpy(&hash_of_c_identifier, hash_of_construction.data, sizeof(wg_qqword)); |
444 | 15 | wg_mix_hash(&hash_of_c_identifier, wg_identifier, strlen(wg_identifier)); |
445 | 15 | return true; |
446 | 15 | } |
447 | | |
448 | | static gcry_cipher_hd_t |
449 | | wg_create_cipher(const wg_qqword *key) |
450 | 0 | { |
451 | 0 | gcry_cipher_hd_t hd; |
452 | 0 | if (gcry_cipher_open(&hd, GCRY_CIPHER_CHACHA20, GCRY_CIPHER_MODE_POLY1305, 0)) { |
453 | 0 | return NULL; |
454 | 0 | } |
455 | | |
456 | 0 | if (gcry_cipher_setkey(hd, key->data, sizeof(*key))) { |
457 | 0 | gcry_cipher_close(hd); |
458 | 0 | hd = NULL; |
459 | 0 | } |
460 | 0 | return hd; |
461 | 0 | } |
462 | | |
463 | | static bool |
464 | | wg_handshake_state_destroy_cb(wmem_allocator_t *allocator _U_, wmem_cb_event_t event _U_, void *user_data) |
465 | 0 | { |
466 | 0 | wg_handshake_state_t *hs = (wg_handshake_state_t *)user_data; |
467 | |
|
468 | 0 | if (hs->initiator_recv_cipher) { |
469 | 0 | gcry_cipher_close(hs->initiator_recv_cipher); |
470 | 0 | hs->initiator_recv_cipher = NULL; |
471 | 0 | } |
472 | 0 | if (hs->responder_recv_cipher) { |
473 | 0 | gcry_cipher_close(hs->responder_recv_cipher); |
474 | 0 | hs->responder_recv_cipher = NULL; |
475 | 0 | } |
476 | 0 | return false; |
477 | 0 | } |
478 | | |
479 | | /* |
480 | | * Decrypt ciphertext using the ChaCha20-Poly1305 cipher. The auth tag must be |
481 | | * included with the ciphertext. |
482 | | */ |
483 | | static bool |
484 | | wg_aead_decrypt(gcry_cipher_hd_t hd, uint64_t counter, const unsigned char *ctext, unsigned ctext_len, const unsigned char *aad, unsigned aad_len, unsigned char *out, unsigned out_len) |
485 | 0 | { |
486 | 0 | DISSECTOR_ASSERT(ctext_len >= AUTH_TAG_LENGTH); |
487 | 0 | ctext_len -= AUTH_TAG_LENGTH; |
488 | 0 | const unsigned char *auth_tag = ctext + ctext_len; |
489 | |
|
490 | 0 | counter = GUINT64_TO_LE(counter); |
491 | 0 | unsigned char nonce[12] = { 0 }; |
492 | 0 | memcpy(nonce + 4, &counter, 8); |
493 | |
|
494 | 0 | return gcry_cipher_setiv(hd, nonce, sizeof(nonce)) == 0 && |
495 | 0 | gcry_cipher_authenticate(hd, aad, aad_len) == 0 && |
496 | 0 | gcry_cipher_decrypt(hd, out, out_len, ctext, ctext_len) == 0 && |
497 | 0 | gcry_cipher_checktag(hd, auth_tag, AUTH_TAG_LENGTH) == 0; |
498 | 0 | } |
499 | | |
500 | | /** |
501 | | * Decrypt ciphertext using the ChaCha20-Poly1305 cipher. The auth tag must be |
502 | | * included with the ciphertext. |
503 | | */ |
504 | | static bool |
505 | | aead_decrypt(const wg_qqword *key, uint64_t counter, const unsigned char *ctext, unsigned ctext_len, const unsigned char *aad, unsigned aad_len, unsigned char *out, unsigned out_len) |
506 | 0 | { |
507 | 0 | DISSECTOR_ASSERT(ctext_len >= AUTH_TAG_LENGTH); |
508 | |
|
509 | 0 | gcry_cipher_hd_t hd = wg_create_cipher(key); |
510 | 0 | DISSECTOR_ASSERT(hd); |
511 | 0 | bool ok = wg_aead_decrypt(hd, counter, ctext, ctext_len, aad, aad_len, out, out_len); |
512 | 0 | gcry_cipher_close(hd); |
513 | 0 | return ok; |
514 | 0 | } |
515 | | /* Protocol-specific crypto routines. }}} */ |
516 | | |
517 | | /* |
518 | | * Add a static public or private key to "wg_static_keys". |
519 | | */ |
520 | | static void |
521 | | wg_add_static_key(const wg_qqword *tmp_key, bool is_private) |
522 | 0 | { |
523 | 0 | if (!wg_decryption_supported) { |
524 | 0 | return; |
525 | 0 | } |
526 | | |
527 | 0 | wg_skey_t *key = g_new0(wg_skey_t, 1); |
528 | 0 | if (is_private) { |
529 | 0 | set_private_key(&key->priv_key, tmp_key); |
530 | 0 | priv_to_pub(&key->pub_key, tmp_key); |
531 | 0 | } else { |
532 | 0 | key->pub_key = *tmp_key; |
533 | 0 | } |
534 | | |
535 | | // If a previous pubkey exists, skip adding the new key. Do add the |
536 | | // secret if it has become known in meantime. |
537 | 0 | wg_skey_t *oldkey = (wg_skey_t *)g_hash_table_lookup(wg_static_keys, &key->pub_key); |
538 | 0 | if (oldkey) { |
539 | 0 | if (!has_private_key(&oldkey->priv_key) && is_private) { |
540 | 0 | oldkey->priv_key = key->priv_key; |
541 | 0 | } |
542 | 0 | g_free(key); |
543 | 0 | return; |
544 | 0 | } |
545 | | |
546 | | // New key, precompute the MAC1 label. |
547 | 0 | wg_mac1_key(&key->pub_key, &key->mac1_key); |
548 | |
|
549 | 0 | g_hash_table_insert(wg_static_keys, &key->pub_key, key); |
550 | 0 | } |
551 | | |
552 | | /** |
553 | | * Stores the given ephemeral private key. |
554 | | */ |
555 | | static wg_ekey_t * |
556 | | wg_add_ephemeral_privkey(const wg_qqword *priv_key) |
557 | 0 | { |
558 | 0 | if (!wg_decryption_supported) { |
559 | 0 | return NULL; |
560 | 0 | } |
561 | | |
562 | 0 | wg_qqword pub_key; |
563 | 0 | priv_to_pub(&pub_key, priv_key); |
564 | 0 | wg_ekey_t *key = (wg_ekey_t *)wmem_map_lookup(wg_ephemeral_keys, &pub_key); |
565 | 0 | if (!key) { |
566 | 0 | key = wmem_new0(wmem_file_scope(), wg_ekey_t); |
567 | 0 | key->pub_key = pub_key; |
568 | 0 | set_private_key(&key->priv_key, priv_key); |
569 | 0 | wmem_map_insert(wg_ephemeral_keys, &key->pub_key, key); |
570 | 0 | } |
571 | 0 | return key; |
572 | 0 | } |
573 | | |
574 | | /* PSK handling. {{{ */ |
575 | | static void |
576 | | wg_add_psk(wg_ekey_t *ekey, const wg_qqword *psk) |
577 | 0 | { |
578 | 0 | wg_psk_t *psk_entry = wmem_new0(wmem_file_scope(), wg_psk_t); |
579 | 0 | psk_entry->psk_data = *psk; |
580 | 0 | psk_entry->next = ekey->psk_list; |
581 | 0 | ekey->psk_list = psk_entry; |
582 | 0 | } |
583 | | |
584 | | /* |
585 | | * Retrieves the next PSK to try and returns true if one is found or false if |
586 | | * there are no more to try. |
587 | | */ |
588 | | static bool |
589 | | wg_psk_iter_next(wg_psk_iter_context *psk_iter, const wg_handshake_state_t *hs, |
590 | | wg_qqword *psk_out) |
591 | 0 | { |
592 | 0 | wg_psk_t *psk = psk_iter->next_psk; |
593 | 0 | while (!psk) { |
594 | | /* |
595 | | * Yield PSKs based on Epub_i, then those based on Epub_r, then yield an |
596 | | * all-zeroes key and finally fail in the terminating state. |
597 | | */ |
598 | 0 | switch (psk_iter->state) { |
599 | 0 | case WG_PSK_ITER_STATE_ENTER: |
600 | 0 | psk = hs->initiator_ekey->psk_list; |
601 | 0 | psk_iter->state = WG_PSK_ITER_STATE_INITIATOR; |
602 | 0 | break; |
603 | 0 | case WG_PSK_ITER_STATE_INITIATOR: |
604 | 0 | psk = hs->responder_ekey->psk_list; |
605 | 0 | psk_iter->state = WG_PSK_ITER_STATE_RESPONDER; |
606 | 0 | break; |
607 | 0 | case WG_PSK_ITER_STATE_RESPONDER: |
608 | 0 | memset(psk_out->data, 0, WG_KEY_LEN); |
609 | 0 | psk_iter->state = WG_PSK_ITER_STATE_EXIT; |
610 | 0 | return true; |
611 | 0 | case WG_PSK_ITER_STATE_EXIT: |
612 | 0 | return false; |
613 | 0 | } |
614 | 0 | } |
615 | | |
616 | 0 | *psk_out = psk->psk_data; |
617 | 0 | psk_iter->next_psk = psk->next; |
618 | 0 | return true; |
619 | 0 | } |
620 | | /* PSK handling. }}} */ |
621 | | |
622 | | /* UAT and key configuration. {{{ */ |
623 | | |
624 | | static void |
625 | | wg_keylog_reset(void) |
626 | 15 | { |
627 | 15 | if (wg_keylog_file) { |
628 | 0 | fclose(wg_keylog_file); |
629 | 0 | wg_keylog_file = NULL; |
630 | 0 | wg_keylog_last_ekey = NULL; |
631 | 0 | } |
632 | 15 | } |
633 | | |
634 | | static void wg_keylog_process_lines(const void *data, unsigned datalen); |
635 | | |
636 | | static void |
637 | | wg_keylog_read(void) |
638 | 3 | { |
639 | 3 | if (!wg_decryption_supported) { |
640 | 0 | return; |
641 | 0 | } |
642 | | |
643 | 3 | if (!pref_keylog_file || !*pref_keylog_file) { |
644 | 3 | return; |
645 | 3 | } |
646 | | |
647 | | // Reopen file if it got deleted/overwritten. |
648 | 0 | if (wg_keylog_file && file_needs_reopen(ws_fileno(wg_keylog_file), pref_keylog_file)) { |
649 | 0 | ws_debug("Key log file got changed or deleted, trying to re-open."); |
650 | 0 | wg_keylog_reset(); |
651 | 0 | } |
652 | |
|
653 | 0 | if (!wg_keylog_file) { |
654 | 0 | wg_keylog_file = ws_fopen(pref_keylog_file, "r"); |
655 | 0 | if (!wg_keylog_file) { |
656 | 0 | ws_debug("Failed to open key log file %s: %s", pref_keylog_file, g_strerror(errno)); |
657 | 0 | return; |
658 | 0 | } |
659 | 0 | ws_debug("Opened key log file %s", pref_keylog_file); |
660 | 0 | } |
661 | | |
662 | | /* File format: each line follows the format "<type>=<key>" (leading spaces |
663 | | * and spaces around '=' as produced by extract-handshakes.sh are ignored). |
664 | | * For available <type>s, see below. <key> is the base64-encoded key (44 |
665 | | * characters). |
666 | | * |
667 | | * Example: |
668 | | * LOCAL_STATIC_PRIVATE_KEY = AKeZaHwBxjiKLFnkY2unvEdOTtg4AL+M9dQXfopFVFk= |
669 | | * REMOTE_STATIC_PUBLIC_KEY = YDCttCs9e1J52/g9vEnwJJa+2x6RqaayAYMpSVQfGEY= |
670 | | * LOCAL_EPHEMERAL_PRIVATE_KEY = sLGLJSOQfyz7JNJ5ZDzFf3Uz1rkiCMMjbWerNYcPFFU= |
671 | | * PRESHARED_KEY = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= |
672 | | */ |
673 | | |
674 | 0 | for (;;) { |
675 | 0 | char buf[512]; |
676 | 0 | if (!fgets(buf, sizeof(buf), wg_keylog_file)) { |
677 | 0 | if (feof(wg_keylog_file)) { |
678 | 0 | clearerr(wg_keylog_file); |
679 | 0 | } else if (ferror(wg_keylog_file)) { |
680 | 0 | ws_debug("Error while reading %s, closing it.", pref_keylog_file); |
681 | 0 | wg_keylog_reset(); |
682 | 0 | } |
683 | 0 | break; |
684 | 0 | } |
685 | | |
686 | 0 | wg_keylog_process_lines((const uint8_t *)buf, (unsigned)strlen(buf)); |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | | static void |
691 | | wg_keylog_process_lines(const void *data, unsigned datalen) |
692 | 0 | { |
693 | 0 | const char *next_line = (const char *)data; |
694 | 0 | const char *line_end = next_line + datalen; |
695 | 0 | while (next_line && next_line < line_end) { |
696 | | /* Note: line is NOT nul-terminated. */ |
697 | 0 | const char *line = next_line; |
698 | 0 | next_line = (const char *)memchr(line, '\n', line_end - line); |
699 | 0 | ssize_t linelen; |
700 | |
|
701 | 0 | if (next_line) { |
702 | 0 | linelen = next_line - line; |
703 | 0 | next_line++; /* drop LF */ |
704 | 0 | } else { |
705 | 0 | linelen = (ssize_t)(line_end - line); |
706 | 0 | } |
707 | 0 | if (linelen > 0 && line[linelen - 1] == '\r') { |
708 | 0 | linelen--; /* drop CR */ |
709 | 0 | } |
710 | |
|
711 | 0 | ws_debug("Read WG key log line: %.*s", (int)linelen, line); |
712 | | |
713 | | /* Strip leading spaces. */ |
714 | 0 | const char *p = line; |
715 | 0 | while (p < line_end && *p == ' ') { |
716 | 0 | ++p; |
717 | 0 | } |
718 | 0 | char key_type[sizeof("LOCAL_EPHEMERAL_PRIVATE_KEY")]; |
719 | 0 | char key_value[45] = { 0 }; |
720 | 0 | const char *p0 = p; |
721 | 0 | p = (const char *)memchr(p0, '=', line_end - p); |
722 | 0 | if (p && p0 != p) { |
723 | | /* Extract "key-type" from "key-type = key-value" */ |
724 | 0 | size_t key_type_len = p - p0; |
725 | 0 | while (key_type_len && p0[key_type_len - 1] == ' ') { |
726 | 0 | --key_type_len; |
727 | 0 | } |
728 | 0 | if (key_type_len && key_type_len < sizeof(key_type)) { |
729 | 0 | memcpy(key_type, p0, key_type_len); |
730 | 0 | key_type[key_type_len] = '\0'; |
731 | | |
732 | | /* Skip '=' and any spaces. */ |
733 | 0 | p = p + 1; |
734 | 0 | while (p < line_end && *p == ' ') { |
735 | 0 | ++p; |
736 | 0 | } |
737 | 0 | size_t key_value_len = (line + linelen) - p; |
738 | 0 | if (key_value_len && key_value_len < sizeof(key_value)) { |
739 | 0 | memcpy(key_value, p, key_value_len); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | } |
743 | |
|
744 | 0 | wg_qqword key; |
745 | 0 | if (!key_value[0] || !decode_base64_key(&key, key_value)) { |
746 | 0 | ws_debug("Unrecognized key log line: %.*s", (int)linelen, line); |
747 | 0 | continue; |
748 | 0 | } |
749 | | |
750 | 0 | if (!strcmp(key_type, "LOCAL_STATIC_PRIVATE_KEY")) { |
751 | 0 | wg_add_static_key(&key, true); |
752 | 0 | } else if (!strcmp(key_type, "REMOTE_STATIC_PUBLIC_KEY")) { |
753 | 0 | wg_add_static_key(&key, false); |
754 | 0 | } else if (!strcmp(key_type, "LOCAL_EPHEMERAL_PRIVATE_KEY")) { |
755 | 0 | wg_keylog_last_ekey = wg_add_ephemeral_privkey(&key); |
756 | 0 | } else if (!strcmp(key_type, "PRESHARED_KEY")) { |
757 | | /* Link the PSK to the last ephemeral key. */ |
758 | 0 | if (wg_keylog_last_ekey) { |
759 | 0 | wg_add_psk(wg_keylog_last_ekey, &key); |
760 | 0 | wg_keylog_last_ekey = NULL; |
761 | 0 | } else { |
762 | 0 | ws_debug("Ignored PSK as no new ephemeral key was found"); |
763 | 0 | } |
764 | 0 | } else { |
765 | 0 | ws_debug("Unrecognized key log line: %.*s", (int)linelen, line); |
766 | 0 | } |
767 | 0 | } |
768 | 0 | } |
769 | | |
770 | | static void* |
771 | | wg_key_uat_record_copy_cb(void *dest, const void *source, size_t len _U_) |
772 | 0 | { |
773 | 0 | const wg_key_uat_record_t* o = (const wg_key_uat_record_t*)source; |
774 | 0 | wg_key_uat_record_t* d = (wg_key_uat_record_t*)dest; |
775 | |
|
776 | 0 | d->key_type = o->key_type; |
777 | 0 | d->key = g_strdup(o->key); |
778 | |
|
779 | 0 | return dest; |
780 | 0 | } |
781 | | |
782 | | static bool |
783 | | wg_key_uat_record_update_cb(void *r, char **error) |
784 | 0 | { |
785 | 0 | wg_key_uat_record_t *rec = (wg_key_uat_record_t *)r; |
786 | 0 | wg_qqword key; |
787 | | |
788 | | /* Check for valid base64-encoding. */ |
789 | 0 | if (!decode_base64_key(&key, rec->key)) { |
790 | 0 | *error = g_strdup("Invalid key"); |
791 | 0 | return false; |
792 | 0 | } |
793 | | |
794 | 0 | return true; |
795 | 0 | } |
796 | | |
797 | | static void |
798 | | wg_key_uat_record_free_cb(void *r) |
799 | 0 | { |
800 | 0 | wg_key_uat_record_t *rec = (wg_key_uat_record_t *)r; |
801 | 0 | g_free(rec->key); |
802 | 0 | } |
803 | | |
804 | | static void |
805 | | wg_key_uat_apply(void) |
806 | 15 | { |
807 | 15 | if (!wg_decryption_supported) { |
808 | 0 | return; |
809 | 0 | } |
810 | | |
811 | 15 | if (!wg_static_keys) { |
812 | | // The first field of "wg_skey_t" is the pubkey (and the table key), |
813 | | // its initial four bytes should be good enough as key hash. |
814 | 15 | wg_static_keys = g_hash_table_new_full(g_int_hash, wg_pubkey_equal, NULL, g_free); |
815 | 15 | } else { |
816 | 0 | g_hash_table_remove_all(wg_static_keys); |
817 | 0 | } |
818 | | |
819 | | // As static keys from the key log file also end up in "wg_static_keys", |
820 | | // reset the file pointer such that it will be fully read later. |
821 | 15 | wg_keylog_reset(); |
822 | | |
823 | | /* Convert base64-encoded strings to wg_skey_t and derive pubkey. */ |
824 | 15 | for (unsigned i = 0; i < num_wg_key_records; i++) { |
825 | 0 | wg_key_uat_record_t *rec = &wg_key_records[i]; |
826 | 0 | wg_qqword tmp_key; /* Either public or private, not sure yet. */ |
827 | | |
828 | | /* Populate public (and private) keys. */ |
829 | 0 | bool decoded = decode_base64_key(&tmp_key, rec->key); |
830 | 0 | DISSECTOR_ASSERT(decoded); |
831 | 0 | wg_add_static_key(&tmp_key, rec->key_type == WG_KEY_UAT_PRIVATE); |
832 | 0 | } |
833 | 15 | } |
834 | | |
835 | | static void |
836 | | wg_key_uat_reset(void) |
837 | 0 | { |
838 | | /* Erase keys when the UAT is unloaded. */ |
839 | 0 | if (wg_static_keys != NULL) { |
840 | 0 | g_hash_table_destroy(wg_static_keys); |
841 | 0 | wg_static_keys = NULL; |
842 | 0 | } |
843 | 0 | } |
844 | | |
845 | 0 | UAT_VS_DEF(wg_key_uat, key_type, wg_key_uat_record_t, unsigned, WG_KEY_UAT_PUBLIC, "Public") Unexecuted instantiation: packet-wireguard.c:wg_key_uat_key_type_set_cb Unexecuted instantiation: packet-wireguard.c:wg_key_uat_key_type_tostr_cb |
846 | 0 | UAT_CSTRING_CB_DEF(wg_key_uat, key, wg_key_uat_record_t) |
847 | | /* UAT and key configuration. }}} */ |
848 | | |
849 | | /** |
850 | | * Tries to decrypt the initiation message. |
851 | | * Assumes responder_skey and initiator_ekey to be set. |
852 | | */ |
853 | | static void |
854 | | wg_process_initiation(tvbuff_t *tvb, wg_handshake_state_t *hs) |
855 | 0 | { |
856 | 0 | DISSECTOR_ASSERT(hs->responder_skey); |
857 | 0 | DISSECTOR_ASSERT(hs->initiator_ekey); |
858 | 0 | DISSECTOR_ASSERT(hs->initiator_skey == NULL); |
859 | |
|
860 | 0 | wg_qqword decrypted_static = {{ 0 }}; |
861 | 0 | const bool has_Spriv_r = has_private_key(&hs->responder_skey->priv_key); |
862 | 0 | const bool has_Epriv_i = has_private_key(&hs->initiator_ekey->priv_key); |
863 | | |
864 | | // Either Spriv_r or Epriv_i + Spriv_i are needed. If the first two are not |
865 | | // available, fail early. Spriv_i will be looked up later. |
866 | 0 | if (!has_Spriv_r && !has_Epriv_i) { |
867 | 0 | return; |
868 | 0 | } |
869 | | |
870 | 0 | const wg_qqword *ephemeral = (const wg_qqword *)tvb_get_ptr(tvb, 8, WG_KEY_LEN); |
871 | 0 | #define WG_ENCRYPTED_STATIC_LENGTH (32 + AUTH_TAG_LENGTH) |
872 | 0 | const uint8_t *encrypted_static = (const uint8_t *)tvb_get_ptr(tvb, 40, WG_ENCRYPTED_STATIC_LENGTH); |
873 | 0 | #define WG_ENCRYPTED_TIMESTAMP_LENGTH (12 + AUTH_TAG_LENGTH) |
874 | 0 | const uint8_t *encrypted_timestamp = (const uint8_t *)tvb_get_ptr(tvb, 88, WG_ENCRYPTED_TIMESTAMP_LENGTH); |
875 | |
|
876 | 0 | wg_qqword c_and_k[2], h; |
877 | 0 | wg_qqword *c = &c_and_k[0], *k = &c_and_k[1]; |
878 | | // c = Hash(CONSTRUCTION) |
879 | 0 | memcpy(c->data, hash_of_construction.data, sizeof(wg_qqword)); |
880 | | // h = Hash(c || IDENTIFIER) |
881 | 0 | memcpy(h.data, hash_of_c_identifier.data, sizeof(wg_qqword)); |
882 | | // h = Hash(h || Spub_r) |
883 | 0 | wg_mix_hash(&h, hs->responder_skey->pub_key.data, sizeof(wg_qqword)); |
884 | | // c = KDF1(c, msg.ephemeral) |
885 | 0 | wg_kdf(c, ephemeral->data, WG_KEY_LEN, 1, c); |
886 | | // h = Hash(h || msg.ephemeral) |
887 | 0 | wg_mix_hash(&h, ephemeral, WG_KEY_LEN); |
888 | | // dh1 = DH(Spriv_r, msg.ephemeral) if kType = R |
889 | | // dh1 = DH(Epriv_i, Spub_r) if kType = I |
890 | 0 | wg_qqword dh1 = {{ 0 }}; |
891 | 0 | if (has_Spriv_r) { |
892 | 0 | dh_x25519(&dh1, &hs->responder_skey->priv_key, ephemeral); |
893 | 0 | } else { |
894 | 0 | dh_x25519(&dh1, &hs->initiator_ekey->priv_key, &hs->responder_skey->pub_key); |
895 | 0 | } |
896 | | // (c, k) = KDF2(c, dh1) |
897 | 0 | wg_kdf(c, dh1.data, sizeof(dh1), 2, c_and_k); |
898 | | // Spub_i = AEAD-Decrypt(k, 0, msg.static, h) |
899 | 0 | if (!aead_decrypt(k, 0, encrypted_static, WG_ENCRYPTED_STATIC_LENGTH, h.data, sizeof(wg_qqword), decrypted_static.data, sizeof(decrypted_static))) { |
900 | 0 | return; |
901 | 0 | } |
902 | | // Save static public key to the context and lookup private key if possible. |
903 | 0 | wg_skey_t *skey_i = (wg_skey_t *)g_hash_table_lookup(wg_static_keys, &decrypted_static); |
904 | 0 | if (!skey_i) { |
905 | 0 | skey_i = wmem_new0(wmem_file_scope(), wg_skey_t); |
906 | 0 | skey_i->pub_key = decrypted_static; |
907 | 0 | } |
908 | 0 | hs->initiator_skey = skey_i; |
909 | | // If Spriv_r is not available, then Epriv_i + Spriv_i must be available. |
910 | 0 | if (!has_Spriv_r && !has_private_key(&hs->initiator_skey->priv_key)) { |
911 | 0 | return; |
912 | 0 | } |
913 | | |
914 | | // h = Hash(h || msg.static) |
915 | 0 | wg_mix_hash(&h, encrypted_static, WG_ENCRYPTED_STATIC_LENGTH); |
916 | | // dh2 = DH(Spriv_r, Spub_i) if kType = R |
917 | | // dh2 = DH(Spriv_i, Spub_r) if kType = I |
918 | 0 | wg_qqword dh2 = {{ 0 }}; |
919 | 0 | if (has_Spriv_r) { |
920 | 0 | dh_x25519(&dh2, &hs->responder_skey->priv_key, &hs->initiator_skey->pub_key); |
921 | 0 | } else { |
922 | 0 | dh_x25519(&dh2, &hs->initiator_skey->priv_key, &hs->responder_skey->pub_key); |
923 | 0 | } |
924 | | // (c, k) = KDF2(c, dh2) |
925 | 0 | wg_kdf(c, dh2.data, sizeof(wg_qqword), 2, c_and_k); |
926 | | // timestamp = AEAD-Decrypt(k, 0, msg.timestamp, h) |
927 | 0 | if (!aead_decrypt(k, 0, encrypted_timestamp, WG_ENCRYPTED_TIMESTAMP_LENGTH, h.data, sizeof(wg_qqword), hs->timestamp, sizeof(hs->timestamp))) { |
928 | 0 | return; |
929 | 0 | } |
930 | 0 | hs->timestamp_ok = true; |
931 | | // h = Hash(h || msg.timestamp) |
932 | 0 | wg_mix_hash(&h, encrypted_timestamp, WG_ENCRYPTED_TIMESTAMP_LENGTH); |
933 | | |
934 | | // save (h, k) context for responder message processing |
935 | 0 | hs->handshake_hash = h; |
936 | 0 | hs->chaining_key = *c; |
937 | 0 | } |
938 | | |
939 | | static void |
940 | | wg_process_response(tvbuff_t *tvb, wg_handshake_state_t *hs) |
941 | 0 | { |
942 | 0 | DISSECTOR_ASSERT(hs->initiator_ekey); |
943 | 0 | DISSECTOR_ASSERT(hs->initiator_skey); |
944 | 0 | DISSECTOR_ASSERT(hs->responder_ekey); |
945 | 0 | DISSECTOR_ASSERT(hs->responder_skey); |
946 | | // XXX when multiple responses are linkable to a single handshake state, |
947 | | // they should probably fork into a new state or be discarded when equal. |
948 | 0 | if (hs->initiator_recv_cipher || hs->responder_recv_cipher) { |
949 | 0 | ws_warning("FIXME multiple responses linked to a single session"); |
950 | 0 | return; |
951 | 0 | } |
952 | 0 | DISSECTOR_ASSERT(!hs->initiator_recv_cipher); |
953 | 0 | DISSECTOR_ASSERT(!hs->responder_recv_cipher); |
954 | |
|
955 | 0 | const bool has_Epriv_i = has_private_key(&hs->initiator_ekey->priv_key); |
956 | 0 | const bool has_Spriv_i = has_private_key(&hs->initiator_skey->priv_key); |
957 | 0 | const bool has_Epriv_r = has_private_key(&hs->responder_ekey->priv_key); |
958 | | |
959 | | // Either Epriv_i + Spriv_i or Epriv_r + Epub_i + Spub_i are required. |
960 | 0 | if (!(has_Epriv_i && has_Spriv_i) && !has_Epriv_r) { |
961 | 0 | return; |
962 | 0 | } |
963 | | |
964 | 0 | const wg_qqword *ephemeral = (const wg_qqword *)tvb_get_ptr(tvb, 12, WG_KEY_LEN); |
965 | 0 | const uint8_t *encrypted_empty = (const uint8_t *)tvb_get_ptr(tvb, 44, AUTH_TAG_LENGTH); |
966 | |
|
967 | 0 | wg_qqword ctk[3], h; |
968 | 0 | wg_qqword *c = &ctk[0], *t = &ctk[1], *k = &ctk[2]; |
969 | 0 | h = hs->handshake_hash; |
970 | 0 | *c = hs->chaining_key; |
971 | | |
972 | | // c = KDF1(c, msg.ephemeral) |
973 | 0 | wg_kdf(c, ephemeral->data, WG_KEY_LEN, 1, c); |
974 | | // h = Hash(h || msg.ephemeral) |
975 | 0 | wg_mix_hash(&h, ephemeral, WG_KEY_LEN); |
976 | | // dh1 = DH(Epriv_i, msg.ephemeral) if kType == I |
977 | | // dh1 = DH(Epriv_r, Epub_i) if kType == R |
978 | 0 | wg_qqword dh1; |
979 | 0 | if (has_Epriv_i && has_Spriv_i) { |
980 | 0 | dh_x25519(&dh1, &hs->initiator_ekey->priv_key, ephemeral); |
981 | 0 | } else { |
982 | 0 | dh_x25519(&dh1, &hs->responder_ekey->priv_key, &hs->initiator_ekey->pub_key); |
983 | 0 | } |
984 | | // c = KDF1(c, dh1) |
985 | 0 | wg_kdf(c, dh1.data, sizeof(dh1), 1, c); |
986 | | // dh2 = DH(Spriv_i, msg.ephemeral) if kType == I |
987 | | // dh2 = DH(Epriv_r, Spub_i) if kType == R |
988 | 0 | wg_qqword dh2; |
989 | 0 | if (has_Epriv_i && has_Spriv_i) { |
990 | 0 | dh_x25519(&dh2, &hs->initiator_skey->priv_key, ephemeral); |
991 | 0 | } else { |
992 | 0 | dh_x25519(&dh2, &hs->responder_ekey->priv_key, &hs->initiator_skey->pub_key); |
993 | 0 | } |
994 | | // c = KDF1(c, dh2) |
995 | 0 | wg_kdf(c, dh2.data, sizeof(dh2), 1, c); |
996 | 0 | wg_qqword h_before_psk = h, c_before_psk = *c, psk; |
997 | 0 | wg_psk_iter_context psk_iter = { WG_PSK_ITER_STATE_ENTER, NULL }; |
998 | 0 | while (wg_psk_iter_next(&psk_iter, hs, &psk)) { |
999 | | // c, t, k = KDF3(c, PSK) |
1000 | 0 | wg_kdf(c, psk.data, WG_KEY_LEN, 3, ctk); |
1001 | | // h = Hash(h || t) |
1002 | 0 | wg_mix_hash(&h, t, sizeof(wg_qqword)); |
1003 | | // empty = AEAD-Decrypt(k, 0, msg.empty, h) |
1004 | 0 | if (!aead_decrypt(k, 0, encrypted_empty, AUTH_TAG_LENGTH, h.data, sizeof(wg_qqword), NULL, 0)) { |
1005 | | /* Possibly bad PSK, reset and try another. */ |
1006 | 0 | h = h_before_psk; |
1007 | 0 | *c = c_before_psk; |
1008 | 0 | continue; |
1009 | 0 | } |
1010 | 0 | hs->empty_ok = true; |
1011 | 0 | break; |
1012 | 0 | } |
1013 | 0 | if (!hs->empty_ok) { |
1014 | 0 | return; |
1015 | 0 | } |
1016 | | // h = Hash(h || msg.empty) |
1017 | 0 | wg_mix_hash(&h, encrypted_empty, AUTH_TAG_LENGTH); |
1018 | | |
1019 | | // Calculate transport keys and create ciphers. |
1020 | | // (Tsend_i = Trecv_r, Trecv_i = Tsend_r) = KDF2(C, "") |
1021 | 0 | wg_qqword transport_keys[2]; |
1022 | 0 | wg_kdf(c, NULL, 0, 2, transport_keys); |
1023 | |
|
1024 | 0 | hs->initiator_recv_cipher = wg_create_cipher(&transport_keys[1]); |
1025 | 0 | hs->responder_recv_cipher = wg_create_cipher(&transport_keys[0]); |
1026 | 0 | } |
1027 | | |
1028 | | |
1029 | | static void |
1030 | | wg_sessions_insert(uint32_t id, wg_session_t *session) |
1031 | 2 | { |
1032 | 2 | wmem_list_t *list = (wmem_list_t *)wmem_map_lookup(sessions, GUINT_TO_POINTER(id)); |
1033 | 2 | if (!list) { |
1034 | 1 | list = wmem_list_new(wmem_file_scope()); |
1035 | 1 | wmem_map_insert(sessions, GUINT_TO_POINTER(id), list); |
1036 | 1 | } |
1037 | 2 | wmem_list_append(list, session); |
1038 | 2 | } |
1039 | | |
1040 | | static wg_session_t * |
1041 | | wg_session_new(void) |
1042 | 2 | { |
1043 | 2 | wg_session_t *session = wmem_new0(wmem_file_scope(), wg_session_t); |
1044 | 2 | session->stream = wg_session_count++; |
1045 | 2 | return session; |
1046 | 2 | } |
1047 | | |
1048 | | /* Updates the peer address based on the source address. */ |
1049 | | static void |
1050 | | wg_session_update_address(wg_session_t *session, packet_info *pinfo, bool sender_is_initiator) |
1051 | 2 | { |
1052 | 2 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo)); |
1053 | | |
1054 | 2 | if (sender_is_initiator) { |
1055 | 2 | copy_address_wmem(wmem_file_scope(), &session->initial.initiator_address, &pinfo->src); |
1056 | 2 | session->initial.initiator_port = (uint16_t)pinfo->srcport; |
1057 | 2 | } else { |
1058 | 0 | copy_address_wmem(wmem_file_scope(), &session->initial.responder_address, &pinfo->src); |
1059 | 0 | session->initial.responder_port = (uint16_t)pinfo->srcport; |
1060 | 0 | } |
1061 | 2 | } |
1062 | | |
1063 | | /* Finds an initiation message based on the given Receiver ID that was not |
1064 | | * previously associated with a responder message. Returns the session if a |
1065 | | * matching initiation message can be found or NULL otherwise. |
1066 | | */ |
1067 | | static wg_session_t * |
1068 | | wg_sessions_lookup_initiation(packet_info *pinfo, uint32_t receiver_id) |
1069 | 2 | { |
1070 | 2 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo)); |
1071 | | |
1072 | | /* Look for the initiation message matching this Receiver ID. */ |
1073 | 2 | wmem_list_t *list = (wmem_list_t *)wmem_map_lookup(sessions, GUINT_TO_POINTER(receiver_id)); |
1074 | 2 | if (!list) { |
1075 | 2 | return NULL; |
1076 | 2 | } |
1077 | | |
1078 | | /* Walk backwards to find the most recent message first. All packets are |
1079 | | * guaranteed to arrive before this frame because this is the first pass. */ |
1080 | 0 | for (wmem_list_frame_t *item = wmem_list_tail(list); item; item = wmem_list_frame_prev(item)) { |
1081 | 0 | wg_session_t *session = (wg_session_t *)wmem_list_frame_data(item); |
1082 | 0 | if (session->initial.initiator_port != pinfo->destport || |
1083 | 0 | !addresses_equal(&session->initial.initiator_address, &pinfo->dst)) { |
1084 | | /* Responder messages are expected to be sent to the initiator. */ |
1085 | 0 | continue; |
1086 | 0 | } |
1087 | 0 | if (session->response_frame && session->response_frame != pinfo->num) { |
1088 | | /* This session was linked elsewhere. */ |
1089 | 0 | continue; |
1090 | 0 | } |
1091 | | |
1092 | | /* This assumes no malicious messages and no contrived sequences: |
1093 | | * Any initiator or responder message is not duplicated nor are these |
1094 | | * mutated. If this must be detected, the caller could decrypt or check |
1095 | | * mac1 to distinguish valid messages. |
1096 | | */ |
1097 | 0 | return session; |
1098 | 0 | } |
1099 | | |
1100 | 0 | return NULL; |
1101 | 0 | } |
1102 | | |
1103 | | /* Finds a session with a completed handshake that matches the Receiver ID. */ |
1104 | | static wg_session_t * |
1105 | | wg_sessions_lookup(packet_info *pinfo, uint32_t receiver_id, bool *receiver_is_initiator) |
1106 | 2 | { |
1107 | 2 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo)); |
1108 | | |
1109 | 2 | wmem_list_t *list = (wmem_list_t *)wmem_map_lookup(sessions, GUINT_TO_POINTER(receiver_id)); |
1110 | 2 | if (!list) { |
1111 | 2 | return NULL; |
1112 | 2 | } |
1113 | | |
1114 | | /* Walk backwards to find the most recent message first. */ |
1115 | 0 | for (wmem_list_frame_t *item = wmem_list_tail(list); item; item = wmem_list_frame_prev(item)) { |
1116 | 0 | wg_session_t *session = (wg_session_t *)wmem_list_frame_data(item); |
1117 | 0 | if (!session->response_frame) { |
1118 | | /* Ignore sessions that are not fully established. */ |
1119 | 0 | continue; |
1120 | 0 | } |
1121 | 0 | if (session->initial.initiator_port == pinfo->destport && |
1122 | 0 | addresses_equal(&session->initial.initiator_address, &pinfo->dst)) { |
1123 | 0 | *receiver_is_initiator = true; |
1124 | 0 | } else if (session->initial.responder_port == pinfo->destport && |
1125 | 0 | addresses_equal(&session->initial.responder_address, &pinfo->dst)) { |
1126 | 0 | *receiver_is_initiator = false; |
1127 | 0 | } else { |
1128 | | /* Both peers do not match the destination, ignore. */ |
1129 | 0 | continue; |
1130 | 0 | } |
1131 | 0 | return session; |
1132 | 0 | } |
1133 | | |
1134 | 0 | return NULL; |
1135 | 0 | } |
1136 | | |
1137 | | /* |
1138 | | * Finds the static public key for the receiver of this message based on the |
1139 | | * MAC1 value. |
1140 | | * TODO on PINFO_FD_VISITED, reuse previously discovered keys from session? |
1141 | | */ |
1142 | | static const wg_skey_t * |
1143 | | wg_mac1_key_probe(wmem_allocator_t* allocator, tvbuff_t *tvb, bool is_initiation) |
1144 | 3 | { |
1145 | 3 | const int mac1_offset = is_initiation ? 116 : 60; |
1146 | | |
1147 | | // Shortcut: skip MAC1 validation if no pubkeys are configured. |
1148 | 3 | if (!wg_static_keys || g_hash_table_size(wg_static_keys) == 0) { |
1149 | 3 | return NULL; |
1150 | 3 | } |
1151 | | |
1152 | 0 | uint8_t *mac1_msgdata = (uint8_t *)tvb_memdup(allocator, tvb, 0, mac1_offset); |
1153 | 0 | const uint8_t *mac1_output = tvb_get_ptr(tvb, mac1_offset, 16); |
1154 | | |
1155 | | // MAC1 is computed over a message with three reserved bytes set to zero. |
1156 | 0 | mac1_msgdata[1] = mac1_msgdata[2] = mac1_msgdata[3] = 0; |
1157 | | |
1158 | | // Find public key that matches the 16-byte MAC1 field. |
1159 | 0 | GHashTableIter iter; |
1160 | 0 | void *value; |
1161 | 0 | g_hash_table_iter_init(&iter, wg_static_keys); |
1162 | 0 | while (g_hash_table_iter_next(&iter, NULL, &value)) { |
1163 | 0 | const wg_skey_t *skey = (wg_skey_t *)value; |
1164 | 0 | if (wg_mac_verify(&skey->mac1_key, mac1_msgdata, (unsigned)mac1_offset, mac1_output)) { |
1165 | 0 | return skey; |
1166 | 0 | } |
1167 | 0 | } |
1168 | | |
1169 | 0 | return NULL; |
1170 | 0 | } |
1171 | | |
1172 | | /* |
1173 | | * Builds the handshake decryption state when sufficient keying material is |
1174 | | * available from the initiation message. |
1175 | | */ |
1176 | | static wg_handshake_state_t * |
1177 | | wg_prepare_handshake_keys(const wg_skey_t *skey_r, tvbuff_t *tvb) |
1178 | 0 | { |
1179 | 0 | wg_handshake_state_t *hs; |
1180 | 0 | bool has_r_keys = skey_r && has_private_key(&skey_r->priv_key); |
1181 | 0 | wg_ekey_t *ekey_i = (wg_ekey_t *)wmem_map_lookup(wg_ephemeral_keys, tvb_get_ptr(tvb, 8, WG_KEY_LEN)); |
1182 | | |
1183 | | // If neither private keys are available, do not create a session. |
1184 | 0 | if (!has_r_keys && !ekey_i) { |
1185 | 0 | return NULL; |
1186 | 0 | } |
1187 | | |
1188 | | // Even if Spriv_r is available, store Epub_i for Response decryption. |
1189 | 0 | if (!ekey_i) { |
1190 | 0 | ekey_i = wmem_new0(wmem_file_scope(), wg_ekey_t); |
1191 | 0 | tvb_memcpy(tvb, ekey_i->pub_key.data, 8, WG_KEY_LEN); |
1192 | 0 | } |
1193 | |
|
1194 | 0 | hs = wmem_new0(wmem_file_scope(), wg_handshake_state_t); |
1195 | 0 | hs->responder_skey = skey_r; |
1196 | 0 | hs->initiator_ekey = ekey_i; |
1197 | 0 | wmem_register_callback(wmem_file_scope(), wg_handshake_state_destroy_cb, hs); |
1198 | 0 | return hs; |
1199 | 0 | } |
1200 | | |
1201 | | /* |
1202 | | * Processes a Response message, storing additional keys in the state. |
1203 | | */ |
1204 | | static void |
1205 | | wg_prepare_handshake_responder_keys(wg_handshake_state_t *hs, tvbuff_t *tvb) |
1206 | 0 | { |
1207 | 0 | wg_ekey_t *ekey_r = (wg_ekey_t *)wmem_map_lookup(wg_ephemeral_keys, tvb_get_ptr(tvb, 12, WG_KEY_LEN)); |
1208 | | |
1209 | | // Response decryption needs Epriv_r (or Epub_r + additional secrets). |
1210 | 0 | if (!ekey_r) { |
1211 | 0 | ekey_r = wmem_new0(wmem_file_scope(), wg_ekey_t); |
1212 | 0 | tvb_memcpy(tvb, ekey_r->pub_key.data, 12, WG_KEY_LEN); |
1213 | 0 | } |
1214 | |
|
1215 | 0 | hs->responder_ekey = ekey_r; |
1216 | 0 | } |
1217 | | |
1218 | | /* Converts a TAI64 label to the seconds since the Unix epoch. |
1219 | | * See https://cr.yp.to/libtai/tai64.html */ |
1220 | | static bool tai64n_to_unix(uint64_t tai64_label, uint32_t nanoseconds, nstime_t *nstime) |
1221 | 0 | { |
1222 | 0 | const uint64_t pow2_62 = 1ULL << 62; |
1223 | 0 | if (tai64_label < pow2_62 || tai64_label >= (1ULL << 63) || nanoseconds > 999999999) { |
1224 | | // Seconds before 1970 and values larger than 2^63 (reserved) cannot |
1225 | | // be represented. Nanoseconds must also be valid. |
1226 | 0 | return false; |
1227 | 0 | } |
1228 | | |
1229 | | // TODO this can result in loss of precision |
1230 | 0 | nstime->secs = (time_t)(tai64_label - pow2_62); |
1231 | 0 | nstime->nsecs = (int)nanoseconds; |
1232 | 0 | return true; |
1233 | 0 | } |
1234 | | |
1235 | | static void |
1236 | | wg_dissect_key_extra(proto_tree *tree, tvbuff_t *tvb, const wg_qqword *pubkey, bool is_ephemeral) |
1237 | 3 | { |
1238 | 3 | uint32_t has_private = false; |
1239 | 3 | proto_item *ti; |
1240 | | |
1241 | 3 | if (is_ephemeral) { |
1242 | 3 | wg_ekey_t *ekey = (wg_ekey_t *)wmem_map_lookup(wg_ephemeral_keys, pubkey->data); |
1243 | 3 | has_private = ekey && has_private_key(&ekey->priv_key); |
1244 | 3 | } else { |
1245 | 0 | wg_skey_t *skey = (wg_skey_t *)g_hash_table_lookup(wg_static_keys, pubkey->data); |
1246 | 0 | has_private = skey && has_private_key(&skey->priv_key); |
1247 | 0 | ti = proto_tree_add_boolean(tree, hf_wg_static_known_pubkey, tvb, 0, 0, !!skey); |
1248 | 0 | proto_item_set_generated(ti); |
1249 | 0 | } |
1250 | | |
1251 | 3 | int hf_known_privkey = is_ephemeral ? hf_wg_ephemeral_known_privkey : hf_wg_static_known_privkey; |
1252 | 3 | ti = proto_tree_add_boolean(tree, hf_known_privkey, tvb, 0, 0, has_private); |
1253 | 3 | proto_item_set_generated(ti); |
1254 | 3 | } |
1255 | | |
1256 | | |
1257 | | static void |
1258 | | wg_dissect_pubkey(proto_tree *tree, packet_info* pinfo, tvbuff_t *tvb, int offset, bool is_ephemeral) |
1259 | 3 | { |
1260 | 3 | const uint8_t *pubkey = tvb_get_ptr(tvb, offset, 32); |
1261 | 3 | char *str = g_base64_encode(pubkey, 32); |
1262 | 3 | char *key_str = wmem_strdup(pinfo->pool, str); |
1263 | 3 | g_free(str); |
1264 | | |
1265 | 3 | int hf_id = is_ephemeral ? hf_wg_ephemeral : hf_wg_static; |
1266 | 3 | proto_item *ti = proto_tree_add_string(tree, hf_id, tvb, offset, 32, key_str); |
1267 | 3 | if (wg_decryption_supported) { |
1268 | 3 | proto_tree *key_tree = proto_item_add_subtree(ti, ett_key_info); |
1269 | 3 | wg_dissect_key_extra(key_tree, tvb, (const wg_qqword *)pubkey, is_ephemeral); |
1270 | 3 | } else { |
1271 | 0 | expert_add_info(NULL, ti, &ei_wg_decryption_unsupported); |
1272 | 0 | } |
1273 | 3 | } |
1274 | | |
1275 | | static void |
1276 | | wg_dissect_decrypted_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_handshake_state_t *hs) |
1277 | 2 | { |
1278 | 2 | tvbuff_t *new_tvb; |
1279 | | |
1280 | 2 | if (!hs || !hs->initiator_skey) { |
1281 | 2 | return; |
1282 | 2 | } |
1283 | | |
1284 | 0 | new_tvb = tvb_new_child_real_data(tvb, hs->initiator_skey->pub_key.data, WG_KEY_LEN, WG_KEY_LEN); |
1285 | 0 | add_new_data_source(pinfo, new_tvb, "Decrypted Static"); |
1286 | 0 | wg_dissect_pubkey(wg_tree, pinfo, new_tvb, 0, false); |
1287 | 0 | } |
1288 | | |
1289 | | static void |
1290 | | wg_dissect_decrypted_timestamp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, wg_handshake_state_t *hs) |
1291 | 2 | { |
1292 | 2 | uint64_t tai64_label; |
1293 | 2 | uint32_t nanoseconds; |
1294 | 2 | nstime_t nstime; |
1295 | 2 | proto_item *ti; |
1296 | 2 | tvbuff_t *new_tvb; |
1297 | | |
1298 | 2 | if (!hs || !hs->timestamp_ok) { |
1299 | 2 | return; |
1300 | 2 | } |
1301 | | |
1302 | 0 | new_tvb = tvb_new_child_real_data(tvb, hs->timestamp, sizeof(hs->timestamp), sizeof(hs->timestamp)); |
1303 | 0 | add_new_data_source(pinfo, new_tvb, "Decrypted Timestamp"); |
1304 | |
|
1305 | 0 | tai64_label = tvb_get_uint64(new_tvb, 0, ENC_BIG_ENDIAN); |
1306 | 0 | nanoseconds = tvb_get_uint32(new_tvb, 8, ENC_BIG_ENDIAN); |
1307 | 0 | if (tai64n_to_unix(tai64_label, nanoseconds, &nstime)) { |
1308 | 0 | ti = proto_tree_add_time(tree, hf_wg_timestamp_value, new_tvb, 0, 12, &nstime); |
1309 | 0 | tree = proto_item_add_subtree(ti, ett_timestamp); |
1310 | 0 | } |
1311 | 0 | proto_tree_add_item(tree, hf_wg_timestamp_tai64_label, new_tvb, 0, 8, ENC_BIG_ENDIAN); |
1312 | 0 | proto_tree_add_item(tree, hf_wg_timestamp_nanoseconds, new_tvb, 8, 4, ENC_BIG_ENDIAN); |
1313 | 0 | } |
1314 | | |
1315 | | static void |
1316 | | wg_dissect_decrypted_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_packet_info_t *wg_pinfo, uint64_t counter, int plain_length) |
1317 | 0 | { |
1318 | 0 | wg_handshake_state_t *hs = wg_pinfo->session->hs; |
1319 | 0 | gcry_cipher_hd_t cipher = wg_pinfo->receiver_is_initiator ? hs->initiator_recv_cipher : hs->responder_recv_cipher; |
1320 | 0 | if (!cipher) { |
1321 | 0 | return; |
1322 | 0 | } |
1323 | | |
1324 | 0 | DISSECTOR_ASSERT(plain_length >= 0); |
1325 | 0 | const int ctext_len = plain_length + AUTH_TAG_LENGTH; |
1326 | 0 | const unsigned char *ctext = tvb_get_ptr(tvb, 16, ctext_len); |
1327 | 0 | unsigned char *plain = (unsigned char *)wmem_alloc0(pinfo->pool, (unsigned)plain_length); |
1328 | 0 | if (!wg_aead_decrypt(cipher, counter, ctext, (unsigned)ctext_len, NULL, 0, plain, (unsigned)plain_length)) { |
1329 | 0 | proto_tree_add_expert(wg_tree, pinfo, &ei_wg_decryption_error, tvb, 16, ctext_len); |
1330 | 0 | return; |
1331 | 0 | } |
1332 | 0 | if (plain_length == 0) { |
1333 | 0 | return; |
1334 | 0 | } |
1335 | | |
1336 | 0 | tvbuff_t *new_tvb = tvb_new_child_real_data(tvb, plain, (unsigned)plain_length, plain_length); |
1337 | 0 | add_new_data_source(pinfo, new_tvb, "Decrypted Packet"); |
1338 | |
|
1339 | 0 | proto_tree *tree = proto_item_get_parent(wg_tree); |
1340 | 0 | if (!pref_dissect_packet) { |
1341 | | // (IP packet not shown, preference "Dissect transport data" is disabled) |
1342 | 0 | call_data_dissector(new_tvb, pinfo, tree); |
1343 | 0 | } else { |
1344 | 0 | call_dissector(ip_handle, new_tvb, pinfo, tree); |
1345 | 0 | } |
1346 | 0 | } |
1347 | | |
1348 | | static void |
1349 | | wg_dissect_mac1_pubkey(proto_tree *tree, packet_info* pinfo, tvbuff_t *tvb, const wg_skey_t *skey) |
1350 | 3 | { |
1351 | 3 | proto_item *ti; |
1352 | | |
1353 | 3 | if (!skey) { |
1354 | 3 | return; |
1355 | 3 | } |
1356 | | |
1357 | 0 | ti = proto_tree_add_string(tree, hf_wg_receiver_pubkey, tvb, 0, 0, pubkey_to_string(pinfo->pool, &skey->pub_key)); |
1358 | 0 | proto_item_set_generated(ti); |
1359 | 0 | proto_tree *key_tree = proto_item_add_subtree(ti, ett_key_info); |
1360 | 0 | ti = proto_tree_add_boolean(key_tree, hf_wg_receiver_pubkey_known_privkey, tvb, 0, 0, !!has_private_key(&skey->priv_key)); |
1361 | 0 | proto_item_set_generated(ti); |
1362 | 0 | } |
1363 | | |
1364 | | static int |
1365 | | wg_dissect_handshake_initiation(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_packet_info_t *wg_pinfo) |
1366 | 2 | { |
1367 | 2 | uint32_t sender_id; |
1368 | 2 | proto_item *ti; |
1369 | | |
1370 | 2 | wg_keylog_read(); |
1371 | 2 | const wg_skey_t *skey_r = wg_mac1_key_probe(pinfo->pool, tvb, true); |
1372 | 2 | wg_handshake_state_t *hs = NULL; |
1373 | | |
1374 | 2 | if (!PINFO_FD_VISITED(pinfo)) { |
1375 | 2 | if (skey_r) { |
1376 | 0 | hs = wg_prepare_handshake_keys(skey_r, tvb); |
1377 | 0 | if (hs) { |
1378 | 0 | wg_process_initiation(tvb, hs); |
1379 | 0 | } |
1380 | 0 | } |
1381 | 2 | } else if (wg_pinfo && wg_pinfo->session) { |
1382 | 0 | hs = wg_pinfo->session->hs; |
1383 | 0 | } |
1384 | | |
1385 | 2 | proto_tree_add_item_ret_uint(wg_tree, hf_wg_sender, tvb, 4, 4, ENC_LITTLE_ENDIAN, &sender_id); |
1386 | 2 | col_append_fstr(pinfo->cinfo, COL_INFO, ", sender=0x%08X", sender_id); |
1387 | 2 | wg_dissect_pubkey(wg_tree, pinfo, tvb, 8, true); |
1388 | 2 | proto_tree_add_item(wg_tree, hf_wg_encrypted_static, tvb, 40, 32 + AUTH_TAG_LENGTH, ENC_NA); |
1389 | 2 | wg_dissect_decrypted_static(tvb, pinfo, wg_tree, hs); |
1390 | 2 | proto_tree_add_item(wg_tree, hf_wg_encrypted_timestamp, tvb, 88, 12 + AUTH_TAG_LENGTH, ENC_NA); |
1391 | 2 | wg_dissect_decrypted_timestamp(tvb, pinfo, wg_tree, hs); |
1392 | 2 | proto_tree_add_item(wg_tree, hf_wg_mac1, tvb, 116, 16, ENC_NA); |
1393 | 2 | wg_dissect_mac1_pubkey(wg_tree, pinfo, tvb, skey_r); |
1394 | 2 | proto_tree_add_item(wg_tree, hf_wg_mac2, tvb, 132, 16, ENC_NA); |
1395 | | |
1396 | 2 | if (!PINFO_FD_VISITED(pinfo)) { |
1397 | | /* XXX should an initiation message with the same contents (except MAC2) be |
1398 | | * considered part of the same "session"? */ |
1399 | 2 | wg_session_t *session = wg_session_new(); |
1400 | 2 | session->initiator_frame = pinfo->num; |
1401 | 2 | wg_session_update_address(session, pinfo, true); |
1402 | 2 | session->hs = hs; |
1403 | 2 | wg_sessions_insert(sender_id, session); |
1404 | 2 | wg_pinfo->session = session; |
1405 | 2 | } |
1406 | 2 | wg_session_t *session = wg_pinfo ? wg_pinfo->session : NULL; |
1407 | 2 | if (session) { |
1408 | 2 | ti = proto_tree_add_uint(wg_tree, hf_wg_stream, tvb, 0, 0, session->stream); |
1409 | 2 | proto_item_set_generated(ti); |
1410 | 2 | } |
1411 | 2 | if (session && session->response_frame) { |
1412 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_response_in, tvb, 0, 0, session->response_frame); |
1413 | 0 | proto_item_set_generated(ti); |
1414 | 0 | } |
1415 | | |
1416 | 2 | return 148; |
1417 | 2 | } |
1418 | | |
1419 | | static int |
1420 | | wg_dissect_handshake_response(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_packet_info_t *wg_pinfo) |
1421 | 1 | { |
1422 | 1 | uint32_t sender_id, receiver_id; |
1423 | 1 | proto_item *ti; |
1424 | 1 | wg_session_t *session; |
1425 | | |
1426 | 1 | wg_keylog_read(); |
1427 | 1 | const wg_skey_t *skey_i = wg_mac1_key_probe(pinfo->pool, tvb, false); |
1428 | | |
1429 | 1 | proto_tree_add_item_ret_uint(wg_tree, hf_wg_sender, tvb, 4, 4, ENC_LITTLE_ENDIAN, &sender_id); |
1430 | 1 | col_append_fstr(pinfo->cinfo, COL_INFO, ", sender=0x%08X", sender_id); |
1431 | 1 | proto_tree_add_item_ret_uint(wg_tree, hf_wg_receiver, tvb, 8, 4, ENC_LITTLE_ENDIAN, &receiver_id); |
1432 | 1 | col_append_fstr(pinfo->cinfo, COL_INFO, ", receiver=0x%08X", receiver_id); |
1433 | | |
1434 | 1 | if (!PINFO_FD_VISITED(pinfo)) { |
1435 | 1 | session = wg_sessions_lookup_initiation(pinfo, receiver_id); |
1436 | 1 | if (session && session->hs) { |
1437 | 0 | wg_prepare_handshake_responder_keys(session->hs, tvb); |
1438 | 0 | wg_process_response(tvb, session->hs); |
1439 | 0 | } |
1440 | 1 | } else { |
1441 | 0 | session = wg_pinfo ? wg_pinfo->session : NULL; |
1442 | 0 | } |
1443 | | |
1444 | 1 | wg_dissect_pubkey(wg_tree, pinfo, tvb, 12, true); |
1445 | 1 | proto_tree_add_item(wg_tree, hf_wg_encrypted_empty, tvb, 44, 16, ENC_NA); |
1446 | 1 | if (session && session->hs) { |
1447 | 0 | ti = proto_tree_add_boolean(wg_tree, hf_wg_handshake_ok, tvb, 0, 0, !!session->hs->empty_ok); |
1448 | 0 | proto_item_set_generated(ti); |
1449 | 0 | } |
1450 | 1 | proto_tree_add_item(wg_tree, hf_wg_mac1, tvb, 60, 16, ENC_NA); |
1451 | 1 | wg_dissect_mac1_pubkey(wg_tree, pinfo, tvb, skey_i); |
1452 | 1 | proto_tree_add_item(wg_tree, hf_wg_mac2, tvb, 76, 16, ENC_NA); |
1453 | | |
1454 | 1 | if (!PINFO_FD_VISITED(pinfo)) { |
1455 | | /* XXX should probably check whether decryption succeeds before linking |
1456 | | * and somehow mark that this response is related but not correct. */ |
1457 | 1 | if (session) { |
1458 | 0 | session->response_frame = pinfo->num; |
1459 | 0 | wg_session_update_address(session, pinfo, false); |
1460 | 0 | wg_sessions_insert(sender_id, session); |
1461 | 0 | wg_pinfo->session = session; |
1462 | 0 | } |
1463 | 1 | } |
1464 | 1 | if (session) { |
1465 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_stream, tvb, 0, 0, session->stream); |
1466 | 0 | proto_item_set_generated(ti); |
1467 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_response_to, tvb, 0, 0, session->initiator_frame); |
1468 | 0 | proto_item_set_generated(ti); |
1469 | 0 | } |
1470 | | |
1471 | 1 | return 92; |
1472 | 1 | } |
1473 | | |
1474 | | static int |
1475 | | wg_dissect_handshake_cookie(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_packet_info_t *wg_pinfo) |
1476 | 1 | { |
1477 | 1 | uint32_t receiver_id; |
1478 | 1 | proto_item *ti; |
1479 | | |
1480 | 1 | proto_tree_add_item_ret_uint(wg_tree, hf_wg_receiver, tvb, 4, 4, ENC_LITTLE_ENDIAN, &receiver_id); |
1481 | 1 | col_append_fstr(pinfo->cinfo, COL_INFO, ", receiver=0x%08X", receiver_id); |
1482 | 1 | proto_tree_add_item(wg_tree, hf_wg_nonce, tvb, 8, 24, ENC_NA); |
1483 | 1 | proto_tree_add_item(wg_tree, hf_wg_encrypted_cookie, tvb, 32, 16 + AUTH_TAG_LENGTH, ENC_NA); |
1484 | | |
1485 | 1 | wg_session_t *session; |
1486 | 1 | if (!PINFO_FD_VISITED(pinfo)) { |
1487 | | /* Check for Cookie Reply from Responder to Initiator. */ |
1488 | 1 | session = wg_sessions_lookup_initiation(pinfo, receiver_id); |
1489 | 1 | if (session) { |
1490 | 0 | session->response_frame = pinfo->num; |
1491 | 0 | wg_session_update_address(session, pinfo, false); |
1492 | 0 | wg_pinfo->session = session; |
1493 | 0 | } |
1494 | | /* XXX check for cookie reply from Initiator to Responder */ |
1495 | 1 | } else { |
1496 | 0 | session = wg_pinfo ? wg_pinfo->session : NULL; |
1497 | 0 | } |
1498 | 1 | if (session) { |
1499 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_stream, tvb, 0, 0, session->stream); |
1500 | 0 | proto_item_set_generated(ti); |
1501 | | /* XXX check for cookie reply from Initiator to Responder */ |
1502 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_response_to, tvb, 0, 0, session->initiator_frame); |
1503 | 0 | proto_item_set_generated(ti); |
1504 | 0 | } |
1505 | | |
1506 | 1 | return 64; |
1507 | 1 | } |
1508 | | |
1509 | | static int |
1510 | | wg_dissect_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *wg_tree, wg_packet_info_t *wg_pinfo) |
1511 | 2 | { |
1512 | 2 | uint32_t receiver_id; |
1513 | 2 | uint64_t counter; |
1514 | 2 | proto_item *ti; |
1515 | | |
1516 | 2 | proto_tree_add_item_ret_uint(wg_tree, hf_wg_receiver, tvb, 4, 4, ENC_LITTLE_ENDIAN, &receiver_id); |
1517 | 2 | col_append_fstr(pinfo->cinfo, COL_INFO, ", receiver=0x%08X", receiver_id); |
1518 | 2 | proto_tree_add_item_ret_uint64(wg_tree, hf_wg_counter, tvb, 8, 8, ENC_LITTLE_ENDIAN, &counter); |
1519 | 2 | col_append_fstr(pinfo->cinfo, COL_INFO, ", counter=%" PRIu64, counter); |
1520 | | |
1521 | 2 | int packet_length = tvb_captured_length_remaining(tvb, 16); |
1522 | 2 | if (packet_length < AUTH_TAG_LENGTH) { |
1523 | 0 | proto_tree_add_expert(wg_tree, pinfo, &ei_wg_bad_packet_length, tvb, 16, packet_length); |
1524 | 0 | return 16 + packet_length; |
1525 | 2 | } else if (packet_length != AUTH_TAG_LENGTH) { |
1526 | | /* Keepalive messages are already marked, no need to append data length. */ |
1527 | 1 | col_append_fstr(pinfo->cinfo, COL_INFO, ", datalen=%d", packet_length - AUTH_TAG_LENGTH); |
1528 | 1 | } |
1529 | 2 | ti = proto_tree_add_item(wg_tree, hf_wg_encrypted_packet, tvb, 16, packet_length, ENC_NA); |
1530 | | |
1531 | 2 | if (packet_length == AUTH_TAG_LENGTH) { |
1532 | 1 | expert_add_info(pinfo, ti, &ei_wg_keepalive); |
1533 | 1 | } |
1534 | | |
1535 | 2 | wg_session_t *session; |
1536 | 2 | if (!PINFO_FD_VISITED(pinfo)) { |
1537 | 2 | bool receiver_is_initiator; |
1538 | 2 | session = wg_sessions_lookup(pinfo, receiver_id, &receiver_is_initiator); |
1539 | 2 | if (session) { |
1540 | 0 | wg_session_update_address(session, pinfo, !receiver_is_initiator); |
1541 | 0 | wg_pinfo->session = session; |
1542 | 0 | wg_pinfo->receiver_is_initiator = receiver_is_initiator; |
1543 | 0 | } |
1544 | 2 | } else { |
1545 | 0 | session = wg_pinfo ? wg_pinfo->session : NULL; |
1546 | 0 | } |
1547 | 2 | if (session) { |
1548 | 0 | ti = proto_tree_add_uint(wg_tree, hf_wg_stream, tvb, 0, 0, session->stream); |
1549 | 0 | proto_item_set_generated(ti); |
1550 | 0 | } |
1551 | | |
1552 | 2 | if (session && session->hs) { |
1553 | 0 | wg_dissect_decrypted_packet(tvb, pinfo, wg_tree, wg_pinfo, counter, packet_length - AUTH_TAG_LENGTH); |
1554 | 0 | } |
1555 | | |
1556 | 2 | return 16 + packet_length; |
1557 | 2 | } |
1558 | | |
1559 | | static bool |
1560 | | wg_is_valid_message_length(uint8_t message_type, unsigned length) |
1561 | 1.54k | { |
1562 | 1.54k | switch (message_type) { |
1563 | 27 | case WG_TYPE_HANDSHAKE_INITIATION: |
1564 | 27 | return length == 148; |
1565 | 26 | case WG_TYPE_HANDSHAKE_RESPONSE: |
1566 | 26 | return length == 92; |
1567 | 27 | case WG_TYPE_COOKIE_REPLY: |
1568 | 27 | return length == 64; |
1569 | 54 | case WG_TYPE_TRANSPORT_DATA: |
1570 | 54 | return length >= 32; |
1571 | 1.41k | default: |
1572 | 1.41k | return false; |
1573 | 1.54k | } |
1574 | 1.54k | } |
1575 | | |
1576 | | static int |
1577 | | dissect_wg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) |
1578 | 6 | { |
1579 | 6 | proto_item *ti; |
1580 | 6 | proto_tree *wg_tree; |
1581 | 6 | uint32_t message_type; |
1582 | 6 | const char *message_type_str; |
1583 | 6 | wg_packet_info_t *wg_pinfo; |
1584 | | |
1585 | 6 | message_type = tvb_get_uint8(tvb, 0); |
1586 | 6 | message_type_str = try_val_to_str(message_type, wg_type_names); |
1587 | 6 | if (!message_type_str) |
1588 | 0 | return 0; |
1589 | | |
1590 | 6 | if (!wg_is_valid_message_length(message_type, tvb_reported_length(tvb))) { |
1591 | 0 | return 0; |
1592 | 0 | } |
1593 | | |
1594 | | /* Special case: zero-length data message is a Keepalive message. */ |
1595 | 6 | if (message_type == WG_TYPE_TRANSPORT_DATA && tvb_reported_length(tvb) == 32) { |
1596 | 1 | message_type_str = "Keepalive"; |
1597 | 1 | } |
1598 | | |
1599 | 6 | col_set_str(pinfo->cinfo, COL_PROTOCOL, "WireGuard"); |
1600 | 6 | col_set_str(pinfo->cinfo, COL_INFO, message_type_str); |
1601 | | |
1602 | 6 | ti = proto_tree_add_item(tree, proto_wg, tvb, 0, -1, ENC_NA); |
1603 | 6 | wg_tree = proto_item_add_subtree(ti, ett_wg); |
1604 | | |
1605 | 6 | proto_tree_add_item(wg_tree, hf_wg_type, tvb, 0, 1, ENC_NA); |
1606 | 6 | proto_tree_add_item(wg_tree, hf_wg_reserved, tvb, 1, 3, ENC_NA); |
1607 | | |
1608 | 6 | if (!PINFO_FD_VISITED(pinfo)) { |
1609 | 6 | wg_pinfo = wmem_new0(wmem_file_scope(), wg_packet_info_t); |
1610 | 6 | p_add_proto_data(wmem_file_scope(), pinfo, proto_wg, 0, wg_pinfo); |
1611 | 6 | } else { |
1612 | | /* |
1613 | | * Note: this may be NULL if the heuristics dissector sets a |
1614 | | * conversation dissector later in the stream, for example due to a new |
1615 | | * Handshake Initiation message. Previous messages are potentially |
1616 | | * Transport Data messages which might not be detected through |
1617 | | * heuristics. |
1618 | | */ |
1619 | 0 | wg_pinfo = (wg_packet_info_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto_wg, 0); |
1620 | 0 | } |
1621 | | |
1622 | 6 | switch ((wg_message_type)message_type) { |
1623 | 2 | case WG_TYPE_HANDSHAKE_INITIATION: |
1624 | 2 | return wg_dissect_handshake_initiation(tvb, pinfo, wg_tree, wg_pinfo); |
1625 | 1 | case WG_TYPE_HANDSHAKE_RESPONSE: |
1626 | 1 | return wg_dissect_handshake_response(tvb, pinfo, wg_tree, wg_pinfo); |
1627 | 1 | case WG_TYPE_COOKIE_REPLY: |
1628 | 1 | return wg_dissect_handshake_cookie(tvb, pinfo, wg_tree, wg_pinfo); |
1629 | 2 | case WG_TYPE_TRANSPORT_DATA: |
1630 | 2 | return wg_dissect_data(tvb, pinfo, wg_tree, wg_pinfo); |
1631 | 6 | } |
1632 | | |
1633 | 0 | DISSECTOR_ASSERT_NOT_REACHED(); |
1634 | 0 | } |
1635 | | |
1636 | | static bool |
1637 | | dissect_wg_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) |
1638 | 1.74k | { |
1639 | | /* |
1640 | | * Heuristics to detect the WireGuard protocol: |
1641 | | * - The first byte must be one of the valid four messages. |
1642 | | * - The total packet length depends on the message type, and is fixed for |
1643 | | * three of them. The Data type has a minimum length however. |
1644 | | * - The next three bytes are reserved and zero in the official protocol. |
1645 | | * Cloudflare's implementation however uses this field for load balancing |
1646 | | * purposes, so this condition is not checked here for most messages. |
1647 | | * It is checked for data messages to avoid false positives. |
1648 | | */ |
1649 | 1.74k | uint32_t message_type; |
1650 | 1.74k | bool reserved_is_zeroes; |
1651 | | |
1652 | 1.74k | if (tvb_reported_length(tvb) < 4) |
1653 | 206 | return false; |
1654 | | |
1655 | 1.53k | message_type = tvb_get_uint8(tvb, 0); |
1656 | 1.53k | reserved_is_zeroes = tvb_get_ntoh24(tvb, 1) == 0; |
1657 | | |
1658 | 1.53k | if (!wg_is_valid_message_length(message_type, tvb_reported_length(tvb))) { |
1659 | 1.50k | return false; |
1660 | 1.50k | } |
1661 | | |
1662 | 37 | switch (message_type) { |
1663 | 1 | case WG_TYPE_COOKIE_REPLY: |
1664 | 35 | case WG_TYPE_TRANSPORT_DATA: |
1665 | 35 | if (!reserved_is_zeroes) |
1666 | 32 | return false; |
1667 | 3 | break; |
1668 | 37 | } |
1669 | | |
1670 | | /* |
1671 | | * Assuming that this is a new handshake, make sure that future messages are |
1672 | | * directed to our dissector. This ensures that cookie replies and data |
1673 | | * messages using non-zero reserved bytes are still properly recognized. |
1674 | | * An edge case occurs when the address or port change. In that case, Data |
1675 | | * messages using non-zero reserved bytes will not be recognized. The user |
1676 | | * can use Decode As for this case. |
1677 | | */ |
1678 | 5 | if (message_type == WG_TYPE_HANDSHAKE_INITIATION) { |
1679 | 1 | conversation_t *conversation = find_or_create_conversation(pinfo); |
1680 | 1 | conversation_set_dissector(conversation, wg_handle); |
1681 | 1 | } |
1682 | | |
1683 | 5 | dissect_wg(tvb, pinfo, tree, data); |
1684 | 5 | return true; |
1685 | 37 | } |
1686 | | |
1687 | | static void |
1688 | | wg_init(void) |
1689 | 15 | { |
1690 | 15 | wg_session_count = 0; |
1691 | 15 | } |
1692 | | |
1693 | | void |
1694 | | proto_register_wg(void) |
1695 | 15 | { |
1696 | 15 | module_t *wg_module; |
1697 | 15 | expert_module_t *expert_wg; |
1698 | | |
1699 | 15 | static hf_register_info hf[] = { |
1700 | | /* Initiation message */ |
1701 | 15 | { &hf_wg_type, |
1702 | 15 | { "Type", "wg.type", |
1703 | 15 | FT_UINT8, BASE_DEC, VALS(wg_type_names), 0x0, |
1704 | 15 | NULL, HFILL } |
1705 | 15 | }, |
1706 | 15 | { &hf_wg_reserved, |
1707 | 15 | { "Reserved", "wg.reserved", |
1708 | 15 | FT_BYTES, BASE_NONE, NULL, 0x0, |
1709 | 15 | NULL, HFILL } |
1710 | 15 | }, |
1711 | 15 | { &hf_wg_sender, |
1712 | 15 | { "Sender", "wg.sender", |
1713 | 15 | FT_UINT32, BASE_HEX, NULL, 0x0, |
1714 | 15 | "Identifier as chosen by the sender", HFILL } |
1715 | 15 | }, |
1716 | 15 | { &hf_wg_ephemeral, |
1717 | 15 | { "Ephemeral", "wg.ephemeral", |
1718 | 15 | FT_STRING, BASE_NONE, NULL, 0x0, |
1719 | 15 | "Ephemeral public key of sender", HFILL } |
1720 | 15 | }, |
1721 | 15 | { &hf_wg_encrypted_static, |
1722 | 15 | { "Encrypted Static", "wg.encrypted_static", |
1723 | 15 | FT_NONE, BASE_NONE, NULL, 0x0, |
1724 | 15 | "Encrypted long-term static public key of sender", HFILL } |
1725 | 15 | }, |
1726 | 15 | { &hf_wg_static, |
1727 | 15 | { "Static Public Key", "wg.static", |
1728 | 15 | FT_STRING, BASE_NONE, NULL, 0x0, |
1729 | 15 | "Long-term static public key of sender", HFILL } |
1730 | 15 | }, |
1731 | 15 | { &hf_wg_encrypted_timestamp, |
1732 | 15 | { "Encrypted Timestamp", "wg.encrypted_timestamp", |
1733 | 15 | FT_NONE, BASE_NONE, NULL, 0x0, |
1734 | 15 | NULL, HFILL } |
1735 | 15 | }, |
1736 | 15 | { &hf_wg_timestamp_tai64_label, |
1737 | 15 | { "TAI64 Label", "wg.timestamp.tai64_label", |
1738 | 15 | FT_UINT64, BASE_DEC, NULL, 0x0, |
1739 | 15 | NULL, HFILL } |
1740 | 15 | }, |
1741 | 15 | { &hf_wg_timestamp_nanoseconds, |
1742 | 15 | { "Nanoseconds", "wg.timestamp.nanoseconds", |
1743 | 15 | FT_UINT32, BASE_DEC, NULL, 0x0, |
1744 | 15 | NULL, HFILL } |
1745 | 15 | }, |
1746 | 15 | { &hf_wg_timestamp_value, |
1747 | 15 | { "Timestamp", "wg.timestamp.value", |
1748 | 15 | FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0x0, |
1749 | 15 | NULL, HFILL } |
1750 | 15 | }, |
1751 | 15 | { &hf_wg_mac1, |
1752 | 15 | { "mac1", "wg.mac1", |
1753 | 15 | FT_BYTES, BASE_NONE, NULL, 0x0, |
1754 | 15 | NULL, HFILL } |
1755 | 15 | }, |
1756 | 15 | { &hf_wg_mac2, |
1757 | 15 | { "mac2", "wg.mac2", |
1758 | 15 | FT_BYTES, BASE_NONE, NULL, 0x0, |
1759 | 15 | NULL, HFILL } |
1760 | 15 | }, |
1761 | | |
1762 | | /* Response message */ |
1763 | 15 | { &hf_wg_receiver, |
1764 | 15 | { "Receiver", "wg.receiver", |
1765 | 15 | FT_UINT32, BASE_HEX, NULL, 0x0, |
1766 | 15 | "Identifier as chosen by receiver", HFILL } |
1767 | 15 | }, |
1768 | 15 | { &hf_wg_encrypted_empty, |
1769 | 15 | { "Encrypted Empty", "wg.encrypted_empty", |
1770 | 15 | FT_NONE, BASE_NONE, NULL, 0x0, |
1771 | 15 | "Authenticated encryption of an empty string", HFILL } |
1772 | 15 | }, |
1773 | 15 | { &hf_wg_handshake_ok, |
1774 | 15 | { "Handshake decryption successful", "wg.handshake_ok", |
1775 | 15 | FT_BOOLEAN, BASE_NONE, NULL, 0x0, |
1776 | 15 | "Whether decryption keys were successfully derived", HFILL } |
1777 | 15 | }, |
1778 | | |
1779 | | /* Cookie message */ |
1780 | 15 | { &hf_wg_nonce, |
1781 | 15 | { "Nonce", "wg.nonce", |
1782 | 15 | FT_BYTES, BASE_NONE, NULL, 0x0, |
1783 | 15 | NULL, HFILL } |
1784 | 15 | }, |
1785 | 15 | { &hf_wg_encrypted_cookie, |
1786 | 15 | { "Encrypted Cookie", "wg.encrypted_cookie", |
1787 | 15 | FT_BYTES, BASE_NONE, NULL, 0x0, |
1788 | 15 | NULL, HFILL } |
1789 | 15 | }, |
1790 | | /* TODO decrypted cookie field. */ |
1791 | | |
1792 | | /* Data message */ |
1793 | 15 | { &hf_wg_counter, |
1794 | 15 | { "Counter", "wg.counter", |
1795 | 15 | FT_UINT64, BASE_DEC, NULL, 0x0, |
1796 | 15 | NULL, HFILL } |
1797 | 15 | }, |
1798 | 15 | { &hf_wg_encrypted_packet, |
1799 | 15 | { "Encrypted Packet", "wg.encrypted_packet", |
1800 | 15 | FT_NONE, BASE_NONE, NULL, 0x0, |
1801 | 15 | NULL, HFILL } |
1802 | 15 | }, |
1803 | | |
1804 | | /* Association tracking. */ |
1805 | 15 | { &hf_wg_stream, |
1806 | 15 | { "Stream index", "wg.stream", |
1807 | 15 | FT_UINT32, BASE_DEC, NULL, 0x0, |
1808 | 15 | "Identifies a session in this capture file", HFILL } |
1809 | 15 | }, |
1810 | 15 | { &hf_wg_response_in, |
1811 | 15 | { "Response in Frame", "wg.response_in", |
1812 | 15 | FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RESPONSE), 0x0, |
1813 | 15 | "The response to this initiation message is in this frame", HFILL } |
1814 | 15 | }, |
1815 | 15 | { &hf_wg_response_to, |
1816 | 15 | { "Response to Frame", "wg.response_to", |
1817 | 15 | FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_REQUEST), 0x0, |
1818 | 15 | "This is a response to the initiation message in this frame", HFILL } |
1819 | 15 | }, |
1820 | | |
1821 | | /* Additional fields. */ |
1822 | 15 | { &hf_wg_receiver_pubkey, |
1823 | 15 | { "Receiver Static Public Key", "wg.receiver_pubkey", |
1824 | 15 | FT_STRING, BASE_NONE, NULL, 0x0, |
1825 | 15 | "Public key of the receiver (matched based on MAC1)", HFILL } |
1826 | 15 | }, |
1827 | 15 | { &hf_wg_receiver_pubkey_known_privkey, |
1828 | 15 | { "Has Private Key", "wg.receiver_pubkey.known_privkey", |
1829 | 15 | FT_BOOLEAN, BASE_NONE, NULL, 0x0, |
1830 | 15 | "Whether the corresponding private key is known (configured via prefs)", HFILL } |
1831 | 15 | }, |
1832 | 15 | { &hf_wg_ephemeral_known_privkey, |
1833 | 15 | { "Has Private Key", "wg.ephemeral.known_privkey", |
1834 | 15 | FT_BOOLEAN, BASE_NONE, NULL, 0x0, |
1835 | 15 | "Whether the corresponding private key is known (configured via prefs)", HFILL } |
1836 | 15 | }, |
1837 | 15 | { &hf_wg_static_known_pubkey, |
1838 | 15 | { "Known Public Key", "wg.static.known_pubkey", |
1839 | 15 | FT_BOOLEAN, BASE_NONE, NULL, 0x0, |
1840 | 15 | "Whether this public key is known (configured via prefs)", HFILL } |
1841 | 15 | }, |
1842 | 15 | { &hf_wg_static_known_privkey, |
1843 | 15 | { "Has Private Key", "wg.static.known_privkey", |
1844 | 15 | FT_BOOLEAN, BASE_NONE, NULL, 0x0, |
1845 | 15 | "Whether the corresponding private key is known (configured via prefs)", HFILL } |
1846 | 15 | }, |
1847 | 15 | }; |
1848 | | |
1849 | 15 | static int *ett[] = { |
1850 | 15 | &ett_wg, |
1851 | 15 | &ett_timestamp, |
1852 | 15 | &ett_key_info, |
1853 | 15 | }; |
1854 | | |
1855 | 15 | static ei_register_info ei[] = { |
1856 | 15 | { &ei_wg_bad_packet_length, |
1857 | 15 | { "wg.bad_packet_length", PI_MALFORMED, PI_ERROR, |
1858 | 15 | "Packet length is too small", EXPFILL } |
1859 | 15 | }, |
1860 | 15 | { &ei_wg_keepalive, |
1861 | 15 | { "wg.keepalive", PI_SEQUENCE, PI_CHAT, |
1862 | 15 | "This is a Keepalive message", EXPFILL } |
1863 | 15 | }, |
1864 | 15 | { &ei_wg_decryption_error, |
1865 | 15 | { "wg.decryption_error", PI_DECRYPTION, PI_WARN, |
1866 | 15 | "Packet data decryption failed", EXPFILL } |
1867 | 15 | }, |
1868 | 15 | { &ei_wg_decryption_unsupported, |
1869 | 15 | { "wg.decryption_unsupported", PI_DECRYPTION, PI_WARN, |
1870 | 15 | "Decryption unsupported (disable FIPS mode or upgrade Libgcrypt to 1.10.0 or higher)", EXPFILL } |
1871 | 15 | }, |
1872 | 15 | }; |
1873 | | |
1874 | | /* UAT for header fields */ |
1875 | 15 | static uat_field_t wg_key_uat_fields[] = { |
1876 | 15 | UAT_FLD_VS(wg_key_uat, key_type, "Key type", wg_key_uat_type_vals, "Public or Private"), |
1877 | 15 | UAT_FLD_CSTRING(wg_key_uat, key, "Key", "Base64-encoded key"), |
1878 | 15 | UAT_END_FIELDS |
1879 | 15 | }; |
1880 | | |
1881 | 15 | proto_wg = proto_register_protocol("WireGuard Protocol", "WireGuard", "wg"); |
1882 | | |
1883 | 15 | proto_register_field_array(proto_wg, hf, array_length(hf)); |
1884 | 15 | proto_register_subtree_array(ett, array_length(ett)); |
1885 | | |
1886 | 15 | expert_wg = expert_register_protocol(proto_wg); |
1887 | 15 | expert_register_field_array(expert_wg, ei, array_length(ei)); |
1888 | | |
1889 | 15 | wg_handle = register_dissector("wg", dissect_wg, proto_wg); |
1890 | | |
1891 | 15 | wg_module = prefs_register_protocol(proto_wg, NULL); |
1892 | | |
1893 | 15 | uat_t *wg_keys_uat = uat_new("WireGuard static keys", |
1894 | 15 | sizeof(wg_key_uat_record_t), |
1895 | 15 | "wg_keys", /* filename */ |
1896 | 15 | true, /* from_profile */ |
1897 | 15 | &wg_key_records, /* data_ptr */ |
1898 | 15 | &num_wg_key_records, /* numitems_ptr */ |
1899 | 15 | UAT_AFFECTS_DISSECTION, /* affects dissection of packets, but not set of named fields */ |
1900 | 15 | NULL, /* Help section (currently a wiki page) */ |
1901 | 15 | wg_key_uat_record_copy_cb, /* copy_cb */ |
1902 | 15 | wg_key_uat_record_update_cb, /* update_cb */ |
1903 | 15 | wg_key_uat_record_free_cb, /* free_cb */ |
1904 | 15 | wg_key_uat_apply, /* post_update_cb */ |
1905 | 15 | wg_key_uat_reset, /* reset_cb */ |
1906 | 15 | wg_key_uat_fields); |
1907 | | |
1908 | 15 | prefs_register_uat_preference(wg_module, "keys", |
1909 | 15 | "WireGuard static keys", |
1910 | 15 | "A table of long-term static keys to enable WireGuard peer identification or partial decryption", |
1911 | 15 | wg_keys_uat); |
1912 | | |
1913 | 15 | prefs_register_bool_preference(wg_module, "dissect_packet", |
1914 | 15 | "Dissect transport data", |
1915 | 15 | "Whether the IP dissector should dissect decrypted transport data.", |
1916 | 15 | &pref_dissect_packet); |
1917 | | |
1918 | 15 | prefs_register_filename_preference(wg_module, "keylog_file", "Key log filename", |
1919 | 15 | "The path to the file which contains a list of secrets in the following format:\n" |
1920 | 15 | "\"<key-type> = <base64-encoded-key>\" (without quotes, leading spaces and spaces around '=' are ignored).\n" |
1921 | 15 | "<key-type> is one of: LOCAL_STATIC_PRIVATE_KEY, REMOTE_STATIC_PUBLIC_KEY, " |
1922 | 15 | "LOCAL_EPHEMERAL_PRIVATE_KEY or PRESHARED_KEY.", |
1923 | 15 | &pref_keylog_file, false); |
1924 | | |
1925 | 15 | wg_decryption_supported = wg_decrypt_init(); |
1926 | | /* We require libgcrypt 1.8.0, so if the algorithms aren't supported |
1927 | | * that's almost surely because FIPS mode is on. For libgcrypt 1.10.0 |
1928 | | * and higher we turn it off in epan_init() when initializing gcrypt. |
1929 | | * We could verify that's the reason by calling gcry_fips_mode_active() |
1930 | | */ |
1931 | | |
1932 | 15 | if (wg_decryption_supported) { |
1933 | 15 | secrets_register_type(SECRETS_TYPE_WIREGUARD, wg_keylog_process_lines); |
1934 | 15 | } |
1935 | | |
1936 | 15 | wg_ephemeral_keys = wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(), g_int_hash, wg_pubkey_equal); |
1937 | | |
1938 | 15 | register_init_routine(wg_init); |
1939 | 15 | register_cleanup_routine(wg_keylog_reset); |
1940 | 15 | sessions = wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(), g_direct_hash, g_direct_equal); |
1941 | 15 | } |
1942 | | |
1943 | | void |
1944 | | proto_reg_handoff_wg(void) |
1945 | 15 | { |
1946 | 15 | dissector_add_uint_with_preference("udp.port", 0, wg_handle); |
1947 | 15 | heur_dissector_add("udp", dissect_wg_heur, "WireGuard", "wg", proto_wg, HEURISTIC_ENABLE); |
1948 | | |
1949 | 15 | ip_handle = find_dissector("ip"); |
1950 | 15 | } |
1951 | | |
1952 | | /* |
1953 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
1954 | | * |
1955 | | * Local variables: |
1956 | | * c-basic-offset: 4 |
1957 | | * tab-width: 8 |
1958 | | * indent-tabs-mode: nil |
1959 | | * End: |
1960 | | * |
1961 | | * vi: set shiftwidth=4 tabstop=8 expandtab: |
1962 | | * :indentSize=4:tabSize=8:noTabs=true: |
1963 | | */ |