2 * hostapd / EAP-TLS/PEAP/TTLS common functions
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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.
18 #include <netinet/in.h>
23 #include "eap_tls_common.h"
28 int eap_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
32 data->phase2 = sm->init_phase2;
34 data->conn = tls_connection_init(sm->ssl_ctx);
35 if (data->conn == NULL) {
36 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
41 if (tls_connection_set_verify(sm->ssl_ctx, data->conn, verify_peer)) {
42 wpa_printf(MSG_INFO, "SSL: Failed to configure verification "
43 "of TLS peer certificate");
44 tls_connection_deinit(sm->ssl_ctx, data->conn);
49 /* TODO: make this configurable */
50 data->tls_out_limit = 1398;
52 /* Limit the fragment size in the inner TLS authentication
53 * since the outer authentication with EAP-PEAP does not yet
54 * support fragmentation */
55 if (data->tls_out_limit > 100)
56 data->tls_out_limit -= 100;
62 void eap_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
64 tls_connection_deinit(sm->ssl_ctx, data->conn);
70 u8 * eap_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
71 char *label, size_t len)
77 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
80 random = malloc(keys.client_random_len + keys.server_random_len);
81 if (out == NULL || random == NULL) {
86 memcpy(random, keys.client_random, keys.client_random_len);
87 memcpy(random + keys.client_random_len, keys.server_random,
88 keys.server_random_len);
90 if (tls_prf(keys.master_key, keys.master_key_len,
91 label, random, keys.client_random_len +
92 keys.server_random_len, out, len)) {
102 int eap_tls_data_reassemble(struct eap_sm *sm, struct eap_ssl_data *data,
103 u8 **in_data, size_t *in_len)
107 if (data->tls_in_left > *in_len || data->tls_in) {
108 buf = realloc(data->tls_in, data->tls_in_len + *in_len);
112 data->tls_in_len = 0;
113 wpa_printf(MSG_INFO, "SSL: Could not allocate memory "
117 memcpy(buf + data->tls_in_len, *in_data, *in_len);
119 data->tls_in_len += *in_len;
120 if (*in_len > data->tls_in_left) {
121 wpa_printf(MSG_INFO, "SSL: more data than TLS message "
123 data->tls_in_left = 0;
126 data->tls_in_left -= *in_len;
127 if (data->tls_in_left > 0) {
128 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
129 "data", (unsigned long) data->tls_in_left);
133 *in_data = data->tls_in;
134 *in_len = data->tls_in_len;
136 data->tls_in_left = 0;
142 int eap_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
143 u8 *in_data, size_t in_len)
145 WPA_ASSERT(data->tls_out_len == 0 || in_len == 0);
147 if (data->tls_out_len == 0) {
148 /* No more data to send out - expect to receive more data from
150 int res = eap_tls_data_reassemble(sm, data, &in_data, &in_len);
151 if (res < 0 || res == 1) {
152 wpa_printf(MSG_DEBUG, "SSL: data reassembly failed");
155 /* Full TLS message reassembled - continue handshake processing
158 /* This should not happen.. */
159 wpa_printf(MSG_INFO, "SSL: eap_tls_process_helper - "
160 "pending tls_out data even though "
163 WPA_ASSERT(data->tls_out == NULL);
165 data->tls_out = tls_connection_server_handshake(
166 sm->ssl_ctx, data->conn, in_data, in_len,
169 /* Clear reassembled input data (if the buffer was needed). */
170 data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
175 if (data->tls_out == NULL) {
176 wpa_printf(MSG_DEBUG, "SSL: failed to generate output data");
177 data->tls_out_len = 0;
180 if (data->tls_out_len == 0) {
181 /* TLS negotiation should now be complete since all other cases
182 * needing more that should have been catched above based on
183 * the TLS Message Length field. */
184 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
186 data->tls_out = NULL;
188 if (tls_connection_get_read_alerts(sm->ssl_ctx, data->conn)) {
189 wpa_printf(MSG_DEBUG, "SSL: Remote end sent a fatal "
190 "alert - abort handshake");
197 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
199 (unsigned long) data->tls_out_len - data->tls_out_pos,
200 (unsigned long) data->tls_out_len);
206 int eap_tls_buildReq_helper(struct eap_sm *sm, struct eap_ssl_data *data,
207 int eap_type, int peap_version, u8 id,
208 u8 **out_data, size_t *out_len)
216 req = malloc(sizeof(struct eap_hdr) + 2 + 4 + data->tls_out_limit);
221 req->code = EAP_CODE_REQUEST;
222 req->identifier = id;
223 pos = (u8 *) (req + 1);
226 *flags = peap_version;
227 if (data->tls_out_pos == 0 &&
228 data->tls_out_len > data->tls_out_limit) {
229 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
230 *pos++ = (data->tls_out_len >> 24) & 0xff;
231 *pos++ = (data->tls_out_len >> 16) & 0xff;
232 *pos++ = (data->tls_out_len >> 8) & 0xff;
233 *pos++ = data->tls_out_len & 0xff;
236 len = data->tls_out_len - data->tls_out_pos;
237 if (len > data->tls_out_limit) {
238 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
239 len = data->tls_out_limit;
240 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
241 "will follow", (unsigned long) len);
243 memcpy(pos, &data->tls_out[data->tls_out_pos], len);
244 data->tls_out_pos += len;
245 *out_len = (pos - (u8 *) req) + len;
246 req->length = htons(*out_len);
247 *out_data = (u8 *) req;
249 if (!(*flags & EAP_TLS_FLAGS_MORE_FRAGMENTS)) {
250 data->tls_out_len = 0;
251 data->tls_out_pos = 0;
253 data->tls_out = NULL;
260 u8 * eap_tls_build_ack(size_t *reqDataLen, u8 id, int eap_type,
266 *reqDataLen = sizeof(struct eap_hdr) + 2;
267 req = malloc(*reqDataLen);
270 wpa_printf(MSG_DEBUG, "SSL: Building ACK");
271 req->code = EAP_CODE_REQUEST;
272 req->identifier = id;
273 req->length = htons(*reqDataLen);
274 pos = (u8 *) (req + 1);
275 *pos++ = eap_type; /* Type */
276 *pos = peap_version; /* Flags */