wpas_kay.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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, u32 channel, u8 an,
  90. u32 lowest_pn, const u8 *sak)
  91. {
  92. return wpa_drv_create_receive_sa(wpa_s, channel, an, lowest_pn, sak);
  93. }
  94. static int wpas_enable_receive_sa(void *wpa_s, u32 channel, u8 an)
  95. {
  96. return wpa_drv_enable_receive_sa(wpa_s, channel, an);
  97. }
  98. static int wpas_disable_receive_sa(void *wpa_s, u32 channel, u8 an)
  99. {
  100. return wpa_drv_disable_receive_sa(wpa_s, channel, an);
  101. }
  102. static int wpas_get_available_transmit_sc(void *wpa_s, u32 *channel)
  103. {
  104. return wpa_drv_get_available_transmit_sc(wpa_s, channel);
  105. }
  106. static int
  107. wpas_create_transmit_sc(void *wpa_s, u32 channel,
  108. const struct ieee802_1x_mka_sci *sci,
  109. enum confidentiality_offset co)
  110. {
  111. return wpa_drv_create_transmit_sc(wpa_s, channel, sci->addr,
  112. be_to_host16(sci->port),
  113. conf_offset_val(co));
  114. }
  115. static int wpas_delete_transmit_sc(void *wpa_s, u32 channel)
  116. {
  117. return wpa_drv_delete_transmit_sc(wpa_s, channel);
  118. }
  119. static int wpas_create_transmit_sa(void *wpa_s, u32 channel, u8 an,
  120. u32 next_pn, Boolean confidentiality,
  121. const u8 *sak)
  122. {
  123. return wpa_drv_create_transmit_sa(wpa_s, channel, an, next_pn,
  124. confidentiality, sak);
  125. }
  126. static int wpas_enable_transmit_sa(void *wpa_s, u32 channel, u8 an)
  127. {
  128. return wpa_drv_enable_transmit_sa(wpa_s, channel, an);
  129. }
  130. static int wpas_disable_transmit_sa(void *wpa_s, u32 channel, u8 an)
  131. {
  132. return wpa_drv_disable_transmit_sa(wpa_s, channel, an);
  133. }
  134. int ieee802_1x_alloc_kay_sm(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  135. {
  136. struct ieee802_1x_kay_ctx *kay_ctx;
  137. struct ieee802_1x_kay *res = NULL;
  138. enum macsec_policy policy;
  139. ieee802_1x_dealloc_kay_sm(wpa_s);
  140. if (!ssid || ssid->macsec_policy == 0)
  141. return 0;
  142. policy = ssid->macsec_policy == 1 ? SHOULD_SECURE : DO_NOT_SECURE;
  143. kay_ctx = os_zalloc(sizeof(*kay_ctx));
  144. if (!kay_ctx)
  145. return -1;
  146. kay_ctx->ctx = wpa_s;
  147. kay_ctx->macsec_init = wpas_macsec_init;
  148. kay_ctx->macsec_deinit = wpas_macsec_deinit;
  149. kay_ctx->enable_protect_frames = wpas_enable_protect_frames;
  150. kay_ctx->set_replay_protect = wpas_set_replay_protect;
  151. kay_ctx->set_current_cipher_suite = wpas_set_current_cipher_suite;
  152. kay_ctx->enable_controlled_port = wpas_enable_controlled_port;
  153. kay_ctx->get_receive_lowest_pn = wpas_get_receive_lowest_pn;
  154. kay_ctx->get_transmit_next_pn = wpas_get_transmit_next_pn;
  155. kay_ctx->set_transmit_next_pn = wpas_set_transmit_next_pn;
  156. kay_ctx->get_available_receive_sc = wpas_get_available_receive_sc;
  157. kay_ctx->create_receive_sc = wpas_create_receive_sc;
  158. kay_ctx->delete_receive_sc = wpas_delete_receive_sc;
  159. kay_ctx->create_receive_sa = wpas_create_receive_sa;
  160. kay_ctx->enable_receive_sa = wpas_enable_receive_sa;
  161. kay_ctx->disable_receive_sa = wpas_disable_receive_sa;
  162. kay_ctx->get_available_transmit_sc = wpas_get_available_transmit_sc;
  163. kay_ctx->create_transmit_sc = wpas_create_transmit_sc;
  164. kay_ctx->delete_transmit_sc = wpas_delete_transmit_sc;
  165. kay_ctx->create_transmit_sa = wpas_create_transmit_sa;
  166. kay_ctx->enable_transmit_sa = wpas_enable_transmit_sa;
  167. kay_ctx->disable_transmit_sa = wpas_disable_transmit_sa;
  168. res = ieee802_1x_kay_init(kay_ctx, policy, wpa_s->ifname,
  169. wpa_s->own_addr);
  170. if (res == NULL) {
  171. os_free(kay_ctx);
  172. return -1;
  173. }
  174. wpa_s->kay = res;
  175. return 0;
  176. }
  177. void ieee802_1x_dealloc_kay_sm(struct wpa_supplicant *wpa_s)
  178. {
  179. if (!wpa_s->kay)
  180. return;
  181. ieee802_1x_kay_deinit(wpa_s->kay);
  182. wpa_s->kay = NULL;
  183. }
  184. static int ieee802_1x_auth_get_session_id(struct wpa_supplicant *wpa_s,
  185. const u8 *addr, u8 *sid, size_t *len)
  186. {
  187. const u8 *session_id;
  188. size_t id_len, need_len;
  189. session_id = eapol_sm_get_session_id(wpa_s->eapol, &id_len);
  190. if (session_id == NULL) {
  191. wpa_printf(MSG_DEBUG,
  192. "Failed to get SessionID from EAPOL state machines");
  193. return -1;
  194. }
  195. need_len = 1 + 2 * SSL3_RANDOM_SIZE;
  196. if (need_len > id_len) {
  197. wpa_printf(MSG_DEBUG, "EAP Session-Id not long enough");
  198. return -1;
  199. }
  200. os_memcpy(sid, session_id, need_len);
  201. *len = need_len;
  202. return 0;
  203. }
  204. static int ieee802_1x_auth_get_msk(struct wpa_supplicant *wpa_s, const u8 *addr,
  205. u8 *msk, size_t *len)
  206. {
  207. u8 key[EAP_MSK_LEN];
  208. size_t keylen;
  209. struct eapol_sm *sm;
  210. int res;
  211. sm = wpa_s->eapol;
  212. if (sm == NULL)
  213. return -1;
  214. keylen = EAP_MSK_LEN;
  215. res = eapol_sm_get_key(sm, key, keylen);
  216. if (res) {
  217. wpa_printf(MSG_DEBUG,
  218. "Failed to get MSK from EAPOL state machines");
  219. return -1;
  220. }
  221. if (keylen > *len)
  222. keylen = *len;
  223. os_memcpy(msk, key, keylen);
  224. *len = keylen;
  225. return 0;
  226. }
  227. void * ieee802_1x_notify_create_actor(struct wpa_supplicant *wpa_s,
  228. const u8 *peer_addr)
  229. {
  230. u8 *sid;
  231. size_t sid_len = 128;
  232. struct mka_key_name *ckn;
  233. struct mka_key *cak;
  234. struct mka_key *msk;
  235. void *res = NULL;
  236. if (!wpa_s->kay || wpa_s->kay->policy == DO_NOT_SECURE)
  237. return NULL;
  238. wpa_printf(MSG_DEBUG,
  239. "IEEE 802.1X: External notification - Create MKA for "
  240. MACSTR, MAC2STR(peer_addr));
  241. msk = os_zalloc(sizeof(*msk));
  242. sid = os_zalloc(sid_len);
  243. ckn = os_zalloc(sizeof(*ckn));
  244. cak = os_zalloc(sizeof(*cak));
  245. if (!msk || !sid || !ckn || !cak)
  246. goto fail;
  247. msk->len = DEFAULT_KEY_LEN;
  248. if (ieee802_1x_auth_get_msk(wpa_s, wpa_s->bssid, msk->key, &msk->len)) {
  249. wpa_printf(MSG_ERROR, "IEEE 802.1X: Could not get MSK");
  250. goto fail;
  251. }
  252. if (ieee802_1x_auth_get_session_id(wpa_s, wpa_s->bssid, sid, &sid_len))
  253. {
  254. wpa_printf(MSG_ERROR,
  255. "IEEE 802.1X: Could not get EAP Session Id");
  256. goto fail;
  257. }
  258. /* Derive CAK from MSK */
  259. cak->len = DEFAULT_KEY_LEN;
  260. if (ieee802_1x_cak_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  261. peer_addr, cak->key)) {
  262. wpa_printf(MSG_ERROR,
  263. "IEEE 802.1X: Deriving CAK failed");
  264. goto fail;
  265. }
  266. wpa_hexdump_key(MSG_DEBUG, "Derived CAK", cak->key, cak->len);
  267. /* Derive CKN from MSK */
  268. ckn->len = DEFAULT_CKN_LEN;
  269. if (ieee802_1x_ckn_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  270. peer_addr, sid, sid_len,
  271. ckn->name)) {
  272. wpa_printf(MSG_ERROR,
  273. "IEEE 802.1X: Deriving CKN failed");
  274. goto fail;
  275. }
  276. wpa_hexdump(MSG_DEBUG, "Derived CKN", ckn->name, ckn->len);
  277. res = ieee802_1x_kay_create_mka(wpa_s->kay, ckn, cak, 0,
  278. EAP_EXCHANGE, FALSE);
  279. fail:
  280. if (msk) {
  281. os_memset(msk, 0, sizeof(*msk));
  282. os_free(msk);
  283. }
  284. os_free(sid);
  285. os_free(ckn);
  286. if (cak) {
  287. os_memset(cak, 0, sizeof(*cak));
  288. os_free(cak);
  289. }
  290. return res;
  291. }