LCOV - code coverage report
Current view: top level - src/wps - wps_common.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 387 468 82.7 %
Date: 2014-05-28 Functions: 33 33 100.0 %

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

Generated by: LCOV version 1.10