Bug Summary

File:builds/wireshark/wireshark/epan/crypt/dot11decrypt_util.c
Warning:line 219, column 5
The first element of the 2nd argument is undefined
Note:line 219, column 5
Other elements might also be undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name dot11decrypt_util.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-23/lib/clang/23 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/build/epan/crypt -I /builds/wireshark/wireshark/epan/crypt -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fwrapv -fwrapv-pointer -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-12-05-103046-3656-1 -x c /builds/wireshark/wireshark/epan/crypt/dot11decrypt_util.c
1/* dot11decrypt_util.c
2 *
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 * Copyright (c) 2006 CACE Technologies, Davis (California)
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
8 */
9
10/****************************************************************************/
11/* File includes */
12#include "config.h"
13
14#include "dot11decrypt_debug.h"
15#include "dot11decrypt_int.h"
16#include "dot11decrypt_util.h"
17
18/****************************************************************************/
19/* Internal definitions */
20
21#define FC0_AAD_MASK0x8f 0x8f
22#define FC1_AAD_MASK0xc7 0xc7
23#define FC1_AAD_QOS_MASK0x47 0x47
24
25/****************************************************************************/
26/* Internal macros */
27
28/****************************************************************************/
29/* Internal function prototypes declarations */
30
31/****************************************************************************/
32/* Function definitions */
33
34/* From IEEE 802.11 2016 Chapter 12.5.3.3.3 and 12.5.5.3.3 Construct AAD */
35void dot11decrypt_construct_aad(
36 PDOT11DECRYPT_MAC_FRAME wh,
37 const uint8_t *A1,
38 const uint8_t *A2,
39 const uint8_t *A3,
40 uint8_t *aad,
41 size_t *aad_len)
42{
43 uint8_t mgmt = (DOT11DECRYPT_TYPE(wh->fc[0])(uint8_t)((wh->fc[0] >> 2) & 0x3) == DOT11DECRYPT_TYPE_MANAGEMENT0);
44 int alen = 22;
45
46 /* AAD:
47 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one; 15 zero when QoS Control field present
48 * A1 | A2 | A3
49 * SC with bits 4..15 (seq#) masked to zero
50 * A4 (if present)
51 * QC (if present)
52 */
53
54 /* NB: aad[1] set below */
55 if (!mgmt) {
56 aad[0] = (uint8_t)(wh->fc[0] & FC0_AAD_MASK0x8f);
57 } else {
58 aad[0] = wh->fc[0];
59 }
60 if (DOT11DECRYPT_IS_QOS_DATA(wh)(((wh)->fc[0] & (0x0c | 0x80)) == (0x08 | 0x80))) {
61 aad[1] = (uint8_t)((wh->fc[1] & FC1_AAD_QOS_MASK0x47) | 0x40);
62 } else {
63 aad[1] = (uint8_t)((wh->fc[1] & FC1_AAD_MASK0xc7) | 0x40);
64 }
65 memcpy(aad + 2, A1, DOT11DECRYPT_MAC_LEN6);
66 memcpy(aad + 8, A2, DOT11DECRYPT_MAC_LEN6);
67 memcpy(aad + 14, A3, DOT11DECRYPT_MAC_LEN6);
68 aad[20] = (uint8_t)(wh->seq[0] & DOT11DECRYPT_SEQ_FRAG_MASK0x000f);
69 aad[21] = 0; /* all bits masked */
70
71 /*
72 * Construct variable-length portion of AAD based
73 * on whether this is a 4-address frame/QOS frame.
74 */
75 if (DOT11DECRYPT_IS_4ADDRESS(wh)((wh->fc[1] & 0x03) == 0x03)) {
76 alen += 6;
77 DOT11DECRYPT_ADDR_COPY(aad + 22,memcpy(aad + 22, ((PDOT11DECRYPT_MAC_FRAME_ADDR4)wh)->addr4
, 6)
78 ((PDOT11DECRYPT_MAC_FRAME_ADDR4)wh)->addr4)memcpy(aad + 22, ((PDOT11DECRYPT_MAC_FRAME_ADDR4)wh)->addr4
, 6)
;
79 if (DOT11DECRYPT_IS_QOS_DATA(wh)(((wh)->fc[0] & (0x0c | 0x80)) == (0x08 | 0x80))) {
80 PDOT11DECRYPT_MAC_FRAME_ADDR4_QOS qwh4 =
81 (PDOT11DECRYPT_MAC_FRAME_ADDR4_QOS) wh;
82 aad[28] = (uint8_t)(qwh4->qos[0] & 0x0f);/* just priority bits */
83 aad[29] = 0;
84 alen += 2;
85 }
86 } else {
87 if (DOT11DECRYPT_IS_QOS_DATA(wh)(((wh)->fc[0] & (0x0c | 0x80)) == (0x08 | 0x80))) {
88 PDOT11DECRYPT_MAC_FRAME_QOS qwh =
89 (PDOT11DECRYPT_MAC_FRAME_QOS) wh;
90 aad[22] = (uint8_t)(qwh->qos[0] & 0x0f); /* just priority bits */
91 aad[23] = 0;
92 alen += 2;
93 }
94 }
95 *aad_len = alen;
96}
97
98/**
99 * IEEE 802.11-2016 12.7.1.2 PRF (Pseudo Random Function)
100 *
101 * @param key Derivation input key.
102 * @param key_len Length of the key in bytes.
103 * @param label Unique label for each different purpose of the PRF (named 'A' in the standard).
104 * @param context Provides context to identify the derived key (named 'B' in the standard).
105 * @param context_len Length of context in bytes.
106 * @param hash_algo Hash algorithm to use for the PRF.
107 * See gcrypt available hash algorithms:
108 * https://gnupg.org/documentation/manuals/gcrypt/Available-hash-algorithms.html
109 * @param[out] output Derived key.
110 * @param output_len Length of derived key in bytes.
111 * @return false on error
112 */
113#define MAX_R_LEN256 256
114#define MAX_TMP_LEN1024 1024
115#define MAX_CONTEXT_LEN256 256
116
117bool_Bool
118dot11decrypt_prf(const uint8_t *key, size_t key_len,
119 const char *label,
120 const uint8_t *context, size_t context_len,
121 int hash_algo,
122 uint8_t *output, size_t output_len)
123{
124 uint8_t R[MAX_R_LEN256]; /* Will hold "label || 0 || context || i" */
125 size_t label_len;
126 uint8_t tmp[MAX_TMP_LEN1024];
127 uint16_t hash_len = gcry_md_get_algo_dlen(hash_algo);
128 size_t offset = 0;
129
130 if (!key || !label || !context || !output) {
131 return false0;
132 }
133
134 label_len = strlen(label);
135 if (label_len + 1 + context_len + 1 > MAX_R_LEN256 ||
136 output_len > 64) {
137 ws_warning("Invalid input or output sizes")do { if (1) { ws_log_full("dot11decrypt", LOG_LEVEL_WARNING, "epan/crypt/dot11decrypt_util.c"
, 137, __func__, "Invalid input or output sizes"); } } while (
0)
;
138 return false0;
139 }
140
141 /* Fill R with "label || 0 || context || i" */
142 memcpy(R + offset, label, label_len);
143 offset += label_len;
144 R[offset++] = 0;
145 memcpy(R + offset, context, context_len);
146 offset += context_len;
147
148 for (size_t i = 0; i <= output_len * 8 / 160; i++)
149 {
150 R[offset] = (uint8_t)i;
151 if (ws_hmac_buffer(hash_algo, tmp + hash_len * i, R, offset + 1, key, key_len)) {
152 return false0;
153 }
154 }
155 memcpy(output, tmp, output_len);
156 return true1;
157}
158
159/**
160 * 12.7.1.7.2 Key derivation function (KDF)
161 *
162 * @param key Derivation input key.
163 * @param key_len Length of the key in bytes.
164 * @param label A string identifying the purpose of the keys derived using this KDF.
165 * @param context Provides context to identify the derived key.
166 * @param context_len Length of context in bytes.
167 * @param hash_algo Hash algorithm to use for the KDF.
168 * See gcrypt available hash algorithms:
169 * https://gnupg.org/documentation/manuals/gcrypt/Available-hash-algorithms.html
170 * @param[out] output Derived key.
171 * @param output_len Length of derived key in bytes.
172 * @return false on error
173 */
174bool_Bool
175dot11decrypt_kdf(const uint8_t *key, size_t key_len,
176 const char *label,
177 const uint8_t *context, size_t context_len,
178 int hash_algo,
179 uint8_t *output, size_t output_len)
180{
181 uint8_t R[MAX_R_LEN256]; /* Will hold "i || Label || Context || Length" */
182 uint8_t tmp[MAX_TMP_LEN1024];
2
'tmp' declared without an initial value
183 size_t label_len;
184 size_t hash_len = gcry_md_get_algo_dlen(hash_algo);
185 size_t iterations = output_len * 8 / hash_len;
186 uint16_t len_le = GUINT16_TO_LE(output_len * 8)((guint16) (output_len * 8));
187 size_t offset = 0;
188 size_t i;
189
190 if (!key || !label
3.1
'label' is non-null
|| !context
3.2
'context' is non-null
|| !output) {
3
Assuming 'key' is non-null
4
Assuming 'output' is non-null
5
Taking false branch
191 return false0;
192 }
193
194 label_len = strlen(label);
195 if (2 + label_len + context_len + 2 > MAX_R_LEN256 ||
7
Taking false branch
196 iterations * hash_len > MAX_TMP_LEN1024) {
6
Assuming the condition is false
197 ws_warning("Invalid input sizes")do { if (1) { ws_log_full("dot11decrypt", LOG_LEVEL_WARNING, "epan/crypt/dot11decrypt_util.c"
, 197, __func__, "Invalid input sizes"); } } while (0)
;
198 return false0;
199 }
200
201 /* Fill tmp with "i || Label || Context || Length" */
202 offset += 2; /* Skip "i" (will be copied in for loop below) */
203 memcpy(R + offset, label, label_len);
204 offset += label_len;
205 memcpy(R + offset, context, context_len);
206 offset += context_len;
207 memcpy(R + offset, &len_le, 2);
208 offset += 2;
209
210 for (i = 0; i < iterations; i++)
8
Assuming 'i' is >= 'iterations'
9
Loop condition is false. Execution continues on line 219
211 {
212 uint16_t count_le = GUINT16_TO_LE(i + 1)((guint16) (i + 1));
213 memcpy(R, &count_le, 2);
214
215 if (ws_hmac_buffer(hash_algo, tmp + hash_len * i, R, offset, key, key_len)) {
216 return false0;
217 }
218 }
219 memcpy(output, tmp, output_len);
Other elements might also be undefined
10
The first element of the 2nd argument is undefined
220 return true1;
221}
222
223static bool_Bool sha256(const uint8_t *data, size_t len, uint8_t output[32])
224{
225 gcry_md_hd_t ctx;
226 gcry_error_t result = gcry_md_open(&ctx, GCRY_MD_SHA256, 0);
227 uint8_t *digest;
228
229 if (result) {
230 return false0;
231 }
232 gcry_md_write(ctx, data, len);
233 digest = gcry_md_read(ctx, GCRY_MD_SHA256);
234 if (!digest) {
235 return false0;
236 }
237 memcpy(output, digest, 32);
238 gcry_md_close(ctx);
239 return true1;
240}
241
242/**
243 * Derive PMK-R0 and PMKR0Name. See IEEE 802.11-2016 12.7.1.7.3 PMK-R0
244 *
245 * @param xxkey PSK / MPMK or certain part of MSK.
246 * @param xxkey_len Length of xxkey in bytes.
247 * @param ssid SSID
248 * @param ssid_len Length of SSID in bytes.
249 * @param mdid MDID (Mobility Domain Identifier).
250 * @param r0kh_id PMK-R0 key holder identifier in the Authenticator.
251 * @param r0kh_id_len Length of r0kh_id in bytes.
252 * @param s0kh_id PMK-R0 key holder in the Supplicant (STA mac address)
253 * @param hash_algo Hash algorithm to use for the KDF.
254 * See gcrypt available hash algorithms:
255 * https://gnupg.org/documentation/manuals/gcrypt/Available-hash-algorithms.html
256 * @param[out] pmk_r0 Pairwise master key, first level
257 * @param pmk_r0_len Length of pmk_r0 in bytes.
258 * @param[out] pmk_r0_name Pairwise master key (PMK) R0 name.
259 */
260bool_Bool
261dot11decrypt_derive_pmk_r0(const uint8_t *xxkey, size_t xxkey_len,
262 const uint8_t *ssid, size_t ssid_len,
263 const uint8_t mdid[2],
264 const uint8_t *r0kh_id, size_t r0kh_id_len,
265 const uint8_t s0kh_id[DOT11DECRYPT_MAC_LEN6],
266 int hash_algo,
267 uint8_t *pmk_r0,
268 size_t *pmk_r0_len,
269 uint8_t pmk_r0_name[16])
270{
271 const char *ft_r0n = "FT-R0N";
272 const size_t ft_r0n_len = strlen(ft_r0n);
273 uint8_t context[MAX_CONTEXT_LEN256];
274 uint8_t r0_key_data[DOT11DECRYPT_WPA_PMK_MAX_LEN64 + 16];
275 uint8_t sha256_res[32];
276 size_t offset = 0;
277 unsigned q = gcry_md_get_algo_dlen(hash_algo);
278
279 if (!xxkey || !ssid || !mdid || !r0kh_id || !s0kh_id ||
280 !pmk_r0 || !pmk_r0_len || !pmk_r0_name)
281 {
282 return false0;
283 }
284 if (1 + ssid_len + 2 + 1 + r0kh_id_len + DOT11DECRYPT_MAC_LEN6 > MAX_CONTEXT_LEN256)
285 {
286 ws_warning("Invalid input sizes")do { if (1) { ws_log_full("dot11decrypt", LOG_LEVEL_WARNING, "epan/crypt/dot11decrypt_util.c"
, 286, __func__, "Invalid input sizes"); } } while (0)
;
287 return false0;
288 }
289
290 // R0-Key-Data =
291 // KDF-Hash-Length(XXKey, "FT-R0",
292 // SSIDlength || SSID || MDID || R0KHlength || R0KH-ID || S0KH-ID)
293 // PMK-R0 = L(R0-Key-Data, 0, Q) * PMK-R0Name-Salt = L(R0-Key-Data, Q, 128)
294 context[offset++] = (uint8_t)ssid_len;
295 memcpy(context + offset, ssid, ssid_len);
296 offset += ssid_len;
297 memcpy(context + offset, mdid, 2);
298 offset += 2;
299 context[offset++] = (uint8_t)r0kh_id_len;
300 memcpy(context + offset, r0kh_id, r0kh_id_len);
301 offset += r0kh_id_len;
302 memcpy(context + offset, s0kh_id, DOT11DECRYPT_MAC_LEN6);
303 offset += DOT11DECRYPT_MAC_LEN6;
304 dot11decrypt_kdf(xxkey, xxkey_len, "FT-R0", context, offset, hash_algo,
305 r0_key_data, q + 16);
306 memcpy(pmk_r0, r0_key_data, q);
307 *pmk_r0_len = q;
308
309 // PMK-R0Name-Salt = L(R0-Key-Data, Q, 128)
310 // PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt))
311 offset = 0;
312 memcpy(context + offset, ft_r0n, ft_r0n_len);
313 offset += ft_r0n_len;
314 memcpy(context + offset, r0_key_data + q, 16);
315 offset += 16;
316 if(!sha256(context, offset, sha256_res))
317 return false0;
318 memcpy(pmk_r0_name, sha256_res, 16);
319 return true1;
320}
321
322/**
323 * Derive PMK-R1 and PMKR1Name. See IEEE 802.11-2016 12.7.1.7.4 PMK-R1
324 *
325 */
326bool_Bool
327dot11decrypt_derive_pmk_r1(const uint8_t *pmk_r0, size_t pmk_r0_len,
328 const uint8_t *pmk_r0_name,
329 const uint8_t *r1kh_id, const uint8_t *s1kh_id,
330 int hash_algo,
331 uint8_t *pmk_r1, size_t *pmk_r1_len,
332 uint8_t *pmk_r1_name)
333{
334 const char *ft_r1n = "FT-R1N";
335 const size_t ft_r1n_len = strlen(ft_r1n);
336 // context len = MAX(R1KH-ID || S1KH-ID, "FT-R1N" || PMKR0Name || R1KH-ID || S1KH-ID)
337 uint8_t context[6 + 16 + 6 + 6];
338 uint8_t sha256_res[32];
339 size_t offset = 0;
340
341 if (!pmk_r0 || !pmk_r0_name || !r1kh_id || !s1kh_id ||
342 !pmk_r1 || !pmk_r1_len || !pmk_r1_name)
343 {
344 return false0;
345 }
346 *pmk_r1_len = gcry_md_get_algo_dlen(hash_algo);
347
348 // PMK-R1 = KDF-Hash-Length(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID)
349 memcpy(context + offset, r1kh_id, DOT11DECRYPT_MAC_LEN6);
350 offset += DOT11DECRYPT_MAC_LEN6;
351 memcpy(context + offset, s1kh_id, DOT11DECRYPT_MAC_LEN6);
352 offset += DOT11DECRYPT_MAC_LEN6;
353 dot11decrypt_kdf(pmk_r0, pmk_r0_len, "FT-R1", context, offset, hash_algo,
354 pmk_r1, *pmk_r1_len);
355
356 // PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name || R1KH-ID || S1KH-ID))
357 offset = 0;
358 memcpy(context + offset, ft_r1n, ft_r1n_len);
359 offset += ft_r1n_len;
360 memcpy(context + offset, pmk_r0_name, 16);
361 offset += 16;
362 memcpy(context + offset, r1kh_id, DOT11DECRYPT_MAC_LEN6);
363 offset += DOT11DECRYPT_MAC_LEN6;
364 memcpy(context + offset, s1kh_id, DOT11DECRYPT_MAC_LEN6);
365 offset += DOT11DECRYPT_MAC_LEN6;
366 if(!sha256(context, offset, sha256_res))
367 return false0;
368 memcpy(pmk_r1_name, sha256_res, 16);
369 return true1;
370}
371
372/**
373 * Derive PTK for FT AKMS. See IEE 802.11-2016 12.7.1.7.5 PTK
374 *
375 * PTK = KDF-Hash-Length(PMK-R1, "FT-PTK", SNonce || ANonce || BSSID || STA-ADDR)
376 * PTKName = Truncate-128(
377 * SHA-256(PMKR1Name || "FT-PTKN" || SNonce || ANonce || BSSID || STA-ADDR))
378 */
379bool_Bool
380dot11decrypt_derive_ft_ptk(const uint8_t *pmk_r1, size_t pmk_r1_len,
381 const uint8_t *pmk_r1_name _U___attribute__((unused)),
382 const uint8_t *snonce, const uint8_t *anonce,
383 const uint8_t *bssid, const uint8_t *sta_addr,
384 int hash_algo,
385 uint8_t *ptk, const size_t ptk_len, uint8_t *ptk_name _U___attribute__((unused)))
386{
387 uint8_t context[32 + 32 + 6 + 6];
388 unsigned offset = 0;
389
390 // PTK = KDF-Hash-Length(PMK-R1, "FT-PTK", SNonce || ANonce || BSSID || STA-ADDR)
391 memcpy(context + offset, snonce, 32);
392 offset += 32;
393 memcpy(context + offset, anonce, 32);
394 offset += 32;
395 memcpy(context + offset, bssid, DOT11DECRYPT_MAC_LEN6);
396 offset += DOT11DECRYPT_MAC_LEN6;
397 memcpy(context + offset, sta_addr, DOT11DECRYPT_MAC_LEN6);
398 offset += DOT11DECRYPT_MAC_LEN6;
399 dot11decrypt_kdf(pmk_r1, pmk_r1_len, "FT-PTK", context, offset, hash_algo,
1
Calling 'dot11decrypt_kdf'
400 ptk, ptk_len);
401
402 // TODO derive PTKName
403 return true1;
404}
405
406/*
407 * Editor modelines
408 *
409 * Local Variables:
410 * c-basic-offset: 4
411 * tab-width: 8
412 * indent-tabs-mode: nil
413 * End:
414 *
415 * ex: set shiftwidth=4 tabstop=8 expandtab:
416 * :indentSize=4:tabSize=8:noTabs=true:
417 */