xref: /freebsd/contrib/wpa/src/nan/nan_pairing.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * Wi-Fi Aware - NAN pairing module
3  * Copyright (C) 2025 Intel Corporation
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include "common.h"
11 #include "common/ieee802_11_defs.h"
12 #include "common/ieee802_11_common.h"
13 #include "pasn/pasn_common.h"
14 #include "nan/nan_i.h"
15 
16 static void nan_pairing_prepare_pasn_elems(struct nan_data *nan_data,
17 					   struct nan_peer *peer,
18 					   struct wpabuf *extra_ies,
19 					   int publish_id, int auth_mode);
20 static int nan_pairing_pasn_initialize(struct nan_data *nan_data,
21 				       struct nan_peer *peer, u8 auth_mode,
22 				       int cipher, const char *password,
23 				       enum nan_pairing_role self_role);
24 
25 /**
26  * nan_nira_get_tag_nonce - Generate NIRA nonce and compute NIRA tag
27  * @nan: Pointer to NAN configuration structure
28  * @nonce: Buffer to store the generated NIRA nonce (output)
29  * @tag: Buffer to store the computed NIRA tag (output)
30  * Returns: 0 on success, -1 on failure
31  *
32  * This function generates a random NIRA (NAN Identity Resolution Attribute)
33  * nonce and derives the corresponding NIRA tag using the NIK (NAN Identity
34  * Key), NMI address, and the generated nonce.
35  *
36  * The caller must ensure that nonce buffer is at least NAN_NIRA_NONCE_LEN bytes
37  * and tag buffer is at least NAN_NIRA_TAG_LEN bytes.
38  */
nan_nira_get_tag_nonce(const struct nan_config * nan,u8 * nonce,u8 * tag)39 int nan_nira_get_tag_nonce(const struct nan_config *nan, u8 *nonce, u8 *tag)
40 {
41 	struct wpabuf *tag_buf;
42 
43 	if (os_get_random(nonce, NAN_NIRA_NONCE_LEN) < 0) {
44 		wpa_printf(MSG_INFO, "NAN: Failed to generate NIRA nonce");
45 		return -1;
46 	}
47 
48 	tag_buf = nan_crypto_derive_nira_tag(nan->nik, NAN_NIK_LEN,
49 					     nan->nmi_addr, nonce);
50 	if (!tag_buf)
51 		return -1;
52 
53 	os_memcpy(tag, wpabuf_head(tag_buf), NAN_NIRA_TAG_LEN);
54 	wpabuf_free(tag_buf);
55 
56 	wpa_hexdump_key(MSG_DEBUG, "NAN: NIK", nan->nik, NAN_NIK_LEN);
57 	wpa_hexdump(MSG_DEBUG, "NAN: NIRA-NONCE", nonce, NAN_NIRA_NONCE_LEN);
58 	wpa_hexdump(MSG_DEBUG, "NAN: NIRA-TAG", tag, NAN_NIRA_TAG_LEN);
59 	return 0;
60 }
61 
62 
63 /**
64  * nan_pairing_add_attrs - Add NAN pairing attributes to a buffer
65  * @nan: Pointer to NAN data structure containing configuration
66  * @buf: Pointer to wpabuf where attributes will be added
67  * Returns: 0 on success, -1 otherwise
68  *
69  * This function adds NAN attributes that indicate pairing capabilities
70  * to the provided buffer.
71  */
nan_pairing_add_attrs(struct nan_data * nan,struct wpabuf * buf)72 int nan_pairing_add_attrs(struct nan_data *nan, struct wpabuf *buf)
73 {
74 	if (!nan || !buf)
75 		return -1;
76 
77 	nan_add_dev_capa_ext_attr(nan, buf);
78 
79 	if (nan->cfg->pairing_cfg.pairing_verification) {
80 		if (nan_add_nira(buf, nan->nira_tag, nan->nira_nonce)) {
81 			wpa_printf(MSG_INFO, "NAN: Failed to add NIRA");
82 			return -1;
83 		}
84 	}
85 
86 	return 0;
87 }
88 
89 
nan_pairing_deinit_peer(struct nan_peer * peer)90 void nan_pairing_deinit_peer(struct nan_peer *peer)
91 {
92 	wpabuf_free(peer->pairing.pending_auth1);
93 	peer->pairing.pending_auth1 = NULL;
94 
95 	if (!peer->pairing.pasn)
96 		return;
97 
98 	wpa_pasn_reset(peer->pairing.pasn);
99 	pasn_data_deinit(peer->pairing.pasn);
100 	peer->pairing.pasn = NULL;
101 	peer->pairing.self_pairing_role = NAN_PAIRING_ROLE_IDLE;
102 }
103 
104 
nan_pairing_abort(struct nan_data * nan_data,const u8 * peer_addr)105 int nan_pairing_abort(struct nan_data *nan_data, const u8 *peer_addr)
106 {
107 	struct nan_peer *peer;
108 	int cipher;
109 	struct wpabuf *extra_ies;
110 	int ret = -1;
111 
112 	peer = nan_get_peer(nan_data, peer_addr);
113 	if (!peer) {
114 		wpa_printf(MSG_DEBUG,
115 			   "NAN: Pairing abort: Peer " MACSTR " not found",
116 			   MAC2STR(peer_addr));
117 		return -1;
118 	}
119 
120 	if (!peer->pairing.pasn && !peer->pairing.pending_auth1) {
121 		wpa_printf(MSG_DEBUG,
122 			   "NAN: Pairing abort: No PASN in progress with peer "
123 			   MACSTR, MAC2STR(peer_addr));
124 		return -1;
125 	}
126 
127 	wpa_printf(MSG_DEBUG, "NAN: Aborting pairing with peer " MACSTR,
128 		   MAC2STR(peer_addr));
129 
130 	if (!peer->pairing.pending_auth1) {
131 		wpa_printf(MSG_DEBUG,
132 			   "NAN: Pairing abort: No pending Auth1 frame for peer "
133 			   MACSTR, MAC2STR(peer_addr));
134 		ret = 0;
135 		goto done;
136 	}
137 
138 	/* The auth mode and cipher are not important when rejecting.
139 	 * Just make sure to use a supported cipher so
140 	 * nan_pairing_pasn_initialize() won't fail.
141 	 */
142 	cipher = (nan_data->cfg->pairing_cfg.cipher_suites &
143 		  NAN_PAIRING_PASN_128) ? WPA_CIPHER_CCMP : WPA_CIPHER_GCMP_256;
144 
145 	if (nan_pairing_pasn_initialize(nan_data, peer, NAN_PASN_AUTH_MODE_PASN,
146 					cipher, "",
147 					NAN_PAIRING_ROLE_RESPONDER)) {
148 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Initialize failed");
149 		goto done;
150 	}
151 
152 	extra_ies = wpabuf_alloc(NAN_ELEMENT_MAX_SIZE);
153 	if (!extra_ies) {
154 		wpa_printf(MSG_INFO,
155 			   "NAN: Pairing: Failed to allocate buffer for extra elements");
156 		goto done;
157 	}
158 
159 	nan_pairing_prepare_pasn_elems(nan_data, peer, extra_ies,
160 				       peer->pairing.peer_instance_id,
161 				       NAN_PASN_AUTH_MODE_PASN);
162 	pasn_set_extra_ies(peer->pairing.pasn, wpabuf_head_u8(extra_ies),
163 			   wpabuf_len(extra_ies));
164 	wpabuf_free(extra_ies);
165 
166 	nan_configure_peer_schedule(nan_data, peer, &nan_data->sched);
167 
168 	ret = handle_auth_pasn_resp(peer->pairing.pasn, nan_data->cfg->nmi_addr,
169 				    peer_addr, NULL,
170 				    WLAN_STATUS_UNSPECIFIED_FAILURE);
171 	if (ret < 0) {
172 		wpa_printf(MSG_DEBUG,
173 			   "NAN: Pairing abort: Failed to send response");
174 		nan_clear_peer_schedule(nan_data, peer);
175 	}
176 
177 done:
178 	nan_pairing_deinit_peer(peer);
179 	return ret;
180 }
181 
182 
nan_pairing_is_supported(struct nan_data * nan_data,struct nan_peer * peer,u8 auth_mode)183 static bool nan_pairing_is_supported(struct nan_data *nan_data,
184 				     struct nan_peer *peer, u8 auth_mode)
185 {
186 	if (auth_mode == NAN_PASN_AUTH_MODE_PASN ||
187 	    auth_mode == NAN_PASN_AUTH_MODE_SAE) {
188 		if (!nan_data->cfg->pairing_cfg.pairing_setup) {
189 			wpa_printf(MSG_DEBUG,
190 				   "NAN: Pairing: Device doesn't support pairing setup");
191 			return false;
192 		}
193 
194 		if (!peer->pairing.pairing_cfg.pairing_setup) {
195 			wpa_printf(MSG_DEBUG,
196 				   "NAN: Pairing: Peer doesn't support pairing setup");
197 			return false;
198 		}
199 	} else if (auth_mode == NAN_PASN_AUTH_MODE_PMK) {
200 		if (!nan_data->cfg->pairing_cfg.pairing_verification) {
201 			wpa_printf(MSG_DEBUG,
202 				   "NAN: Pairing: Device doesn't support pairing verification");
203 			return false;
204 		}
205 
206 		if (!peer->pairing.pairing_cfg.pairing_verification) {
207 			wpa_printf(MSG_DEBUG,
208 				   "NAN: Pairing: Peer doesn't support pairing verification");
209 			return false;
210 		}
211 
212 		if (!nan_data->cfg->get_npk_akmp) {
213 			wpa_printf(MSG_DEBUG,
214 				   "NAN: Pairing: get_npk_akmp callback not set");
215 			return false;
216 		}
217 	}
218 
219 	return true;
220 }
221 
222 
nan_pairing_set_password(struct pasn_data * pasn,const char * passphrase)223 static int nan_pairing_set_password(struct pasn_data *pasn,
224 				    const char *passphrase)
225 {
226 #ifdef CONFIG_SAE
227 	struct sae_pt *pt;
228 
229 	pt = sae_derive_pt(pasn->pasn_groups, (const u8 *) NAN_PASN_SSID,
230 			   os_strlen(NAN_PASN_SSID), (const u8 *) passphrase,
231 			   os_strlen(passphrase), NULL, 0);
232 	if (pasn_set_pt(pasn, pt) < 0) {
233 		wpa_printf(MSG_INFO, "NAN: Pairing: Failed to set SAE pt");
234 		sae_deinit_pt(pt);
235 		return -1;
236 	}
237 
238 	return 0;
239 #else  /* CONFIG_SAE */
240 	return -1;
241 #endif /* CONFIG_SAE */
242 }
243 
244 
nan_pairing_generate_rsnxe(int akmp)245 static struct wpabuf * nan_pairing_generate_rsnxe(int akmp)
246 {
247 	/* According to Wi-Fi Aware Specification version 4.0, Table 26,
248 	 * the RSNXE's capabilities field in NAN PASN Authentication frames is
249 	 * 16 bits long.
250 	 */
251 	u16 capab = 1; /* bit 0-3 = Field length (n - 1) */
252 
253 	struct wpabuf *buf;
254 
255 	if (wpa_key_mgmt_sae(akmp))
256 		capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
257 
258 	/* Element header (2 octets) + capabilities field (2 octets) */
259 	buf = wpabuf_alloc(4);
260 	if (!buf)
261 		return NULL;
262 
263 	wpa_printf(MSG_DEBUG, "NAN: RSNXE capabilities: %04x", capab);
264 	wpabuf_put_u8(buf, WLAN_EID_RSNX);
265 	wpabuf_put_u8(buf, 2);
266 	wpabuf_put_le16(buf, capab);
267 	return buf;
268 }
269 
270 
nan_pairing_send_cb(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)271 static int nan_pairing_send_cb(void *ctx, const u8 *data, size_t data_len,
272 			       int noack, unsigned int freq, unsigned int wait)
273 {
274 	struct nan_data *nan_data = (struct nan_data *) ctx;
275 
276 	return nan_data->cfg->send_pasn(nan_data->cfg->cb_ctx, data, data_len);
277 }
278 
279 
280 /**
281  * nan_pasn_verification_init - Initialize PASN data for pairing verification
282  * @nan_data: Pointer to NAN data structure containing configuration
283  * @peer: Pointer to the NAN peer structure
284  * Returns: 0 on success, -1 on failure
285  *
286  * This function gets the NPK and AKMP for the given peer and sets it as the
287  * PASN PMK and AKMP. It also generates the NIRA nonce and tag to be used as the
288  * custom PMKID for the PASN verification process.
289  */
nan_pasn_verification_init(struct nan_data * nan_data,struct nan_peer * peer)290 static int nan_pasn_verification_init(struct nan_data *nan_data,
291 				      struct nan_peer *peer)
292 {
293 	struct nan_pairing_peer_data *pairing_data;
294 	const struct wpabuf *npk;
295 	int akmp;
296 	u8 npkid[NAN_NIRA_NONCE_LEN + NAN_NIRA_TAG_LEN];
297 
298 	pairing_data = &peer->pairing;
299 
300 	if (!pairing_data->nonce_tag_valid) {
301 		wpa_printf(MSG_DEBUG,
302 			   "NAN: Pairing: NIK ID not available for verification");
303 		return -1;
304 	}
305 
306 	npk = nan_data->cfg->get_npk_akmp(nan_data->cfg->cb_ctx, peer->nmi_addr,
307 					  pairing_data->nonce,
308 					  pairing_data->tag, &akmp);
309 	if (!npk) {
310 		wpa_printf(MSG_DEBUG,
311 			   "NAN: Pairing: Failed to get NPK AKMP for verification");
312 		return -1;
313 	}
314 
315 	pasn_set_akmp(pairing_data->pasn, akmp);
316 
317 	if (pairing_data->self_pairing_role == NAN_PAIRING_ROLE_INITIATOR)
318 		pasn_initiator_pmksa_cache_add(nan_data->initiator_pmksa,
319 					       nan_data->cfg->nmi_addr,
320 					       peer->nmi_addr,
321 					       wpabuf_head_u8(npk),
322 					       wpabuf_len(npk), NULL, akmp);
323 	else
324 		pasn_responder_pmksa_cache_add(nan_data->responder_pmksa,
325 					       nan_data->cfg->nmi_addr,
326 					       peer->nmi_addr,
327 					       wpabuf_head_u8(npk),
328 					       wpabuf_len(npk), NULL, akmp);
329 
330 	/*
331 	 * According to Wi-Fi Aware Specification v4.0, section 7.6.5, pairing
332 	 * verification uses NPKID constructed from NIRA nonce and tag. The same
333 	 * nonce and tag should be used in the NIRA added to PASN first and
334 	 * second frames.
335 	 */
336 	if (nan_nira_get_tag_nonce(nan_data->cfg, npkid,
337 				   &npkid[NAN_NIRA_NONCE_LEN]) < 0) {
338 		wpa_printf(MSG_DEBUG, "NAN: Failed to get NIRA tag and nonce");
339 		return -1;
340 	}
341 
342 	pasn_set_custom_pmkid(pairing_data->pasn, npkid);
343 	return 0;
344 }
345 
346 
nan_validate_custom_pmkid(void * ctx,const u8 * addr,const u8 * pmkid)347 static int nan_validate_custom_pmkid(void *ctx, const u8 *addr, const u8 *pmkid)
348 {
349 	/*
350 	 * In NAN pairing, custom PMKID is constructed from NIRA nonce and tag.
351 	 * Matching the tag to a known NIK is done during NIRA validation so
352 	 * here we just accept any PMKID.
353 	 */
354 	return 0;
355 }
356 
357 
nan_pairing_pasn_initialize(struct nan_data * nan_data,struct nan_peer * peer,u8 auth_mode,int cipher,const char * password,enum nan_pairing_role self_role)358 static int nan_pairing_pasn_initialize(struct nan_data *nan_data,
359 				       struct nan_peer *peer, u8 auth_mode,
360 				       int cipher, const char *password,
361 				       enum nan_pairing_role self_role)
362 {
363 	struct wpabuf *rsnxe = NULL;
364 	struct pasn_data *pasn;
365 	struct nan_pairing_peer_data *pairing;
366 
367 	pairing = &peer->pairing;
368 	if (pairing->pasn) {
369 		wpa_pasn_reset(pairing->pasn);
370 	} else {
371 		pairing->pasn = pasn_data_init();
372 		if (!pairing->pasn) {
373 			wpa_printf(MSG_INFO,
374 				   "NAN: Pairing: Failed to initialize PASN data");
375 			return -1;
376 		}
377 	}
378 
379 	pasn = pairing->pasn;
380 	pasn_set_own_addr(pasn, nan_data->cfg->nmi_addr);
381 	pasn_set_peer_addr(pasn, peer->nmi_addr);
382 	pasn_set_bssid(pasn, nan_data->cluster_id);
383 
384 	if (self_role == NAN_PAIRING_ROLE_INITIATOR)
385 		pasn->pmksa = nan_data->initiator_pmksa;
386 	else
387 		pasn->pmksa = nan_data->responder_pmksa;
388 
389 	if (cipher == WPA_CIPHER_GCMP_256 &&
390 	    (nan_data->cfg->pairing_cfg.cipher_suites & NAN_PAIRING_PASN_256)) {
391 		pasn->group = 20;
392 		pasn->cipher = WPA_CIPHER_GCMP_256;
393 	} else if (cipher == WPA_CIPHER_CCMP &&
394 		   (nan_data->cfg->pairing_cfg.cipher_suites &
395 		    NAN_PAIRING_PASN_128)) {
396 		pasn->group = 19;
397 		pasn->cipher = WPA_CIPHER_CCMP;
398 	} else {
399 		wpa_printf(MSG_INFO,
400 			   "NAN: Pairing: Unsupported cipher suite %s",
401 			   wpa_cipher_txt(cipher));
402 		goto fail;
403 	}
404 
405 	pasn_enable_kdk_derivation(pasn);
406 
407 	/* Set allowed PASN groups. This is needed for all modes */
408 	os_free(pasn->pasn_groups);
409 	pasn->pasn_groups = os_calloc(2, sizeof(*pasn->pasn_groups));
410 	if (!pasn->pasn_groups) {
411 		wpa_printf(MSG_INFO,
412 			   "NAN: Pairing: Failed to allocate PASN groups");
413 		goto fail;
414 	}
415 	pasn->pasn_groups[0] = pasn->group;
416 
417 	if (auth_mode == NAN_PASN_AUTH_MODE_SAE) {
418 		pasn_set_akmp(pasn, WPA_KEY_MGMT_SAE);
419 		if (!password) {
420 			wpa_printf(MSG_INFO,
421 				   "NAN: Pairing: Password not available");
422 			goto fail;
423 		}
424 
425 		if (nan_pairing_set_password(pasn, password) < 0) {
426 			wpa_printf(MSG_INFO,
427 				   "NAN: Pairing: Failed to set password");
428 			goto fail;
429 		}
430 	} else if (auth_mode == NAN_PASN_AUTH_MODE_PASN) {
431 		pasn_set_akmp(pasn, WPA_KEY_MGMT_PASN);
432 		pasn_set_noauth(pasn, true);
433 	} else if (auth_mode == NAN_PASN_AUTH_MODE_PMK) {
434 		if (nan_pasn_verification_init(nan_data, peer)) {
435 			wpa_printf(MSG_DEBUG,
436 				   "NAN: Pairing: PASN verification init failed");
437 			goto fail;
438 		}
439 	} else {
440 		wpa_printf(MSG_INFO,
441 			   "NAN: Pairing: Unsupported authentication mode %u",
442 			   auth_mode);
443 		goto fail;
444 	}
445 
446 	pasn_set_rsn_pairwise(pasn, pasn->cipher);
447 	pasn_set_wpa_key_mgmt(pasn, pasn->akmp);
448 
449 	if (auth_mode != NAN_PASN_AUTH_MODE_PASN) {
450 		rsnxe = nan_pairing_generate_rsnxe(pasn->akmp);
451 		if (!rsnxe) {
452 			wpa_printf(MSG_INFO,
453 				   "NAN: Pairing: Failed to generate RSNXE");
454 			goto fail;
455 		}
456 
457 		pasn_set_rsnxe_ie(pairing->pasn, wpabuf_head_u8(rsnxe));
458 		wpabuf_free(rsnxe);
459 	}
460 
461 	pasn_register_callbacks(pasn, nan_data, nan_pairing_send_cb,
462 				nan_validate_custom_pmkid, NULL, NULL);
463 	return 0;
464 
465 fail:
466 	pasn_data_deinit(pasn);
467 	pairing->pasn = NULL;
468 	return -1;
469 }
470 
471 
472 /*
473  * nan_pairing_prepare_pasn_elems - Prepare NAN element for pairing PASN frames
474  * @nan_data: Pointer to NAN data structure
475  * @peer: Pointer to NAN peer structure
476  * @extra_ies: Buffer to which the NAN element is appended
477  * @publish_id: Publish ID to use in the CSIA
478  * @auth_mode: Pairing authentication mode
479  *
480  * This function adds a NAN element containing the NAN attributes that shall be
481  * included in the first and second PASN frames for NAN pairing.
482  * The added attributes are:
483  * - Device Capability Extension attribute (DCEA)
484  * - Cipher suite information attribute (CSIA) with appropriate PASN cipher
485  *   (either GCMP-256 or GCMP-128)
486  * - NAN Pairing Bootstrapping Attribute (NPBA) if available
487  */
nan_pairing_prepare_pasn_elems(struct nan_data * nan_data,struct nan_peer * peer,struct wpabuf * extra_ies,int publish_id,int auth_mode)488 static void nan_pairing_prepare_pasn_elems(struct nan_data *nan_data,
489 					   struct nan_peer *peer,
490 					   struct wpabuf *extra_ies,
491 					   int publish_id, int auth_mode)
492 {
493 	u8 *len_ptr;
494 	struct nan_cipher_suite cs;
495 	size_t initial_len = wpabuf_len(extra_ies);
496 
497 	wpabuf_put_u8(extra_ies, WLAN_EID_VENDOR_SPECIFIC);
498 
499 	/* placeholder for length - to be filled later */
500 	len_ptr = wpabuf_put(extra_ies, 1);
501 
502 	/* OUI + OUI Type */
503 	wpabuf_put_be32(extra_ies, NAN_IE_VENDOR_TYPE);
504 
505 	if (peer->pairing.pasn->cipher == WPA_CIPHER_GCMP_256)
506 		cs.csid = NAN_CS_PK_PASN_256;
507 	else
508 		cs.csid = NAN_CS_PK_PASN_128;
509 
510 	cs.instance_id = publish_id;
511 
512 	nan_add_csia(extra_ies, nan_data->cfg->security_capab, 1, &cs);
513 
514 	if (auth_mode == NAN_PASN_AUTH_MODE_SAE ||
515 	    auth_mode == NAN_PASN_AUTH_MODE_PASN) {
516 		nan_add_dev_capa_ext_attr(nan_data, extra_ies);
517 		if (peer->bootstrap.npba)
518 			wpabuf_put_buf(extra_ies, peer->bootstrap.npba);
519 	} else {
520 		const u8 *npkid = peer->pairing.pasn->custom_pmkid;
521 
522 		/*
523 		 * Add NIRA with the same nonce and tag as in the NPKID.
524 		 * NPKID is: NONCE || TAG.
525 		 */
526 		if (nan_add_nira(extra_ies, &npkid[NAN_NIRA_NONCE_LEN],
527 				 npkid)) {
528 			wpa_printf(MSG_DEBUG, "NAN: Failed to add NIRA");
529 		}
530 	}
531 
532 	*len_ptr = wpabuf_len(extra_ies) - initial_len - 2;
533 }
534 
535 
536 /**
537  * nan_pairing_initiate_pasn_auth - Initiate PASN authentication for NAN pairing
538  * @nan_data: NAN data context
539  * @addr: MAC address of the peer device
540  * @auth_mode: Authentication mode to be used (PASN, SAE, or PMK)
541  * @cipher: Cipher suite to be used for the pairing
542  * @handle: Handle of the service instance for which pairing is requested
543  * @peer_instance_id: Instance ID of the peer service for which pairing is
544  *	requested
545  * @responder: Whether this device is acting as PASN responder
546  * @password: Password to be used for authentication (if applicable)
547  * Returns: 0 on success, -1 on failure
548  */
nan_pairing_initiate_pasn_auth(struct nan_data * nan_data,const u8 * addr,u8 auth_mode,int cipher,int handle,u8 peer_instance_id,bool responder,const char * password,const struct nan_schedule * sched)549 int nan_pairing_initiate_pasn_auth(struct nan_data *nan_data, const u8 *addr,
550 				   u8 auth_mode, int cipher, int handle,
551 				   u8 peer_instance_id, bool responder,
552 				   const char *password,
553 				   const struct nan_schedule *sched)
554 {
555 	int ret = 0;
556 	struct pasn_data *pasn;
557 	struct nan_peer *peer;
558 	struct wpabuf *extra_ies;
559 
560 	if (!addr) {
561 		wpa_printf(MSG_INFO, "NAN: Pairing: Peer address missing");
562 		return -1;
563 	}
564 
565 	peer = nan_get_peer(nan_data, addr);
566 	if (!peer) {
567 		wpa_printf(MSG_INFO, "NAN: Pairing: Peer not known");
568 		return -1;
569 	}
570 
571 	if (peer->bootstrap.in_progress) {
572 		wpa_printf(MSG_DEBUG,
573 			   "NAN: Pairing: Bootstrap in progress with peer");
574 		return -1;
575 	}
576 
577 	if (!nan_pairing_is_supported(nan_data, peer, auth_mode)) {
578 		wpa_printf(MSG_INFO,
579 			   "NAN: Pairing: Invalid params to initiate authentication");
580 		return -1;
581 	}
582 
583 	peer->pairing.self_pairing_role = responder ?
584 		NAN_PAIRING_ROLE_RESPONDER : NAN_PAIRING_ROLE_INITIATOR;
585 
586 	if (nan_pairing_pasn_initialize(nan_data, peer, auth_mode, cipher,
587 					password,
588 					peer->pairing.self_pairing_role)) {
589 		wpa_printf(MSG_INFO, "NAN: Pairing: Initialize failed");
590 		return -1;
591 	}
592 
593 	pasn = peer->pairing.pasn;
594 
595 	extra_ies = wpabuf_alloc(NAN_ELEMENT_MAX_SIZE);
596 	if (!extra_ies)
597 		return -1;
598 
599 	/* TODO: Add support for NAN element fragmentation if it's larger than
600 	 * 255 octets, as defined in Wi-Fi Aware Specification v4.0 section 9.1.
601 	 */
602 	nan_pairing_prepare_pasn_elems(nan_data, peer, extra_ies, handle,
603 				       auth_mode);
604 	pasn_set_extra_ies(pasn, wpabuf_head_u8(extra_ies),
605 			   wpabuf_len(extra_ies));
606 	wpabuf_free(extra_ies);
607 
608 	peer->pairing.handle = handle;
609 	peer->pairing.peer_instance_id = peer_instance_id;
610 	peer->pairing.flags = 0;
611 
612 	if (nan_configure_peer_schedule(nan_data, peer, sched))
613 		wpa_printf(MSG_DEBUG, "NAN: Could not configure peer schedule");
614 
615 	if (responder) {
616 		if (peer->pairing.pending_auth1) {
617 			wpa_printf(MSG_DEBUG,
618 				   "NAN: Pairing: Responder - process pending Auth1");
619 			ret = nan_pairing_auth_rx(
620 				nan_data,
621 				wpabuf_head(peer->pairing.pending_auth1),
622 				wpabuf_len(peer->pairing.pending_auth1));
623 			wpabuf_free(peer->pairing.pending_auth1);
624 			peer->pairing.pending_auth1 = NULL;
625 
626 			return ret;
627 		}
628 		return 0;
629 	}
630 
631 	if (auth_mode == NAN_PASN_AUTH_MODE_PMK) {
632 		peer->pairing.flags |= NAN_PAIRING_FLAG_NPK_VERIFICATION;
633 		ret = wpa_pasn_verify(pasn, pasn->own_addr, pasn->peer_addr,
634 				      pasn->bssid, pasn->akmp, pasn->cipher,
635 				      pasn->group, 0, NULL, 0, NULL, 0, NULL);
636 	} else {
637 		ret = wpas_pasn_start(pasn, pasn->own_addr, pasn->peer_addr,
638 				      pasn->bssid, pasn->akmp, pasn->cipher,
639 				      pasn->group, 0, NULL, 0, NULL, 0, NULL);
640 	}
641 
642 	if (ret) {
643 		wpa_printf(MSG_INFO, "NAN: Pairing: Failed to start PASN");
644 		nan_pairing_deinit_peer(peer);
645 	}
646 
647 	return ret;
648 }
649 
650 
651 /**
652  * nan_pairing_done - Derive NPK caching related keys after successful pairing
653  * @nan_data: NAN interface data
654  * @peer: NAN peer with which pairing is being completed
655  *
656  * This function completes the NAN pairing process by deriving the necessary
657  * cryptographic keys (KEK and NPK for opportunistic pairing) when NPK caching
658  * is enabled.
659  */
nan_pairing_done(struct nan_data * nan_data,struct nan_peer * peer)660 static void nan_pairing_done(struct nan_data *nan_data, struct nan_peer *peer)
661 {
662 	u8 npk[NAN_NPK_LEN];
663 	struct pasn_data *pasn = peer->pairing.pasn;
664 	int cipher = pasn_get_cipher(pasn);
665 	u8 *initiator_nmi, *responder_nmi;
666 	int ret;
667 
668 	peer->pairing.flags |= NAN_PAIRING_FLAG_PAIRED;
669 
670 	peer->pairing.pairing_csid = cipher == WPA_CIPHER_GCMP_256 ?
671 		NAN_CS_PK_PASN_256 : NAN_CS_PK_PASN_128;
672 	peer->pairing.pairing_akmp = pasn_get_akmp(pasn);
673 
674 	if (!nan_data->cfg->pairing_cfg.npk_caching ||
675 	    !peer->pairing.pairing_cfg.npk_caching ||
676 	    (peer->pairing.flags & NAN_PAIRING_FLAG_NPK_VERIFICATION))
677 		return;
678 
679 	wpa_printf(MSG_DEBUG, "NAN: Pairing: Derive KEK after PASN pairing");
680 
681 	if (peer->pairing.self_pairing_role == NAN_PAIRING_ROLE_INITIATOR) {
682 		initiator_nmi = nan_data->cfg->nmi_addr;
683 		responder_nmi = peer->nmi_addr;
684 	} else {
685 		initiator_nmi = peer->nmi_addr;
686 		responder_nmi = nan_data->cfg->nmi_addr;
687 	}
688 
689 	ret = nan_crypto_derive_kek(pasn->ptk.kdk, pasn->ptk.kdk_len,
690 				    peer->pairing.pairing_csid,
691 				    initiator_nmi, responder_nmi,
692 				    &pasn->ptk);
693 	if (ret) {
694 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Failed to derive KEK");
695 		return;
696 	}
697 
698 	/* For SAE AKMP, NPK was already derived inside the PASN module and
699 	 * stored in pasn->pmk. For PASN AKMP, derive NPK here and configure it
700 	 * to the PASN module. The NPK will be stored alongside the peer's NIK
701 	 * when the NIK is received from the peer.
702 	 */
703 	if (pasn_get_akmp(pasn) != WPA_KEY_MGMT_PASN)
704 		return;
705 
706 	wpa_printf(MSG_DEBUG, "NAN: Pairing: Derive NPK after PASN pairing");
707 
708 	ret = nan_crypto_derive_npk(pasn->ptk.kdk, pasn->ptk.kdk_len,
709 				    peer->pairing.pairing_csid,
710 				    initiator_nmi, responder_nmi, npk,
711 				    sizeof(npk));
712 	if (ret) {
713 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Failed to derive NPK");
714 		return;
715 	}
716 
717 	os_memcpy(pasn->pmk, npk, NAN_NPK_LEN);
718 	pasn->pmk_len = NAN_NPK_LEN;
719 }
720 
721 
722 /**
723  * nan_nik_build_key_data - Build NAN Identity Key (NIK) key data buffer
724  * @nan_data: Pointer to NAN data structure containing configuration
725  * Returns: Pointer to allocated wpabuf containing the key data, or NULL
726  *	on failure.
727  *
728  * This function constructs a buffer containing NAN key data elements including:
729  * - NIK KDE (Key Data Encapsulation) with cipher version and NIK value
730  * - Key Lifetime KDE indicating the NIK key lifetime
731  *
732  * Note: Caller is responsible for freeing the returned buffer.
733  */
nan_nik_build_key_data(struct nan_data * nan_data)734 static struct wpabuf * nan_nik_build_key_data(struct nan_data *nan_data)
735 {
736 	struct wpabuf *buf;
737 
738 	buf = wpabuf_alloc(KDE_HDR_LEN + sizeof(struct nan_nik_kde) +
739 			   KDE_HDR_LEN + sizeof(struct nan_key_lifetime_kde));
740 	if (!buf)
741 		return NULL;
742 
743 	nan_add_kde_hdr(buf, NAN_KEY_DATA_NIK, sizeof(struct nan_nik_kde));
744 	wpabuf_put_u8(buf, NAN_NIRA_CIPHER_VER_128);
745 	wpabuf_put_data(buf, nan_data->cfg->nik, sizeof(nan_data->cfg->nik));
746 
747 	nan_add_kde_hdr(buf, NAN_KEY_DATA_LIFETIME,
748 			sizeof(struct nan_key_lifetime_kde));
749 	wpabuf_put_le16(buf, NAN_KEY_LIFETIME_NIK);
750 	wpabuf_put_be32(buf, nan_data->cfg->nik_lifetime);
751 
752 	return buf;
753 }
754 
755 
756 /**
757  * nan_send_nik - Send NAN Identity Key (NIK) to a peer
758  * @nan_data: Pointer to NAN data structure containing configuration and state
759  * @peer: Pointer to the NAN peer structure to send the NIK to
760  * Returns: 0 on success, -1 in case of an error
761  *
762  * This function sends the NAN Identity Key (NIK) and the NIK lifetime to a peer
763  * device as part of the NAN pairing process. The NIK is encrypted using the KEK
764  * (Key Encryption Key) derived from PASN and sent in a Shared Key Descriptor
765  * Attribute (SKDA) within a follow-up message.
766  */
nan_send_nik(struct nan_data * nan_data,struct nan_peer * peer)767 static int nan_send_nik(struct nan_data *nan_data, struct nan_peer *peer)
768 {
769 	struct wpabuf *skda, *key_data;
770 	struct wpa_eapol_key *key_desc;
771 	u16 info, key_len;
772 	int ret;
773 	struct wpabuf *encrypted_key_data = NULL;
774 	size_t skda_len;
775 
776 	if (!nan_data->cfg->pairing_cfg.npk_caching) {
777 		wpa_printf(MSG_DEBUG,
778 			   "NAN: Pairing: Local NPK caching not enabled, don't send NIK");
779 		return 0;
780 	}
781 
782 	if (!peer->pairing.pairing_cfg.npk_caching) {
783 		wpa_printf(MSG_DEBUG,
784 			   "NAN: Pairing: Peer NPK caching not enabled, don't send NIK");
785 		return 0;
786 	}
787 
788 	if (peer->pairing.flags & NAN_PAIRING_FLAG_NPK_VERIFICATION)
789 		return 0;
790 
791 	if (!peer->pairing.pasn || !peer->pairing.pasn->ptk.kek_len) {
792 		wpa_printf(MSG_DEBUG,
793 			   "NAN: Pairing: KEK not available for NIK encryption");
794 		return -1;
795 	}
796 
797 	key_data = nan_nik_build_key_data(nan_data);
798 	if (!key_data) {
799 		wpa_printf(MSG_DEBUG,
800 			   "NAN: Pairing: Failed to build NIK key data");
801 		return -1;
802 	}
803 
804 	/* Encrypt the key data using the KEK from the PASN data */
805 	encrypted_key_data = nan_crypto_encrypt_key_data(
806 		key_data, peer->pairing.pasn->ptk.kek,
807 		peer->pairing.pasn->ptk.kek_len);
808 	wpabuf_clear_free(key_data);
809 	if (!encrypted_key_data) {
810 		wpa_printf(MSG_DEBUG,
811 			   "NAN: Pairing: Failed to encrypt NIK key data");
812 		return -1;
813 	}
814 
815 	skda_len = sizeof(struct nan_shared_key) +
816 		sizeof(struct wpa_eapol_key) + 2 +
817 		wpabuf_len(encrypted_key_data);
818 
819 	skda = wpabuf_alloc(NAN_ATTR_HDR_LEN + skda_len);
820 	if (!skda) {
821 		wpa_printf(MSG_INFO,
822 			   "NAN: Pairing: Failed to allocate SKDA buffer");
823 		wpabuf_free(encrypted_key_data);
824 		return -1;
825 	}
826 
827 	wpabuf_put_u8(skda, NAN_ATTR_SHARED_KEY_DESCR);
828 	wpabuf_put_le16(skda, skda_len);
829 	wpabuf_put_u8(skda, peer->pairing.handle);
830 
831 	key_desc = wpabuf_put(skda, sizeof(*key_desc));
832 	os_memset(key_desc, 0, sizeof(*key_desc));
833 
834 	key_desc->type = NAN_KEY_DESC;
835 	info = WPA_KEY_INFO_TYPE_AKM_DEFINED | WPA_KEY_INFO_KEY_TYPE |
836 		WPA_KEY_INFO_ACK | WPA_KEY_INFO_ENCR_KEY_DATA;
837 	WPA_PUT_BE16(key_desc->key_info, info);
838 
839 	key_len = wpa_cipher_key_len(peer->pairing.pasn->cipher);
840 	WPA_PUT_BE16(key_desc->key_length, key_len);
841 
842 	wpabuf_put_be16(skda, wpabuf_len(encrypted_key_data));
843 	wpabuf_put_buf(skda, encrypted_key_data);
844 
845 	ret = nan_data->cfg->transmit_followup(nan_data->cfg->cb_ctx,
846 					       peer->nmi_addr, skda,
847 					       peer->pairing.handle,
848 					       peer->pairing.peer_instance_id);
849 
850 	wpabuf_free(encrypted_key_data);
851 	wpabuf_free(skda);
852 
853 	return ret;
854 }
855 
856 
nan_pairing_derive_nd_pmk(struct nan_data * nan_data,struct nan_peer * peer,u8 * nd_pmk)857 static int nan_pairing_derive_nd_pmk(struct nan_data *nan_data,
858 				     struct nan_peer *peer, u8 *nd_pmk)
859 {
860 	struct pasn_data *pasn = peer->pairing.pasn;
861 	int cipher = pasn_get_cipher(pasn);
862 	enum nan_cipher_suite_id csid;
863 	const u8 *initiator_nmi, *responder_nmi;
864 	int ret;
865 
866 	wpa_printf(MSG_DEBUG, "NAN: Pairing: Derive ND-PMK after PASN pairing");
867 
868 	if (peer->pairing.self_pairing_role == NAN_PAIRING_ROLE_INITIATOR) {
869 		initiator_nmi = nan_data->cfg->nmi_addr;
870 		responder_nmi = peer->nmi_addr;
871 	} else {
872 		initiator_nmi = peer->nmi_addr;
873 		responder_nmi = nan_data->cfg->nmi_addr;
874 	}
875 
876 	csid = cipher == WPA_CIPHER_GCMP_256 ? NAN_CS_PK_PASN_256 :
877 		NAN_CS_PK_PASN_128;
878 
879 	ret = nan_crypto_derive_nd_pmk_from_kdk(pasn->ptk.kdk,
880 						pasn->ptk.kdk_len, csid,
881 						initiator_nmi, responder_nmi,
882 						nd_pmk);
883 	if (ret)
884 		wpa_printf(MSG_INFO,
885 			   "NAN: Pairing: Failed to derive ND PMK");
886 	return ret;
887 }
888 
889 
890 /**
891  * nan_pairing_pasn_auth_tx_status - Handle PASN Authentication frame TX status
892  * @nan: Pointer to NAN data structure
893  * @data: Pointer to the transmitted frame data
894  * @data_len: Length of the transmitted frame data in bytes
895  * @acked: Whether the frame was acknowledged
896  * Returns: 0 on success, -1 on error
897  *
898  * This function processes the transmission status of a PASN Authentication
899  * frame used in NAN pairing and triggers the pairing result callback in case
900  * PASN is done.
901  */
nan_pairing_pasn_auth_tx_status(struct nan_data * nan,const u8 * data,size_t data_len,bool acked)902 int nan_pairing_pasn_auth_tx_status(struct nan_data *nan, const u8 *data,
903 				    size_t data_len, bool acked)
904 {
905 	int ret;
906 	struct nan_peer *peer;
907 	struct pasn_data *pasn;
908 	const struct ieee80211_mgmt *mgmt =
909 		(const struct ieee80211_mgmt *) data;
910 
911 	if (!nan || !data ||
912 	    data_len < offsetof(struct ieee80211_mgmt, u.auth.variable))
913 		return -1;
914 
915 	peer = nan_get_peer(nan, mgmt->da);
916 	if (!peer) {
917 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Peer not found " MACSTR,
918 			   MAC2STR(mgmt->da));
919 		return -1;
920 	}
921 
922 	/* Pairing was rejected. Clear peer schedule if no active NDPs */
923 	if (!peer->pairing.pasn) {
924 		if (dl_list_empty(&peer->ndps) && !peer->ndp_setup.ndp)
925 			nan_clear_peer_schedule(nan, peer);
926 
927 		return 0;
928 	}
929 
930 	pasn = peer->pairing.pasn;
931 
932 	ret = wpa_pasn_auth_tx_status(pasn, data, data_len, acked);
933 	if (ret == 1) {
934 		u8 nd_pmk[PMK_LEN];
935 
936 		if (pasn->status == WLAN_STATUS_SUCCESS &&
937 		    nan_pairing_derive_nd_pmk(nan, peer, nd_pmk)) {
938 			pasn->status = WLAN_STATUS_UNSPECIFIED_FAILURE;
939 			wpa_printf(MSG_DEBUG,
940 				   "NAN: Pairing: Failed to derive ND PMK");
941 		}
942 
943 		ret = nan->cfg->pairing_result_cb(nan->cfg->cb_ctx,
944 						  peer->nmi_addr, pasn->akmp,
945 						  pasn->cipher, pasn->status,
946 						  &pasn->ptk,
947 						  pasn->status ==
948 						  WLAN_STATUS_SUCCESS ? nd_pmk :
949 						  NULL);
950 		forced_memzero(nd_pmk, PMK_LEN);
951 		if (pasn->status != WLAN_STATUS_SUCCESS || ret < 0) {
952 			nan_pairing_deinit_peer(peer);
953 			return -1;
954 		}
955 
956 		nan_pairing_done(nan, peer);
957 
958 		/*
959 		 * Allow the peer to install the keys before transmitting the
960 		 * follow-up.
961 		 */
962 		/* FIX: A blocking sleep should not really be used here, i.e.,
963 		 * this needs to be removed or replace with a registered eloop
964 		 * timeout to avoid blocking the process. */
965 		os_sleep(0, 30000);
966 
967 		if (nan_send_nik(nan, peer) < 0) {
968 			wpa_printf(MSG_DEBUG,
969 				   "NAN: Pairing: Failed to send NIK");
970 			nan_pairing_deinit_peer(peer);
971 			return -1;
972 		}
973 	}
974 
975 	wpabuf_free(pasn->frame);
976 	pasn->frame = NULL;
977 
978 	return 0;
979 }
980 
981 
982 /**
983  * nan_parse_csia - Parse NAN Cipher Suite Info Attribute
984  * @csia: Pointer to the CSIA data buffer
985  * @len: Length of the CSIA data buffer
986  * @cs: Pointer to nan_cipher_suite structure to store parsed information
987  * Returns: 0 on success, -1 on failure
988  *
989  * Parses the NAN Cipher Suite Info Attribute (CSIA) and extracts the cipher
990  * suite ID (csid) and instance ID from the attribute. It is assumed that only
991  * one cipher suite is present in the attribute (which is the case for NAN
992  * pairing).
993  */
nan_parse_csia(const u8 * csia,size_t len,struct nan_cipher_suite * cs)994 static int nan_parse_csia(const u8 *csia, size_t len,
995 			  struct nan_cipher_suite *cs)
996 {
997 	/* Capabilities (1) + Cipher Suite list (2) */
998 	if (len < sizeof(struct nan_cipher_suite_info) +
999 	    sizeof(struct nan_cipher_suite)) {
1000 		wpa_printf(MSG_DEBUG, "NAN: Pairing: CSIA too short");
1001 		return -1;
1002 	}
1003 
1004 	cs->csid = csia[1];
1005 	cs->instance_id = csia[2];
1006 
1007 	if (cs->csid != NAN_CS_PK_PASN_128 && cs->csid != NAN_CS_PK_PASN_256) {
1008 		wpa_printf(MSG_DEBUG,
1009 			   "NAN: Pairing: Unsupported cipher suite in CSIA: %u",
1010 			   cs->csid);
1011 		return -1;
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 
1018 /**
1019  * nan_pairing_process_elems - Process NAN pairing information elements
1020  * @nan_data: NAN state data
1021  * @peer: NAN peer information structure
1022  * @mgmt: PASN Authentication frame
1023  * @len: Length of the PASN Authentication frame
1024  * @cs: Output cipher suite structure to be filled
1025  * Returns: 0 on success, -1 on failure
1026  *
1027  * This function processes NAN pairing information elements from a PASN
1028  * Authentication frame. It extracts the selected cipher suite and intance ID.
1029  */
nan_pairing_process_elems(struct nan_data * nan_data,struct nan_peer * peer,const struct ieee80211_mgmt * mgmt,size_t len,struct nan_cipher_suite * cs)1030 static int nan_pairing_process_elems(struct nan_data *nan_data,
1031 				     struct nan_peer *peer,
1032 				     const struct ieee80211_mgmt *mgmt,
1033 				     size_t len, struct nan_cipher_suite *cs)
1034 {
1035 	const u8 *ies;
1036 	size_t ies_len;
1037 	const u8 *buf;
1038 	struct wpabuf *ie_buf;
1039 	struct nan_attrs attrs;
1040 	int ret;
1041 
1042 	if (len < offsetof(struct ieee80211_mgmt, u.auth.variable)) {
1043 		wpa_printf(MSG_DEBUG,
1044 			   "NAN: Pairing: PASN frame too short for NAN elements");
1045 		return -1;
1046 	}
1047 
1048 	ies = mgmt->u.auth.variable;
1049 	ies_len = len - offsetof(struct ieee80211_mgmt, u.auth.variable);
1050 
1051 	buf = get_vendor_ie(ies, ies_len, NAN_IE_VENDOR_TYPE);
1052 	if (!buf)
1053 		return -1;
1054 
1055 	ie_buf = ieee802_11_defrag(buf + 2, buf[1], false);
1056 	if (!ie_buf)
1057 		return -1;
1058 
1059 	buf = wpabuf_head(ie_buf);
1060 	ret = nan_parse_attrs(nan_data, &buf[4], wpabuf_len(ie_buf) - 4,
1061 			      &attrs);
1062 	if (ret)
1063 		goto fail;
1064 
1065 	nan_parse_peer_dev_capa_ext(nan_data, peer, &attrs);
1066 
1067 	if (!attrs.cipher_suite_info || !attrs.cipher_suite_info_len ||
1068 	    nan_parse_csia(attrs.cipher_suite_info, attrs.cipher_suite_info_len,
1069 			   cs) < 0) {
1070 		wpa_printf(MSG_DEBUG, "NAN: Pairing: CSIA missing or invalid");
1071 		ret = -1;
1072 	}
1073 
1074 
1075 	nan_attrs_clear(nan_data, &attrs);
1076 fail:
1077 	wpabuf_free(ie_buf);
1078 	return ret;
1079 }
1080 
1081 
1082 /**
1083  * nan_pairing_handle_auth_1 - Handle the first PASN frame in NAN pairing
1084  * @nan_data: Pointer to NAN data structure
1085  * @own_addr: Own MAC address
1086  * @peer: Pointer to NAN peer structure
1087  * @mgmt: Pointer to the received PASN frame
1088  * @len: Length of the PASN frame
1089  * Returns: 0 on success, -1 on failure
1090  *
1091  * This function processes the first PASN Authentication frame during NAN
1092  * pairing as a responder. It initializes the PASN data structure, prepares
1093  * the necessary information elements, and delegates to the PASN module to
1094  * handle the authentication.
1095  */
nan_pairing_handle_auth_1(struct nan_data * nan_data,u8 * own_addr,struct nan_peer * peer,const struct ieee80211_mgmt * mgmt,size_t len)1096 static int nan_pairing_handle_auth_1(struct nan_data *nan_data, u8 *own_addr,
1097 				     struct nan_peer *peer,
1098 				     const struct ieee80211_mgmt *mgmt,
1099 				     size_t len)
1100 {
1101 	struct nan_cipher_suite cs;
1102 	struct pasn_data *pasn;
1103 	int cipher;
1104 
1105 	if (peer->pairing.self_pairing_role != NAN_PAIRING_ROLE_RESPONDER) {
1106 		wpa_printf(MSG_DEBUG,
1107 			   "NAN: Pairing: Unexpected Auth1 frame");
1108 		return -1;
1109 	}
1110 
1111 	pasn = peer->pairing.pasn;
1112 
1113 	if (nan_pairing_process_elems(nan_data, peer, mgmt, len, &cs)) {
1114 		wpa_printf(MSG_DEBUG,
1115 			   "NAN: Pairing: Handle Auth1 NAN attributes failed");
1116 		return -1;
1117 	}
1118 
1119 	cipher = cs.csid == NAN_CS_PK_PASN_256 ? WPA_CIPHER_GCMP_256 :
1120 		WPA_CIPHER_CCMP;
1121 
1122 	if (cipher != pasn->cipher) {
1123 		wpa_printf(MSG_DEBUG,
1124 			   "NAN: Pairing: Cipher suite mismatch (CSIA: %s, PASN: %s)",
1125 			   wpa_cipher_txt(cipher),
1126 			   wpa_cipher_txt(pasn->cipher));
1127 		return -1;
1128 	}
1129 
1130 	if (handle_auth_pasn_1(pasn, own_addr, peer->nmi_addr, mgmt, len,
1131 			       false) < 0) {
1132 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Handle Auth1 failed");
1133 		return -1;
1134 	}
1135 
1136 	return 0;
1137 }
1138 
1139 
nan_pairing_handle_auth_2(struct nan_data * nan_data,struct nan_peer * peer,const struct ieee80211_mgmt * mgmt,size_t len)1140 static int nan_pairing_handle_auth_2(struct nan_data *nan_data,
1141 				     struct nan_peer *peer,
1142 				     const struct ieee80211_mgmt *mgmt,
1143 				     size_t len)
1144 {
1145 	struct wpa_pasn_params_data pasn_data;
1146 	struct pasn_data *pasn = peer->pairing.pasn;
1147 
1148 	if (wpa_pasn_auth_rx(peer->pairing.pasn, (const u8 *)mgmt, len,
1149 			     &pasn_data) < 0) {
1150 		wpa_printf(MSG_DEBUG,
1151 			   "NAN: Pairing: wpa_pasn_auth_rx() failed");
1152 		nan_data->cfg->pairing_result_cb(
1153 			nan_data->cfg->cb_ctx, peer->nmi_addr, pasn->akmp,
1154 			pasn->cipher, WLAN_STATUS_UNSPECIFIED_FAILURE, NULL,
1155 			NULL);
1156 		nan_pairing_deinit_peer(peer);
1157 		return -1;
1158 	}
1159 
1160 	return 0;
1161 }
1162 
1163 
nan_pairing_handle_auth_3(struct nan_data * nan_data,struct nan_peer * peer,const struct ieee80211_mgmt * mgmt,size_t len)1164 static int nan_pairing_handle_auth_3(struct nan_data *nan_data,
1165 				     struct nan_peer *peer,
1166 				     const struct ieee80211_mgmt *mgmt,
1167 				     size_t len)
1168 {
1169 	struct pasn_data *pasn = peer->pairing.pasn;
1170 	int ret;
1171 	u16 status = WLAN_STATUS_SUCCESS;
1172 	u8 nd_pmk[PMK_LEN];
1173 
1174 	ret = handle_auth_pasn_3(pasn, nan_data->cfg->nmi_addr, peer->nmi_addr,
1175 				 mgmt, len);
1176 	if (ret < 0) {
1177 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1178 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Handle Auth3 failed");
1179 	} else {
1180 		if (nan_pairing_derive_nd_pmk(nan_data, peer, nd_pmk)) {
1181 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1182 			wpa_printf(MSG_DEBUG,
1183 				   "NAN: Pairing: Failed to derive ND PMK");
1184 		}
1185 	}
1186 
1187 	ret = nan_data->cfg->pairing_result_cb(nan_data->cfg->cb_ctx,
1188 					       peer->nmi_addr, pasn->akmp,
1189 					       pasn->cipher, status,
1190 					       &pasn->ptk,
1191 					       status == WLAN_STATUS_SUCCESS ?
1192 					       nd_pmk : NULL);
1193 	forced_memzero(nd_pmk, PMK_LEN);
1194 	if (ret < 0 || status != WLAN_STATUS_SUCCESS)
1195 		nan_pairing_deinit_peer(peer);
1196 	else if (status == WLAN_STATUS_SUCCESS)
1197 		nan_pairing_done(nan_data, peer);
1198 
1199 	/* Don't clear PASN data if pairing is successful. If caching is
1200 	 * enabled, it will still be needed when the NIK is received from
1201 	 * the peer.
1202 	 */
1203 	return status == WLAN_STATUS_SUCCESS ? ret : -1;
1204 }
1205 
1206 
1207 /**
1208  * nan_pairing_auth_rx - Handle received NAN pairing Authentication frames
1209  * @nan_data: Pointer to NAN data structure
1210  * @mgmt: Pointer to the PASN Authentication frame
1211  * @len: Length of the PASN Authentication frame in bytes
1212  * Returns: 0 on success, -1 on failure
1213  */
nan_pairing_auth_rx(struct nan_data * nan_data,const struct ieee80211_mgmt * mgmt,size_t len)1214 int nan_pairing_auth_rx(struct nan_data *nan_data,
1215 			const struct ieee80211_mgmt *mgmt, size_t len)
1216 {
1217 	struct nan_peer *peer;
1218 	u16 auth_alg, auth_transaction, status_code;
1219 	int ret;
1220 	struct wpabuf *nan_ie;
1221 	const u8 *buf;
1222 
1223 	if (len < offsetof(struct ieee80211_mgmt, u.auth.variable))
1224 		return -1;
1225 
1226 	if (!ether_addr_equal(mgmt->da, nan_data->cfg->nmi_addr)) {
1227 		wpa_printf(MSG_DEBUG, "NAN: Pairing: Not our frame");
1228 		return -1;
1229 	}
1230 
1231 	auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1232 	auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1233 	status_code = le_to_host16(mgmt->u.auth.status_code);
1234 
1235 	if (auth_alg != WLAN_AUTH_PASN) {
1236 		wpa_printf(MSG_DEBUG,
1237 			   "NAN: Pairing: Not a PASN frame, auth_alg=%d",
1238 			   auth_alg);
1239 		return -1;
1240 	}
1241 
1242 	buf = get_vendor_ie(mgmt->u.auth.variable,
1243 			    len - offsetof(struct ieee80211_mgmt,
1244 					   u.auth.variable),
1245 			    NAN_IE_VENDOR_TYPE);
1246 	if (!buf)
1247 		return -1;
1248 
1249 	nan_ie = ieee802_11_defrag(buf + 2, buf[1], false);
1250 	if (!nan_ie) {
1251 		wpa_printf(MSG_DEBUG,
1252 			   "NAN: Pairing: No NAN element in PASN Authentication frame");
1253 		return -1;
1254 	}
1255 
1256 	ret = nan_add_peer(nan_data, mgmt->sa, wpabuf_head_u8(nan_ie) + 4,
1257 			   wpabuf_len(nan_ie) - 4);
1258 	wpabuf_free(nan_ie);
1259 	if (ret) {
1260 		wpa_printf(MSG_DEBUG,
1261 			   "NAN: Pairing: Failed to add peer from PASN");
1262 		return -1;
1263 	}
1264 
1265 	peer = nan_get_peer(nan_data, mgmt->sa);
1266 	if (!peer) {
1267 		wpa_printf(MSG_DEBUG,
1268 			   "NAN: Pairing: Failed to get a peer that was just added");
1269 		return -1;
1270 	}
1271 
1272 	if (!peer->pairing.pasn) {
1273 		if (status_code == WLAN_STATUS_SUCCESS &&
1274 		    auth_transaction == 1) {
1275 			struct nan_cipher_suite cs;
1276 			const u8 *rsne;
1277 			struct wpa_ie_data rsn_data;
1278 
1279 			if (nan_pairing_process_elems(nan_data, peer, mgmt, len,
1280 						      &cs)) {
1281 				wpa_printf(MSG_DEBUG,
1282 					   "NAN: Pairing: Handle Auth1 NAN attributes failed");
1283 				return -1;
1284 			}
1285 
1286 			rsne = get_ie(mgmt->u.auth.variable,
1287 				      len - offsetof(struct ieee80211_mgmt,
1288 						     u.auth.variable),
1289 				      WLAN_EID_RSN);
1290 			if (!rsne) {
1291 				wpa_printf(MSG_DEBUG,
1292 					   "NAN: Pairing: RSNE missing in Auth1");
1293 				return -1;
1294 			}
1295 
1296 			if (wpa_parse_wpa_ie_rsn(rsne, rsne[1] + 2,
1297 						 &rsn_data)) {
1298 				wpa_printf(MSG_DEBUG,
1299 					   "NAN: Pairing: Failed to parse RSNE in Auth1");
1300 				return -1;
1301 			}
1302 
1303 			wpabuf_free(peer->pairing.pending_auth1);
1304 			peer->pairing.pending_auth1 =
1305 				wpabuf_alloc_copy(mgmt, len);
1306 			if (!peer->pairing.pending_auth1)
1307 				return -1;
1308 
1309 			nan_data->cfg->pairing_request(nan_data->cfg->cb_ctx,
1310 						       peer->nmi_addr, cs.csid,
1311 						       cs.instance_id,
1312 						       &rsn_data);
1313 			return 0;
1314 		}
1315 
1316 		wpa_printf(MSG_DEBUG,
1317 			   "NAN: Pairing: PASN data not initialized for peer");
1318 		return -1;
1319 	}
1320 
1321 	if (status_code != WLAN_STATUS_SUCCESS) {
1322 		struct pasn_data *pasn = peer->pairing.pasn;
1323 
1324 		nan_data->cfg->pairing_result_cb(nan_data->cfg->cb_ctx,
1325 						 peer->nmi_addr, pasn->akmp,
1326 						 pasn->cipher, status_code,
1327 						 NULL, NULL);
1328 		nan_pairing_deinit_peer(peer);
1329 		wpa_printf(MSG_DEBUG,
1330 			   "NAN: Pairing: Authentication rejected - status=%u",
1331 			   status_code);
1332 		return -1;
1333 	}
1334 
1335 	if (auth_transaction == 1)
1336 		return nan_pairing_handle_auth_1(nan_data,
1337 						 nan_data->cfg->nmi_addr, peer,
1338 						 mgmt, len);
1339 	if (auth_transaction == 2)
1340 		return nan_pairing_handle_auth_2(nan_data, peer, mgmt, len);
1341 	if (auth_transaction == 3)
1342 		return nan_pairing_handle_auth_3(nan_data, peer, mgmt, len);
1343 
1344 	return -1;
1345 }
1346 
1347 
1348 /**
1349  * nan_pairing_followup_rx - Process received NAN pairing follow-up frame
1350  * @nan_data: NAN data context
1351  * @peer_addr: MAC address of the peer device
1352  * @shared_key_descr: Pointer to the shared key descriptor attribute
1353  * @attr_len: Length of the shared key descriptor attribute
1354  * Returns: true if the follow-up frame was processed, false otherwise.
1355  *
1356  * This function processes a received NAN pairing follow-up frame. It extracts
1357  * the NIK (NAN Identity Key) from the frame and notifies about the received
1358  * NIK.
1359  *
1360  * If the local device acted as the responder in the pairing process, it also
1361  * sends the local NIK to the peer.
1362  */
nan_pairing_followup_rx(struct nan_data * nan_data,const u8 * peer_addr,const struct nan_shared_key * shared_key_descr,size_t attr_len)1363 bool nan_pairing_followup_rx(struct nan_data *nan_data, const u8 *peer_addr,
1364 			     const struct nan_shared_key *shared_key_descr,
1365 			     size_t attr_len)
1366 {
1367 	struct nan_peer *peer;
1368 	struct pasn_data *pasn;
1369 	const struct wpa_eapol_key *key_desc;
1370 	struct wpa_eapol_ie_parse ie;
1371 	const struct nan_nik_kde *nik_kde;
1372 	const struct nan_key_lifetime_kde *lifetime_kde;
1373 	const u8 *pos;
1374 	struct wpabuf *key_data = NULL;
1375 	u16 key_data_len, key_info;
1376 	bool ret = false;
1377 	u16 lifetime_bitmap;
1378 
1379 	peer = nan_get_peer(nan_data, peer_addr);
1380 	if (!peer) {
1381 		wpa_printf(MSG_DEBUG,
1382 			   "NAN: Pairing: Follow-up frame from unknown peer");
1383 		return false;
1384 	}
1385 
1386 	pasn = peer->pairing.pasn;
1387 	if (!pasn || !pasn->ptk.kek_len) {
1388 		wpa_printf(MSG_DEBUG,
1389 			   "NAN: Pairing: No PASN data for follow-up frame");
1390 		return false;
1391 	}
1392 
1393 	if (!nan_data->cfg->pairing_cfg.npk_caching) {
1394 		wpa_printf(MSG_DEBUG,
1395 			   "NAN: Pairing: NPK caching not enabled, ignore follow-up frame");
1396 		return false;
1397 	}
1398 
1399 	key_desc = (const struct wpa_eapol_key *) shared_key_descr->key;
1400 	key_info = WPA_GET_BE16(key_desc->key_info);
1401 
1402 	if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1403 		wpa_printf(MSG_DEBUG,
1404 			   "NAN: Pairing: Follow-up frame does not contain pairwise key");
1405 		return false;
1406 	}
1407 
1408 	if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1409 		wpa_printf(MSG_DEBUG,
1410 			   "NAN: Pairing: Follow-up frame does not contain encrypted key data");
1411 		return false;
1412 	}
1413 
1414 	if (attr_len < sizeof(*shared_key_descr) + sizeof(*key_desc) + 2) {
1415 		wpa_printf(MSG_DEBUG,
1416 			   "NAN: Pairing: Follow-up frame too short for Key Data Length field");
1417 		return false;
1418 	}
1419 
1420 	pos = shared_key_descr->key + sizeof(*key_desc);
1421 	key_data_len = WPA_GET_BE16(pos);
1422 
1423 	if (attr_len < sizeof(*shared_key_descr) + sizeof(*key_desc) + 2 +
1424 	    key_data_len) {
1425 		wpa_printf(MSG_DEBUG,
1426 			   "NAN: Pairing: Follow-up frame too short for Key Data field");
1427 		return false;
1428 	}
1429 
1430 	pos += 2;
1431 
1432 	key_data = nan_crypto_decrypt_key_data(pasn->ptk.kek, pasn->ptk.kek_len,
1433 					       pos, key_data_len);
1434 	if (!key_data) {
1435 		wpa_printf(MSG_DEBUG,
1436 			   "NAN: Pairing: Failed to decrypt key data in follow-up frame");
1437 		goto fail;
1438 	}
1439 
1440 	if (wpa_parse_kde_ies(wpabuf_head(key_data), wpabuf_len(key_data),
1441 			      &ie) < 0) {
1442 		wpa_printf(MSG_DEBUG,
1443 			   "NAN: Pairing: Failed to parse decrypted key data in follow-up frame");
1444 		goto fail;
1445 	}
1446 
1447 	if (!ie.nan_nik) {
1448 		wpa_printf(MSG_DEBUG,
1449 			   "NAN: Pairing: NIK KDE missing in decrypted key data");
1450 		goto fail;
1451 	}
1452 
1453 	nik_kde = (const struct nan_nik_kde *) ie.nan_nik;
1454 	if (nik_kde->cipher_ver != NAN_NIRA_CIPHER_VER_128) {
1455 		wpa_printf(MSG_DEBUG,
1456 			   "NAN: Pairing: Unsupported NIK cipher version: %u",
1457 			   nik_kde->cipher_ver);
1458 		goto fail;
1459 	}
1460 
1461 	if (!ie.nan_key_lifetime) {
1462 		wpa_printf(MSG_DEBUG,
1463 			   "NAN: Pairing: Key Lifetime KDE missing in decrypted key data");
1464 		goto fail;
1465 	}
1466 
1467 	lifetime_kde = (const struct nan_key_lifetime_kde *)
1468 		ie.nan_key_lifetime;
1469 	lifetime_bitmap = le_to_host16(lifetime_kde->key_bitmap);
1470 	if (!(lifetime_bitmap & NAN_KEY_LIFETIME_NIK)) {
1471 		wpa_printf(MSG_DEBUG,
1472 			   "NAN: Pairing: Unexpected key bitmap in Key "
1473 			   "Lifetime KDE: 0x%02x",
1474 			   lifetime_bitmap);
1475 		goto fail;
1476 	}
1477 
1478 	nan_data->cfg->update_pairing_credentials(
1479 		nan_data->cfg->cb_ctx, nik_kde->nik, NAN_NIK_LEN,
1480 		nik_kde->cipher_ver, be_to_host32(lifetime_kde->lifetime_sec),
1481 		pasn_get_akmp(pasn),
1482 		pasn_get_pmk(pasn), pasn_get_pmk_len(pasn));
1483 
1484 	if (peer->pairing.self_pairing_role == NAN_PAIRING_ROLE_RESPONDER)
1485 		nan_send_nik(nan_data, peer);
1486 
1487 	ret = true;
1488 fail:
1489 	nan_pairing_deinit_peer(peer);
1490 	wpabuf_free(key_data);
1491 	return ret;
1492 }
1493 
1494 
nan_pairing_set_pairing_setup(struct nan_data * nan,bool value)1495 int nan_pairing_set_pairing_setup(struct nan_data *nan, bool value)
1496 {
1497 	wpa_printf(MSG_DEBUG, "NAN: SET: Pairing setup: %d -> %d",
1498 		   nan->cfg->pairing_cfg.pairing_setup, value);
1499 	nan->cfg->pairing_cfg.pairing_setup = value;
1500 	return 0;
1501 }
1502 
1503 
nan_pairing_set_npk_caching(struct nan_data * nan,bool value)1504 int nan_pairing_set_npk_caching(struct nan_data *nan, bool value)
1505 {
1506 	wpa_printf(MSG_DEBUG, "NAN: SET: NPK caching: %d -> %d",
1507 		   nan->cfg->pairing_cfg.npk_caching, value);
1508 	nan->cfg->pairing_cfg.npk_caching = value;
1509 	return 0;
1510 }
1511 
1512 
nan_pairing_set_pairing_verification(struct nan_data * nan,bool value)1513 int nan_pairing_set_pairing_verification(struct nan_data *nan, bool value)
1514 {
1515 	wpa_printf(MSG_DEBUG, "NAN: SET: Pairing verification: %d -> %d",
1516 		   nan->cfg->pairing_cfg.pairing_verification, value);
1517 
1518 	if (!nan->cfg->pairing_cfg.pairing_verification && value &&
1519 	    nan_nira_get_tag_nonce(nan->cfg, nan->nira_nonce,
1520 				   nan->nira_tag) < 0) {
1521 		wpa_printf(MSG_INFO,
1522 			   "NAN: Failed to enable pairing verification");
1523 		return -1;
1524 	}
1525 
1526 	nan->cfg->pairing_cfg.pairing_verification = value;
1527 
1528 	return 0;
1529 }
1530 
1531 
nan_pairing_set_cipher_suites(struct nan_data * nan,u32 value)1532 int nan_pairing_set_cipher_suites(struct nan_data *nan, u32 value)
1533 {
1534 	if (value & ~(NAN_PAIRING_PASN_128 | NAN_PAIRING_PASN_256)) {
1535 		wpa_printf(MSG_INFO,
1536 			   "NAN: Pairing: Invalid cipher suites 0x%08x", value);
1537 		return -1;
1538 	}
1539 
1540 	wpa_printf(MSG_DEBUG,
1541 		   "NAN: SET: Pairing cipher suites: 0x%08x -> 0x%08x",
1542 		   nan->cfg->pairing_cfg.cipher_suites, value);
1543 
1544 	nan->cfg->pairing_cfg.cipher_suites = value;
1545 	return 0;
1546 }
1547 
1548 
nan_pairing_set_nik(struct nan_data * nan,const u8 * nik,size_t nik_len)1549 int nan_pairing_set_nik(struct nan_data *nan, const u8 *nik, size_t nik_len)
1550 {
1551 	u8 nonce[NAN_NIRA_NONCE_LEN];
1552 	u8 tag[NAN_NIRA_TAG_LEN];
1553 
1554 	if (!nik || nik_len != NAN_NIK_LEN) {
1555 		wpa_printf(MSG_INFO, "NAN: Pairing: Invalid NIK (len=%zu)",
1556 			   nik_len);
1557 		return -1;
1558 	}
1559 
1560 	os_memcpy(nan->cfg->nik, nik, NAN_NIK_LEN);
1561 
1562 	if (nan->cfg->pairing_cfg.pairing_verification) {
1563 		if (nan_nira_get_tag_nonce(nan->cfg, nonce, tag) < 0) {
1564 			wpa_printf(MSG_INFO,
1565 				   "NAN: Failed to set NIRA for new NIK");
1566 			return -1;
1567 		}
1568 		os_memcpy(nan->nira_nonce, nonce, NAN_NIRA_NONCE_LEN);
1569 		wpa_hexdump_key(MSG_DEBUG, "NAN: NIRA nonce",
1570 				nan->nira_nonce, NAN_NIRA_NONCE_LEN);
1571 		os_memcpy(nan->nira_tag, tag, NAN_NIRA_TAG_LEN);
1572 		wpa_hexdump_key(MSG_DEBUG, "NAN: NIRA tag",
1573 				nan->nira_tag, NAN_NIRA_TAG_LEN);
1574 	} else {
1575 		os_memset(nan->nira_nonce, 0, NAN_NIRA_NONCE_LEN);
1576 		os_memset(nan->nira_tag, 0, NAN_NIRA_TAG_LEN);
1577 	}
1578 
1579 	wpa_hexdump_key(MSG_DEBUG, "NAN: New NIK", nan->cfg->nik, NAN_NIK_LEN);
1580 
1581 	return 0;
1582 }
1583 
1584 
nan_pairing_set_nik_lifetime(struct nan_data * nan,u32 lifetime)1585 int nan_pairing_set_nik_lifetime(struct nan_data *nan, u32 lifetime)
1586 {
1587 	if (!lifetime) {
1588 		wpa_printf(MSG_INFO, "NAN: Pairing: Invalid NIK lifetime (%u)",
1589 			   lifetime);
1590 		return -1;
1591 	}
1592 
1593 	nan->cfg->nik_lifetime = lifetime;
1594 	wpa_printf(MSG_DEBUG, "NAN: SET: NIK lifetime: %u seconds",
1595 		   lifetime);
1596 	return 0;
1597 }
1598 
1599 
nan_pairing_is_peer_paired(struct nan_data * nan_data,const u8 * peer_addr)1600 bool nan_pairing_is_peer_paired(struct nan_data *nan_data, const u8 *peer_addr)
1601 {
1602 	struct nan_peer *peer;
1603 
1604 	peer = nan_get_peer(nan_data, peer_addr);
1605 	if (!peer)
1606 		return false;
1607 
1608 	return !!(peer->pairing.flags & NAN_PAIRING_FLAG_PAIRED);
1609 }
1610 
1611 
nan_pairing_unpair_peer(struct nan_data * nan_data,const u8 * peer_addr)1612 void nan_pairing_unpair_peer(struct nan_data *nan_data, const u8 *peer_addr)
1613 {
1614 	struct nan_peer *peer;
1615 
1616 	peer = nan_get_peer(nan_data, peer_addr);
1617 	if (!peer)
1618 		return;
1619 
1620 	wpa_printf(MSG_DEBUG, "NAN: Unpair peer " MACSTR,
1621 		   MAC2STR(peer->nmi_addr));
1622 
1623 	peer->pairing.flags &= ~NAN_PAIRING_FLAG_PAIRED;
1624 	nan_pairing_deinit_peer(peer);
1625 }
1626