2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2008, 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.
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
25 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
26 const u8 **data, size_t *data_len)
28 const struct wpa_config_blob *blob;
30 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
33 blob = eap_get_config_blob(sm, *name + 7);
35 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
36 "found", __func__, *name + 7);
42 *data_len = blob->len;
48 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
49 struct eap_peer_config *config)
51 params->ca_cert = (char *) config->ca_cert;
52 params->ca_path = (char *) config->ca_path;
53 params->client_cert = (char *) config->client_cert;
54 params->private_key = (char *) config->private_key;
55 params->private_key_passwd = (char *) config->private_key_passwd;
56 params->dh_file = (char *) config->dh_file;
57 params->subject_match = (char *) config->subject_match;
58 params->altsubject_match = (char *) config->altsubject_match;
59 params->engine = config->engine;
60 params->engine_id = config->engine_id;
61 params->pin = config->pin;
62 params->key_id = config->key_id;
63 params->cert_id = config->cert_id;
64 params->ca_cert_id = config->ca_cert_id;
68 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
69 struct eap_peer_config *config)
71 params->ca_cert = (char *) config->ca_cert2;
72 params->ca_path = (char *) config->ca_path2;
73 params->client_cert = (char *) config->client_cert2;
74 params->private_key = (char *) config->private_key2;
75 params->private_key_passwd = (char *) config->private_key2_passwd;
76 params->dh_file = (char *) config->dh_file2;
77 params->subject_match = (char *) config->subject_match2;
78 params->altsubject_match = (char *) config->altsubject_match2;
79 params->engine = config->engine2;
80 params->engine_id = config->engine2_id;
81 params->pin = config->pin2;
82 params->key_id = config->key2_id;
83 params->cert_id = config->cert2_id;
84 params->ca_cert_id = config->ca_cert2_id;
88 static int eap_tls_params_from_conf(struct eap_sm *sm,
89 struct eap_ssl_data *data,
90 struct tls_connection_params *params,
91 struct eap_peer_config *config, int phase2)
93 os_memset(params, 0, sizeof(*params));
95 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
96 eap_tls_params_from_conf2(params, config);
98 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
99 eap_tls_params_from_conf1(params, config);
101 params->tls_ia = data->tls_ia;
104 * Use blob data, if available. Otherwise, leave reference to external
107 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
108 ¶ms->ca_cert_blob_len) ||
109 eap_tls_check_blob(sm, ¶ms->client_cert,
110 ¶ms->client_cert_blob,
111 ¶ms->client_cert_blob_len) ||
112 eap_tls_check_blob(sm, ¶ms->private_key,
113 ¶ms->private_key_blob,
114 ¶ms->private_key_blob_len) ||
115 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
116 ¶ms->dh_blob_len)) {
117 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
125 static int eap_tls_init_connection(struct eap_sm *sm,
126 struct eap_ssl_data *data,
127 struct eap_peer_config *config,
128 struct tls_connection_params *params)
132 data->conn = tls_connection_init(sm->ssl_ctx);
133 if (data->conn == NULL) {
134 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
139 res = tls_connection_set_params(sm->ssl_ctx, data->conn, params);
140 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
142 * At this point with the pkcs11 engine the PIN might be wrong.
143 * We reset the PIN in the configuration to be sure to not use
144 * it again and the calling function must request a new one.
146 os_free(config->pin);
148 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
149 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
151 * We do not know exactly but maybe the PIN was wrong,
152 * so ask for a new one.
154 os_free(config->pin);
156 eap_sm_request_pin(sm);
160 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
170 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
171 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
172 * @data: Data for TLS processing
173 * @config: Pointer to the network configuration
174 * Returns: 0 on success, -1 on failure
176 * This function is used to initialize shared TLS functionality for EAP-TLS,
177 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
179 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
180 struct eap_peer_config *config)
182 struct tls_connection_params params;
188 data->phase2 = sm->init_phase2;
189 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
193 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
196 data->tls_out_limit = config->fragment_size;
198 /* Limit the fragment size in the inner TLS authentication
199 * since the outer authentication with EAP-PEAP does not yet
200 * support fragmentation */
201 if (data->tls_out_limit > 100)
202 data->tls_out_limit -= 100;
205 if (config->phase1 &&
206 os_strstr(config->phase1, "include_tls_length=1")) {
207 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
208 "unfragmented packets");
209 data->include_tls_length = 1;
217 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
218 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
219 * @data: Data for TLS processing
221 * This function deinitializes shared TLS functionality that was initialized
222 * with eap_peer_tls_ssl_init().
224 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
226 tls_connection_deinit(sm->ssl_ctx, data->conn);
227 eap_peer_tls_reset_input(data);
228 eap_peer_tls_reset_output(data);
233 * eap_peer_tls_derive_key - Derive a key based on TLS session data
234 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
235 * @data: Data for TLS processing
236 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
237 * @len: Length of the key material to generate (usually 64 for MSK)
238 * Returns: Pointer to allocated key on success or %NULL on failure
240 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
241 * session data (client/server random and master key). Each key type may use a
242 * different label to bind the key usage into the generated material.
244 * The caller is responsible for freeing the returned buffer.
246 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
247 const char *label, size_t len)
249 struct tls_keys keys;
250 u8 *rnd = NULL, *out;
252 out = os_malloc(len);
256 /* First, try to use TLS library function for PRF, if available. */
257 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
262 * TLS library did not support key generation, so get the needed TLS
263 * session parameters and use an internal implementation of TLS PRF to
266 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
269 if (keys.client_random == NULL || keys.server_random == NULL ||
270 keys.master_key == NULL)
273 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
276 os_memcpy(rnd, keys.client_random, keys.client_random_len);
277 os_memcpy(rnd + keys.client_random_len, keys.server_random,
278 keys.server_random_len);
280 if (tls_prf(keys.master_key, keys.master_key_len,
281 label, rnd, keys.client_random_len +
282 keys.server_random_len, out, len))
296 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
297 * @data: Data for TLS processing
298 * @in_data: Next incoming TLS segment
299 * @in_len: Length of in_data
300 * Returns: 0 on success, 1 if more data is needed for the full message, or
303 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
304 const u8 *in_data, size_t in_len)
308 if (data->tls_in_len + in_len == 0) {
309 /* No message data received?! */
310 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
311 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
312 (unsigned long) data->tls_in_left,
313 (unsigned long) data->tls_in_len,
314 (unsigned long) in_len);
315 eap_peer_tls_reset_input(data);
319 if (data->tls_in_len + in_len > 65536) {
321 * Limit length to avoid rogue servers from causing large
322 * memory allocations.
324 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
326 eap_peer_tls_reset_input(data);
330 if (in_len > data->tls_in_left) {
331 /* Sender is doing something odd - reject message */
332 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
334 eap_peer_tls_reset_input(data);
338 buf = os_realloc(data->tls_in, data->tls_in_len + in_len);
340 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
342 eap_peer_tls_reset_input(data);
345 os_memcpy(buf + data->tls_in_len, in_data, in_len);
347 data->tls_in_len += in_len;
348 data->tls_in_left -= in_len;
350 if (data->tls_in_left > 0) {
351 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
352 "data", (unsigned long) data->tls_in_left);
361 * eap_peer_tls_data_reassemble - Reassemble TLS data
362 * @data: Data for TLS processing
363 * @in_data: Next incoming TLS segment
364 * @in_len: Length of in_data
365 * @out_len: Variable for returning length of the reassembled message
366 * @need_more_input: Variable for returning whether more input data is needed
367 * to reassemble this TLS packet
368 * Returns: Pointer to output data, %NULL on error or when more data is needed
369 * for the full message (in which case, *need_more_input is also set to 1).
371 * This function reassembles TLS fragments. Caller must not free the returned
372 * data buffer since an internal pointer to it is maintained.
374 const u8 * eap_peer_tls_data_reassemble(
375 struct eap_ssl_data *data, const u8 *in_data, size_t in_len,
376 size_t *out_len, int *need_more_input)
378 *need_more_input = 0;
380 if (data->tls_in_left > in_len || data->tls_in) {
381 /* Message has fragments */
382 int res = eap_peer_tls_reassemble_fragment(data, in_data,
386 *need_more_input = 1;
390 /* Message is now fully reassembled. */
392 /* No fragments in this message, so just make a copy of it. */
393 data->tls_in_left = 0;
394 data->tls_in = os_malloc(in_len ? in_len : 1);
395 if (data->tls_in == NULL)
397 os_memcpy(data->tls_in, in_data, in_len);
398 data->tls_in_len = in_len;
401 *out_len = data->tls_in_len;
407 * eap_tls_process_input - Process incoming TLS message
408 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
409 * @data: Data for TLS processing
410 * @in_data: Message received from the server
411 * @in_len: Length of in_data
412 * @out_data: Buffer for returning a pointer to application data (if available)
413 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
414 * is available, -1 on failure
416 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
417 const u8 *in_data, size_t in_len,
418 struct wpabuf **out_data)
424 size_t appl_data_len;
426 msg = eap_peer_tls_data_reassemble(data, in_data, in_len,
427 &msg_len, &need_more_input);
429 return need_more_input ? 1 : -1;
431 /* Full TLS message reassembled - continue handshake processing */
433 /* This should not happen.. */
434 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
435 "tls_out data even though tls_out_len = 0");
436 os_free(data->tls_out);
437 WPA_ASSERT(data->tls_out == NULL);
440 data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn,
443 &appl_data, &appl_data_len);
445 eap_peer_tls_reset_input(data);
448 tls_connection_established(sm->ssl_ctx, data->conn) &&
449 !tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
450 wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application data",
451 appl_data, appl_data_len);
452 *out_data = wpabuf_alloc_ext_data(appl_data, appl_data_len);
453 if (*out_data == NULL) {
467 * eap_tls_process_output - Process outgoing TLS message
468 * @data: Data for TLS processing
469 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
470 * @peap_version: Version number for EAP-PEAP/TTLS
471 * @id: EAP identifier for the response
472 * @ret: Return value to use on success
473 * @out_data: Buffer for returning the allocated output buffer
474 * Returns: ret (0 or 1) on success, -1 on failure
476 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
477 int peap_version, u8 id, int ret,
478 struct wpabuf **out_data)
482 int more_fragments, length_included;
484 len = data->tls_out_len - data->tls_out_pos;
485 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
487 (unsigned long) len, (unsigned long) data->tls_out_len);
490 * Limit outgoing message to the configured maximum size. Fragment
493 if (len > data->tls_out_limit) {
495 len = data->tls_out_limit;
496 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
497 "will follow", (unsigned long) len);
501 length_included = data->tls_out_pos == 0 &&
502 (data->tls_out_len > data->tls_out_limit ||
503 data->include_tls_length);
505 *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type,
506 1 + length_included * 4 + len,
507 EAP_CODE_RESPONSE, id);
508 if (*out_data == NULL)
511 flags = wpabuf_put(*out_data, 1);
512 *flags = peap_version;
514 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
515 if (length_included) {
516 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
517 wpabuf_put_be32(*out_data, data->tls_out_len);
520 wpabuf_put_data(*out_data, &data->tls_out[data->tls_out_pos], len);
521 data->tls_out_pos += len;
524 eap_peer_tls_reset_output(data);
531 * eap_peer_tls_process_helper - Process TLS handshake message
532 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
533 * @data: Data for TLS processing
534 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
535 * @peap_version: Version number for EAP-PEAP/TTLS
536 * @id: EAP identifier for the response
537 * @in_data: Message received from the server
538 * @in_len: Length of in_data
539 * @out_data: Buffer for returning a pointer to the response message
540 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
541 * is available, or -1 on failure
543 * This function can be used to process TLS handshake messages. It reassembles
544 * the received fragments and uses a TLS library to process the messages. The
545 * response data from the TLS library is fragmented to suitable output messages
546 * that the caller can send out.
548 * out_data is used to return the response message if the return value of this
549 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
550 * alarm message. The caller is responsible for freeing the allocated buffer if
551 * *out_data is not %NULL.
553 * This function is called for each received TLS message during the TLS
554 * handshake after eap_peer_tls_process_init() call and possible processing of
555 * TLS Flags field. Once the handshake has been completed, i.e., when
556 * tls_connection_established() returns 1, EAP method specific decrypting of
557 * the tunneled data is used.
559 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
560 EapType eap_type, int peap_version,
561 u8 id, const u8 *in_data, size_t in_len,
562 struct wpabuf **out_data)
568 if (data->tls_out_len > 0 && in_len > 0) {
569 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
570 "fragments are waiting to be sent out");
574 if (data->tls_out_len == 0) {
576 * No more data to send out - expect to receive more data from
579 int res = eap_tls_process_input(sm, data, in_data, in_len,
583 * Input processing failed (res = -1) or more data is
590 * The incoming message has been reassembled and processed. The
591 * response was allocated into data->tls_out buffer.
595 if (data->tls_out == NULL) {
597 * No outgoing fragments remaining from the previous message
598 * and no new message generated. This indicates an error in TLS
601 eap_peer_tls_reset_output(data);
605 if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
606 /* TLS processing has failed - return error */
607 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
610 /* TODO: clean pin if engine used? */
613 if (data->tls_out_len == 0) {
615 * TLS negotiation should now be complete since all other cases
616 * needing more data should have been caught above based on
617 * the TLS Message Length field.
619 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
620 os_free(data->tls_out);
621 data->tls_out = NULL;
625 /* Send the pending message (in fragments, if needed). */
626 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
632 * eap_peer_tls_build_ack - Build a TLS ACK frame
633 * @id: EAP identifier for the response
634 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
635 * @peap_version: Version number for EAP-PEAP/TTLS
636 * Returns: Pointer to the allocated ACK frame or %NULL on failure
638 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
643 resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE,
647 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
648 (int) eap_type, id, peap_version);
649 wpabuf_put_u8(resp, peap_version); /* Flags */
655 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
656 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
657 * @data: Data for TLS processing
658 * Returns: 0 on success, -1 on failure
660 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
662 eap_peer_tls_reset_input(data);
663 eap_peer_tls_reset_output(data);
664 return tls_connection_shutdown(sm->ssl_ctx, data->conn);
669 * eap_peer_tls_status - Get TLS status
670 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
671 * @data: Data for TLS processing
672 * @buf: Buffer for status information
673 * @buflen: Maximum buffer length
674 * @verbose: Whether to include verbose status information
675 * Returns: Number of bytes written to buf.
677 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
678 char *buf, size_t buflen, int verbose)
683 if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) {
684 ret = os_snprintf(buf + len, buflen - len,
685 "EAP TLS cipher=%s\n", name);
686 if (ret < 0 || (size_t) ret >= buflen - len)
696 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
697 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
698 * @data: Data for TLS processing
699 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
700 * @ret: Return values from EAP request validation and processing
701 * @reqData: EAP request to be processed (eapReqData)
702 * @len: Buffer for returning length of the remaining payload
703 * @flags: Buffer for returning TLS flags
704 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
706 * This function validates the EAP header and processes the optional TLS
707 * Message Length field. If this is the first fragment of a TLS message, the
708 * TLS reassembly code is initialized to receive the indicated number of bytes.
710 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
711 * function as the first step in processing received messages. They will need
712 * to process the flags (apart from Message Length Included) that are returned
713 * through the flags pointer and the message payload that will be returned (and
714 * the length is returned through the len pointer). Return values (ret) are set
715 * for continuation of EAP method processing. The caller is responsible for
716 * setting these to indicate completion (either success or failure) based on
717 * the authentication result.
719 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
720 struct eap_ssl_data *data,
722 struct eap_method_ret *ret,
723 const struct wpabuf *reqData,
724 size_t *len, u8 *flags)
728 unsigned int tls_msg_len;
730 if (tls_get_errors(sm->ssl_ctx)) {
731 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
736 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left);
743 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
744 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
746 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
748 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
753 tls_msg_len = WPA_GET_BE32(pos);
754 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
756 if (data->tls_in_left == 0) {
757 data->tls_in_total = tls_msg_len;
758 data->tls_in_left = tls_msg_len;
759 os_free(data->tls_in);
761 data->tls_in_len = 0;
768 ret->methodState = METHOD_MAY_CONT;
769 ret->decision = DECISION_FAIL;
770 ret->allowNotifications = TRUE;
778 * eap_peer_tls_reset_input - Reset input buffers
779 * @data: Data for TLS processing
781 * This function frees any allocated memory for input buffers and resets input
784 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
786 data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
787 os_free(data->tls_in);
793 * eap_peer_tls_reset_output - Reset output buffers
794 * @data: Data for TLS processing
796 * This function frees any allocated memory for output buffers and resets
799 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
801 data->tls_out_len = 0;
802 data->tls_out_pos = 0;
803 os_free(data->tls_out);
804 data->tls_out = NULL;
809 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
810 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
811 * @data: Data for TLS processing
812 * @in_data: Message received from the server
813 * @in_decrypted: Buffer for returning a pointer to the decrypted message
814 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
816 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
817 const struct wpabuf *in_data,
818 struct wpabuf **in_decrypted)
822 size_t msg_len, buf_len;
825 msg = eap_peer_tls_data_reassemble(data, wpabuf_head(in_data),
826 wpabuf_len(in_data), &msg_len,
829 return need_more_input ? 1 : -1;
831 buf_len = wpabuf_len(in_data);
832 if (data->tls_in_total > buf_len)
833 buf_len = data->tls_in_total;
835 * Even though we try to disable TLS compression, it is possible that
836 * this cannot be done with all TLS libraries. Add extra buffer space
837 * to handle the possibility of the decrypted data being longer than
842 *in_decrypted = wpabuf_alloc(buf_len ? buf_len : 1);
843 if (*in_decrypted == NULL) {
844 eap_peer_tls_reset_input(data);
845 wpa_printf(MSG_WARNING, "SSL: Failed to allocate memory for "
850 res = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg, msg_len,
851 wpabuf_mhead(*in_decrypted), buf_len);
852 eap_peer_tls_reset_input(data);
854 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
857 wpabuf_put(*in_decrypted, res);
863 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
864 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
865 * @data: Data for TLS processing
866 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
867 * @peap_version: Version number for EAP-PEAP/TTLS
868 * @id: EAP identifier for the response
869 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
870 * @out_data: Buffer for returning a pointer to the encrypted response message
871 * Returns: 0 on success, -1 on failure
873 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
874 EapType eap_type, int peap_version, u8 id,
875 const struct wpabuf *in_data,
876 struct wpabuf **out_data)
882 eap_peer_tls_reset_output(data);
883 len = wpabuf_len(in_data) + 100;
884 data->tls_out = os_malloc(len);
885 if (data->tls_out == NULL)
888 res = tls_connection_encrypt(sm->ssl_ctx, data->conn,
889 wpabuf_head(in_data),
893 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
895 (unsigned long) wpabuf_len(in_data));
896 eap_peer_tls_reset_output(data);
900 data->tls_out_len = res;
903 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
909 * eap_peer_select_phase2_methods - Select phase 2 EAP method
910 * @config: Pointer to the network configuration
911 * @prefix: 'phase2' configuration prefix, e.g., "auth="
912 * @types: Buffer for returning allocated list of allowed EAP methods
913 * @num_types: Buffer for returning number of allocated EAP methods
914 * Returns: 0 on success, -1 on failure
916 * This function is used to parse EAP method list and select allowed methods
917 * for Phase2 authentication.
919 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
921 struct eap_method_type **types,
924 char *start, *pos, *buf;
925 struct eap_method_type *methods = NULL, *_methods;
927 size_t num_methods = 0, prefix_len;
929 if (config == NULL || config->phase2 == NULL)
932 start = buf = os_strdup(config->phase2);
936 prefix_len = os_strlen(prefix);
938 while (start && *start != '\0') {
940 pos = os_strstr(start, prefix);
943 if (start != pos && *(pos - 1) != ' ') {
944 start = pos + prefix_len;
948 start = pos + prefix_len;
949 pos = os_strchr(start, ' ');
952 method = eap_get_phase2_type(start, &vendor);
953 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
954 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
955 "method '%s'", start);
958 _methods = os_realloc(methods,
959 num_methods * sizeof(*methods));
960 if (_methods == NULL) {
966 methods[num_methods - 1].vendor = vendor;
967 methods[num_methods - 1].method = method;
977 methods = eap_get_phase2_types(config, &num_methods);
979 if (methods == NULL) {
980 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
983 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
985 num_methods * sizeof(struct eap_method_type));
988 *num_types = num_methods;
995 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
996 * @types: Buffer for returning allocated list of allowed EAP methods
997 * @num_types: Buffer for returning number of allocated EAP methods
998 * @hdr: EAP-Request header (and the following EAP type octet)
999 * @resp: Buffer for returning the EAP-Nak message
1000 * Returns: 0 on success, -1 on failure
1002 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1003 struct eap_hdr *hdr, struct wpabuf **resp)
1005 u8 *pos = (u8 *) (hdr + 1);
1008 /* TODO: add support for expanded Nak */
1009 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1010 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1011 (u8 *) types, num_types * sizeof(struct eap_method_type));
1012 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1013 EAP_CODE_RESPONSE, hdr->identifier);
1017 for (i = 0; i < num_types; i++) {
1018 if (types[i].vendor == EAP_VENDOR_IETF &&
1019 types[i].method < 256)
1020 wpabuf_put_u8(*resp, types[i].method);
1023 eap_update_len(*resp);