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.
20 #include "eap_tls_common.h"
24 static void eap_tls_reset(struct eap_sm
*sm
, void *priv
);
28 struct eap_ssl_data ssl
;
29 enum { START
, CONTINUE
, SUCCESS
, FAILURE
} state
;
33 static void * eap_tls_init(struct eap_sm
*sm
)
35 struct eap_tls_data
*data
;
37 data
= wpa_zalloc(sizeof(*data
));
42 if (eap_tls_ssl_init(sm
, &data
->ssl
, 1)) {
43 wpa_printf(MSG_INFO
, "EAP-TLS: Failed to initialize SSL.");
44 eap_tls_reset(sm
, data
);
52 static void eap_tls_reset(struct eap_sm
*sm
, void *priv
)
54 struct eap_tls_data
*data
= priv
;
57 eap_tls_ssl_deinit(sm
, &data
->ssl
);
62 static u8
* eap_tls_build_start(struct eap_sm
*sm
, struct eap_tls_data
*data
,
63 int id
, size_t *reqDataLen
)
68 *reqDataLen
= sizeof(*req
) + 2;
69 req
= malloc(*reqDataLen
);
71 wpa_printf(MSG_ERROR
, "EAP-TLS: Failed to allocate memory for "
73 data
->state
= FAILURE
;
77 req
->code
= EAP_CODE_REQUEST
;
79 req
->length
= htons(*reqDataLen
);
80 pos
= (u8
*) (req
+ 1);
81 *pos
++ = EAP_TYPE_TLS
;
82 *pos
= EAP_TLS_FLAGS_START
;
84 data
->state
= CONTINUE
;
90 static u8
* eap_tls_build_req(struct eap_sm
*sm
, struct eap_tls_data
*data
,
91 int id
, size_t *reqDataLen
)
96 res
= eap_tls_buildReq_helper(sm
, &data
->ssl
, EAP_TYPE_TLS
, 0, id
,
99 if (tls_connection_established(sm
->ssl_ctx
, data
->ssl
.conn
)) {
100 wpa_printf(MSG_DEBUG
, "EAP-TLS: Done");
101 data
->state
= SUCCESS
;
105 return eap_tls_build_ack(reqDataLen
, id
, EAP_TYPE_TLS
, 0);
110 static u8
* eap_tls_buildReq(struct eap_sm
*sm
, void *priv
, int id
,
113 struct eap_tls_data
*data
= priv
;
115 switch (data
->state
) {
117 return eap_tls_build_start(sm
, data
, id
, reqDataLen
);
119 return eap_tls_build_req(sm
, data
, id
, reqDataLen
);
121 wpa_printf(MSG_DEBUG
, "EAP-TLS: %s - unexpected state %d",
122 __func__
, data
->state
);
128 static Boolean
eap_tls_check(struct eap_sm
*sm
, void *priv
,
129 u8
*respData
, size_t respDataLen
)
131 struct eap_hdr
*resp
;
134 resp
= (struct eap_hdr
*) respData
;
135 pos
= (u8
*) (resp
+ 1);
136 if (respDataLen
< sizeof(*resp
) + 2 || *pos
!= EAP_TYPE_TLS
||
137 (ntohs(resp
->length
)) > respDataLen
) {
138 wpa_printf(MSG_INFO
, "EAP-TLS: Invalid frame");
146 static void eap_tls_process(struct eap_sm
*sm
, void *priv
,
147 u8
*respData
, size_t respDataLen
)
149 struct eap_tls_data
*data
= priv
;
150 struct eap_hdr
*resp
;
153 unsigned int tls_msg_len
;
155 resp
= (struct eap_hdr
*) respData
;
156 pos
= (u8
*) (resp
+ 1);
159 left
= htons(resp
->length
) - sizeof(struct eap_hdr
) - 2;
160 wpa_printf(MSG_DEBUG
, "EAP-TLS: Received packet(len=%lu) - "
161 "Flags 0x%02x", (unsigned long) respDataLen
, flags
);
162 if (flags
& EAP_TLS_FLAGS_LENGTH_INCLUDED
) {
164 wpa_printf(MSG_INFO
, "EAP-TLS: Short frame with TLS "
166 data
->state
= FAILURE
;
169 tls_msg_len
= (pos
[0] << 24) | (pos
[1] << 16) | (pos
[2] << 8) |
171 wpa_printf(MSG_DEBUG
, "EAP-TLS: TLS Message Length: %d",
173 if (data
->ssl
.tls_in_left
== 0) {
174 data
->ssl
.tls_in_total
= tls_msg_len
;
175 data
->ssl
.tls_in_left
= tls_msg_len
;
176 free(data
->ssl
.tls_in
);
177 data
->ssl
.tls_in
= NULL
;
178 data
->ssl
.tls_in_len
= 0;
184 if (eap_tls_process_helper(sm
, &data
->ssl
, pos
, left
) < 0) {
185 wpa_printf(MSG_INFO
, "EAP-TLS: TLS processing failed");
186 data
->state
= FAILURE
;
190 if (tls_connection_get_write_alerts(sm
->ssl_ctx
, data
->ssl
.conn
) > 1) {
191 wpa_printf(MSG_INFO
, "EAP-TLS: Locally detected fatal error "
192 "in TLS processing");
193 data
->state
= FAILURE
;
199 static Boolean
eap_tls_isDone(struct eap_sm
*sm
, void *priv
)
201 struct eap_tls_data
*data
= priv
;
202 return data
->state
== SUCCESS
|| data
->state
== FAILURE
;
206 static u8
* eap_tls_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
208 struct eap_tls_data
*data
= priv
;
211 if (data
->state
!= SUCCESS
)
214 eapKeyData
= eap_tls_derive_key(sm
, &data
->ssl
,
215 "client EAP encryption",
218 *len
= EAP_TLS_KEY_LEN
;
219 wpa_hexdump(MSG_DEBUG
, "EAP-TLS: Derived key",
220 eapKeyData
, EAP_TLS_KEY_LEN
);
222 wpa_printf(MSG_DEBUG
, "EAP-TLS: Failed to derive key");
229 static u8
* eap_tls_get_emsk(struct eap_sm
*sm
, void *priv
, size_t *len
)
231 struct eap_tls_data
*data
= priv
;
232 u8
*eapKeyData
, *emsk
;
234 if (data
->state
!= SUCCESS
)
237 eapKeyData
= eap_tls_derive_key(sm
, &data
->ssl
,
238 "client EAP encryption",
239 EAP_TLS_KEY_LEN
+ EAP_EMSK_LEN
);
241 emsk
= malloc(EAP_EMSK_LEN
);
243 memcpy(emsk
, eapKeyData
+ EAP_TLS_KEY_LEN
,
251 wpa_hexdump(MSG_DEBUG
, "EAP-TLS: Derived EMSK",
254 wpa_printf(MSG_DEBUG
, "EAP-TLS: Failed to derive EMSK");
261 static Boolean
eap_tls_isSuccess(struct eap_sm
*sm
, void *priv
)
263 struct eap_tls_data
*data
= priv
;
264 return data
->state
== SUCCESS
;
268 int eap_server_tls_register(void)
270 struct eap_method
*eap
;
273 eap
= eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION
,
274 EAP_VENDOR_IETF
, EAP_TYPE_TLS
, "TLS");
278 eap
->init
= eap_tls_init
;
279 eap
->reset
= eap_tls_reset
;
280 eap
->buildReq
= eap_tls_buildReq
;
281 eap
->check
= eap_tls_check
;
282 eap
->process
= eap_tls_process
;
283 eap
->isDone
= eap_tls_isDone
;
284 eap
->getKey
= eap_tls_getKey
;
285 eap
->isSuccess
= eap_tls_isSuccess
;
286 eap
->get_emsk
= eap_tls_get_emsk
;
288 ret
= eap_server_method_register(eap
);
290 eap_server_method_free(eap
);