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 &&
163 data->eap_type != EAP_TYPE_TEAP) {
165 * Some deployed authentication servers seem to be unable to
166 * handle the TLS Session Ticket extension (they are supposed
167 * to ignore unrecognized TLS extensions, but end up rejecting
168 * the ClientHello instead). As a workaround, disable use of
169 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
170 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
171 * supports EAP-FAST does not need this workaround).
173 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
175 if (data->eap_type == EAP_TYPE_TEAP) {
176 /* RFC 7170 requires TLS v1.2 or newer to be used with TEAP */
177 params->flags |= TLS_CONN_DISABLE_TLSv1_0 |
178 TLS_CONN_DISABLE_TLSv1_1;
179 if (config->teap_anon_dh)
180 params->flags |= TLS_CONN_TEAP_ANON_DH;
182 if (data->eap_type == EAP_TYPE_FAST ||
183 data->eap_type == EAP_TYPE_TEAP ||
184 data->eap_type == EAP_TYPE_TTLS ||
185 data->eap_type == EAP_TYPE_PEAP) {
186 /* The current EAP peer implementation is not yet ready for the
187 * TLS v1.3 changes, so disable this by default for now. */
188 params->flags |= TLS_CONN_DISABLE_TLSv1_3;
190 if (data->eap_type == EAP_TYPE_TLS ||
191 data->eap_type == EAP_UNAUTH_TLS_TYPE ||
192 data->eap_type == EAP_WFA_UNAUTH_TLS_TYPE) {
193 /* While the current EAP-TLS implementation is more or less
194 * complete for TLS v1.3, there has been no interoperability
195 * testing with other implementations, so disable for by default
196 * for now until there has been chance to confirm that no
197 * significant interoperability issues show up with TLS version
200 params->flags |= TLS_CONN_DISABLE_TLSv1_3;
203 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
204 eap_tls_params_from_conf2(params, config);
206 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
207 eap_tls_params_from_conf1(params, config);
208 if (data->eap_type == EAP_TYPE_FAST)
209 params->flags |= TLS_CONN_EAP_FAST;
213 * Use blob data, if available. Otherwise, leave reference to external
216 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
217 ¶ms->ca_cert_blob_len) ||
218 eap_tls_check_blob(sm, ¶ms->client_cert,
219 ¶ms->client_cert_blob,
220 ¶ms->client_cert_blob_len) ||
221 eap_tls_check_blob(sm, ¶ms->private_key,
222 ¶ms->private_key_blob,
223 ¶ms->private_key_blob_len) ||
224 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
225 ¶ms->dh_blob_len)) {
226 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
230 params->openssl_ciphers = config->openssl_ciphers;
232 sm->ext_cert_check = !!(params->flags & TLS_CONN_EXT_CERT_CHECK);
238 static int eap_tls_init_connection(struct eap_sm *sm,
239 struct eap_ssl_data *data,
240 struct eap_peer_config *config,
241 struct tls_connection_params *params)
246 params->flags |= TLS_CONN_REQUEST_OCSP;
247 if (config->ocsp >= 2)
248 params->flags |= TLS_CONN_REQUIRE_OCSP;
249 if (config->ocsp == 3)
250 params->flags |= TLS_CONN_REQUIRE_OCSP_ALL;
251 data->conn = tls_connection_init(data->ssl_ctx);
252 if (data->conn == NULL) {
253 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
258 res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
259 if (res == TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN) {
261 * At this point with the pkcs11 engine the PIN is wrong. We
262 * reset the PIN in the configuration to be sure to not use it
263 * again and the calling function must request a new one.
266 "TLS: Bad PIN provided, requesting a new one");
267 os_free(config->pin);
269 eap_sm_request_pin(sm);
271 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
272 wpa_printf(MSG_INFO, "TLS: Failed to initialize engine");
273 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
274 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
278 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
280 tls_connection_deinit(data->ssl_ctx, data->conn);
290 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
291 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
292 * @data: Data for TLS processing
293 * @config: Pointer to the network configuration
294 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
295 * Returns: 0 on success, -1 on failure
297 * This function is used to initialize shared TLS functionality for EAP-TLS,
298 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
300 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
301 struct eap_peer_config *config, u8 eap_type)
303 struct tls_connection_params params;
309 data->eap_type = eap_type;
310 data->phase2 = sm->init_phase2;
311 data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
313 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
317 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
320 data->tls_out_limit = config->fragment_size;
322 /* Limit the fragment size in the inner TLS authentication
323 * since the outer authentication with EAP-PEAP does not yet
324 * support fragmentation */
325 if (data->tls_out_limit > 100)
326 data->tls_out_limit -= 100;
329 if (config->phase1 &&
330 os_strstr(config->phase1, "include_tls_length=1")) {
331 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
332 "unfragmented packets");
333 data->include_tls_length = 1;
341 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
342 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
343 * @data: Data for TLS processing
345 * This function deinitializes shared TLS functionality that was initialized
346 * with eap_peer_tls_ssl_init().
348 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
350 tls_connection_deinit(data->ssl_ctx, data->conn);
351 eap_peer_tls_reset_input(data);
352 eap_peer_tls_reset_output(data);
357 * eap_peer_tls_derive_key - Derive a key based on TLS session data
358 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
359 * @data: Data for TLS processing
360 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
361 * @context: Optional extra upper-layer context (max len 2^16)
362 * @context_len: The length of the context value
363 * @len: Length of the key material to generate (usually 64 for MSK)
364 * Returns: Pointer to allocated key on success or %NULL on failure
366 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
367 * session data (client/server random and master key). Each key type may use a
368 * different label to bind the key usage into the generated material.
370 * The caller is responsible for freeing the returned buffer.
372 * Note: To provide the RFC 5705 context, the context variable must be non-NULL.
374 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
375 const char *label, const u8 *context,
376 size_t context_len, size_t len)
380 out = os_malloc(len);
384 if (tls_connection_export_key(data->ssl_ctx, data->conn, label,
385 context, context_len, out, len)) {
395 * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
396 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
397 * @data: Data for TLS processing
398 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
399 * @len: Pointer to length of the session ID generated
400 * Returns: Pointer to allocated Session-Id on success or %NULL on failure
402 * This function derive the Session-Id based on the TLS session data
403 * (client/server random and method type).
405 * The caller is responsible for freeing the returned buffer.
407 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
408 struct eap_ssl_data *data, u8 eap_type,
411 struct tls_random keys;
414 if (eap_type == EAP_TYPE_TLS && data->tls_v13) {
416 const u8 context[] = { EAP_TYPE_TLS };
418 /* Session-Id = <EAP-Type> || Method-Id
419 * Method-Id = TLS-Exporter("EXPORTER_EAP_TLS_Method-Id",
423 id = os_malloc(*len);
426 method_id = eap_peer_tls_derive_key(
427 sm, data, "EXPORTER_EAP_TLS_Method-Id", context, 1, 64);
433 os_memcpy(id + 1, method_id, 64);
438 if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys) ||
439 keys.client_random == NULL || keys.server_random == NULL)
442 *len = 1 + keys.client_random_len + keys.server_random_len;
443 out = os_malloc(*len);
447 /* Session-Id = EAP type || client.random || server.random */
449 os_memcpy(out + 1, keys.client_random, keys.client_random_len);
450 os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
451 keys.server_random_len);
458 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
459 * @data: Data for TLS processing
460 * @in_data: Next incoming TLS segment
461 * Returns: 0 on success, 1 if more data is needed for the full message, or
464 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
465 const struct wpabuf *in_data)
467 size_t tls_in_len, in_len;
469 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
470 in_len = in_data ? wpabuf_len(in_data) : 0;
472 if (tls_in_len + in_len == 0) {
473 /* No message data received?! */
474 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
475 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
476 (unsigned long) data->tls_in_left,
477 (unsigned long) tls_in_len,
478 (unsigned long) in_len);
479 eap_peer_tls_reset_input(data);
483 if (tls_in_len + in_len > 65536) {
485 * Limit length to avoid rogue servers from causing large
486 * memory allocations.
488 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
490 eap_peer_tls_reset_input(data);
494 if (in_len > data->tls_in_left) {
495 /* Sender is doing something odd - reject message */
496 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
498 eap_peer_tls_reset_input(data);
502 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
503 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
505 eap_peer_tls_reset_input(data);
509 wpabuf_put_buf(data->tls_in, in_data);
510 data->tls_in_left -= in_len;
512 if (data->tls_in_left > 0) {
513 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
514 "data", (unsigned long) data->tls_in_left);
523 * eap_peer_tls_data_reassemble - Reassemble TLS data
524 * @data: Data for TLS processing
525 * @in_data: Next incoming TLS segment
526 * @need_more_input: Variable for returning whether more input data is needed
527 * to reassemble this TLS packet
528 * Returns: Pointer to output data, %NULL on error or when more data is needed
529 * for the full message (in which case, *need_more_input is also set to 1).
531 * This function reassembles TLS fragments. Caller must not free the returned
532 * data buffer since an internal pointer to it is maintained.
534 static const struct wpabuf * eap_peer_tls_data_reassemble(
535 struct eap_ssl_data *data, const struct wpabuf *in_data,
536 int *need_more_input)
538 *need_more_input = 0;
540 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
541 /* Message has fragments */
542 int res = eap_peer_tls_reassemble_fragment(data, in_data);
545 *need_more_input = 1;
549 /* Message is now fully reassembled. */
551 /* No fragments in this message, so just make a copy of it. */
552 data->tls_in_left = 0;
553 data->tls_in = wpabuf_dup(in_data);
554 if (data->tls_in == NULL)
563 * eap_tls_process_input - Process incoming TLS message
564 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
565 * @data: Data for TLS processing
566 * @in_data: Message received from the server
567 * @out_data: Buffer for returning a pointer to application data (if available)
568 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
569 * is available, -1 on failure
571 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
572 const struct wpabuf *in_data,
573 struct wpabuf **out_data)
575 const struct wpabuf *msg;
577 struct wpabuf *appl_data;
579 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
581 return need_more_input ? 1 : -1;
583 /* Full TLS message reassembled - continue handshake processing */
585 /* This should not happen.. */
586 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
587 "tls_out data even though tls_out_len = 0");
588 wpabuf_free(data->tls_out);
589 WPA_ASSERT(data->tls_out == NULL);
592 data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
595 eap_peer_tls_reset_input(data);
598 tls_connection_established(data->ssl_ctx, data->conn) &&
599 !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
600 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
602 *out_data = appl_data;
606 wpabuf_free(appl_data);
613 * eap_tls_process_output - Process outgoing TLS message
614 * @data: Data for TLS processing
615 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
616 * @peap_version: Version number for EAP-PEAP/TTLS
617 * @id: EAP identifier for the response
618 * @ret: Return value to use on success
619 * @out_data: Buffer for returning the allocated output buffer
620 * Returns: ret (0 or 1) on success, -1 on failure
622 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
623 int peap_version, u8 id, int ret,
624 struct wpabuf **out_data)
628 int more_fragments, length_included;
630 if (data->tls_out == NULL)
632 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
633 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
636 (unsigned long) wpabuf_len(data->tls_out));
639 * Limit outgoing message to the configured maximum size. Fragment
642 if (len > data->tls_out_limit) {
644 len = data->tls_out_limit;
645 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
646 "will follow", (unsigned long) len);
650 length_included = data->tls_out_pos == 0 &&
651 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
652 data->include_tls_length);
653 if (!length_included &&
654 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
655 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
657 * Windows Server 2008 NPS really wants to have the TLS Message
658 * length included in phase 0 even for unfragmented frames or
659 * it will get very confused with Compound MAC calculation and
665 *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
666 EAP_CODE_RESPONSE, id);
667 if (*out_data == NULL)
670 flags = wpabuf_put(*out_data, 1);
671 *flags = peap_version;
673 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
674 if (length_included) {
675 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
676 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
679 wpabuf_put_data(*out_data,
680 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
682 data->tls_out_pos += len;
685 eap_peer_tls_reset_output(data);
692 * eap_peer_tls_process_helper - Process TLS handshake message
693 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
694 * @data: Data for TLS processing
695 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
696 * @peap_version: Version number for EAP-PEAP/TTLS
697 * @id: EAP identifier for the response
698 * @in_data: Message received from the server
699 * @out_data: Buffer for returning a pointer to the response message
700 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
701 * is available, or -1 on failure
703 * This function can be used to process TLS handshake messages. It reassembles
704 * the received fragments and uses a TLS library to process the messages. The
705 * response data from the TLS library is fragmented to suitable output messages
706 * that the caller can send out.
708 * out_data is used to return the response message if the return value of this
709 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
710 * alarm message. The caller is responsible for freeing the allocated buffer if
711 * *out_data is not %NULL.
713 * This function is called for each received TLS message during the TLS
714 * handshake after eap_peer_tls_process_init() call and possible processing of
715 * TLS Flags field. Once the handshake has been completed, i.e., when
716 * tls_connection_established() returns 1, EAP method specific decrypting of
717 * the tunneled data is used.
719 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
720 EapType eap_type, int peap_version,
721 u8 id, const struct wpabuf *in_data,
722 struct wpabuf **out_data)
728 if (data->tls_out && wpabuf_len(data->tls_out) > 0 &&
729 wpabuf_len(in_data) > 0) {
730 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
731 "fragments are waiting to be sent out");
735 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
737 * No more data to send out - expect to receive more data from
740 int res = eap_tls_process_input(sm, data, in_data, out_data);
745 * Input processing failed (res = -1) or more data is
752 * The incoming message has been reassembled and processed. The
753 * response was allocated into data->tls_out buffer.
756 if (tls_get_version(data->ssl_ctx, data->conn,
757 buf, sizeof(buf)) == 0) {
758 wpa_printf(MSG_DEBUG, "SSL: Using TLS version %s", buf);
759 data->tls_v13 = os_strcmp(buf, "TLSv1.3") == 0;
763 if (data->tls_out == NULL) {
765 * No outgoing fragments remaining from the previous message
766 * and no new message generated. This indicates an error in TLS
769 eap_peer_tls_reset_output(data);
773 if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
774 /* TLS processing has failed - return error */
775 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
776 "report error (len=%u)",
777 (unsigned int) wpabuf_len(data->tls_out));
779 /* TODO: clean pin if engine used? */
780 if (wpabuf_len(data->tls_out) == 0) {
781 wpabuf_free(data->tls_out);
782 data->tls_out = NULL;
787 if (wpabuf_len(data->tls_out) == 0) {
789 * TLS negotiation should now be complete since all other cases
790 * needing more data should have been caught above based on
791 * the TLS Message Length field.
793 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
794 wpabuf_free(data->tls_out);
795 data->tls_out = NULL;
799 /* Send the pending message (in fragments, if needed). */
800 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
806 * eap_peer_tls_build_ack - Build a TLS ACK frame
807 * @id: EAP identifier for the response
808 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
809 * @peap_version: Version number for EAP-PEAP/TTLS
810 * Returns: Pointer to the allocated ACK frame or %NULL on failure
812 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
817 resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
820 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
821 (int) eap_type, id, peap_version);
822 wpabuf_put_u8(resp, peap_version); /* Flags */
828 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
829 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
830 * @data: Data for TLS processing
831 * Returns: 0 on success, -1 on failure
833 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
835 eap_peer_tls_reset_input(data);
836 eap_peer_tls_reset_output(data);
837 return tls_connection_shutdown(data->ssl_ctx, data->conn);
842 * eap_peer_tls_status - Get TLS status
843 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
844 * @data: Data for TLS processing
845 * @buf: Buffer for status information
846 * @buflen: Maximum buffer length
847 * @verbose: Whether to include verbose status information
848 * Returns: Number of bytes written to buf.
850 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
851 char *buf, size_t buflen, int verbose)
853 char version[20], name[128];
856 if (tls_get_version(data->ssl_ctx, data->conn, version,
857 sizeof(version)) < 0)
859 if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) < 0)
862 ret = os_snprintf(buf + len, buflen - len,
863 "eap_tls_version=%s\n"
864 "EAP TLS cipher=%s\n"
865 "tls_session_reused=%d\n",
867 tls_connection_resumed(data->ssl_ctx, data->conn));
868 if (os_snprintf_error(buflen - len, ret))
877 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
878 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
879 * @data: Data for TLS processing
880 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
881 * @ret: Return values from EAP request validation and processing
882 * @reqData: EAP request to be processed (eapReqData)
883 * @len: Buffer for returning length of the remaining payload
884 * @flags: Buffer for returning TLS flags
885 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
887 * This function validates the EAP header and processes the optional TLS
888 * Message Length field. If this is the first fragment of a TLS message, the
889 * TLS reassembly code is initialized to receive the indicated number of bytes.
891 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
892 * function as the first step in processing received messages. They will need
893 * to process the flags (apart from Message Length Included) that are returned
894 * through the flags pointer and the message payload that will be returned (and
895 * the length is returned through the len pointer). Return values (ret) are set
896 * for continuation of EAP method processing. The caller is responsible for
897 * setting these to indicate completion (either success or failure) based on
898 * the authentication result.
900 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
901 struct eap_ssl_data *data,
903 struct eap_method_ret *ret,
904 const struct wpabuf *reqData,
905 size_t *len, u8 *flags)
909 unsigned int tls_msg_len;
911 if (tls_get_errors(data->ssl_ctx)) {
912 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
917 if (eap_type == EAP_UNAUTH_TLS_TYPE)
918 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
919 EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
921 else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
922 pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
923 EAP_VENDOR_WFA_UNAUTH_TLS, reqData,
926 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
933 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
935 if (!sm->workaround) {
940 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
941 "indicates ACK frame");
947 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
948 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
950 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
952 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
957 tls_msg_len = WPA_GET_BE32(pos);
958 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
960 if (data->tls_in_left == 0) {
961 data->tls_in_total = tls_msg_len;
962 data->tls_in_left = tls_msg_len;
963 wpabuf_free(data->tls_in);
969 if (left > tls_msg_len) {
970 wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
971 "bytes) smaller than this fragment (%d "
972 "bytes)", (int) tls_msg_len, (int) left);
979 ret->methodState = METHOD_MAY_CONT;
980 ret->decision = DECISION_FAIL;
981 ret->allowNotifications = TRUE;
989 * eap_peer_tls_reset_input - Reset input buffers
990 * @data: Data for TLS processing
992 * This function frees any allocated memory for input buffers and resets input
995 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
997 data->tls_in_left = data->tls_in_total = 0;
998 wpabuf_free(data->tls_in);
1004 * eap_peer_tls_reset_output - Reset output buffers
1005 * @data: Data for TLS processing
1007 * This function frees any allocated memory for output buffers and resets
1010 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
1012 data->tls_out_pos = 0;
1013 wpabuf_free(data->tls_out);
1014 data->tls_out = NULL;
1019 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
1020 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1021 * @data: Data for TLS processing
1022 * @in_data: Message received from the server
1023 * @in_decrypted: Buffer for returning a pointer to the decrypted message
1024 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
1026 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1027 const struct wpabuf *in_data,
1028 struct wpabuf **in_decrypted)
1030 const struct wpabuf *msg;
1031 int need_more_input;
1033 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
1035 return need_more_input ? 1 : -1;
1037 *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
1038 eap_peer_tls_reset_input(data);
1039 if (*in_decrypted == NULL) {
1040 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
1048 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
1049 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1050 * @data: Data for TLS processing
1051 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
1052 * @peap_version: Version number for EAP-PEAP/TTLS
1053 * @id: EAP identifier for the response
1054 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
1055 * @out_data: Buffer for returning a pointer to the encrypted response message
1056 * Returns: 0 on success, -1 on failure
1058 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1059 EapType eap_type, int peap_version, u8 id,
1060 const struct wpabuf *in_data,
1061 struct wpabuf **out_data)
1064 eap_peer_tls_reset_output(data);
1065 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
1066 data->conn, in_data);
1067 if (data->tls_out == NULL) {
1068 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
1069 "data (in_len=%lu)",
1070 (unsigned long) wpabuf_len(in_data));
1071 eap_peer_tls_reset_output(data);
1076 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
1082 * eap_peer_select_phase2_methods - Select phase 2 EAP method
1083 * @config: Pointer to the network configuration
1084 * @prefix: 'phase2' configuration prefix, e.g., "auth="
1085 * @types: Buffer for returning allocated list of allowed EAP methods
1086 * @num_types: Buffer for returning number of allocated EAP methods
1087 * Returns: 0 on success, -1 on failure
1089 * This function is used to parse EAP method list and select allowed methods
1090 * for Phase2 authentication.
1092 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
1094 struct eap_method_type **types,
1097 char *start, *pos, *buf;
1098 struct eap_method_type *methods = NULL, *_methods;
1100 size_t num_methods = 0, prefix_len;
1102 if (config == NULL || config->phase2 == NULL)
1105 start = buf = os_strdup(config->phase2);
1109 prefix_len = os_strlen(prefix);
1111 while (start && *start != '\0') {
1113 pos = os_strstr(start, prefix);
1116 if (start != pos && *(pos - 1) != ' ') {
1117 start = pos + prefix_len;
1121 start = pos + prefix_len;
1122 pos = os_strchr(start, ' ');
1125 method = eap_get_phase2_type(start, &vendor);
1126 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
1127 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
1128 "method '%s'", start);
1134 _methods = os_realloc_array(methods, num_methods,
1136 if (_methods == NULL) {
1142 methods[num_methods - 1].vendor = vendor;
1143 methods[num_methods - 1].method = method;
1152 if (methods == NULL)
1153 methods = eap_get_phase2_types(config, &num_methods);
1155 if (methods == NULL) {
1156 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
1159 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1161 num_methods * sizeof(struct eap_method_type));
1164 *num_types = num_methods;
1171 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1172 * @types: Buffer for returning allocated list of allowed EAP methods
1173 * @num_types: Buffer for returning number of allocated EAP methods
1174 * @hdr: EAP-Request header (and the following EAP type octet)
1175 * @resp: Buffer for returning the EAP-Nak message
1176 * Returns: 0 on success, -1 on failure
1178 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1179 struct eap_hdr *hdr, struct wpabuf **resp)
1181 u8 *pos = (u8 *) (hdr + 1);
1184 /* TODO: add support for expanded Nak */
1185 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1186 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1187 (u8 *) types, num_types * sizeof(struct eap_method_type));
1188 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1189 EAP_CODE_RESPONSE, hdr->identifier);
1193 for (i = 0; i < num_types; i++) {
1194 if (types[i].vendor == EAP_VENDOR_IETF &&
1195 types[i].method < 256)
1196 wpabuf_put_u8(*resp, types[i].method);
1199 eap_update_len(*resp);