Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / eap_peer / eap_tls_common.c
blobd2a494bbf2b4d65237a7c0cfaab813ad6542d5c7
1 /*
2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eap_i.h"
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
21 #include "sha1.h"
22 #include "tls.h"
25 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
26 const u8 **data, size_t *data_len)
28 const struct wpa_config_blob *blob;
30 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
31 return 0;
33 blob = eap_get_config_blob(sm, *name + 7);
34 if (blob == NULL) {
35 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
36 "found", __func__, *name + 7);
37 return -1;
40 *name = NULL;
41 *data = blob->data;
42 *data_len = blob->len;
44 return 0;
48 static void eap_tls_params_from_conf1(struct tls_connection_params *params,
49 struct eap_peer_config *config)
51 params->ca_cert = (char *) config->ca_cert;
52 params->ca_path = (char *) config->ca_path;
53 params->client_cert = (char *) config->client_cert;
54 params->private_key = (char *) config->private_key;
55 params->private_key_passwd = (char *) config->private_key_passwd;
56 params->dh_file = (char *) config->dh_file;
57 params->subject_match = (char *) config->subject_match;
58 params->altsubject_match = (char *) config->altsubject_match;
59 params->engine_id = config->engine_id;
60 params->pin = config->pin;
61 params->key_id = config->key_id;
62 params->cert_id = config->cert_id;
63 params->ca_cert_id = config->ca_cert_id;
67 static void eap_tls_params_from_conf2(struct tls_connection_params *params,
68 struct eap_peer_config *config)
70 params->ca_cert = (char *) config->ca_cert2;
71 params->ca_path = (char *) config->ca_path2;
72 params->client_cert = (char *) config->client_cert2;
73 params->private_key = (char *) config->private_key2;
74 params->private_key_passwd = (char *) config->private_key2_passwd;
75 params->dh_file = (char *) config->dh_file2;
76 params->subject_match = (char *) config->subject_match2;
77 params->altsubject_match = (char *) config->altsubject_match2;
78 params->engine_id = config->engine_id;
79 params->pin = config->pin;
80 params->key_id = config->key2_id;
81 params->cert_id = config->cert2_id;
82 params->ca_cert_id = config->ca_cert2_id;
86 static int eap_tls_params_from_conf(struct eap_sm *sm,
87 struct eap_ssl_data *data,
88 struct tls_connection_params *params,
89 struct eap_peer_config *config, int phase2)
91 os_memset(params, 0, sizeof(*params));
92 params->engine = config->engine;
93 if (phase2)
94 eap_tls_params_from_conf2(params, config);
95 else
96 eap_tls_params_from_conf1(params, config);
97 params->tls_ia = data->tls_ia;
100 * Use blob data, if available. Otherwise, leave reference to external
101 * file as-is.
103 if (eap_tls_check_blob(sm, &params->ca_cert, &params->ca_cert_blob,
104 &params->ca_cert_blob_len) ||
105 eap_tls_check_blob(sm, &params->client_cert,
106 &params->client_cert_blob,
107 &params->client_cert_blob_len) ||
108 eap_tls_check_blob(sm, &params->private_key,
109 &params->private_key_blob,
110 &params->private_key_blob_len) ||
111 eap_tls_check_blob(sm, &params->dh_file, &params->dh_blob,
112 &params->dh_blob_len)) {
113 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
114 return -1;
117 return 0;
121 static int eap_tls_init_connection(struct eap_sm *sm,
122 struct eap_ssl_data *data,
123 struct eap_peer_config *config,
124 struct tls_connection_params *params)
126 int res;
128 data->conn = tls_connection_init(sm->ssl_ctx);
129 if (data->conn == NULL) {
130 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
131 "connection");
132 return -1;
135 res = tls_connection_set_params(sm->ssl_ctx, data->conn, params);
136 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
138 * At this point with the pkcs11 engine the PIN might be wrong.
139 * We reset the PIN in the configuration to be sure to not use
140 * it again and the calling function must request a new one.
142 os_free(config->pin);
143 config->pin = NULL;
144 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
145 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
147 * We do not know exactly but maybe the PIN was wrong,
148 * so ask for a new one.
150 os_free(config->pin);
151 config->pin = NULL;
152 eap_sm_request_pin(sm);
153 sm->ignore = TRUE;
154 return -1;
155 } else if (res) {
156 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
157 "parameters");
158 return -1;
161 return 0;
166 * eap_peer_tls_ssl_init - Initialize shared TLS functionality
167 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
168 * @data: Data for TLS processing
169 * @config: Pointer to the network configuration
170 * Returns: 0 on success, -1 on failure
172 * This function is used to initialize shared TLS functionality for EAP-TLS,
173 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
175 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
176 struct eap_peer_config *config)
178 struct tls_connection_params params;
180 if (config == NULL)
181 return -1;
183 data->eap = sm;
184 data->phase2 = sm->init_phase2;
185 if (eap_tls_params_from_conf(sm, data, &params, config, data->phase2) <
187 return -1;
189 if (eap_tls_init_connection(sm, data, config, &params) < 0)
190 return -1;
192 data->tls_out_limit = config->fragment_size;
193 if (data->phase2) {
194 /* Limit the fragment size in the inner TLS authentication
195 * since the outer authentication with EAP-PEAP does not yet
196 * support fragmentation */
197 if (data->tls_out_limit > 100)
198 data->tls_out_limit -= 100;
201 if (config->phase1 &&
202 os_strstr(config->phase1, "include_tls_length=1")) {
203 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
204 "unfragmented packets");
205 data->include_tls_length = 1;
208 return 0;
213 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
214 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
215 * @data: Data for TLS processing
217 * This function deinitializes shared TLS functionality that was initialized
218 * with eap_peer_tls_ssl_init().
220 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
222 tls_connection_deinit(sm->ssl_ctx, data->conn);
223 eap_peer_tls_reset_input(data);
224 eap_peer_tls_reset_output(data);
229 * eap_peer_tls_derive_key - Derive a key based on TLS session data
230 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
231 * @data: Data for TLS processing
232 * @label: Label string for deriving the keys, e.g., "client EAP encryption"
233 * @len: Length of the key material to generate (usually 64 for MSK)
234 * Returns: Pointer to allocated key on success or %NULL on failure
236 * This function uses TLS-PRF to generate pseudo-random data based on the TLS
237 * session data (client/server random and master key). Each key type may use a
238 * different label to bind the key usage into the generated material.
240 * The caller is responsible for freeing the returned buffer.
242 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
243 const char *label, size_t len)
245 struct tls_keys keys;
246 u8 *rnd = NULL, *out;
248 out = os_malloc(len);
249 if (out == NULL)
250 return NULL;
252 /* First, try to use TLS library function for PRF, if available. */
253 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
255 return out;
258 * TLS library did not support key generation, so get the needed TLS
259 * session parameters and use an internal implementation of TLS PRF to
260 * derive the key.
262 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
263 goto fail;
265 if (keys.client_random == NULL || keys.server_random == NULL ||
266 keys.master_key == NULL)
267 goto fail;
269 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
270 if (rnd == NULL)
271 goto fail;
272 os_memcpy(rnd, keys.client_random, keys.client_random_len);
273 os_memcpy(rnd + keys.client_random_len, keys.server_random,
274 keys.server_random_len);
276 if (tls_prf(keys.master_key, keys.master_key_len,
277 label, rnd, keys.client_random_len +
278 keys.server_random_len, out, len))
279 goto fail;
281 os_free(rnd);
282 return out;
284 fail:
285 os_free(out);
286 os_free(rnd);
287 return NULL;
292 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
293 * @data: Data for TLS processing
294 * @in_data: Next incoming TLS segment
295 * @in_len: Length of in_data
296 * Returns: 0 on success, 1 if more data is needed for the full message, or
297 * -1 on error
299 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
300 const u8 *in_data, size_t in_len)
302 u8 *buf;
304 if (data->tls_in_len + in_len == 0) {
305 /* No message data received?! */
306 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
307 "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
308 (unsigned long) data->tls_in_left,
309 (unsigned long) data->tls_in_len,
310 (unsigned long) in_len);
311 eap_peer_tls_reset_input(data);
312 return -1;
315 if (data->tls_in_len + in_len > 65536) {
317 * Limit length to avoid rogue servers from causing large
318 * memory allocations.
320 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
321 "64 kB)");
322 eap_peer_tls_reset_input(data);
323 return -1;
326 if (in_len > data->tls_in_left) {
327 /* Sender is doing something odd - reject message */
328 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
329 "indicated");
330 eap_peer_tls_reset_input(data);
331 return -1;
334 buf = os_realloc(data->tls_in, data->tls_in_len + in_len);
335 if (buf == NULL) {
336 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
337 "data");
338 eap_peer_tls_reset_input(data);
339 return -1;
341 os_memcpy(buf + data->tls_in_len, in_data, in_len);
342 data->tls_in = buf;
343 data->tls_in_len += in_len;
344 data->tls_in_left -= in_len;
346 if (data->tls_in_left > 0) {
347 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
348 "data", (unsigned long) data->tls_in_left);
349 return 1;
352 return 0;
357 * eap_peer_tls_data_reassemble - Reassemble TLS data
358 * @data: Data for TLS processing
359 * @in_data: Next incoming TLS segment
360 * @in_len: Length of in_data
361 * @out_len: Variable for returning length of the reassembled message
362 * @need_more_input: Variable for returning whether more input data is needed
363 * to reassemble this TLS packet
364 * Returns: Pointer to output data, %NULL on error or when more data is needed
365 * for the full message (in which case, *need_more_input is also set to 1).
367 * This function reassembles TLS fragments. Caller must not free the returned
368 * data buffer since an internal pointer to it is maintained.
370 const u8 * eap_peer_tls_data_reassemble(
371 struct eap_ssl_data *data, const u8 *in_data, size_t in_len,
372 size_t *out_len, int *need_more_input)
374 *need_more_input = 0;
376 if (data->tls_in_left > in_len || data->tls_in) {
377 /* Message has fragments */
378 int res = eap_peer_tls_reassemble_fragment(data, in_data,
379 in_len);
380 if (res) {
381 if (res == 1)
382 *need_more_input = 1;
383 return NULL;
386 /* Message is now fully reassembled. */
387 } else {
388 /* No fragments in this message, so just make a copy of it. */
389 data->tls_in_left = 0;
390 data->tls_in = os_malloc(in_len ? in_len : 1);
391 if (data->tls_in == NULL)
392 return NULL;
393 os_memcpy(data->tls_in, in_data, in_len);
394 data->tls_in_len = in_len;
397 *out_len = data->tls_in_len;
398 return data->tls_in;
403 * eap_tls_process_input - Process incoming TLS message
404 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
405 * @data: Data for TLS processing
406 * @in_data: Message received from the server
407 * @in_len: Length of in_data
408 * @out_data: Buffer for returning a pointer to application data (if available)
409 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
410 * is available, -1 on failure
412 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
413 const u8 *in_data, size_t in_len,
414 struct wpabuf **out_data)
416 const u8 *msg;
417 size_t msg_len;
418 int need_more_input;
419 u8 *appl_data;
420 size_t appl_data_len;
422 msg = eap_peer_tls_data_reassemble(data, in_data, in_len,
423 &msg_len, &need_more_input);
424 if (msg == NULL)
425 return need_more_input ? 1 : -1;
427 /* Full TLS message reassembled - continue handshake processing */
428 if (data->tls_out) {
429 /* This should not happen.. */
430 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
431 "tls_out data even though tls_out_len = 0");
432 os_free(data->tls_out);
433 WPA_ASSERT(data->tls_out == NULL);
435 appl_data = NULL;
436 data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn,
437 msg, msg_len,
438 &data->tls_out_len,
439 &appl_data, &appl_data_len);
441 eap_peer_tls_reset_input(data);
443 if (appl_data &&
444 tls_connection_established(sm->ssl_ctx, data->conn) &&
445 !tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
446 wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application data",
447 appl_data, appl_data_len);
448 *out_data = wpabuf_alloc_ext_data(appl_data, appl_data_len);
449 if (*out_data == NULL) {
450 os_free(appl_data);
451 return -1;
453 return 2;
456 os_free(appl_data);
458 return 0;
463 * eap_tls_process_output - Process outgoing TLS message
464 * @data: Data for TLS processing
465 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
466 * @peap_version: Version number for EAP-PEAP/TTLS
467 * @id: EAP identifier for the response
468 * @ret: Return value to use on success
469 * @out_data: Buffer for returning the allocated output buffer
470 * Returns: ret (0 or 1) on success, -1 on failure
472 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
473 int peap_version, u8 id, int ret,
474 struct wpabuf **out_data)
476 size_t len;
477 u8 *flags;
478 int more_fragments, length_included;
480 len = data->tls_out_len - data->tls_out_pos;
481 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
482 "%lu bytes)",
483 (unsigned long) len, (unsigned long) data->tls_out_len);
486 * Limit outgoing message to the configured maximum size. Fragment
487 * message if needed.
489 if (len > data->tls_out_limit) {
490 more_fragments = 1;
491 len = data->tls_out_limit;
492 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
493 "will follow", (unsigned long) len);
494 } else
495 more_fragments = 0;
497 length_included = data->tls_out_pos == 0 &&
498 (data->tls_out_len > data->tls_out_limit ||
499 data->include_tls_length);
501 *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type,
502 1 + length_included * 4 + len,
503 EAP_CODE_RESPONSE, id);
504 if (*out_data == NULL)
505 return -1;
507 flags = wpabuf_put(*out_data, 1);
508 *flags = peap_version;
509 if (more_fragments)
510 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
511 if (length_included) {
512 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
513 wpabuf_put_be32(*out_data, data->tls_out_len);
516 wpabuf_put_data(*out_data, &data->tls_out[data->tls_out_pos], len);
517 data->tls_out_pos += len;
519 if (!more_fragments)
520 eap_peer_tls_reset_output(data);
522 return ret;
527 * eap_peer_tls_process_helper - Process TLS handshake message
528 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
529 * @data: Data for TLS processing
530 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
531 * @peap_version: Version number for EAP-PEAP/TTLS
532 * @id: EAP identifier for the response
533 * @in_data: Message received from the server
534 * @in_len: Length of in_data
535 * @out_data: Buffer for returning a pointer to the response message
536 * Returns: 0 on success, 1 if more input data is needed, 2 if application data
537 * is available, or -1 on failure
539 * This function can be used to process TLS handshake messages. It reassembles
540 * the received fragments and uses a TLS library to process the messages. The
541 * response data from the TLS library is fragmented to suitable output messages
542 * that the caller can send out.
544 * out_data is used to return the response message if the return value of this
545 * function is 0, 2, or -1. In case of failure, the message is likely a TLS
546 * alarm message. The caller is responsible for freeing the allocated buffer if
547 * *out_data is not %NULL.
549 * This function is called for each received TLS message during the TLS
550 * handshake after eap_peer_tls_process_init() call and possible processing of
551 * TLS Flags field. Once the handshake has been completed, i.e., when
552 * tls_connection_established() returns 1, EAP method specific decrypting of
553 * the tunneled data is used.
555 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
556 EapType eap_type, int peap_version,
557 u8 id, const u8 *in_data, size_t in_len,
558 struct wpabuf **out_data)
560 int ret = 0;
562 *out_data = NULL;
564 if (data->tls_out_len > 0 && in_len > 0) {
565 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
566 "fragments are waiting to be sent out");
567 return -1;
570 if (data->tls_out_len == 0) {
572 * No more data to send out - expect to receive more data from
573 * the AS.
575 int res = eap_tls_process_input(sm, data, in_data, in_len,
576 out_data);
577 if (res) {
579 * Input processing failed (res = -1) or more data is
580 * needed (res = 1).
582 return res;
586 * The incoming message has been reassembled and processed. The
587 * response was allocated into data->tls_out buffer.
591 if (data->tls_out == NULL) {
593 * No outgoing fragments remaining from the previous message
594 * and no new message generated. This indicates an error in TLS
595 * processing.
597 eap_peer_tls_reset_output(data);
598 return -1;
601 if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
602 /* TLS processing has failed - return error */
603 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
604 "report error");
605 ret = -1;
606 /* TODO: clean pin if engine used? */
609 if (data->tls_out_len == 0) {
611 * TLS negotiation should now be complete since all other cases
612 * needing more data should have been caught above based on
613 * the TLS Message Length field.
615 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
616 os_free(data->tls_out);
617 data->tls_out = NULL;
618 return 1;
621 /* Send the pending message (in fragments, if needed). */
622 return eap_tls_process_output(data, eap_type, peap_version, id, ret,
623 out_data);
628 * eap_peer_tls_build_ack - Build a TLS ACK frame
629 * @id: EAP identifier for the response
630 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
631 * @peap_version: Version number for EAP-PEAP/TTLS
632 * Returns: Pointer to the allocated ACK frame or %NULL on failure
634 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
635 int peap_version)
637 struct wpabuf *resp;
639 resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE,
640 id);
641 if (resp == NULL)
642 return NULL;
643 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
644 (int) eap_type, id, peap_version);
645 wpabuf_put_u8(resp, peap_version); /* Flags */
646 return resp;
651 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
652 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
653 * @data: Data for TLS processing
654 * Returns: 0 on success, -1 on failure
656 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
658 eap_peer_tls_reset_input(data);
659 eap_peer_tls_reset_output(data);
660 return tls_connection_shutdown(sm->ssl_ctx, data->conn);
665 * eap_peer_tls_status - Get TLS status
666 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
667 * @data: Data for TLS processing
668 * @buf: Buffer for status information
669 * @buflen: Maximum buffer length
670 * @verbose: Whether to include verbose status information
671 * Returns: Number of bytes written to buf.
673 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
674 char *buf, size_t buflen, int verbose)
676 char name[128];
677 int len = 0, ret;
679 if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) {
680 ret = os_snprintf(buf + len, buflen - len,
681 "EAP TLS cipher=%s\n", name);
682 if (ret < 0 || (size_t) ret >= buflen - len)
683 return len;
684 len += ret;
687 return len;
692 * eap_peer_tls_process_init - Initial validation/processing of EAP requests
693 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
694 * @data: Data for TLS processing
695 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
696 * @ret: Return values from EAP request validation and processing
697 * @reqData: EAP request to be processed (eapReqData)
698 * @len: Buffer for returning length of the remaining payload
699 * @flags: Buffer for returning TLS flags
700 * Returns: Pointer to payload after TLS flags and length or %NULL on failure
702 * This function validates the EAP header and processes the optional TLS
703 * Message Length field. If this is the first fragment of a TLS message, the
704 * TLS reassembly code is initialized to receive the indicated number of bytes.
706 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
707 * function as the first step in processing received messages. They will need
708 * to process the flags (apart from Message Length Included) that are returned
709 * through the flags pointer and the message payload that will be returned (and
710 * the length is returned through the len pointer). Return values (ret) are set
711 * for continuation of EAP method processing. The caller is responsible for
712 * setting these to indicate completion (either success or failure) based on
713 * the authentication result.
715 const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
716 struct eap_ssl_data *data,
717 EapType eap_type,
718 struct eap_method_ret *ret,
719 const struct wpabuf *reqData,
720 size_t *len, u8 *flags)
722 const u8 *pos;
723 size_t left;
724 unsigned int tls_msg_len;
726 if (tls_get_errors(sm->ssl_ctx)) {
727 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
728 ret->ignore = TRUE;
729 return NULL;
732 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left);
733 if (pos == NULL) {
734 ret->ignore = TRUE;
735 return NULL;
737 *flags = *pos++;
738 left--;
739 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
740 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
741 *flags);
742 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
743 if (left < 4) {
744 wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
745 "length");
746 ret->ignore = TRUE;
747 return NULL;
749 tls_msg_len = WPA_GET_BE32(pos);
750 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
751 tls_msg_len);
752 if (data->tls_in_left == 0) {
753 data->tls_in_total = tls_msg_len;
754 data->tls_in_left = tls_msg_len;
755 os_free(data->tls_in);
756 data->tls_in = NULL;
757 data->tls_in_len = 0;
759 pos += 4;
760 left -= 4;
763 ret->ignore = FALSE;
764 ret->methodState = METHOD_MAY_CONT;
765 ret->decision = DECISION_FAIL;
766 ret->allowNotifications = TRUE;
768 *len = left;
769 return pos;
774 * eap_peer_tls_reset_input - Reset input buffers
775 * @data: Data for TLS processing
777 * This function frees any allocated memory for input buffers and resets input
778 * state.
780 void eap_peer_tls_reset_input(struct eap_ssl_data *data)
782 data->tls_in_left = data->tls_in_total = data->tls_in_len = 0;
783 os_free(data->tls_in);
784 data->tls_in = NULL;
789 * eap_peer_tls_reset_output - Reset output buffers
790 * @data: Data for TLS processing
792 * This function frees any allocated memory for output buffers and resets
793 * output state.
795 void eap_peer_tls_reset_output(struct eap_ssl_data *data)
797 data->tls_out_len = 0;
798 data->tls_out_pos = 0;
799 os_free(data->tls_out);
800 data->tls_out = NULL;
805 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
806 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
807 * @data: Data for TLS processing
808 * @in_data: Message received from the server
809 * @in_decrypted: Buffer for returning a pointer to the decrypted message
810 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
812 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
813 const struct wpabuf *in_data,
814 struct wpabuf **in_decrypted)
816 int res;
817 const u8 *msg;
818 size_t msg_len, buf_len;
819 int need_more_input;
821 msg = eap_peer_tls_data_reassemble(data, wpabuf_head(in_data),
822 wpabuf_len(in_data), &msg_len,
823 &need_more_input);
824 if (msg == NULL)
825 return need_more_input ? 1 : -1;
827 buf_len = wpabuf_len(in_data);
828 if (data->tls_in_total > buf_len)
829 buf_len = data->tls_in_total;
831 * Even though we try to disable TLS compression, it is possible that
832 * this cannot be done with all TLS libraries. Add extra buffer space
833 * to handle the possibility of the decrypted data being longer than
834 * input data.
836 buf_len += 500;
837 buf_len *= 3;
838 *in_decrypted = wpabuf_alloc(buf_len ? buf_len : 1);
839 if (*in_decrypted == NULL) {
840 eap_peer_tls_reset_input(data);
841 wpa_printf(MSG_WARNING, "SSL: Failed to allocate memory for "
842 "decryption");
843 return -1;
846 res = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg, msg_len,
847 wpabuf_mhead(*in_decrypted), buf_len);
848 eap_peer_tls_reset_input(data);
849 if (res < 0) {
850 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
851 return -1;
853 wpabuf_put(*in_decrypted, res);
854 return 0;
859 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
860 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
861 * @data: Data for TLS processing
862 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
863 * @peap_version: Version number for EAP-PEAP/TTLS
864 * @id: EAP identifier for the response
865 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
866 * @out_data: Buffer for returning a pointer to the encrypted response message
867 * Returns: 0 on success, -1 on failure
869 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
870 EapType eap_type, int peap_version, u8 id,
871 const struct wpabuf *in_data,
872 struct wpabuf **out_data)
874 int res;
875 size_t len;
877 if (in_data) {
878 eap_peer_tls_reset_output(data);
879 len = wpabuf_len(in_data) + 100;
880 data->tls_out = os_malloc(len);
881 if (data->tls_out == NULL)
882 return -1;
884 res = tls_connection_encrypt(sm->ssl_ctx, data->conn,
885 wpabuf_head(in_data),
886 wpabuf_len(in_data),
887 data->tls_out, len);
888 if (res < 0) {
889 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
890 "data (in_len=%lu)",
891 (unsigned long) wpabuf_len(in_data));
892 eap_peer_tls_reset_output(data);
893 return -1;
896 data->tls_out_len = res;
899 return eap_tls_process_output(data, eap_type, peap_version, id, 0,
900 out_data);
905 * eap_peer_select_phase2_methods - Select phase 2 EAP method
906 * @config: Pointer to the network configuration
907 * @prefix: 'phase2' configuration prefix, e.g., "auth="
908 * @types: Buffer for returning allocated list of allowed EAP methods
909 * @num_types: Buffer for returning number of allocated EAP methods
910 * Returns: 0 on success, -1 on failure
912 * This function is used to parse EAP method list and select allowed methods
913 * for Phase2 authentication.
915 int eap_peer_select_phase2_methods(struct eap_peer_config *config,
916 const char *prefix,
917 struct eap_method_type **types,
918 size_t *num_types)
920 char *start, *pos, *buf;
921 struct eap_method_type *methods = NULL, *_methods;
922 u8 method;
923 size_t num_methods = 0, prefix_len;
925 if (config == NULL || config->phase2 == NULL)
926 goto get_defaults;
928 start = buf = os_strdup(config->phase2);
929 if (buf == NULL)
930 return -1;
932 prefix_len = os_strlen(prefix);
934 while (start && *start != '\0') {
935 int vendor;
936 pos = os_strstr(start, prefix);
937 if (pos == NULL)
938 break;
939 if (start != pos && *(pos - 1) != ' ') {
940 start = pos + prefix_len;
941 continue;
944 start = pos + prefix_len;
945 pos = os_strchr(start, ' ');
946 if (pos)
947 *pos++ = '\0';
948 method = eap_get_phase2_type(start, &vendor);
949 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
950 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
951 "method '%s'", start);
952 } else {
953 num_methods++;
954 _methods = os_realloc(methods,
955 num_methods * sizeof(*methods));
956 if (_methods == NULL) {
957 os_free(methods);
958 os_free(buf);
959 return -1;
961 methods = _methods;
962 methods[num_methods - 1].vendor = vendor;
963 methods[num_methods - 1].method = method;
966 start = pos;
969 os_free(buf);
971 get_defaults:
972 if (methods == NULL)
973 methods = eap_get_phase2_types(config, &num_methods);
975 if (methods == NULL) {
976 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
977 return -1;
979 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
980 (u8 *) methods,
981 num_methods * sizeof(struct eap_method_type));
983 *types = methods;
984 *num_types = num_methods;
986 return 0;
991 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
992 * @types: Buffer for returning allocated list of allowed EAP methods
993 * @num_types: Buffer for returning number of allocated EAP methods
994 * @hdr: EAP-Request header (and the following EAP type octet)
995 * @resp: Buffer for returning the EAP-Nak message
996 * Returns: 0 on success, -1 on failure
998 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
999 struct eap_hdr *hdr, struct wpabuf **resp)
1001 u8 *pos = (u8 *) (hdr + 1);
1002 size_t i;
1004 /* TODO: add support for expanded Nak */
1005 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
1006 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
1007 (u8 *) types, num_types * sizeof(struct eap_method_type));
1008 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
1009 EAP_CODE_RESPONSE, hdr->identifier);
1010 if (*resp == NULL)
1011 return -1;
1013 for (i = 0; i < num_types; i++) {
1014 if (types[i].vendor == EAP_VENDOR_IETF &&
1015 types[i].method < 256)
1016 wpabuf_put_u8(*resp, types[i].method);
1019 eap_update_len(*resp);
1021 return 0;