2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2013, 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 if (os_strstr(txt, "tls_disable_tlsv1_1=1"))
76 params->flags |= TLS_CONN_DISABLE_TLSv1_1;
77 if (os_strstr(txt, "tls_disable_tlsv1_1=0"))
78 params->flags &= ~TLS_CONN_DISABLE_TLSv1_1;
79 if (os_strstr(txt, "tls_disable_tlsv1_2=1"))
80 params->flags |= TLS_CONN_DISABLE_TLSv1_2;
81 if (os_strstr(txt, "tls_disable_tlsv1_2=0"))
82 params->flags &= ~TLS_CONN_DISABLE_TLSv1_2;
86 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
87 struct eap_peer_config *config)
89 params->ca_cert = (char *) config->ca_cert;
90 params->ca_path = (char *) config->ca_path;
91 params->client_cert = (char *) config->client_cert;
92 params->private_key = (char *) config->private_key;
93 params->private_key_passwd = (char *) config->private_key_passwd;
94 params->dh_file = (char *) config->dh_file;
95 params->subject_match = (char *) config->subject_match;
96 params->altsubject_match = (char *) config->altsubject_match;
97 params->suffix_match = config->domain_suffix_match;
98 params->domain_match = config->domain_match;
99 params->engine = config->engine;
100 params->engine_id = config->engine_id;
101 params->pin = config->pin;
102 params->key_id = config->key_id;
103 params->cert_id = config->cert_id;
104 params->ca_cert_id = config->ca_cert_id;
105 eap_tls_params_flags(params, config->phase1);
106 params->validate_ca_cb = config->validate_ca_cb;
107 params->validate_ca_ctx = config->validate_ca_ctx;
111 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
112 struct eap_peer_config *config)
114 params->ca_cert = (char *) config->ca_cert2;
115 params->ca_path = (char *) config->ca_path2;
116 params->client_cert = (char *) config->client_cert2;
117 params->private_key = (char *) config->private_key2;
118 params->private_key_passwd = (char *) config->private_key2_passwd;
119 params->dh_file = (char *) config->dh_file2;
120 params->subject_match = (char *) config->subject_match2;
121 params->altsubject_match = (char *) config->altsubject_match2;
122 params->suffix_match = config->domain_suffix_match2;
123 params->domain_match = config->domain_match2;
124 params->engine = config->engine2;
125 params->engine_id = config->engine2_id;
126 params->pin = config->pin2;
127 params->key_id = config->key2_id;
128 params->cert_id = config->cert2_id;
129 params->ca_cert_id = config->ca_cert2_id;
130 eap_tls_params_flags(params, config->phase2);
134 static int eap_tls_params_from_conf(struct eap_sm *sm,
135 struct eap_ssl_data *data,
136 struct tls_connection_params *params,
137 struct eap_peer_config *config, int phase2)
139 os_memset(params, 0, sizeof(*params));
140 if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
142 * Some deployed authentication servers seem to be unable to
143 * handle the TLS Session Ticket extension (they are supposed
144 * to ignore unrecognized TLS extensions, but end up rejecting
145 * the ClientHello instead). As a workaround, disable use of
146 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
147 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
148 * supports EAP-FAST does not need this workaround).
150 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
153 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
154 eap_tls_params_from_conf2(params, config);
156 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
157 eap_tls_params_from_conf1(params, config);
158 if (data->eap_type == EAP_TYPE_FAST)
159 params->flags |= TLS_CONN_EAP_FAST;
163 * Use blob data, if available. Otherwise, leave reference to external
166 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
167 ¶ms->ca_cert_blob_len) ||
168 eap_tls_check_blob(sm, ¶ms->client_cert,
169 ¶ms->client_cert_blob,
170 ¶ms->client_cert_blob_len) ||
171 eap_tls_check_blob(sm, ¶ms->private_key,
172 ¶ms->private_key_blob,
173 ¶ms->private_key_blob_len) ||
174 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
175 ¶ms->dh_blob_len)) {
176 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
180 params->openssl_ciphers = config->openssl_ciphers;
186 static int eap_tls_init_connection(struct eap_sm *sm,
187 struct eap_ssl_data *data,
188 struct eap_peer_config *config,
189 struct tls_connection_params *params)
194 params->flags |= TLS_CONN_REQUEST_OCSP;
195 if (config->ocsp == 2)
196 params->flags |= TLS_CONN_REQUIRE_OCSP;
197 data->conn = tls_connection_init(data->ssl_ctx);
198 if (data->conn == NULL) {
199 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
204 res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
205 if (res == TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN) {
207 * At this point with the pkcs11 engine the PIN is wrong. We
208 * reset the PIN in the configuration to be sure to not use it
209 * again and the calling function must request a new one.
212 "TLS: Bad PIN provided, requesting a new one");
213 os_free(config->pin);
215 eap_sm_request_pin(sm);
217 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
218 wpa_printf(MSG_INFO, "TLS: Failed to initialize engine");
219 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
220 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
224 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
226 tls_connection_deinit(data->ssl_ctx, data->conn);
236 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
237 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
238 * @data: Data for TLS processing
239 * @config: Pointer to the network configuration
240 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
241 * Returns: 0 on success, -1 on failure
243 * This function is used to initialize shared TLS functionality for EAP-TLS,
244 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
246 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
247 struct eap_peer_config *config, u8 eap_type)
249 struct tls_connection_params params;
255 data->eap_type = eap_type;
256 data->phase2 = sm->init_phase2;
257 data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
259 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
263 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
266 data->tls_out_limit = config->fragment_size;
268 /* Limit the fragment size in the inner TLS authentication
269 * since the outer authentication with EAP-PEAP does not yet
270 * support fragmentation */
271 if (data->tls_out_limit > 100)
272 data->tls_out_limit -= 100;
275 if (config->phase1 &&
276 os_strstr(config->phase1, "include_tls_length=1")) {
277 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
278 "unfragmented packets");
279 data->include_tls_length = 1;
287 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
288 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
289 * @data: Data for TLS processing
291 * This function deinitializes shared TLS functionality that was initialized
292 * with eap_peer_tls_ssl_init().
294 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
296 tls_connection_deinit(data->ssl_ctx, data->conn);
297 eap_peer_tls_reset_input(data);
298 eap_peer_tls_reset_output(data);
303 * eap_peer_tls_derive_key - Derive a key based on TLS session data
304 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
305 * @data: Data for TLS processing
306 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
307 * @len: Length of the key material to generate (usually 64 for MSK)
308 * Returns: Pointer to allocated key on success or %NULL on failure
310 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
311 * session data (client/server random and master key). Each key type may use a
312 * different label to bind the key usage into the generated material.
314 * The caller is responsible for freeing the returned buffer.
316 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
317 const char *label, size_t len)
321 out = os_malloc(len);
325 if (tls_connection_prf(data->ssl_ctx, data->conn, label, 0, 0,
336 * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
337 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
338 * @data: Data for TLS processing
339 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
340 * @len: Pointer to length of the session ID generated
341 * Returns: Pointer to allocated Session-Id on success or %NULL on failure
343 * This function derive the Session-Id based on the TLS session data
344 * (client/server random and method type).
346 * The caller is responsible for freeing the returned buffer.
348 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
349 struct eap_ssl_data *data, u8 eap_type,
352 struct tls_random keys;
355 if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys))
358 if (keys.client_random == NULL || keys.server_random == NULL)
361 *len = 1 + keys.client_random_len + keys.server_random_len;
362 out = os_malloc(*len);
366 /* Session-Id = EAP type || client.random || server.random */
368 os_memcpy(out + 1, keys.client_random, keys.client_random_len);
369 os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
370 keys.server_random_len);
377 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
378 * @data: Data for TLS processing
379 * @in_data: Next incoming TLS segment
380 * Returns: 0 on success, 1 if more data is needed for the full message, or
383 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
384 const struct wpabuf *in_data)
386 size_t tls_in_len, in_len;
388 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
389 in_len = in_data ? wpabuf_len(in_data) : 0;
391 if (tls_in_len + in_len == 0) {
392 /* No message data received?! */
393 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
394 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
395 (unsigned long) data->tls_in_left,
396 (unsigned long) tls_in_len,
397 (unsigned long) in_len);
398 eap_peer_tls_reset_input(data);
402 if (tls_in_len + in_len > 65536) {
404 * Limit length to avoid rogue servers from causing large
405 * memory allocations.
407 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
409 eap_peer_tls_reset_input(data);
413 if (in_len > data->tls_in_left) {
414 /* Sender is doing something odd - reject message */
415 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
417 eap_peer_tls_reset_input(data);
421 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
422 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
424 eap_peer_tls_reset_input(data);
428 wpabuf_put_buf(data->tls_in, in_data);
429 data->tls_in_left -= in_len;
431 if (data->tls_in_left > 0) {
432 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
433 "data", (unsigned long) data->tls_in_left);
442 * eap_peer_tls_data_reassemble - Reassemble TLS data
443 * @data: Data for TLS processing
444 * @in_data: Next incoming TLS segment
445 * @need_more_input: Variable for returning whether more input data is needed
446 * to reassemble this TLS packet
447 * Returns: Pointer to output data, %NULL on error or when more data is needed
448 * for the full message (in which case, *need_more_input is also set to 1).
450 * This function reassembles TLS fragments. Caller must not free the returned
451 * data buffer since an internal pointer to it is maintained.
453 static const struct wpabuf * eap_peer_tls_data_reassemble(
454 struct eap_ssl_data *data, const struct wpabuf *in_data,
455 int *need_more_input)
457 *need_more_input = 0;
459 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
460 /* Message has fragments */
461 int res = eap_peer_tls_reassemble_fragment(data, in_data);
464 *need_more_input = 1;
468 /* Message is now fully reassembled. */
470 /* No fragments in this message, so just make a copy of it. */
471 data->tls_in_left = 0;
472 data->tls_in = wpabuf_dup(in_data);
473 if (data->tls_in == NULL)
482 * eap_tls_process_input - Process incoming TLS message
483 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
484 * @data: Data for TLS processing
485 * @in_data: Message received from the server
486 * @out_data: Buffer for returning a pointer to application data (if available)
487 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
488 * is available, -1 on failure
490 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
491 const struct wpabuf *in_data,
492 struct wpabuf **out_data)
494 const struct wpabuf *msg;
496 struct wpabuf *appl_data;
498 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
500 return need_more_input ? 1 : -1;
502 /* Full TLS message reassembled - continue handshake processing */
504 /* This should not happen.. */
505 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
506 "tls_out data even though tls_out_len = 0");
507 wpabuf_free(data->tls_out);
508 WPA_ASSERT(data->tls_out == NULL);
511 data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
514 eap_peer_tls_reset_input(data);
517 tls_connection_established(data->ssl_ctx, data->conn) &&
518 !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
519 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
521 *out_data = appl_data;
525 wpabuf_free(appl_data);
532 * eap_tls_process_output - Process outgoing TLS message
533 * @data: Data for TLS processing
534 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
535 * @peap_version: Version number for EAP-PEAP/TTLS
536 * @id: EAP identifier for the response
537 * @ret: Return value to use on success
538 * @out_data: Buffer for returning the allocated output buffer
539 * Returns: ret (0 or 1) on success, -1 on failure
541 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
542 int peap_version, u8 id, int ret,
543 struct wpabuf **out_data)
547 int more_fragments, length_included;
549 if (data->tls_out == NULL)
551 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
552 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
555 (unsigned long) wpabuf_len(data->tls_out));
558 * Limit outgoing message to the configured maximum size. Fragment
561 if (len > data->tls_out_limit) {
563 len = data->tls_out_limit;
564 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
565 "will follow", (unsigned long) len);
569 length_included = data->tls_out_pos == 0 &&
570 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
571 data->include_tls_length);
572 if (!length_included &&
573 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
574 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
576 * Windows Server 2008 NPS really wants to have the TLS Message
577 * length included in phase 0 even for unfragmented frames or
578 * it will get very confused with Compound MAC calculation and
584 *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
585 EAP_CODE_RESPONSE, id);
586 if (*out_data == NULL)
589 flags = wpabuf_put(*out_data, 1);
590 *flags = peap_version;
592 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
593 if (length_included) {
594 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
595 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
598 wpabuf_put_data(*out_data,
599 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
601 data->tls_out_pos += len;
604 eap_peer_tls_reset_output(data);
611 * eap_peer_tls_process_helper - Process TLS handshake message
612 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
613 * @data: Data for TLS processing
614 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
615 * @peap_version: Version number for EAP-PEAP/TTLS
616 * @id: EAP identifier for the response
617 * @in_data: Message received from the server
618 * @out_data: Buffer for returning a pointer to the response message
619 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
620 * is available, or -1 on failure
622 * This function can be used to process TLS handshake messages. It reassembles
623 * the received fragments and uses a TLS library to process the messages. The
624 * response data from the TLS library is fragmented to suitable output messages
625 * that the caller can send out.
627 * out_data is used to return the response message if the return value of this
628 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
629 * alarm message. The caller is responsible for freeing the allocated buffer if
630 * *out_data is not %NULL.
632 * This function is called for each received TLS message during the TLS
633 * handshake after eap_peer_tls_process_init() call and possible processing of
634 * TLS Flags field. Once the handshake has been completed, i.e., when
635 * tls_connection_established() returns 1, EAP method specific decrypting of
636 * the tunneled data is used.
638 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
639 EapType eap_type, int peap_version,
640 u8 id, const struct wpabuf *in_data,
641 struct wpabuf **out_data)
647 if (data->tls_out && wpabuf_len(data->tls_out) > 0 &&
648 wpabuf_len(in_data) > 0) {
649 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
650 "fragments are waiting to be sent out");
654 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
656 * No more data to send out - expect to receive more data from
659 int res = eap_tls_process_input(sm, data, in_data, out_data);
662 * Input processing failed (res = -1) or more data is
669 * The incoming message has been reassembled and processed. The
670 * response was allocated into data->tls_out buffer.
674 if (data->tls_out == NULL) {
676 * No outgoing fragments remaining from the previous message
677 * and no new message generated. This indicates an error in TLS
680 eap_peer_tls_reset_output(data);
684 if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
685 /* TLS processing has failed - return error */
686 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
687 "report error (len=%u)",
688 (unsigned int) wpabuf_len(data->tls_out));
690 /* TODO: clean pin if engine used? */
691 if (wpabuf_len(data->tls_out) == 0) {
692 wpabuf_free(data->tls_out);
693 data->tls_out = NULL;
698 if (wpabuf_len(data->tls_out) == 0) {
700 * TLS negotiation should now be complete since all other cases
701 * needing more data should have been caught above based on
702 * the TLS Message Length field.
704 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
705 wpabuf_free(data->tls_out);
706 data->tls_out = NULL;
710 /* Send the pending message (in fragments, if needed). */
711 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
717 * eap_peer_tls_build_ack - Build a TLS ACK frame
718 * @id: EAP identifier for the response
719 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
720 * @peap_version: Version number for EAP-PEAP/TTLS
721 * Returns: Pointer to the allocated ACK frame or %NULL on failure
723 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
728 resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
731 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
732 (int) eap_type, id, peap_version);
733 wpabuf_put_u8(resp, peap_version); /* Flags */
739 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
740 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
741 * @data: Data for TLS processing
742 * Returns: 0 on success, -1 on failure
744 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
746 eap_peer_tls_reset_input(data);
747 eap_peer_tls_reset_output(data);
748 return tls_connection_shutdown(data->ssl_ctx, data->conn);
753 * eap_peer_tls_status - Get TLS status
754 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
755 * @data: Data for TLS processing
756 * @buf: Buffer for status information
757 * @buflen: Maximum buffer length
758 * @verbose: Whether to include verbose status information
759 * Returns: Number of bytes written to buf.
761 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
762 char *buf, size_t buflen, int verbose)
764 char version[20], name[128];
767 if (tls_get_version(data->ssl_ctx, data->conn, version,
768 sizeof(version)) < 0)
770 if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) < 0)
773 ret = os_snprintf(buf + len, buflen - len,
774 "eap_tls_version=%s\n"
775 "EAP TLS cipher=%s\n"
776 "tls_session_reused=%d\n",
778 tls_connection_resumed(data->ssl_ctx, data->conn));
779 if (os_snprintf_error(buflen - len, ret))
788 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
789 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
790 * @data: Data for TLS processing
791 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
792 * @ret: Return values from EAP request validation and processing
793 * @reqData: EAP request to be processed (eapReqData)
794 * @len: Buffer for returning length of the remaining payload
795 * @flags: Buffer for returning TLS flags
796 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
798 * This function validates the EAP header and processes the optional TLS
799 * Message Length field. If this is the first fragment of a TLS message, the
800 * TLS reassembly code is initialized to receive the indicated number of bytes.
802 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
803 * function as the first step in processing received messages. They will need
804 * to process the flags (apart from Message Length Included) that are returned
805 * through the flags pointer and the message payload that will be returned (and
806 * the length is returned through the len pointer). Return values (ret) are set
807 * for continuation of EAP method processing. The caller is responsible for
808 * setting these to indicate completion (either success or failure) based on
809 * the authentication result.
811 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
812 struct eap_ssl_data *data,
814 struct eap_method_ret *ret,
815 const struct wpabuf *reqData,
816 size_t *len, u8 *flags)
820 unsigned int tls_msg_len;
822 /* Ignore errors before we do anything*/
823 (void) tls_get_errors(sm->ssl_ctx);
825 //// if (tls_get_errors(data->ssl_ctx)) {
826 //// wpa_printf(MSG_INFO, "SSL: TLS errors detected");
827 //// ret->ignore = TRUE;
831 if (eap_type == EAP_UNAUTH_TLS_TYPE)
832 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
833 EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
835 else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
836 pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
837 EAP_VENDOR_WFA_UNAUTH_TLS, reqData,
840 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
847 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
849 if (!sm->workaround) {
854 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
855 "indicates ACK frame");
861 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
862 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
864 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
866 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
871 tls_msg_len = WPA_GET_BE32(pos);
872 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
874 if (data->tls_in_left == 0) {
875 data->tls_in_total = tls_msg_len;
876 data->tls_in_left = tls_msg_len;
877 wpabuf_free(data->tls_in);
883 if (left > tls_msg_len) {
884 wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
885 "bytes) smaller than this fragment (%d "
886 "bytes)", (int) tls_msg_len, (int) left);
893 ret->methodState = METHOD_MAY_CONT;
894 ret->decision = DECISION_FAIL;
895 ret->allowNotifications = TRUE;
903 * eap_peer_tls_reset_input - Reset input buffers
904 * @data: Data for TLS processing
906 * This function frees any allocated memory for input buffers and resets input
909 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
911 data->tls_in_left = data->tls_in_total = 0;
912 wpabuf_free(data->tls_in);
918 * eap_peer_tls_reset_output - Reset output buffers
919 * @data: Data for TLS processing
921 * This function frees any allocated memory for output buffers and resets
924 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
926 data->tls_out_pos = 0;
927 wpabuf_free(data->tls_out);
928 data->tls_out = NULL;
933 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
934 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
935 * @data: Data for TLS processing
936 * @in_data: Message received from the server
937 * @in_decrypted: Buffer for returning a pointer to the decrypted message
938 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
940 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
941 const struct wpabuf *in_data,
942 struct wpabuf **in_decrypted)
944 const struct wpabuf *msg;
947 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
949 return need_more_input ? 1 : -1;
951 *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
952 eap_peer_tls_reset_input(data);
953 if (*in_decrypted == NULL) {
954 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
962 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
963 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
964 * @data: Data for TLS processing
965 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
966 * @peap_version: Version number for EAP-PEAP/TTLS
967 * @id: EAP identifier for the response
968 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
969 * @out_data: Buffer for returning a pointer to the encrypted response message
970 * Returns: 0 on success, -1 on failure
972 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
973 EapType eap_type, int peap_version, u8 id,
974 const struct wpabuf *in_data,
975 struct wpabuf **out_data)
978 eap_peer_tls_reset_output(data);
979 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
980 data->conn, in_data);
981 if (data->tls_out == NULL) {
982 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
984 (unsigned long) wpabuf_len(in_data));
985 eap_peer_tls_reset_output(data);
990 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
996 * eap_peer_select_phase2_methods - Select phase 2 EAP method
997 * @config: Pointer to the network configuration
998 * @prefix: 'phase2' configuration prefix, e.g., "auth="
999 * @types: Buffer for returning allocated list of allowed EAP methods
1000 * @num_types: Buffer for returning number of allocated EAP methods
1001 * Returns: 0 on success, -1 on failure
1003 * This function is used to parse EAP method list and select allowed methods
1004 * for Phase2 authentication.
1006 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
1008 struct eap_method_type **types,
1011 char *start, *pos, *buf;
1012 struct eap_method_type *methods = NULL, *_methods;
1014 size_t num_methods = 0, prefix_len;
1016 if (config == NULL || config->phase2 == NULL)
1019 start = buf = os_strdup(config->phase2);
1023 prefix_len = os_strlen(prefix);
1025 while (start && *start != '\0') {
1027 pos = os_strstr(start, prefix);
1030 if (start != pos && *(pos - 1) != ' ') {
1031 start = pos + prefix_len;
1035 start = pos + prefix_len;
1036 pos = os_strchr(start, ' ');
1039 method = eap_get_phase2_type(start, &vendor);
1040 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
1041 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
1042 "method '%s'", start);
1045 _methods = os_realloc_array(methods, num_methods,
1047 if (_methods == NULL) {
1053 methods[num_methods - 1].vendor = vendor;
1054 methods[num_methods - 1].method = method;
1063 if (methods == NULL)
1064 methods = eap_get_phase2_types(config, &num_methods);
1066 if (methods == NULL) {
1067 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
1070 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1072 num_methods * sizeof(struct eap_method_type));
1075 *num_types = num_methods;
1082 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1083 * @types: Buffer for returning allocated list of allowed EAP methods
1084 * @num_types: Buffer for returning number of allocated EAP methods
1085 * @hdr: EAP-Request header (and the following EAP type octet)
1086 * @resp: Buffer for returning the EAP-Nak message
1087 * Returns: 0 on success, -1 on failure
1089 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1090 struct eap_hdr *hdr, struct wpabuf **resp)
1092 u8 *pos = (u8 *) (hdr + 1);
1095 /* TODO: add support for expanded Nak */
1096 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1097 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1098 (u8 *) types, num_types * sizeof(struct eap_method_type));
1099 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1100 EAP_CODE_RESPONSE, hdr->identifier);
1104 for (i = 0; i < num_types; i++) {
1105 if (types[i].vendor == EAP_VENDOR_IETF &&
1106 types[i].method < 256)
1107 wpabuf_put_u8(*resp, types[i].method);
1110 eap_update_len(*resp);