1189251Ssam/*
2189251Ssam * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3252726Srpaulo * Copyright (c) 2004-2009, 2012, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#ifndef EAP_TLS_COMMON_H
10189251Ssam#define EAP_TLS_COMMON_H
11189251Ssam
12189251Ssam/**
13189251Ssam * struct eap_ssl_data - TLS data for EAP methods
14189251Ssam */
15189251Ssamstruct eap_ssl_data {
16189251Ssam	/**
17189251Ssam	 * conn - TLS connection context data from tls_connection_init()
18189251Ssam	 */
19189251Ssam	struct tls_connection *conn;
20189251Ssam
21189251Ssam	/**
22189251Ssam	 * tls_out - TLS message to be sent out in fragments
23189251Ssam	 */
24214734Srpaulo	struct wpabuf *tls_out;
25189251Ssam
26189251Ssam	/**
27189251Ssam	 * tls_out_pos - The current position in the outgoing TLS message
28189251Ssam	 */
29189251Ssam	size_t tls_out_pos;
30189251Ssam
31189251Ssam	/**
32189251Ssam	 * tls_out_limit - Maximum fragment size for outgoing TLS messages
33189251Ssam	 */
34189251Ssam	size_t tls_out_limit;
35189251Ssam
36189251Ssam	/**
37189251Ssam	 * tls_in - Received TLS message buffer for re-assembly
38189251Ssam	 */
39214734Srpaulo	struct wpabuf *tls_in;
40189251Ssam
41189251Ssam	/**
42189251Ssam	 * tls_in_left - Number of remaining bytes in the incoming TLS message
43189251Ssam	 */
44189251Ssam	size_t tls_in_left;
45189251Ssam
46189251Ssam	/**
47189251Ssam	 * tls_in_total - Total number of bytes in the incoming TLS message
48189251Ssam	 */
49189251Ssam	size_t tls_in_total;
50189251Ssam
51189251Ssam	/**
52189251Ssam	 * phase2 - Whether this TLS connection is used in EAP phase 2 (tunnel)
53189251Ssam	 */
54189251Ssam	int phase2;
55189251Ssam
56189251Ssam	/**
57189251Ssam	 * include_tls_length - Whether the TLS length field is included even
58189251Ssam	 * if the TLS data is not fragmented
59189251Ssam	 */
60189251Ssam	int include_tls_length;
61189251Ssam
62189251Ssam	/**
63252726Srpaulo	 * eap - EAP state machine allocated with eap_peer_sm_init()
64189251Ssam	 */
65252726Srpaulo	struct eap_sm *eap;
66189251Ssam
67189251Ssam	/**
68252726Srpaulo	 * ssl_ctx - TLS library context to use for the connection
69189251Ssam	 */
70252726Srpaulo	void *ssl_ctx;
71252726Srpaulo
72252726Srpaulo	/**
73351611Scy	 * eap_type - EAP method used in Phase 1
74351611Scy	 * (EAP_TYPE_TLS/PEAP/TTLS/FAST/TEAP)
75252726Srpaulo	 */
76252726Srpaulo	u8 eap_type;
77346981Scy
78346981Scy	/**
79346981Scy	 * tls_v13 - Whether TLS v1.3 or newer is used
80346981Scy	 */
81346981Scy	int tls_v13;
82189251Ssam};
83189251Ssam
84189251Ssam
85189251Ssam/* EAP TLS Flags */
86189251Ssam#define EAP_TLS_FLAGS_LENGTH_INCLUDED 0x80
87189251Ssam#define EAP_TLS_FLAGS_MORE_FRAGMENTS 0x40
88189251Ssam#define EAP_TLS_FLAGS_START 0x20
89351611Scy#define EAP_TEAP_FLAGS_OUTER_TLV_LEN 0x10
90214734Srpaulo#define EAP_TLS_VERSION_MASK 0x07
91189251Ssam
92189251Ssam /* could be up to 128 bytes, but only the first 64 bytes are used */
93189251Ssam#define EAP_TLS_KEY_LEN 64
94189251Ssam
95252726Srpaulo/* dummy type used as a flag for UNAUTH-TLS */
96252726Srpaulo#define EAP_UNAUTH_TLS_TYPE 255
97281806Srpaulo#define EAP_WFA_UNAUTH_TLS_TYPE 254
98189251Ssam
99252726Srpaulo
100189251Ssamint eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
101252726Srpaulo			  struct eap_peer_config *config, u8 eap_type);
102189251Ssamvoid eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data);
103189251Ssamu8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
104346981Scy			     const char *label, const u8 *context,
105346981Scy			     size_t context_len, size_t len);
106281806Srpaulou8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
107281806Srpaulo				    struct eap_ssl_data *data, u8 eap_type,
108281806Srpaulo				    size_t *len);
109189251Ssamint eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
110189251Ssam				EapType eap_type, int peap_version,
111289549Srpaulo				u8 id, const struct wpabuf *in_data,
112189251Ssam				struct wpabuf **out_data);
113189251Ssamstruct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
114189251Ssam				       int peap_version);
115189251Ssamint eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data);
116189251Ssamint eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
117189251Ssam			char *buf, size_t buflen, int verbose);
118189251Ssamconst u8 * eap_peer_tls_process_init(struct eap_sm *sm,
119189251Ssam				     struct eap_ssl_data *data,
120189251Ssam				     EapType eap_type,
121189251Ssam				     struct eap_method_ret *ret,
122189251Ssam				     const struct wpabuf *reqData,
123189251Ssam				     size_t *len, u8 *flags);
124189251Ssamvoid eap_peer_tls_reset_input(struct eap_ssl_data *data);
125189251Ssamvoid eap_peer_tls_reset_output(struct eap_ssl_data *data);
126189251Ssamint eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
127189251Ssam			 const struct wpabuf *in_data,
128189251Ssam			 struct wpabuf **in_decrypted);
129189251Ssamint eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
130189251Ssam			 EapType eap_type, int peap_version, u8 id,
131189251Ssam			 const struct wpabuf *in_data,
132189251Ssam			 struct wpabuf **out_data);
133189251Ssamint eap_peer_select_phase2_methods(struct eap_peer_config *config,
134189251Ssam				   const char *prefix,
135189251Ssam				   struct eap_method_type **types,
136189251Ssam				   size_t *num_types);
137189251Ssamint eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
138189251Ssam			    struct eap_hdr *hdr, struct wpabuf **resp);
139189251Ssam
140189251Ssam#endif /* EAP_TLS_COMMON_H */
141