• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * EAP peer method: EAP-TLS (RFC 2716)
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "crypto/tls.h"
13 #include "eap_i.h"
14 #include "eap_tls_common.h"
15 #include "eap_config.h"
16 
17 
18 static void eap_tls_deinit(struct eap_sm *sm, void *priv);
19 
20 
21 struct eap_tls_data {
22 	struct eap_ssl_data ssl;
23 	u8 *key_data;
24 };
25 
26 
eap_tls_init(struct eap_sm * sm)27 static void * eap_tls_init(struct eap_sm *sm)
28 {
29 	struct eap_tls_data *data;
30 	struct eap_peer_config *config = eap_get_config(sm);
31 	if (config == NULL ||
32 	    ((sm->init_phase2 ? config->private_key2 : config->private_key)
33 	     == NULL &&
34 	     (sm->init_phase2 ? config->engine2 : config->engine) == 0)) {
35 		wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
36 		return NULL;
37 	}
38 
39 	data = os_zalloc(sizeof(*data));
40 	if (data == NULL)
41 		return NULL;
42 
43 	if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
44 		wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
45 		eap_tls_deinit(sm, data);
46 		if (config->engine) {
47 			wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting Smartcard "
48 				   "PIN");
49 			eap_sm_request_pin(sm);
50 			sm->ignore = TRUE;
51 		} else if (config->private_key && !config->private_key_passwd)
52 		{
53 			wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting private "
54 				   "key passphrase");
55 			eap_sm_request_passphrase(sm);
56 			sm->ignore = TRUE;
57 		}
58 		return NULL;
59 	}
60 
61 	return data;
62 }
63 
64 
eap_tls_deinit(struct eap_sm * sm,void * priv)65 static void eap_tls_deinit(struct eap_sm *sm, void *priv)
66 {
67 	struct eap_tls_data *data = priv;
68 	if (data == NULL)
69 		return;
70 	eap_peer_tls_ssl_deinit(sm, &data->ssl);
71 	os_free(data->key_data);
72 	os_free(data);
73 }
74 
75 
eap_tls_failure(struct eap_sm * sm,struct eap_tls_data * data,struct eap_method_ret * ret,int res,struct wpabuf * resp,u8 id)76 static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
77 				       struct eap_tls_data *data,
78 				       struct eap_method_ret *ret, int res,
79 				       struct wpabuf *resp, u8 id)
80 {
81 	wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
82 
83 	ret->methodState = METHOD_DONE;
84 	ret->decision = DECISION_FAIL;
85 
86 	if (res == -1) {
87 		struct eap_peer_config *config = eap_get_config(sm);
88 		if (config) {
89 			/*
90 			 * The TLS handshake failed. So better forget the old
91 			 * PIN. It may be wrong, we cannot be sure but trying
92 			 * the wrong one again might block it on the card--so
93 			 * better ask the user again.
94 			 */
95 			os_free(config->pin);
96 			config->pin = NULL;
97 		}
98 	}
99 
100 	if (resp) {
101 		/*
102 		 * This is likely an alert message, so send it instead of just
103 		 * ACKing the error.
104 		 */
105 		return resp;
106 	}
107 
108 	return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
109 }
110 
111 
eap_tls_success(struct eap_sm * sm,struct eap_tls_data * data,struct eap_method_ret * ret)112 static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
113 			    struct eap_method_ret *ret)
114 {
115 	wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
116 
117 	ret->methodState = METHOD_DONE;
118 	ret->decision = DECISION_UNCOND_SUCC;
119 
120 	os_free(data->key_data);
121 	data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
122 						 "client EAP encryption",
123 						 EAP_TLS_KEY_LEN +
124 						 EAP_EMSK_LEN);
125 	if (data->key_data) {
126 		wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
127 				data->key_data, EAP_TLS_KEY_LEN);
128 		wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
129 				data->key_data + EAP_TLS_KEY_LEN,
130 				EAP_EMSK_LEN);
131 	} else {
132 		wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
133 	}
134 }
135 
136 
eap_tls_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)137 static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
138 				       struct eap_method_ret *ret,
139 				       const struct wpabuf *reqData)
140 {
141 	size_t left;
142 	int res;
143 	struct wpabuf *resp;
144 	u8 flags, id;
145 	const u8 *pos;
146 	struct eap_tls_data *data = priv;
147 
148 	pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TLS, ret,
149 					reqData, &left, &flags);
150 	if (pos == NULL)
151 		return NULL;
152 	id = eap_get_id(reqData);
153 
154 	if (flags & EAP_TLS_FLAGS_START) {
155 		wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
156 		left = 0; /* make sure that this frame is empty, even though it
157 			   * should always be, anyway */
158 	}
159 
160 	resp = NULL;
161 	res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TLS, 0, id,
162 					  pos, left, &resp);
163 
164 	if (res < 0) {
165 		return eap_tls_failure(sm, data, ret, res, resp, id);
166 	}
167 
168 	if (tls_connection_established(sm->ssl_ctx, data->ssl.conn))
169 		eap_tls_success(sm, data, ret);
170 
171 	if (res == 1) {
172 		wpabuf_free(resp);
173 		return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
174 	}
175 
176 	return resp;
177 }
178 
179 
eap_tls_has_reauth_data(struct eap_sm * sm,void * priv)180 static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
181 {
182 	struct eap_tls_data *data = priv;
183 	return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
184 }
185 
186 
eap_tls_deinit_for_reauth(struct eap_sm * sm,void * priv)187 static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
188 {
189 }
190 
191 
eap_tls_init_for_reauth(struct eap_sm * sm,void * priv)192 static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
193 {
194 	struct eap_tls_data *data = priv;
195 	os_free(data->key_data);
196 	data->key_data = NULL;
197 	if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
198 		os_free(data);
199 		return NULL;
200 	}
201 	return priv;
202 }
203 
204 
eap_tls_get_status(struct eap_sm * sm,void * priv,char * buf,size_t buflen,int verbose)205 static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
206 			      size_t buflen, int verbose)
207 {
208 	struct eap_tls_data *data = priv;
209 	return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
210 }
211 
212 
eap_tls_isKeyAvailable(struct eap_sm * sm,void * priv)213 static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
214 {
215 	struct eap_tls_data *data = priv;
216 	return data->key_data != NULL;
217 }
218 
219 
eap_tls_getKey(struct eap_sm * sm,void * priv,size_t * len)220 static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
221 {
222 	struct eap_tls_data *data = priv;
223 	u8 *key;
224 
225 	if (data->key_data == NULL)
226 		return NULL;
227 
228 	key = os_malloc(EAP_TLS_KEY_LEN);
229 	if (key == NULL)
230 		return NULL;
231 
232 	*len = EAP_TLS_KEY_LEN;
233 	os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
234 
235 	return key;
236 }
237 
238 
eap_tls_get_emsk(struct eap_sm * sm,void * priv,size_t * len)239 static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
240 {
241 	struct eap_tls_data *data = priv;
242 	u8 *key;
243 
244 	if (data->key_data == NULL)
245 		return NULL;
246 
247 	key = os_malloc(EAP_EMSK_LEN);
248 	if (key == NULL)
249 		return NULL;
250 
251 	*len = EAP_EMSK_LEN;
252 	os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
253 
254 	return key;
255 }
256 
257 
eap_peer_tls_register(void)258 int eap_peer_tls_register(void)
259 {
260 	struct eap_method *eap;
261 	int ret;
262 
263 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
264 				    EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
265 	if (eap == NULL)
266 		return -1;
267 
268 	eap->init = eap_tls_init;
269 	eap->deinit = eap_tls_deinit;
270 	eap->process = eap_tls_process;
271 	eap->isKeyAvailable = eap_tls_isKeyAvailable;
272 	eap->getKey = eap_tls_getKey;
273 	eap->get_status = eap_tls_get_status;
274 	eap->has_reauth_data = eap_tls_has_reauth_data;
275 	eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
276 	eap->init_for_reauth = eap_tls_init_for_reauth;
277 	eap->get_emsk = eap_tls_get_emsk;
278 
279 	ret = eap_peer_method_register(eap);
280 	if (ret)
281 		eap_peer_method_free(eap);
282 	return ret;
283 }
284