Line data Source code
1 : /*
2 : * EAP peer method: EAP-TLS (RFC 2716)
3 : * Copyright (c) 2004-2008, 2012, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "crypto/tls.h"
13 : #include "eap_i.h"
14 : #include "eap_tls_common.h"
15 : #include "eap_config.h"
16 :
17 :
18 : static void eap_tls_deinit(struct eap_sm *sm, void *priv);
19 :
20 :
21 : struct eap_tls_data {
22 : struct eap_ssl_data ssl;
23 : u8 *key_data;
24 : u8 *session_id;
25 : size_t id_len;
26 : void *ssl_ctx;
27 : u8 eap_type;
28 : };
29 :
30 :
31 14 : static void * eap_tls_init(struct eap_sm *sm)
32 : {
33 : struct eap_tls_data *data;
34 14 : struct eap_peer_config *config = eap_get_config(sm);
35 29 : if (config == NULL ||
36 28 : ((sm->init_phase2 ? config->private_key2 : config->private_key)
37 13 : == NULL &&
38 0 : (sm->init_phase2 ? config->engine2 : config->engine) == 0)) {
39 0 : wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
40 0 : return NULL;
41 : }
42 :
43 14 : data = os_zalloc(sizeof(*data));
44 14 : if (data == NULL)
45 0 : return NULL;
46 :
47 14 : data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
48 : sm->ssl_ctx;
49 :
50 14 : if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TLS)) {
51 1 : wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
52 1 : eap_tls_deinit(sm, data);
53 1 : if (config->engine) {
54 0 : wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting Smartcard "
55 : "PIN");
56 0 : eap_sm_request_pin(sm);
57 0 : sm->ignore = TRUE;
58 1 : } else if (config->private_key && !config->private_key_passwd)
59 : {
60 1 : wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting private "
61 : "key passphrase");
62 1 : eap_sm_request_passphrase(sm);
63 1 : sm->ignore = TRUE;
64 : }
65 1 : return NULL;
66 : }
67 :
68 13 : data->eap_type = EAP_TYPE_TLS;
69 :
70 13 : return data;
71 : }
72 :
73 :
74 : #ifdef EAP_UNAUTH_TLS
75 1 : static void * eap_unauth_tls_init(struct eap_sm *sm)
76 : {
77 : struct eap_tls_data *data;
78 1 : struct eap_peer_config *config = eap_get_config(sm);
79 :
80 1 : data = os_zalloc(sizeof(*data));
81 1 : if (data == NULL)
82 0 : return NULL;
83 :
84 1 : data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
85 : sm->ssl_ctx;
86 :
87 1 : if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
88 : EAP_UNAUTH_TLS_TYPE)) {
89 0 : wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
90 0 : eap_tls_deinit(sm, data);
91 0 : return NULL;
92 : }
93 :
94 1 : data->eap_type = EAP_UNAUTH_TLS_TYPE;
95 :
96 1 : return data;
97 : }
98 : #endif /* EAP_UNAUTH_TLS */
99 :
100 :
101 : #ifdef CONFIG_HS20
102 2 : static void * eap_wfa_unauth_tls_init(struct eap_sm *sm)
103 : {
104 : struct eap_tls_data *data;
105 2 : struct eap_peer_config *config = eap_get_config(sm);
106 :
107 2 : data = os_zalloc(sizeof(*data));
108 2 : if (data == NULL)
109 0 : return NULL;
110 :
111 2 : data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
112 : sm->ssl_ctx;
113 :
114 2 : if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
115 : EAP_WFA_UNAUTH_TLS_TYPE)) {
116 0 : wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
117 0 : eap_tls_deinit(sm, data);
118 0 : return NULL;
119 : }
120 :
121 2 : data->eap_type = EAP_WFA_UNAUTH_TLS_TYPE;
122 :
123 2 : return data;
124 : }
125 : #endif /* CONFIG_HS20 */
126 :
127 :
128 17 : static void eap_tls_deinit(struct eap_sm *sm, void *priv)
129 : {
130 17 : struct eap_tls_data *data = priv;
131 17 : if (data == NULL)
132 17 : return;
133 17 : eap_peer_tls_ssl_deinit(sm, &data->ssl);
134 17 : os_free(data->key_data);
135 17 : os_free(data->session_id);
136 17 : os_free(data);
137 : }
138 :
139 :
140 3 : static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
141 : struct eap_tls_data *data,
142 : struct eap_method_ret *ret, int res,
143 : struct wpabuf *resp, u8 id)
144 : {
145 3 : wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
146 :
147 3 : ret->methodState = METHOD_DONE;
148 3 : ret->decision = DECISION_FAIL;
149 :
150 3 : if (res == -1) {
151 3 : struct eap_peer_config *config = eap_get_config(sm);
152 3 : if (config) {
153 : /*
154 : * The TLS handshake failed. So better forget the old
155 : * PIN. It may be wrong, we cannot be sure but trying
156 : * the wrong one again might block it on the card--so
157 : * better ask the user again.
158 : */
159 3 : os_free(config->pin);
160 3 : config->pin = NULL;
161 : }
162 : }
163 :
164 3 : if (resp) {
165 : /*
166 : * This is likely an alert message, so send it instead of just
167 : * ACKing the error.
168 : */
169 3 : return resp;
170 : }
171 :
172 0 : return eap_peer_tls_build_ack(id, data->eap_type, 0);
173 : }
174 :
175 :
176 16 : static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
177 : struct eap_method_ret *ret)
178 : {
179 16 : wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
180 :
181 16 : ret->methodState = METHOD_DONE;
182 16 : ret->decision = DECISION_UNCOND_SUCC;
183 :
184 16 : os_free(data->key_data);
185 16 : data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
186 : "client EAP encryption",
187 : EAP_TLS_KEY_LEN +
188 : EAP_EMSK_LEN);
189 16 : if (data->key_data) {
190 16 : wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
191 16 : data->key_data, EAP_TLS_KEY_LEN);
192 16 : wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
193 16 : data->key_data + EAP_TLS_KEY_LEN,
194 : EAP_EMSK_LEN);
195 : } else {
196 0 : wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
197 : }
198 :
199 16 : os_free(data->session_id);
200 16 : data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
201 : EAP_TYPE_TLS,
202 : &data->id_len);
203 16 : if (data->session_id) {
204 32 : wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived Session-Id",
205 16 : data->session_id, data->id_len);
206 : } else {
207 0 : wpa_printf(MSG_ERROR, "EAP-TLS: Failed to derive Session-Id");
208 : }
209 16 : }
210 :
211 :
212 81 : static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
213 : struct eap_method_ret *ret,
214 : const struct wpabuf *reqData)
215 : {
216 : size_t left;
217 : int res;
218 : struct wpabuf *resp;
219 : u8 flags, id;
220 : const u8 *pos;
221 81 : struct eap_tls_data *data = priv;
222 :
223 81 : pos = eap_peer_tls_process_init(sm, &data->ssl, data->eap_type, ret,
224 : reqData, &left, &flags);
225 81 : if (pos == NULL)
226 0 : return NULL;
227 81 : id = eap_get_id(reqData);
228 :
229 81 : if (flags & EAP_TLS_FLAGS_START) {
230 19 : wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
231 19 : left = 0; /* make sure that this frame is empty, even though it
232 : * should always be, anyway */
233 : }
234 :
235 81 : resp = NULL;
236 81 : res = eap_peer_tls_process_helper(sm, &data->ssl, data->eap_type, 0,
237 : id, pos, left, &resp);
238 :
239 81 : if (res < 0) {
240 3 : return eap_tls_failure(sm, data, ret, res, resp, id);
241 : }
242 :
243 78 : if (tls_connection_established(data->ssl_ctx, data->ssl.conn))
244 16 : eap_tls_success(sm, data, ret);
245 :
246 78 : if (res == 1) {
247 31 : wpabuf_free(resp);
248 31 : return eap_peer_tls_build_ack(id, data->eap_type, 0);
249 : }
250 :
251 47 : return resp;
252 : }
253 :
254 :
255 4 : static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
256 : {
257 4 : struct eap_tls_data *data = priv;
258 4 : return tls_connection_established(data->ssl_ctx, data->ssl.conn);
259 : }
260 :
261 :
262 2 : static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
263 : {
264 2 : }
265 :
266 :
267 3 : static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
268 : {
269 3 : struct eap_tls_data *data = priv;
270 3 : os_free(data->key_data);
271 3 : data->key_data = NULL;
272 3 : os_free(data->session_id);
273 3 : data->session_id = NULL;
274 3 : if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
275 0 : os_free(data);
276 0 : return NULL;
277 : }
278 3 : return priv;
279 : }
280 :
281 :
282 8 : static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
283 : size_t buflen, int verbose)
284 : {
285 8 : struct eap_tls_data *data = priv;
286 8 : return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
287 : }
288 :
289 :
290 71 : static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
291 : {
292 71 : struct eap_tls_data *data = priv;
293 71 : return data->key_data != NULL;
294 : }
295 :
296 :
297 14 : static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
298 : {
299 14 : struct eap_tls_data *data = priv;
300 : u8 *key;
301 :
302 14 : if (data->key_data == NULL)
303 0 : return NULL;
304 :
305 14 : key = os_malloc(EAP_TLS_KEY_LEN);
306 14 : if (key == NULL)
307 0 : return NULL;
308 :
309 14 : *len = EAP_TLS_KEY_LEN;
310 14 : os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
311 :
312 14 : return key;
313 : }
314 :
315 :
316 0 : static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
317 : {
318 0 : struct eap_tls_data *data = priv;
319 : u8 *key;
320 :
321 0 : if (data->key_data == NULL)
322 0 : return NULL;
323 :
324 0 : key = os_malloc(EAP_EMSK_LEN);
325 0 : if (key == NULL)
326 0 : return NULL;
327 :
328 0 : *len = EAP_EMSK_LEN;
329 0 : os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
330 :
331 0 : return key;
332 : }
333 :
334 :
335 10 : static u8 * eap_tls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
336 : {
337 10 : struct eap_tls_data *data = priv;
338 : u8 *id;
339 :
340 10 : if (data->session_id == NULL)
341 0 : return NULL;
342 :
343 10 : id = os_malloc(data->id_len);
344 10 : if (id == NULL)
345 0 : return NULL;
346 :
347 10 : *len = data->id_len;
348 10 : os_memcpy(id, data->session_id, data->id_len);
349 :
350 10 : return id;
351 : }
352 :
353 :
354 4 : int eap_peer_tls_register(void)
355 : {
356 : struct eap_method *eap;
357 : int ret;
358 :
359 4 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
360 : EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
361 4 : if (eap == NULL)
362 0 : return -1;
363 :
364 4 : eap->init = eap_tls_init;
365 4 : eap->deinit = eap_tls_deinit;
366 4 : eap->process = eap_tls_process;
367 4 : eap->isKeyAvailable = eap_tls_isKeyAvailable;
368 4 : eap->getKey = eap_tls_getKey;
369 4 : eap->getSessionId = eap_tls_get_session_id;
370 4 : eap->get_status = eap_tls_get_status;
371 4 : eap->has_reauth_data = eap_tls_has_reauth_data;
372 4 : eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
373 4 : eap->init_for_reauth = eap_tls_init_for_reauth;
374 4 : eap->get_emsk = eap_tls_get_emsk;
375 :
376 4 : ret = eap_peer_method_register(eap);
377 4 : if (ret)
378 0 : eap_peer_method_free(eap);
379 4 : return ret;
380 : }
381 :
382 :
383 : #ifdef EAP_UNAUTH_TLS
384 4 : int eap_peer_unauth_tls_register(void)
385 : {
386 : struct eap_method *eap;
387 : int ret;
388 :
389 4 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
390 : EAP_VENDOR_UNAUTH_TLS,
391 : EAP_VENDOR_TYPE_UNAUTH_TLS, "UNAUTH-TLS");
392 4 : if (eap == NULL)
393 0 : return -1;
394 :
395 4 : eap->init = eap_unauth_tls_init;
396 4 : eap->deinit = eap_tls_deinit;
397 4 : eap->process = eap_tls_process;
398 4 : eap->isKeyAvailable = eap_tls_isKeyAvailable;
399 4 : eap->getKey = eap_tls_getKey;
400 4 : eap->get_status = eap_tls_get_status;
401 4 : eap->has_reauth_data = eap_tls_has_reauth_data;
402 4 : eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
403 4 : eap->init_for_reauth = eap_tls_init_for_reauth;
404 4 : eap->get_emsk = eap_tls_get_emsk;
405 :
406 4 : ret = eap_peer_method_register(eap);
407 4 : if (ret)
408 0 : eap_peer_method_free(eap);
409 4 : return ret;
410 : }
411 : #endif /* EAP_UNAUTH_TLS */
412 :
413 :
414 : #ifdef CONFIG_HS20
415 4 : int eap_peer_wfa_unauth_tls_register(void)
416 : {
417 : struct eap_method *eap;
418 : int ret;
419 :
420 4 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
421 : EAP_VENDOR_WFA_NEW,
422 : EAP_VENDOR_WFA_UNAUTH_TLS,
423 : "WFA-UNAUTH-TLS");
424 4 : if (eap == NULL)
425 0 : return -1;
426 :
427 4 : eap->init = eap_wfa_unauth_tls_init;
428 4 : eap->deinit = eap_tls_deinit;
429 4 : eap->process = eap_tls_process;
430 4 : eap->isKeyAvailable = eap_tls_isKeyAvailable;
431 4 : eap->getKey = eap_tls_getKey;
432 4 : eap->get_status = eap_tls_get_status;
433 4 : eap->has_reauth_data = eap_tls_has_reauth_data;
434 4 : eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
435 4 : eap->init_for_reauth = eap_tls_init_for_reauth;
436 4 : eap->get_emsk = eap_tls_get_emsk;
437 :
438 4 : ret = eap_peer_method_register(eap);
439 4 : if (ret)
440 0 : eap_peer_method_free(eap);
441 4 : return ret;
442 : }
443 : #endif /* CONFIG_HS20 */
|