LCOV - code coverage report
Current view: top level - src/wps - wps_common.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1388338050 Lines: 259 344 75.3 %
Date: 2013-12-29 Functions: 22 26 84.6 %
Branches: 76 166 45.8 %

           Branch data     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 "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"
      18                 :            : #include "wps_i.h"
      19                 :            : 
      20                 :            : 
      21                 :         29 : 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)
      23                 :            : {
      24                 :            :         u8 i_buf[4], key_bits[4];
      25                 :            :         const u8 *addr[4];
      26                 :            :         size_t len[4];
      27                 :            :         int i, iter;
      28                 :            :         u8 hash[SHA256_MAC_LEN], *opos;
      29                 :            :         size_t left;
      30                 :            : 
      31                 :         29 :         WPA_PUT_BE32(key_bits, res_len * 8);
      32                 :            : 
      33                 :         29 :         addr[0] = i_buf;
      34                 :         29 :         len[0] = sizeof(i_buf);
      35                 :         29 :         addr[1] = label_prefix;
      36                 :         29 :         len[1] = label_prefix_len;
      37                 :         29 :         addr[2] = (const u8 *) label;
      38                 :         29 :         len[2] = os_strlen(label);
      39                 :         29 :         addr[3] = key_bits;
      40                 :         29 :         len[3] = sizeof(key_bits);
      41                 :            : 
      42                 :         29 :         iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
      43                 :         29 :         opos = res;
      44                 :         29 :         left = res_len;
      45                 :            : 
      46         [ +  + ]:        116 :         for (i = 1; i <= iter; i++) {
      47                 :         87 :                 WPA_PUT_BE32(i_buf, i);
      48                 :         87 :                 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
      49         [ +  + ]:         87 :                 if (i < iter) {
      50                 :         58 :                         os_memcpy(opos, hash, SHA256_MAC_LEN);
      51                 :         58 :                         opos += SHA256_MAC_LEN;
      52                 :         58 :                         left -= SHA256_MAC_LEN;
      53                 :            :                 } else
      54                 :         29 :                         os_memcpy(opos, hash, left);
      55                 :            :         }
      56                 :         29 : }
      57                 :            : 
      58                 :            : 
      59                 :         29 : int wps_derive_keys(struct wps_data *wps)
      60                 :            : {
      61                 :            :         struct wpabuf *pubkey, *dh_shared;
      62                 :            :         u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
      63                 :            :         const u8 *addr[3];
      64                 :            :         size_t len[3];
      65                 :            :         u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
      66                 :            : 
      67         [ -  + ]:         29 :         if (wps->dh_privkey == NULL) {
      68                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
      69                 :          0 :                 return -1;
      70                 :            :         }
      71                 :            : 
      72         [ +  + ]:         29 :         pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
      73         [ -  + ]:         29 :         if (pubkey == NULL) {
      74                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
      75                 :          0 :                 return -1;
      76                 :            :         }
      77                 :            : 
      78                 :         29 :         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
      79                 :         29 :         wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
      80                 :         29 :         dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
      81                 :         29 :         dh5_free(wps->dh_ctx);
      82                 :         29 :         wps->dh_ctx = NULL;
      83                 :         29 :         dh_shared = wpabuf_zeropad(dh_shared, 192);
      84         [ -  + ]:         29 :         if (dh_shared == NULL) {
      85                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
      86                 :          0 :                 return -1;
      87                 :            :         }
      88                 :            : 
      89                 :            :         /* Own DH private key is not needed anymore */
      90                 :         29 :         wpabuf_free(wps->dh_privkey);
      91                 :         29 :         wps->dh_privkey = NULL;
      92                 :            : 
      93                 :         29 :         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
      94                 :            : 
      95                 :            :         /* DHKey = SHA-256(g^AB mod p) */
      96                 :         29 :         addr[0] = wpabuf_head(dh_shared);
      97                 :         29 :         len[0] = wpabuf_len(dh_shared);
      98                 :         29 :         sha256_vector(1, addr, len, dhkey);
      99                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
     100                 :         29 :         wpabuf_free(dh_shared);
     101                 :            : 
     102                 :            :         /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
     103                 :         29 :         addr[0] = wps->nonce_e;
     104                 :         29 :         len[0] = WPS_NONCE_LEN;
     105                 :         29 :         addr[1] = wps->mac_addr_e;
     106                 :         29 :         len[1] = ETH_ALEN;
     107                 :         29 :         addr[2] = wps->nonce_r;
     108                 :         29 :         len[2] = WPS_NONCE_LEN;
     109                 :         29 :         hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
     110                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
     111                 :            : 
     112                 :         29 :         wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
     113                 :            :                 keys, sizeof(keys));
     114                 :         29 :         os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
     115                 :         29 :         os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
     116                 :         29 :         os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
     117                 :            :                   WPS_EMSK_LEN);
     118                 :            : 
     119                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
     120                 :         29 :                         wps->authkey, WPS_AUTHKEY_LEN);
     121                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
     122                 :         29 :                         wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
     123                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
     124                 :            : 
     125                 :         29 :         return 0;
     126                 :            : }
     127                 :            : 
     128                 :            : 
     129                 :         29 : void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
     130                 :            :                     size_t dev_passwd_len)
     131                 :            : {
     132                 :            :         u8 hash[SHA256_MAC_LEN];
     133                 :            : 
     134                 :         29 :         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
     135                 :         29 :                     (dev_passwd_len + 1) / 2, hash);
     136                 :         29 :         os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
     137                 :         29 :         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
     138                 :         29 :                     dev_passwd + (dev_passwd_len + 1) / 2,
     139                 :            :                     dev_passwd_len / 2, hash);
     140                 :         29 :         os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
     141                 :            : 
     142                 :         29 :         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
     143                 :            :                               dev_passwd, dev_passwd_len);
     144                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
     145                 :         29 :         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
     146                 :         29 : }
     147                 :            : 
     148                 :            : 
     149                 :         58 : struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
     150                 :            :                                           size_t encr_len)
     151                 :            : {
     152                 :            :         struct wpabuf *decrypted;
     153                 :         58 :         const size_t block_size = 16;
     154                 :            :         size_t i;
     155                 :            :         u8 pad;
     156                 :            :         const u8 *pos;
     157                 :            : 
     158                 :            :         /* AES-128-CBC */
     159 [ +  - ][ +  - ]:         58 :         if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
                 [ -  + ]
     160                 :            :         {
     161                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
     162                 :          0 :                 return NULL;
     163                 :            :         }
     164                 :            : 
     165                 :         58 :         decrypted = wpabuf_alloc(encr_len - block_size);
     166         [ -  + ]:         58 :         if (decrypted == NULL)
     167                 :          0 :                 return NULL;
     168                 :            : 
     169                 :         58 :         wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
     170                 :         58 :         wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
     171         [ -  + ]:         58 :         if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
     172                 :            :                                 wpabuf_len(decrypted))) {
     173                 :          0 :                 wpabuf_free(decrypted);
     174                 :          0 :                 return NULL;
     175                 :            :         }
     176                 :            : 
     177                 :         58 :         wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
     178                 :            :                             decrypted);
     179                 :            : 
     180                 :         58 :         pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
     181                 :         58 :         pad = *pos;
     182         [ -  + ]:         58 :         if (pad > wpabuf_len(decrypted)) {
     183                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
     184                 :          0 :                 wpabuf_free(decrypted);
     185                 :          0 :                 return NULL;
     186                 :            :         }
     187         [ +  + ]:        938 :         for (i = 0; i < pad; i++) {
     188         [ -  + ]:        880 :                 if (*pos-- != pad) {
     189                 :          0 :                         wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
     190                 :            :                                    "string");
     191                 :          0 :                         wpabuf_free(decrypted);
     192                 :          0 :                         return NULL;
     193                 :            :                 }
     194                 :            :         }
     195                 :         58 :         decrypted->used -= pad;
     196                 :            : 
     197                 :         58 :         return decrypted;
     198                 :            : }
     199                 :            : 
     200                 :            : 
     201                 :            : /**
     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
     205                 :            :  */
     206                 :          1 : unsigned int wps_pin_checksum(unsigned int pin)
     207                 :            : {
     208                 :          1 :         unsigned int accum = 0;
     209         [ +  + ]:          4 :         while (pin) {
     210                 :          3 :                 accum += 3 * (pin % 10);
     211                 :          3 :                 pin /= 10;
     212                 :          3 :                 accum += pin % 10;
     213                 :          3 :                 pin /= 10;
     214                 :            :         }
     215                 :            : 
     216                 :          1 :         return (10 - accum % 10) % 10;
     217                 :            : }
     218                 :            : 
     219                 :            : 
     220                 :            : /**
     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
     224                 :            :  */
     225                 :          0 : unsigned int wps_pin_valid(unsigned int pin)
     226                 :            : {
     227                 :          0 :         return wps_pin_checksum(pin / 10) == (pin % 10);
     228                 :            : }
     229                 :            : 
     230                 :            : 
     231                 :            : /**
     232                 :            :  * wps_generate_pin - Generate a random PIN
     233                 :            :  * Returns: Eight digit PIN (i.e., including the checksum digit)
     234                 :            :  */
     235                 :          1 : unsigned int wps_generate_pin(void)
     236                 :            : {
     237                 :            :         unsigned int val;
     238                 :            : 
     239                 :            :         /* Generate seven random digits for the PIN */
     240         [ -  + ]:          1 :         if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) {
     241                 :            :                 struct os_time now;
     242                 :          0 :                 os_get_time(&now);
     243                 :          0 :                 val = os_random() ^ now.sec ^ now.usec;
     244                 :            :         }
     245                 :          1 :         val %= 10000000;
     246                 :            : 
     247                 :            :         /* Append checksum digit */
     248                 :          1 :         return val * 10 + wps_pin_checksum(val);
     249                 :            : }
     250                 :            : 
     251                 :            : 
     252                 :          0 : int wps_pin_str_valid(const char *pin)
     253                 :            : {
     254                 :            :         const char *p;
     255                 :            :         size_t len;
     256                 :            : 
     257                 :          0 :         p = pin;
     258 [ #  # ][ #  # ]:          0 :         while (*p >= '0' && *p <= '9')
     259                 :          0 :                 p++;
     260         [ #  # ]:          0 :         if (*p != '\0')
     261                 :          0 :                 return 0;
     262                 :            : 
     263                 :          0 :         len = p - pin;
     264 [ #  # ][ #  # ]:          0 :         return len == 4 || len == 8;
     265                 :            : }
     266                 :            : 
     267                 :            : 
     268                 :         11 : void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
     269                 :            :                     u16 config_error, u16 error_indication, const u8 *mac_addr)
     270                 :            : {
     271                 :            :         union wps_event_data data;
     272                 :            : 
     273         [ -  + ]:         11 :         if (wps->event_cb == NULL)
     274                 :         11 :                 return;
     275                 :            : 
     276                 :         11 :         os_memset(&data, 0, sizeof(data));
     277                 :         11 :         data.fail.msg = msg;
     278                 :         11 :         data.fail.config_error = config_error;
     279                 :         11 :         data.fail.error_indication = error_indication;
     280                 :         11 :         os_memcpy(data.fail.peer_macaddr, mac_addr, ETH_ALEN);
     281                 :         11 :         wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
     282                 :            : }
     283                 :            : 
     284                 :            : 
     285                 :         19 : void wps_success_event(struct wps_context *wps, const u8 *mac_addr)
     286                 :            : {
     287                 :            :         union wps_event_data data;
     288                 :            : 
     289         [ -  + ]:         19 :         if (wps->event_cb == NULL)
     290                 :         19 :                 return;
     291                 :            : 
     292                 :         19 :         os_memset(&data, 0, sizeof(data));
     293                 :         19 :         os_memcpy(data.success.peer_macaddr, mac_addr, ETH_ALEN);
     294                 :         19 :         wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, &data);
     295                 :            : }
     296                 :            : 
     297                 :            : 
     298                 :          3 : void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part,
     299                 :            :                              const u8 *mac_addr)
     300                 :            : {
     301                 :            :         union wps_event_data data;
     302                 :            : 
     303         [ -  + ]:          3 :         if (wps->event_cb == NULL)
     304                 :          3 :                 return;
     305                 :            : 
     306                 :          3 :         os_memset(&data, 0, sizeof(data));
     307                 :          3 :         data.pwd_auth_fail.enrollee = enrollee;
     308                 :          3 :         data.pwd_auth_fail.part = part;
     309                 :          3 :         os_memcpy(data.pwd_auth_fail.peer_macaddr, mac_addr, ETH_ALEN);
     310                 :          3 :         wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
     311                 :            : }
     312                 :            : 
     313                 :            : 
     314                 :          3 : void wps_pbc_overlap_event(struct wps_context *wps)
     315                 :            : {
     316         [ -  + ]:          3 :         if (wps->event_cb == NULL)
     317                 :          3 :                 return;
     318                 :            : 
     319                 :          3 :         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
     320                 :            : }
     321                 :            : 
     322                 :            : 
     323                 :          1 : void wps_pbc_timeout_event(struct wps_context *wps)
     324                 :            : {
     325         [ -  + ]:          1 :         if (wps->event_cb == NULL)
     326                 :          1 :                 return;
     327                 :            : 
     328                 :          1 :         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
     329                 :            : }
     330                 :            : 
     331                 :            : 
     332                 :         13 : void wps_pbc_active_event(struct wps_context *wps)
     333                 :            : {
     334         [ -  + ]:         13 :         if (wps->event_cb == NULL)
     335                 :         13 :                 return;
     336                 :            : 
     337                 :         13 :         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL);
     338                 :            : }
     339                 :            : 
     340                 :            : 
     341                 :          9 : void wps_pbc_disable_event(struct wps_context *wps)
     342                 :            : {
     343         [ -  + ]:          9 :         if (wps->event_cb == NULL)
     344                 :          9 :                 return;
     345                 :            : 
     346                 :          9 :         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL);
     347                 :            : }
     348                 :            : 
     349                 :            : 
     350                 :            : #ifdef CONFIG_WPS_OOB
     351                 :            : 
     352                 :          3 : struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
     353                 :            : {
     354                 :            :         struct wps_data data;
     355                 :            :         struct wpabuf *plain;
     356                 :            : 
     357                 :          3 :         plain = wpabuf_alloc(500);
     358         [ -  + ]:          3 :         if (plain == NULL) {
     359                 :          0 :                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
     360                 :            :                            "credential");
     361                 :          0 :                 return NULL;
     362                 :            :         }
     363                 :            : 
     364                 :          3 :         os_memset(&data, 0, sizeof(data));
     365                 :          3 :         data.wps = wps;
     366                 :          3 :         data.auth_type = wps->auth_types;
     367                 :          3 :         data.encr_type = wps->encr_types;
     368   [ +  -  +  - ]:          6 :         if (wps_build_version(plain) ||
     369         [ -  + ]:          6 :             wps_build_cred(&data, plain) ||
     370                 :          3 :             wps_build_wfa_ext(plain, 0, NULL, 0)) {
     371                 :          0 :                 os_free(data.new_psk);
     372                 :          0 :                 wpabuf_free(plain);
     373                 :          0 :                 return NULL;
     374                 :            :         }
     375                 :            : 
     376 [ +  + ][ +  - ]:          3 :         if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk &&
                 [ +  - ]
     377                 :          1 :             wps->ap) {
     378                 :            :                 struct wps_credential cred;
     379                 :            : 
     380                 :          1 :                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
     381                 :            :                            "on credential token generation");
     382                 :            : 
     383                 :          1 :                 os_memset(&cred, 0, sizeof(cred));
     384                 :          1 :                 os_memcpy(cred.ssid, wps->ssid, wps->ssid_len);
     385                 :          1 :                 cred.ssid_len = wps->ssid_len;
     386                 :          1 :                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
     387                 :          1 :                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
     388                 :          1 :                 os_memcpy(cred.key, data.new_psk, data.new_psk_len);
     389                 :          1 :                 cred.key_len = data.new_psk_len;
     390                 :            : 
     391                 :          1 :                 wps->wps_state = WPS_STATE_CONFIGURED;
     392                 :          1 :                 wpa_hexdump_ascii_key(MSG_DEBUG,
     393                 :            :                                       "WPS: Generated random passphrase",
     394                 :          1 :                                       data.new_psk, data.new_psk_len);
     395         [ +  - ]:          1 :                 if (wps->cred_cb)
     396                 :          1 :                         wps->cred_cb(wps->cb_ctx, &cred);
     397                 :            :         }
     398                 :            : 
     399                 :          3 :         os_free(data.new_psk);
     400                 :            : 
     401                 :          3 :         return plain;
     402                 :            : }
     403                 :            : 
     404                 :            : 
     405                 :          1 : struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
     406                 :            :                                        const struct wpabuf *pubkey,
     407                 :            :                                        const struct wpabuf *dev_pw)
     408                 :            : {
     409                 :            :         struct wpabuf *data;
     410                 :            : 
     411                 :          1 :         data = wpabuf_alloc(200);
     412         [ -  + ]:          1 :         if (data == NULL)
     413                 :          0 :                 return NULL;
     414                 :            : 
     415   [ +  -  +  - ]:          2 :         if (wps_build_version(data) ||
     416                 :          2 :             wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
     417         [ -  + ]:          2 :                                  wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
     418                 :          1 :             wps_build_wfa_ext(data, 0, NULL, 0)) {
     419                 :          0 :                 wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password "
     420                 :            :                            "token");
     421                 :          0 :                 wpabuf_free(data);
     422                 :          0 :                 return NULL;
     423                 :            :         }
     424                 :            : 
     425                 :          1 :         return data;
     426                 :            : }
     427                 :            : 
     428                 :            : 
     429                 :          0 : int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
     430                 :            : {
     431                 :            :         struct wpabuf msg;
     432                 :            :         size_t i;
     433                 :            : 
     434         [ #  # ]:          0 :         for (i = 0; i < attr->num_cred; i++) {
     435                 :            :                 struct wps_credential local_cred;
     436                 :            :                 struct wps_parse_attr cattr;
     437                 :            : 
     438                 :          0 :                 os_memset(&local_cred, 0, sizeof(local_cred));
     439                 :          0 :                 wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
     440   [ #  #  #  # ]:          0 :                 if (wps_parse_msg(&msg, &cattr) < 0 ||
     441                 :          0 :                     wps_process_cred(&cattr, &local_cred)) {
     442                 :          0 :                         wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
     443                 :            :                                    "credential");
     444                 :          0 :                         return -1;
     445                 :            :                 }
     446                 :          0 :                 wps->cred_cb(wps->cb_ctx, &local_cred);
     447                 :            :         }
     448                 :            : 
     449                 :          0 :         return 0;
     450                 :            : }
     451                 :            : 
     452                 :            : 
     453                 :            : #endif /* CONFIG_WPS_OOB */
     454                 :            : 
     455                 :            : 
     456                 :          4 : int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
     457                 :            : {
     458                 :            :         const char *pos;
     459                 :            : 
     460                 :            :         /* <categ>-<OUI>-<subcateg> */
     461                 :          4 :         WPA_PUT_BE16(dev_type, atoi(str));
     462                 :          4 :         pos = os_strchr(str, '-');
     463         [ -  + ]:          4 :         if (pos == NULL)
     464                 :          0 :                 return -1;
     465                 :          4 :         pos++;
     466         [ -  + ]:          4 :         if (hexstr2bin(pos, &dev_type[2], 4))
     467                 :          0 :                 return -1;
     468                 :          4 :         pos = os_strchr(pos, '-');
     469         [ -  + ]:          4 :         if (pos == NULL)
     470                 :          0 :                 return -1;
     471                 :          4 :         pos++;
     472                 :          4 :         WPA_PUT_BE16(&dev_type[6], atoi(pos));
     473                 :            : 
     474                 :            : 
     475                 :          4 :         return 0;
     476                 :            : }
     477                 :            : 
     478                 :            : 
     479                 :        126 : char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
     480                 :            :                             size_t buf_len)
     481                 :            : {
     482                 :            :         int ret;
     483                 :            : 
     484                 :        126 :         ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
     485                 :        126 :                           WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
     486                 :        126 :                           WPA_GET_BE16(&dev_type[6]));
     487 [ +  - ][ -  + ]:        126 :         if (ret < 0 || (unsigned int) ret >= buf_len)
     488                 :          0 :                 return NULL;
     489                 :            : 
     490                 :        126 :         return buf;
     491                 :            : }
     492                 :            : 
     493                 :            : 
     494                 :         27 : void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
     495                 :            : {
     496                 :            :         const u8 *addr[2];
     497                 :            :         size_t len[2];
     498                 :            :         u8 hash[SHA1_MAC_LEN];
     499                 :         27 :         u8 nsid[16] = {
     500                 :            :                 0x52, 0x64, 0x80, 0xf8,
     501                 :            :                 0xc9, 0x9b,
     502                 :            :                 0x4b, 0xe5,
     503                 :            :                 0xa6, 0x55,
     504                 :            :                 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
     505                 :            :         };
     506                 :            : 
     507                 :         27 :         addr[0] = nsid;
     508                 :         27 :         len[0] = sizeof(nsid);
     509                 :         27 :         addr[1] = mac_addr;
     510                 :         27 :         len[1] = 6;
     511                 :         27 :         sha1_vector(2, addr, len, hash);
     512                 :         27 :         os_memcpy(uuid, hash, 16);
     513                 :            : 
     514                 :            :         /* Version: 5 = named-based version using SHA-1 */
     515                 :         27 :         uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
     516                 :            : 
     517                 :            :         /* Variant specified in RFC 4122 */
     518                 :         27 :         uuid[8] = 0x80 | (uuid[8] & 0x3f);
     519                 :         27 : }
     520                 :            : 
     521                 :            : 
     522                 :         33 : u16 wps_config_methods_str2bin(const char *str)
     523                 :            : {
     524                 :         33 :         u16 methods = 0;
     525                 :            : 
     526         [ +  + ]:         33 :         if (str == NULL) {
     527                 :            :                 /* Default to enabling methods based on build configuration */
     528                 :         29 :                 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
     529                 :            : #ifdef CONFIG_WPS2
     530                 :         29 :                 methods |= WPS_CONFIG_VIRT_DISPLAY;
     531                 :            : #endif /* CONFIG_WPS2 */
     532                 :            : #ifdef CONFIG_WPS_NFC
     533                 :         29 :                 methods |= WPS_CONFIG_NFC_INTERFACE;
     534                 :            : #endif /* CONFIG_WPS_NFC */
     535                 :            :         } else {
     536         [ -  + ]:          4 :                 if (os_strstr(str, "ethernet"))
     537                 :          0 :                         methods |= WPS_CONFIG_ETHERNET;
     538         [ +  - ]:          4 :                 if (os_strstr(str, "label"))
     539                 :          4 :                         methods |= WPS_CONFIG_LABEL;
     540         [ -  + ]:          4 :                 if (os_strstr(str, "display"))
     541                 :          0 :                         methods |= WPS_CONFIG_DISPLAY;
     542         [ -  + ]:          4 :                 if (os_strstr(str, "ext_nfc_token"))
     543                 :          0 :                         methods |= WPS_CONFIG_EXT_NFC_TOKEN;
     544         [ -  + ]:          4 :                 if (os_strstr(str, "int_nfc_token"))
     545                 :          0 :                         methods |= WPS_CONFIG_INT_NFC_TOKEN;
     546         [ -  + ]:          4 :                 if (os_strstr(str, "nfc_interface"))
     547                 :          0 :                         methods |= WPS_CONFIG_NFC_INTERFACE;
     548         [ +  - ]:          4 :                 if (os_strstr(str, "push_button"))
     549                 :          4 :                         methods |= WPS_CONFIG_PUSHBUTTON;
     550         [ -  + ]:          4 :                 if (os_strstr(str, "keypad"))
     551                 :          0 :                         methods |= WPS_CONFIG_KEYPAD;
     552                 :            : #ifdef CONFIG_WPS2
     553         [ -  + ]:          4 :                 if (os_strstr(str, "virtual_display"))
     554                 :          0 :                         methods |= WPS_CONFIG_VIRT_DISPLAY;
     555         [ -  + ]:          4 :                 if (os_strstr(str, "physical_display"))
     556                 :          0 :                         methods |= WPS_CONFIG_PHY_DISPLAY;
     557         [ -  + ]:          4 :                 if (os_strstr(str, "virtual_push_button"))
     558                 :          0 :                         methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
     559         [ -  + ]:          4 :                 if (os_strstr(str, "physical_push_button"))
     560                 :          0 :                         methods |= WPS_CONFIG_PHY_PUSHBUTTON;
     561                 :            : #endif /* CONFIG_WPS2 */
     562                 :            :         }
     563                 :            : 
     564                 :         33 :         return methods;
     565                 :            : }
     566                 :            : 
     567                 :            : 
     568                 :          0 : struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
     569                 :            : {
     570                 :            :         struct wpabuf *msg;
     571                 :            : 
     572                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
     573                 :            : 
     574                 :          0 :         msg = wpabuf_alloc(1000);
     575         [ #  # ]:          0 :         if (msg == NULL)
     576                 :          0 :                 return NULL;
     577                 :            : 
     578   [ #  #  #  # ]:          0 :         if (wps_build_version(msg) ||
     579         [ #  # ]:          0 :             wps_build_msg_type(msg, WPS_WSC_ACK) ||
     580         [ #  # ]:          0 :             wps_build_enrollee_nonce(wps, msg) ||
     581         [ #  # ]:          0 :             wps_build_registrar_nonce(wps, msg) ||
     582                 :          0 :             wps_build_wfa_ext(msg, 0, NULL, 0)) {
     583                 :          0 :                 wpabuf_free(msg);
     584                 :          0 :                 return NULL;
     585                 :            :         }
     586                 :            : 
     587                 :          0 :         return msg;
     588                 :            : }
     589                 :            : 
     590                 :            : 
     591                 :          6 : struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
     592                 :            : {
     593                 :            :         struct wpabuf *msg;
     594                 :            : 
     595                 :          6 :         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
     596                 :            : 
     597                 :          6 :         msg = wpabuf_alloc(1000);
     598         [ -  + ]:          6 :         if (msg == NULL)
     599                 :          0 :                 return NULL;
     600                 :            : 
     601   [ +  -  +  - ]:         12 :         if (wps_build_version(msg) ||
     602         [ +  - ]:         12 :             wps_build_msg_type(msg, WPS_WSC_NACK) ||
     603         [ +  - ]:         12 :             wps_build_enrollee_nonce(wps, msg) ||
     604         [ +  - ]:         12 :             wps_build_registrar_nonce(wps, msg) ||
     605         [ -  + ]:         12 :             wps_build_config_error(msg, wps->config_error) ||
     606                 :          6 :             wps_build_wfa_ext(msg, 0, NULL, 0)) {
     607                 :          0 :                 wpabuf_free(msg);
     608                 :          0 :                 return NULL;
     609                 :            :         }
     610                 :            : 
     611                 :          6 :         return msg;
     612                 :            : }
     613                 :            : 
     614                 :            : 
     615                 :            : #ifdef CONFIG_WPS_NFC
     616                 :            : 
     617                 :          1 : struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey,
     618                 :            :                                     struct wpabuf *dev_pw)
     619                 :            : {
     620                 :            :         struct wpabuf *ret;
     621                 :            : 
     622 [ +  - ][ -  + ]:          1 :         if (pubkey == NULL || dev_pw == NULL)
     623                 :          0 :                 return NULL;
     624                 :            : 
     625                 :          1 :         ret = wps_build_nfc_pw_token(id, pubkey, dev_pw);
     626 [ +  - ][ +  - ]:          1 :         if (ndef && ret) {
     627                 :            :                 struct wpabuf *tmp;
     628                 :          1 :                 tmp = ndef_build_wifi(ret);
     629                 :          1 :                 wpabuf_free(ret);
     630         [ -  + ]:          1 :                 if (tmp == NULL)
     631                 :          0 :                         return NULL;
     632                 :          1 :                 ret = tmp;
     633                 :            :         }
     634                 :            : 
     635                 :          1 :         return ret;
     636                 :            : }
     637                 :            : 
     638                 :            : 
     639                 :          1 : struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
     640                 :            :                                   struct wpabuf **privkey,
     641                 :            :                                   struct wpabuf **dev_pw)
     642                 :            : {
     643                 :          1 :         struct wpabuf *priv = NULL, *pub = NULL, *pw;
     644                 :            :         void *dh_ctx;
     645                 :            :         u16 val;
     646                 :            : 
     647                 :          1 :         pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
     648         [ -  + ]:          1 :         if (pw == NULL)
     649                 :          0 :                 return NULL;
     650                 :            : 
     651         [ +  - ]:          1 :         if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
     652         [ -  + ]:          1 :                              WPS_OOB_DEVICE_PASSWORD_LEN) ||
     653                 :          1 :             random_get_bytes((u8 *) &val, sizeof(val))) {
     654                 :          0 :                 wpabuf_free(pw);
     655                 :          0 :                 return NULL;
     656                 :            :         }
     657                 :            : 
     658                 :          1 :         dh_ctx = dh5_init(&priv, &pub);
     659         [ -  + ]:          1 :         if (dh_ctx == NULL) {
     660                 :          0 :                 wpabuf_free(pw);
     661                 :          0 :                 return NULL;
     662                 :            :         }
     663                 :          1 :         dh5_free(dh_ctx);
     664                 :            : 
     665                 :          1 :         *id = 0x10 + val % 0xfff0;
     666                 :          1 :         wpabuf_free(*pubkey);
     667                 :          1 :         *pubkey = pub;
     668                 :          1 :         wpabuf_free(*privkey);
     669                 :          1 :         *privkey = priv;
     670                 :          1 :         wpabuf_free(*dev_pw);
     671                 :          1 :         *dev_pw = pw;
     672                 :            : 
     673                 :          1 :         return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw);
     674                 :            : }
     675                 :            : 
     676                 :            : #endif /* CONFIG_WPS_NFC */

Generated by: LCOV version 1.9