More minor IPI work.
[dragonfly/vkernel-mp.git] / contrib / hostapd-0.4.9 / eap_tls_common.c
blobd573064b616b9ade1bcfa8b57602381fab91ac4e
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <netinet/in.h>
20 #include "hostapd.h"
21 #include "common.h"
22 #include "eap_i.h"
23 #include "eap_tls_common.h"
24 #include "sha1.h"
25 #include "tls.h"
28 int eap_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
29 int verify_peer)
31 data->eap = sm;
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 "
37 "connection");
38 return -1;
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);
45 data->conn = NULL;
46 return -1;
49 /* TODO: make this configurable */
50 data->tls_out_limit = 1398;
51 if (data->phase2) {
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;
58 return 0;
62 void eap_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
64 tls_connection_deinit(sm->ssl_ctx, data->conn);
65 free(data->tls_in);
66 free(data->tls_out);
70 u8 * eap_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
71 char *label, size_t len)
73 struct tls_keys keys;
74 u8 *random;
75 u8 *out;
77 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
78 return NULL;
79 out = malloc(len);
80 random = malloc(keys.client_random_len + keys.server_random_len);
81 if (out == NULL || random == NULL) {
82 free(out);
83 free(random);
84 return 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)) {
93 free(random);
94 free(out);
95 return NULL;
97 free(random);
98 return out;
102 int eap_tls_data_reassemble(struct eap_sm *sm, struct eap_ssl_data *data,
103 u8 **in_data, size_t *in_len)
105 u8 *buf;
107 if (data->tls_in_left > *in_len || data->tls_in) {
108 buf = realloc(data->tls_in, data->tls_in_len + *in_len);
109 if (buf == NULL) {
110 free(data->tls_in);
111 data->tls_in = NULL;
112 data->tls_in_len = 0;
113 wpa_printf(MSG_INFO, "SSL: Could not allocate memory "
114 "for TLS data");
115 return -1;
117 memcpy(buf + data->tls_in_len, *in_data, *in_len);
118 data->tls_in = buf;
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 "
122 "length indicated");
123 data->tls_in_left = 0;
124 return -1;
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);
130 return 1;
133 *in_data = data->tls_in;
134 *in_len = data->tls_in_len;
135 } else
136 data->tls_in_left = 0;
138 return 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
149 * the peer. */
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");
153 return res;
155 /* Full TLS message reassembled - continue handshake processing
157 if (data->tls_out) {
158 /* This should not happen.. */
159 wpa_printf(MSG_INFO, "SSL: eap_tls_process_helper - "
160 "pending tls_out data even though "
161 "tls_out_len = 0");
162 free(data->tls_out);
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,
167 &data->tls_out_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;
171 free(data->tls_in);
172 data->tls_in = NULL;
175 if (data->tls_out == NULL) {
176 wpa_printf(MSG_DEBUG, "SSL: failed to generate output data");
177 data->tls_out_len = 0;
178 return -1;
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");
185 free(data->tls_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");
191 return -1;
194 return 1;
197 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
198 "%lu bytes)",
199 (unsigned long) data->tls_out_len - data->tls_out_pos,
200 (unsigned long) data->tls_out_len);
202 return 0;
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)
210 size_t len;
211 u8 *pos, *flags;
212 struct eap_hdr *req;
214 *out_len = 0;
216 req = malloc(sizeof(struct eap_hdr) + 2 + 4 + data->tls_out_limit);
217 if (req == NULL) {
218 *out_data = NULL;
219 return -1;
221 req->code = EAP_CODE_REQUEST;
222 req->identifier = id;
223 pos = (u8 *) (req + 1);
224 *pos++ = eap_type;
225 flags = pos++;
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;
252 free(data->tls_out);
253 data->tls_out = NULL;
256 return 0;
260 u8 * eap_tls_build_ack(size_t *reqDataLen, u8 id, int eap_type,
261 int peap_version)
263 struct eap_hdr *req;
264 u8 *pos;
266 *reqDataLen = sizeof(struct eap_hdr) + 2;
267 req = malloc(*reqDataLen);
268 if (req == NULL)
269 return NULL;
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 */
277 return (u8 *) req;