2 * Wi-Fi Protected Setup - common functionality
3 * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/aes_wrap.h"
13 #include "crypto/crypto.h"
14 #include "crypto/dh_group5.h"
15 #include "crypto/sha1.h"
16 #include "crypto/sha256.h"
17 #include "crypto/random.h"
21 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
22 const char *label, u8 *res, size_t res_len)
24 u8 i_buf[4], key_bits[4];
28 u8 hash[SHA256_MAC_LEN], *opos;
31 WPA_PUT_BE32(key_bits, res_len * 8);
34 len[0] = sizeof(i_buf);
35 addr[1] = label_prefix;
36 len[1] = label_prefix_len;
37 addr[2] = (const u8 *) label;
38 len[2] = os_strlen(label);
40 len[3] = sizeof(key_bits);
42 iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
46 for (i = 1; i <= iter; i++) {
47 WPA_PUT_BE32(i_buf, i);
48 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
50 os_memcpy(opos, hash, SHA256_MAC_LEN);
51 opos += SHA256_MAC_LEN;
52 left -= SHA256_MAC_LEN;
54 os_memcpy(opos, hash, left);
59 int wps_derive_keys(struct wps_data *wps)
61 struct wpabuf *pubkey, *dh_shared;
62 u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
65 u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
67 if (wps->dh_privkey == NULL) {
68 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
72 pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
74 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
78 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
79 wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
80 dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
81 dh5_free(wps->dh_ctx);
83 dh_shared = wpabuf_zeropad(dh_shared, 192);
84 if (dh_shared == NULL) {
85 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
89 /* Own DH private key is not needed anymore */
90 wpabuf_free(wps->dh_privkey);
91 wps->dh_privkey = NULL;
93 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
95 /* DHKey = SHA-256(g^AB mod p) */
96 addr[0] = wpabuf_head(dh_shared);
97 len[0] = wpabuf_len(dh_shared);
98 sha256_vector(1, addr, len, dhkey);
99 wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
100 wpabuf_free(dh_shared);
102 /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
103 addr[0] = wps->nonce_e;
104 len[0] = WPS_NONCE_LEN;
105 addr[1] = wps->mac_addr_e;
107 addr[2] = wps->nonce_r;
108 len[2] = WPS_NONCE_LEN;
109 hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
110 wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
112 wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
114 os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
115 os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
116 os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
119 wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
120 wps->authkey, WPS_AUTHKEY_LEN);
121 wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
122 wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
123 wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
129 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
130 size_t dev_passwd_len)
132 u8 hash[SHA256_MAC_LEN];
134 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
135 (dev_passwd_len + 1) / 2, hash);
136 os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
137 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
138 dev_passwd + (dev_passwd_len + 1) / 2,
139 dev_passwd_len / 2, hash);
140 os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
142 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
143 dev_passwd, dev_passwd_len);
144 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
145 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
149 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
152 struct wpabuf *decrypted;
153 const size_t block_size = 16;
159 if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
161 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
165 decrypted = wpabuf_alloc(encr_len - block_size);
166 if (decrypted == NULL)
169 wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
170 wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
171 if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
172 wpabuf_len(decrypted))) {
173 wpabuf_free(decrypted);
177 wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
180 pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
182 if (pad > wpabuf_len(decrypted)) {
183 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
184 wpabuf_free(decrypted);
187 for (i = 0; i < pad; i++) {
189 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
191 wpabuf_free(decrypted);
195 decrypted->used -= pad;
202 * wps_pin_checksum - Compute PIN checksum
203 * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
204 * Returns: Checksum digit
206 unsigned int wps_pin_checksum(unsigned int pin)
208 unsigned int accum = 0;
210 accum += 3 * (pin % 10);
216 return (10 - accum % 10) % 10;
221 * wps_pin_valid - Check whether a PIN has a valid checksum
222 * @pin: Eight digit PIN (i.e., including the checksum digit)
223 * Returns: 1 if checksum digit is valid, or 0 if not
225 unsigned int wps_pin_valid(unsigned int pin)
227 return wps_pin_checksum(pin / 10) == (pin % 10);
232 * wps_generate_pin - Generate a random PIN
233 * Returns: Eight digit PIN (i.e., including the checksum digit)
235 unsigned int wps_generate_pin(void)
239 /* Generate seven random digits for the PIN */
240 if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) {
243 val = os_random() ^ now.sec ^ now.usec;
247 /* Append checksum digit */
248 return val * 10 + wps_pin_checksum(val);
252 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
253 u16 config_error, u16 error_indication)
255 union wps_event_data data;
257 if (wps->event_cb == NULL)
260 os_memset(&data, 0, sizeof(data));
262 data.fail.config_error = config_error;
263 data.fail.error_indication = error_indication;
264 wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
268 void wps_success_event(struct wps_context *wps)
270 if (wps->event_cb == NULL)
273 wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
277 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
279 union wps_event_data data;
281 if (wps->event_cb == NULL)
284 os_memset(&data, 0, sizeof(data));
285 data.pwd_auth_fail.enrollee = enrollee;
286 data.pwd_auth_fail.part = part;
287 wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
291 void wps_pbc_overlap_event(struct wps_context *wps)
293 if (wps->event_cb == NULL)
296 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
300 void wps_pbc_timeout_event(struct wps_context *wps)
302 if (wps->event_cb == NULL)
305 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
309 #ifdef CONFIG_WPS_OOB
311 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
313 struct wps_data data;
314 struct wpabuf *plain;
316 plain = wpabuf_alloc(500);
318 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
323 os_memset(&data, 0, sizeof(data));
325 data.auth_type = wps->auth_types;
326 data.encr_type = wps->encr_types;
327 if (wps_build_version(plain) ||
328 wps_build_cred(&data, plain) ||
329 wps_build_wfa_ext(plain, 0, NULL, 0)) {
338 static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
342 data = wpabuf_alloc(9 + WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
344 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
345 "device password attribute");
349 wpabuf_free(wps->oob_conf.dev_password);
350 wps->oob_conf.dev_password =
351 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
352 if (wps->oob_conf.dev_password == NULL) {
353 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
359 if (wps_build_version(data) ||
360 wps_build_oob_dev_password(data, wps) ||
361 wps_build_wfa_ext(data, 0, NULL, 0)) {
362 wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
372 static int wps_parse_oob_dev_pwd(struct wps_context *wps,
375 struct oob_conf_data *oob_conf = &wps->oob_conf;
376 struct wps_parse_attr attr;
379 if (wps_parse_msg(data, &attr) < 0 ||
380 attr.oob_dev_password == NULL) {
381 wpa_printf(MSG_ERROR, "WPS: OOB device password not found");
385 pos = attr.oob_dev_password;
387 oob_conf->pubkey_hash =
388 wpabuf_alloc_copy(pos, WPS_OOB_PUBKEY_HASH_LEN);
389 if (oob_conf->pubkey_hash == NULL) {
390 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
394 pos += WPS_OOB_PUBKEY_HASH_LEN;
396 wps->oob_dev_pw_id = WPA_GET_BE16(pos);
397 pos += sizeof(wps->oob_dev_pw_id);
399 oob_conf->dev_password =
400 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
401 if (oob_conf->dev_password == NULL) {
402 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
406 wpa_snprintf_hex_uppercase(wpabuf_put(oob_conf->dev_password,
407 wpabuf_size(oob_conf->dev_password)),
408 wpabuf_size(oob_conf->dev_password), pos,
409 WPS_OOB_DEVICE_PASSWORD_LEN);
415 static int wps_parse_oob_cred(struct wps_context *wps, struct wpabuf *data)
418 struct wps_parse_attr attr;
421 if (wps_parse_msg(data, &attr) < 0 || attr.num_cred <= 0) {
422 wpa_printf(MSG_ERROR, "WPS: OOB credential not found");
426 for (i = 0; i < attr.num_cred; i++) {
427 struct wps_credential local_cred;
428 struct wps_parse_attr cattr;
430 os_memset(&local_cred, 0, sizeof(local_cred));
431 wpabuf_set(&msg, attr.cred[i], attr.cred_len[i]);
432 if (wps_parse_msg(&msg, &cattr) < 0 ||
433 wps_process_cred(&cattr, &local_cred)) {
434 wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
438 wps->cred_cb(wps->cb_ctx, &local_cred);
445 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
449 int ret, write_f, oob_method = wps->oob_conf.oob_method;
452 write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
454 oob_priv = oob_dev->init_func(wps, oob_dev, registrar);
455 if (oob_priv == NULL) {
456 wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
461 if (oob_method == OOB_METHOD_CRED)
462 data = wps_get_oob_cred(wps);
464 data = wps_get_oob_dev_pwd(wps);
467 if (data == NULL || oob_dev->write_func(oob_priv, data) < 0)
470 data = oob_dev->read_func(oob_priv);
474 if (oob_method == OOB_METHOD_CRED)
475 ret = wps_parse_oob_cred(wps, data);
477 ret = wps_parse_oob_dev_pwd(wps, data);
481 oob_dev->deinit_func(oob_priv);
484 wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
492 struct oob_device_data * wps_get_oob_device(char *device_type)
494 #ifdef CONFIG_WPS_UFD
495 if (os_strstr(device_type, "ufd") != NULL)
496 return &oob_ufd_device_data;
497 #endif /* CONFIG_WPS_UFD */
498 #ifdef CONFIG_WPS_NFC
499 if (os_strstr(device_type, "nfc") != NULL)
500 return &oob_nfc_device_data;
501 #endif /* CONFIG_WPS_NFC */
507 #ifdef CONFIG_WPS_NFC
508 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name)
510 if (device_name == NULL)
512 #ifdef CONFIG_WPS_NFC_PN531
513 if (os_strstr(device_name, "pn531") != NULL)
514 return &oob_nfc_pn531_device_data;
515 #endif /* CONFIG_WPS_NFC_PN531 */
519 #endif /* CONFIG_WPS_NFC */
522 int wps_get_oob_method(char *method)
524 if (os_strstr(method, "pin-e") != NULL)
525 return OOB_METHOD_DEV_PWD_E;
526 if (os_strstr(method, "pin-r") != NULL)
527 return OOB_METHOD_DEV_PWD_R;
528 if (os_strstr(method, "cred") != NULL)
529 return OOB_METHOD_CRED;
530 return OOB_METHOD_UNKNOWN;
533 #endif /* CONFIG_WPS_OOB */
536 int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
540 /* <categ>-<OUI>-<subcateg> */
541 WPA_PUT_BE16(dev_type, atoi(str));
542 pos = os_strchr(str, '-');
546 if (hexstr2bin(pos, &dev_type[2], 4))
548 pos = os_strchr(pos, '-');
552 WPA_PUT_BE16(&dev_type[6], atoi(pos));
559 char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
564 ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
565 WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
566 WPA_GET_BE16(&dev_type[6]));
567 if (ret < 0 || (unsigned int) ret >= buf_len)
574 void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
578 u8 hash[SHA1_MAC_LEN];
580 0x52, 0x64, 0x80, 0xf8,
584 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
588 len[0] = sizeof(nsid);
591 sha1_vector(2, addr, len, hash);
592 os_memcpy(uuid, hash, 16);
594 /* Version: 5 = named-based version using SHA-1 */
595 uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
597 /* Variant specified in RFC 4122 */
598 uuid[8] = 0x80 | (uuid[8] & 0x3f);
602 u16 wps_config_methods_str2bin(const char *str)
607 /* Default to enabling methods based on build configuration */
608 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
610 methods |= WPS_CONFIG_VIRT_DISPLAY;
611 #endif /* CONFIG_WPS2 */
612 #ifdef CONFIG_WPS_UFD
613 methods |= WPS_CONFIG_USBA;
614 #endif /* CONFIG_WPS_UFD */
615 #ifdef CONFIG_WPS_NFC
616 methods |= WPS_CONFIG_NFC_INTERFACE;
617 #endif /* CONFIG_WPS_NFC */
619 if (os_strstr(str, "usba"))
620 methods |= WPS_CONFIG_USBA;
621 if (os_strstr(str, "ethernet"))
622 methods |= WPS_CONFIG_ETHERNET;
623 if (os_strstr(str, "label"))
624 methods |= WPS_CONFIG_LABEL;
625 if (os_strstr(str, "display"))
626 methods |= WPS_CONFIG_DISPLAY;
627 if (os_strstr(str, "ext_nfc_token"))
628 methods |= WPS_CONFIG_EXT_NFC_TOKEN;
629 if (os_strstr(str, "int_nfc_token"))
630 methods |= WPS_CONFIG_INT_NFC_TOKEN;
631 if (os_strstr(str, "nfc_interface"))
632 methods |= WPS_CONFIG_NFC_INTERFACE;
633 if (os_strstr(str, "push_button"))
634 methods |= WPS_CONFIG_PUSHBUTTON;
635 if (os_strstr(str, "keypad"))
636 methods |= WPS_CONFIG_KEYPAD;
638 if (os_strstr(str, "virtual_display"))
639 methods |= WPS_CONFIG_VIRT_DISPLAY;
640 if (os_strstr(str, "physical_display"))
641 methods |= WPS_CONFIG_PHY_DISPLAY;
642 if (os_strstr(str, "virtual_push_button"))
643 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
644 if (os_strstr(str, "physical_push_button"))
645 methods |= WPS_CONFIG_PHY_PUSHBUTTON;
646 #endif /* CONFIG_WPS2 */
653 struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
657 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
659 msg = wpabuf_alloc(1000);
663 if (wps_build_version(msg) ||
664 wps_build_msg_type(msg, WPS_WSC_ACK) ||
665 wps_build_enrollee_nonce(wps, msg) ||
666 wps_build_registrar_nonce(wps, msg) ||
667 wps_build_wfa_ext(msg, 0, NULL, 0)) {
676 struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
680 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
682 msg = wpabuf_alloc(1000);
686 if (wps_build_version(msg) ||
687 wps_build_msg_type(msg, WPS_WSC_NACK) ||
688 wps_build_enrollee_nonce(wps, msg) ||
689 wps_build_registrar_nonce(wps, msg) ||
690 wps_build_config_error(msg, wps->config_error) ||
691 wps_build_wfa_ext(msg, 0, NULL, 0)) {