eap_tls.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * EAP peer method: EAP-TLS (RFC 2716)
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eap_i.h"
  17. #include "eap_tls_common.h"
  18. #include "eap_config.h"
  19. #include "tls.h"
  20. static void eap_tls_deinit(struct eap_sm *sm, void *priv);
  21. struct eap_tls_data {
  22. struct eap_ssl_data ssl;
  23. u8 *key_data;
  24. };
  25. static void * eap_tls_init(struct eap_sm *sm)
  26. {
  27. struct eap_tls_data *data;
  28. struct eap_peer_config *config = eap_get_config(sm);
  29. if (config == NULL ||
  30. ((sm->init_phase2 ? config->private_key2 : config->private_key)
  31. == NULL &&
  32. (sm->init_phase2 ? config->engine2 : config->engine) == 0)) {
  33. wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
  34. return NULL;
  35. }
  36. data = os_zalloc(sizeof(*data));
  37. if (data == NULL)
  38. return NULL;
  39. if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
  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. return data;
  57. }
  58. static void eap_tls_deinit(struct eap_sm *sm, void *priv)
  59. {
  60. struct eap_tls_data *data = priv;
  61. if (data == NULL)
  62. return;
  63. eap_peer_tls_ssl_deinit(sm, &data->ssl);
  64. os_free(data->key_data);
  65. os_free(data);
  66. }
  67. static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
  68. struct eap_tls_data *data,
  69. struct eap_method_ret *ret, int res,
  70. struct wpabuf *resp, u8 id)
  71. {
  72. wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
  73. ret->methodState = METHOD_DONE;
  74. ret->decision = DECISION_FAIL;
  75. if (res == -1) {
  76. struct eap_peer_config *config = eap_get_config(sm);
  77. if (config) {
  78. /*
  79. * The TLS handshake failed. So better forget the old
  80. * PIN. It may be wrong, we cannot be sure but trying
  81. * the wrong one again might block it on the card--so
  82. * better ask the user again.
  83. */
  84. os_free(config->pin);
  85. config->pin = NULL;
  86. }
  87. }
  88. if (resp) {
  89. /*
  90. * This is likely an alert message, so send it instead of just
  91. * ACKing the error.
  92. */
  93. return resp;
  94. }
  95. return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
  96. }
  97. static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
  98. struct eap_method_ret *ret)
  99. {
  100. wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
  101. ret->methodState = METHOD_DONE;
  102. ret->decision = DECISION_UNCOND_SUCC;
  103. os_free(data->key_data);
  104. data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
  105. "client EAP encryption",
  106. EAP_TLS_KEY_LEN +
  107. EAP_EMSK_LEN);
  108. if (data->key_data) {
  109. wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
  110. data->key_data, EAP_TLS_KEY_LEN);
  111. wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
  112. data->key_data + EAP_TLS_KEY_LEN,
  113. EAP_EMSK_LEN);
  114. } else {
  115. wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
  116. }
  117. }
  118. static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
  119. struct eap_method_ret *ret,
  120. const struct wpabuf *reqData)
  121. {
  122. size_t left;
  123. int res;
  124. struct wpabuf *resp;
  125. u8 flags, id;
  126. const u8 *pos;
  127. struct eap_tls_data *data = priv;
  128. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TLS, ret,
  129. reqData, &left, &flags);
  130. if (pos == NULL)
  131. return NULL;
  132. id = eap_get_id(reqData);
  133. if (flags & EAP_TLS_FLAGS_START) {
  134. wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
  135. left = 0; /* make sure that this frame is empty, even though it
  136. * should always be, anyway */
  137. }
  138. resp = NULL;
  139. res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TLS, 0, id,
  140. pos, left, &resp);
  141. if (res < 0) {
  142. return eap_tls_failure(sm, data, ret, res, resp, id);
  143. }
  144. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn))
  145. eap_tls_success(sm, data, ret);
  146. if (res == 1) {
  147. wpabuf_free(resp);
  148. return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
  149. }
  150. return resp;
  151. }
  152. static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
  153. {
  154. struct eap_tls_data *data = priv;
  155. return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
  156. }
  157. static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
  158. {
  159. }
  160. static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
  161. {
  162. struct eap_tls_data *data = priv;
  163. os_free(data->key_data);
  164. data->key_data = NULL;
  165. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  166. os_free(data);
  167. return NULL;
  168. }
  169. return priv;
  170. }
  171. static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
  172. size_t buflen, int verbose)
  173. {
  174. struct eap_tls_data *data = priv;
  175. return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  176. }
  177. static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
  178. {
  179. struct eap_tls_data *data = priv;
  180. return data->key_data != NULL;
  181. }
  182. static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  183. {
  184. struct eap_tls_data *data = priv;
  185. u8 *key;
  186. if (data->key_data == NULL)
  187. return NULL;
  188. key = os_malloc(EAP_TLS_KEY_LEN);
  189. if (key == NULL)
  190. return NULL;
  191. *len = EAP_TLS_KEY_LEN;
  192. os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
  193. return key;
  194. }
  195. static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  196. {
  197. struct eap_tls_data *data = priv;
  198. u8 *key;
  199. if (data->key_data == NULL)
  200. return NULL;
  201. key = os_malloc(EAP_EMSK_LEN);
  202. if (key == NULL)
  203. return NULL;
  204. *len = EAP_EMSK_LEN;
  205. os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
  206. return key;
  207. }
  208. int eap_peer_tls_register(void)
  209. {
  210. struct eap_method *eap;
  211. int ret;
  212. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  213. EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
  214. if (eap == NULL)
  215. return -1;
  216. eap->init = eap_tls_init;
  217. eap->deinit = eap_tls_deinit;
  218. eap->process = eap_tls_process;
  219. eap->isKeyAvailable = eap_tls_isKeyAvailable;
  220. eap->getKey = eap_tls_getKey;
  221. eap->get_status = eap_tls_get_status;
  222. eap->has_reauth_data = eap_tls_has_reauth_data;
  223. eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
  224. eap->init_for_reauth = eap_tls_init_for_reauth;
  225. eap->get_emsk = eap_tls_get_emsk;
  226. ret = eap_peer_method_register(eap);
  227. if (ret)
  228. eap_peer_method_free(eap);
  229. return ret;
  230. }