1 /*
2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2013, 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 "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "crypto/sha1.h"
13 #include "crypto/tls.h"
14 #include "eap_peer/eap_i.h"
15 #include "eap_peer/eap_tls_common.h"
16 #include "eap_peer/eap_config.h"
17 #include "eap_peer/eap_methods.h"
18
eap_tls_msg_alloc(EapType type,size_t payload_len,u8 code,u8 identifier)19 static struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
20 u8 code, u8 identifier)
21 {
22 if (type == EAP_UNAUTH_TLS_TYPE)
23 return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
24 EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
25 code, identifier);
26 return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
27 identifier);
28 }
29
30
eap_tls_check_blob(struct eap_sm * sm,const char ** name,const u8 ** data,size_t * data_len)31 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
32 const u8 **data, size_t *data_len)
33 {
34 const struct wpa_config_blob *blob;
35
36 if (*name == NULL)// || os_strncmp(*name, "blob://", 7) != 0)
37 return 0;
38
39 blob = eap_get_config_blob(sm, *name);// + 7);
40 if (blob == NULL) {
41 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
42 "found", __func__, *name);// + 7);
43 return -1;
44 }
45
46 *name = NULL;
47 *data = blob->data;
48 *data_len = blob->len;
49
50 return 0;
51 }
52
53
eap_tls_params_flags(struct tls_connection_params * params,const char * txt)54 static void eap_tls_params_flags(struct tls_connection_params *params,
55 const char *txt)
56 {
57 if (txt == NULL)
58 return;
59 if (os_strstr(txt, "tls_allow_md5=1"))
60 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
61 if (os_strstr(txt, "tls_disable_time_checks=1"))
62 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
63 if (os_strstr(txt, "tls_disable_session_ticket=1"))
64 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
65 if (os_strstr(txt, "tls_disable_session_ticket=0"))
66 params->flags &= ~TLS_CONN_DISABLE_SESSION_TICKET;
67 }
68
eap_tls_params_from_conf1(struct tls_connection_params * params,struct eap_peer_config * config)69 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
70 struct eap_peer_config *config)
71 {
72 params->ca_cert = (char *) config->ca_cert;
73 params->ca_path = (char *) config->ca_path;
74 params->client_cert = (char *) config->client_cert;
75 params->private_key = (char *) config->private_key;
76 params->private_key_passwd = (char *) config->private_key_passwd;
77 eap_tls_params_flags(params, config->phase1);
78 if (wifi_sta_get_enterprise_disable_time_check())
79 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
80 else
81 params->flags &= (~TLS_CONN_DISABLE_TIME_CHECKS);
82
83 if (config->flags & TLS_CONN_SUITEB)
84 params->flags |= TLS_CONN_SUITEB;
85 else
86 params->flags &= (~TLS_CONN_SUITEB);
87
88 if (config->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE)
89 params->flags |= TLS_CONN_USE_DEFAULT_CERT_BUNDLE;
90 else
91 params->flags &= (~TLS_CONN_USE_DEFAULT_CERT_BUNDLE);
92 }
93
eap_tls_params_from_conf(struct eap_sm * sm,struct eap_ssl_data * data,struct tls_connection_params * params,struct eap_peer_config * config,int phase2)94 static int eap_tls_params_from_conf(struct eap_sm *sm,
95 struct eap_ssl_data *data,
96 struct tls_connection_params *params,
97 struct eap_peer_config *config, int phase2)
98 {
99 os_memset(params, 0, sizeof(*params));
100 if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
101 /*
102 * Some deployed authentication servers seem to be unable to
103 * handle the TLS Session Ticket extension (they are supposed
104 * to ignore unrecognized TLS extensions, but end up rejecting
105 * the ClientHello instead). As a workaround, disable use of
106 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
107 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
108 * supports EAP-FAST does not need this workaround).
109 */
110 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
111 }
112
113 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
114 eap_tls_params_from_conf1(params, config);
115 if (data->eap_type == EAP_TYPE_FAST) {
116 wpa_printf(MSG_DEBUG, "EAP-TYPE == EAP-FAST #####################################");
117 params->flags |= TLS_CONN_EAP_FAST;
118 }
119
120 /*
121 * Use blob data, if available. Otherwise, leave reference to external
122 * file as-is.
123 */
124 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
125 ¶ms->ca_cert_blob_len) ||
126 eap_tls_check_blob(sm, ¶ms->client_cert,
127 ¶ms->client_cert_blob,
128 ¶ms->client_cert_blob_len) ||
129 eap_tls_check_blob(sm, ¶ms->private_key,
130 ¶ms->private_key_blob,
131 ¶ms->private_key_blob_len)) {
132 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
133 return -1;
134 }
135
136 if (!phase2)
137 data->client_cert_conf = params->client_cert ||
138 params->client_cert_blob ||
139 params->private_key ||
140 params->private_key_blob;
141
142 return 0;
143 }
144
145
eap_tls_init_connection(struct eap_sm * sm,struct eap_ssl_data * data,struct eap_peer_config * config,struct tls_connection_params * params)146 static int eap_tls_init_connection(struct eap_sm *sm,
147 struct eap_ssl_data *data,
148 struct eap_peer_config *config,
149 struct tls_connection_params *params)
150 {
151 int res;
152
153 if (config->ocsp)
154 params->flags |= TLS_CONN_REQUEST_OCSP;
155 if (config->ocsp == 2)
156 params->flags |= TLS_CONN_REQUIRE_OCSP;
157 data->conn = tls_connection_init(data->ssl_ctx);
158 if (data->conn == NULL) {
159 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
160 "connection");
161 return -1;
162 }
163
164 res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
165
166 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
167 /*
168 * At this point with the pkcs11 engine the PIN might be wrong.
169 * We reset the PIN in the configuration to be sure to not use
170 * it again and the calling function must request a new one.
171 */
172 os_free(config->pin);
173 config->pin = NULL;
174 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
175 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
176 /*
177 * We do not know exactly but maybe the PIN was wrong,
178 * so ask for a new one.
179 */
180 os_free(config->pin);
181 config->pin = NULL;
182 tls_connection_deinit(data->ssl_ctx, data->conn);
183 data->conn = NULL;
184 return -1;
185 } else if (res) {
186 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
187 "parameters");
188 tls_connection_deinit(data->ssl_ctx, data->conn);
189 data->conn = NULL;
190 return -1;
191 }
192
193 return 0;
194 }
195
196
197 /**
198 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
199 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
200 * @data: Data for TLS processing
201 * @config: Pointer to the network configuration
202 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
203 * Returns: 0 on success, -1 on failure
204 *
205 * This function is used to initialize shared TLS functionality for EAP-TLS,
206 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
207 */
eap_peer_tls_ssl_init(struct eap_sm * sm,struct eap_ssl_data * data,struct eap_peer_config * config,u8 eap_type)208 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
209 struct eap_peer_config *config, u8 eap_type)
210 {
211 struct tls_connection_params params;
212
213 if (config == NULL)
214 return -1;
215
216 data->eap = sm;
217 data->eap_type = eap_type;
218 data->ssl_ctx = sm->ssl_ctx;
219 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) < 0) /* no phase2 */
220 return -1;
221
222 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
223 return -1;
224
225 data->tls_out_limit = config->fragment_size;
226
227 if (config->phase1 &&
228 os_strstr(config->phase1, "include_tls_length=1")) {
229 wpa_printf(MSG_INFO, "TLS: Include TLS Message Length in "
230 "unfragmented packets");
231 data->include_tls_length = 1;
232 }
233
234 return 0;
235 }
236
237
238 /**
239 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
240 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
241 * @data: Data for TLS processing
242 *
243 * This function deinitializes shared TLS functionality that was initialized
244 * with eap_peer_tls_ssl_init().
245 */
eap_peer_tls_ssl_deinit(struct eap_sm * sm,struct eap_ssl_data * data)246 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
247 {
248 tls_connection_deinit(data->ssl_ctx, data->conn);
249 eap_peer_tls_reset_input(data);
250 eap_peer_tls_reset_output(data);
251 }
252
253
254 /**
255 * eap_peer_tls_derive_key - Derive a key based on TLS session data
256 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
257 * @data: Data for TLS processing
258 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
259 * @len: Length of the key material to generate (usually 64 for MSK)
260 * Returns: Pointer to allocated key on success or %NULL on failure
261 *
262 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
263 * session data (client/server random and master key). Each key type may use a
264 * different label to bind the key usage into the generated material.
265 *
266 * The caller is responsible for freeing the returned buffer.
267 */
eap_peer_tls_derive_key(struct eap_sm * sm,struct eap_ssl_data * data,const char * label,size_t len)268 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
269 const char *label, size_t len)
270 {
271 u8 *out;
272
273 out = os_malloc(len);
274 if (out == NULL)
275 return NULL;
276
277 if (tls_connection_export_key(data->ssl_ctx, data->conn, label, 0, 0, out,
278 len)) {
279 os_free(out);
280 return NULL;
281 }
282
283 return out;
284 }
285
286 /**
287 * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
288 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
289 * @data: Data for TLS processing
290 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
291 * @len: Pointer to length of the session ID generated
292 * Returns: Pointer to allocated Session-Id on success or %NULL on failure
293 *
294 * This function derive the Session-Id based on the TLS session data
295 * (client/server random and method type).
296 *
297 * The caller is responsible for freeing the returned buffer.
298 */
eap_peer_tls_derive_session_id(struct eap_sm * sm,struct eap_ssl_data * data,u8 eap_type,size_t * len)299 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
300 struct eap_ssl_data *data, u8 eap_type,
301 size_t *len)
302 {
303 struct tls_random keys;
304 u8 *out;
305
306 /*
307 * TLS library did not support session ID generation,
308 * so get the needed TLS session parameters
309 */
310 if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys))
311 return NULL;
312
313 if (keys.client_random == NULL || keys.server_random == NULL)
314 return NULL;
315
316 *len = 1 + keys.client_random_len + keys.server_random_len;
317 out = os_malloc(*len);
318 if (out == NULL)
319 return NULL;
320
321 /* Session-Id = EAP type || client.random || server.random */
322 out[0] = eap_type;
323 os_memcpy(out + 1, keys.client_random, keys.client_random_len);
324 os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
325 keys.server_random_len);
326
327 return out;
328 }
329
330
331 /**
332 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
333 * @data: Data for TLS processing
334 * @in_data: Next incoming TLS segment
335 * Returns: 0 on success, 1 if more data is needed for the full message, or
336 * -1 on error
337 */
eap_peer_tls_reassemble_fragment(struct eap_ssl_data * data,const struct wpabuf * in_data)338 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
339 const struct wpabuf *in_data)
340 {
341 size_t tls_in_len, in_len;
342
343 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
344 in_len = in_data ? wpabuf_len(in_data) : 0;
345
346 if (tls_in_len + in_len == 0) {
347 /* No message data received?! */
348 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
349 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
350 (unsigned long) data->tls_in_left,
351 (unsigned long) tls_in_len,
352 (unsigned long) in_len);
353 eap_peer_tls_reset_input(data);
354 return -1;
355 }
356
357 if (tls_in_len + in_len > 65536) {
358 /*
359 * Limit length to avoid rogue servers from causing large
360 * memory allocations.
361 */
362 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
363 "64 kB)");
364 eap_peer_tls_reset_input(data);
365 return -1;
366 }
367
368 if (in_len > data->tls_in_left) {
369 /* Sender is doing something odd - reject message */
370 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
371 "indicated");
372 eap_peer_tls_reset_input(data);
373 return -1;
374 }
375
376 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
377 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
378 "data");
379 eap_peer_tls_reset_input(data);
380 return -1;
381 }
382 if (in_data)
383 wpabuf_put_buf(data->tls_in, in_data);
384 data->tls_in_left -= in_len;
385
386 if (data->tls_in_left > 0) {
387 wpa_printf(MSG_INFO, "SSL: Need %lu bytes more input "
388 "data", (unsigned long) data->tls_in_left);
389 return 1;
390 }
391
392 return 0;
393 }
394
395
396 /**
397 * eap_peer_tls_data_reassemble - Reassemble TLS data
398 * @data: Data for TLS processing
399 * @in_data: Next incoming TLS segment
400 * @need_more_input: Variable for returning whether more input data is needed
401 * to reassemble this TLS packet
402 * Returns: Pointer to output data, %NULL on error or when more data is needed
403 * for the full message (in which case, *need_more_input is also set to 1).
404 *
405 * This function reassembles TLS fragments. Caller must not free the returned
406 * data buffer since an internal pointer to it is maintained.
407 */
eap_peer_tls_data_reassemble(struct eap_ssl_data * data,const struct wpabuf * in_data,int * need_more_input)408 static const struct wpabuf * eap_peer_tls_data_reassemble(
409 struct eap_ssl_data *data, const struct wpabuf *in_data,
410 int *need_more_input)
411 {
412 *need_more_input = 0;
413
414 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
415 /* Message has fragments */
416 int res = eap_peer_tls_reassemble_fragment(data, in_data);
417 if (res) {
418 if (res == 1)
419 *need_more_input = 1;
420 return NULL;
421 }
422
423 /* Message is now fully reassembled. */
424 } else {
425 /* No fragments in this message, so just make a copy of it. */
426 data->tls_in_left = 0;
427 data->tls_in = wpabuf_dup(in_data);
428 if (data->tls_in == NULL)
429 return NULL;
430 }
431
432 return data->tls_in;
433 }
434
435
436 /**
437 * eap_tls_process_input - Process incoming TLS message
438 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
439 * @data: Data for TLS processing
440 * @in_data: Message received from the server
441 * @in_len: Length of in_data
442 * @out_data: Buffer for returning a pointer to application data (if available)
443 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
444 * is available, -1 on failure
445 */
eap_tls_process_input(struct eap_sm * sm,struct eap_ssl_data * data,const u8 * in_data,size_t in_len,struct wpabuf ** out_data)446 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
447 const u8 *in_data, size_t in_len,
448 struct wpabuf **out_data)
449 {
450 const struct wpabuf *msg;
451 int need_more_input;
452 struct wpabuf *appl_data;
453 struct wpabuf buf;
454
455 wpabuf_set(&buf, in_data, in_len);
456 msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
457 if (msg == NULL)
458 return need_more_input ? 1 : -1;
459
460 /* Full TLS message reassembled - continue handshake processing */
461 if (data->tls_out) {
462 /* This should not happen.. */
463 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
464 "tls_out data even though tls_out_len = 0");
465 wpabuf_free(data->tls_out);
466 //WPA_ASSERT(data->tls_out == NULL);
467 }
468 appl_data = NULL;
469 data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
470 msg, &appl_data);
471
472 eap_peer_tls_reset_input(data);
473 if (appl_data &&
474 tls_connection_established(data->ssl_ctx, data->conn) &&
475 !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
476 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
477 appl_data);
478 *out_data = appl_data;
479 return 2;
480 }
481
482 wpabuf_free(appl_data);
483
484 return 0;
485 }
486
487
488 /**
489 * eap_tls_process_output - Process outgoing TLS message
490 * @data: Data for TLS processing
491 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
492 * @peap_version: Version number for EAP-PEAP/TTLS
493 * @id: EAP identifier for the response
494 * @ret: Return value to use on success
495 * @out_data: Buffer for returning the allocated output buffer
496 * Returns: ret (0 or 1) on success, -1 on failure
497 */
eap_tls_process_output(struct eap_ssl_data * data,EapType eap_type,int peap_version,u8 id,int ret,struct wpabuf ** out_data)498 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
499 int peap_version, u8 id, int ret,
500 struct wpabuf **out_data)
501 {
502 size_t len;
503 u8 *flags;
504 int more_fragments, length_included;
505
506 if (data->tls_out == NULL)
507 return -1;
508 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
509 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
510 "%lu bytes)",
511 (unsigned long) len,
512 (unsigned long) wpabuf_len(data->tls_out));
513
514 /*
515 * Limit outgoing message to the configured maximum size. Fragment
516 * message if needed.
517 */
518 if (len > data->tls_out_limit) {
519 more_fragments = 1;
520 len = data->tls_out_limit;
521 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
522 "will follow", (unsigned long) len);
523 } else
524 more_fragments = 0;
525
526 length_included = data->tls_out_pos == 0 &&
527 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
528 data->include_tls_length);
529 if (!length_included &&
530 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
531 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
532 /*
533 * Windows Server 2008 NPS really wants to have the TLS Message
534 * length included in phase 0 even for unfragmented frames or
535 * it will get very confused with Compound MAC calculation and
536 * Outer TLVs.
537 */
538 length_included = 1;
539 }
540
541 *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
542 EAP_CODE_RESPONSE, id);
543 if (*out_data == NULL) {
544 printf("[Debug] out_data is null, return \n");
545 return -1;
546 }
547
548 flags = wpabuf_put(*out_data, 1);
549 *flags = peap_version;
550 if (more_fragments)
551 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
552 if (length_included) {
553 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
554 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
555 }
556 wpabuf_put_data(*out_data,
557 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
558 len);
559 data->tls_out_pos += len;
560
561 if (!more_fragments)
562 eap_peer_tls_reset_output(data);
563
564 return ret;
565 }
566
567
568 /**
569 * eap_peer_tls_process_helper - Process TLS handshake message
570 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
571 * @data: Data for TLS processing
572 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
573 * @peap_version: Version number for EAP-PEAP/TTLS
574 * @id: EAP identifier for the response
575 * @in_data: Message received from the server
576 * @in_len: Length of in_data
577 * @out_data: Buffer for returning a pointer to the response message
578 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
579 * is available, or -1 on failure
580 *
581 * This function can be used to process TLS handshake messages. It reassembles
582 * the received fragments and uses a TLS library to process the messages. The
583 * response data from the TLS library is fragmented to suitable output messages
584 * that the caller can send out.
585 *
586 * out_data is used to return the response message if the return value of this
587 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
588 * alarm message. The caller is responsible for freeing the allocated buffer if
589 * *out_data is not %NULL.
590 *
591 * This function is called for each received TLS message during the TLS
592 * handshake after eap_peer_tls_process_init() call and possible processing of
593 * TLS Flags field. Once the handshake has been completed, i.e., when
594 * tls_connection_established() returns 1, EAP method specific decrypting of
595 * the tunneled data is used.
596 */
eap_peer_tls_process_helper(struct eap_sm * sm,struct eap_ssl_data * data,EapType eap_type,int peap_version,u8 id,const u8 * in_data,size_t in_len,struct wpabuf ** out_data)597 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
598 EapType eap_type, int peap_version,
599 u8 id, const u8 *in_data, size_t in_len,
600 struct wpabuf **out_data)
601 {
602 int ret = 0;
603
604 *out_data = NULL;
605
606 if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
607 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
608 "fragments are waiting to be sent out");
609 return -1;
610 }
611
612 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
613 /*
614 * No more data to send out - expect to receive more data from
615 * the AS.
616 */
617 int res = eap_tls_process_input(sm, data, in_data, in_len,
618 out_data);
619 if (res) {
620 /*
621 * Input processing failed (res = -1) or more data is
622 * needed (res = 1).
623 */
624 return res;
625 }
626
627 /*
628 * The incoming message has been reassembled and processed. The
629 * response was allocated into data->tls_out buffer.
630 */
631 }
632
633 if (data->tls_out == NULL) {
634 /*
635 * No outgoing fragments remaining from the previous message
636 * and no new message generated. This indicates an error in TLS
637 * processing.
638 */
639 eap_peer_tls_reset_output(data);
640 return -1;
641 }
642
643 if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
644 /* TLS processing has failed - return error */
645 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
646 "report error");
647 ret = -1;
648 /* TODO: clean pin if engine used? */
649 }
650
651 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
652 /*
653 * TLS negotiation should now be complete since all other cases
654 * needing more data should have been caught above based on
655 * the TLS Message Length field.
656 */
657 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
658 wpabuf_free(data->tls_out);
659 data->tls_out = NULL;
660 return 1;
661 }
662
663 /* Send the pending message (in fragments, if needed). */
664 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
665 out_data);
666 }
667
668
669 /**
670 * eap_peer_tls_build_ack - Build a TLS ACK frame
671 * @id: EAP identifier for the response
672 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
673 * @peap_version: Version number for EAP-PEAP/TTLS
674 * Returns: Pointer to the allocated ACK frame or %NULL on failure
675 */
eap_peer_tls_build_ack(u8 id,EapType eap_type,int peap_version)676 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
677 int peap_version)
678 {
679 struct wpabuf *resp;
680
681 resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
682 if (resp == NULL)
683 return NULL;
684 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
685 (int) eap_type, id, peap_version);
686 wpabuf_put_u8(resp, peap_version); /* Flags */
687 return resp;
688 }
689
690
691 /**
692 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
693 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
694 * @data: Data for TLS processing
695 * Returns: 0 on success, -1 on failure
696 */
eap_peer_tls_reauth_init(struct eap_sm * sm,struct eap_ssl_data * data)697 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
698 {
699 eap_peer_tls_reset_input(data);
700 eap_peer_tls_reset_output(data);
701 return tls_connection_shutdown(data->ssl_ctx, data->conn);
702 }
703
704
705 /**
706 * eap_peer_tls_status - Get TLS status
707 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
708 * @data: Data for TLS processing
709 * @buf: Buffer for status information
710 * @buflen: Maximum buffer length
711 * @verbose: Whether to include verbose status information
712 * Returns: Number of bytes written to buf.
713 */
eap_peer_tls_status(struct eap_sm * sm,struct eap_ssl_data * data,char * buf,size_t buflen,int verbose)714 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
715 char *buf, size_t buflen, int verbose)
716 {
717 char name[128];
718 int len = 0, ret;
719
720 if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) == 0)
721 {
722 ret = os_snprintf(buf + len, buflen - len,
723 "EAP TLS cipher=%s\n", name);
724 if (ret < 0 || (size_t) ret >= buflen - len)
725 return len;
726 len += ret;
727 }
728
729 return len;
730 }
731
732
733 /**
734 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
735 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
736 * @data: Data for TLS processing
737 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
738 * @ret: Return values from EAP request validation and processing
739 * @reqData: EAP request to be processed (eapReqData)
740 * @len: Buffer for returning length of the remaining payload
741 * @flags: Buffer for returning TLS flags
742 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
743 *
744 * This function validates the EAP header and processes the optional TLS
745 * Message Length field. If this is the first fragment of a TLS message, the
746 * TLS reassembly code is initialized to receive the indicated number of bytes.
747 *
748 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
749 * function as the first step in processing received messages. They will need
750 * to process the flags (apart from Message Length Included) that are returned
751 * through the flags pointer and the message payload that will be returned (and
752 * the length is returned through the len pointer). Return values (ret) are set
753 * for continuation of EAP method processing. The caller is responsible for
754 * setting these to indicate completion (either success or failure) based on
755 * the authentication result.
756 */
eap_peer_tls_process_init(struct eap_sm * sm,struct eap_ssl_data * data,EapType eap_type,struct eap_method_ret * ret,const struct wpabuf * reqData,size_t * len,u8 * flags)757 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
758 struct eap_ssl_data *data,
759 EapType eap_type,
760 struct eap_method_ret *ret,
761 const struct wpabuf *reqData,
762 size_t *len, u8 *flags)
763 {
764 const u8 *pos;
765 size_t left;
766 unsigned int tls_msg_len;
767
768 if (tls_get_errors(data->ssl_ctx)) {
769 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
770 ret->ignore = TRUE;
771 return NULL;
772 }
773
774 if (eap_type == EAP_UNAUTH_TLS_TYPE)
775 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
776 EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
777 &left);
778 else
779 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
780 &left);
781 if (pos == NULL) {
782 ret->ignore = TRUE;
783 return NULL;
784 }
785 if (left == 0) {
786 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
787 "octet included");
788 if (!sm->workaround) {
789 ret->ignore = TRUE;
790 return NULL;
791 }
792
793 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
794 "indicates ACK frame");
795 *flags = 0;
796 } else {
797 *flags = *pos++;
798 left--;
799 }
800 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
801 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
802 *flags);
803 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
804 if (left < 4) {
805 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
806 "length");
807 ret->ignore = TRUE;
808 return NULL;
809 }
810 tls_msg_len = WPA_GET_BE32(pos);
811 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
812 tls_msg_len);
813 if (data->tls_in_left == 0) {
814 data->tls_in_total = tls_msg_len;
815 data->tls_in_left = tls_msg_len;
816 wpabuf_free(data->tls_in);
817 data->tls_in = NULL;
818 }
819 pos += 4;
820 left -= 4;
821
822 if (left > tls_msg_len) {
823 wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
824 "bytes) smaller than this fragment (%d "
825 "bytes)", (int) tls_msg_len, (int) left);
826 ret->ignore = TRUE;
827 return NULL;
828 }
829 }
830
831 ret->ignore = FALSE;
832 ret->methodState = METHOD_MAY_CONT;
833 ret->decision = DECISION_FAIL;
834 ret->allowNotifications = TRUE;
835
836 *len = left;
837 return pos;
838 }
839
840
841 /**
842 * eap_peer_tls_reset_input - Reset input buffers
843 * @data: Data for TLS processing
844 *
845 * This function frees any allocated memory for input buffers and resets input
846 * state.
847 */
eap_peer_tls_reset_input(struct eap_ssl_data * data)848 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
849 {
850 data->tls_in_left = data->tls_in_total = 0;
851 wpabuf_free(data->tls_in);
852 data->tls_in = NULL;
853 }
854
855
856 /**
857 * eap_peer_tls_reset_output - Reset output buffers
858 * @data: Data for TLS processing
859 *
860 * This function frees any allocated memory for output buffers and resets
861 * output state.
862 */
eap_peer_tls_reset_output(struct eap_ssl_data * data)863 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
864 {
865 data->tls_out_pos = 0;
866 wpabuf_free(data->tls_out);
867 data->tls_out = NULL;
868 }
869
870
871 /**
872 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
873 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
874 * @data: Data for TLS processing
875 * @in_data: Message received from the server
876 * @in_decrypted: Buffer for returning a pointer to the decrypted message
877 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
878 */
eap_peer_tls_decrypt(struct eap_sm * sm,struct eap_ssl_data * data,const struct wpabuf * in_data,struct wpabuf ** in_decrypted)879 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
880 const struct wpabuf *in_data,
881 struct wpabuf **in_decrypted)
882 {
883 const struct wpabuf *msg;
884 int need_more_input;
885
886 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
887 if (msg == NULL)
888 return need_more_input ? 1 : -1;
889
890 *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
891 eap_peer_tls_reset_input(data);
892 if (*in_decrypted == NULL) {
893 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
894 return -1;
895 }
896 return 0;
897 }
898
899
900 /**
901 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
902 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
903 * @data: Data for TLS processing
904 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
905 * @peap_version: Version number for EAP-PEAP/TTLS
906 * @id: EAP identifier for the response
907 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
908 * @out_data: Buffer for returning a pointer to the encrypted response message
909 * Returns: 0 on success, -1 on failure
910 */
eap_peer_tls_encrypt(struct eap_sm * sm,struct eap_ssl_data * data,EapType eap_type,int peap_version,u8 id,const struct wpabuf * in_data,struct wpabuf ** out_data)911 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
912 EapType eap_type, int peap_version, u8 id,
913 const struct wpabuf *in_data,
914 struct wpabuf **out_data)
915 {
916 if (in_data) {
917 eap_peer_tls_reset_output(data);
918 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
919 data->conn, in_data);
920 if (data->tls_out == NULL) {
921 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
922 "data (in_len=%lu)",
923 (unsigned long) wpabuf_len(in_data));
924 eap_peer_tls_reset_output(data);
925 return -1;
926 }
927 }
928
929 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
930 out_data);
931 }
932
933 /**
934 * eap_peer_select_phase2_methods - Select phase 2 EAP method
935 * @config: Pointer to the network configuration
936 * @prefix: 'phase2' configuration prefix, e.g., "auth="
937 * @types: Buffer for returning allocated list of allowed EAP methods
938 * @num_types: Buffer for returning number of allocated EAP methods
939 * Returns: 0 on success, -1 on failure
940 *
941 * This function is used to parse EAP method list and select allowed methods
942 * for Phase2 authentication.
943 */
eap_peer_select_phase2_methods(struct eap_peer_config * config,const char * prefix,struct eap_method_type ** types,size_t * num_types)944 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
945 const char *prefix,
946 struct eap_method_type **types,
947 size_t *num_types)
948 {
949 char *start, *pos, *buf;
950 struct eap_method_type *methods = NULL, *_methods;
951 u8 method;
952 size_t num_methods = 0, prefix_len;
953
954 if (config == NULL || config->phase2 == NULL)
955 goto get_defaults;
956
957 start = buf = os_strdup(config->phase2);
958 if (buf == NULL)
959 return -1;
960
961 prefix_len = os_strlen(prefix);
962
963 while (start && *start != '\0') {
964 int vendor;
965 pos = os_strstr(start, prefix);
966 if (pos == NULL)
967 break;
968 if (start != pos && *(pos - 1) != ' ') {
969 start = pos + prefix_len;
970 continue;
971 }
972
973 start = pos + prefix_len;
974 pos = (char *)os_strchr(start, ' ');
975 if (pos)
976 *pos++ = '\0';
977 method = eap_get_phase2_type(start, &vendor);
978 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
979 wpa_printf(MSG_INFO, "TLS: Unsupported Phase2 EAP "
980 "method '%s'\n", start);
981 } else {
982 num_methods++;
983 _methods = (struct eap_method_type *)os_realloc(methods,
984 num_methods * sizeof(*methods));
985 if (_methods == NULL) {
986 os_free(methods);
987 os_free(buf);
988 return -1;
989 }
990 methods = _methods;
991 methods[num_methods - 1].vendor = vendor;
992 methods[num_methods - 1].method = method;
993 }
994
995 start = pos;
996 }
997
998 os_free(buf);
999
1000 get_defaults:
1001 if (methods == NULL)
1002 methods = eap_get_phase2_types(config, &num_methods);
1003 if (methods == NULL) {
1004 wpa_printf(MSG_ERROR, "TLS: No Phase EAP methods available");
1005 return -1;
1006 }
1007 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1008 (u8 *) methods,
1009 num_methods * sizeof(struct eap_method_type));
1010
1011 *types = methods;
1012 *num_types = num_methods;
1013
1014 return 0;
1015 }
1016
1017 /**
1018 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1019 * @types: Buffer for returning allocated list of allowed EAP methods
1020 * @num_types: Buffer for returning number of allocated EAP methods
1021 * @hdr: EAP-Request header (and the following EAP type octet)
1022 * @resp: Buffer for returning the EAP-Nak message
1023 * Returns: 0 on success, -1 on failure
1024 */
eap_peer_tls_phase2_nak(struct eap_method_type * types,size_t num_types,struct eap_hdr * hdr,struct wpabuf ** resp)1025 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1026 struct eap_hdr *hdr, struct wpabuf **resp)
1027 {
1028 #ifdef DEBUG_PRINT
1029 u8 *pos = (u8 *) (hdr + 1);
1030 #endif
1031 size_t i;
1032
1033 /* TODO: add support for expanded Nak */
1034 wpa_printf(MSG_DEBUG, "TLS: Phase Request: Nak type=%d", *pos);
1035 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1036 (u8 *) types, num_types * sizeof(struct eap_method_type));
1037 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1038 EAP_CODE_RESPONSE, hdr->identifier);
1039 if (*resp == NULL)
1040 return -1;
1041
1042 for (i = 0; i < num_types; i++) {
1043 if (types[i].vendor == EAP_VENDOR_IETF &&
1044 types[i].method < 256)
1045 wpabuf_put_u8(*resp, types[i].method);
1046 }
1047
1048 eap_update_len(*resp);
1049
1050 return 0;
1051 }
1052