wps_supplicant.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "wpa_supplicant_i.h"
  20. #include "wps/wps.h"
  21. #include "wps/wps_defs.h"
  22. #include "wps_supplicant.h"
  23. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  24. {
  25. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  26. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  27. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  28. "try to associate with the received credential");
  29. wpa_supplicant_deauthenticate(wpa_s,
  30. WLAN_REASON_DEAUTH_LEAVING);
  31. wpa_s->reassociate = 1;
  32. wpa_supplicant_req_scan(wpa_s, 0, 0);
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. static int wpa_supplicant_wps_cred(void *ctx,
  38. const struct wps_credential *cred)
  39. {
  40. struct wpa_supplicant *wpa_s = ctx;
  41. struct wpa_ssid *ssid = wpa_s->current_ssid;
  42. wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
  43. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  44. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  45. "on the received credential");
  46. os_free(ssid->eap.identity);
  47. ssid->eap.identity = NULL;
  48. ssid->eap.identity_len = 0;
  49. os_free(ssid->eap.phase1);
  50. ssid->eap.phase1 = NULL;
  51. os_free(ssid->eap.eap_methods);
  52. ssid->eap.eap_methods = NULL;
  53. } else {
  54. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  55. "received credential");
  56. ssid = wpa_config_add_network(wpa_s->conf);
  57. if (ssid == NULL)
  58. return -1;
  59. }
  60. wpa_config_set_network_defaults(ssid);
  61. os_free(ssid->ssid);
  62. ssid->ssid = os_malloc(cred->ssid_len);
  63. if (ssid->ssid) {
  64. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  65. ssid->ssid_len = cred->ssid_len;
  66. }
  67. switch (cred->encr_type) {
  68. case WPS_ENCR_NONE:
  69. ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
  70. break;
  71. case WPS_ENCR_WEP:
  72. ssid->pairwise_cipher = ssid->group_cipher =
  73. WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
  74. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  75. cred->key_idx < NUM_WEP_KEYS) {
  76. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  77. cred->key_len);
  78. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  79. ssid->wep_tx_keyidx = cred->key_idx;
  80. }
  81. break;
  82. case WPS_ENCR_TKIP:
  83. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  84. ssid->group_cipher = WPA_CIPHER_TKIP;
  85. break;
  86. case WPS_ENCR_AES:
  87. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  88. ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
  89. break;
  90. }
  91. switch (cred->auth_type) {
  92. case WPS_AUTH_OPEN:
  93. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  94. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  95. ssid->proto = 0;
  96. break;
  97. case WPS_AUTH_SHARED:
  98. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  99. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  100. ssid->proto = 0;
  101. break;
  102. case WPS_AUTH_WPAPSK:
  103. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  104. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  105. ssid->proto = WPA_PROTO_WPA;
  106. break;
  107. case WPS_AUTH_WPA:
  108. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  109. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  110. ssid->proto = WPA_PROTO_WPA;
  111. break;
  112. case WPS_AUTH_WPA2:
  113. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  114. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  115. ssid->proto = WPA_PROTO_RSN;
  116. break;
  117. case WPS_AUTH_WPA2PSK:
  118. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  119. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  120. ssid->proto = WPA_PROTO_RSN;
  121. break;
  122. }
  123. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  124. if (cred->key_len == 2 * PMK_LEN) {
  125. if (hexstr2bin((const char *) cred->key, ssid->psk,
  126. PMK_LEN)) {
  127. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  128. "Key");
  129. return -1;
  130. }
  131. ssid->psk_set = 1;
  132. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  133. os_free(ssid->passphrase);
  134. ssid->passphrase = os_malloc(cred->key_len + 1);
  135. if (ssid->passphrase == NULL)
  136. return -1;
  137. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  138. ssid->passphrase[cred->key_len] = '\0';
  139. wpa_config_update_psk(ssid);
  140. } else {
  141. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  142. "length %lu",
  143. (unsigned long) cred->key_len);
  144. return -1;
  145. }
  146. }
  147. #ifndef CONFIG_NO_CONFIG_WRITE
  148. if (wpa_s->conf->update_config &&
  149. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  150. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  151. return -1;
  152. }
  153. #endif /* CONFIG_NO_CONFIG_WRITE */
  154. return 0;
  155. }
  156. void * wpas_wps_get_cred_cb(void)
  157. {
  158. return wpa_supplicant_wps_cred;
  159. }
  160. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  161. {
  162. struct wps_context *wps;
  163. wps = os_zalloc(sizeof(*wps));
  164. if (wps == NULL)
  165. return -1;
  166. wps->cred_cb = wpa_supplicant_wps_cred;
  167. wps->cb_ctx = wpa_s;
  168. /* TODO: make the device data configurable */
  169. wps->dev.device_name = "dev name";
  170. wps->dev.manufacturer = "manuf";
  171. wps->dev.model_name = "model name";
  172. wps->dev.model_number = "model number";
  173. wps->dev.serial_number = "12345";
  174. wps->dev.categ = WPS_DEV_COMPUTER;
  175. wps->dev.oui = WPS_DEV_OUI_WFA;
  176. wps->dev.sub_categ = WPS_DEV_COMPUTER_PC;
  177. wpa_s->wps = wps;
  178. return 0;
  179. }
  180. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  181. {
  182. if (wpa_s->wps == NULL)
  183. return;
  184. os_free(wpa_s->wps->network_key);
  185. os_free(wpa_s->wps);
  186. wpa_s->wps = NULL;
  187. }