00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "dh_group5.h"
00020 #include "sha256.h"
00021 #include "aes_wrap.h"
00022 #include "crypto.h"
00023 #include "wps_i.h"
00024 #include "wps_dev_attr.h"
00025
00026
00027 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
00028 const char *label, u8 *res, size_t res_len)
00029 {
00030 u8 i_buf[4], key_bits[4];
00031 const u8 *addr[4];
00032 size_t len[4];
00033 int i, iter;
00034 u8 hash[SHA256_MAC_LEN], *opos;
00035 size_t left;
00036
00037 WPA_PUT_BE32(key_bits, res_len * 8);
00038
00039 addr[0] = i_buf;
00040 len[0] = sizeof(i_buf);
00041 addr[1] = label_prefix;
00042 len[1] = label_prefix_len;
00043 addr[2] = (const u8 *) label;
00044 len[2] = os_strlen(label);
00045 addr[3] = key_bits;
00046 len[3] = sizeof(key_bits);
00047
00048 iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
00049 opos = res;
00050 left = res_len;
00051
00052 for (i = 1; i <= iter; i++) {
00053 WPA_PUT_BE32(i_buf, i);
00054 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
00055 if (i < iter) {
00056 os_memcpy(opos, hash, SHA256_MAC_LEN);
00057 opos += SHA256_MAC_LEN;
00058 left -= SHA256_MAC_LEN;
00059 } else
00060 os_memcpy(opos, hash, left);
00061 }
00062 }
00063
00064
00065 int wps_derive_keys(struct wps_data *wps)
00066 {
00067 struct wpabuf *pubkey, *dh_shared;
00068 u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
00069 const u8 *addr[3];
00070 size_t len[3];
00071 u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
00072
00073 if (wps->dh_privkey == NULL) {
00074 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
00075 return -1;
00076 }
00077
00078 pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
00079 if (pubkey == NULL) {
00080 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
00081 return -1;
00082 }
00083
00084 dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
00085 dh5_free(wps->dh_ctx);
00086 wps->dh_ctx = NULL;
00087 dh_shared = wpabuf_zeropad(dh_shared, 192);
00088 if (dh_shared == NULL) {
00089 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
00090 return -1;
00091 }
00092
00093
00094 wpabuf_free(wps->dh_privkey);
00095 wps->dh_privkey = NULL;
00096
00097 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
00098
00099
00100 addr[0] = wpabuf_head(dh_shared);
00101 len[0] = wpabuf_len(dh_shared);
00102 sha256_vector(1, addr, len, dhkey);
00103 wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
00104 wpabuf_free(dh_shared);
00105
00106
00107 addr[0] = wps->nonce_e;
00108 len[0] = WPS_NONCE_LEN;
00109 addr[1] = wps->mac_addr_e;
00110 len[1] = ETH_ALEN;
00111 addr[2] = wps->nonce_r;
00112 len[2] = WPS_NONCE_LEN;
00113 hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
00114 wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
00115
00116 wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
00117 keys, sizeof(keys));
00118 os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
00119 os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
00120 os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
00121 WPS_EMSK_LEN);
00122
00123 wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
00124 wps->authkey, WPS_AUTHKEY_LEN);
00125 wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
00126 wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
00127 wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
00128
00129 return 0;
00130 }
00131
00132
00133 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
00134 size_t dev_passwd_len)
00135 {
00136 u8 hash[SHA256_MAC_LEN];
00137
00138 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
00139 (dev_passwd_len + 1) / 2, hash);
00140 os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
00141 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
00142 dev_passwd + (dev_passwd_len + 1) / 2,
00143 dev_passwd_len / 2, hash);
00144 os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
00145
00146 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
00147 dev_passwd, dev_passwd_len);
00148 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
00149 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
00150 }
00151
00152
00153 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
00154 size_t encr_len)
00155 {
00156 struct wpabuf *decrypted;
00157 const size_t block_size = 16;
00158 size_t i;
00159 u8 pad;
00160 const u8 *pos;
00161
00162
00163 if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
00164 {
00165 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
00166 return NULL;
00167 }
00168
00169 decrypted = wpabuf_alloc(encr_len - block_size);
00170 if (decrypted == NULL)
00171 return NULL;
00172
00173 wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
00174 wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
00175 if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
00176 wpabuf_len(decrypted))) {
00177 wpabuf_free(decrypted);
00178 return NULL;
00179 }
00180
00181 wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
00182 decrypted);
00183
00184 pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
00185 pad = *pos;
00186 if (pad > wpabuf_len(decrypted)) {
00187 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
00188 wpabuf_free(decrypted);
00189 return NULL;
00190 }
00191 for (i = 0; i < pad; i++) {
00192 if (*pos-- != pad) {
00193 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
00194 "string");
00195 wpabuf_free(decrypted);
00196 return NULL;
00197 }
00198 }
00199 decrypted->used -= pad;
00200
00201 return decrypted;
00202 }
00203
00204
00211 unsigned int wps_pin_checksum(unsigned int pin)
00212 {
00213 unsigned int accum = 0;
00214 while (pin) {
00215 accum += 3 * (pin % 10);
00216 pin /= 10;
00217 accum += pin % 10;
00218 pin /= 10;
00219 }
00220
00221 return (10 - accum % 10) % 10;
00222 }
00223
00224
00231 unsigned int wps_pin_valid(unsigned int pin)
00232 {
00233 return wps_pin_checksum(pin / 10) == (pin % 10);
00234 }
00235
00236
00242 unsigned int wps_generate_pin(void)
00243 {
00244 unsigned int val;
00245
00246
00247 if (os_get_random((unsigned char *) &val, sizeof(val)) < 0) {
00248 struct os_time now;
00249 os_get_time(&now);
00250 val = os_random() ^ now.sec ^ now.usec;
00251 }
00252 val %= 10000000;
00253
00254
00255 return val * 10 + wps_pin_checksum(val);
00256 }
00257
00258
00259 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg)
00260 {
00261 union wps_event_data data;
00262
00263 if (wps->event_cb == NULL)
00264 return;
00265
00266 os_memset(&data, 0, sizeof(data));
00267 data.fail.msg = msg;
00268 wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
00269 }
00270
00271
00272 void wps_success_event(struct wps_context *wps)
00273 {
00274 if (wps->event_cb == NULL)
00275 return;
00276
00277 wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
00278 }
00279
00280
00281 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
00282 {
00283 union wps_event_data data;
00284
00285 if (wps->event_cb == NULL)
00286 return;
00287
00288 os_memset(&data, 0, sizeof(data));
00289 data.pwd_auth_fail.enrollee = enrollee;
00290 data.pwd_auth_fail.part = part;
00291 wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
00292 }
00293
00294
00295 void wps_pbc_overlap_event(struct wps_context *wps)
00296 {
00297 if (wps->event_cb == NULL)
00298 return;
00299
00300 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
00301 }
00302
00303
00304 void wps_pbc_timeout_event(struct wps_context *wps)
00305 {
00306 if (wps->event_cb == NULL)
00307 return;
00308
00309 wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
00310 }
00311
00312
00313 #ifdef CONFIG_WPS_OOB
00314
00315 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
00316 {
00317 struct wps_data data;
00318 struct wpabuf *plain;
00319
00320 plain = wpabuf_alloc(500);
00321 if (plain == NULL) {
00322 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
00323 "credential");
00324 return NULL;
00325 }
00326
00327 os_memset(&data, 0, sizeof(data));
00328 data.wps = wps;
00329 data.auth_type = wps->auth_types;
00330 data.encr_type = wps->encr_types;
00331 if (wps_build_version(plain) || wps_build_cred(&data, plain)) {
00332 wpabuf_free(plain);
00333 return NULL;
00334 }
00335
00336 return plain;
00337 }
00338
00339
00340 static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
00341 {
00342 struct wpabuf *data;
00343
00344 data = wpabuf_alloc(9 + WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
00345 if (data == NULL) {
00346 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
00347 "device password attribute");
00348 return NULL;
00349 }
00350
00351 wpabuf_free(wps->oob_conf.dev_password);
00352 wps->oob_conf.dev_password =
00353 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
00354 if (wps->oob_conf.dev_password == NULL) {
00355 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
00356 "device password");
00357 wpabuf_free(data);
00358 return NULL;
00359 }
00360
00361 if (wps_build_version(data) ||
00362 wps_build_oob_dev_password(data, wps)) {
00363 wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
00364 "attribute error");
00365 wpabuf_free(data);
00366 return NULL;
00367 }
00368
00369 return data;
00370 }
00371
00372
00373 static int wps_parse_oob_dev_pwd(struct wps_context *wps,
00374 struct wpabuf *data)
00375 {
00376 struct oob_conf_data *oob_conf = &wps->oob_conf;
00377 struct wps_parse_attr attr;
00378 const u8 *pos;
00379
00380 if (wps_parse_msg(data, &attr) < 0 ||
00381 attr.oob_dev_password == NULL) {
00382 wpa_printf(MSG_ERROR, "WPS: OOB device password not found");
00383 return -1;
00384 }
00385
00386 pos = attr.oob_dev_password;
00387
00388 oob_conf->pubkey_hash =
00389 wpabuf_alloc_copy(pos, WPS_OOB_PUBKEY_HASH_LEN);
00390 if (oob_conf->pubkey_hash == NULL) {
00391 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
00392 "public key hash");
00393 return -1;
00394 }
00395 pos += WPS_OOB_PUBKEY_HASH_LEN;
00396
00397 wps->oob_dev_pw_id = WPA_GET_BE16(pos);
00398 pos += sizeof(wps->oob_dev_pw_id);
00399
00400 oob_conf->dev_password =
00401 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
00402 if (oob_conf->dev_password == NULL) {
00403 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
00404 "device password");
00405 return -1;
00406 }
00407 wpa_snprintf_hex_uppercase(wpabuf_put(oob_conf->dev_password,
00408 wpabuf_size(oob_conf->dev_password)),
00409 wpabuf_size(oob_conf->dev_password), pos,
00410 WPS_OOB_DEVICE_PASSWORD_LEN);
00411
00412 return 0;
00413 }
00414
00415
00416 static int wps_parse_oob_cred(struct wps_context *wps, struct wpabuf *data)
00417 {
00418 struct wpabuf msg;
00419 struct wps_parse_attr attr;
00420 size_t i;
00421
00422 if (wps_parse_msg(data, &attr) < 0 || attr.num_cred <= 0) {
00423 wpa_printf(MSG_ERROR, "WPS: OOB credential not found");
00424 return -1;
00425 }
00426
00427 for (i = 0; i < attr.num_cred; i++) {
00428 struct wps_credential local_cred;
00429 struct wps_parse_attr cattr;
00430
00431 os_memset(&local_cred, 0, sizeof(local_cred));
00432 wpabuf_set(&msg, attr.cred[i], attr.cred_len[i]);
00433 if (wps_parse_msg(&msg, &cattr) < 0 ||
00434 wps_process_cred(&cattr, &local_cred)) {
00435 wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
00436 "credential");
00437 return -1;
00438 }
00439 wps->cred_cb(wps->cb_ctx, &local_cred);
00440 }
00441
00442 return 0;
00443 }
00444
00445
00446 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
00447 int registrar)
00448 {
00449 struct wpabuf *data;
00450 int ret, write_f, oob_method = wps->oob_conf.oob_method;
00451 void *oob_priv;
00452
00453 write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
00454
00455 oob_priv = oob_dev->init_func(wps, oob_dev, registrar);
00456 if (oob_priv == NULL) {
00457 wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
00458 return -1;
00459 }
00460
00461 if (write_f) {
00462 if (oob_method == OOB_METHOD_CRED)
00463 data = wps_get_oob_cred(wps);
00464 else
00465 data = wps_get_oob_dev_pwd(wps);
00466
00467 ret = 0;
00468 if (data == NULL || oob_dev->write_func(oob_priv, data) < 0)
00469 ret = -1;
00470 } else {
00471 data = oob_dev->read_func(oob_priv);
00472 if (data == NULL)
00473 ret = -1;
00474 else {
00475 if (oob_method == OOB_METHOD_CRED)
00476 ret = wps_parse_oob_cred(wps, data);
00477 else
00478 ret = wps_parse_oob_dev_pwd(wps, data);
00479 }
00480 }
00481 wpabuf_free(data);
00482 oob_dev->deinit_func(oob_priv);
00483
00484 if (ret < 0) {
00485 wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
00486 return -1;
00487 }
00488
00489 return 0;
00490 }
00491
00492
00493 struct oob_device_data * wps_get_oob_device(char *device_type)
00494 {
00495 #ifdef CONFIG_WPS_UFD
00496 if (os_strstr(device_type, "ufd") != NULL)
00497 return &oob_ufd_device_data;
00498 #endif
00499 #ifdef CONFIG_WPS_NFC
00500 if (os_strstr(device_type, "nfc") != NULL)
00501 return &oob_nfc_device_data;
00502 #endif
00503
00504 return NULL;
00505 }
00506
00507
00508 #ifdef CONFIG_WPS_NFC
00509 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name)
00510 {
00511 if (device_name == NULL)
00512 return NULL;
00513 #ifdef CONFIG_WPS_NFC_PN531
00514 if (os_strstr(device_name, "pn531") != NULL)
00515 return &oob_nfc_pn531_device_data;
00516 #endif
00517
00518 return NULL;
00519 }
00520 #endif
00521
00522
00523 int wps_get_oob_method(char *method)
00524 {
00525 if (os_strstr(method, "pin-e") != NULL)
00526 return OOB_METHOD_DEV_PWD_E;
00527 if (os_strstr(method, "pin-r") != NULL)
00528 return OOB_METHOD_DEV_PWD_R;
00529 if (os_strstr(method, "cred") != NULL)
00530 return OOB_METHOD_CRED;
00531 return OOB_METHOD_UNKNOWN;
00532 }
00533
00534 #endif
00535