2
* hostapd / EAP-TLS (RFC 2716)
3
* Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
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.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
19
#include "eap_tls_common.h"
23
static void eap_tls_reset(struct eap_sm *sm, void *priv);
27
struct eap_ssl_data ssl;
28
enum { START, CONTINUE, SUCCESS, FAILURE } state;
32
static void * eap_tls_init(struct eap_sm *sm)
34
struct eap_tls_data *data;
36
data = os_zalloc(sizeof(*data));
41
if (eap_server_tls_ssl_init(sm, &data->ssl, 1)) {
42
wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
43
eap_tls_reset(sm, data);
51
static void eap_tls_reset(struct eap_sm *sm, void *priv)
53
struct eap_tls_data *data = priv;
56
eap_server_tls_ssl_deinit(sm, &data->ssl);
61
static struct wpabuf * eap_tls_build_start(struct eap_sm *sm,
62
struct eap_tls_data *data, u8 id)
66
req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLS, 1, EAP_CODE_REQUEST,
69
wpa_printf(MSG_ERROR, "EAP-TLS: Failed to allocate memory for "
71
data->state = FAILURE;
75
wpabuf_put_u8(req, EAP_TLS_FLAGS_START);
77
data->state = CONTINUE;
83
static struct wpabuf * eap_tls_build_req(struct eap_sm *sm,
84
struct eap_tls_data *data, u8 id)
89
res = eap_server_tls_buildReq_helper(sm, &data->ssl, EAP_TYPE_TLS, 0,
92
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
93
wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
94
data->state = SUCCESS;
98
return eap_server_tls_build_ack(id, EAP_TYPE_TLS, 0);
103
static struct wpabuf * eap_tls_buildReq(struct eap_sm *sm, void *priv, u8 id)
105
struct eap_tls_data *data = priv;
107
switch (data->state) {
109
return eap_tls_build_start(sm, data, id);
111
return eap_tls_build_req(sm, data, id);
113
wpa_printf(MSG_DEBUG, "EAP-TLS: %s - unexpected state %d",
114
__func__, data->state);
120
static Boolean eap_tls_check(struct eap_sm *sm, void *priv,
121
struct wpabuf *respData)
126
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLS, respData, &len);
127
if (pos == NULL || len < 1) {
128
wpa_printf(MSG_INFO, "EAP-TLS: Invalid frame");
136
static void eap_tls_process(struct eap_sm *sm, void *priv,
137
struct wpabuf *respData)
139
struct eap_tls_data *data = priv;
143
unsigned int tls_msg_len;
145
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLS, respData, &left);
146
if (pos == NULL || left < 1)
147
return; /* Should not happen - frame already validated */
151
wpa_printf(MSG_DEBUG, "EAP-TLS: Received packet(len=%lu) - "
152
"Flags 0x%02x", (unsigned long) wpabuf_len(respData),
154
if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
156
wpa_printf(MSG_INFO, "EAP-TLS: Short frame with TLS "
158
data->state = FAILURE;
161
tls_msg_len = WPA_GET_BE32(pos);
162
wpa_printf(MSG_DEBUG, "EAP-TLS: TLS Message Length: %d",
164
if (data->ssl.tls_in_left == 0) {
165
data->ssl.tls_in_total = tls_msg_len;
166
data->ssl.tls_in_left = tls_msg_len;
167
os_free(data->ssl.tls_in);
168
data->ssl.tls_in = NULL;
169
data->ssl.tls_in_len = 0;
175
if (eap_server_tls_process_helper(sm, &data->ssl, pos, left) < 0) {
176
wpa_printf(MSG_INFO, "EAP-TLS: TLS processing failed");
177
data->state = FAILURE;
181
if (tls_connection_get_write_alerts(sm->ssl_ctx, data->ssl.conn) > 1) {
182
wpa_printf(MSG_INFO, "EAP-TLS: Locally detected fatal error "
183
"in TLS processing");
184
data->state = FAILURE;
190
static Boolean eap_tls_isDone(struct eap_sm *sm, void *priv)
192
struct eap_tls_data *data = priv;
193
return data->state == SUCCESS || data->state == FAILURE;
197
static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
199
struct eap_tls_data *data = priv;
202
if (data->state != SUCCESS)
205
eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
206
"client EAP encryption",
209
*len = EAP_TLS_KEY_LEN;
210
wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived key",
211
eapKeyData, EAP_TLS_KEY_LEN);
213
wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive key");
220
static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
222
struct eap_tls_data *data = priv;
223
u8 *eapKeyData, *emsk;
225
if (data->state != SUCCESS)
228
eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
229
"client EAP encryption",
230
EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
232
emsk = os_malloc(EAP_EMSK_LEN);
234
os_memcpy(emsk, eapKeyData + EAP_TLS_KEY_LEN,
242
wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived EMSK",
245
wpa_printf(MSG_DEBUG, "EAP-TLS: Failed to derive EMSK");
252
static Boolean eap_tls_isSuccess(struct eap_sm *sm, void *priv)
254
struct eap_tls_data *data = priv;
255
return data->state == SUCCESS;
259
int eap_server_tls_register(void)
261
struct eap_method *eap;
264
eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
265
EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
269
eap->init = eap_tls_init;
270
eap->reset = eap_tls_reset;
271
eap->buildReq = eap_tls_buildReq;
272
eap->check = eap_tls_check;
273
eap->process = eap_tls_process;
274
eap->isDone = eap_tls_isDone;
275
eap->getKey = eap_tls_getKey;
276
eap->isSuccess = eap_tls_isSuccess;
277
eap->get_emsk = eap_tls_get_emsk;
279
ret = eap_server_method_register(eap);
281
eap_server_method_free(eap);