wps_attr_process.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Wi-Fi Protected Setup - attribute processing
  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 "crypto/sha256.h"
  17. #include "wps_i.h"
  18. int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
  19. const struct wpabuf *msg)
  20. {
  21. u8 hash[SHA256_MAC_LEN];
  22. const u8 *addr[2];
  23. size_t len[2];
  24. if (authenticator == NULL) {
  25. wpa_printf(MSG_DEBUG, "WPS: No Authenticator attribute "
  26. "included");
  27. return -1;
  28. }
  29. if (wps->last_msg == NULL) {
  30. wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
  31. "validating authenticator");
  32. return -1;
  33. }
  34. /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
  35. * (M_curr* is M_curr without the Authenticator attribute)
  36. */
  37. addr[0] = wpabuf_head(wps->last_msg);
  38. len[0] = wpabuf_len(wps->last_msg);
  39. addr[1] = wpabuf_head(msg);
  40. len[1] = wpabuf_len(msg) - 4 - WPS_AUTHENTICATOR_LEN;
  41. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
  42. if (os_memcmp(hash, authenticator, WPS_AUTHENTICATOR_LEN) != 0) {
  43. wpa_printf(MSG_DEBUG, "WPS: Incorrect Authenticator");
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
  49. const u8 *key_wrap_auth)
  50. {
  51. u8 hash[SHA256_MAC_LEN];
  52. const u8 *head;
  53. size_t len;
  54. if (key_wrap_auth == NULL) {
  55. wpa_printf(MSG_DEBUG, "WPS: No KWA in decrypted attribute");
  56. return -1;
  57. }
  58. head = wpabuf_head(msg);
  59. len = wpabuf_len(msg) - 4 - WPS_KWA_LEN;
  60. if (head + len != key_wrap_auth - 4) {
  61. wpa_printf(MSG_DEBUG, "WPS: KWA not in the end of the "
  62. "decrypted attribute");
  63. return -1;
  64. }
  65. hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, head, len, hash);
  66. if (os_memcmp(hash, key_wrap_auth, WPS_KWA_LEN) != 0) {
  67. wpa_printf(MSG_DEBUG, "WPS: Invalid KWA");
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. static int wps_process_cred_network_idx(struct wps_credential *cred,
  73. const u8 *idx)
  74. {
  75. if (idx == NULL) {
  76. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  77. "Network Index");
  78. return -1;
  79. }
  80. wpa_printf(MSG_DEBUG, "WPS: Network Index: %d", *idx);
  81. return 0;
  82. }
  83. static int wps_process_cred_ssid(struct wps_credential *cred, const u8 *ssid,
  84. size_t ssid_len)
  85. {
  86. if (ssid == NULL) {
  87. wpa_printf(MSG_DEBUG, "WPS: Credential did not include SSID");
  88. return -1;
  89. }
  90. /* Remove zero-padding since some Registrar implementations seem to use
  91. * hardcoded 32-octet length for this attribute */
  92. while (ssid_len > 0 && ssid[ssid_len - 1] == 0)
  93. ssid_len--;
  94. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", ssid, ssid_len);
  95. if (ssid_len <= sizeof(cred->ssid)) {
  96. os_memcpy(cred->ssid, ssid, ssid_len);
  97. cred->ssid_len = ssid_len;
  98. }
  99. return 0;
  100. }
  101. static int wps_process_cred_auth_type(struct wps_credential *cred,
  102. const u8 *auth_type)
  103. {
  104. if (auth_type == NULL) {
  105. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  106. "Authentication Type");
  107. return -1;
  108. }
  109. cred->auth_type = WPA_GET_BE16(auth_type);
  110. wpa_printf(MSG_DEBUG, "WPS: Authentication Type: 0x%x",
  111. cred->auth_type);
  112. return 0;
  113. }
  114. static int wps_process_cred_encr_type(struct wps_credential *cred,
  115. const u8 *encr_type)
  116. {
  117. if (encr_type == NULL) {
  118. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  119. "Encryption Type");
  120. return -1;
  121. }
  122. cred->encr_type = WPA_GET_BE16(encr_type);
  123. wpa_printf(MSG_DEBUG, "WPS: Encryption Type: 0x%x",
  124. cred->encr_type);
  125. return 0;
  126. }
  127. static int wps_process_cred_network_key_idx(struct wps_credential *cred,
  128. const u8 *key_idx)
  129. {
  130. if (key_idx == NULL)
  131. return 0; /* optional attribute */
  132. wpa_printf(MSG_DEBUG, "WPS: Network Key Index: %d", *key_idx);
  133. cred->key_idx = *key_idx;
  134. return 0;
  135. }
  136. static int wps_process_cred_network_key(struct wps_credential *cred,
  137. const u8 *key, size_t key_len)
  138. {
  139. if (key == NULL) {
  140. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  141. "Network Key");
  142. if (cred->auth_type == WPS_AUTH_OPEN &&
  143. cred->encr_type == WPS_ENCR_NONE) {
  144. wpa_printf(MSG_DEBUG, "WPS: Workaround - Allow "
  145. "missing mandatory Network Key attribute "
  146. "for open network");
  147. return 0;
  148. }
  149. return -1;
  150. }
  151. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", key, key_len);
  152. if (key_len <= sizeof(cred->key)) {
  153. os_memcpy(cred->key, key, key_len);
  154. cred->key_len = key_len;
  155. }
  156. return 0;
  157. }
  158. static int wps_process_cred_mac_addr(struct wps_credential *cred,
  159. const u8 *mac_addr)
  160. {
  161. if (mac_addr == NULL) {
  162. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  163. "MAC Address");
  164. return -1;
  165. }
  166. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, MAC2STR(mac_addr));
  167. os_memcpy(cred->mac_addr, mac_addr, ETH_ALEN);
  168. return 0;
  169. }
  170. static int wps_process_cred_eap_type(struct wps_credential *cred,
  171. const u8 *eap_type, size_t eap_type_len)
  172. {
  173. if (eap_type == NULL)
  174. return 0; /* optional attribute */
  175. wpa_hexdump(MSG_DEBUG, "WPS: EAP Type", eap_type, eap_type_len);
  176. return 0;
  177. }
  178. static int wps_process_cred_eap_identity(struct wps_credential *cred,
  179. const u8 *identity,
  180. size_t identity_len)
  181. {
  182. if (identity == NULL)
  183. return 0; /* optional attribute */
  184. wpa_hexdump_ascii(MSG_DEBUG, "WPS: EAP Identity",
  185. identity, identity_len);
  186. return 0;
  187. }
  188. static int wps_process_cred_key_prov_auto(struct wps_credential *cred,
  189. const u8 *key_prov_auto)
  190. {
  191. if (key_prov_auto == NULL)
  192. return 0; /* optional attribute */
  193. wpa_printf(MSG_DEBUG, "WPS: Key Provided Automatically: %d",
  194. *key_prov_auto);
  195. return 0;
  196. }
  197. static int wps_process_cred_802_1x_enabled(struct wps_credential *cred,
  198. const u8 *dot1x_enabled)
  199. {
  200. if (dot1x_enabled == NULL)
  201. return 0; /* optional attribute */
  202. wpa_printf(MSG_DEBUG, "WPS: 802.1X Enabled: %d", *dot1x_enabled);
  203. return 0;
  204. }
  205. static int wps_workaround_cred_key(struct wps_credential *cred)
  206. {
  207. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK) &&
  208. cred->key_len > 8 && cred->key_len < 64 &&
  209. cred->key[cred->key_len - 1] == 0) {
  210. #ifdef CONFIG_WPS_STRICT
  211. wpa_printf(MSG_INFO, "WPS: WPA/WPA2-Personal passphrase uses "
  212. "forbidden NULL termination");
  213. wpa_hexdump_ascii_key(MSG_INFO, "WPS: Network Key",
  214. cred->key, cred->key_len);
  215. return -1;
  216. #else /* CONFIG_WPS_STRICT */
  217. /*
  218. * A deployed external registrar is known to encode ASCII
  219. * passphrases incorrectly. Remove the extra NULL termination
  220. * to fix the encoding.
  221. */
  222. wpa_printf(MSG_DEBUG, "WPS: Workaround - remove NULL "
  223. "termination from ASCII passphrase");
  224. cred->key_len--;
  225. #endif /* CONFIG_WPS_STRICT */
  226. }
  227. return 0;
  228. }
  229. int wps_process_cred(struct wps_parse_attr *attr,
  230. struct wps_credential *cred)
  231. {
  232. wpa_printf(MSG_DEBUG, "WPS: Process Credential");
  233. /* TODO: support multiple Network Keys */
  234. if (wps_process_cred_network_idx(cred, attr->network_idx) ||
  235. wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
  236. wps_process_cred_auth_type(cred, attr->auth_type) ||
  237. wps_process_cred_encr_type(cred, attr->encr_type) ||
  238. wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
  239. wps_process_cred_network_key(cred, attr->network_key,
  240. attr->network_key_len) ||
  241. wps_process_cred_mac_addr(cred, attr->mac_addr) ||
  242. wps_process_cred_eap_type(cred, attr->eap_type,
  243. attr->eap_type_len) ||
  244. wps_process_cred_eap_identity(cred, attr->eap_identity,
  245. attr->eap_identity_len) ||
  246. wps_process_cred_key_prov_auto(cred, attr->key_prov_auto) ||
  247. wps_process_cred_802_1x_enabled(cred, attr->dot1x_enabled))
  248. return -1;
  249. return wps_workaround_cred_key(cred);
  250. }
  251. int wps_process_ap_settings(struct wps_parse_attr *attr,
  252. struct wps_credential *cred)
  253. {
  254. wpa_printf(MSG_DEBUG, "WPS: Processing AP Settings");
  255. os_memset(cred, 0, sizeof(*cred));
  256. /* TODO: optional attributes New Password and Device Password ID */
  257. if (wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
  258. wps_process_cred_auth_type(cred, attr->auth_type) ||
  259. wps_process_cred_encr_type(cred, attr->encr_type) ||
  260. wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
  261. wps_process_cred_network_key(cred, attr->network_key,
  262. attr->network_key_len) ||
  263. wps_process_cred_mac_addr(cred, attr->mac_addr))
  264. return -1;
  265. return wps_workaround_cred_key(cred);
  266. }