Line data Source code
1 : /*
2 : * Wi-Fi Protected Setup - common functionality
3 : * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
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 :
11 : #include "common.h"
12 : #include "common/defs.h"
13 : #include "common/ieee802_11_common.h"
14 : #include "crypto/aes_wrap.h"
15 : #include "crypto/crypto.h"
16 : #include "crypto/dh_group5.h"
17 : #include "crypto/sha1.h"
18 : #include "crypto/sha256.h"
19 : #include "crypto/random.h"
20 : #include "wps_i.h"
21 : #include "wps_dev_attr.h"
22 :
23 :
24 906 : void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
25 : const char *label, u8 *res, size_t res_len)
26 : {
27 : u8 i_buf[4], key_bits[4];
28 : const u8 *addr[4];
29 : size_t len[4];
30 : int i, iter;
31 : u8 hash[SHA256_MAC_LEN], *opos;
32 : size_t left;
33 :
34 906 : WPA_PUT_BE32(key_bits, res_len * 8);
35 :
36 906 : addr[0] = i_buf;
37 906 : len[0] = sizeof(i_buf);
38 906 : addr[1] = label_prefix;
39 906 : len[1] = label_prefix_len;
40 906 : addr[2] = (const u8 *) label;
41 906 : len[2] = os_strlen(label);
42 906 : addr[3] = key_bits;
43 906 : len[3] = sizeof(key_bits);
44 :
45 906 : iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
46 906 : opos = res;
47 906 : left = res_len;
48 :
49 3624 : for (i = 1; i <= iter; i++) {
50 2718 : WPA_PUT_BE32(i_buf, i);
51 2718 : hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
52 2718 : if (i < iter) {
53 1812 : os_memcpy(opos, hash, SHA256_MAC_LEN);
54 1812 : opos += SHA256_MAC_LEN;
55 1812 : left -= SHA256_MAC_LEN;
56 : } else
57 906 : os_memcpy(opos, hash, left);
58 : }
59 906 : }
60 :
61 :
62 907 : int wps_derive_keys(struct wps_data *wps)
63 : {
64 : struct wpabuf *pubkey, *dh_shared;
65 : u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
66 : const u8 *addr[3];
67 : size_t len[3];
68 : u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
69 :
70 907 : if (wps->dh_privkey == NULL) {
71 0 : wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
72 0 : return -1;
73 : }
74 :
75 907 : pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
76 907 : if (pubkey == NULL) {
77 0 : wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
78 0 : return -1;
79 : }
80 :
81 907 : wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
82 907 : wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
83 907 : dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
84 907 : dh5_free(wps->dh_ctx);
85 907 : wps->dh_ctx = NULL;
86 907 : dh_shared = wpabuf_zeropad(dh_shared, 192);
87 907 : if (dh_shared == NULL) {
88 1 : wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
89 1 : return -1;
90 : }
91 :
92 : /* Own DH private key is not needed anymore */
93 906 : wpabuf_clear_free(wps->dh_privkey);
94 906 : wps->dh_privkey = NULL;
95 :
96 906 : wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
97 :
98 : /* DHKey = SHA-256(g^AB mod p) */
99 906 : addr[0] = wpabuf_head(dh_shared);
100 906 : len[0] = wpabuf_len(dh_shared);
101 906 : sha256_vector(1, addr, len, dhkey);
102 906 : wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
103 906 : wpabuf_clear_free(dh_shared);
104 :
105 : /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
106 906 : addr[0] = wps->nonce_e;
107 906 : len[0] = WPS_NONCE_LEN;
108 906 : addr[1] = wps->mac_addr_e;
109 906 : len[1] = ETH_ALEN;
110 906 : addr[2] = wps->nonce_r;
111 906 : len[2] = WPS_NONCE_LEN;
112 906 : hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
113 906 : wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
114 :
115 906 : wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
116 : keys, sizeof(keys));
117 906 : os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
118 906 : os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
119 906 : os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
120 : WPS_EMSK_LEN);
121 :
122 906 : wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
123 906 : wps->authkey, WPS_AUTHKEY_LEN);
124 906 : wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
125 906 : wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
126 906 : wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
127 :
128 906 : return 0;
129 : }
130 :
131 :
132 819 : int wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
133 : size_t dev_passwd_len)
134 : {
135 : u8 hash[SHA256_MAC_LEN];
136 :
137 819 : if (hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
138 819 : (dev_passwd_len + 1) / 2, hash) < 0)
139 1 : return -1;
140 818 : os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
141 1636 : if (hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
142 818 : dev_passwd + (dev_passwd_len + 1) / 2,
143 : dev_passwd_len / 2, hash) < 0)
144 0 : return -1;
145 818 : os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
146 :
147 818 : wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
148 : dev_passwd, dev_passwd_len);
149 818 : wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
150 818 : wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
151 818 : return 0;
152 : }
153 :
154 :
155 1842 : struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
156 : size_t encr_len)
157 : {
158 : struct wpabuf *decrypted;
159 1842 : const size_t block_size = 16;
160 : size_t i;
161 : u8 pad;
162 : const u8 *pos;
163 :
164 : /* AES-128-CBC */
165 1842 : if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
166 : {
167 5 : wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
168 5 : return NULL;
169 : }
170 :
171 1837 : decrypted = wpabuf_alloc(encr_len - block_size);
172 1837 : if (decrypted == NULL)
173 1 : return NULL;
174 :
175 1836 : wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
176 1836 : wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
177 1836 : if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
178 : wpabuf_len(decrypted))) {
179 0 : wpabuf_clear_free(decrypted);
180 0 : return NULL;
181 : }
182 :
183 1836 : wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
184 : decrypted);
185 :
186 1836 : pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
187 1836 : pad = *pos;
188 1836 : if (pad > wpabuf_len(decrypted)) {
189 1 : wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
190 1 : wpabuf_clear_free(decrypted);
191 1 : return NULL;
192 : }
193 27238 : for (i = 0; i < pad; i++) {
194 25404 : if (*pos-- != pad) {
195 1 : wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
196 : "string");
197 1 : wpabuf_clear_free(decrypted);
198 1 : return NULL;
199 : }
200 : }
201 1834 : decrypted->used -= pad;
202 :
203 1834 : return decrypted;
204 : }
205 :
206 :
207 : /**
208 : * wps_pin_checksum - Compute PIN checksum
209 : * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
210 : * Returns: Checksum digit
211 : */
212 335 : unsigned int wps_pin_checksum(unsigned int pin)
213 : {
214 335 : unsigned int accum = 0;
215 1975 : while (pin) {
216 1305 : accum += 3 * (pin % 10);
217 1305 : pin /= 10;
218 1305 : accum += pin % 10;
219 1305 : pin /= 10;
220 : }
221 :
222 335 : return (10 - accum % 10) % 10;
223 : }
224 :
225 :
226 : /**
227 : * wps_pin_valid - Check whether a PIN has a valid checksum
228 : * @pin: Eight digit PIN (i.e., including the checksum digit)
229 : * Returns: 1 if checksum digit is valid, or 0 if not
230 : */
231 20 : unsigned int wps_pin_valid(unsigned int pin)
232 : {
233 20 : return wps_pin_checksum(pin / 10) == (pin % 10);
234 : }
235 :
236 :
237 : /**
238 : * wps_generate_pin - Generate a random PIN
239 : * Returns: Eight digit PIN (i.e., including the checksum digit)
240 : */
241 316 : int wps_generate_pin(unsigned int *pin)
242 : {
243 : unsigned int val;
244 :
245 : /* Generate seven random digits for the PIN */
246 316 : if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0)
247 1 : return -1;
248 315 : val %= 10000000;
249 :
250 : /* Append checksum digit */
251 315 : *pin = val * 10 + wps_pin_checksum(val);
252 315 : return 0;
253 : }
254 :
255 :
256 289 : int wps_pin_str_valid(const char *pin)
257 : {
258 : const char *p;
259 : size_t len;
260 :
261 289 : p = pin;
262 2889 : while (*p >= '0' && *p <= '9')
263 2311 : p++;
264 289 : if (*p != '\0')
265 1 : return 0;
266 :
267 288 : len = p - pin;
268 288 : return len == 4 || len == 8;
269 : }
270 :
271 :
272 191 : void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
273 : u16 config_error, u16 error_indication, const u8 *mac_addr)
274 : {
275 : union wps_event_data data;
276 :
277 191 : if (wps->event_cb == NULL)
278 191 : return;
279 :
280 191 : os_memset(&data, 0, sizeof(data));
281 191 : data.fail.msg = msg;
282 191 : data.fail.config_error = config_error;
283 191 : data.fail.error_indication = error_indication;
284 191 : os_memcpy(data.fail.peer_macaddr, mac_addr, ETH_ALEN);
285 191 : wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
286 : }
287 :
288 :
289 632 : void wps_success_event(struct wps_context *wps, const u8 *mac_addr)
290 : {
291 : union wps_event_data data;
292 :
293 632 : if (wps->event_cb == NULL)
294 632 : return;
295 :
296 632 : os_memset(&data, 0, sizeof(data));
297 632 : os_memcpy(data.success.peer_macaddr, mac_addr, ETH_ALEN);
298 632 : wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, &data);
299 : }
300 :
301 :
302 17 : void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part,
303 : const u8 *mac_addr)
304 : {
305 : union wps_event_data data;
306 :
307 17 : if (wps->event_cb == NULL)
308 17 : return;
309 :
310 17 : os_memset(&data, 0, sizeof(data));
311 17 : data.pwd_auth_fail.enrollee = enrollee;
312 17 : data.pwd_auth_fail.part = part;
313 17 : os_memcpy(data.pwd_auth_fail.peer_macaddr, mac_addr, ETH_ALEN);
314 17 : wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
315 : }
316 :
317 :
318 13 : void wps_pbc_overlap_event(struct wps_context *wps)
319 : {
320 13 : if (wps->event_cb == NULL)
321 13 : return;
322 :
323 13 : wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
324 : }
325 :
326 :
327 7 : void wps_pbc_timeout_event(struct wps_context *wps)
328 : {
329 7 : if (wps->event_cb == NULL)
330 7 : return;
331 :
332 7 : wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
333 : }
334 :
335 :
336 124 : void wps_pbc_active_event(struct wps_context *wps)
337 : {
338 124 : if (wps->event_cb == NULL)
339 124 : return;
340 :
341 124 : wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL);
342 : }
343 :
344 :
345 54 : void wps_pbc_disable_event(struct wps_context *wps)
346 : {
347 54 : if (wps->event_cb == NULL)
348 54 : return;
349 :
350 54 : wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL);
351 : }
352 :
353 :
354 : #ifdef CONFIG_WPS_OOB
355 :
356 4 : struct wpabuf * wps_get_oob_cred(struct wps_context *wps, int rf_band,
357 : int channel)
358 : {
359 : struct wps_data data;
360 : struct wpabuf *plain;
361 :
362 4 : plain = wpabuf_alloc(500);
363 4 : if (plain == NULL) {
364 1 : wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
365 : "credential");
366 1 : return NULL;
367 : }
368 :
369 3 : os_memset(&data, 0, sizeof(data));
370 3 : data.wps = wps;
371 3 : data.auth_type = wps->auth_types;
372 3 : data.encr_type = wps->encr_types;
373 3 : if (wps_build_cred(&data, plain) ||
374 3 : (rf_band && wps_build_rf_bands_attr(plain, rf_band)) ||
375 6 : (channel && wps_build_ap_channel(plain, channel)) ||
376 6 : wps_build_mac_addr(plain, wps->dev.mac_addr) ||
377 3 : wps_build_wfa_ext(plain, 0, NULL, 0)) {
378 0 : os_free(data.new_psk);
379 0 : wpabuf_clear_free(plain);
380 0 : return NULL;
381 : }
382 :
383 4 : if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk &&
384 1 : wps->ap) {
385 : struct wps_credential cred;
386 :
387 1 : wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
388 : "on credential token generation");
389 :
390 1 : os_memset(&cred, 0, sizeof(cred));
391 1 : os_memcpy(cred.ssid, wps->ssid, wps->ssid_len);
392 1 : cred.ssid_len = wps->ssid_len;
393 1 : cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
394 1 : cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
395 1 : os_memcpy(cred.key, data.new_psk, data.new_psk_len);
396 1 : cred.key_len = data.new_psk_len;
397 :
398 1 : wps->wps_state = WPS_STATE_CONFIGURED;
399 2 : wpa_hexdump_ascii_key(MSG_DEBUG,
400 : "WPS: Generated random passphrase",
401 1 : data.new_psk, data.new_psk_len);
402 1 : if (wps->cred_cb)
403 1 : wps->cred_cb(wps->cb_ctx, &cred);
404 : }
405 :
406 3 : os_free(data.new_psk);
407 :
408 3 : return plain;
409 : }
410 :
411 :
412 19 : struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
413 : const struct wpabuf *pubkey,
414 : const struct wpabuf *dev_pw)
415 : {
416 : struct wpabuf *data;
417 :
418 19 : data = wpabuf_alloc(200);
419 19 : if (data == NULL)
420 1 : return NULL;
421 :
422 36 : if (wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
423 36 : wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
424 18 : wps_build_wfa_ext(data, 0, NULL, 0)) {
425 0 : wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password "
426 : "token");
427 0 : wpabuf_clear_free(data);
428 0 : return NULL;
429 : }
430 :
431 18 : return data;
432 : }
433 :
434 :
435 8 : int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
436 : {
437 : struct wpabuf msg;
438 : size_t i;
439 :
440 12 : for (i = 0; i < attr->num_cred; i++) {
441 : struct wps_credential local_cred;
442 : struct wps_parse_attr cattr;
443 :
444 8 : os_memset(&local_cred, 0, sizeof(local_cred));
445 8 : wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
446 14 : if (wps_parse_msg(&msg, &cattr) < 0 ||
447 6 : wps_process_cred(&cattr, &local_cred)) {
448 4 : wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
449 : "credential");
450 4 : return -1;
451 : }
452 4 : wps->cred_cb(wps->cb_ctx, &local_cred);
453 : }
454 :
455 4 : return 0;
456 : }
457 :
458 :
459 : #endif /* CONFIG_WPS_OOB */
460 :
461 :
462 53 : int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
463 : {
464 : const char *pos;
465 :
466 : /* <categ>-<OUI>-<subcateg> */
467 53 : WPA_PUT_BE16(dev_type, atoi(str));
468 53 : pos = os_strchr(str, '-');
469 53 : if (pos == NULL)
470 2 : return -1;
471 51 : pos++;
472 51 : if (hexstr2bin(pos, &dev_type[2], 4))
473 1 : return -1;
474 50 : pos = os_strchr(pos, '-');
475 50 : if (pos == NULL)
476 1 : return -1;
477 49 : pos++;
478 49 : WPA_PUT_BE16(&dev_type[6], atoi(pos));
479 :
480 :
481 49 : return 0;
482 : }
483 :
484 :
485 15511 : char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
486 : size_t buf_len)
487 : {
488 : int ret;
489 :
490 31022 : ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
491 15511 : WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
492 15511 : WPA_GET_BE16(&dev_type[6]));
493 15511 : if (os_snprintf_error(buf_len, ret))
494 0 : return NULL;
495 :
496 15511 : return buf;
497 : }
498 :
499 :
500 515 : void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
501 : {
502 : const u8 *addr[2];
503 : size_t len[2];
504 : u8 hash[SHA1_MAC_LEN];
505 515 : u8 nsid[16] = {
506 : 0x52, 0x64, 0x80, 0xf8,
507 : 0xc9, 0x9b,
508 : 0x4b, 0xe5,
509 : 0xa6, 0x55,
510 : 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
511 : };
512 :
513 515 : addr[0] = nsid;
514 515 : len[0] = sizeof(nsid);
515 515 : addr[1] = mac_addr;
516 515 : len[1] = 6;
517 515 : sha1_vector(2, addr, len, hash);
518 515 : os_memcpy(uuid, hash, 16);
519 :
520 : /* Version: 5 = named-based version using SHA-1 */
521 515 : uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
522 :
523 : /* Variant specified in RFC 4122 */
524 515 : uuid[8] = 0x80 | (uuid[8] & 0x3f);
525 515 : }
526 :
527 :
528 1413 : u16 wps_config_methods_str2bin(const char *str)
529 : {
530 1413 : u16 methods = 0;
531 :
532 1413 : if (str == NULL || str[0] == '\0') {
533 : /* Default to enabling methods based on build configuration */
534 1357 : methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
535 1357 : methods |= WPS_CONFIG_VIRT_DISPLAY;
536 : #ifdef CONFIG_WPS_NFC
537 1357 : methods |= WPS_CONFIG_NFC_INTERFACE;
538 : #endif /* CONFIG_WPS_NFC */
539 : #ifdef CONFIG_P2P
540 1133 : methods |= WPS_CONFIG_P2PS;
541 : #endif /* CONFIG_P2P */
542 : } else {
543 56 : if (os_strstr(str, "ethernet"))
544 1 : methods |= WPS_CONFIG_ETHERNET;
545 56 : if (os_strstr(str, "label"))
546 40 : methods |= WPS_CONFIG_LABEL;
547 56 : if (os_strstr(str, "display"))
548 17 : methods |= WPS_CONFIG_DISPLAY;
549 56 : if (os_strstr(str, "ext_nfc_token"))
550 1 : methods |= WPS_CONFIG_EXT_NFC_TOKEN;
551 56 : if (os_strstr(str, "int_nfc_token"))
552 1 : methods |= WPS_CONFIG_INT_NFC_TOKEN;
553 56 : if (os_strstr(str, "nfc_interface"))
554 11 : methods |= WPS_CONFIG_NFC_INTERFACE;
555 56 : if (os_strstr(str, "push_button"))
556 43 : methods |= WPS_CONFIG_PUSHBUTTON;
557 56 : if (os_strstr(str, "keypad"))
558 13 : methods |= WPS_CONFIG_KEYPAD;
559 56 : if (os_strstr(str, "virtual_display"))
560 13 : methods |= WPS_CONFIG_VIRT_DISPLAY;
561 56 : if (os_strstr(str, "physical_display"))
562 1 : methods |= WPS_CONFIG_PHY_DISPLAY;
563 56 : if (os_strstr(str, "virtual_push_button"))
564 2 : methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
565 56 : if (os_strstr(str, "physical_push_button"))
566 1 : methods |= WPS_CONFIG_PHY_PUSHBUTTON;
567 56 : if (os_strstr(str, "p2ps"))
568 9 : methods |= WPS_CONFIG_P2PS;
569 : }
570 :
571 1413 : return methods;
572 : }
573 :
574 :
575 36 : struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
576 : {
577 : struct wpabuf *msg;
578 :
579 36 : wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
580 :
581 36 : msg = wpabuf_alloc(1000);
582 36 : if (msg == NULL)
583 0 : return NULL;
584 :
585 72 : if (wps_build_version(msg) ||
586 72 : wps_build_msg_type(msg, WPS_WSC_ACK) ||
587 72 : wps_build_enrollee_nonce(wps, msg) ||
588 72 : wps_build_registrar_nonce(wps, msg) ||
589 36 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
590 0 : wpabuf_free(msg);
591 0 : return NULL;
592 : }
593 :
594 36 : return msg;
595 : }
596 :
597 :
598 145 : struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
599 : {
600 : struct wpabuf *msg;
601 :
602 145 : wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
603 :
604 145 : msg = wpabuf_alloc(1000);
605 145 : if (msg == NULL)
606 0 : return NULL;
607 :
608 290 : if (wps_build_version(msg) ||
609 290 : wps_build_msg_type(msg, WPS_WSC_NACK) ||
610 290 : wps_build_enrollee_nonce(wps, msg) ||
611 290 : wps_build_registrar_nonce(wps, msg) ||
612 290 : wps_build_config_error(msg, wps->config_error) ||
613 145 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
614 0 : wpabuf_free(msg);
615 0 : return NULL;
616 : }
617 :
618 145 : return msg;
619 : }
620 :
621 :
622 : #ifdef CONFIG_WPS_NFC
623 :
624 19 : struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey,
625 : struct wpabuf *dev_pw)
626 : {
627 : struct wpabuf *ret;
628 :
629 19 : if (pubkey == NULL || dev_pw == NULL)
630 0 : return NULL;
631 :
632 19 : ret = wps_build_nfc_pw_token(id, pubkey, dev_pw);
633 19 : if (ndef && ret) {
634 : struct wpabuf *tmp;
635 15 : tmp = ndef_build_wifi(ret);
636 15 : wpabuf_free(ret);
637 15 : if (tmp == NULL)
638 0 : return NULL;
639 15 : ret = tmp;
640 : }
641 :
642 19 : return ret;
643 : }
644 :
645 :
646 41 : int wps_nfc_gen_dh(struct wpabuf **pubkey, struct wpabuf **privkey)
647 : {
648 41 : struct wpabuf *priv = NULL, *pub = NULL;
649 : void *dh_ctx;
650 :
651 41 : dh_ctx = dh5_init(&priv, &pub);
652 41 : if (dh_ctx == NULL)
653 2 : return -1;
654 39 : pub = wpabuf_zeropad(pub, 192);
655 39 : if (pub == NULL) {
656 0 : wpabuf_free(priv);
657 0 : return -1;
658 : }
659 39 : wpa_hexdump_buf(MSG_DEBUG, "WPS: Generated new DH pubkey", pub);
660 39 : dh5_free(dh_ctx);
661 :
662 39 : wpabuf_free(*pubkey);
663 39 : *pubkey = pub;
664 39 : wpabuf_clear_free(*privkey);
665 39 : *privkey = priv;
666 :
667 39 : return 0;
668 : }
669 :
670 :
671 26 : struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
672 : struct wpabuf **privkey,
673 : struct wpabuf **dev_pw)
674 : {
675 : struct wpabuf *pw;
676 : u16 val;
677 :
678 26 : pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
679 26 : if (pw == NULL)
680 1 : return NULL;
681 :
682 25 : if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
683 23 : WPS_OOB_DEVICE_PASSWORD_LEN) ||
684 23 : random_get_bytes((u8 *) &val, sizeof(val))) {
685 4 : wpabuf_free(pw);
686 4 : return NULL;
687 : }
688 :
689 21 : if (wps_nfc_gen_dh(pubkey, privkey) < 0) {
690 2 : wpabuf_free(pw);
691 2 : return NULL;
692 : }
693 :
694 19 : *id = 0x10 + val % 0xfff0;
695 19 : wpabuf_clear_free(*dev_pw);
696 19 : *dev_pw = pw;
697 :
698 19 : return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw);
699 : }
700 :
701 :
702 14 : struct wpabuf * wps_build_nfc_handover_req(struct wps_context *ctx,
703 : struct wpabuf *nfc_dh_pubkey)
704 : {
705 : struct wpabuf *msg;
706 : void *len;
707 :
708 14 : if (ctx == NULL)
709 0 : return NULL;
710 :
711 14 : wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
712 : "handover request");
713 :
714 14 : if (nfc_dh_pubkey == NULL) {
715 0 : wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
716 : "configured");
717 0 : return NULL;
718 : }
719 :
720 14 : msg = wpabuf_alloc(1000);
721 14 : if (msg == NULL)
722 1 : return msg;
723 13 : len = wpabuf_put(msg, 2);
724 :
725 13 : if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
726 13 : nfc_dh_pubkey, NULL, 0) ||
727 26 : wps_build_uuid_e(msg, ctx->uuid) ||
728 13 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
729 0 : wpabuf_free(msg);
730 0 : return NULL;
731 : }
732 :
733 13 : WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
734 :
735 13 : return msg;
736 : }
737 :
738 :
739 12 : static int wps_build_ssid(struct wpabuf *msg, struct wps_context *wps)
740 : {
741 12 : wpa_printf(MSG_DEBUG, "WPS: * SSID");
742 24 : wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID in Connection Handover Select",
743 12 : wps->ssid, wps->ssid_len);
744 12 : wpabuf_put_be16(msg, ATTR_SSID);
745 12 : wpabuf_put_be16(msg, wps->ssid_len);
746 12 : wpabuf_put_data(msg, wps->ssid, wps->ssid_len);
747 12 : return 0;
748 : }
749 :
750 :
751 12 : static int wps_build_ap_freq(struct wpabuf *msg, int freq)
752 : {
753 : enum hostapd_hw_mode mode;
754 : u8 channel, rf_band;
755 : u16 ap_channel;
756 :
757 12 : if (freq <= 0)
758 3 : return 0;
759 :
760 9 : mode = ieee80211_freq_to_chan(freq, &channel);
761 9 : if (mode == NUM_HOSTAPD_MODES)
762 0 : return 0; /* Unknown channel */
763 :
764 9 : if (mode == HOSTAPD_MODE_IEEE80211G || mode == HOSTAPD_MODE_IEEE80211B)
765 8 : rf_band = WPS_RF_24GHZ;
766 1 : else if (mode == HOSTAPD_MODE_IEEE80211A)
767 1 : rf_band = WPS_RF_50GHZ;
768 0 : else if (mode == HOSTAPD_MODE_IEEE80211AD)
769 0 : rf_band = WPS_RF_60GHZ;
770 : else
771 0 : return 0; /* Unknown band */
772 9 : ap_channel = channel;
773 :
774 18 : if (wps_build_rf_bands_attr(msg, rf_band) ||
775 9 : wps_build_ap_channel(msg, ap_channel))
776 0 : return -1;
777 :
778 9 : return 0;
779 : }
780 :
781 :
782 13 : struct wpabuf * wps_build_nfc_handover_sel(struct wps_context *ctx,
783 : struct wpabuf *nfc_dh_pubkey,
784 : const u8 *bssid, int freq)
785 : {
786 : struct wpabuf *msg;
787 : void *len;
788 :
789 13 : if (ctx == NULL)
790 0 : return NULL;
791 :
792 13 : wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
793 : "handover select");
794 :
795 13 : if (nfc_dh_pubkey == NULL) {
796 0 : wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
797 : "configured");
798 0 : return NULL;
799 : }
800 :
801 13 : msg = wpabuf_alloc(1000);
802 13 : if (msg == NULL)
803 1 : return msg;
804 12 : len = wpabuf_put(msg, 2);
805 :
806 12 : if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
807 12 : nfc_dh_pubkey, NULL, 0) ||
808 24 : wps_build_ssid(msg, ctx) ||
809 24 : wps_build_ap_freq(msg, freq) ||
810 24 : (bssid && wps_build_mac_addr(msg, bssid)) ||
811 12 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
812 0 : wpabuf_free(msg);
813 0 : return NULL;
814 : }
815 :
816 12 : WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
817 :
818 12 : return msg;
819 : }
820 :
821 :
822 15 : struct wpabuf * wps_build_nfc_handover_req_p2p(struct wps_context *ctx,
823 : struct wpabuf *nfc_dh_pubkey)
824 : {
825 : struct wpabuf *msg;
826 :
827 15 : if (ctx == NULL)
828 0 : return NULL;
829 :
830 15 : wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
831 : "handover request (P2P)");
832 :
833 15 : if (nfc_dh_pubkey == NULL) {
834 0 : wpa_printf(MSG_DEBUG, "WPS: No NFC DH Public Key configured");
835 0 : return NULL;
836 : }
837 :
838 15 : msg = wpabuf_alloc(1000);
839 15 : if (msg == NULL)
840 2 : return msg;
841 :
842 26 : if (wps_build_manufacturer(&ctx->dev, msg) ||
843 26 : wps_build_model_name(&ctx->dev, msg) ||
844 26 : wps_build_model_number(&ctx->dev, msg) ||
845 13 : wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
846 11 : nfc_dh_pubkey, NULL, 0) ||
847 22 : wps_build_rf_bands(&ctx->dev, msg, 0) ||
848 22 : wps_build_serial_number(&ctx->dev, msg) ||
849 22 : wps_build_uuid_e(msg, ctx->uuid) ||
850 11 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
851 2 : wpabuf_free(msg);
852 2 : return NULL;
853 : }
854 :
855 11 : return msg;
856 : }
857 :
858 :
859 25 : struct wpabuf * wps_build_nfc_handover_sel_p2p(struct wps_context *ctx,
860 : int nfc_dev_pw_id,
861 : struct wpabuf *nfc_dh_pubkey,
862 : struct wpabuf *nfc_dev_pw)
863 : {
864 : struct wpabuf *msg;
865 : const u8 *dev_pw;
866 : size_t dev_pw_len;
867 :
868 25 : if (ctx == NULL)
869 0 : return NULL;
870 :
871 25 : wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
872 : "handover select (P2P)");
873 :
874 25 : if (nfc_dh_pubkey == NULL ||
875 13 : (nfc_dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
876 : nfc_dev_pw == NULL)) {
877 0 : wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
878 : "configured");
879 0 : return NULL;
880 : }
881 :
882 25 : msg = wpabuf_alloc(1000);
883 25 : if (msg == NULL)
884 2 : return msg;
885 :
886 23 : if (nfc_dev_pw) {
887 12 : dev_pw = wpabuf_head(nfc_dev_pw);
888 12 : dev_pw_len = wpabuf_len(nfc_dev_pw);
889 : } else {
890 11 : dev_pw = NULL;
891 11 : dev_pw_len = 0;
892 : }
893 :
894 46 : if (wps_build_manufacturer(&ctx->dev, msg) ||
895 46 : wps_build_model_name(&ctx->dev, msg) ||
896 46 : wps_build_model_number(&ctx->dev, msg) ||
897 23 : wps_build_oob_dev_pw(msg, nfc_dev_pw_id, nfc_dh_pubkey,
898 23 : dev_pw, dev_pw_len) ||
899 46 : wps_build_rf_bands(&ctx->dev, msg, 0) ||
900 46 : wps_build_serial_number(&ctx->dev, msg) ||
901 46 : wps_build_uuid_e(msg, ctx->uuid) ||
902 23 : wps_build_wfa_ext(msg, 0, NULL, 0)) {
903 0 : wpabuf_free(msg);
904 0 : return NULL;
905 : }
906 :
907 23 : return msg;
908 : }
909 :
910 : #endif /* CONFIG_WPS_NFC */
|