wps_attr_process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Wi-Fi Protected Setup - attribute processing
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha256.h"
  11. #include "wps_i.h"
  12. int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
  13. const struct wpabuf *msg)
  14. {
  15. u8 hash[SHA256_MAC_LEN];
  16. const u8 *addr[2];
  17. size_t len[2];
  18. if (authenticator == NULL) {
  19. wpa_printf(MSG_DEBUG, "WPS: No Authenticator attribute "
  20. "included");
  21. return -1;
  22. }
  23. if (wps->last_msg == NULL) {
  24. wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
  25. "validating authenticator");
  26. return -1;
  27. }
  28. /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
  29. * (M_curr* is M_curr without the Authenticator attribute)
  30. */
  31. addr[0] = wpabuf_head(wps->last_msg);
  32. len[0] = wpabuf_len(wps->last_msg);
  33. addr[1] = wpabuf_head(msg);
  34. len[1] = wpabuf_len(msg) - 4 - WPS_AUTHENTICATOR_LEN;
  35. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
  36. if (os_memcmp_const(hash, authenticator, WPS_AUTHENTICATOR_LEN) != 0) {
  37. wpa_printf(MSG_DEBUG, "WPS: Incorrect Authenticator");
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
  43. const u8 *key_wrap_auth)
  44. {
  45. u8 hash[SHA256_MAC_LEN];
  46. const u8 *head;
  47. size_t len;
  48. if (key_wrap_auth == NULL) {
  49. wpa_printf(MSG_DEBUG, "WPS: No KWA in decrypted attribute");
  50. return -1;
  51. }
  52. head = wpabuf_head(msg);
  53. len = wpabuf_len(msg) - 4 - WPS_KWA_LEN;
  54. if (head + len != key_wrap_auth - 4) {
  55. wpa_printf(MSG_DEBUG, "WPS: KWA not in the end of the "
  56. "decrypted attribute");
  57. return -1;
  58. }
  59. hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, head, len, hash);
  60. if (os_memcmp_const(hash, key_wrap_auth, WPS_KWA_LEN) != 0) {
  61. wpa_printf(MSG_DEBUG, "WPS: Invalid KWA");
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. static int wps_process_cred_network_idx(struct wps_credential *cred,
  67. const u8 *idx)
  68. {
  69. if (idx == NULL) {
  70. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  71. "Network Index");
  72. return -1;
  73. }
  74. wpa_printf(MSG_DEBUG, "WPS: Network Index: %d", *idx);
  75. return 0;
  76. }
  77. static int wps_process_cred_ssid(struct wps_credential *cred, const u8 *ssid,
  78. size_t ssid_len)
  79. {
  80. if (ssid == NULL) {
  81. wpa_printf(MSG_DEBUG, "WPS: Credential did not include SSID");
  82. return -1;
  83. }
  84. /* Remove zero-padding since some Registrar implementations seem to use
  85. * hardcoded 32-octet length for this attribute */
  86. while (ssid_len > 0 && ssid[ssid_len - 1] == 0)
  87. ssid_len--;
  88. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", ssid, ssid_len);
  89. if (ssid_len <= sizeof(cred->ssid)) {
  90. os_memcpy(cred->ssid, ssid, ssid_len);
  91. cred->ssid_len = ssid_len;
  92. }
  93. return 0;
  94. }
  95. static int wps_process_cred_auth_type(struct wps_credential *cred,
  96. const u8 *auth_type)
  97. {
  98. if (auth_type == NULL) {
  99. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  100. "Authentication Type");
  101. return -1;
  102. }
  103. cred->auth_type = WPA_GET_BE16(auth_type);
  104. wpa_printf(MSG_DEBUG, "WPS: Authentication Type: 0x%x",
  105. cred->auth_type);
  106. return 0;
  107. }
  108. static int wps_process_cred_encr_type(struct wps_credential *cred,
  109. const u8 *encr_type)
  110. {
  111. if (encr_type == NULL) {
  112. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  113. "Encryption Type");
  114. return -1;
  115. }
  116. cred->encr_type = WPA_GET_BE16(encr_type);
  117. wpa_printf(MSG_DEBUG, "WPS: Encryption Type: 0x%x",
  118. cred->encr_type);
  119. return 0;
  120. }
  121. static int wps_process_cred_network_key_idx(struct wps_credential *cred,
  122. const u8 *key_idx)
  123. {
  124. if (key_idx == NULL)
  125. return 0; /* optional attribute */
  126. wpa_printf(MSG_DEBUG, "WPS: Network Key Index: %d", *key_idx);
  127. cred->key_idx = *key_idx;
  128. return 0;
  129. }
  130. static int wps_process_cred_network_key(struct wps_credential *cred,
  131. const u8 *key, size_t key_len)
  132. {
  133. if (key == NULL) {
  134. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  135. "Network Key");
  136. if (cred->auth_type == WPS_AUTH_OPEN &&
  137. cred->encr_type == WPS_ENCR_NONE) {
  138. wpa_printf(MSG_DEBUG, "WPS: Workaround - Allow "
  139. "missing mandatory Network Key attribute "
  140. "for open network");
  141. return 0;
  142. }
  143. return -1;
  144. }
  145. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", key, key_len);
  146. if (key_len <= sizeof(cred->key)) {
  147. os_memcpy(cred->key, key, key_len);
  148. cred->key_len = key_len;
  149. }
  150. return 0;
  151. }
  152. static int wps_process_cred_mac_addr(struct wps_credential *cred,
  153. const u8 *mac_addr)
  154. {
  155. if (mac_addr == NULL) {
  156. wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
  157. "MAC Address");
  158. return -1;
  159. }
  160. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, MAC2STR(mac_addr));
  161. os_memcpy(cred->mac_addr, mac_addr, ETH_ALEN);
  162. return 0;
  163. }
  164. static int wps_workaround_cred_key(struct wps_credential *cred)
  165. {
  166. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK) &&
  167. cred->key_len > 8 && cred->key_len < 64 &&
  168. cred->key[cred->key_len - 1] == 0) {
  169. #ifdef CONFIG_WPS_STRICT
  170. wpa_printf(MSG_INFO, "WPS: WPA/WPA2-Personal passphrase uses "
  171. "forbidden NULL termination");
  172. wpa_hexdump_ascii_key(MSG_INFO, "WPS: Network Key",
  173. cred->key, cred->key_len);
  174. return -1;
  175. #else /* CONFIG_WPS_STRICT */
  176. /*
  177. * A deployed external registrar is known to encode ASCII
  178. * passphrases incorrectly. Remove the extra NULL termination
  179. * to fix the encoding.
  180. */
  181. wpa_printf(MSG_DEBUG, "WPS: Workaround - remove NULL "
  182. "termination from ASCII passphrase");
  183. cred->key_len--;
  184. #endif /* CONFIG_WPS_STRICT */
  185. }
  186. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK) &&
  187. (cred->key_len < 8 || has_ctrl_char(cred->key, cred->key_len))) {
  188. wpa_printf(MSG_INFO, "WPS: Reject credential with invalid WPA/WPA2-Personal passphrase");
  189. wpa_hexdump_ascii_key(MSG_INFO, "WPS: Network Key",
  190. cred->key, cred->key_len);
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. int wps_process_cred(struct wps_parse_attr *attr,
  196. struct wps_credential *cred)
  197. {
  198. wpa_printf(MSG_DEBUG, "WPS: Process Credential");
  199. /* TODO: support multiple Network Keys */
  200. if (wps_process_cred_network_idx(cred, attr->network_idx) ||
  201. wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
  202. wps_process_cred_auth_type(cred, attr->auth_type) ||
  203. wps_process_cred_encr_type(cred, attr->encr_type) ||
  204. wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
  205. wps_process_cred_network_key(cred, attr->network_key,
  206. attr->network_key_len) ||
  207. wps_process_cred_mac_addr(cred, attr->mac_addr))
  208. return -1;
  209. return wps_workaround_cred_key(cred);
  210. }
  211. int wps_process_ap_settings(struct wps_parse_attr *attr,
  212. struct wps_credential *cred)
  213. {
  214. wpa_printf(MSG_DEBUG, "WPS: Processing AP Settings");
  215. os_memset(cred, 0, sizeof(*cred));
  216. /* TODO: optional attributes New Password and Device Password ID */
  217. if (wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
  218. wps_process_cred_auth_type(cred, attr->auth_type) ||
  219. wps_process_cred_encr_type(cred, attr->encr_type) ||
  220. wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
  221. wps_process_cred_network_key(cred, attr->network_key,
  222. attr->network_key_len) ||
  223. wps_process_cred_mac_addr(cred, attr->mac_addr))
  224. return -1;
  225. return wps_workaround_cred_key(cred);
  226. }