wps_supplicant.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, struct wps_credential *cred)
  38. {
  39. struct wpa_supplicant *wpa_s = ctx;
  40. struct wpa_ssid *ssid = wpa_s->current_ssid;
  41. wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
  42. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  43. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  44. "on the received credential");
  45. os_free(ssid->eap.identity);
  46. ssid->eap.identity = NULL;
  47. ssid->eap.identity_len = 0;
  48. os_free(ssid->eap.phase1);
  49. ssid->eap.phase1 = NULL;
  50. os_free(ssid->eap.eap_methods);
  51. ssid->eap.eap_methods = NULL;
  52. } else {
  53. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  54. "received credential");
  55. ssid = wpa_config_add_network(wpa_s->conf);
  56. if (ssid == NULL)
  57. return -1;
  58. }
  59. wpa_config_set_network_defaults(ssid);
  60. os_free(ssid->ssid);
  61. ssid->ssid = os_malloc(cred->ssid_len);
  62. if (ssid->ssid) {
  63. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  64. ssid->ssid_len = cred->ssid_len;
  65. }
  66. switch (cred->encr_type) {
  67. case WPS_ENCR_NONE:
  68. ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
  69. break;
  70. case WPS_ENCR_WEP:
  71. ssid->pairwise_cipher = ssid->group_cipher =
  72. WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
  73. if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
  74. cred->key_idx < NUM_WEP_KEYS) {
  75. os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
  76. cred->key_len);
  77. ssid->wep_key_len[cred->key_idx] = cred->key_len;
  78. ssid->wep_tx_keyidx = cred->key_idx;
  79. }
  80. break;
  81. case WPS_ENCR_TKIP:
  82. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  83. ssid->group_cipher = WPA_CIPHER_TKIP;
  84. break;
  85. case WPS_ENCR_AES:
  86. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  87. ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
  88. break;
  89. }
  90. switch (cred->auth_type) {
  91. case WPS_AUTH_OPEN:
  92. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  93. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  94. ssid->proto = 0;
  95. break;
  96. case WPS_AUTH_SHARED:
  97. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  98. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  99. ssid->proto = 0;
  100. break;
  101. case WPS_AUTH_WPAPSK:
  102. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  103. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  104. ssid->proto = WPA_PROTO_WPA;
  105. break;
  106. case WPS_AUTH_WPA:
  107. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  108. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  109. ssid->proto = WPA_PROTO_WPA;
  110. break;
  111. case WPS_AUTH_WPA2:
  112. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  113. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  114. ssid->proto = WPA_PROTO_RSN;
  115. break;
  116. case WPS_AUTH_WPA2PSK:
  117. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  118. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  119. ssid->proto = WPA_PROTO_RSN;
  120. break;
  121. }
  122. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  123. if (cred->key_len == 2 * PMK_LEN) {
  124. if (hexstr2bin((const char *) cred->key, ssid->psk,
  125. PMK_LEN)) {
  126. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  127. "Key");
  128. return -1;
  129. }
  130. ssid->psk_set = 1;
  131. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  132. os_free(ssid->passphrase);
  133. ssid->passphrase = os_malloc(cred->key_len + 1);
  134. if (ssid->passphrase == NULL)
  135. return -1;
  136. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  137. ssid->passphrase[cred->key_len] = '\0';
  138. wpa_config_update_psk(ssid);
  139. } else {
  140. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  141. "length %lu",
  142. (unsigned long) cred->key_len);
  143. return -1;
  144. }
  145. }
  146. #ifndef CONFIG_NO_CONFIG_WRITE
  147. if (wpa_s->conf->update_config &&
  148. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  149. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  150. return -1;
  151. }
  152. #endif /* CONFIG_NO_CONFIG_WRITE */
  153. return 0;
  154. }
  155. void * wpas_wps_get_cred_cb(void)
  156. {
  157. return wpa_supplicant_wps_cred;
  158. }