2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2019, 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 struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
20 u8 code, u8 identifier)
22 if (type == EAP_UNAUTH_TLS_TYPE)
23 return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
24 EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
26 if (type == EAP_WFA_UNAUTH_TLS_TYPE)
27 return eap_msg_alloc(EAP_VENDOR_WFA_NEW,
28 EAP_VENDOR_WFA_UNAUTH_TLS, payload_len,
30 return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
35 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
36 const u8 **data, size_t *data_len)
38 const struct wpa_config_blob *blob;
40 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
43 blob = eap_get_config_blob(sm, *name + 7);
45 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
46 "found", __func__, *name + 7);
52 *data_len = blob->len;
58 static void eap_tls_params_flags(struct tls_connection_params *params,
63 if (os_strstr(txt, "tls_allow_md5=1"))
64 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
65 if (os_strstr(txt, "tls_disable_time_checks=1"))
66 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
67 if (os_strstr(txt, "tls_disable_session_ticket=1"))
68 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
69 if (os_strstr(txt, "tls_disable_session_ticket=0"))
70 params->flags &= ~TLS_CONN_DISABLE_SESSION_TICKET;
71 if (os_strstr(txt, "tls_disable_tlsv1_0=1"))
72 params->flags |= TLS_CONN_DISABLE_TLSv1_0;
73 if (os_strstr(txt, "tls_disable_tlsv1_0=0")) {
74 params->flags &= ~TLS_CONN_DISABLE_TLSv1_0;
75 params->flags |= TLS_CONN_ENABLE_TLSv1_0;
77 if (os_strstr(txt, "tls_disable_tlsv1_1=1"))
78 params->flags |= TLS_CONN_DISABLE_TLSv1_1;
79 if (os_strstr(txt, "tls_disable_tlsv1_1=0")) {
80 params->flags &= ~TLS_CONN_DISABLE_TLSv1_1;
81 params->flags |= TLS_CONN_ENABLE_TLSv1_1;
83 if (os_strstr(txt, "tls_disable_tlsv1_2=1"))
84 params->flags |= TLS_CONN_DISABLE_TLSv1_2;
85 if (os_strstr(txt, "tls_disable_tlsv1_2=0")) {
86 params->flags &= ~TLS_CONN_DISABLE_TLSv1_2;
87 params->flags |= TLS_CONN_ENABLE_TLSv1_2;
89 if (os_strstr(txt, "tls_disable_tlsv1_3=1"))
90 params->flags |= TLS_CONN_DISABLE_TLSv1_3;
91 if (os_strstr(txt, "tls_disable_tlsv1_3=0"))
92 params->flags &= ~TLS_CONN_DISABLE_TLSv1_3;
93 if (os_strstr(txt, "tls_ext_cert_check=1"))
94 params->flags |= TLS_CONN_EXT_CERT_CHECK;
95 if (os_strstr(txt, "tls_ext_cert_check=0"))
96 params->flags &= ~TLS_CONN_EXT_CERT_CHECK;
97 if (os_strstr(txt, "tls_suiteb=1"))
98 params->flags |= TLS_CONN_SUITEB;
99 if (os_strstr(txt, "tls_suiteb=0"))
100 params->flags &= ~TLS_CONN_SUITEB;
101 if (os_strstr(txt, "tls_suiteb_no_ecdh=1"))
102 params->flags |= TLS_CONN_SUITEB_NO_ECDH;
103 if (os_strstr(txt, "tls_suiteb_no_ecdh=0"))
104 params->flags &= ~TLS_CONN_SUITEB_NO_ECDH;
108 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
109 struct eap_peer_config *config)
111 params->ca_cert = config->ca_cert;
112 params->ca_path = config->ca_path;
113 params->client_cert = config->client_cert;
114 params->private_key = config->private_key;
115 params->private_key_passwd = config->private_key_passwd;
116 params->dh_file = config->dh_file;
117 params->subject_match = config->subject_match;
118 params->altsubject_match = config->altsubject_match;
119 params->check_cert_subject = config->check_cert_subject;
120 params->suffix_match = config->domain_suffix_match;
121 params->domain_match = config->domain_match;
122 params->engine = config->engine;
123 params->engine_id = config->engine_id;
124 params->pin = config->pin;
125 params->key_id = config->key_id;
126 params->cert_id = config->cert_id;
127 params->ca_cert_id = config->ca_cert_id;
128 eap_tls_params_flags(params, config->phase1);
132 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
133 struct eap_peer_config *config)
135 params->ca_cert = config->ca_cert2;
136 params->ca_path = config->ca_path2;
137 params->client_cert = config->client_cert2;
138 params->private_key = config->private_key2;
139 params->private_key_passwd = config->private_key2_passwd;
140 params->dh_file = config->dh_file2;
141 params->subject_match = config->subject_match2;
142 params->altsubject_match = config->altsubject_match2;
143 params->check_cert_subject = config->check_cert_subject2;
144 params->suffix_match = config->domain_suffix_match2;
145 params->domain_match = config->domain_match2;
146 params->engine = config->engine2;
147 params->engine_id = config->engine2_id;
148 params->pin = config->pin2;
149 params->key_id = config->key2_id;
150 params->cert_id = config->cert2_id;
151 params->ca_cert_id = config->ca_cert2_id;
152 eap_tls_params_flags(params, config->phase2);
156 static int eap_tls_params_from_conf(struct eap_sm *sm,
157 struct eap_ssl_data *data,
158 struct tls_connection_params *params,
159 struct eap_peer_config *config, int phase2)
161 os_memset(params, 0, sizeof(*params));
162 if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
164 * Some deployed authentication servers seem to be unable to
165 * handle the TLS Session Ticket extension (they are supposed
166 * to ignore unrecognized TLS extensions, but end up rejecting
167 * the ClientHello instead). As a workaround, disable use of
168 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
169 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
170 * supports EAP-FAST does not need this workaround).
172 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
174 if (data->eap_type == EAP_TYPE_FAST ||
175 data->eap_type == EAP_TYPE_TTLS ||
176 data->eap_type == EAP_TYPE_PEAP) {
177 /* The current EAP peer implementation is not yet ready for the
178 * TLS v1.3 changes, so disable this by default for now. */
179 params->flags |= TLS_CONN_DISABLE_TLSv1_3;
181 if (data->eap_type == EAP_TYPE_TLS ||
182 data->eap_type == EAP_UNAUTH_TLS_TYPE ||
183 data->eap_type == EAP_WFA_UNAUTH_TLS_TYPE) {
184 /* While the current EAP-TLS implementation is more or less
185 * complete for TLS v1.3, there has been no interoperability
186 * testing with other implementations, so disable for by default
187 * for now until there has been chance to confirm that no
188 * significant interoperability issues show up with TLS version
191 params->flags |= TLS_CONN_DISABLE_TLSv1_3;
194 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
195 eap_tls_params_from_conf2(params, config);
197 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
198 eap_tls_params_from_conf1(params, config);
199 if (data->eap_type == EAP_TYPE_FAST)
200 params->flags |= TLS_CONN_EAP_FAST;
204 * Use blob data, if available. Otherwise, leave reference to external
207 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
208 ¶ms->ca_cert_blob_len) ||
209 eap_tls_check_blob(sm, ¶ms->client_cert,
210 ¶ms->client_cert_blob,
211 ¶ms->client_cert_blob_len) ||
212 eap_tls_check_blob(sm, ¶ms->private_key,
213 ¶ms->private_key_blob,
214 ¶ms->private_key_blob_len) ||
215 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
216 ¶ms->dh_blob_len)) {
217 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
221 params->openssl_ciphers = config->openssl_ciphers;
223 sm->ext_cert_check = !!(params->flags & TLS_CONN_EXT_CERT_CHECK);
229 static int eap_tls_init_connection(struct eap_sm *sm,
230 struct eap_ssl_data *data,
231 struct eap_peer_config *config,
232 struct tls_connection_params *params)
237 params->flags |= TLS_CONN_REQUEST_OCSP;
238 if (config->ocsp >= 2)
239 params->flags |= TLS_CONN_REQUIRE_OCSP;
240 if (config->ocsp == 3)
241 params->flags |= TLS_CONN_REQUIRE_OCSP_ALL;
242 data->conn = tls_connection_init(data->ssl_ctx);
243 if (data->conn == NULL) {
244 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
249 res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
250 if (res == TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN) {
252 * At this point with the pkcs11 engine the PIN is wrong. We
253 * reset the PIN in the configuration to be sure to not use it
254 * again and the calling function must request a new one.
257 "TLS: Bad PIN provided, requesting a new one");
258 os_free(config->pin);
260 eap_sm_request_pin(sm);
262 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
263 wpa_printf(MSG_INFO, "TLS: Failed to initialize engine");
264 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
265 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
269 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
271 tls_connection_deinit(data->ssl_ctx, data->conn);
281 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
282 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
283 * @data: Data for TLS processing
284 * @config: Pointer to the network configuration
285 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
286 * Returns: 0 on success, -1 on failure
288 * This function is used to initialize shared TLS functionality for EAP-TLS,
289 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
291 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
292 struct eap_peer_config *config, u8 eap_type)
294 struct tls_connection_params params;
300 data->eap_type = eap_type;
301 data->phase2 = sm->init_phase2;
302 data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
304 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
308 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
311 data->tls_out_limit = config->fragment_size;
313 /* Limit the fragment size in the inner TLS authentication
314 * since the outer authentication with EAP-PEAP does not yet
315 * support fragmentation */
316 if (data->tls_out_limit > 100)
317 data->tls_out_limit -= 100;
320 if (config->phase1 &&
321 os_strstr(config->phase1, "include_tls_length=1")) {
322 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
323 "unfragmented packets");
324 data->include_tls_length = 1;
332 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
333 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
334 * @data: Data for TLS processing
336 * This function deinitializes shared TLS functionality that was initialized
337 * with eap_peer_tls_ssl_init().
339 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
341 tls_connection_deinit(data->ssl_ctx, data->conn);
342 eap_peer_tls_reset_input(data);
343 eap_peer_tls_reset_output(data);
348 * eap_peer_tls_derive_key - Derive a key based on TLS session data
349 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
350 * @data: Data for TLS processing
351 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
352 * @context: Optional extra upper-layer context (max len 2^16)
353 * @context_len: The length of the context value
354 * @len: Length of the key material to generate (usually 64 for MSK)
355 * Returns: Pointer to allocated key on success or %NULL on failure
357 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
358 * session data (client/server random and master key). Each key type may use a
359 * different label to bind the key usage into the generated material.
361 * The caller is responsible for freeing the returned buffer.
363 * Note: To provide the RFC 5705 context, the context variable must be non-NULL.
365 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
366 const char *label, const u8 *context,
367 size_t context_len, size_t len)
371 out = os_malloc(len);
375 if (tls_connection_export_key(data->ssl_ctx, data->conn, label,
376 context, context_len, out, len)) {
386 * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
387 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
388 * @data: Data for TLS processing
389 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
390 * @len: Pointer to length of the session ID generated
391 * Returns: Pointer to allocated Session-Id on success or %NULL on failure
393 * This function derive the Session-Id based on the TLS session data
394 * (client/server random and method type).
396 * The caller is responsible for freeing the returned buffer.
398 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
399 struct eap_ssl_data *data, u8 eap_type,
402 struct tls_random keys;
405 if (eap_type == EAP_TYPE_TLS && data->tls_v13) {
408 /* Session-Id = <EAP-Type> || Method-Id
409 * Method-Id = TLS-Exporter("EXPORTER_EAP_TLS_Method-Id",
413 id = os_malloc(*len);
416 method_id = eap_peer_tls_derive_key(
417 sm, data, "EXPORTER_EAP_TLS_Method-Id", NULL, 0, 64);
423 os_memcpy(id + 1, method_id, 64);
428 if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys) ||
429 keys.client_random == NULL || keys.server_random == NULL)
432 *len = 1 + keys.client_random_len + keys.server_random_len;
433 out = os_malloc(*len);
437 /* Session-Id = EAP type || client.random || server.random */
439 os_memcpy(out + 1, keys.client_random, keys.client_random_len);
440 os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
441 keys.server_random_len);
448 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
449 * @data: Data for TLS processing
450 * @in_data: Next incoming TLS segment
451 * Returns: 0 on success, 1 if more data is needed for the full message, or
454 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
455 const struct wpabuf *in_data)
457 size_t tls_in_len, in_len;
459 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
460 in_len = in_data ? wpabuf_len(in_data) : 0;
462 if (tls_in_len + in_len == 0) {
463 /* No message data received?! */
464 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
465 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
466 (unsigned long) data->tls_in_left,
467 (unsigned long) tls_in_len,
468 (unsigned long) in_len);
469 eap_peer_tls_reset_input(data);
473 if (tls_in_len + in_len > 65536) {
475 * Limit length to avoid rogue servers from causing large
476 * memory allocations.
478 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
480 eap_peer_tls_reset_input(data);
484 if (in_len > data->tls_in_left) {
485 /* Sender is doing something odd - reject message */
486 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
488 eap_peer_tls_reset_input(data);
492 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
493 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
495 eap_peer_tls_reset_input(data);
499 wpabuf_put_buf(data->tls_in, in_data);
500 data->tls_in_left -= in_len;
502 if (data->tls_in_left > 0) {
503 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
504 "data", (unsigned long) data->tls_in_left);
513 * eap_peer_tls_data_reassemble - Reassemble TLS data
514 * @data: Data for TLS processing
515 * @in_data: Next incoming TLS segment
516 * @need_more_input: Variable for returning whether more input data is needed
517 * to reassemble this TLS packet
518 * Returns: Pointer to output data, %NULL on error or when more data is needed
519 * for the full message (in which case, *need_more_input is also set to 1).
521 * This function reassembles TLS fragments. Caller must not free the returned
522 * data buffer since an internal pointer to it is maintained.
524 static const struct wpabuf * eap_peer_tls_data_reassemble(
525 struct eap_ssl_data *data, const struct wpabuf *in_data,
526 int *need_more_input)
528 *need_more_input = 0;
530 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
531 /* Message has fragments */
532 int res = eap_peer_tls_reassemble_fragment(data, in_data);
535 *need_more_input = 1;
539 /* Message is now fully reassembled. */
541 /* No fragments in this message, so just make a copy of it. */
542 data->tls_in_left = 0;
543 data->tls_in = wpabuf_dup(in_data);
544 if (data->tls_in == NULL)
553 * eap_tls_process_input - Process incoming TLS message
554 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
555 * @data: Data for TLS processing
556 * @in_data: Message received from the server
557 * @out_data: Buffer for returning a pointer to application data (if available)
558 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
559 * is available, -1 on failure
561 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
562 const struct wpabuf *in_data,
563 struct wpabuf **out_data)
565 const struct wpabuf *msg;
567 struct wpabuf *appl_data;
569 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
571 return need_more_input ? 1 : -1;
573 /* Full TLS message reassembled - continue handshake processing */
575 /* This should not happen.. */
576 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
577 "tls_out data even though tls_out_len = 0");
578 wpabuf_free(data->tls_out);
579 WPA_ASSERT(data->tls_out == NULL);
582 data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
585 eap_peer_tls_reset_input(data);
588 tls_connection_established(data->ssl_ctx, data->conn) &&
589 !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
590 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
592 *out_data = appl_data;
596 wpabuf_free(appl_data);
603 * eap_tls_process_output - Process outgoing TLS message
604 * @data: Data for TLS processing
605 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
606 * @peap_version: Version number for EAP-PEAP/TTLS
607 * @id: EAP identifier for the response
608 * @ret: Return value to use on success
609 * @out_data: Buffer for returning the allocated output buffer
610 * Returns: ret (0 or 1) on success, -1 on failure
612 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
613 int peap_version, u8 id, int ret,
614 struct wpabuf **out_data)
618 int more_fragments, length_included;
620 if (data->tls_out == NULL)
622 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
623 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
626 (unsigned long) wpabuf_len(data->tls_out));
629 * Limit outgoing message to the configured maximum size. Fragment
632 if (len > data->tls_out_limit) {
634 len = data->tls_out_limit;
635 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
636 "will follow", (unsigned long) len);
640 length_included = data->tls_out_pos == 0 &&
641 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
642 data->include_tls_length);
643 if (!length_included &&
644 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
645 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
647 * Windows Server 2008 NPS really wants to have the TLS Message
648 * length included in phase 0 even for unfragmented frames or
649 * it will get very confused with Compound MAC calculation and
655 *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
656 EAP_CODE_RESPONSE, id);
657 if (*out_data == NULL)
660 flags = wpabuf_put(*out_data, 1);
661 *flags = peap_version;
663 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
664 if (length_included) {
665 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
666 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
669 wpabuf_put_data(*out_data,
670 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
672 data->tls_out_pos += len;
675 eap_peer_tls_reset_output(data);
682 * eap_peer_tls_process_helper - Process TLS handshake message
683 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
684 * @data: Data for TLS processing
685 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
686 * @peap_version: Version number for EAP-PEAP/TTLS
687 * @id: EAP identifier for the response
688 * @in_data: Message received from the server
689 * @out_data: Buffer for returning a pointer to the response message
690 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
691 * is available, or -1 on failure
693 * This function can be used to process TLS handshake messages. It reassembles
694 * the received fragments and uses a TLS library to process the messages. The
695 * response data from the TLS library is fragmented to suitable output messages
696 * that the caller can send out.
698 * out_data is used to return the response message if the return value of this
699 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
700 * alarm message. The caller is responsible for freeing the allocated buffer if
701 * *out_data is not %NULL.
703 * This function is called for each received TLS message during the TLS
704 * handshake after eap_peer_tls_process_init() call and possible processing of
705 * TLS Flags field. Once the handshake has been completed, i.e., when
706 * tls_connection_established() returns 1, EAP method specific decrypting of
707 * the tunneled data is used.
709 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
710 EapType eap_type, int peap_version,
711 u8 id, const struct wpabuf *in_data,
712 struct wpabuf **out_data)
718 if (data->tls_out && wpabuf_len(data->tls_out) > 0 &&
719 wpabuf_len(in_data) > 0) {
720 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
721 "fragments are waiting to be sent out");
725 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
727 * No more data to send out - expect to receive more data from
730 int res = eap_tls_process_input(sm, data, in_data, out_data);
735 * Input processing failed (res = -1) or more data is
742 * The incoming message has been reassembled and processed. The
743 * response was allocated into data->tls_out buffer.
746 if (tls_get_version(data->ssl_ctx, data->conn,
747 buf, sizeof(buf)) == 0) {
748 wpa_printf(MSG_DEBUG, "SSL: Using TLS version %s", buf);
749 data->tls_v13 = os_strcmp(buf, "TLSv1.3") == 0;
753 if (data->tls_out == NULL) {
755 * No outgoing fragments remaining from the previous message
756 * and no new message generated. This indicates an error in TLS
759 eap_peer_tls_reset_output(data);
763 if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
764 /* TLS processing has failed - return error */
765 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
766 "report error (len=%u)",
767 (unsigned int) wpabuf_len(data->tls_out));
769 /* TODO: clean pin if engine used? */
770 if (wpabuf_len(data->tls_out) == 0) {
771 wpabuf_free(data->tls_out);
772 data->tls_out = NULL;
777 if (wpabuf_len(data->tls_out) == 0) {
779 * TLS negotiation should now be complete since all other cases
780 * needing more data should have been caught above based on
781 * the TLS Message Length field.
783 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
784 wpabuf_free(data->tls_out);
785 data->tls_out = NULL;
789 /* Send the pending message (in fragments, if needed). */
790 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
796 * eap_peer_tls_build_ack - Build a TLS ACK frame
797 * @id: EAP identifier for the response
798 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
799 * @peap_version: Version number for EAP-PEAP/TTLS
800 * Returns: Pointer to the allocated ACK frame or %NULL on failure
802 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
807 resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
810 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
811 (int) eap_type, id, peap_version);
812 wpabuf_put_u8(resp, peap_version); /* Flags */
818 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
819 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
820 * @data: Data for TLS processing
821 * Returns: 0 on success, -1 on failure
823 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
825 eap_peer_tls_reset_input(data);
826 eap_peer_tls_reset_output(data);
827 return tls_connection_shutdown(data->ssl_ctx, data->conn);
832 * eap_peer_tls_status - Get TLS status
833 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
834 * @data: Data for TLS processing
835 * @buf: Buffer for status information
836 * @buflen: Maximum buffer length
837 * @verbose: Whether to include verbose status information
838 * Returns: Number of bytes written to buf.
840 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
841 char *buf, size_t buflen, int verbose)
843 char version[20], name[128];
846 if (tls_get_version(data->ssl_ctx, data->conn, version,
847 sizeof(version)) < 0)
849 if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) < 0)
852 ret = os_snprintf(buf + len, buflen - len,
853 "eap_tls_version=%s\n"
854 "EAP TLS cipher=%s\n"
855 "tls_session_reused=%d\n",
857 tls_connection_resumed(data->ssl_ctx, data->conn));
858 if (os_snprintf_error(buflen - len, ret))
867 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
868 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
869 * @data: Data for TLS processing
870 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
871 * @ret: Return values from EAP request validation and processing
872 * @reqData: EAP request to be processed (eapReqData)
873 * @len: Buffer for returning length of the remaining payload
874 * @flags: Buffer for returning TLS flags
875 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
877 * This function validates the EAP header and processes the optional TLS
878 * Message Length field. If this is the first fragment of a TLS message, the
879 * TLS reassembly code is initialized to receive the indicated number of bytes.
881 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
882 * function as the first step in processing received messages. They will need
883 * to process the flags (apart from Message Length Included) that are returned
884 * through the flags pointer and the message payload that will be returned (and
885 * the length is returned through the len pointer). Return values (ret) are set
886 * for continuation of EAP method processing. The caller is responsible for
887 * setting these to indicate completion (either success or failure) based on
888 * the authentication result.
890 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
891 struct eap_ssl_data *data,
893 struct eap_method_ret *ret,
894 const struct wpabuf *reqData,
895 size_t *len, u8 *flags)
899 unsigned int tls_msg_len;
901 if (tls_get_errors(data->ssl_ctx)) {
902 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
907 if (eap_type == EAP_UNAUTH_TLS_TYPE)
908 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
909 EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
911 else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
912 pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
913 EAP_VENDOR_WFA_UNAUTH_TLS, reqData,
916 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
923 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
925 if (!sm->workaround) {
930 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
931 "indicates ACK frame");
937 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
938 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
940 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
942 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
947 tls_msg_len = WPA_GET_BE32(pos);
948 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
950 if (data->tls_in_left == 0) {
951 data->tls_in_total = tls_msg_len;
952 data->tls_in_left = tls_msg_len;
953 wpabuf_free(data->tls_in);
959 if (left > tls_msg_len) {
960 wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
961 "bytes) smaller than this fragment (%d "
962 "bytes)", (int) tls_msg_len, (int) left);
969 ret->methodState = METHOD_MAY_CONT;
970 ret->decision = DECISION_FAIL;
971 ret->allowNotifications = TRUE;
979 * eap_peer_tls_reset_input - Reset input buffers
980 * @data: Data for TLS processing
982 * This function frees any allocated memory for input buffers and resets input
985 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
987 data->tls_in_left = data->tls_in_total = 0;
988 wpabuf_free(data->tls_in);
994 * eap_peer_tls_reset_output - Reset output buffers
995 * @data: Data for TLS processing
997 * This function frees any allocated memory for output buffers and resets
1000 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
1002 data->tls_out_pos = 0;
1003 wpabuf_free(data->tls_out);
1004 data->tls_out = NULL;
1009 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
1010 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1011 * @data: Data for TLS processing
1012 * @in_data: Message received from the server
1013 * @in_decrypted: Buffer for returning a pointer to the decrypted message
1014 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
1016 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1017 const struct wpabuf *in_data,
1018 struct wpabuf **in_decrypted)
1020 const struct wpabuf *msg;
1021 int need_more_input;
1023 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
1025 return need_more_input ? 1 : -1;
1027 *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
1028 eap_peer_tls_reset_input(data);
1029 if (*in_decrypted == NULL) {
1030 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
1038 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
1039 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1040 * @data: Data for TLS processing
1041 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
1042 * @peap_version: Version number for EAP-PEAP/TTLS
1043 * @id: EAP identifier for the response
1044 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
1045 * @out_data: Buffer for returning a pointer to the encrypted response message
1046 * Returns: 0 on success, -1 on failure
1048 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1049 EapType eap_type, int peap_version, u8 id,
1050 const struct wpabuf *in_data,
1051 struct wpabuf **out_data)
1054 eap_peer_tls_reset_output(data);
1055 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
1056 data->conn, in_data);
1057 if (data->tls_out == NULL) {
1058 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
1059 "data (in_len=%lu)",
1060 (unsigned long) wpabuf_len(in_data));
1061 eap_peer_tls_reset_output(data);
1066 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
1072 * eap_peer_select_phase2_methods - Select phase 2 EAP method
1073 * @config: Pointer to the network configuration
1074 * @prefix: 'phase2' configuration prefix, e.g., "auth="
1075 * @types: Buffer for returning allocated list of allowed EAP methods
1076 * @num_types: Buffer for returning number of allocated EAP methods
1077 * Returns: 0 on success, -1 on failure
1079 * This function is used to parse EAP method list and select allowed methods
1080 * for Phase2 authentication.
1082 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
1084 struct eap_method_type **types,
1087 char *start, *pos, *buf;
1088 struct eap_method_type *methods = NULL, *_methods;
1090 size_t num_methods = 0, prefix_len;
1092 if (config == NULL || config->phase2 == NULL)
1095 start = buf = os_strdup(config->phase2);
1099 prefix_len = os_strlen(prefix);
1101 while (start && *start != '\0') {
1103 pos = os_strstr(start, prefix);
1106 if (start != pos && *(pos - 1) != ' ') {
1107 start = pos + prefix_len;
1111 start = pos + prefix_len;
1112 pos = os_strchr(start, ' ');
1115 method = eap_get_phase2_type(start, &vendor);
1116 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
1117 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
1118 "method '%s'", start);
1124 _methods = os_realloc_array(methods, num_methods,
1126 if (_methods == NULL) {
1132 methods[num_methods - 1].vendor = vendor;
1133 methods[num_methods - 1].method = method;
1142 if (methods == NULL)
1143 methods = eap_get_phase2_types(config, &num_methods);
1145 if (methods == NULL) {
1146 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
1149 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1151 num_methods * sizeof(struct eap_method_type));
1154 *num_types = num_methods;
1161 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1162 * @types: Buffer for returning allocated list of allowed EAP methods
1163 * @num_types: Buffer for returning number of allocated EAP methods
1164 * @hdr: EAP-Request header (and the following EAP type octet)
1165 * @resp: Buffer for returning the EAP-Nak message
1166 * Returns: 0 on success, -1 on failure
1168 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1169 struct eap_hdr *hdr, struct wpabuf **resp)
1171 u8 *pos = (u8 *) (hdr + 1);
1174 /* TODO: add support for expanded Nak */
1175 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1176 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1177 (u8 *) types, num_types * sizeof(struct eap_method_type));
1178 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1179 EAP_CODE_RESPONSE, hdr->identifier);
1183 for (i = 0; i < num_types; i++) {
1184 if (types[i].vendor == EAP_VENDOR_IETF &&
1185 types[i].method < 256)
1186 wpabuf_put_u8(*resp, types[i].method);
1189 eap_update_len(*resp);