1 /*
2  * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3  * Copyright (c) 2004-2019, 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/sha1.h"
13 #include "crypto/tls.h"
14 #include "eap_i.h"
15 #include "eap_tls_common.h"
16 #include "eap_config.h"
17 
18 
eap_tls_msg_alloc(enum eap_type type,size_t payload_len,u8 code,u8 identifier)19 static struct wpabuf * eap_tls_msg_alloc(enum eap_type type, size_t payload_len,
20 					 u8 code, u8 identifier)
21 {
22 	if (type == EAP_UNAUTH_TLS_TYPE)
23 		return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
24 				     EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
25 				     code, identifier);
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,
29 				     code, identifier);
30 	return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
31 			     identifier);
32 }
33 
34 
eap_tls_check_blob(struct eap_sm * sm,const char ** name,const u8 ** data,size_t * data_len)35 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
36 			      const u8 **data, size_t *data_len)
37 {
38 	const struct wpa_config_blob *blob;
39 
40 	if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
41 		return 0;
42 
43 	blob = eap_get_config_blob(sm, *name + 7);
44 	if (blob == NULL) {
45 		wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
46 			   "found", __func__, *name + 7);
47 		return -1;
48 	}
49 
50 	*name = NULL;
51 	*data = blob->data;
52 	*data_len = blob->len;
53 
54 	return 0;
55 }
56 
57 
eap_tls_params_flags(struct tls_connection_params * params,const char * txt)58 static void eap_tls_params_flags(struct tls_connection_params *params,
59 				 const char *txt)
60 {
61 	if (txt == NULL)
62 		return;
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;
76 	}
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;
82 	}
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;
88 	}
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;
105 }
106 
107 
eap_tls_cert_params_from_conf(struct tls_connection_params * params,struct eap_peer_cert_config * config)108 static void eap_tls_cert_params_from_conf(struct tls_connection_params *params,
109 					  struct eap_peer_cert_config *config)
110 {
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 	if (config->ocsp)
129 		params->flags |= TLS_CONN_REQUEST_OCSP;
130 	if (config->ocsp >= 2)
131 		params->flags |= TLS_CONN_REQUIRE_OCSP;
132 	if (config->ocsp == 3)
133 		params->flags |= TLS_CONN_REQUIRE_OCSP_ALL;
134 }
135 
136 
eap_tls_params_from_conf1(struct tls_connection_params * params,struct eap_peer_config * config)137 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
138 				      struct eap_peer_config *config)
139 {
140 	eap_tls_cert_params_from_conf(params, &config->cert);
141 	eap_tls_params_flags(params, config->phase1);
142 }
143 
144 
eap_tls_params_from_conf2(struct tls_connection_params * params,struct eap_peer_config * config)145 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
146 				      struct eap_peer_config *config)
147 {
148 	eap_tls_cert_params_from_conf(params, &config->phase2_cert);
149 	eap_tls_params_flags(params, config->phase2);
150 }
151 
152 
eap_tls_params_from_conf2m(struct tls_connection_params * params,struct eap_peer_config * config)153 static void eap_tls_params_from_conf2m(struct tls_connection_params *params,
154 				       struct eap_peer_config *config)
155 {
156 	eap_tls_cert_params_from_conf(params, &config->machine_cert);
157 	eap_tls_params_flags(params, config->machine_phase2);
158 }
159 
160 
eap_tls_params_from_conf(struct eap_sm * sm,struct eap_ssl_data * data,struct tls_connection_params * params,struct eap_peer_config * config,int phase2)161 static int eap_tls_params_from_conf(struct eap_sm *sm,
162 				    struct eap_ssl_data *data,
163 				    struct tls_connection_params *params,
164 				    struct eap_peer_config *config, int phase2)
165 {
166 	os_memset(params, 0, sizeof(*params));
167 	if (sm->workaround && data->eap_type != EAP_TYPE_FAST &&
168 	    data->eap_type != EAP_TYPE_TEAP) {
169 		/*
170 		 * Some deployed authentication servers seem to be unable to
171 		 * handle the TLS Session Ticket extension (they are supposed
172 		 * to ignore unrecognized TLS extensions, but end up rejecting
173 		 * the ClientHello instead). As a workaround, disable use of
174 		 * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
175 		 * EAP-TTLS (EAP-FAST uses session ticket, so any server that
176 		 * supports EAP-FAST does not need this workaround).
177 		 */
178 		params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
179 	}
180 	if (data->eap_type == EAP_TYPE_TEAP) {
181 		/* RFC 7170 requires TLS v1.2 or newer to be used with TEAP */
182 		params->flags |= TLS_CONN_DISABLE_TLSv1_0 |
183 			TLS_CONN_DISABLE_TLSv1_1;
184 		if (config->teap_anon_dh)
185 			params->flags |= TLS_CONN_TEAP_ANON_DH;
186 	}
187 	if (data->eap_type == EAP_TYPE_FAST ||
188 	    data->eap_type == EAP_TYPE_TEAP ||
189 	    data->eap_type == EAP_TYPE_TTLS ||
190 	    data->eap_type == EAP_TYPE_PEAP) {
191 		/* The current EAP peer implementation is not yet ready for the
192 		 * TLS v1.3 changes, so disable this by default for now. */
193 		params->flags |= TLS_CONN_DISABLE_TLSv1_3;
194 	}
195 	if (data->eap_type == EAP_TYPE_TLS ||
196 	    data->eap_type == EAP_UNAUTH_TLS_TYPE ||
197 	    data->eap_type == EAP_WFA_UNAUTH_TLS_TYPE) {
198 		/* While the current EAP-TLS implementation is more or less
199 		 * complete for TLS v1.3, there has been no interoperability
200 		 * testing with other implementations, so disable for by default
201 		 * for now until there has been chance to confirm that no
202 		 * significant interoperability issues show up with TLS version
203 		 * update.
204 		 */
205 		params->flags |= TLS_CONN_DISABLE_TLSv1_3;
206 	}
207 	if (phase2 && sm->use_machine_cred) {
208 		wpa_printf(MSG_DEBUG, "TLS: using machine config options");
209 		eap_tls_params_from_conf2m(params, config);
210 	} else if (phase2) {
211 		wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
212 		eap_tls_params_from_conf2(params, config);
213 	} else {
214 		wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
215 		eap_tls_params_from_conf1(params, config);
216 		if (data->eap_type == EAP_TYPE_FAST)
217 			params->flags |= TLS_CONN_EAP_FAST;
218 	}
219 
220 	/*
221 	 * Use blob data, if available. Otherwise, leave reference to external
222 	 * file as-is.
223 	 */
224 	if (eap_tls_check_blob(sm, &params->ca_cert, &params->ca_cert_blob,
225 			       &params->ca_cert_blob_len) ||
226 	    eap_tls_check_blob(sm, &params->client_cert,
227 			       &params->client_cert_blob,
228 			       &params->client_cert_blob_len) ||
229 	    eap_tls_check_blob(sm, &params->private_key,
230 			       &params->private_key_blob,
231 			       &params->private_key_blob_len) ||
232 	    eap_tls_check_blob(sm, &params->dh_file, &params->dh_blob,
233 			       &params->dh_blob_len)) {
234 		wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
235 		return -1;
236 	}
237 
238 	params->openssl_ciphers = config->openssl_ciphers;
239 
240 	sm->ext_cert_check = !!(params->flags & TLS_CONN_EXT_CERT_CHECK);
241 
242 	return 0;
243 }
244 
245 
eap_tls_init_connection(struct eap_sm * sm,struct eap_ssl_data * data,struct eap_peer_config * config,struct tls_connection_params * params)246 static int eap_tls_init_connection(struct eap_sm *sm,
247 				   struct eap_ssl_data *data,
248 				   struct eap_peer_config *config,
249 				   struct tls_connection_params *params)
250 {
251 	int res;
252 
253 	data->conn = tls_connection_init(data->ssl_ctx);
254 	if (data->conn == NULL) {
255 		wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
256 			   "connection");
257 		return -1;
258 	}
259 
260 	res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
261 	if (res == TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN) {
262 		/*
263 		 * At this point with the pkcs11 engine the PIN is wrong. We
264 		 * reset the PIN in the configuration to be sure to not use it
265 		 * again and the calling function must request a new one.
266 		 */
267 		wpa_printf(MSG_INFO,
268 			   "TLS: Bad PIN provided, requesting a new one");
269 		os_free(config->cert.pin);
270 		config->cert.pin = NULL;
271 		eap_sm_request_pin(sm);
272 		sm->ignore = true;
273 	} else if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
274 		wpa_printf(MSG_INFO, "TLS: Failed to initialize engine");
275 	} else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
276 		wpa_printf(MSG_INFO, "TLS: Failed to load private key");
277 		sm->ignore = true;
278 	}
279 	if (res) {
280 		wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
281 			   "parameters");
282 		tls_connection_deinit(data->ssl_ctx, data->conn);
283 		data->conn = NULL;
284 		return -1;
285 	}
286 
287 	return 0;
288 }
289 
290 
291 /**
292  * eap_peer_tls_ssl_init - Initialize shared TLS functionality
293  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
294  * @data: Data for TLS processing
295  * @config: Pointer to the network configuration
296  * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
297  * Returns: 0 on success, -1 on failure
298  *
299  * This function is used to initialize shared TLS functionality for EAP-TLS,
300  * EAP-PEAP, EAP-TTLS, and EAP-FAST.
301  */
eap_peer_tls_ssl_init(struct eap_sm * sm,struct eap_ssl_data * data,struct eap_peer_config * config,u8 eap_type)302 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
303 			  struct eap_peer_config *config, u8 eap_type)
304 {
305 	struct tls_connection_params params;
306 
307 	if (config == NULL)
308 		return -1;
309 
310 	data->eap = sm;
311 	data->eap_type = eap_type;
312 	data->phase2 = sm->init_phase2;
313 	data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
314 		sm->ssl_ctx;
315 	if (eap_tls_params_from_conf(sm, data, &params, config, data->phase2) <
316 	    0)
317 		return -1;
318 
319 	if (eap_tls_init_connection(sm, data, config, &params) < 0)
320 		return -1;
321 
322 	data->tls_out_limit = config->fragment_size;
323 	if (data->phase2) {
324 		/* Limit the fragment size in the inner TLS authentication
325 		 * since the outer authentication with EAP-PEAP does not yet
326 		 * support fragmentation */
327 		if (data->tls_out_limit > 100)
328 			data->tls_out_limit -= 100;
329 	}
330 
331 	if (config->phase1 &&
332 	    os_strstr(config->phase1, "include_tls_length=1")) {
333 		wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
334 			   "unfragmented packets");
335 		data->include_tls_length = 1;
336 	}
337 
338 	return 0;
339 }
340 
341 
342 /**
343  * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
344  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
345  * @data: Data for TLS processing
346  *
347  * This function deinitializes shared TLS functionality that was initialized
348  * with eap_peer_tls_ssl_init().
349  */
eap_peer_tls_ssl_deinit(struct eap_sm * sm,struct eap_ssl_data * data)350 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
351 {
352 	tls_connection_deinit(data->ssl_ctx, data->conn);
353 	eap_peer_tls_reset_input(data);
354 	eap_peer_tls_reset_output(data);
355 }
356 
357 
358 /**
359  * eap_peer_tls_derive_key - Derive a key based on TLS session data
360  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
361  * @data: Data for TLS processing
362  * @label: Label string for deriving the keys, e.g., "client EAP encryption"
363  * @context: Optional extra upper-layer context (max len 2^16)
364  * @context_len: The length of the context value
365  * @len: Length of the key material to generate (usually 64 for MSK)
366  * Returns: Pointer to allocated key on success or %NULL on failure
367  *
368  * This function uses TLS-PRF to generate pseudo-random data based on the TLS
369  * session data (client/server random and master key). Each key type may use a
370  * different label to bind the key usage into the generated material.
371  *
372  * The caller is responsible for freeing the returned buffer.
373  *
374  * Note: To provide the RFC 5705 context, the context variable must be non-NULL.
375  */
eap_peer_tls_derive_key(struct eap_sm * sm,struct eap_ssl_data * data,const char * label,const u8 * context,size_t context_len,size_t len)376 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
377 			     const char *label, const u8 *context,
378 			     size_t context_len, size_t len)
379 {
380 	u8 *out;
381 
382 	out = os_malloc(len);
383 	if (out == NULL)
384 		return NULL;
385 
386 	if (tls_connection_export_key(data->ssl_ctx, data->conn, label,
387 				      context, context_len, out, len)) {
388 		os_free(out);
389 		return NULL;
390 	}
391 
392 	return out;
393 }
394 
395 
396 /**
397  * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
398  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
399  * @data: Data for TLS processing
400  * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
401  * @len: Pointer to length of the session ID generated
402  * Returns: Pointer to allocated Session-Id on success or %NULL on failure
403  *
404  * This function derive the Session-Id based on the TLS session data
405  * (client/server random and method type).
406  *
407  * The caller is responsible for freeing the returned buffer.
408  */
eap_peer_tls_derive_session_id(struct eap_sm * sm,struct eap_ssl_data * data,u8 eap_type,size_t * len)409 u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
410 				    struct eap_ssl_data *data, u8 eap_type,
411 				    size_t *len)
412 {
413 	struct tls_random keys;
414 	u8 *out;
415 
416 	if (data->tls_v13) {
417 		u8 *id, *method_id;
418 		const u8 context[] = { eap_type };
419 
420 		/* Session-Id = <EAP-Type> || Method-Id
421 		 * Method-Id = TLS-Exporter("EXPORTER_EAP_TLS_Method-Id",
422 		 *                          Type-Code, 64)
423 		 */
424 		*len = 1 + 64;
425 		id = os_malloc(*len);
426 		if (!id)
427 			return NULL;
428 		method_id = eap_peer_tls_derive_key(
429 			sm, data, "EXPORTER_EAP_TLS_Method-Id", context, 1, 64);
430 		if (!method_id) {
431 			os_free(id);
432 			return NULL;
433 		}
434 		id[0] = eap_type;
435 		os_memcpy(id + 1, method_id, 64);
436 		os_free(method_id);
437 		return id;
438 	}
439 
440 	if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys) ||
441 	    keys.client_random == NULL || keys.server_random == NULL)
442 		return NULL;
443 
444 	*len = 1 + keys.client_random_len + keys.server_random_len;
445 	out = os_malloc(*len);
446 	if (out == NULL)
447 		return NULL;
448 
449 	/* Session-Id = EAP type || client.random || server.random */
450 	out[0] = eap_type;
451 	os_memcpy(out + 1, keys.client_random, keys.client_random_len);
452 	os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
453 		  keys.server_random_len);
454 
455 	return out;
456 }
457 
458 
459 /**
460  * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
461  * @data: Data for TLS processing
462  * @in_data: Next incoming TLS segment
463  * Returns: 0 on success, 1 if more data is needed for the full message, or
464  * -1 on error
465  */
eap_peer_tls_reassemble_fragment(struct eap_ssl_data * data,const struct wpabuf * in_data)466 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
467 					    const struct wpabuf *in_data)
468 {
469 	size_t tls_in_len, in_len;
470 
471 	tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
472 	in_len = in_data ? wpabuf_len(in_data) : 0;
473 
474 	if (tls_in_len + in_len == 0) {
475 		/* No message data received?! */
476 		wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
477 			   "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
478 			   (unsigned long) data->tls_in_left,
479 			   (unsigned long) tls_in_len,
480 			   (unsigned long) in_len);
481 		eap_peer_tls_reset_input(data);
482 		return -1;
483 	}
484 
485 	if (tls_in_len + in_len > 65536) {
486 		/*
487 		 * Limit length to avoid rogue servers from causing large
488 		 * memory allocations.
489 		 */
490 		wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
491 			   "64 kB)");
492 		eap_peer_tls_reset_input(data);
493 		return -1;
494 	}
495 
496 	if (in_len > data->tls_in_left) {
497 		/* Sender is doing something odd - reject message */
498 		wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
499 			   "indicated");
500 		eap_peer_tls_reset_input(data);
501 		return -1;
502 	}
503 
504 	if (wpabuf_resize(&data->tls_in, in_len) < 0) {
505 		wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
506 			   "data");
507 		eap_peer_tls_reset_input(data);
508 		return -1;
509 	}
510 	if (in_data)
511 		wpabuf_put_buf(data->tls_in, in_data);
512 	data->tls_in_left -= in_len;
513 
514 	if (data->tls_in_left > 0) {
515 		wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
516 			   "data", (unsigned long) data->tls_in_left);
517 		return 1;
518 	}
519 
520 	return 0;
521 }
522 
523 
524 /**
525  * eap_peer_tls_data_reassemble - Reassemble TLS data
526  * @data: Data for TLS processing
527  * @in_data: Next incoming TLS segment
528  * @need_more_input: Variable for returning whether more input data is needed
529  * to reassemble this TLS packet
530  * Returns: Pointer to output data, %NULL on error or when more data is needed
531  * for the full message (in which case, *need_more_input is also set to 1).
532  *
533  * This function reassembles TLS fragments. Caller must not free the returned
534  * data buffer since an internal pointer to it is maintained.
535  */
eap_peer_tls_data_reassemble(struct eap_ssl_data * data,const struct wpabuf * in_data,int * need_more_input)536 static const struct wpabuf * eap_peer_tls_data_reassemble(
537 	struct eap_ssl_data *data, const struct wpabuf *in_data,
538 	int *need_more_input)
539 {
540 	*need_more_input = 0;
541 
542 	if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
543 		/* Message has fragments */
544 		int res = eap_peer_tls_reassemble_fragment(data, in_data);
545 		if (res) {
546 			if (res == 1)
547 				*need_more_input = 1;
548 			return NULL;
549 		}
550 
551 		/* Message is now fully reassembled. */
552 	} else {
553 		/* No fragments in this message, so just make a copy of it. */
554 		data->tls_in_left = 0;
555 		data->tls_in = wpabuf_dup(in_data);
556 		if (data->tls_in == NULL)
557 			return NULL;
558 	}
559 
560 	return data->tls_in;
561 }
562 
563 
564 /**
565  * eap_tls_process_input - Process incoming TLS message
566  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
567  * @data: Data for TLS processing
568  * @in_data: Message received from the server
569  * @out_data: Buffer for returning a pointer to application data (if available)
570  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
571  * is available, -1 on failure
572  */
eap_tls_process_input(struct eap_sm * sm,struct eap_ssl_data * data,const struct wpabuf * in_data,struct wpabuf ** out_data)573 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
574 				 const struct wpabuf *in_data,
575 				 struct wpabuf **out_data)
576 {
577 	const struct wpabuf *msg;
578 	int need_more_input;
579 	struct wpabuf *appl_data;
580 
581 	msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
582 	if (msg == NULL)
583 		return need_more_input ? 1 : -1;
584 
585 	/* Full TLS message reassembled - continue handshake processing */
586 	if (data->tls_out) {
587 		/* This should not happen.. */
588 		wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
589 			   "tls_out data even though tls_out_len = 0");
590 		wpabuf_free(data->tls_out);
591 		WPA_ASSERT(data->tls_out == NULL);
592 	}
593 	appl_data = NULL;
594 	data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
595 						 msg, &appl_data);
596 
597 	eap_peer_tls_reset_input(data);
598 
599 	if (appl_data &&
600 	    tls_connection_established(data->ssl_ctx, data->conn) &&
601 	    !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
602 		wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
603 				    appl_data);
604 		*out_data = appl_data;
605 		return 2;
606 	}
607 
608 	wpabuf_free(appl_data);
609 
610 	return 0;
611 }
612 
613 
614 /**
615  * eap_tls_process_output - Process outgoing TLS message
616  * @data: Data for TLS processing
617  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
618  * @peap_version: Version number for EAP-PEAP/TTLS
619  * @id: EAP identifier for the response
620  * @ret: Return value to use on success
621  * @out_data: Buffer for returning the allocated output buffer
622  * Returns: ret (0 or 1) on success, -1 on failure
623  */
eap_tls_process_output(struct eap_ssl_data * data,enum eap_type eap_type,int peap_version,u8 id,int ret,struct wpabuf ** out_data)624 static int eap_tls_process_output(struct eap_ssl_data *data,
625 				  enum eap_type eap_type,
626 				  int peap_version, u8 id, int ret,
627 				  struct wpabuf **out_data)
628 {
629 	size_t len;
630 	u8 *flags;
631 	int more_fragments, length_included;
632 
633 	if (data->tls_out == NULL)
634 		return -1;
635 	len = wpabuf_len(data->tls_out) - data->tls_out_pos;
636 	wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
637 		   "%lu bytes)",
638 		   (unsigned long) len,
639 		   (unsigned long) wpabuf_len(data->tls_out));
640 
641 	/*
642 	 * Limit outgoing message to the configured maximum size. Fragment
643 	 * message if needed.
644 	 */
645 	if (len > data->tls_out_limit) {
646 		more_fragments = 1;
647 		len = data->tls_out_limit;
648 		wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
649 			   "will follow", (unsigned long) len);
650 	} else
651 		more_fragments = 0;
652 
653 	length_included = data->tls_out_pos == 0 &&
654 		(wpabuf_len(data->tls_out) > data->tls_out_limit ||
655 		 data->include_tls_length);
656 	if (!length_included &&
657 	    eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
658 	    !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
659 		/*
660 		 * Windows Server 2008 NPS really wants to have the TLS Message
661 		 * length included in phase 0 even for unfragmented frames or
662 		 * it will get very confused with Compound MAC calculation and
663 		 * Outer TLVs.
664 		 */
665 		length_included = 1;
666 	}
667 
668 	*out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
669 				      EAP_CODE_RESPONSE, id);
670 	if (*out_data == NULL)
671 		return -1;
672 
673 	flags = wpabuf_put(*out_data, 1);
674 	*flags = peap_version;
675 	if (more_fragments)
676 		*flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
677 	if (length_included) {
678 		*flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
679 		wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
680 	}
681 
682 	wpabuf_put_data(*out_data,
683 			wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
684 			len);
685 	data->tls_out_pos += len;
686 
687 	if (!more_fragments)
688 		eap_peer_tls_reset_output(data);
689 
690 	return ret;
691 }
692 
693 
694 /**
695  * eap_peer_tls_process_helper - Process TLS handshake message
696  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
697  * @data: Data for TLS processing
698  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
699  * @peap_version: Version number for EAP-PEAP/TTLS
700  * @id: EAP identifier for the response
701  * @in_data: Message received from the server
702  * @out_data: Buffer for returning a pointer to the response message
703  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
704  * is available, or -1 on failure
705  *
706  * This function can be used to process TLS handshake messages. It reassembles
707  * the received fragments and uses a TLS library to process the messages. The
708  * response data from the TLS library is fragmented to suitable output messages
709  * that the caller can send out.
710  *
711  * out_data is used to return the response message if the return value of this
712  * function is 0, 2, or -1. In case of failure, the message is likely a TLS
713  * alarm message. The caller is responsible for freeing the allocated buffer if
714  * *out_data is not %NULL.
715  *
716  * This function is called for each received TLS message during the TLS
717  * handshake after eap_peer_tls_process_init() call and possible processing of
718  * TLS Flags field. Once the handshake has been completed, i.e., when
719  * tls_connection_established() returns 1, EAP method specific decrypting of
720  * the tunneled data is used.
721  */
eap_peer_tls_process_helper(struct eap_sm * sm,struct eap_ssl_data * data,enum eap_type eap_type,int peap_version,u8 id,const struct wpabuf * in_data,struct wpabuf ** out_data)722 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
723 				enum eap_type eap_type, int peap_version,
724 				u8 id, const struct wpabuf *in_data,
725 				struct wpabuf **out_data)
726 {
727 	int ret = 0;
728 
729 	*out_data = NULL;
730 
731 	if (data->tls_out && wpabuf_len(data->tls_out) > 0 &&
732 	    wpabuf_len(in_data) > 0) {
733 		wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
734 			   "fragments are waiting to be sent out");
735 		return -1;
736 	}
737 
738 	if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
739 		/*
740 		 * No more data to send out - expect to receive more data from
741 		 * the AS.
742 		 */
743 		int res = eap_tls_process_input(sm, data, in_data, out_data);
744 		char buf[20];
745 
746 		if (res) {
747 			/*
748 			 * Input processing failed (res = -1) or more data is
749 			 * needed (res = 1).
750 			 */
751 			return res;
752 		}
753 
754 		/*
755 		 * The incoming message has been reassembled and processed. The
756 		 * response was allocated into data->tls_out buffer.
757 		 */
758 
759 		if (tls_get_version(data->ssl_ctx, data->conn,
760 				    buf, sizeof(buf)) == 0) {
761 			wpa_printf(MSG_DEBUG, "SSL: Using TLS version %s", buf);
762 			data->tls_v13 = os_strcmp(buf, "TLSv1.3") == 0;
763 		}
764 	}
765 
766 	if (data->tls_out == NULL) {
767 		/*
768 		 * No outgoing fragments remaining from the previous message
769 		 * and no new message generated. This indicates an error in TLS
770 		 * processing.
771 		 */
772 		eap_peer_tls_reset_output(data);
773 		return -1;
774 	}
775 
776 	if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
777 		/* TLS processing has failed - return error */
778 		wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
779 			   "report error (len=%u)",
780 			   (unsigned int) wpabuf_len(data->tls_out));
781 		ret = -1;
782 		/* TODO: clean pin if engine used? */
783 		if (wpabuf_len(data->tls_out) == 0) {
784 			wpabuf_free(data->tls_out);
785 			data->tls_out = NULL;
786 			return -1;
787 		}
788 	}
789 
790 	if (wpabuf_len(data->tls_out) == 0) {
791 		/*
792 		 * TLS negotiation should now be complete since all other cases
793 		 * needing more data should have been caught above based on
794 		 * the TLS Message Length field.
795 		 */
796 		wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
797 		wpabuf_free(data->tls_out);
798 		data->tls_out = NULL;
799 		return 1;
800 	}
801 
802 	/* Send the pending message (in fragments, if needed). */
803 	return eap_tls_process_output(data, eap_type, peap_version, id, ret,
804 				      out_data);
805 }
806 
807 
808 /**
809  * eap_peer_tls_build_ack - Build a TLS ACK frame
810  * @id: EAP identifier for the response
811  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
812  * @peap_version: Version number for EAP-PEAP/TTLS
813  * Returns: Pointer to the allocated ACK frame or %NULL on failure
814  */
eap_peer_tls_build_ack(u8 id,enum eap_type eap_type,int peap_version)815 struct wpabuf * eap_peer_tls_build_ack(u8 id, enum eap_type eap_type,
816 				       int peap_version)
817 {
818 	struct wpabuf *resp;
819 
820 	resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
821 	if (resp == NULL)
822 		return NULL;
823 	wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
824 		   (int) eap_type, id, peap_version);
825 	wpabuf_put_u8(resp, peap_version); /* Flags */
826 	return resp;
827 }
828 
829 
830 /**
831  * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
832  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
833  * @data: Data for TLS processing
834  * Returns: 0 on success, -1 on failure
835  */
eap_peer_tls_reauth_init(struct eap_sm * sm,struct eap_ssl_data * data)836 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
837 {
838 	eap_peer_tls_reset_input(data);
839 	eap_peer_tls_reset_output(data);
840 	return tls_connection_shutdown(data->ssl_ctx, data->conn);
841 }
842 
843 
844 /**
845  * eap_peer_tls_status - Get TLS status
846  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
847  * @data: Data for TLS processing
848  * @buf: Buffer for status information
849  * @buflen: Maximum buffer length
850  * @verbose: Whether to include verbose status information
851  * Returns: Number of bytes written to buf.
852  */
eap_peer_tls_status(struct eap_sm * sm,struct eap_ssl_data * data,char * buf,size_t buflen,int verbose)853 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
854 			char *buf, size_t buflen, int verbose)
855 {
856 	char version[20], name[128];
857 	int len = 0, ret;
858 
859 	if (tls_get_version(data->ssl_ctx, data->conn, version,
860 			    sizeof(version)) < 0)
861 		version[0] = '\0';
862 	if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) < 0)
863 		name[0] = '\0';
864 
865 	ret = os_snprintf(buf + len, buflen - len,
866 			  "eap_tls_version=%s\n"
867 			  "EAP TLS cipher=%s\n"
868 			  "tls_session_reused=%d\n",
869 			  version, name,
870 			  tls_connection_resumed(data->ssl_ctx, data->conn));
871 	if (os_snprintf_error(buflen - len, ret))
872 		return len;
873 	len += ret;
874 
875 	return len;
876 }
877 
878 
879 /**
880  * eap_peer_tls_process_init - Initial validation/processing of EAP requests
881  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
882  * @data: Data for TLS processing
883  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
884  * @ret: Return values from EAP request validation and processing
885  * @reqData: EAP request to be processed (eapReqData)
886  * @len: Buffer for returning length of the remaining payload
887  * @flags: Buffer for returning TLS flags
888  * Returns: Pointer to payload after TLS flags and length or %NULL on failure
889  *
890  * This function validates the EAP header and processes the optional TLS
891  * Message Length field. If this is the first fragment of a TLS message, the
892  * TLS reassembly code is initialized to receive the indicated number of bytes.
893  *
894  * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
895  * function as the first step in processing received messages. They will need
896  * to process the flags (apart from Message Length Included) that are returned
897  * through the flags pointer and the message payload that will be returned (and
898  * the length is returned through the len pointer). Return values (ret) are set
899  * for continuation of EAP method processing. The caller is responsible for
900  * setting these to indicate completion (either success or failure) based on
901  * the authentication result.
902  */
eap_peer_tls_process_init(struct eap_sm * sm,struct eap_ssl_data * data,enum eap_type eap_type,struct eap_method_ret * ret,const struct wpabuf * reqData,size_t * len,u8 * flags)903 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
904 				     struct eap_ssl_data *data,
905 				     enum eap_type eap_type,
906 				     struct eap_method_ret *ret,
907 				     const struct wpabuf *reqData,
908 				     size_t *len, u8 *flags)
909 {
910 	const u8 *pos;
911 	size_t left;
912 	unsigned int tls_msg_len;
913 
914 	if (tls_get_errors(data->ssl_ctx)) {
915 		wpa_printf(MSG_INFO, "SSL: TLS errors detected");
916 		ret->ignore = true;
917 		return NULL;
918 	}
919 
920 	if (eap_type == EAP_UNAUTH_TLS_TYPE)
921 		pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
922 				       EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
923 				       &left);
924 	else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
925 		pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
926 				       EAP_VENDOR_WFA_UNAUTH_TLS, reqData,
927 				       &left);
928 	else
929 		pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
930 				       &left);
931 	if (pos == NULL) {
932 		ret->ignore = true;
933 		return NULL;
934 	}
935 	if (left == 0) {
936 		wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
937 			   "octet included");
938 		if (!sm->workaround) {
939 			ret->ignore = true;
940 			return NULL;
941 		}
942 
943 		wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
944 			   "indicates ACK frame");
945 		*flags = 0;
946 	} else {
947 		*flags = *pos++;
948 		left--;
949 	}
950 	wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
951 		   "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
952 		   *flags);
953 	if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
954 		if (left < 4) {
955 			wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
956 				   "length");
957 			ret->ignore = true;
958 			return NULL;
959 		}
960 		tls_msg_len = WPA_GET_BE32(pos);
961 		wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
962 			   tls_msg_len);
963 		if (data->tls_in_left == 0) {
964 			data->tls_in_total = tls_msg_len;
965 			data->tls_in_left = tls_msg_len;
966 			wpabuf_free(data->tls_in);
967 			data->tls_in = NULL;
968 		}
969 		pos += 4;
970 		left -= 4;
971 
972 		if (left > tls_msg_len) {
973 			wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
974 				   "bytes) smaller than this fragment (%d "
975 				   "bytes)", (int) tls_msg_len, (int) left);
976 			ret->ignore = true;
977 			return NULL;
978 		}
979 	}
980 
981 	ret->ignore = false;
982 	ret->methodState = METHOD_MAY_CONT;
983 	ret->decision = DECISION_FAIL;
984 	ret->allowNotifications = true;
985 
986 	*len = left;
987 	return pos;
988 }
989 
990 
991 /**
992  * eap_peer_tls_reset_input - Reset input buffers
993  * @data: Data for TLS processing
994  *
995  * This function frees any allocated memory for input buffers and resets input
996  * state.
997  */
eap_peer_tls_reset_input(struct eap_ssl_data * data)998 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
999 {
1000 	data->tls_in_left = data->tls_in_total = 0;
1001 	wpabuf_free(data->tls_in);
1002 	data->tls_in = NULL;
1003 }
1004 
1005 
1006 /**
1007  * eap_peer_tls_reset_output - Reset output buffers
1008  * @data: Data for TLS processing
1009  *
1010  * This function frees any allocated memory for output buffers and resets
1011  * output state.
1012  */
eap_peer_tls_reset_output(struct eap_ssl_data * data)1013 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
1014 {
1015 	data->tls_out_pos = 0;
1016 	wpabuf_free(data->tls_out);
1017 	data->tls_out = NULL;
1018 }
1019 
1020 
1021 /**
1022  * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
1023  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1024  * @data: Data for TLS processing
1025  * @in_data: Message received from the server
1026  * @in_decrypted: Buffer for returning a pointer to the decrypted message
1027  * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
1028  */
eap_peer_tls_decrypt(struct eap_sm * sm,struct eap_ssl_data * data,const struct wpabuf * in_data,struct wpabuf ** in_decrypted)1029 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1030 			 const struct wpabuf *in_data,
1031 			 struct wpabuf **in_decrypted)
1032 {
1033 	const struct wpabuf *msg;
1034 	int need_more_input;
1035 
1036 	msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
1037 	if (msg == NULL)
1038 		return need_more_input ? 1 : -1;
1039 
1040 	*in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
1041 	eap_peer_tls_reset_input(data);
1042 	if (*in_decrypted == NULL) {
1043 		wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
1044 		return -1;
1045 	}
1046 	return 0;
1047 }
1048 
1049 
1050 /**
1051  * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
1052  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1053  * @data: Data for TLS processing
1054  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
1055  * @peap_version: Version number for EAP-PEAP/TTLS
1056  * @id: EAP identifier for the response
1057  * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
1058  * @out_data: Buffer for returning a pointer to the encrypted response message
1059  * Returns: 0 on success, -1 on failure
1060  */
eap_peer_tls_encrypt(struct eap_sm * sm,struct eap_ssl_data * data,enum eap_type eap_type,int peap_version,u8 id,const struct wpabuf * in_data,struct wpabuf ** out_data)1061 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
1062 			 enum eap_type eap_type, int peap_version, u8 id,
1063 			 const struct wpabuf *in_data,
1064 			 struct wpabuf **out_data)
1065 {
1066 	if (in_data) {
1067 		eap_peer_tls_reset_output(data);
1068 		data->tls_out = tls_connection_encrypt(data->ssl_ctx,
1069 						       data->conn, in_data);
1070 		if (data->tls_out == NULL) {
1071 			wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
1072 				   "data (in_len=%lu)",
1073 				   (unsigned long) wpabuf_len(in_data));
1074 			eap_peer_tls_reset_output(data);
1075 			return -1;
1076 		}
1077 	}
1078 
1079 	return eap_tls_process_output(data, eap_type, peap_version, id, 0,
1080 				      out_data);
1081 }
1082 
1083 
1084 /**
1085  * eap_peer_select_phase2_methods - Select phase 2 EAP method
1086  * @config: Pointer to the network configuration
1087  * @prefix: 'phase2' configuration prefix, e.g., "auth="
1088  * @types: Buffer for returning allocated list of allowed EAP methods
1089  * @num_types: Buffer for returning number of allocated EAP methods
1090  * Returns: 0 on success, -1 on failure
1091  *
1092  * This function is used to parse EAP method list and select allowed methods
1093  * for Phase2 authentication.
1094  */
eap_peer_select_phase2_methods(struct eap_peer_config * config,const char * prefix,struct eap_method_type ** types,size_t * num_types,int use_machine_cred)1095 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
1096 				   const char *prefix,
1097 				   struct eap_method_type **types,
1098 				   size_t *num_types, int use_machine_cred)
1099 {
1100 	char *start, *pos, *buf;
1101 	struct eap_method_type *methods = NULL, *_methods;
1102 	u32 method;
1103 	size_t num_methods = 0, prefix_len;
1104 	const char *phase2;
1105 
1106 	if (!config)
1107 		goto get_defaults;
1108 	phase2 = use_machine_cred ? config->machine_phase2 : config->phase2;
1109 	if (!phase2)
1110 		goto get_defaults;
1111 
1112 	start = buf = os_strdup(phase2);
1113 	if (buf == NULL)
1114 		return -1;
1115 
1116 	prefix_len = os_strlen(prefix);
1117 
1118 	while (start && *start != '\0') {
1119 		int vendor;
1120 		pos = os_strstr(start, prefix);
1121 		if (pos == NULL)
1122 			break;
1123 		if (start != pos && *(pos - 1) != ' ') {
1124 			start = pos + prefix_len;
1125 			continue;
1126 		}
1127 
1128 		start = pos + prefix_len;
1129 		pos = os_strchr(start, ' ');
1130 		if (pos)
1131 			*pos++ = '\0';
1132 		method = eap_get_phase2_type(start, &vendor);
1133 		if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
1134 			wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
1135 				   "method '%s'", start);
1136 			os_free(methods);
1137 			os_free(buf);
1138 			return -1;
1139 		} else {
1140 			num_methods++;
1141 			_methods = os_realloc_array(methods, num_methods,
1142 						    sizeof(*methods));
1143 			if (_methods == NULL) {
1144 				os_free(methods);
1145 				os_free(buf);
1146 				return -1;
1147 			}
1148 			methods = _methods;
1149 			methods[num_methods - 1].vendor = vendor;
1150 			methods[num_methods - 1].method = method;
1151 		}
1152 
1153 		start = pos;
1154 	}
1155 
1156 	os_free(buf);
1157 
1158 get_defaults:
1159 	if (methods == NULL)
1160 		methods = eap_get_phase2_types(config, &num_methods);
1161 
1162 	if (methods == NULL) {
1163 		wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
1164 		return -1;
1165 	}
1166 	wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
1167 		    (u8 *) methods,
1168 		    num_methods * sizeof(struct eap_method_type));
1169 
1170 	*types = methods;
1171 	*num_types = num_methods;
1172 
1173 	return 0;
1174 }
1175 
1176 
1177 /**
1178  * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
1179  * @types: Buffer for returning allocated list of allowed EAP methods
1180  * @num_types: Buffer for returning number of allocated EAP methods
1181  * @hdr: EAP-Request header (and the following EAP type octet)
1182  * @resp: Buffer for returning the EAP-Nak message
1183  * Returns: 0 on success, -1 on failure
1184  */
eap_peer_tls_phase2_nak(struct eap_method_type * types,size_t num_types,struct eap_hdr * hdr,struct wpabuf ** resp)1185 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
1186 			    struct eap_hdr *hdr, struct wpabuf **resp)
1187 {
1188 	u8 *pos = (u8 *) (hdr + 1);
1189 	size_t i;
1190 
1191 	/* TODO: add support for expanded Nak */
1192 	wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1193 	wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1194 		    (u8 *) types, num_types * sizeof(struct eap_method_type));
1195 	*resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1196 			      EAP_CODE_RESPONSE, hdr->identifier);
1197 	if (*resp == NULL)
1198 		return -1;
1199 
1200 	for (i = 0; i < num_types; i++) {
1201 		if (types[i].vendor == EAP_VENDOR_IETF &&
1202 		    types[i].method < 256)
1203 			wpabuf_put_u8(*resp, types[i].method);
1204 	}
1205 
1206 	eap_update_len(*resp);
1207 
1208 	return 0;
1209 }
1210