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
) {
116 if (data
->tls_in_len
+ *in_len
> 65536) {
117 /* Limit length to avoid rogue peers from causing large
118 * memory allocations. */
121 data
->tls_in_len
= 0;
122 wpa_printf(MSG_INFO
, "SSL: Too long TLS fragment (size"
126 buf
= realloc(data
->tls_in
, data
->tls_in_len
+ *in_len
);
130 data
->tls_in_len
= 0;
131 wpa_printf(MSG_INFO
, "SSL: Could not allocate memory "
135 memcpy(buf
+ data
->tls_in_len
, *in_data
, *in_len
);
137 data
->tls_in_len
+= *in_len
;
138 if (*in_len
> data
->tls_in_left
) {
139 wpa_printf(MSG_INFO
, "SSL: more data than TLS message "
141 data
->tls_in_left
= 0;
144 data
->tls_in_left
-= *in_len
;
145 if (data
->tls_in_left
> 0) {
146 wpa_printf(MSG_DEBUG
, "SSL: Need %lu bytes more input "
147 "data", (unsigned long) data
->tls_in_left
);
151 *in_data
= data
->tls_in
;
152 *in_len
= data
->tls_in_len
;
154 data
->tls_in_left
= 0;
160 int eap_tls_process_helper(struct eap_sm
*sm
, struct eap_ssl_data
*data
,
161 u8
*in_data
, size_t in_len
)
163 WPA_ASSERT(data
->tls_out_len
== 0 || in_len
== 0);
165 if (data
->tls_out_len
== 0) {
166 /* No more data to send out - expect to receive more data from
168 int res
= eap_tls_data_reassemble(sm
, data
, &in_data
, &in_len
);
169 if (res
< 0 || res
== 1) {
170 wpa_printf(MSG_DEBUG
, "SSL: data reassembly failed");
173 /* Full TLS message reassembled - continue handshake processing
176 /* This should not happen.. */
177 wpa_printf(MSG_INFO
, "SSL: eap_tls_process_helper - "
178 "pending tls_out data even though "
181 WPA_ASSERT(data
->tls_out
== NULL
);
183 data
->tls_out
= tls_connection_server_handshake(
184 sm
->ssl_ctx
, data
->conn
, in_data
, in_len
,
187 /* Clear reassembled input data (if the buffer was needed). */
188 data
->tls_in_left
= data
->tls_in_total
= data
->tls_in_len
= 0;
193 if (data
->tls_out
== NULL
) {
194 wpa_printf(MSG_DEBUG
, "SSL: failed to generate output data");
195 data
->tls_out_len
= 0;
198 if (data
->tls_out_len
== 0) {
199 /* TLS negotiation should now be complete since all other cases
200 * needing more that should have been catched above based on
201 * the TLS Message Length field. */
202 wpa_printf(MSG_DEBUG
, "SSL: No data to be sent out");
204 data
->tls_out
= NULL
;
206 if (tls_connection_get_read_alerts(sm
->ssl_ctx
, data
->conn
)) {
207 wpa_printf(MSG_DEBUG
, "SSL: Remote end sent a fatal "
208 "alert - abort handshake");
215 wpa_printf(MSG_DEBUG
, "SSL: %lu bytes left to be sent out (of total "
217 (unsigned long) data
->tls_out_len
- data
->tls_out_pos
,
218 (unsigned long) data
->tls_out_len
);
224 int eap_tls_buildReq_helper(struct eap_sm
*sm
, struct eap_ssl_data
*data
,
225 int eap_type
, int peap_version
, u8 id
,
226 u8
**out_data
, size_t *out_len
)
234 req
= malloc(sizeof(struct eap_hdr
) + 2 + 4 + data
->tls_out_limit
);
239 req
->code
= EAP_CODE_REQUEST
;
240 req
->identifier
= id
;
241 pos
= (u8
*) (req
+ 1);
244 *flags
= peap_version
;
245 if (data
->tls_out_pos
== 0 &&
246 data
->tls_out_len
> data
->tls_out_limit
) {
247 *flags
|= EAP_TLS_FLAGS_LENGTH_INCLUDED
;
248 *pos
++ = (data
->tls_out_len
>> 24) & 0xff;
249 *pos
++ = (data
->tls_out_len
>> 16) & 0xff;
250 *pos
++ = (data
->tls_out_len
>> 8) & 0xff;
251 *pos
++ = data
->tls_out_len
& 0xff;
254 len
= data
->tls_out_len
- data
->tls_out_pos
;
255 if (len
> data
->tls_out_limit
) {
256 *flags
|= EAP_TLS_FLAGS_MORE_FRAGMENTS
;
257 len
= data
->tls_out_limit
;
258 wpa_printf(MSG_DEBUG
, "SSL: sending %lu bytes, more fragments "
259 "will follow", (unsigned long) len
);
261 memcpy(pos
, &data
->tls_out
[data
->tls_out_pos
], len
);
262 data
->tls_out_pos
+= len
;
263 *out_len
= (pos
- (u8
*) req
) + len
;
264 req
->length
= htons(*out_len
);
265 *out_data
= (u8
*) req
;
267 if (!(*flags
& EAP_TLS_FLAGS_MORE_FRAGMENTS
)) {
268 data
->tls_out_len
= 0;
269 data
->tls_out_pos
= 0;
271 data
->tls_out
= NULL
;
278 u8
* eap_tls_build_ack(size_t *reqDataLen
, u8 id
, int eap_type
,
284 *reqDataLen
= sizeof(struct eap_hdr
) + 2;
285 req
= malloc(*reqDataLen
);
288 wpa_printf(MSG_DEBUG
, "SSL: Building ACK");
289 req
->code
= EAP_CODE_REQUEST
;
290 req
->identifier
= id
;
291 req
->length
= htons(*reqDataLen
);
292 pos
= (u8
*) (req
+ 1);
293 *pos
++ = eap_type
; /* Type */
294 *pos
= peap_version
; /* Flags */