eap_tls.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * EAP peer method: EAP-TLS (RFC 2716)
  3. * Copyright (c) 2004-2008, 2012, 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/tls.h"
  11. #include "eap_i.h"
  12. #include "eap_tls_common.h"
  13. #include "eap_config.h"
  14. static void eap_tls_deinit(struct eap_sm *sm, void *priv);
  15. struct eap_tls_data {
  16. struct eap_ssl_data ssl;
  17. u8 *key_data;
  18. u8 *session_id;
  19. size_t id_len;
  20. void *ssl_ctx;
  21. u8 eap_type;
  22. };
  23. static void * eap_tls_init(struct eap_sm *sm)
  24. {
  25. struct eap_tls_data *data;
  26. struct eap_peer_config *config = eap_get_config(sm);
  27. if (config == NULL ||
  28. ((sm->init_phase2 ? config->private_key2 : config->private_key)
  29. == NULL &&
  30. (sm->init_phase2 ? config->engine2 : config->engine) == 0)) {
  31. wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
  32. return NULL;
  33. }
  34. data = os_zalloc(sizeof(*data));
  35. if (data == NULL)
  36. return NULL;
  37. data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
  38. sm->ssl_ctx;
  39. if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TLS)) {
  40. wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
  41. eap_tls_deinit(sm, data);
  42. if (config->engine) {
  43. wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting Smartcard "
  44. "PIN");
  45. eap_sm_request_pin(sm);
  46. sm->ignore = TRUE;
  47. } else if (config->private_key && !config->private_key_passwd)
  48. {
  49. wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting private "
  50. "key passphrase");
  51. eap_sm_request_passphrase(sm);
  52. sm->ignore = TRUE;
  53. }
  54. return NULL;
  55. }
  56. data->eap_type = EAP_TYPE_TLS;
  57. return data;
  58. }
  59. #ifdef EAP_UNAUTH_TLS
  60. static void * eap_unauth_tls_init(struct eap_sm *sm)
  61. {
  62. struct eap_tls_data *data;
  63. struct eap_peer_config *config = eap_get_config(sm);
  64. data = os_zalloc(sizeof(*data));
  65. if (data == NULL)
  66. return NULL;
  67. data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
  68. sm->ssl_ctx;
  69. if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
  70. EAP_UNAUTH_TLS_TYPE)) {
  71. wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
  72. eap_tls_deinit(sm, data);
  73. return NULL;
  74. }
  75. data->eap_type = EAP_UNAUTH_TLS_TYPE;
  76. return data;
  77. }
  78. #endif /* EAP_UNAUTH_TLS */
  79. #ifdef CONFIG_HS20
  80. static void * eap_wfa_unauth_tls_init(struct eap_sm *sm)
  81. {
  82. struct eap_tls_data *data;
  83. struct eap_peer_config *config = eap_get_config(sm);
  84. data = os_zalloc(sizeof(*data));
  85. if (data == NULL)
  86. return NULL;
  87. data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
  88. sm->ssl_ctx;
  89. if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
  90. EAP_WFA_UNAUTH_TLS_TYPE)) {
  91. wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
  92. eap_tls_deinit(sm, data);
  93. return NULL;
  94. }
  95. data->eap_type = EAP_WFA_UNAUTH_TLS_TYPE;
  96. return data;
  97. }
  98. #endif /* CONFIG_HS20 */
  99. static void eap_tls_deinit(struct eap_sm *sm, void *priv)
  100. {
  101. struct eap_tls_data *data = priv;
  102. if (data == NULL)
  103. return;
  104. eap_peer_tls_ssl_deinit(sm, &data->ssl);
  105. os_free(data->key_data);
  106. os_free(data->session_id);
  107. os_free(data);
  108. }
  109. static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
  110. struct eap_tls_data *data,
  111. struct eap_method_ret *ret, int res,
  112. struct wpabuf *resp, u8 id)
  113. {
  114. wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
  115. ret->methodState = METHOD_DONE;
  116. ret->decision = DECISION_FAIL;
  117. if (res == -1) {
  118. struct eap_peer_config *config = eap_get_config(sm);
  119. if (config) {
  120. /*
  121. * The TLS handshake failed. So better forget the old
  122. * PIN. It may be wrong, we cannot be sure but trying
  123. * the wrong one again might block it on the card--so
  124. * better ask the user again.
  125. */
  126. os_free(config->pin);
  127. config->pin = NULL;
  128. }
  129. }
  130. if (resp) {
  131. /*
  132. * This is likely an alert message, so send it instead of just
  133. * ACKing the error.
  134. */
  135. return resp;
  136. }
  137. return eap_peer_tls_build_ack(id, data->eap_type, 0);
  138. }
  139. static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
  140. struct eap_method_ret *ret)
  141. {
  142. wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
  143. ret->methodState = METHOD_DONE;
  144. ret->decision = DECISION_UNCOND_SUCC;
  145. os_free(data->key_data);
  146. data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
  147. "client EAP encryption",
  148. EAP_TLS_KEY_LEN +
  149. EAP_EMSK_LEN);
  150. if (data->key_data) {
  151. wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
  152. data->key_data, EAP_TLS_KEY_LEN);
  153. wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
  154. data->key_data + EAP_TLS_KEY_LEN,
  155. EAP_EMSK_LEN);
  156. } else {
  157. wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
  158. }
  159. os_free(data->session_id);
  160. data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
  161. EAP_TYPE_TLS,
  162. &data->id_len);
  163. if (data->session_id) {
  164. wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived Session-Id",
  165. data->session_id, data->id_len);
  166. } else {
  167. wpa_printf(MSG_ERROR, "EAP-TLS: Failed to derive Session-Id");
  168. }
  169. }
  170. static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
  171. struct eap_method_ret *ret,
  172. const struct wpabuf *reqData)
  173. {
  174. size_t left;
  175. int res;
  176. struct wpabuf *resp;
  177. u8 flags, id;
  178. const u8 *pos;
  179. struct eap_tls_data *data = priv;
  180. pos = eap_peer_tls_process_init(sm, &data->ssl, data->eap_type, ret,
  181. reqData, &left, &flags);
  182. if (pos == NULL)
  183. return NULL;
  184. id = eap_get_id(reqData);
  185. if (flags & EAP_TLS_FLAGS_START) {
  186. wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
  187. left = 0; /* make sure that this frame is empty, even though it
  188. * should always be, anyway */
  189. }
  190. resp = NULL;
  191. res = eap_peer_tls_process_helper(sm, &data->ssl, data->eap_type, 0,
  192. id, pos, left, &resp);
  193. if (res < 0) {
  194. return eap_tls_failure(sm, data, ret, res, resp, id);
  195. }
  196. if (tls_connection_established(data->ssl_ctx, data->ssl.conn))
  197. eap_tls_success(sm, data, ret);
  198. if (res == 1) {
  199. wpabuf_free(resp);
  200. return eap_peer_tls_build_ack(id, data->eap_type, 0);
  201. }
  202. return resp;
  203. }
  204. static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
  205. {
  206. struct eap_tls_data *data = priv;
  207. return tls_connection_established(data->ssl_ctx, data->ssl.conn);
  208. }
  209. static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
  210. {
  211. }
  212. static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
  213. {
  214. struct eap_tls_data *data = priv;
  215. os_free(data->key_data);
  216. data->key_data = NULL;
  217. os_free(data->session_id);
  218. data->session_id = NULL;
  219. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  220. os_free(data);
  221. return NULL;
  222. }
  223. return priv;
  224. }
  225. static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
  226. size_t buflen, int verbose)
  227. {
  228. struct eap_tls_data *data = priv;
  229. return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  230. }
  231. static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
  232. {
  233. struct eap_tls_data *data = priv;
  234. return data->key_data != NULL;
  235. }
  236. static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  237. {
  238. struct eap_tls_data *data = priv;
  239. u8 *key;
  240. if (data->key_data == NULL)
  241. return NULL;
  242. key = os_malloc(EAP_TLS_KEY_LEN);
  243. if (key == NULL)
  244. return NULL;
  245. *len = EAP_TLS_KEY_LEN;
  246. os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
  247. return key;
  248. }
  249. static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  250. {
  251. struct eap_tls_data *data = priv;
  252. u8 *key;
  253. if (data->key_data == NULL)
  254. return NULL;
  255. key = os_malloc(EAP_EMSK_LEN);
  256. if (key == NULL)
  257. return NULL;
  258. *len = EAP_EMSK_LEN;
  259. os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
  260. return key;
  261. }
  262. static u8 * eap_tls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  263. {
  264. struct eap_tls_data *data = priv;
  265. u8 *id;
  266. if (data->session_id == NULL)
  267. return NULL;
  268. id = os_malloc(data->id_len);
  269. if (id == NULL)
  270. return NULL;
  271. *len = data->id_len;
  272. os_memcpy(id, data->session_id, data->id_len);
  273. return id;
  274. }
  275. int eap_peer_tls_register(void)
  276. {
  277. struct eap_method *eap;
  278. int ret;
  279. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  280. EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
  281. if (eap == NULL)
  282. return -1;
  283. eap->init = eap_tls_init;
  284. eap->deinit = eap_tls_deinit;
  285. eap->process = eap_tls_process;
  286. eap->isKeyAvailable = eap_tls_isKeyAvailable;
  287. eap->getKey = eap_tls_getKey;
  288. eap->getSessionId = eap_tls_get_session_id;
  289. eap->get_status = eap_tls_get_status;
  290. eap->has_reauth_data = eap_tls_has_reauth_data;
  291. eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
  292. eap->init_for_reauth = eap_tls_init_for_reauth;
  293. eap->get_emsk = eap_tls_get_emsk;
  294. ret = eap_peer_method_register(eap);
  295. if (ret)
  296. eap_peer_method_free(eap);
  297. return ret;
  298. }
  299. #ifdef EAP_UNAUTH_TLS
  300. int eap_peer_unauth_tls_register(void)
  301. {
  302. struct eap_method *eap;
  303. int ret;
  304. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  305. EAP_VENDOR_UNAUTH_TLS,
  306. EAP_VENDOR_TYPE_UNAUTH_TLS, "UNAUTH-TLS");
  307. if (eap == NULL)
  308. return -1;
  309. eap->init = eap_unauth_tls_init;
  310. eap->deinit = eap_tls_deinit;
  311. eap->process = eap_tls_process;
  312. eap->isKeyAvailable = eap_tls_isKeyAvailable;
  313. eap->getKey = eap_tls_getKey;
  314. eap->get_status = eap_tls_get_status;
  315. eap->has_reauth_data = eap_tls_has_reauth_data;
  316. eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
  317. eap->init_for_reauth = eap_tls_init_for_reauth;
  318. eap->get_emsk = eap_tls_get_emsk;
  319. ret = eap_peer_method_register(eap);
  320. if (ret)
  321. eap_peer_method_free(eap);
  322. return ret;
  323. }
  324. #endif /* EAP_UNAUTH_TLS */
  325. #ifdef CONFIG_HS20
  326. int eap_peer_wfa_unauth_tls_register(void)
  327. {
  328. struct eap_method *eap;
  329. int ret;
  330. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  331. EAP_VENDOR_WFA_NEW,
  332. EAP_VENDOR_WFA_UNAUTH_TLS,
  333. "WFA-UNAUTH-TLS");
  334. if (eap == NULL)
  335. return -1;
  336. eap->init = eap_wfa_unauth_tls_init;
  337. eap->deinit = eap_tls_deinit;
  338. eap->process = eap_tls_process;
  339. eap->isKeyAvailable = eap_tls_isKeyAvailable;
  340. eap->getKey = eap_tls_getKey;
  341. eap->get_status = eap_tls_get_status;
  342. eap->has_reauth_data = eap_tls_has_reauth_data;
  343. eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
  344. eap->init_for_reauth = eap_tls_init_for_reauth;
  345. eap->get_emsk = eap_tls_get_emsk;
  346. ret = eap_peer_method_register(eap);
  347. if (ret)
  348. eap_peer_method_free(eap);
  349. return ret;
  350. }
  351. #endif /* CONFIG_HS20 */