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 return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
31 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
32 const u8 **data, size_t *data_len)
34 const struct wpa_config_blob *blob;
36 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
39 blob = eap_get_config_blob(sm, *name + 7);
41 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
42 "found", __func__, *name + 7);
48 *data_len = blob->len;
54 static void eap_tls_params_flags(struct tls_connection_params *params,
59 if (os_strstr(txt, "tls_allow_md5=1"))
60 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
61 if (os_strstr(txt, "tls_disable_time_checks=1"))
62 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
63 if (os_strstr(txt, "tls_disable_session_ticket=1"))
64 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
65 if (os_strstr(txt, "tls_disable_session_ticket=0"))
66 params->flags &= ~TLS_CONN_DISABLE_SESSION_TICKET;
70 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
71 struct eap_peer_config *config)
73 params->ca_cert = (char *) config->ca_cert;
74 params->ca_path = (char *) config->ca_path;
75 params->client_cert = (char *) config->client_cert;
76 params->private_key = (char *) config->private_key;
77 params->private_key_passwd = (char *) config->private_key_passwd;
78 params->dh_file = (char *) config->dh_file;
79 params->subject_match = (char *) config->subject_match;
80 params->altsubject_match = (char *) config->altsubject_match;
81 params->suffix_match = config->domain_suffix_match;
82 params->engine = config->engine;
83 params->engine_id = config->engine_id;
84 params->pin = config->pin;
85 params->key_id = config->key_id;
86 params->cert_id = config->cert_id;
87 params->ca_cert_id = config->ca_cert_id;
88 eap_tls_params_flags(params, config->phase1);
92 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
93 struct eap_peer_config *config)
95 params->ca_cert = (char *) config->ca_cert2;
96 params->ca_path = (char *) config->ca_path2;
97 params->client_cert = (char *) config->client_cert2;
98 params->private_key = (char *) config->private_key2;
99 params->private_key_passwd = (char *) config->private_key2_passwd;
100 params->dh_file = (char *) config->dh_file2;
101 params->subject_match = (char *) config->subject_match2;
102 params->altsubject_match = (char *) config->altsubject_match2;
103 params->suffix_match = config->domain_suffix_match2;
104 params->engine = config->engine2;
105 params->engine_id = config->engine2_id;
106 params->pin = config->pin2;
107 params->key_id = config->key2_id;
108 params->cert_id = config->cert2_id;
109 params->ca_cert_id = config->ca_cert2_id;
110 eap_tls_params_flags(params, config->phase2);
114 static int eap_tls_params_from_conf(struct eap_sm *sm,
115 struct eap_ssl_data *data,
116 struct tls_connection_params *params,
117 struct eap_peer_config *config, int phase2)
119 os_memset(params, 0, sizeof(*params));
120 if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
122 * Some deployed authentication servers seem to be unable to
123 * handle the TLS Session Ticket extension (they are supposed
124 * to ignore unrecognized TLS extensions, but end up rejecting
125 * the ClientHello instead). As a workaround, disable use of
126 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
127 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
128 * supports EAP-FAST does not need this workaround).
130 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
133 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
134 eap_tls_params_from_conf2(params, config);
136 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
137 eap_tls_params_from_conf1(params, config);
141 * Use blob data, if available. Otherwise, leave reference to external
144 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob,
145 ¶ms->ca_cert_blob_len) ||
146 eap_tls_check_blob(sm, ¶ms->client_cert,
147 ¶ms->client_cert_blob,
148 ¶ms->client_cert_blob_len) ||
149 eap_tls_check_blob(sm, ¶ms->private_key,
150 ¶ms->private_key_blob,
151 ¶ms->private_key_blob_len) ||
152 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob,
153 ¶ms->dh_blob_len)) {
154 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
162 static int eap_tls_init_connection(struct eap_sm *sm,
163 struct eap_ssl_data *data,
164 struct eap_peer_config *config,
165 struct tls_connection_params *params)
170 params->flags |= TLS_CONN_REQUEST_OCSP;
171 if (config->ocsp == 2)
172 params->flags |= TLS_CONN_REQUIRE_OCSP;
173 data->conn = tls_connection_init(data->ssl_ctx);
174 if (data->conn == NULL) {
175 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
180 res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
181 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
183 * At this point with the pkcs11 engine the PIN might be wrong.
184 * We reset the PIN in the configuration to be sure to not use
185 * it again and the calling function must request a new one.
187 os_free(config->pin);
189 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
190 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
192 * We do not know exactly but maybe the PIN was wrong,
193 * so ask for a new one.
195 os_free(config->pin);
197 eap_sm_request_pin(sm);
199 tls_connection_deinit(data->ssl_ctx, data->conn);
203 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
205 tls_connection_deinit(data->ssl_ctx, data->conn);
215 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
216 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
217 * @data: Data for TLS processing
218 * @config: Pointer to the network configuration
219 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
220 * Returns: 0 on success, -1 on failure
222 * This function is used to initialize shared TLS functionality for EAP-TLS,
223 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
225 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
226 struct eap_peer_config *config, u8 eap_type)
228 struct tls_connection_params params;
234 data->eap_type = eap_type;
235 data->phase2 = sm->init_phase2;
236 data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
238 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) <
242 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0)
245 data->tls_out_limit = config->fragment_size;
247 /* Limit the fragment size in the inner TLS authentication
248 * since the outer authentication with EAP-PEAP does not yet
249 * support fragmentation */
250 if (data->tls_out_limit > 100)
251 data->tls_out_limit -= 100;
254 if (config->phase1 &&
255 os_strstr(config->phase1, "include_tls_length=1")) {
256 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
257 "unfragmented packets");
258 data->include_tls_length = 1;
266 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
267 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
268 * @data: Data for TLS processing
270 * This function deinitializes shared TLS functionality that was initialized
271 * with eap_peer_tls_ssl_init().
273 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
275 tls_connection_deinit(data->ssl_ctx, data->conn);
276 eap_peer_tls_reset_input(data);
277 eap_peer_tls_reset_output(data);
282 * eap_peer_tls_derive_key - Derive a key based on TLS session data
283 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
284 * @data: Data for TLS processing
285 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
286 * @len: Length of the key material to generate (usually 64 for MSK)
287 * Returns: Pointer to allocated key on success or %NULL on failure
289 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
290 * session data (client/server random and master key). Each key type may use a
291 * different label to bind the key usage into the generated material.
293 * The caller is responsible for freeing the returned buffer.
295 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
296 const char *label, size_t len)
299 struct tls_keys keys;
300 #endif /* CONFIG_FIPS */
301 u8 *rnd = NULL, *out;
303 out = os_malloc(len);
307 /* First, try to use TLS library function for PRF, if available. */
308 if (tls_connection_prf(data->ssl_ctx, data->conn, label, 0, out, len)
314 * TLS library did not support key generation, so get the needed TLS
315 * session parameters and use an internal implementation of TLS PRF to
318 if (tls_connection_get_keys(data->ssl_ctx, data->conn, &keys))
321 if (keys.client_random == NULL || keys.server_random == NULL ||
322 keys.master_key == NULL)
325 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
328 os_memcpy(rnd, keys.client_random, keys.client_random_len);
329 os_memcpy(rnd + keys.client_random_len, keys.server_random,
330 keys.server_random_len);
332 if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
333 label, rnd, keys.client_random_len +
334 keys.server_random_len, out, len))
341 #endif /* CONFIG_FIPS */
349 * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
350 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
351 * @data: Data for TLS processing
352 * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
353 * @len: Pointer to length of the session ID generated
354 * Returns: Pointer to allocated Session-Id on success or %NULL on failure
356 * This function derive the Session-Id based on the TLS session data
357 * (client/server random and method type).
359 * The caller is responsible for freeing the returned buffer.
361 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
362 struct eap_ssl_data *data, u8 eap_type,
365 struct tls_keys keys;
369 * TLS library did not support session ID generation,
370 * so get the needed TLS session parameters
372 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
375 if (keys.client_random == NULL || keys.server_random == NULL ||
376 keys.master_key == NULL)
379 *len = 1 + keys.client_random_len + keys.server_random_len;
380 out = os_malloc(*len);
384 /* Session-Id = EAP type || client.random || server.random */
386 os_memcpy(out + 1, keys.client_random, keys.client_random_len);
387 os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
388 keys.server_random_len);
395 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
396 * @data: Data for TLS processing
397 * @in_data: Next incoming TLS segment
398 * Returns: 0 on success, 1 if more data is needed for the full message, or
401 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
402 const struct wpabuf *in_data)
404 size_t tls_in_len, in_len;
406 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
407 in_len = in_data ? wpabuf_len(in_data) : 0;
409 if (tls_in_len + in_len == 0) {
410 /* No message data received?! */
411 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
412 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
413 (unsigned long) data->tls_in_left,
414 (unsigned long) tls_in_len,
415 (unsigned long) in_len);
416 eap_peer_tls_reset_input(data);
420 if (tls_in_len + in_len > 65536) {
422 * Limit length to avoid rogue servers from causing large
423 * memory allocations.
425 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
427 eap_peer_tls_reset_input(data);
431 if (in_len > data->tls_in_left) {
432 /* Sender is doing something odd - reject message */
433 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
435 eap_peer_tls_reset_input(data);
439 if (wpabuf_resize(&data->tls_in, in_len) < 0) {
440 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
442 eap_peer_tls_reset_input(data);
446 wpabuf_put_buf(data->tls_in, in_data);
447 data->tls_in_left -= in_len;
449 if (data->tls_in_left > 0) {
450 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
451 "data", (unsigned long) data->tls_in_left);
460 * eap_peer_tls_data_reassemble - Reassemble TLS data
461 * @data: Data for TLS processing
462 * @in_data: Next incoming TLS segment
463 * @need_more_input: Variable for returning whether more input data is needed
464 * to reassemble this TLS packet
465 * Returns: Pointer to output data, %NULL on error or when more data is needed
466 * for the full message (in which case, *need_more_input is also set to 1).
468 * This function reassembles TLS fragments. Caller must not free the returned
469 * data buffer since an internal pointer to it is maintained.
471 static const struct wpabuf * eap_peer_tls_data_reassemble(
472 struct eap_ssl_data *data, const struct wpabuf *in_data,
473 int *need_more_input)
475 *need_more_input = 0;
477 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
478 /* Message has fragments */
479 int res = eap_peer_tls_reassemble_fragment(data, in_data);
482 *need_more_input = 1;
486 /* Message is now fully reassembled. */
488 /* No fragments in this message, so just make a copy of it. */
489 data->tls_in_left = 0;
490 data->tls_in = wpabuf_dup(in_data);
491 if (data->tls_in == NULL)
500 * eap_tls_process_input - Process incoming TLS message
501 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
502 * @data: Data for TLS processing
503 * @in_data: Message received from the server
504 * @in_len: Length of in_data
505 * @out_data: Buffer for returning a pointer to application data (if available)
506 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
507 * is available, -1 on failure
509 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
510 const u8 *in_data, size_t in_len,
511 struct wpabuf **out_data)
513 const struct wpabuf *msg;
515 struct wpabuf *appl_data;
518 wpabuf_set(&buf, in_data, in_len);
519 msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
521 return need_more_input ? 1 : -1;
523 /* Full TLS message reassembled - continue handshake processing */
525 /* This should not happen.. */
526 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
527 "tls_out data even though tls_out_len = 0");
528 wpabuf_free(data->tls_out);
529 WPA_ASSERT(data->tls_out == NULL);
532 data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
535 eap_peer_tls_reset_input(data);
538 tls_connection_established(data->ssl_ctx, data->conn) &&
539 !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
540 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
542 *out_data = appl_data;
546 wpabuf_free(appl_data);
553 * eap_tls_process_output - Process outgoing TLS message
554 * @data: Data for TLS processing
555 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
556 * @peap_version: Version number for EAP-PEAP/TTLS
557 * @id: EAP identifier for the response
558 * @ret: Return value to use on success
559 * @out_data: Buffer for returning the allocated output buffer
560 * Returns: ret (0 or 1) on success, -1 on failure
562 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
563 int peap_version, u8 id, int ret,
564 struct wpabuf **out_data)
568 int more_fragments, length_included;
570 if (data->tls_out == NULL)
572 len = wpabuf_len(data->tls_out) - data->tls_out_pos;
573 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
576 (unsigned long) wpabuf_len(data->tls_out));
579 * Limit outgoing message to the configured maximum size. Fragment
582 if (len > data->tls_out_limit) {
584 len = data->tls_out_limit;
585 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
586 "will follow", (unsigned long) len);
590 length_included = data->tls_out_pos == 0 &&
591 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
592 data->include_tls_length);
593 if (!length_included &&
594 eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
595 !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
597 * Windows Server 2008 NPS really wants to have the TLS Message
598 * length included in phase 0 even for unfragmented frames or
599 * it will get very confused with Compound MAC calculation and
605 *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
606 EAP_CODE_RESPONSE, id);
607 if (*out_data == NULL)
610 flags = wpabuf_put(*out_data, 1);
611 *flags = peap_version;
613 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
614 if (length_included) {
615 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
616 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
619 wpabuf_put_data(*out_data,
620 wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
622 data->tls_out_pos += len;
625 eap_peer_tls_reset_output(data);
632 * eap_peer_tls_process_helper - Process TLS handshake message
633 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
634 * @data: Data for TLS processing
635 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
636 * @peap_version: Version number for EAP-PEAP/TTLS
637 * @id: EAP identifier for the response
638 * @in_data: Message received from the server
639 * @in_len: Length of in_data
640 * @out_data: Buffer for returning a pointer to the response message
641 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
642 * is available, or -1 on failure
644 * This function can be used to process TLS handshake messages. It reassembles
645 * the received fragments and uses a TLS library to process the messages. The
646 * response data from the TLS library is fragmented to suitable output messages
647 * that the caller can send out.
649 * out_data is used to return the response message if the return value of this
650 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
651 * alarm message. The caller is responsible for freeing the allocated buffer if
652 * *out_data is not %NULL.
654 * This function is called for each received TLS message during the TLS
655 * handshake after eap_peer_tls_process_init() call and possible processing of
656 * TLS Flags field. Once the handshake has been completed, i.e., when
657 * tls_connection_established() returns 1, EAP method specific decrypting of
658 * the tunneled data is used.
660 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
661 EapType eap_type, int peap_version,
662 u8 id, const u8 *in_data, size_t in_len,
663 struct wpabuf **out_data)
669 if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
670 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
671 "fragments are waiting to be sent out");
675 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
677 * No more data to send out - expect to receive more data from
680 int res = eap_tls_process_input(sm, data, in_data, in_len,
684 * Input processing failed (res = -1) or more data is
691 * The incoming message has been reassembled and processed. The
692 * response was allocated into data->tls_out buffer.
696 if (data->tls_out == NULL) {
698 * No outgoing fragments remaining from the previous message
699 * and no new message generated. This indicates an error in TLS
702 eap_peer_tls_reset_output(data);
706 if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
707 /* TLS processing has failed - return error */
708 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
711 /* TODO: clean pin if engine used? */
714 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
716 * TLS negotiation should now be complete since all other cases
717 * needing more data should have been caught above based on
718 * the TLS Message Length field.
720 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
721 wpabuf_free(data->tls_out);
722 data->tls_out = NULL;
726 /* Send the pending message (in fragments, if needed). */
727 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
733 * eap_peer_tls_build_ack - Build a TLS ACK frame
734 * @id: EAP identifier for the response
735 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
736 * @peap_version: Version number for EAP-PEAP/TTLS
737 * Returns: Pointer to the allocated ACK frame or %NULL on failure
739 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
744 resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
747 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
748 (int) eap_type, id, peap_version);
749 wpabuf_put_u8(resp, peap_version); /* Flags */
755 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
756 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
757 * @data: Data for TLS processing
758 * Returns: 0 on success, -1 on failure
760 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
762 eap_peer_tls_reset_input(data);
763 eap_peer_tls_reset_output(data);
764 return tls_connection_shutdown(data->ssl_ctx, data->conn);
769 * eap_peer_tls_status - Get TLS status
770 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
771 * @data: Data for TLS processing
772 * @buf: Buffer for status information
773 * @buflen: Maximum buffer length
774 * @verbose: Whether to include verbose status information
775 * Returns: Number of bytes written to buf.
777 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
778 char *buf, size_t buflen, int verbose)
783 if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) == 0)
785 ret = os_snprintf(buf + len, buflen - len,
786 "EAP TLS cipher=%s\n", name);
787 if (ret < 0 || (size_t) ret >= buflen - len)
797 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
798 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
799 * @data: Data for TLS processing
800 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
801 * @ret: Return values from EAP request validation and processing
802 * @reqData: EAP request to be processed (eapReqData)
803 * @len: Buffer for returning length of the remaining payload
804 * @flags: Buffer for returning TLS flags
805 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
807 * This function validates the EAP header and processes the optional TLS
808 * Message Length field. If this is the first fragment of a TLS message, the
809 * TLS reassembly code is initialized to receive the indicated number of bytes.
811 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
812 * function as the first step in processing received messages. They will need
813 * to process the flags (apart from Message Length Included) that are returned
814 * through the flags pointer and the message payload that will be returned (and
815 * the length is returned through the len pointer). Return values (ret) are set
816 * for continuation of EAP method processing. The caller is responsible for
817 * setting these to indicate completion (either success or failure) based on
818 * the authentication result.
820 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
821 struct eap_ssl_data *data,
823 struct eap_method_ret *ret,
824 const struct wpabuf *reqData,
825 size_t *len, u8 *flags)
829 unsigned int tls_msg_len;
831 if (tls_get_errors(data->ssl_ctx)) {
832 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
837 if (eap_type == EAP_UNAUTH_TLS_TYPE)
838 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
839 EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
842 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
849 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
851 if (!sm->workaround) {
856 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
857 "indicates ACK frame");
863 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
864 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
866 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
868 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
873 tls_msg_len = WPA_GET_BE32(pos);
874 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
876 if (data->tls_in_left == 0) {
877 data->tls_in_total = tls_msg_len;
878 data->tls_in_left = tls_msg_len;
879 wpabuf_free(data->tls_in);
885 if (left > tls_msg_len) {
886 wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
887 "bytes) smaller than this fragment (%d "
888 "bytes)", (int) tls_msg_len, (int) left);
895 ret->methodState = METHOD_MAY_CONT;
896 ret->decision = DECISION_FAIL;
897 ret->allowNotifications = TRUE;
905 * eap_peer_tls_reset_input - Reset input buffers
906 * @data: Data for TLS processing
908 * This function frees any allocated memory for input buffers and resets input
911 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
913 data->tls_in_left = data->tls_in_total = 0;
914 wpabuf_free(data->tls_in);
920 * eap_peer_tls_reset_output - Reset output buffers
921 * @data: Data for TLS processing
923 * This function frees any allocated memory for output buffers and resets
926 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
928 data->tls_out_pos = 0;
929 wpabuf_free(data->tls_out);
930 data->tls_out = NULL;
935 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
936 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
937 * @data: Data for TLS processing
938 * @in_data: Message received from the server
939 * @in_decrypted: Buffer for returning a pointer to the decrypted message
940 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
942 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
943 const struct wpabuf *in_data,
944 struct wpabuf **in_decrypted)
946 const struct wpabuf *msg;
949 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
951 return need_more_input ? 1 : -1;
953 *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
954 eap_peer_tls_reset_input(data);
955 if (*in_decrypted == NULL) {
956 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
964 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
965 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
966 * @data: Data for TLS processing
967 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
968 * @peap_version: Version number for EAP-PEAP/TTLS
969 * @id: EAP identifier for the response
970 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
971 * @out_data: Buffer for returning a pointer to the encrypted response message
972 * Returns: 0 on success, -1 on failure
974 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
975 EapType eap_type, int peap_version, u8 id,
976 const struct wpabuf *in_data,
977 struct wpabuf **out_data)
980 eap_peer_tls_reset_output(data);
981 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
982 data->conn, in_data);
983 if (data->tls_out == NULL) {
984 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
986 (unsigned long) wpabuf_len(in_data));
987 eap_peer_tls_reset_output(data);
992 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
998 * eap_peer_select_phase2_methods - Select phase 2 EAP method
999 * @config: Pointer to the network configuration
1000 * @prefix: 'phase2' configuration prefix, e.g., "auth="
1001 * @types: Buffer for returning allocated list of allowed EAP methods
1002 * @num_types: Buffer for returning number of allocated EAP methods
1003 * Returns: 0 on success, -1 on failure
1005 * This function is used to parse EAP method list and select allowed methods
1006 * for Phase2 authentication.
1008 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
1010 struct eap_method_type **types,
1013 char *start, *pos, *buf;
1014 struct eap_method_type *methods = NULL, *_methods;
1016 size_t num_methods = 0, prefix_len;
1018 if (config == NULL || config->phase2 == NULL)
1021 start = buf = os_strdup(config->phase2);
1025 prefix_len = os_strlen(prefix);
1027 while (start && *start != '\0') {
1029 pos = os_strstr(start, prefix);
1032 if (start != pos && *(pos - 1) != ' ') {
1033 start = pos + prefix_len;
1037 start = pos + prefix_len;
1038 pos = os_strchr(start, ' ');
1041 method = eap_get_phase2_type(start, &vendor);
1042 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
1043 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
1044 "method '%s'", start);
1047 _methods = os_realloc_array(methods, num_methods,
1049 if (_methods == NULL) {
1055 methods[num_methods - 1].vendor = vendor;
1056 methods[num_methods - 1].method = method;
1065 if (methods == NULL)
1066 methods = eap_get_phase2_types(config, &num_methods);
1068 if (methods == NULL) {
1069 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
1072 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1074 num_methods * sizeof(struct eap_method_type));
1077 *num_types = num_methods;
1084 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1085 * @types: Buffer for returning allocated list of allowed EAP methods
1086 * @num_types: Buffer for returning number of allocated EAP methods
1087 * @hdr: EAP-Request header (and the following EAP type octet)
1088 * @resp: Buffer for returning the EAP-Nak message
1089 * Returns: 0 on success, -1 on failure
1091 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1092 struct eap_hdr *hdr, struct wpabuf **resp)
1094 u8 *pos = (u8 *) (hdr + 1);
1097 /* TODO: add support for expanded Nak */
1098 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1099 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1100 (u8 *) types, num_types * sizeof(struct eap_method_type));
1101 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1102 EAP_CODE_RESPONSE, hdr->identifier);
1106 for (i = 0; i < num_types; i++) {
1107 if (types[i].vendor == EAP_VENDOR_IETF &&
1108 types[i].method < 256)
1109 wpabuf_put_u8(*resp, types[i].method);
1112 eap_update_len(*resp);