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