2008-02-27 20:34:43 -05:00
|
|
|
/*
|
2009-12-20 11:17:55 -05:00
|
|
|
* EAP-TLS/PEAP/TTLS/FAST server common functions
|
|
|
|
* Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
|
2008-02-27 20:34:43 -05:00
|
|
|
*
|
2012-02-11 09:46:35 -05:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-27 20:34:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
2009-11-29 16:04:43 -05:00
|
|
|
#include "crypto/sha1.h"
|
|
|
|
#include "crypto/tls.h"
|
2008-02-27 20:34:43 -05:00
|
|
|
#include "eap_i.h"
|
|
|
|
#include "eap_tls_common.h"
|
|
|
|
|
|
|
|
|
2010-02-12 14:13:51 -05:00
|
|
|
static void eap_server_tls_free_in_buf(struct eap_ssl_data *data);
|
|
|
|
|
|
|
|
|
2012-08-22 15:34:11 -04:00
|
|
|
struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
|
|
|
|
u8 code, u8 identifier)
|
|
|
|
{
|
|
|
|
if (type == EAP_UNAUTH_TLS_TYPE)
|
|
|
|
return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
|
|
|
|
EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
|
|
|
|
code, identifier);
|
2013-07-23 14:22:38 -04:00
|
|
|
else if (type == EAP_WFA_UNAUTH_TLS_TYPE)
|
|
|
|
return eap_msg_alloc(EAP_VENDOR_WFA_NEW,
|
|
|
|
EAP_VENDOR_WFA_UNAUTH_TLS, payload_len,
|
|
|
|
code, identifier);
|
2012-08-22 15:34:11 -04:00
|
|
|
return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
|
|
|
|
identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-01 17:07:50 -05:00
|
|
|
#ifdef CONFIG_TLS_INTERNAL
|
|
|
|
static void eap_server_tls_log_cb(void *ctx, const char *msg)
|
|
|
|
{
|
|
|
|
struct eap_sm *sm = ctx;
|
|
|
|
eap_log_msg(sm, "TLS: %s", msg);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_TLS_INTERNAL */
|
|
|
|
|
|
|
|
|
2008-02-27 20:34:43 -05:00
|
|
|
int eap_server_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
|
2015-08-23 14:22:22 -04:00
|
|
|
int verify_peer, int eap_type)
|
2008-02-27 20:34:43 -05:00
|
|
|
{
|
2015-08-23 15:05:14 -04:00
|
|
|
u8 session_ctx[8];
|
2015-08-23 14:11:01 -04:00
|
|
|
unsigned int flags = 0;
|
|
|
|
|
2013-11-24 11:01:12 -05:00
|
|
|
if (sm->ssl_ctx == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "TLS context not initialized - cannot use TLS-based EAP method");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-27 20:34:43 -05:00
|
|
|
data->eap = sm;
|
|
|
|
data->phase2 = sm->init_phase2;
|
|
|
|
|
|
|
|
data->conn = tls_connection_init(sm->ssl_ctx);
|
|
|
|
if (data->conn == NULL) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
|
|
|
|
"connection");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-01 17:07:50 -05:00
|
|
|
#ifdef CONFIG_TLS_INTERNAL
|
|
|
|
tls_connection_set_log_cb(data->conn, eap_server_tls_log_cb, sm);
|
2014-03-01 17:43:59 -05:00
|
|
|
#ifdef CONFIG_TESTING_OPTIONS
|
|
|
|
tls_connection_set_test_flags(data->conn, sm->tls_test_flags);
|
|
|
|
#endif /* CONFIG_TESTING_OPTIONS */
|
2014-03-01 17:07:50 -05:00
|
|
|
#endif /* CONFIG_TLS_INTERNAL */
|
|
|
|
|
2015-08-23 14:22:22 -04:00
|
|
|
if (eap_type != EAP_TYPE_FAST)
|
|
|
|
flags |= TLS_CONN_DISABLE_SESSION_TICKET;
|
2015-08-23 15:05:14 -04:00
|
|
|
os_memcpy(session_ctx, "hostapd", 7);
|
|
|
|
session_ctx[7] = (u8) eap_type;
|
2015-08-23 14:11:01 -04:00
|
|
|
if (tls_connection_set_verify(sm->ssl_ctx, data->conn, verify_peer,
|
2015-08-23 15:05:14 -04:00
|
|
|
flags, session_ctx,
|
|
|
|
sizeof(session_ctx))) {
|
2008-02-27 20:34:43 -05:00
|
|
|
wpa_printf(MSG_INFO, "SSL: Failed to configure verification "
|
|
|
|
"of TLS peer certificate");
|
|
|
|
tls_connection_deinit(sm->ssl_ctx, data->conn);
|
|
|
|
data->conn = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:28:53 -04:00
|
|
|
data->tls_out_limit = sm->fragment_size > 0 ? sm->fragment_size : 1398;
|
2008-02-27 20:34:43 -05:00
|
|
|
if (data->phase2) {
|
|
|
|
/* Limit the fragment size in the inner TLS authentication
|
|
|
|
* since the outer authentication with EAP-PEAP does not yet
|
|
|
|
* support fragmentation */
|
|
|
|
if (data->tls_out_limit > 100)
|
|
|
|
data->tls_out_limit -= 100;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void eap_server_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
|
|
|
|
{
|
|
|
|
tls_connection_deinit(sm->ssl_ctx, data->conn);
|
2010-02-12 14:13:51 -05:00
|
|
|
eap_server_tls_free_in_buf(data);
|
|
|
|
wpabuf_free(data->tls_out);
|
|
|
|
data->tls_out = NULL;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u8 * eap_server_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
|
|
|
|
char *label, size_t len)
|
|
|
|
{
|
2015-03-31 08:47:32 -04:00
|
|
|
u8 *out;
|
2008-02-27 20:34:43 -05:00
|
|
|
|
|
|
|
out = os_malloc(len);
|
|
|
|
if (out == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-05-16 11:47:37 -04:00
|
|
|
if (tls_connection_export_key(sm->ssl_ctx, data->conn, label, out,
|
|
|
|
len)) {
|
2015-03-31 08:47:32 -04:00
|
|
|
os_free(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-27 20:34:43 -05:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-29 16:46:45 -05:00
|
|
|
/**
|
|
|
|
* eap_server_tls_derive_session_id - Derive a Session-Id based on TLS data
|
|
|
|
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
|
|
|
|
* @data: Data for TLS processing
|
|
|
|
* @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
|
|
|
|
* @len: Pointer to length of the session ID generated
|
|
|
|
* Returns: Pointer to allocated Session-Id on success or %NULL on failure
|
|
|
|
*
|
|
|
|
* This function derive the Session-Id based on the TLS session data
|
|
|
|
* (client/server random and method type).
|
|
|
|
*
|
|
|
|
* The caller is responsible for freeing the returned buffer.
|
|
|
|
*/
|
|
|
|
u8 * eap_server_tls_derive_session_id(struct eap_sm *sm,
|
|
|
|
struct eap_ssl_data *data, u8 eap_type,
|
|
|
|
size_t *len)
|
|
|
|
{
|
2015-08-01 11:17:14 -04:00
|
|
|
struct tls_random keys;
|
2014-11-29 16:46:45 -05:00
|
|
|
u8 *out;
|
|
|
|
|
2015-08-01 11:17:14 -04:00
|
|
|
if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys))
|
2014-11-29 16:46:45 -05:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (keys.client_random == NULL || keys.server_random == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*len = 1 + keys.client_random_len + keys.server_random_len;
|
|
|
|
out = os_malloc(*len);
|
|
|
|
if (out == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Session-Id = EAP type || client.random || server.random */
|
|
|
|
out[0] = eap_type;
|
|
|
|
os_memcpy(out + 1, keys.client_random, keys.client_random_len);
|
|
|
|
os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
|
|
|
|
keys.server_random_len);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
struct wpabuf * eap_server_tls_build_msg(struct eap_ssl_data *data,
|
2008-05-28 02:59:55 -04:00
|
|
|
int eap_type, int version, u8 id)
|
2008-02-27 20:34:43 -05:00
|
|
|
{
|
2008-05-28 02:57:17 -04:00
|
|
|
struct wpabuf *req;
|
|
|
|
u8 flags;
|
|
|
|
size_t send_len, plen;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Generating Request");
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_out == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "SSL: tls_out NULL in %s", __func__);
|
2008-06-05 13:44:30 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-28 02:57:17 -04:00
|
|
|
|
2008-05-28 02:59:55 -04:00
|
|
|
flags = version;
|
2009-12-23 17:16:58 -05:00
|
|
|
send_len = wpabuf_len(data->tls_out) - data->tls_out_pos;
|
2008-05-28 02:57:17 -04:00
|
|
|
if (1 + send_len > data->tls_out_limit) {
|
|
|
|
send_len = data->tls_out_limit - 1;
|
|
|
|
flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_out_pos == 0) {
|
2008-05-28 02:57:17 -04:00
|
|
|
flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
|
|
|
|
send_len -= 4;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
2008-05-28 02:57:17 -04:00
|
|
|
}
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
plen = 1 + send_len;
|
|
|
|
if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)
|
|
|
|
plen += 4;
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2012-08-22 15:34:11 -04:00
|
|
|
req = eap_tls_msg_alloc(eap_type, plen, EAP_CODE_REQUEST, id);
|
2008-05-28 02:57:17 -04:00
|
|
|
if (req == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wpabuf_put_u8(req, flags); /* Flags */
|
|
|
|
if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_put_be32(req, wpabuf_len(data->tls_out));
|
2008-05-28 02:57:17 -04:00
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_put_data(req, wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
|
2008-05-28 02:57:17 -04:00
|
|
|
send_len);
|
2009-12-23 17:16:58 -05:00
|
|
|
data->tls_out_pos += send_len;
|
2008-05-28 02:57:17 -04:00
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_out_pos == wpabuf_len(data->tls_out)) {
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Sending out %lu bytes "
|
|
|
|
"(message sent completely)",
|
|
|
|
(unsigned long) send_len);
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_free(data->tls_out);
|
|
|
|
data->tls_out = NULL;
|
|
|
|
data->tls_out_pos = 0;
|
2008-05-28 02:57:17 -04:00
|
|
|
data->state = MSG;
|
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Sending out %lu bytes "
|
|
|
|
"(%lu more to send)", (unsigned long) send_len,
|
2009-12-23 17:16:58 -05:00
|
|
|
(unsigned long) wpabuf_len(data->tls_out) -
|
|
|
|
data->tls_out_pos);
|
2008-05-28 02:57:17 -04:00
|
|
|
data->state = WAIT_FRAG_ACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 02:59:55 -04:00
|
|
|
struct wpabuf * eap_server_tls_build_ack(u8 id, int eap_type, int version)
|
2008-02-27 20:34:43 -05:00
|
|
|
{
|
2008-05-28 02:57:17 -04:00
|
|
|
struct wpabuf *req;
|
|
|
|
|
2012-08-22 15:34:11 -04:00
|
|
|
req = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_REQUEST, id);
|
2008-05-28 02:57:17 -04:00
|
|
|
if (req == NULL)
|
|
|
|
return NULL;
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Building ACK");
|
2008-05-28 02:59:55 -04:00
|
|
|
wpabuf_put_u8(req, version); /* Flags */
|
2008-05-28 02:57:17 -04:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int eap_server_tls_process_cont(struct eap_ssl_data *data,
|
|
|
|
const u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
/* Process continuation of a pending message */
|
2009-12-23 17:16:58 -05:00
|
|
|
if (len > wpabuf_tailroom(data->tls_in)) {
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Fragment overflow");
|
|
|
|
return -1;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_put_data(data->tls_in, buf, len);
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Received %lu bytes, waiting for %lu "
|
|
|
|
"bytes more", (unsigned long) len,
|
2009-12-23 17:16:58 -05:00
|
|
|
(unsigned long) wpabuf_tailroom(data->tls_in));
|
2008-05-28 02:57:17 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int eap_server_tls_process_fragment(struct eap_ssl_data *data,
|
|
|
|
u8 flags, u32 message_length,
|
|
|
|
const u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
/* Process a fragment that is not the last one of the message */
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_in == NULL && !(flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)) {
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: No Message Length field in a "
|
|
|
|
"fragmented packet");
|
2008-02-27 20:34:43 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2008-05-28 02:57:17 -04:00
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_in == NULL) {
|
2008-05-28 02:57:17 -04:00
|
|
|
/* First fragment of the message */
|
|
|
|
|
|
|
|
/* Limit length to avoid rogue peers from causing large
|
|
|
|
* memory allocations. */
|
|
|
|
if (message_length > 65536) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size"
|
|
|
|
" over 64 kB)");
|
2008-02-27 20:34:43 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-07 13:06:29 -04:00
|
|
|
if (len > message_length) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: Too much data (%d bytes) in "
|
|
|
|
"first fragment of frame (TLS Message "
|
|
|
|
"Length %d bytes)",
|
|
|
|
(int) len, (int) message_length);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
data->tls_in = wpabuf_alloc(message_length);
|
|
|
|
if (data->tls_in == NULL) {
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: No memory for message");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_put_data(data->tls_in, buf, len);
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Received %lu bytes in first "
|
|
|
|
"fragment, waiting for %lu bytes more",
|
|
|
|
(unsigned long) len,
|
2009-12-23 17:16:58 -05:00
|
|
|
(unsigned long) wpabuf_tailroom(data->tls_in));
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
int eap_server_tls_phase1(struct eap_sm *sm, struct eap_ssl_data *data)
|
|
|
|
{
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_out) {
|
2008-05-28 02:57:17 -04:00
|
|
|
/* This should not happen.. */
|
|
|
|
wpa_printf(MSG_INFO, "SSL: pending tls_out data when "
|
|
|
|
"processing new message");
|
2009-12-23 17:16:58 -05:00
|
|
|
wpabuf_free(data->tls_out);
|
|
|
|
WPA_ASSERT(data->tls_out == NULL);
|
2008-05-28 02:57:17 -04:00
|
|
|
}
|
2009-12-20 11:17:55 -05:00
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
data->tls_out = tls_connection_server_handshake(sm->ssl_ctx,
|
2009-12-20 11:17:55 -05:00
|
|
|
data->conn,
|
2009-12-23 17:16:58 -05:00
|
|
|
data->tls_in, NULL);
|
|
|
|
if (data->tls_out == NULL) {
|
2009-12-20 11:17:55 -05:00
|
|
|
wpa_printf(MSG_INFO, "SSL: TLS processing failed");
|
2008-05-28 02:57:17 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-20 12:11:43 -05:00
|
|
|
if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
|
|
|
|
/* TLS processing has failed - return error */
|
2009-12-23 17:16:58 -05:00
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
|
2009-12-20 12:11:43 -05:00
|
|
|
"report error");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-20 11:17:55 -05:00
|
|
|
|
2008-02-27 20:34:43 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 10:15:17 -04:00
|
|
|
static int eap_server_tls_reassemble(struct eap_ssl_data *data, u8 flags,
|
|
|
|
const u8 **pos, size_t *left)
|
2008-02-27 20:34:43 -05:00
|
|
|
{
|
2008-05-28 02:57:17 -04:00
|
|
|
unsigned int tls_msg_len = 0;
|
|
|
|
const u8 *end = *pos + *left;
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
|
|
|
|
if (*left < 4) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
|
|
|
|
"length");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tls_msg_len = WPA_GET_BE32(*pos);
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
|
|
|
|
tls_msg_len);
|
|
|
|
*pos += 4;
|
|
|
|
*left -= 4;
|
2012-10-07 13:18:10 -04:00
|
|
|
|
|
|
|
if (*left > tls_msg_len) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
|
|
|
|
"bytes) smaller than this fragment (%d "
|
|
|
|
"bytes)", (int) tls_msg_len, (int) *left);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
2008-05-28 02:57:17 -04:00
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Received packet: Flags 0x%x "
|
|
|
|
"Message Length %u", flags, tls_msg_len);
|
|
|
|
|
|
|
|
if (data->state == WAIT_FRAG_ACK) {
|
|
|
|
if (*left != 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Unexpected payload in "
|
|
|
|
"WAIT_FRAG_ACK state");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Fragment acknowledged");
|
|
|
|
return 1;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_in &&
|
2008-05-28 02:57:17 -04:00
|
|
|
eap_server_tls_process_cont(data, *pos, end - *pos) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (flags & EAP_TLS_FLAGS_MORE_FRAGMENTS) {
|
|
|
|
if (eap_server_tls_process_fragment(data, flags, tls_msg_len,
|
|
|
|
*pos, end - *pos) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data->state = FRAG_ACK;
|
|
|
|
return 1;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
if (data->state == FRAG_ACK) {
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: All fragments received");
|
|
|
|
data->state = MSG;
|
|
|
|
}
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_in == NULL) {
|
2008-05-28 02:57:17 -04:00
|
|
|
/* Wrap unfragmented messages as wpabuf without extra copy */
|
|
|
|
wpabuf_set(&data->tmpbuf, *pos, end - *pos);
|
2009-12-23 17:16:58 -05:00
|
|
|
data->tls_in = &data->tmpbuf;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 10:15:17 -04:00
|
|
|
static void eap_server_tls_free_in_buf(struct eap_ssl_data *data)
|
2008-02-27 20:34:43 -05:00
|
|
|
{
|
2009-12-23 17:16:58 -05:00
|
|
|
if (data->tls_in != &data->tmpbuf)
|
|
|
|
wpabuf_free(data->tls_in);
|
|
|
|
data->tls_in = NULL;
|
2008-05-28 02:57:17 -04:00
|
|
|
}
|
2008-02-27 20:34:43 -05:00
|
|
|
|
2008-05-28 02:57:17 -04:00
|
|
|
|
|
|
|
struct wpabuf * eap_server_tls_encrypt(struct eap_sm *sm,
|
|
|
|
struct eap_ssl_data *data,
|
2009-12-20 11:17:55 -05:00
|
|
|
const struct wpabuf *plain)
|
2008-05-28 02:57:17 -04:00
|
|
|
{
|
|
|
|
struct wpabuf *buf;
|
|
|
|
|
2009-12-20 11:17:55 -05:00
|
|
|
buf = tls_connection_encrypt(sm->ssl_ctx, data->conn,
|
|
|
|
plain);
|
|
|
|
if (buf == NULL) {
|
2008-05-28 02:57:17 -04:00
|
|
|
wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 data");
|
2008-02-27 20:34:43 -05:00
|
|
|
return NULL;
|
2008-05-28 02:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
2008-02-27 20:34:43 -05:00
|
|
|
}
|
2008-05-28 10:15:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
int eap_server_tls_process(struct eap_sm *sm, struct eap_ssl_data *data,
|
|
|
|
struct wpabuf *respData, void *priv, int eap_type,
|
|
|
|
int (*proc_version)(struct eap_sm *sm, void *priv,
|
|
|
|
int peer_version),
|
|
|
|
void (*proc_msg)(struct eap_sm *sm, void *priv,
|
|
|
|
const struct wpabuf *respData))
|
|
|
|
{
|
|
|
|
const u8 *pos;
|
|
|
|
u8 flags;
|
|
|
|
size_t left;
|
|
|
|
int ret, res = 0;
|
|
|
|
|
2012-08-22 15:34:11 -04:00
|
|
|
if (eap_type == EAP_UNAUTH_TLS_TYPE)
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
|
|
|
|
EAP_VENDOR_TYPE_UNAUTH_TLS, respData,
|
|
|
|
&left);
|
2013-07-23 14:22:38 -04:00
|
|
|
else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
|
|
|
|
EAP_VENDOR_WFA_UNAUTH_TLS, respData,
|
|
|
|
&left);
|
2012-08-22 15:34:11 -04:00
|
|
|
else
|
|
|
|
pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, respData,
|
|
|
|
&left);
|
2008-05-28 10:15:17 -04:00
|
|
|
if (pos == NULL || left < 1)
|
|
|
|
return 0; /* Should not happen - frame already validated */
|
|
|
|
flags = *pos++;
|
|
|
|
left--;
|
|
|
|
wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - Flags 0x%02x",
|
|
|
|
(unsigned long) wpabuf_len(respData), flags);
|
|
|
|
|
|
|
|
if (proc_version &&
|
|
|
|
proc_version(sm, priv, flags & EAP_TLS_VERSION_MASK) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = eap_server_tls_reassemble(data, flags, &pos, &left);
|
|
|
|
if (ret < 0) {
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
} else if (ret == 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (proc_msg)
|
|
|
|
proc_msg(sm, priv, respData);
|
|
|
|
|
|
|
|
if (tls_connection_get_write_alerts(sm->ssl_ctx, data->conn) > 1) {
|
|
|
|
wpa_printf(MSG_INFO, "SSL: Locally detected fatal error in "
|
|
|
|
"TLS processing");
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
eap_server_tls_free_in_buf(data);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|