wpas_kay.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * IEEE 802.1X-2010 KaY Interface
  3. * Copyright (c) 2013-2014, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <openssl/ssl.h>
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "eap_peer/eap.h"
  12. #include "eap_peer/eap_i.h"
  13. #include "eapol_supp/eapol_supp_sm.h"
  14. #include "pae/ieee802_1x_key.h"
  15. #include "pae/ieee802_1x_kay.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "config.h"
  18. #include "config_ssid.h"
  19. #include "driver_i.h"
  20. #include "wpas_kay.h"
  21. #define DEFAULT_KEY_LEN 16
  22. /* secure Connectivity Association Key Name (CKN) */
  23. #define DEFAULT_CKN_LEN 16
  24. static int wpas_macsec_init(void *priv, struct macsec_init_params *params)
  25. {
  26. return wpa_drv_macsec_init(priv, params);
  27. }
  28. static int wpas_macsec_deinit(void *priv)
  29. {
  30. return wpa_drv_macsec_deinit(priv);
  31. }
  32. static int wpas_enable_protect_frames(void *wpa_s, Boolean enabled)
  33. {
  34. return wpa_drv_enable_protect_frames(wpa_s, enabled);
  35. }
  36. static int wpas_set_replay_protect(void *wpa_s, Boolean enabled, u32 window)
  37. {
  38. return wpa_drv_set_replay_protect(wpa_s, enabled, window);
  39. }
  40. static int wpas_set_current_cipher_suite(void *wpa_s, u64 cs)
  41. {
  42. return wpa_drv_set_current_cipher_suite(wpa_s, cs);
  43. }
  44. static int wpas_enable_controlled_port(void *wpa_s, Boolean enabled)
  45. {
  46. return wpa_drv_enable_controlled_port(wpa_s, enabled);
  47. }
  48. static int wpas_get_receive_lowest_pn(void *wpa_s, struct receive_sa *sa)
  49. {
  50. return wpa_drv_get_receive_lowest_pn(wpa_s, sa);
  51. }
  52. static int wpas_get_transmit_next_pn(void *wpa_s, struct transmit_sa *sa)
  53. {
  54. return wpa_drv_get_transmit_next_pn(wpa_s, sa);
  55. }
  56. static int wpas_set_transmit_next_pn(void *wpa_s, struct transmit_sa *sa)
  57. {
  58. return wpa_drv_set_transmit_next_pn(wpa_s, sa);
  59. }
  60. static int wpas_get_available_receive_sc(void *wpa_s, u32 *channel)
  61. {
  62. return wpa_drv_get_available_receive_sc(wpa_s, channel);
  63. }
  64. static unsigned int conf_offset_val(enum confidentiality_offset co)
  65. {
  66. switch (co) {
  67. case CONFIDENTIALITY_OFFSET_30:
  68. return 30;
  69. break;
  70. case CONFIDENTIALITY_OFFSET_50:
  71. return 50;
  72. default:
  73. return 0;
  74. }
  75. }
  76. static int wpas_create_receive_sc(void *wpa_s, u32 channel,
  77. struct ieee802_1x_mka_sci *sci,
  78. enum validate_frames vf,
  79. enum confidentiality_offset co)
  80. {
  81. return wpa_drv_create_receive_sc(wpa_s, channel, sci->addr,
  82. be_to_host16(sci->port),
  83. conf_offset_val(co), vf);
  84. }
  85. static int wpas_delete_receive_sc(void *wpa_s, u32 channel)
  86. {
  87. return wpa_drv_delete_receive_sc(wpa_s, channel);
  88. }
  89. static int wpas_create_receive_sa(void *wpa_s, struct receive_sa *sa)
  90. {
  91. return wpa_drv_create_receive_sa(wpa_s, sa);
  92. }
  93. static int wpas_enable_receive_sa(void *wpa_s, struct receive_sa *sa)
  94. {
  95. return wpa_drv_enable_receive_sa(wpa_s, sa);
  96. }
  97. static int wpas_disable_receive_sa(void *wpa_s, struct receive_sa *sa)
  98. {
  99. return wpa_drv_disable_receive_sa(wpa_s, sa);
  100. }
  101. static int wpas_get_available_transmit_sc(void *wpa_s, u32 *channel)
  102. {
  103. return wpa_drv_get_available_transmit_sc(wpa_s, channel);
  104. }
  105. static int
  106. wpas_create_transmit_sc(void *wpa_s, u32 channel,
  107. const struct ieee802_1x_mka_sci *sci,
  108. enum confidentiality_offset co)
  109. {
  110. return wpa_drv_create_transmit_sc(wpa_s, channel, sci->addr,
  111. be_to_host16(sci->port),
  112. conf_offset_val(co));
  113. }
  114. static int wpas_delete_transmit_sc(void *wpa_s, u32 channel)
  115. {
  116. return wpa_drv_delete_transmit_sc(wpa_s, channel);
  117. }
  118. static int wpas_create_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  119. {
  120. return wpa_drv_create_transmit_sa(wpa_s, sa);
  121. }
  122. static int wpas_enable_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  123. {
  124. return wpa_drv_enable_transmit_sa(wpa_s, sa);
  125. }
  126. static int wpas_disable_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  127. {
  128. return wpa_drv_disable_transmit_sa(wpa_s, sa);
  129. }
  130. int ieee802_1x_alloc_kay_sm(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  131. {
  132. struct ieee802_1x_kay_ctx *kay_ctx;
  133. struct ieee802_1x_kay *res = NULL;
  134. enum macsec_policy policy;
  135. ieee802_1x_dealloc_kay_sm(wpa_s);
  136. if (!ssid || ssid->macsec_policy == 0)
  137. return 0;
  138. policy = ssid->macsec_policy == 1 ? SHOULD_SECURE : DO_NOT_SECURE;
  139. kay_ctx = os_zalloc(sizeof(*kay_ctx));
  140. if (!kay_ctx)
  141. return -1;
  142. kay_ctx->ctx = wpa_s;
  143. kay_ctx->macsec_init = wpas_macsec_init;
  144. kay_ctx->macsec_deinit = wpas_macsec_deinit;
  145. kay_ctx->enable_protect_frames = wpas_enable_protect_frames;
  146. kay_ctx->set_replay_protect = wpas_set_replay_protect;
  147. kay_ctx->set_current_cipher_suite = wpas_set_current_cipher_suite;
  148. kay_ctx->enable_controlled_port = wpas_enable_controlled_port;
  149. kay_ctx->get_receive_lowest_pn = wpas_get_receive_lowest_pn;
  150. kay_ctx->get_transmit_next_pn = wpas_get_transmit_next_pn;
  151. kay_ctx->set_transmit_next_pn = wpas_set_transmit_next_pn;
  152. kay_ctx->get_available_receive_sc = wpas_get_available_receive_sc;
  153. kay_ctx->create_receive_sc = wpas_create_receive_sc;
  154. kay_ctx->delete_receive_sc = wpas_delete_receive_sc;
  155. kay_ctx->create_receive_sa = wpas_create_receive_sa;
  156. kay_ctx->enable_receive_sa = wpas_enable_receive_sa;
  157. kay_ctx->disable_receive_sa = wpas_disable_receive_sa;
  158. kay_ctx->get_available_transmit_sc = wpas_get_available_transmit_sc;
  159. kay_ctx->create_transmit_sc = wpas_create_transmit_sc;
  160. kay_ctx->delete_transmit_sc = wpas_delete_transmit_sc;
  161. kay_ctx->create_transmit_sa = wpas_create_transmit_sa;
  162. kay_ctx->enable_transmit_sa = wpas_enable_transmit_sa;
  163. kay_ctx->disable_transmit_sa = wpas_disable_transmit_sa;
  164. res = ieee802_1x_kay_init(kay_ctx, policy, wpa_s->ifname,
  165. wpa_s->own_addr);
  166. if (res == NULL) {
  167. os_free(kay_ctx);
  168. return -1;
  169. }
  170. wpa_s->kay = res;
  171. return 0;
  172. }
  173. void ieee802_1x_dealloc_kay_sm(struct wpa_supplicant *wpa_s)
  174. {
  175. if (!wpa_s->kay)
  176. return;
  177. ieee802_1x_kay_deinit(wpa_s->kay);
  178. wpa_s->kay = NULL;
  179. }
  180. static int ieee802_1x_auth_get_session_id(struct wpa_supplicant *wpa_s,
  181. const u8 *addr, u8 *sid, size_t *len)
  182. {
  183. const u8 *session_id;
  184. size_t id_len, need_len;
  185. session_id = eapol_sm_get_session_id(wpa_s->eapol, &id_len);
  186. if (session_id == NULL) {
  187. wpa_printf(MSG_DEBUG,
  188. "Failed to get SessionID from EAPOL state machines");
  189. return -1;
  190. }
  191. need_len = 1 + 2 * SSL3_RANDOM_SIZE;
  192. if (need_len > id_len) {
  193. wpa_printf(MSG_DEBUG, "EAP Session-Id not long enough");
  194. return -1;
  195. }
  196. os_memcpy(sid, session_id, need_len);
  197. *len = need_len;
  198. return 0;
  199. }
  200. static int ieee802_1x_auth_get_msk(struct wpa_supplicant *wpa_s, const u8 *addr,
  201. u8 *msk, size_t *len)
  202. {
  203. u8 key[EAP_MSK_LEN];
  204. size_t keylen;
  205. struct eapol_sm *sm;
  206. int res;
  207. sm = wpa_s->eapol;
  208. if (sm == NULL)
  209. return -1;
  210. keylen = EAP_MSK_LEN;
  211. res = eapol_sm_get_key(sm, key, keylen);
  212. if (res) {
  213. wpa_printf(MSG_DEBUG,
  214. "Failed to get MSK from EAPOL state machines");
  215. return -1;
  216. }
  217. if (keylen > *len)
  218. keylen = *len;
  219. os_memcpy(msk, key, keylen);
  220. *len = keylen;
  221. return 0;
  222. }
  223. void * ieee802_1x_notify_create_actor(struct wpa_supplicant *wpa_s,
  224. const u8 *peer_addr)
  225. {
  226. u8 *sid;
  227. size_t sid_len = 128;
  228. struct mka_key_name *ckn;
  229. struct mka_key *cak;
  230. struct mka_key *msk;
  231. void *res = NULL;
  232. if (!wpa_s->kay || wpa_s->kay->policy == DO_NOT_SECURE)
  233. return NULL;
  234. wpa_printf(MSG_DEBUG,
  235. "IEEE 802.1X: External notification - Create MKA for "
  236. MACSTR, MAC2STR(peer_addr));
  237. msk = os_zalloc(sizeof(*msk));
  238. sid = os_zalloc(sid_len);
  239. ckn = os_zalloc(sizeof(*ckn));
  240. cak = os_zalloc(sizeof(*cak));
  241. if (!msk || !sid || !ckn || !cak)
  242. goto fail;
  243. msk->len = DEFAULT_KEY_LEN;
  244. if (ieee802_1x_auth_get_msk(wpa_s, wpa_s->bssid, msk->key, &msk->len)) {
  245. wpa_printf(MSG_ERROR, "IEEE 802.1X: Could not get MSK");
  246. goto fail;
  247. }
  248. if (ieee802_1x_auth_get_session_id(wpa_s, wpa_s->bssid, sid, &sid_len))
  249. {
  250. wpa_printf(MSG_ERROR,
  251. "IEEE 802.1X: Could not get EAP Session Id");
  252. goto fail;
  253. }
  254. /* Derive CAK from MSK */
  255. cak->len = DEFAULT_KEY_LEN;
  256. if (ieee802_1x_cak_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  257. peer_addr, cak->key)) {
  258. wpa_printf(MSG_ERROR,
  259. "IEEE 802.1X: Deriving CAK failed");
  260. goto fail;
  261. }
  262. wpa_hexdump_key(MSG_DEBUG, "Derived CAK", cak->key, cak->len);
  263. /* Derive CKN from MSK */
  264. ckn->len = DEFAULT_CKN_LEN;
  265. if (ieee802_1x_ckn_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  266. peer_addr, sid, sid_len,
  267. ckn->name)) {
  268. wpa_printf(MSG_ERROR,
  269. "IEEE 802.1X: Deriving CKN failed");
  270. goto fail;
  271. }
  272. wpa_hexdump(MSG_DEBUG, "Derived CKN", ckn->name, ckn->len);
  273. res = ieee802_1x_kay_create_mka(wpa_s->kay, ckn, cak, 0,
  274. EAP_EXCHANGE, FALSE);
  275. fail:
  276. if (msk) {
  277. os_memset(msk, 0, sizeof(*msk));
  278. os_free(msk);
  279. }
  280. os_free(sid);
  281. os_free(ckn);
  282. if (cak) {
  283. os_memset(cak, 0, sizeof(*cak));
  284. os_free(cak);
  285. }
  286. return res;
  287. }