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