2 * Wi-Fi Protected Setup - common functionality
3 * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "crypto/dh_group5.h"
21 #include "crypto/sha1.h"
22 #include "crypto/sha256.h"
24 #include "wps_dev_attr.h"
27 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
28 const char *label, u8 *res, size_t res_len)
30 u8 i_buf[4], key_bits[4];
34 u8 hash[SHA256_MAC_LEN], *opos;
37 WPA_PUT_BE32(key_bits, res_len * 8);
40 len[0] = sizeof(i_buf);
41 addr[1] = label_prefix;
42 len[1] = label_prefix_len;
43 addr[2] = (const u8 *) label;
44 len[2] = os_strlen(label);
46 len[3] = sizeof(key_bits);
48 iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
52 for (i = 1; i <= iter; i++) {
53 WPA_PUT_BE32(i_buf, i);
54 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
56 os_memcpy(opos, hash, SHA256_MAC_LEN);
57 opos += SHA256_MAC_LEN;
58 left -= SHA256_MAC_LEN;
60 os_memcpy(opos, hash, left);
65 int wps_derive_keys(struct wps_data *wps)
67 struct wpabuf *pubkey, *dh_shared;
68 u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
71 u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
73 if (wps->dh_privkey == NULL) {
74 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
78 pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
80 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
84 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
85 wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
86 dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
87 dh5_free(wps->dh_ctx);
89 dh_shared = wpabuf_zeropad(dh_shared, 192);
90 if (dh_shared == NULL) {
91 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
95 /* Own DH private key is not needed anymore */
96 wpabuf_free(wps->dh_privkey);
97 wps->dh_privkey = NULL;
99 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
101 /* DHKey = SHA-256(g^AB mod p) */
102 addr[0] = wpabuf_head(dh_shared);
103 len[0] = wpabuf_len(dh_shared);
104 sha256_vector(1, addr, len, dhkey);
105 wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
106 wpabuf_free(dh_shared);
108 /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
109 addr[0] = wps->nonce_e;
110 len[0] = WPS_NONCE_LEN;
111 addr[1] = wps->mac_addr_e;
113 addr[2] = wps->nonce_r;
114 len[2] = WPS_NONCE_LEN;
115 hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
116 wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
118 wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
120 os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
121 os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
122 os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
125 wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
126 wps->authkey, WPS_AUTHKEY_LEN);
127 wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
128 wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
129 wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
135 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
136 size_t dev_passwd_len)
138 u8 hash[SHA256_MAC_LEN];
140 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
141 (dev_passwd_len + 1) / 2, hash);
142 os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
143 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
144 dev_passwd + (dev_passwd_len + 1) / 2,
145 dev_passwd_len / 2, hash);
146 os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
148 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
149 dev_passwd, dev_passwd_len);
150 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
151 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
155 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
158 struct wpabuf *decrypted;
159 const size_t block_size = 16;
165 if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
167 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
171 decrypted = wpabuf_alloc(encr_len - block_size);
172 if (decrypted == NULL)
175 wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
176 wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
177 if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
178 wpabuf_len(decrypted))) {
179 wpabuf_free(decrypted);
183 wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
186 pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
188 if (pad > wpabuf_len(decrypted)) {
189 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
190 wpabuf_free(decrypted);
193 for (i = 0; i < pad; i++) {
195 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
197 wpabuf_free(decrypted);
201 decrypted->used -= pad;
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
212 unsigned int wps_pin_checksum(unsigned int pin)
214 unsigned int accum = 0;
216 accum += 3 * (pin % 10);
222 return (10 - accum % 10) % 10;
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
231 unsigned int wps_pin_valid(unsigned int pin)
233 return wps_pin_checksum(pin / 10) == (pin % 10);
238 * wps_generate_pin - Generate a random PIN
239 * Returns: Eight digit PIN (i.e., including the checksum digit)
241 unsigned int wps_generate_pin(void)
245 /* Generate seven random digits for the PIN */
246 if (os_get_random((unsigned char *) &val, sizeof(val)) < 0) {
249 val = os_random() ^ now.sec ^ now.usec;
253 /* Append checksum digit */
254 return val * 10 + wps_pin_checksum(val);
258 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
261 union wps_event_data data;
263 if (wps->event_cb == NULL)
266 os_memset(&data, 0, sizeof(data));
268 data.fail.config_error = config_error;
269 wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
273 void wps_success_event(struct wps_context *wps)
275 if (wps->event_cb == NULL)
278 wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
282 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
284 union wps_event_data data;
286 if (wps->event_cb == NULL)
289 os_memset(&data, 0, sizeof(data));
290 data.pwd_auth_fail.enrollee = enrollee;
291 data.pwd_auth_fail.part = part;
292 wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
296 void wps_pbc_overlap_event(struct wps_context *wps)
298 if (wps->event_cb == NULL)
301 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
305 void wps_pbc_timeout_event(struct wps_context *wps)
307 if (wps->event_cb == NULL)
310 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
314 #ifdef CONFIG_WPS_OOB
316 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
318 struct wps_data data;
319 struct wpabuf *plain;
321 plain = wpabuf_alloc(500);
323 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
328 os_memset(&data, 0, sizeof(data));
330 data.auth_type = wps->auth_types;
331 data.encr_type = wps->encr_types;
332 if (wps_build_version(plain) ||
333 wps_build_cred(&data, plain) ||
334 wps_build_wfa_ext(plain, 0, NULL, 0)) {
343 static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
347 data = wpabuf_alloc(9 + WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
349 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
350 "device password attribute");
354 wpabuf_free(wps->oob_conf.dev_password);
355 wps->oob_conf.dev_password =
356 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
357 if (wps->oob_conf.dev_password == NULL) {
358 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
364 if (wps_build_version(data) ||
365 wps_build_oob_dev_password(data, wps) ||
366 wps_build_wfa_ext(data, 0, NULL, 0)) {
367 wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
377 static int wps_parse_oob_dev_pwd(struct wps_context *wps,
380 struct oob_conf_data *oob_conf = &wps->oob_conf;
381 struct wps_parse_attr attr;
384 if (wps_parse_msg(data, &attr) < 0 ||
385 attr.oob_dev_password == NULL) {
386 wpa_printf(MSG_ERROR, "WPS: OOB device password not found");
390 pos = attr.oob_dev_password;
392 oob_conf->pubkey_hash =
393 wpabuf_alloc_copy(pos, WPS_OOB_PUBKEY_HASH_LEN);
394 if (oob_conf->pubkey_hash == NULL) {
395 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
399 pos += WPS_OOB_PUBKEY_HASH_LEN;
401 wps->oob_dev_pw_id = WPA_GET_BE16(pos);
402 pos += sizeof(wps->oob_dev_pw_id);
404 oob_conf->dev_password =
405 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
406 if (oob_conf->dev_password == NULL) {
407 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
411 wpa_snprintf_hex_uppercase(wpabuf_put(oob_conf->dev_password,
412 wpabuf_size(oob_conf->dev_password)),
413 wpabuf_size(oob_conf->dev_password), pos,
414 WPS_OOB_DEVICE_PASSWORD_LEN);
420 static int wps_parse_oob_cred(struct wps_context *wps, struct wpabuf *data)
423 struct wps_parse_attr attr;
426 if (wps_parse_msg(data, &attr) < 0 || attr.num_cred <= 0) {
427 wpa_printf(MSG_ERROR, "WPS: OOB credential not found");
431 for (i = 0; i < attr.num_cred; i++) {
432 struct wps_credential local_cred;
433 struct wps_parse_attr cattr;
435 os_memset(&local_cred, 0, sizeof(local_cred));
436 wpabuf_set(&msg, attr.cred[i], attr.cred_len[i]);
437 if (wps_parse_msg(&msg, &cattr) < 0 ||
438 wps_process_cred(&cattr, &local_cred)) {
439 wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
443 wps->cred_cb(wps->cb_ctx, &local_cred);
450 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
454 int ret, write_f, oob_method = wps->oob_conf.oob_method;
457 write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
459 oob_priv = oob_dev->init_func(wps, oob_dev, registrar);
460 if (oob_priv == NULL) {
461 wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
466 if (oob_method == OOB_METHOD_CRED)
467 data = wps_get_oob_cred(wps);
469 data = wps_get_oob_dev_pwd(wps);
472 if (data == NULL || oob_dev->write_func(oob_priv, data) < 0)
475 data = oob_dev->read_func(oob_priv);
479 if (oob_method == OOB_METHOD_CRED)
480 ret = wps_parse_oob_cred(wps, data);
482 ret = wps_parse_oob_dev_pwd(wps, data);
486 oob_dev->deinit_func(oob_priv);
489 wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
497 struct oob_device_data * wps_get_oob_device(char *device_type)
499 #ifdef CONFIG_WPS_UFD
500 if (os_strstr(device_type, "ufd") != NULL)
501 return &oob_ufd_device_data;
502 #endif /* CONFIG_WPS_UFD */
503 #ifdef CONFIG_WPS_NFC
504 if (os_strstr(device_type, "nfc") != NULL)
505 return &oob_nfc_device_data;
506 #endif /* CONFIG_WPS_NFC */
512 #ifdef CONFIG_WPS_NFC
513 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name)
515 if (device_name == NULL)
517 #ifdef CONFIG_WPS_NFC_PN531
518 if (os_strstr(device_name, "pn531") != NULL)
519 return &oob_nfc_pn531_device_data;
520 #endif /* CONFIG_WPS_NFC_PN531 */
524 #endif /* CONFIG_WPS_NFC */
527 int wps_get_oob_method(char *method)
529 if (os_strstr(method, "pin-e") != NULL)
530 return OOB_METHOD_DEV_PWD_E;
531 if (os_strstr(method, "pin-r") != NULL)
532 return OOB_METHOD_DEV_PWD_R;
533 if (os_strstr(method, "cred") != NULL)
534 return OOB_METHOD_CRED;
535 return OOB_METHOD_UNKNOWN;
538 #endif /* CONFIG_WPS_OOB */
541 int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
545 /* <categ>-<OUI>-<subcateg> */
546 WPA_PUT_BE16(dev_type, atoi(str));
547 pos = os_strchr(str, '-');
551 if (hexstr2bin(pos, &dev_type[2], 4))
553 pos = os_strchr(pos, '-');
557 WPA_PUT_BE16(&dev_type[6], atoi(pos));
564 char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
569 ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
570 WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
571 WPA_GET_BE16(&dev_type[6]));
572 if (ret < 0 || (unsigned int) ret >= buf_len)
579 void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
583 u8 hash[SHA1_MAC_LEN];
585 0x52, 0x64, 0x80, 0xf8,
589 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
593 len[0] = sizeof(nsid);
596 sha1_vector(2, addr, len, hash);
597 os_memcpy(uuid, hash, 16);
599 /* Version: 5 = named-based version using SHA-1 */
600 uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
602 /* Variant specified in RFC 4122 */
603 uuid[8] = 0x80 | (uuid[8] & 0x3f);
607 u16 wps_config_methods_str2bin(const char *str)
612 /* Default to enabling methods based on build configuration */
613 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
615 methods |= WPS_CONFIG_VIRT_DISPLAY;
616 #endif /* CONFIG_WPS2 */
617 #ifdef CONFIG_WPS_UFD
618 methods |= WPS_CONFIG_USBA;
619 #endif /* CONFIG_WPS_UFD */
620 #ifdef CONFIG_WPS_NFC
621 methods |= WPS_CONFIG_NFC_INTERFACE;
622 #endif /* CONFIG_WPS_NFC */
624 if (os_strstr(str, "usba"))
625 methods |= WPS_CONFIG_USBA;
626 if (os_strstr(str, "ethernet"))
627 methods |= WPS_CONFIG_ETHERNET;
628 if (os_strstr(str, "label"))
629 methods |= WPS_CONFIG_LABEL;
630 if (os_strstr(str, "display"))
631 methods |= WPS_CONFIG_DISPLAY;
632 if (os_strstr(str, "ext_nfc_token"))
633 methods |= WPS_CONFIG_EXT_NFC_TOKEN;
634 if (os_strstr(str, "int_nfc_token"))
635 methods |= WPS_CONFIG_INT_NFC_TOKEN;
636 if (os_strstr(str, "nfc_interface"))
637 methods |= WPS_CONFIG_NFC_INTERFACE;
638 if (os_strstr(str, "push_button"))
639 methods |= WPS_CONFIG_PUSHBUTTON;
640 if (os_strstr(str, "keypad"))
641 methods |= WPS_CONFIG_KEYPAD;
643 if (os_strstr(str, "virtual_display"))
644 methods |= WPS_CONFIG_VIRT_DISPLAY;
645 if (os_strstr(str, "physical_display"))
646 methods |= WPS_CONFIG_PHY_DISPLAY;
647 if (os_strstr(str, "virtual_push_button"))
648 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
649 if (os_strstr(str, "physical_push_button"))
650 methods |= WPS_CONFIG_PHY_PUSHBUTTON;
651 #endif /* CONFIG_WPS2 */