/** * A client-side 802.1x implementation supporting EAP/TLS * * This code is released under both the GPL version 2 and BSD licenses. * Either license may be used. The respective licenses are found below. * * Copyright (C) 2002 Bryan D. Payne & Nick L. Petroni Jr. * All Rights Reserved * * --- GPL Version 2 License --- * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * --- BSD License --- * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * Maryland at College Park and its contributors. * - Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /******************************************************************* * EAPTLS Function implementations * * File: eaptls.c * * Authors: bdpayne@cs.umd.edu, npetroni@cs.umd.edu * * $Id: eapttls.c,v 1.4 2003/04/14 00:22:06 chessing Exp $ * $Date: 2003/04/14 00:22:06 $ * $Log: eapttls.c,v $ * Revision 1.4 2003/04/14 00:22:06 chessing * Various small fixes, added new configuration options to turn off some things, and to allow static linking of OpenSSL * * Revision 1.3 2003/03/28 22:54:08 chessing * Patch to fix endianess issues between PPC and x86, with TTLS-PAP. * * Revision 1.2 2003/03/25 23:59:28 chessing * Added support for authentication method specific keyblock generation, and added dynamic WEP support for TTLS. * * Revision 1.1 2003/03/23 03:26:17 chessing * Inital support for TTLS. Currently only supports TTLS-PAP. Dynamic rekeying still needs to be implemented. * * Revision 1.10 2003/01/24 20:48:23 chessing * Cleaned up framing code, TLS no longer sends a malformed frame. * * Revision 1.9 2003/01/23 22:45:01 chessing * Added some global functions to handle packet fragments, and more TLS cleanups. * * Revision 1.8 2003/01/14 23:52:07 chessing * More work on the TLS code. It should be mostly stable now. There is a problem if get_pass("") in eaptls_auth_challenge is called twice. * * Revision 1.7 2003/01/14 19:12:50 chessing * TLS code now uses OpenSSL! Cleaned out some of the no longer needed stuff from the TLS code. Still needs more work on error checking. * * Revision 1.6 2003/01/10 22:09:08 galimorerpg * SSL_DEBUG define fix for gcc 3.2 * * Revision 1.5 2003/01/09 20:17:29 galimorerpg * Logging Updates * * Revision 1.4 2003/01/09 18:38:01 galimorerpg * Makefile fixes * * Revision 1.3 2003/01/06 22:31:08 chessing * Debugging code cleanups.... * * Revision 1.2 2003/01/03 22:25:36 chessing * Turned off debugging code, (use the configure options to turn on the debugging) and added -w option to get around some problems with the Intel iANS drivers, and some wired/wireless issues with the MacOS-X wireless code. * * Revision 1.1 2003/01/02 19:35:47 chessing * Add some files that were missed in the last import.. * * *******************************************************************/ #ifndef TTLS_DEBUG #define TTLS_DEBUG 0 /* turn debug info on deprecated- Use --with-tls-debug*/ #endif #include #include #include #include #include #include #include #include #include #include "eapttls.h" #include "ttlsphase2.h" #include "../tls/eapcrypt.h" #include "../../userconf.h" #include "logging.h" #include "auth_methods/auth_tools.h" #define TTLS_SESSION_KEY_CONST "ttls keying material" #define TTLS_SESSION_KEY_CONST_SIZE 20 /** GLOBAL VARS **/ char *eapttls_netid; char *eapttls_config; int phase =1; //char *root_cert = NULL; int ttls_root_cert_loaded = 0; int ttls_user_cert_loaded = 0; // The number of bytes that make up our certificate. uint32_t ttls_cert_size = 0; /** FUNCTION DEFINITIONS **/ /** * Initialization function for EAPTLS. This initializes the * data needed for the protocol and initializes variables needed to * start the eaptls handshake. * CHANGED by npetroni to only do those things which happen *once* * at TLS startup. Added function eaptls_reset() to prepare TLS * for a new handshake * * (IN) file name to the config file * (OUT) success = 0, failure = -1 */ int init_eapttls (char *config, char *netid) { eapttls_netid = netid; eapttls_config = config; #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Changed state to 0\n"); #endif return eapcrypt_tls_init(); } // Shutdown and cleanup anything we need to. int eapttls_shutdown() { // Don't free one_x_globals here. That will be handled by shutdown_eap. #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Cleaning up.\n"); #endif return eapcrypt_tls_shutdown(); } /** * Decodes a packet and creates the data needed for a new packet. * This function is called by the eap layer with new packets as * they are received. The input buffer points to the beginning * of the EAPTLS portion of the packet (the flags). The reply * packet should be placed in the output buffer. * * The memory for the output buffer is created here, but will be * freed by the eap layer when it is done using it. * * (IN) input and output packet buffers, with lengths * (OUT) success = 0, failure = -1 */ int eapttls_decode_packet (u_char *in, int in_size, u_char *out, int *out_size) { int rtnVal = 0; u_char *p = NULL; uint8_t *tptr = NULL; uint32_t len_long; int tcnt; int i; long temp_size; char *temp1, *temp2; int temp1_size, temp2_size; *out_size = 0; // since we are acting as the supplicant, we can safely assume // that the request bit is set on all packets that we receive // see what type of packet this is switch(in[0]){ case (EAPTLS_START): #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Recieved eap-ttls start packet\n"); #endif //TTLS_DEBUG /* prepare to start a new handshake */ eapcrypt_tls_reset(); //This will reset the TLS. #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "Changed state to INIT\n"); #endif rtnVal = eapcrypt_tls_parse_data(NULL, 0); if (rtnVal < 0) { xlogf(DEBUG_NORMAL, "(EAPTTLS) Parse data failed!\n"); return rtnVal; } rtnVal = eapcrypt_tls_return_data(out, out_size); phase = 1; // We are in phase 1. break; //--------------------------------------------------------------------------- case (EAPTLS_LENGTH_MORE): case (EAPTLS_MORE_FRAGS): case (EAPTLS_LENGTH_INCL): /* This is a fragment of data we care about. So, stash it away. If it is the final fragment, start returning data, instead of ACKs. */ p = in + 1; if ((in[0] == EAPTLS_LENGTH_MORE) || (in[0] == EAPTLS_LENGTH_INCL)) { memcpy(&len_long, &in[1], 4); temp_size = ntohl(len_long); ttls_cert_size = temp_size; p=&in[5]; in_size-=1; } else { in_size+=3; } /* sanity check on the given packet length */ if (in_size == 0) { xlogf(DEBUG_NORMAL, "(EAPTTLS) Packet has 0 length! (We should *NEVER* get here!\n"); rtnVal = eapcrypt_tls_return_data(out, out_size); return rtnVal; } if (save_data_fragment(p, in_size) != 0) { xlogf(DEBUG_NORMAL, "(EAPTTLS) Couldn't store packet fragment!\n"); return -1; } else { xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Saved packet fragment.\n"); } #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Saved packet fragment\n"); #endif if (in[0] != EAPTLS_LENGTH_INCL) { // we should also return an ack when we are done rtnVal = eapttls_build_ack(out, out_size); } else { rtnVal = eapcrypt_tls_parse_data((char *)get_data_fragment(), get_data_frag_size()); destroy_data_frags(); rtnVal = eapcrypt_tls_return_data(out, out_size); } if (eapcrypt_state() == 0x0003) { #if TTLS_DEBUG printf("Returning (Should be 0x00) : \n"); for (i=0;i<*out_size;i++) { printf("%02x ", out[i]); } printf("\n"); printf("Entering phase 2! (Non 0x00) -- Lower\n"); #endif // This implementation of phase 2 will only work with PAP! Other // phase 2 authentication methods will require phase 2 hooks in // other locations. phase = 2; temp1 = (char *)malloc(1024); temp2 = (char *)malloc(1024); ttls_do_phase2(temp1, &temp1_size); #if TTLS_PHASE2_DEBUG printf("Going to pass this in to encrypt : \n"); for (i=0;i 1) { p = in +1; if (save_data_fragment(p, in_size+3) != 0) { xlogf(DEBUG_NORMAL, "(TTLS) Couldn't save final data fragment!\n"); return -1; } else { xlogf(DEBUG_AUTHTYPES, "(TTLS) Saved final data fragment!\n"); } tptr = get_data_fragment(); tcnt = get_data_frag_size(); if (tcnt != ttls_cert_size) { xlogf(DEBUG_NORMAL, "(TTLS) Not enough data to form the certificate!!! %d != %d\n",ttls_cert_size,tcnt); printf("\n\n"); for (i=0;i<=tcnt;i++) { printf("%02x ", tptr[i]); } } rtnVal = eapcrypt_tls_parse_data((char *)get_data_fragment(), get_data_frag_size()); } rtnVal = eapcrypt_tls_return_data(out, out_size); destroy_data_frags(); break; //--------------------------------------------------------------------------- default: // we should never get here, return an error and do // absolutly nothing...this packet has funky flags #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Recieved packet with incorrect flags!\n"); #endif rtnVal = -1; break; } return rtnVal; } /** * Builds a simple "ACK" packet. These packets are just sent to * indicate the receipt of a fragmented packet and such. The * packet has only one byte whose value is 0x00. * * (IN) output buffer and length * (OUT) success = 0, failure = -1 */ int eapttls_build_ack (u_char *out, int *out_size) { *out_size = 1; out = 0x00; #if TTLS_DEBUG xlogf(DEBUG_AUTHTYPES, "(EAPTTLS) Sending an ack\n"); #endif return 0; } int eapttls_auth_challenge() { char *temp_username = NULL; char *root_cert = NULL; temp_username = get_username(); if (ttls_root_cert_loaded == 0) { // load CA cert. root_cert = get_root_cert(); //Get the filename/path for the root cert. if (eapcrypt_tls_load_root_certs(root_cert, NULL, NULL) < 0) { return -1; } if (root_cert != NULL) free(root_cert); ttls_root_cert_loaded = 1; // We now have the root cert loaded. } // We shouldn't need to load the user cert, unless there is one to load. /* if (ttls_user_cert_loaded == 0) { // First, get the password for the private key. temp_password = get_password(); //See if we have one in the file. if (temp_password == NULL) { xlogf(DEBUG_NORMAL, "(TTLS Authentication) %s's Password : ", temp_username); temp_password = getpass(""); //This is obsolete! Fix it! (It also causes problems with incorrect passwords.) } if (temp_password == NULL) // This should be impossible at this point! { if (temp_username != NULL) free(temp_username); return -1; } // Clean up after ourselves.. if (temp_username != NULL) free(temp_username); client_cert = get_client_cert(); client_key = get_key_file(); if (eapcrypt_tls_load_user_cert(client_cert, client_key, temp_password) < 0) { xlogf(DEBUG_NORMAL, "Couldn't load certificate! (May be an incorrect password!)\n"); if (client_cert) free(client_cert); if (client_key) free(client_key); return -1; } set_password(temp_password); ttls_user_cert_loaded = 1; return 0; } */ return 0; } int ttls_gen_keyblock() { u_char *gen_keyblock; gen_keyblock = eapcrypt_gen_keyblock(TTLS_SESSION_KEY_CONST, TTLS_SESSION_KEY_CONST_SIZE); eapcrypt_set_keyblock(gen_keyblock, EAPCRYPT_SESSION_KEY_SIZE); free(gen_keyblock); return 0; } /*** EOF ***/