2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/sha1.h"
13 #include "crypto/tls.h"
15 #include "eap_tls_common.h"
16 #include "eap_config.h"
19 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
20 const u8 **data, size_t *data_len)
22 const struct wpa_config_blob *blob;
24 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
27 blob = eap_get_config_blob(sm, *name + 7);
29 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
30 "found", __func__, *name + 7);
36 *data_len = blob->len;
42 static void eap_tls_params_flags(struct tls_connection_params *params,
47 if (os_strstr(txt, "tls_allow_md5=1"))
48 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
49 if (os_strstr(txt, "tls_disable_time_checks=1"))
50 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
54 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
55 struct eap_peer_config *config)
57 params->ca_cert = (char *) config->ca_cert;
58 params->ca_path = (char *) config->ca_path;
59 params->client_cert = (char *) config->client_cert;
60 params->private_key = (char *) config->private_key;
61 params->private_key_passwd = (char *) config->private_key_passwd;
62 params->dh_file = (char *) config->dh_file;
63 params->subject_match = (char *) config->subject_match;
64 params->altsubject_match = (char *) config->altsubject_match;
65 params->engine = config->engine;
66 params->engine_id = config->engine_id;
67 params->pin = config->pin;
68 params->key_id = config->key_id;
69 params->cert_id = config->cert_id;
70 params->ca_cert_id = config->ca_cert_id;
71 eap_tls_params_flags(params, config->phase1);
75 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
76 struct eap_peer_config *config)
78 params->ca_cert = (char *) config->ca_cert2;
79 params->ca_path = (char *) config->ca_path2;
80 params->client_cert = (char *) config->client_cert2;
81 params->private_key = (char *) config->private_key2;
82 params->private_key_passwd = (char *) config->private_key2_passwd;
83 params->dh_file = (char *) config->dh_file2;
84 params->subject_match = (char *) config->subject_match2;
85 params->altsubject_match = (char *) config->altsubject_match2;
86 params->engine = config->engine2;
87 params->engine_id = config->engine2_id;
88 params->pin = config->pin2;
89 params->key_id = config->key2_id;
90 params->cert_id = config->cert2_id;
91 params->ca_cert_id = config->ca_cert2_id;
92 eap_tls_params_flags(params, config->phase2);
96 static int eap_tls_params_from_conf(struct eap_sm *sm,
97 struct eap_ssl_data *data,
98 struct tls_connection_params *params,
99 struct eap_peer_config *config, int phase2)
101 os_memset(params, 0, sizeof(*params));
103 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
104 eap_tls_params_from_conf2(params, config);
106 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
107 eap_tls_params_from_conf1(params, config);
111 * Use blob data, if available. Otherwise, leave reference to external
114 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
115 ¶ms->ca_cert_blob_len) ||
116 eap_tls_check_blob(sm, ¶ms->client_cert,
117 ¶ms->client_cert_blob,
118 ¶ms->client_cert_blob_len) ||
119 eap_tls_check_blob(sm, ¶ms->private_key,
120 ¶ms->private_key_blob,
121 ¶ms->private_key_blob_len) ||
122 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
123 ¶ms->dh_blob_len)) {
124 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
132 static int eap_tls_init_connection(struct eap_sm *sm,
133 struct eap_ssl_data *data,
134 struct eap_peer_config *config,
135 struct tls_connection_params *params)
139 data->conn = tls_connection_init(sm->ssl_ctx);
140 if (data->conn == NULL) {
141 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
146 res = tls_connection_set_params(sm->ssl_ctx, data->conn, params);
147 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
149 * At this point with the pkcs11 engine the PIN might be wrong.
150 * We reset the PIN in the configuration to be sure to not use
151 * it again and the calling function must request a new one.
153 os_free(config->pin);
155 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
156 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
158 * We do not know exactly but maybe the PIN was wrong,
159 * so ask for a new one.
161 os_free(config->pin);
163 eap_sm_request_pin(sm);
165 tls_connection_deinit(sm->ssl_ctx, data->conn);
169 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
171 tls_connection_deinit(sm->ssl_ctx, data->conn);
181 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
182 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
183 * @data: Data for TLS processing
184 * @config: Pointer to the network configuration
185 * Returns: 0 on success, -1 on failure
187 * This function is used to initialize shared TLS functionality for EAP-TLS,
188 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
190 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
191 struct eap_peer_config *config)
193 struct tls_connection_params params;
199 data->phase2 = sm->init_phase2;
200 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
204 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
207 data->tls_out_limit = config->fragment_size;
209 /* Limit the fragment size in the inner TLS authentication
210 * since the outer authentication with EAP-PEAP does not yet
211 * support fragmentation */
212 if (data->tls_out_limit > 100)
213 data->tls_out_limit -= 100;
216 if (config->phase1 &&
217 os_strstr(config->phase1, "include_tls_length=1")) {
218 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
219 "unfragmented packets");
220 data->include_tls_length = 1;
228 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
229 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
230 * @data: Data for TLS processing
232 * This function deinitializes shared TLS functionality that was initialized
233 * with eap_peer_tls_ssl_init().
235 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
237 tls_connection_deinit(sm->ssl_ctx, data->conn);
238 eap_peer_tls_reset_input(data);
239 eap_peer_tls_reset_output(data);
244 * eap_peer_tls_derive_key - Derive a key based on TLS session data
245 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
246 * @data: Data for TLS processing
247 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
248 * @len: Length of the key material to generate (usually 64 for MSK)
249 * Returns: Pointer to allocated key on success or %NULL on failure
251 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
252 * session data (client/server random and master key). Each key type may use a
253 * different label to bind the key usage into the generated material.
255 * The caller is responsible for freeing the returned buffer.
257 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
258 const char *label, size_t len)
260 struct tls_keys keys;
261 u8 *rnd = NULL, *out;
263 out = os_malloc(len);
267 /* First, try to use TLS library function for PRF, if available. */
268 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
273 * TLS library did not support key generation, so get the needed TLS
274 * session parameters and use an internal implementation of TLS PRF to
277 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
280 if (keys.client_random == NULL || keys.server_random == NULL ||
281 keys.master_key == NULL)
284 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
287 os_memcpy(rnd, keys.client_random, keys.client_random_len);
288 os_memcpy(rnd + keys.client_random_len, keys.server_random,
289 keys.server_random_len);
291 if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
292 label, rnd, keys.client_random_len +
293 keys.server_random_len, out, len))
307 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
308 * @data: Data for TLS processing
309 * @in_data: Next incoming TLS segment
310 * Returns: 0 on success, 1 if more data is needed for the full message, or
313 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
314 const struct wpabuf *in_data)
316 size_t tls_in_len, in_len;
318 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
319 in_len = in_data ? wpabuf_len(in_data) : 0;
321 if (tls_in_len + in_len == 0) {
322 /* No message data received?! */
323 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
324 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
325 (unsigned long) data->tls_in_left,
326 (unsigned long) tls_in_len,
327 (unsigned long) in_len);
328 eap_peer_tls_reset_input(data);
332 if (tls_in_len + in_len > 65536) {
334 * Limit length to avoid rogue servers from causing large
335 * memory allocations.
337 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
339 eap_peer_tls_reset_input(data);
343 if (in_len > data->tls_in_left) {
344 /* Sender is doing something odd - reject message */
345 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
347 eap_peer_tls_reset_input(data);
351 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
352 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
354 eap_peer_tls_reset_input(data);
358 wpabuf_put_buf(data->tls_in, in_data);
359 data->tls_in_left -= in_len;
361 if (data->tls_in_left > 0) {
362 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
363 "data", (unsigned long) data->tls_in_left);
372 * eap_peer_tls_data_reassemble - Reassemble TLS data
373 * @data: Data for TLS processing
374 * @in_data: Next incoming TLS segment
375 * @need_more_input: Variable for returning whether more input data is needed
376 * to reassemble this TLS packet
377 * Returns: Pointer to output data, %NULL on error or when more data is needed
378 * for the full message (in which case, *need_more_input is also set to 1).
380 * This function reassembles TLS fragments. Caller must not free the returned
381 * data buffer since an internal pointer to it is maintained.
383 static const struct wpabuf * eap_peer_tls_data_reassemble(
384 struct eap_ssl_data *data, const struct wpabuf *in_data,
385 int *need_more_input)
387 *need_more_input = 0;
389 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
390 /* Message has fragments */
391 int res = eap_peer_tls_reassemble_fragment(data, in_data);
394 *need_more_input = 1;
398 /* Message is now fully reassembled. */
400 /* No fragments in this message, so just make a copy of it. */
401 data->tls_in_left = 0;
402 data->tls_in = wpabuf_dup(in_data);
403 if (data->tls_in == NULL)
412 * eap_tls_process_input - Process incoming TLS message
413 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
414 * @data: Data for TLS processing
415 * @in_data: Message received from the server
416 * @in_len: Length of in_data
417 * @out_data: Buffer for returning a pointer to application data (if available)
418 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
419 * is available, -1 on failure
421 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
422 const u8 *in_data, size_t in_len,
423 struct wpabuf **out_data)
425 const struct wpabuf *msg;
427 struct wpabuf *appl_data;
430 wpabuf_set(&buf, in_data, in_len);
431 msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
433 return need_more_input ? 1 : -1;
435 /* Full TLS message reassembled - continue handshake processing */
437 /* This should not happen.. */
438 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
439 "tls_out data even though tls_out_len = 0");
440 wpabuf_free(data->tls_out);
441 WPA_ASSERT(data->tls_out == NULL);
444 data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn,
447 eap_peer_tls_reset_input(data);
450 tls_connection_established(sm->ssl_ctx, data->conn) &&
451 !tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
452 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
454 *out_data = appl_data;
458 wpabuf_free(appl_data);
465 * eap_tls_process_output - Process outgoing TLS message
466 * @data: Data for TLS processing
467 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
468 * @peap_version: Version number for EAP-PEAP/TTLS
469 * @id: EAP identifier for the response
470 * @ret: Return value to use on success
471 * @out_data: Buffer for returning the allocated output buffer
472 * Returns: ret (0 or 1) on success, -1 on failure
474 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
475 int peap_version, u8 id, int ret,
476 struct wpabuf **out_data)
480 int more_fragments, length_included;
482 if (data->tls_out == NULL)
484 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
485 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
488 (unsigned long) wpabuf_len(data->tls_out));
491 * Limit outgoing message to the configured maximum size. Fragment
494 if (len > data->tls_out_limit) {
496 len = data->tls_out_limit;
497 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
498 "will follow", (unsigned long) len);
502 length_included = data->tls_out_pos == 0 &&
503 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
504 data->include_tls_length);
505 if (!length_included &&
506 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
507 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
509 * Windows Server 2008 NPS really wants to have the TLS Message
510 * length included in phase 0 even for unfragmented frames or
511 * it will get very confused with Compound MAC calculation and
517 *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type,
518 1 + length_included * 4 + len,
519 EAP_CODE_RESPONSE, id);
520 if (*out_data == NULL)
523 flags = wpabuf_put(*out_data, 1);
524 *flags = peap_version;
526 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
527 if (length_included) {
528 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
529 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
532 wpabuf_put_data(*out_data,
533 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
535 data->tls_out_pos += len;
538 eap_peer_tls_reset_output(data);
545 * eap_peer_tls_process_helper - Process TLS handshake message
546 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
547 * @data: Data for TLS processing
548 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
549 * @peap_version: Version number for EAP-PEAP/TTLS
550 * @id: EAP identifier for the response
551 * @in_data: Message received from the server
552 * @in_len: Length of in_data
553 * @out_data: Buffer for returning a pointer to the response message
554 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
555 * is available, or -1 on failure
557 * This function can be used to process TLS handshake messages. It reassembles
558 * the received fragments and uses a TLS library to process the messages. The
559 * response data from the TLS library is fragmented to suitable output messages
560 * that the caller can send out.
562 * out_data is used to return the response message if the return value of this
563 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
564 * alarm message. The caller is responsible for freeing the allocated buffer if
565 * *out_data is not %NULL.
567 * This function is called for each received TLS message during the TLS
568 * handshake after eap_peer_tls_process_init() call and possible processing of
569 * TLS Flags field. Once the handshake has been completed, i.e., when
570 * tls_connection_established() returns 1, EAP method specific decrypting of
571 * the tunneled data is used.
573 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
574 EapType eap_type, int peap_version,
575 u8 id, const u8 *in_data, size_t in_len,
576 struct wpabuf **out_data)
582 if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
583 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
584 "fragments are waiting to be sent out");
588 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
590 * No more data to send out - expect to receive more data from
593 int res = eap_tls_process_input(sm, data, in_data, in_len,
597 * Input processing failed (res = -1) or more data is
604 * The incoming message has been reassembled and processed. The
605 * response was allocated into data->tls_out buffer.
609 if (data->tls_out == NULL) {
611 * No outgoing fragments remaining from the previous message
612 * and no new message generated. This indicates an error in TLS
615 eap_peer_tls_reset_output(data);
619 if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
620 /* TLS processing has failed - return error */
621 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
624 /* TODO: clean pin if engine used? */
627 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
629 * TLS negotiation should now be complete since all other cases
630 * needing more data should have been caught above based on
631 * the TLS Message Length field.
633 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
634 wpabuf_free(data->tls_out);
635 data->tls_out = NULL;
639 /* Send the pending message (in fragments, if needed). */
640 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
646 * eap_peer_tls_build_ack - Build a TLS ACK frame
647 * @id: EAP identifier for the response
648 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
649 * @peap_version: Version number for EAP-PEAP/TTLS
650 * Returns: Pointer to the allocated ACK frame or %NULL on failure
652 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
657 resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE,
661 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
662 (int) eap_type, id, peap_version);
663 wpabuf_put_u8(resp, peap_version); /* Flags */
669 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
670 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
671 * @data: Data for TLS processing
672 * Returns: 0 on success, -1 on failure
674 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
676 eap_peer_tls_reset_input(data);
677 eap_peer_tls_reset_output(data);
678 return tls_connection_shutdown(sm->ssl_ctx, data->conn);
683 * eap_peer_tls_status - Get TLS status
684 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
685 * @data: Data for TLS processing
686 * @buf: Buffer for status information
687 * @buflen: Maximum buffer length
688 * @verbose: Whether to include verbose status information
689 * Returns: Number of bytes written to buf.
691 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
692 char *buf, size_t buflen, int verbose)
697 if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) {
698 ret = os_snprintf(buf + len, buflen - len,
699 "EAP TLS cipher=%s\n", name);
700 if (ret < 0 || (size_t) ret >= buflen - len)
710 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
711 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
712 * @data: Data for TLS processing
713 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
714 * @ret: Return values from EAP request validation and processing
715 * @reqData: EAP request to be processed (eapReqData)
716 * @len: Buffer for returning length of the remaining payload
717 * @flags: Buffer for returning TLS flags
718 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
720 * This function validates the EAP header and processes the optional TLS
721 * Message Length field. If this is the first fragment of a TLS message, the
722 * TLS reassembly code is initialized to receive the indicated number of bytes.
724 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
725 * function as the first step in processing received messages. They will need
726 * to process the flags (apart from Message Length Included) that are returned
727 * through the flags pointer and the message payload that will be returned (and
728 * the length is returned through the len pointer). Return values (ret) are set
729 * for continuation of EAP method processing. The caller is responsible for
730 * setting these to indicate completion (either success or failure) based on
731 * the authentication result.
733 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
734 struct eap_ssl_data *data,
736 struct eap_method_ret *ret,
737 const struct wpabuf *reqData,
738 size_t *len, u8 *flags)
742 unsigned int tls_msg_len;
744 if (tls_get_errors(sm->ssl_ctx)) {
745 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
750 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left);
756 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
758 if (!sm->workaround) {
763 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
764 "indicates ACK frame");
770 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
771 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
773 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
775 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
780 tls_msg_len = WPA_GET_BE32(pos);
781 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
783 if (data->tls_in_left == 0) {
784 data->tls_in_total = tls_msg_len;
785 data->tls_in_left = tls_msg_len;
786 wpabuf_free(data->tls_in);
794 ret->methodState = METHOD_MAY_CONT;
795 ret->decision = DECISION_FAIL;
796 ret->allowNotifications = TRUE;
804 * eap_peer_tls_reset_input - Reset input buffers
805 * @data: Data for TLS processing
807 * This function frees any allocated memory for input buffers and resets input
810 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
812 data->tls_in_left = data->tls_in_total = 0;
813 wpabuf_free(data->tls_in);
819 * eap_peer_tls_reset_output - Reset output buffers
820 * @data: Data for TLS processing
822 * This function frees any allocated memory for output buffers and resets
825 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
827 data->tls_out_pos = 0;
828 wpabuf_free(data->tls_out);
829 data->tls_out = NULL;
834 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
835 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
836 * @data: Data for TLS processing
837 * @in_data: Message received from the server
838 * @in_decrypted: Buffer for returning a pointer to the decrypted message
839 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
841 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
842 const struct wpabuf *in_data,
843 struct wpabuf **in_decrypted)
845 const struct wpabuf *msg;
848 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
850 return need_more_input ? 1 : -1;
852 *in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg);
853 eap_peer_tls_reset_input(data);
854 if (*in_decrypted == NULL) {
855 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
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)
879 eap_peer_tls_reset_output(data);
880 data->tls_out = tls_connection_encrypt(sm->ssl_ctx, data->conn,
882 if (data->tls_out == NULL) {
883 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
885 (unsigned long) wpabuf_len(in_data));
886 eap_peer_tls_reset_output(data);
891 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
897 * eap_peer_select_phase2_methods - Select phase 2 EAP method
898 * @config: Pointer to the network configuration
899 * @prefix: 'phase2' configuration prefix, e.g., "auth="
900 * @types: Buffer for returning allocated list of allowed EAP methods
901 * @num_types: Buffer for returning number of allocated EAP methods
902 * Returns: 0 on success, -1 on failure
904 * This function is used to parse EAP method list and select allowed methods
905 * for Phase2 authentication.
907 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
909 struct eap_method_type **types,
912 char *start, *pos, *buf;
913 struct eap_method_type *methods = NULL, *_methods;
915 size_t num_methods = 0, prefix_len;
917 if (config == NULL || config->phase2 == NULL)
920 start = buf = os_strdup(config->phase2);
924 prefix_len = os_strlen(prefix);
926 while (start && *start != '\0') {
928 pos = os_strstr(start, prefix);
931 if (start != pos && *(pos - 1) != ' ') {
932 start = pos + prefix_len;
936 start = pos + prefix_len;
937 pos = os_strchr(start, ' ');
940 method = eap_get_phase2_type(start, &vendor);
941 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
942 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
943 "method '%s'", start);
946 _methods = os_realloc(methods,
947 num_methods * sizeof(*methods));
948 if (_methods == NULL) {
954 methods[num_methods - 1].vendor = vendor;
955 methods[num_methods - 1].method = method;
965 methods = eap_get_phase2_types(config, &num_methods);
967 if (methods == NULL) {
968 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
971 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
973 num_methods * sizeof(struct eap_method_type));
976 *num_types = num_methods;
983 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
984 * @types: Buffer for returning allocated list of allowed EAP methods
985 * @num_types: Buffer for returning number of allocated EAP methods
986 * @hdr: EAP-Request header (and the following EAP type octet)
987 * @resp: Buffer for returning the EAP-Nak message
988 * Returns: 0 on success, -1 on failure
990 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
991 struct eap_hdr *hdr, struct wpabuf **resp)
993 u8 *pos = (u8 *) (hdr + 1);
996 /* TODO: add support for expanded Nak */
997 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
998 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
999 (u8 *) types, num_types * sizeof(struct eap_method_type));
1000 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1001 EAP_CODE_RESPONSE, hdr->identifier);
1005 for (i = 0; i < num_types; i++) {
1006 if (types[i].vendor == EAP_VENDOR_IETF &&
1007 types[i].method < 256)
1008 wpabuf_put_u8(*resp, types[i].method);
1011 eap_update_len(*resp);