wps_supplicant.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "ieee802_11_defs.h"
  17. #include "wpa_common.h"
  18. #include "config.h"
  19. #include "eap_peer/eap.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "wps/wps.h"
  22. #include "wps/wps_defs.h"
  23. #include "wps_supplicant.h"
  24. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  25. {
  26. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  27. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  28. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  29. "try to associate with the received credential");
  30. wpa_supplicant_deauthenticate(wpa_s,
  31. WLAN_REASON_DEAUTH_LEAVING);
  32. wpa_s->reassociate = 1;
  33. wpa_supplicant_req_scan(wpa_s, 0, 0);
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. static int wpa_supplicant_wps_cred(void *ctx,
  39. const struct wps_credential *cred)
  40. {
  41. struct wpa_supplicant *wpa_s = ctx;
  42. struct wpa_ssid *ssid = wpa_s->current_ssid;
  43. wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
  44. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  45. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  46. "on the received credential");
  47. os_free(ssid->eap.identity);
  48. ssid->eap.identity = NULL;
  49. ssid->eap.identity_len = 0;
  50. os_free(ssid->eap.phase1);
  51. ssid->eap.phase1 = NULL;
  52. os_free(ssid->eap.eap_methods);
  53. ssid->eap.eap_methods = NULL;
  54. } else {
  55. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  56. "received credential");
  57. ssid = wpa_config_add_network(wpa_s->conf);
  58. if (ssid == NULL)
  59. return -1;
  60. }
  61. wpa_config_set_network_defaults(ssid);
  62. os_free(ssid->ssid);
  63. ssid->ssid = os_malloc(cred->ssid_len);
  64. if (ssid->ssid) {
  65. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  66. ssid->ssid_len = cred->ssid_len;
  67. }
  68. switch (cred->encr_type) {
  69. case WPS_ENCR_NONE:
  70. ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
  71. break;
  72. case WPS_ENCR_WEP:
  73. ssid->pairwise_cipher = ssid->group_cipher =
  74. WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
  75. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  76. cred->key_idx < NUM_WEP_KEYS) {
  77. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  78. cred->key_len);
  79. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  80. ssid->wep_tx_keyidx = cred->key_idx;
  81. }
  82. break;
  83. case WPS_ENCR_TKIP:
  84. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  85. ssid->group_cipher = WPA_CIPHER_TKIP;
  86. break;
  87. case WPS_ENCR_AES:
  88. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  89. ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
  90. break;
  91. }
  92. switch (cred->auth_type) {
  93. case WPS_AUTH_OPEN:
  94. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  95. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  96. ssid->proto = 0;
  97. break;
  98. case WPS_AUTH_SHARED:
  99. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  100. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  101. ssid->proto = 0;
  102. break;
  103. case WPS_AUTH_WPAPSK:
  104. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  105. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  106. ssid->proto = WPA_PROTO_WPA;
  107. break;
  108. case WPS_AUTH_WPA:
  109. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  110. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  111. ssid->proto = WPA_PROTO_WPA;
  112. break;
  113. case WPS_AUTH_WPA2:
  114. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  115. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  116. ssid->proto = WPA_PROTO_RSN;
  117. break;
  118. case WPS_AUTH_WPA2PSK:
  119. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  120. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  121. ssid->proto = WPA_PROTO_RSN;
  122. break;
  123. }
  124. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  125. if (cred->key_len == 2 * PMK_LEN) {
  126. if (hexstr2bin((const char *) cred->key, ssid->psk,
  127. PMK_LEN)) {
  128. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  129. "Key");
  130. return -1;
  131. }
  132. ssid->psk_set = 1;
  133. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  134. os_free(ssid->passphrase);
  135. ssid->passphrase = os_malloc(cred->key_len + 1);
  136. if (ssid->passphrase == NULL)
  137. return -1;
  138. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  139. ssid->passphrase[cred->key_len] = '\0';
  140. wpa_config_update_psk(ssid);
  141. } else {
  142. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  143. "length %lu",
  144. (unsigned long) cred->key_len);
  145. return -1;
  146. }
  147. }
  148. #ifndef CONFIG_NO_CONFIG_WRITE
  149. if (wpa_s->conf->update_config &&
  150. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  151. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  152. return -1;
  153. }
  154. #endif /* CONFIG_NO_CONFIG_WRITE */
  155. return 0;
  156. }
  157. u8 wpas_wps_get_req_type(struct wpa_ssid *ssid)
  158. {
  159. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  160. eap_is_wps_pin_enrollee(&ssid->eap))
  161. return WPS_REQ_ENROLLEE;
  162. else
  163. return WPS_REQ_REGISTRAR;
  164. }
  165. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  166. {
  167. struct wps_context *wps;
  168. wps = os_zalloc(sizeof(*wps));
  169. if (wps == NULL)
  170. return -1;
  171. wps->cred_cb = wpa_supplicant_wps_cred;
  172. wps->cb_ctx = wpa_s;
  173. /* TODO: make the device data configurable */
  174. wps->dev.device_name = "dev name";
  175. wps->dev.manufacturer = "manuf";
  176. wps->dev.model_name = "model name";
  177. wps->dev.model_number = "model number";
  178. wps->dev.serial_number = "12345";
  179. wps->dev.categ = WPS_DEV_COMPUTER;
  180. wps->dev.oui = WPS_DEV_OUI_WFA;
  181. wps->dev.sub_categ = WPS_DEV_COMPUTER_PC;
  182. wps->dev.os_version = 0;
  183. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
  184. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  185. os_memcpy(wps->uuid, wpa_s->conf->uuid, 16);
  186. wpa_s->wps = wps;
  187. return 0;
  188. }
  189. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  190. {
  191. if (wpa_s->wps == NULL)
  192. return;
  193. os_free(wpa_s->wps->network_key);
  194. os_free(wpa_s->wps);
  195. wpa_s->wps = NULL;
  196. }