2 * hostapd / EAP-TLS/PEAP/TTLS common functions
3 * Copyright (c) 2004-2006, 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"
25 int eap_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
29 data->phase2 = sm->init_phase2;
31 data->conn = tls_connection_init(sm->ssl_ctx);
32 if (data->conn == NULL) {
33 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
38 if (tls_connection_set_verify(sm->ssl_ctx, data->conn, verify_peer)) {
39 wpa_printf(MSG_INFO, "SSL: Failed to configure verification "
40 "of TLS peer certificate");
41 tls_connection_deinit(sm->ssl_ctx, data->conn);
46 /* TODO: make this configurable */
47 data->tls_out_limit = 1398;
49 /* Limit the fragment size in the inner TLS authentication
50 * since the outer authentication with EAP-PEAP does not yet
51 * support fragmentation */
52 if (data->tls_out_limit > 100)
53 data->tls_out_limit -= 100;
59 void eap_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
61 tls_connection_deinit(sm->ssl_ctx, data->conn);
67 u8 * eap_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
68 char *label, size_t len)
77 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
81 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
84 if (keys.client_random == NULL || keys.server_random == NULL ||
85 keys.master_key == NULL)
88 rnd = malloc(keys.client_random_len + keys.server_random_len);
91 memcpy(rnd, keys.client_random, keys.client_random_len);
92 memcpy(rnd + keys.client_random_len, keys.server_random,
93 keys.server_random_len);
95 if (tls_prf(keys.master_key, keys.master_key_len,
96 label, rnd, keys.client_random_len +
97 keys.server_random_len, out, len))
110 int eap_tls_data_reassemble(struct eap_sm *sm, struct eap_ssl_data *data,
111 u8 **in_data, size_t *in_len)
115 if (data->tls_in_left > *in_len || data->tls_in) {
117 wpa_printf(MSG_INFO, "SSL: Empty fragment when trying "
121 if (data->tls_in_len + *in_len > 65536) {
122 /* Limit length to avoid rogue peers from causing large
123 * memory allocations. */
126 data->tls_in_len = 0;
127 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size"
131 buf = realloc(data->tls_in, data->tls_in_len + *in_len);
135 data->tls_in_len = 0;
136 wpa_printf(MSG_INFO, "SSL: Could not allocate memory "
140 memcpy(buf + data->tls_in_len, *in_data, *in_len);
142 data->tls_in_len += *in_len;
143 if (*in_len > data->tls_in_left) {
144 wpa_printf(MSG_INFO, "SSL: more data than TLS message "
146 data->tls_in_left = 0;
149 data->tls_in_left -= *in_len;
150 if (data->tls_in_left > 0) {
151 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
152 "data", (unsigned long) data->tls_in_left);
156 *in_data = data->tls_in;
157 *in_len = data->tls_in_len;
159 data->tls_in_left = 0;
165 int eap_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
166 u8 *in_data, size_t in_len)
168 WPA_ASSERT(data->tls_out_len == 0 || in_len == 0);
170 if (data->tls_out_len == 0) {
171 /* No more data to send out - expect to receive more data from
173 int res = eap_tls_data_reassemble(sm, data, &in_data, &in_len);
174 if (res < 0 || res == 1) {
175 wpa_printf(MSG_DEBUG, "SSL: data reassembly failed");
178 /* Full TLS message reassembled - continue handshake processing
181 /* This should not happen.. */
182 wpa_printf(MSG_INFO, "SSL: eap_tls_process_helper - "
183 "pending tls_out data even though "
186 WPA_ASSERT(data->tls_out == NULL);
188 data->tls_out = tls_connection_server_handshake(
189 sm->ssl_ctx, data->conn, in_data, in_len,
192 /* Clear reassembled input data (if the buffer was needed). */
193 data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
198 if (data->tls_out == NULL) {
199 wpa_printf(MSG_DEBUG, "SSL: failed to generate output data");
200 data->tls_out_len = 0;
203 if (data->tls_out_len == 0) {
204 /* TLS negotiation should now be complete since all other cases
205 * needing more that should have been catched above based on
206 * the TLS Message Length field. */
207 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
209 data->tls_out = NULL;
211 if (tls_connection_get_read_alerts(sm->ssl_ctx, data->conn)) {
212 wpa_printf(MSG_DEBUG, "SSL: Remote end sent a fatal "
213 "alert - abort handshake");
220 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
222 (unsigned long) data->tls_out_len - data->tls_out_pos,
223 (unsigned long) data->tls_out_len);
229 int eap_tls_buildReq_helper(struct eap_sm *sm, struct eap_ssl_data *data,
230 int eap_type, int peap_version, u8 id,
231 u8 **out_data, size_t *out_len)
239 req = malloc(sizeof(struct eap_hdr) + 2 + 4 + data->tls_out_limit);
244 req->code = EAP_CODE_REQUEST;
245 req->identifier = id;
246 pos = (u8 *) (req + 1);
249 *flags = peap_version;
250 if (data->tls_out_pos == 0 &&
251 data->tls_out_len > data->tls_out_limit) {
252 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
253 *pos++ = (data->tls_out_len >> 24) & 0xff;
254 *pos++ = (data->tls_out_len >> 16) & 0xff;
255 *pos++ = (data->tls_out_len >> 8) & 0xff;
256 *pos++ = data->tls_out_len & 0xff;
259 len = data->tls_out_len - data->tls_out_pos;
260 if (len > data->tls_out_limit) {
261 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
262 len = data->tls_out_limit;
263 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
264 "will follow", (unsigned long) len);
266 memcpy(pos, &data->tls_out[data->tls_out_pos], len);
267 data->tls_out_pos += len;
268 *out_len = (pos - (u8 *) req) + len;
269 req->length = htons(*out_len);
270 *out_data = (u8 *) req;
272 if (!(*flags & EAP_TLS_FLAGS_MORE_FRAGMENTS)) {
273 data->tls_out_len = 0;
274 data->tls_out_pos = 0;
276 data->tls_out = NULL;
283 u8 * eap_tls_build_ack(size_t *reqDataLen, u8 id, int eap_type,
289 *reqDataLen = sizeof(struct eap_hdr) + 2;
290 req = malloc(*reqDataLen);
293 wpa_printf(MSG_DEBUG, "SSL: Building ACK");
294 req->code = EAP_CODE_REQUEST;
295 req->identifier = id;
296 req->length = htons(*reqDataLen);
297 pos = (u8 *) (req + 1);
298 *pos++ = eap_type; /* Type */
299 *pos = peap_version; /* Flags */