wpas_kay.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_macsec_get_capability(void *priv, enum macsec_cap *cap)
  33. {
  34. return wpa_drv_macsec_get_capability(priv, cap);
  35. }
  36. static int wpas_enable_protect_frames(void *wpa_s, Boolean enabled)
  37. {
  38. return wpa_drv_enable_protect_frames(wpa_s, enabled);
  39. }
  40. static int wpas_set_replay_protect(void *wpa_s, Boolean enabled, u32 window)
  41. {
  42. return wpa_drv_set_replay_protect(wpa_s, enabled, window);
  43. }
  44. static int wpas_set_current_cipher_suite(void *wpa_s, u64 cs)
  45. {
  46. return wpa_drv_set_current_cipher_suite(wpa_s, cs);
  47. }
  48. static int wpas_enable_controlled_port(void *wpa_s, Boolean enabled)
  49. {
  50. return wpa_drv_enable_controlled_port(wpa_s, enabled);
  51. }
  52. static int wpas_get_receive_lowest_pn(void *wpa_s, struct receive_sa *sa)
  53. {
  54. return wpa_drv_get_receive_lowest_pn(wpa_s, sa);
  55. }
  56. static int wpas_get_transmit_next_pn(void *wpa_s, struct transmit_sa *sa)
  57. {
  58. return wpa_drv_get_transmit_next_pn(wpa_s, sa);
  59. }
  60. static int wpas_set_transmit_next_pn(void *wpa_s, struct transmit_sa *sa)
  61. {
  62. return wpa_drv_set_transmit_next_pn(wpa_s, sa);
  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, struct receive_sc *sc,
  77. enum validate_frames vf,
  78. enum confidentiality_offset co)
  79. {
  80. return wpa_drv_create_receive_sc(wpa_s, sc, conf_offset_val(co), vf);
  81. }
  82. static int wpas_delete_receive_sc(void *wpa_s, struct receive_sc *sc)
  83. {
  84. return wpa_drv_delete_receive_sc(wpa_s, sc);
  85. }
  86. static int wpas_create_receive_sa(void *wpa_s, struct receive_sa *sa)
  87. {
  88. return wpa_drv_create_receive_sa(wpa_s, sa);
  89. }
  90. static int wpas_delete_receive_sa(void *wpa_s, struct receive_sa *sa)
  91. {
  92. return wpa_drv_delete_receive_sa(wpa_s, sa);
  93. }
  94. static int wpas_enable_receive_sa(void *wpa_s, struct receive_sa *sa)
  95. {
  96. return wpa_drv_enable_receive_sa(wpa_s, sa);
  97. }
  98. static int wpas_disable_receive_sa(void *wpa_s, struct receive_sa *sa)
  99. {
  100. return wpa_drv_disable_receive_sa(wpa_s, sa);
  101. }
  102. static int
  103. wpas_create_transmit_sc(void *wpa_s, struct transmit_sc *sc,
  104. enum confidentiality_offset co)
  105. {
  106. return wpa_drv_create_transmit_sc(wpa_s, sc, conf_offset_val(co));
  107. }
  108. static int wpas_delete_transmit_sc(void *wpa_s, struct transmit_sc *sc)
  109. {
  110. return wpa_drv_delete_transmit_sc(wpa_s, sc);
  111. }
  112. static int wpas_create_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  113. {
  114. return wpa_drv_create_transmit_sa(wpa_s, sa);
  115. }
  116. static int wpas_delete_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  117. {
  118. return wpa_drv_delete_transmit_sa(wpa_s, sa);
  119. }
  120. static int wpas_enable_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  121. {
  122. return wpa_drv_enable_transmit_sa(wpa_s, sa);
  123. }
  124. static int wpas_disable_transmit_sa(void *wpa_s, struct transmit_sa *sa)
  125. {
  126. return wpa_drv_disable_transmit_sa(wpa_s, sa);
  127. }
  128. int ieee802_1x_alloc_kay_sm(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  129. {
  130. struct ieee802_1x_kay_ctx *kay_ctx;
  131. struct ieee802_1x_kay *res = NULL;
  132. enum macsec_policy policy;
  133. ieee802_1x_dealloc_kay_sm(wpa_s);
  134. if (!ssid || ssid->macsec_policy == 0)
  135. return 0;
  136. policy = ssid->macsec_policy == 1 ? SHOULD_SECURE : DO_NOT_SECURE;
  137. kay_ctx = os_zalloc(sizeof(*kay_ctx));
  138. if (!kay_ctx)
  139. return -1;
  140. kay_ctx->ctx = wpa_s;
  141. kay_ctx->macsec_init = wpas_macsec_init;
  142. kay_ctx->macsec_deinit = wpas_macsec_deinit;
  143. kay_ctx->macsec_get_capability = wpas_macsec_get_capability;
  144. kay_ctx->enable_protect_frames = wpas_enable_protect_frames;
  145. kay_ctx->set_replay_protect = wpas_set_replay_protect;
  146. kay_ctx->set_current_cipher_suite = wpas_set_current_cipher_suite;
  147. kay_ctx->enable_controlled_port = wpas_enable_controlled_port;
  148. kay_ctx->get_receive_lowest_pn = wpas_get_receive_lowest_pn;
  149. kay_ctx->get_transmit_next_pn = wpas_get_transmit_next_pn;
  150. kay_ctx->set_transmit_next_pn = wpas_set_transmit_next_pn;
  151. kay_ctx->create_receive_sc = wpas_create_receive_sc;
  152. kay_ctx->delete_receive_sc = wpas_delete_receive_sc;
  153. kay_ctx->create_receive_sa = wpas_create_receive_sa;
  154. kay_ctx->delete_receive_sa = wpas_delete_receive_sa;
  155. kay_ctx->enable_receive_sa = wpas_enable_receive_sa;
  156. kay_ctx->disable_receive_sa = wpas_disable_receive_sa;
  157. kay_ctx->create_transmit_sc = wpas_create_transmit_sc;
  158. kay_ctx->delete_transmit_sc = wpas_delete_transmit_sc;
  159. kay_ctx->create_transmit_sa = wpas_create_transmit_sa;
  160. kay_ctx->delete_transmit_sa = wpas_delete_transmit_sa;
  161. kay_ctx->enable_transmit_sa = wpas_enable_transmit_sa;
  162. kay_ctx->disable_transmit_sa = wpas_disable_transmit_sa;
  163. res = ieee802_1x_kay_init(kay_ctx, policy, wpa_s->ifname,
  164. wpa_s->own_addr);
  165. if (res == NULL) {
  166. os_free(kay_ctx);
  167. return -1;
  168. }
  169. wpa_s->kay = res;
  170. return 0;
  171. }
  172. void ieee802_1x_dealloc_kay_sm(struct wpa_supplicant *wpa_s)
  173. {
  174. if (!wpa_s->kay)
  175. return;
  176. ieee802_1x_kay_deinit(wpa_s->kay);
  177. wpa_s->kay = NULL;
  178. }
  179. static int ieee802_1x_auth_get_session_id(struct wpa_supplicant *wpa_s,
  180. const u8 *addr, u8 *sid, size_t *len)
  181. {
  182. const u8 *session_id;
  183. size_t id_len, need_len;
  184. session_id = eapol_sm_get_session_id(wpa_s->eapol, &id_len);
  185. if (session_id == NULL) {
  186. wpa_printf(MSG_DEBUG,
  187. "Failed to get SessionID from EAPOL state machines");
  188. return -1;
  189. }
  190. need_len = 1 + 2 * SSL3_RANDOM_SIZE;
  191. if (need_len > id_len) {
  192. wpa_printf(MSG_DEBUG, "EAP Session-Id not long enough");
  193. return -1;
  194. }
  195. os_memcpy(sid, session_id, need_len);
  196. *len = need_len;
  197. return 0;
  198. }
  199. static int ieee802_1x_auth_get_msk(struct wpa_supplicant *wpa_s, const u8 *addr,
  200. u8 *msk, size_t *len)
  201. {
  202. u8 key[EAP_MSK_LEN];
  203. size_t keylen;
  204. struct eapol_sm *sm;
  205. int res;
  206. sm = wpa_s->eapol;
  207. if (sm == NULL)
  208. return -1;
  209. keylen = EAP_MSK_LEN;
  210. res = eapol_sm_get_key(sm, key, keylen);
  211. if (res) {
  212. wpa_printf(MSG_DEBUG,
  213. "Failed to get MSK from EAPOL state machines");
  214. return -1;
  215. }
  216. if (keylen > *len)
  217. keylen = *len;
  218. os_memcpy(msk, key, keylen);
  219. *len = keylen;
  220. return 0;
  221. }
  222. void * ieee802_1x_notify_create_actor(struct wpa_supplicant *wpa_s,
  223. const u8 *peer_addr)
  224. {
  225. u8 *sid;
  226. size_t sid_len = 128;
  227. struct mka_key_name *ckn;
  228. struct mka_key *cak;
  229. struct mka_key *msk;
  230. void *res = NULL;
  231. if (!wpa_s->kay || wpa_s->kay->policy == DO_NOT_SECURE)
  232. return NULL;
  233. wpa_printf(MSG_DEBUG,
  234. "IEEE 802.1X: External notification - Create MKA for "
  235. MACSTR, MAC2STR(peer_addr));
  236. msk = os_zalloc(sizeof(*msk));
  237. sid = os_zalloc(sid_len);
  238. ckn = os_zalloc(sizeof(*ckn));
  239. cak = os_zalloc(sizeof(*cak));
  240. if (!msk || !sid || !ckn || !cak)
  241. goto fail;
  242. msk->len = DEFAULT_KEY_LEN;
  243. if (ieee802_1x_auth_get_msk(wpa_s, wpa_s->bssid, msk->key, &msk->len)) {
  244. wpa_printf(MSG_ERROR, "IEEE 802.1X: Could not get MSK");
  245. goto fail;
  246. }
  247. if (ieee802_1x_auth_get_session_id(wpa_s, wpa_s->bssid, sid, &sid_len))
  248. {
  249. wpa_printf(MSG_ERROR,
  250. "IEEE 802.1X: Could not get EAP Session Id");
  251. goto fail;
  252. }
  253. /* Derive CAK from MSK */
  254. cak->len = DEFAULT_KEY_LEN;
  255. if (ieee802_1x_cak_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  256. peer_addr, cak->key)) {
  257. wpa_printf(MSG_ERROR,
  258. "IEEE 802.1X: Deriving CAK failed");
  259. goto fail;
  260. }
  261. wpa_hexdump_key(MSG_DEBUG, "Derived CAK", cak->key, cak->len);
  262. /* Derive CKN from MSK */
  263. ckn->len = DEFAULT_CKN_LEN;
  264. if (ieee802_1x_ckn_128bits_aes_cmac(msk->key, wpa_s->own_addr,
  265. peer_addr, sid, sid_len,
  266. ckn->name)) {
  267. wpa_printf(MSG_ERROR,
  268. "IEEE 802.1X: Deriving CKN failed");
  269. goto fail;
  270. }
  271. wpa_hexdump(MSG_DEBUG, "Derived CKN", ckn->name, ckn->len);
  272. res = ieee802_1x_kay_create_mka(wpa_s->kay, ckn, cak, 0,
  273. EAP_EXCHANGE, FALSE);
  274. fail:
  275. if (msk) {
  276. os_memset(msk, 0, sizeof(*msk));
  277. os_free(msk);
  278. }
  279. os_free(sid);
  280. os_free(ckn);
  281. if (cak) {
  282. os_memset(cak, 0, sizeof(*cak));
  283. os_free(cak);
  284. }
  285. return res;
  286. }
  287. void * ieee802_1x_create_preshared_mka(struct wpa_supplicant *wpa_s,
  288. struct wpa_ssid *ssid)
  289. {
  290. struct mka_key *cak;
  291. struct mka_key_name *ckn;
  292. void *res;
  293. if ((ssid->mka_psk_set & MKA_PSK_SET) != MKA_PSK_SET)
  294. return NULL;
  295. if (ieee802_1x_alloc_kay_sm(wpa_s, ssid) < 0)
  296. return NULL;
  297. if (!wpa_s->kay || wpa_s->kay->policy == DO_NOT_SECURE)
  298. return NULL;
  299. ckn = os_zalloc(sizeof(*ckn));
  300. if (!ckn)
  301. goto dealloc;
  302. cak = os_zalloc(sizeof(*cak));
  303. if (!cak)
  304. goto free_ckn;
  305. cak->len = MACSEC_CAK_LEN;
  306. os_memcpy(cak->key, ssid->mka_cak, cak->len);
  307. ckn->len = MACSEC_CKN_LEN;
  308. os_memcpy(ckn->name, ssid->mka_ckn, ckn->len);
  309. res = ieee802_1x_kay_create_mka(wpa_s->kay, ckn, cak, 0, PSK, FALSE);
  310. if (res)
  311. return res;
  312. /* Failed to create MKA */
  313. os_free(cak);
  314. /* fallthrough */
  315. free_ckn:
  316. os_free(ckn);
  317. dealloc:
  318. ieee802_1x_dealloc_kay_sm(wpa_s);
  319. return NULL;
  320. }