eap_server_tls.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * hostapd / 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 "crypto/tls.h"
  19. static void eap_tls_reset(struct eap_sm *sm, void *priv);
  20. struct eap_tls_data {
  21. struct eap_ssl_data ssl;
  22. enum { START, CONTINUE, SUCCESS, FAILURE } state;
  23. int established;
  24. };
  25. static const char * eap_tls_state_txt(int state)
  26. {
  27. switch (state) {
  28. case START:
  29. return "START";
  30. case CONTINUE:
  31. return "CONTINUE";
  32. case SUCCESS:
  33. return "SUCCESS";
  34. case FAILURE:
  35. return "FAILURE";
  36. default:
  37. return "Unknown?!";
  38. }
  39. }
  40. static void eap_tls_state(struct eap_tls_data *data, int state)
  41. {
  42. wpa_printf(MSG_DEBUG, "EAP-TLS: %s -> %s",
  43. eap_tls_state_txt(data->state),
  44. eap_tls_state_txt(state));
  45. data->state = state;
  46. }
  47. static void * eap_tls_init(struct eap_sm *sm)
  48. {
  49. struct eap_tls_data *data;
  50. data = os_zalloc(sizeof(*data));
  51. if (data == NULL)
  52. return NULL;
  53. data->state = START;
  54. if (eap_server_tls_ssl_init(sm, &data->ssl, 1)) {
  55. wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
  56. eap_tls_reset(sm, data);
  57. return NULL;
  58. }
  59. return data;
  60. }
  61. static void eap_tls_reset(struct eap_sm *sm, void *priv)
  62. {
  63. struct eap_tls_data *data = priv;
  64. if (data == NULL)
  65. return;
  66. eap_server_tls_ssl_deinit(sm, &data->ssl);
  67. os_free(data);
  68. }
  69. static struct wpabuf * eap_tls_build_start(struct eap_sm *sm,
  70. struct eap_tls_data *data, u8 id)
  71. {
  72. struct wpabuf *req;
  73. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLS, 1, EAP_CODE_REQUEST,
  74. id);
  75. if (req == NULL) {
  76. wpa_printf(MSG_ERROR, "EAP-TLS: Failed to allocate memory for "
  77. "request");
  78. eap_tls_state(data, FAILURE);
  79. return NULL;
  80. }
  81. wpabuf_put_u8(req, EAP_TLS_FLAGS_START);
  82. eap_tls_state(data, CONTINUE);
  83. return req;
  84. }
  85. static struct wpabuf * eap_tls_buildReq(struct eap_sm *sm, void *priv, u8 id)
  86. {
  87. struct eap_tls_data *data = priv;
  88. struct wpabuf *res;
  89. if (data->ssl.state == FRAG_ACK) {
  90. return eap_server_tls_build_ack(id, EAP_TYPE_TLS, 0);
  91. }
  92. if (data->ssl.state == WAIT_FRAG_ACK) {
  93. res = eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TLS, 0,
  94. id);
  95. goto check_established;
  96. }
  97. switch (data->state) {
  98. case START:
  99. return eap_tls_build_start(sm, data, id);
  100. case CONTINUE:
  101. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn))
  102. data->established = 1;
  103. break;
  104. default:
  105. wpa_printf(MSG_DEBUG, "EAP-TLS: %s - unexpected state %d",
  106. __func__, data->state);
  107. return NULL;
  108. }
  109. res = eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TLS, 0, id);
  110. check_established:
  111. if (data->established && data->ssl.state != WAIT_FRAG_ACK) {
  112. /* TLS handshake has been completed and there are no more
  113. * fragments waiting to be sent out. */
  114. wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
  115. eap_tls_state(data, SUCCESS);
  116. }
  117. return res;
  118. }
  119. static Boolean eap_tls_check(struct eap_sm *sm, void *priv,
  120. struct wpabuf *respData)
  121. {
  122. const u8 *pos;
  123. size_t len;
  124. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLS, respData, &len);
  125. if (pos == NULL || len < 1) {
  126. wpa_printf(MSG_INFO, "EAP-TLS: Invalid frame");
  127. return TRUE;
  128. }
  129. return FALSE;
  130. }
  131. static void eap_tls_process_msg(struct eap_sm *sm, void *priv,
  132. const struct wpabuf *respData)
  133. {
  134. struct eap_tls_data *data = priv;
  135. if (data->state == SUCCESS && wpabuf_len(data->ssl.tls_in) == 0) {
  136. wpa_printf(MSG_DEBUG, "EAP-TLS: Client acknowledged final TLS "
  137. "handshake message");
  138. return;
  139. }
  140. if (eap_server_tls_phase1(sm, &data->ssl) < 0)
  141. eap_tls_state(data, FAILURE);
  142. }
  143. static void eap_tls_process(struct eap_sm *sm, void *priv,
  144. struct wpabuf *respData)
  145. {
  146. struct eap_tls_data *data = priv;
  147. if (eap_server_tls_process(sm, &data->ssl, respData, data,
  148. EAP_TYPE_TLS, NULL, eap_tls_process_msg) <
  149. 0)
  150. eap_tls_state(data, FAILURE);
  151. }
  152. static Boolean eap_tls_isDone(struct eap_sm *sm, void *priv)
  153. {
  154. struct eap_tls_data *data = priv;
  155. return data->state == SUCCESS || data->state == FAILURE;
  156. }
  157. static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  158. {
  159. struct eap_tls_data *data = priv;
  160. u8 *eapKeyData;
  161. if (data->state != SUCCESS)
  162. return NULL;
  163. eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
  164. "client EAP encryption",
  165. EAP_TLS_KEY_LEN);
  166. if (eapKeyData) {
  167. *len = EAP_TLS_KEY_LEN;
  168. wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived key",
  169. eapKeyData, EAP_TLS_KEY_LEN);
  170. } else {
  171. wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive key");
  172. }
  173. return eapKeyData;
  174. }
  175. static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  176. {
  177. struct eap_tls_data *data = priv;
  178. u8 *eapKeyData, *emsk;
  179. if (data->state != SUCCESS)
  180. return NULL;
  181. eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
  182. "client EAP encryption",
  183. EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
  184. if (eapKeyData) {
  185. emsk = os_malloc(EAP_EMSK_LEN);
  186. if (emsk)
  187. os_memcpy(emsk, eapKeyData + EAP_TLS_KEY_LEN,
  188. EAP_EMSK_LEN);
  189. os_free(eapKeyData);
  190. } else
  191. emsk = NULL;
  192. if (emsk) {
  193. *len = EAP_EMSK_LEN;
  194. wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived EMSK",
  195. emsk, EAP_EMSK_LEN);
  196. } else {
  197. wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive EMSK");
  198. }
  199. return emsk;
  200. }
  201. static Boolean eap_tls_isSuccess(struct eap_sm *sm, void *priv)
  202. {
  203. struct eap_tls_data *data = priv;
  204. return data->state == SUCCESS;
  205. }
  206. int eap_server_tls_register(void)
  207. {
  208. struct eap_method *eap;
  209. int ret;
  210. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  211. EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
  212. if (eap == NULL)
  213. return -1;
  214. eap->init = eap_tls_init;
  215. eap->reset = eap_tls_reset;
  216. eap->buildReq = eap_tls_buildReq;
  217. eap->check = eap_tls_check;
  218. eap->process = eap_tls_process;
  219. eap->isDone = eap_tls_isDone;
  220. eap->getKey = eap_tls_getKey;
  221. eap->isSuccess = eap_tls_isSuccess;
  222. eap->get_emsk = eap_tls_get_emsk;
  223. ret = eap_server_method_register(eap);
  224. if (ret)
  225. eap_server_method_free(eap);
  226. return ret;
  227. }