xref: /freebsd/contrib/wpa/src/pasn/pasn_common.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*
2  * PASN common processing
3  *
4  * Copyright (C) 2024, Qualcomm Innovation Center, Inc.
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "utils/includes.h"
11 
12 #include "utils/common.h"
13 #include "common/wpa_common.h"
14 #include "common/sae.h"
15 #include "crypto/sha384.h"
16 #include "crypto/crypto.h"
17 #include "common/ieee802_11_defs.h"
18 #include "pasn_common.h"
19 
20 
pasn_data_init(void)21 struct pasn_data * pasn_data_init(void)
22 {
23 	struct pasn_data *pasn = os_zalloc(sizeof(struct pasn_data));
24 
25 	return pasn;
26 }
27 
28 
pasn_data_deinit(struct pasn_data * pasn)29 void pasn_data_deinit(struct pasn_data *pasn)
30 {
31 	bin_clear_free(pasn, sizeof(struct pasn_data));
32 }
33 
34 
pasn_register_callbacks(struct pasn_data * pasn,void * cb_ctx,int (* send_mgmt)(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait),int (* validate_custom_pmkid)(void * ctx,const u8 * addr,const u8 * pmkid))35 void pasn_register_callbacks(struct pasn_data *pasn, void *cb_ctx,
36 			     int (*send_mgmt)(void *ctx, const u8 *data,
37 					      size_t data_len, int noack,
38 					      unsigned int freq,
39 					      unsigned int wait),
40 			     int (*validate_custom_pmkid)(void *ctx,
41 							  const u8 *addr,
42 							  const u8 *pmkid))
43 {
44 	if (!pasn)
45 		return;
46 
47 	pasn->cb_ctx = cb_ctx;
48 	pasn->send_mgmt = send_mgmt;
49 	pasn->validate_custom_pmkid = validate_custom_pmkid;
50 }
51 
52 
pasn_enable_kdk_derivation(struct pasn_data * pasn)53 void pasn_enable_kdk_derivation(struct pasn_data *pasn)
54 {
55 	if (!pasn)
56 		return;
57 	pasn->derive_kdk = true;
58 	pasn->kdk_len = WPA_KDK_MAX_LEN;
59 }
60 
61 
pasn_disable_kdk_derivation(struct pasn_data * pasn)62 void pasn_disable_kdk_derivation(struct pasn_data *pasn)
63 {
64 	if (!pasn)
65 		return;
66 	pasn->derive_kdk = false;
67 	pasn->kdk_len = 0;
68 }
69 
70 
pasn_set_akmp(struct pasn_data * pasn,int akmp)71 void pasn_set_akmp(struct pasn_data *pasn, int akmp)
72 {
73 	if (!pasn)
74 		return;
75 	pasn->akmp = akmp;
76 }
77 
78 
pasn_set_cipher(struct pasn_data * pasn,int cipher)79 void pasn_set_cipher(struct pasn_data *pasn, int cipher)
80 {
81 	if (!pasn)
82 		return;
83 	pasn->cipher = cipher;
84 }
85 
86 
pasn_set_own_addr(struct pasn_data * pasn,const u8 * addr)87 void pasn_set_own_addr(struct pasn_data *pasn, const u8 *addr)
88 {
89 	if (!pasn || !addr)
90 		return;
91 	os_memcpy(pasn->own_addr, addr, ETH_ALEN);
92 }
93 
94 
pasn_set_peer_addr(struct pasn_data * pasn,const u8 * addr)95 void pasn_set_peer_addr(struct pasn_data *pasn, const u8 *addr)
96 {
97 	if (!pasn || !addr)
98 		return;
99 	os_memcpy(pasn->peer_addr, addr, ETH_ALEN);
100 }
101 
102 
pasn_set_bssid(struct pasn_data * pasn,const u8 * addr)103 void pasn_set_bssid(struct pasn_data *pasn, const u8 *addr)
104 {
105 	if (!pasn || !addr)
106 		return;
107 	os_memcpy(pasn->bssid, addr, ETH_ALEN);
108 }
109 
110 
pasn_set_pt(struct pasn_data * pasn,struct sae_pt * pt)111 int pasn_set_pt(struct pasn_data *pasn, struct sae_pt *pt)
112 {
113 	if (!pasn)
114 		return -1;
115 #ifdef CONFIG_SAE
116 	pasn->pt = pt;
117 	return 0;
118 #else /* CONFIG_SAE */
119 	return -1;
120 #endif /* CONFIG_SAE */
121 }
122 
123 
pasn_set_password(struct pasn_data * pasn,const char * password)124 void pasn_set_password(struct pasn_data *pasn, const char *password)
125 {
126 	if (!pasn)
127 		return;
128 	pasn->password = password;
129 }
130 
131 
pasn_set_wpa_key_mgmt(struct pasn_data * pasn,int key_mgmt)132 void pasn_set_wpa_key_mgmt(struct pasn_data *pasn, int key_mgmt)
133 {
134 	if (!pasn)
135 		return;
136 	pasn->wpa_key_mgmt = key_mgmt;
137 }
138 
139 
pasn_set_rsn_pairwise(struct pasn_data * pasn,int rsn_pairwise)140 void pasn_set_rsn_pairwise(struct pasn_data *pasn, int rsn_pairwise)
141 {
142 	if (!pasn)
143 		return;
144 	pasn->rsn_pairwise = rsn_pairwise;
145 }
146 
147 
pasn_set_rsnxe_caps(struct pasn_data * pasn,u16 rsnxe_capab)148 void pasn_set_rsnxe_caps(struct pasn_data *pasn, u16 rsnxe_capab)
149 {
150 	if (!pasn)
151 		return;
152 	pasn->rsnxe_capab = rsnxe_capab;
153 }
154 
155 
pasn_set_rsnxe_ie(struct pasn_data * pasn,const u8 * rsnxe_ie)156 void pasn_set_rsnxe_ie(struct pasn_data *pasn, const u8 *rsnxe_ie)
157 {
158 	if (!pasn || !rsnxe_ie)
159 		return;
160 	pasn->rsnxe_ie = rsnxe_ie;
161 }
162 
163 
pasn_set_custom_pmkid(struct pasn_data * pasn,const u8 * pmkid)164 void pasn_set_custom_pmkid(struct pasn_data *pasn, const u8 *pmkid)
165 {
166 	if (!pasn || !pmkid)
167 		return;
168 	os_memcpy(pasn->custom_pmkid, pmkid, PMKID_LEN);
169 	pasn->custom_pmkid_valid = true;
170 }
171 
172 
pasn_set_extra_ies(struct pasn_data * pasn,const u8 * extra_ies,size_t extra_ies_len)173 int pasn_set_extra_ies(struct pasn_data *pasn, const u8 *extra_ies,
174 		       size_t extra_ies_len)
175 {
176 	if (!pasn || !extra_ies_len || !extra_ies)
177 		return -1;
178 
179 	if (pasn->extra_ies) {
180 		os_free((u8 *) pasn->extra_ies);
181 		pasn->extra_ies_len = extra_ies_len;
182 	}
183 
184 	pasn->extra_ies = os_memdup(extra_ies, extra_ies_len);
185 	if (!pasn->extra_ies) {
186 		wpa_printf(MSG_ERROR,
187 			   "PASN: Extra IEs memory allocation failed");
188 		return -1;
189 	}
190 	pasn->extra_ies_len = extra_ies_len;
191 	return 0;
192 }
193 
194 
pasn_get_akmp(struct pasn_data * pasn)195 int pasn_get_akmp(struct pasn_data *pasn)
196 {
197 	if (!pasn)
198 		return 0;
199 	return pasn->akmp;
200 }
201 
202 
pasn_get_cipher(struct pasn_data * pasn)203 int pasn_get_cipher(struct pasn_data *pasn)
204 {
205 	if (!pasn)
206 		return 0;
207 	return pasn->cipher;
208 }
209 
210 
pasn_get_pmk_len(struct pasn_data * pasn)211 size_t pasn_get_pmk_len(struct pasn_data *pasn)
212 {
213 	if (!pasn)
214 		return 0;
215 	return pasn->pmk_len;
216 }
217 
218 
pasn_get_pmk(struct pasn_data * pasn)219 u8 * pasn_get_pmk(struct pasn_data *pasn)
220 {
221 	if (!pasn)
222 		return NULL;
223 	return pasn->pmk;
224 }
225 
226 
pasn_get_ptk(struct pasn_data * pasn)227 struct wpa_ptk * pasn_get_ptk(struct pasn_data *pasn)
228 {
229 	if (!pasn)
230 		return NULL;
231 	return &pasn->ptk;
232 }
233