freeradius-3.0 problem with EAP-TTLS

Till Dörges doerges at pre-sense.de
Wed Jul 15 10:43:29 CEST 2015


[Not sure where to report this. So please advise, if there's a more suitable place.]

Dear all,

When upgrading our running freeradius-2.2 configuration for EAP-TTLS to
freeradius-3.0 we followed the guide from http://freeradius.org/version3.html and
almost everything was running in our test environment except one point already
discussed similarly:

http://lists.freeradius.org/pipermail/freeradius-users/2015-March/076365.html


We had the same problem with our wpa_supplicant and Oddyssey clients:

The very last ACK from the client was not well recognized by freeradius-3 with the
same DEBUG message, also see (partly obfuscated radius.debug.txt):

Login incorrect (eap_ttls: Invalid ACK received: 0)



Encouraged from the thread follow-up
http://lists.freeradius.org/pipermail/freeradius-users/2015-May/077614.html we
checked the values returned to the method tls_ack_handler() in src/main/tls.c :

In fact we found for the last ACK:

ssn->info.content_type == 0 (instead of handshake == 22)

ssn->info.handshake_type == handshake_finished (expected 20)

ssn->dirty_out.used == 0


In order to make it work, we patched freeradius-3 like this (also see attachments
tls.c.patch and tls.c.original):

$ diff tls.c.patch tls.c.original

3130,3134d3129

<       /* patch needed for many versions of TTLS clients, compare
http://lists.freeradius.org/pipermail/freeradius-users/2015-May/077614.html */
<       if ((ssn->info.handshake_type == handshake_finished) && (ssn->dirty_out.used
== 0)) {
<               ssn->info.content_type = handshake;
<       }
<


HTH -- Till
-- 
Dipl.-Inform. Till Dörges                  doerges at pre-sense.de
                                  Tel. +49 - 40 - 244 2407 - 14
                                  Fax  +49 - 40 - 244 2407 - 24
PRESENSE Technologies GmbH            Sachsenstr. 5, D-20097 HH
Geschäftsführer/Managing Directors       AG Hamburg, HRB 107844
Till Dörges, Jürgen Sander               USt-IdNr.: DE263765024
-------------- next part --------------
Copyright (C) 1999-2015 The FreeRADIUS server project and contributors
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE
You may redistribute copies of FreeRADIUS under the terms of the
GNU General Public License
For more information about these matters, see the file named COPYRIGHT
Starting - reading configuration files ...
including dictionary file /etc/raddb/dictionary
including dictionary file /etc/raddb/dictionary
including configuration file /etc/raddb/radiusd.conf
including configuration file /etc/raddb/proxy.conf
including configuration file /etc/raddb/clients.conf
including files in directory /etc/raddb/mods-enabled/
including configuration file /etc/raddb/mods-enabled/utf8
including configuration file /etc/raddb/mods-enabled/passwd
including configuration file /etc/raddb/mods-enabled/soh
including configuration file /etc/raddb/mods-enabled/ntlm_auth
including configuration file /etc/raddb/mods-enabled/unix
including configuration file /etc/raddb/mods-enabled/always
including configuration file /etc/raddb/mods-enabled/realm
including configuration file /etc/raddb/mods-enabled/dynamic_clients
including configuration file /etc/raddb/mods-enabled/attr_filter
including configuration file /etc/raddb/mods-enabled/expiration
including configuration file /etc/raddb/mods-enabled/detail
including configuration file /etc/raddb/mods-enabled/echo
including configuration file /etc/raddb/mods-enabled/replicate
including configuration file /etc/raddb/mods-enabled/radutmp
including configuration file /etc/raddb/mods-enabled/expr
including configuration file /etc/raddb/mods-enabled/dhcp
including configuration file /etc/raddb/mods-enabled/exec
including configuration file /etc/raddb/mods-enabled/unpack
including configuration file /etc/raddb/mods-enabled/preprocess
including configuration file /etc/raddb/mods-enabled/pap
including configuration file /etc/raddb/mods-enabled/files
including configuration file /etc/raddb/mods-enabled/mschap
including configuration file /etc/raddb/mods-enabled/chap
including configuration file /etc/raddb/mods-enabled/digest
including configuration file /etc/raddb/mods-enabled/logintime
including configuration file /etc/raddb/mods-enabled/cache_eap
including configuration file /etc/raddb/mods-enabled/detail.log
including configuration file /etc/raddb/mods-enabled/sradutmp
including configuration file /etc/raddb/mods-enabled/linelog
including configuration file /etc/raddb/mods-enabled/eap
including files in directory /etc/raddb/policy.d/
including configuration file /etc/raddb/policy.d/abfab-tr
including configuration file /etc/raddb/policy.d/debug
including configuration file /etc/raddb/policy.d/filter
including configuration file /etc/raddb/policy.d/operator-name
including configuration file /etc/raddb/policy.d/canonicalization
including configuration file /etc/raddb/policy.d/control
including configuration file /etc/raddb/policy.d/dhcp
including configuration file /etc/raddb/policy.d/cui
including configuration file /etc/raddb/policy.d/eap
including configuration file /etc/raddb/policy.d/accounting
including files in directory /etc/raddb/sites-enabled/
including configuration file /etc/raddb/sites-enabled/inner-tunnel
including configuration file /etc/raddb/sites-enabled/default
main {
	name = "radiusd"
	prefix = "/usr"
	localstatedir = "/var"
	sbindir = "/usr/sbin"
	logdir = "/var/log/"
	run_dir = "/var/run/radiusd"
	libdir = "/usr/lib"
	radacctdir = "/var/log//radacct"
	hostname_lookups = no
	max_request_time = 30
	cleanup_delay = 5
	max_requests = 1024
	pidfile = "/var/run/radiusd/radiusd.pid"
	checkrad = "/usr/sbin/checkrad"
	debug_level = 0
	proxy_requests = no
 log {
 	stripped_names = no
 	auth = yes
 	auth_badpass = yes
 	auth_goodpass = yes
 	colourise = yes
 	msg_denied = "You are already logged in - access denied"
 }
 resources {
 }
 security {
 	max_attributes = 200
 	reject_delay = 1.000000
 	status_server = yes
 }
}
radiusd: #### Loading Realms and Home Servers ####
 proxy server {
 	retry_delay = 5
 	retry_count = 3
 	default_fallback = no
 	dead_time = 120
 	wake_all_if_all_dead = no
 }
 home_server localhost {
 	ipaddr = 127.0.0.1
 	port = 1812
 	type = "auth"
 	secret = <<< secret >>>
 	response_window = 20.000000
 	response_timeouts = 1
 	max_outstanding = 65536
 	zombie_period = 40
 	status_check = "status-server"
 	ping_interval = 30
 	check_interval = 30
 	check_timeout = 4
 	num_answers_to_alive = 3
 	revive_interval = 120
  limit {
  	max_connections = 16
  	max_requests = 0
  	lifetime = 0
  	idle_timeout = 0
  }
  coa {
  	irt = 2
  	mrt = 16
  	mrc = 5
  	mrd = 30
  }
 }
 home_server_pool my_auth_failover {
	type = fail-over
	home_server = localhost
 }
 realm example.com {
	auth_pool = my_auth_failover
 }
 realm LOCAL {
 }
radiusd: #### Loading Clients ####
 client localhost {
 	ipaddr = 127.0.0.1
 	require_message_authenticator = no
 	secret = <<< secret >>>
 	nas_type = "other"
 	proto = "*"
  limit {
  	max_connections = 16
  	lifetime = 0
  	idle_timeout = 30
  }
 }
 client localhost_ipv6 {
 	ipv6addr = ::1
 	require_message_authenticator = no
 	secret = <<< secret >>>
  limit {
  	max_connections = 16
  	lifetime = 0
  	idle_timeout = 30
  }
 }
 client xxx.xxx.xxx.0/24 {
 	ipaddr = xxx.xxx.xxx.2
 	require_message_authenticator = no
 	secret = <<< secret >>>
 	shortname = "CAMPUS-MANAGEMENT"
  limit {
  	max_connections = 16
  	lifetime = 0
  	idle_timeout = 30
  }
 }
Debug state unknown (cap_sys_ptrace capability not set)
radiusd: #### Instantiating modules ####
 instantiate {
 }
 modules {
  # Loaded module rlm_utf8
  # Instantiating module "utf8" from file /etc/raddb/mods-enabled/utf8
  # Loaded module rlm_passwd
  # Instantiating module "etc_passwd" from file /etc/raddb/mods-enabled/passwd
  passwd etc_passwd {
  	filename = "/etc/passwd"
  	format = "*User-Name:Crypt-Password:"
  	delimiter = ":"
  	ignore_nislike = no
  	ignore_empty = yes
  	allow_multiple_keys = no
  	hash_size = 100
  }
rlm_passwd: nfields: 3 keyfield 0(User-Name) listable: no
  # Loaded module rlm_soh
  # Instantiating module "soh" from file /etc/raddb/mods-enabled/soh
  soh {
  	dhcp = yes
  }
  # Loaded module rlm_exec
  # Instantiating module "ntlm_auth" from file /etc/raddb/mods-enabled/ntlm_auth
  exec ntlm_auth {
  	wait = yes
  	program = "/path/to/ntlm_auth --request-nt-key --domain=MYDOMAIN --username=%{mschap:User-Name} --password=%{User-Password}"
  	shell_escape = yes
  }
  # Loaded module rlm_unix
  # Instantiating module "unix" from file /etc/raddb/mods-enabled/unix
  unix {
  	radwtmp = "/var/log//radwtmp"
  }
  # Loaded module rlm_always
  # Instantiating module "reject" from file /etc/raddb/mods-enabled/always
  always reject {
  	rcode = "reject"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "fail" from file /etc/raddb/mods-enabled/always
  always fail {
  	rcode = "fail"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "ok" from file /etc/raddb/mods-enabled/always
  always ok {
  	rcode = "ok"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "handled" from file /etc/raddb/mods-enabled/always
  always handled {
  	rcode = "handled"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "invalid" from file /etc/raddb/mods-enabled/always
  always invalid {
  	rcode = "invalid"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "userlock" from file /etc/raddb/mods-enabled/always
  always userlock {
  	rcode = "userlock"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "notfound" from file /etc/raddb/mods-enabled/always
  always notfound {
  	rcode = "notfound"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "noop" from file /etc/raddb/mods-enabled/always
  always noop {
  	rcode = "noop"
  	simulcount = 0
  	mpp = no
  }
  # Instantiating module "updated" from file /etc/raddb/mods-enabled/always
  always updated {
  	rcode = "updated"
  	simulcount = 0
  	mpp = no
  }
  # Loaded module rlm_realm
  # Instantiating module "IPASS" from file /etc/raddb/mods-enabled/realm
  realm IPASS {
  	format = "prefix"
  	delimiter = "/"
  	ignore_default = no
  	ignore_null = no
  }
  # Instantiating module "suffix" from file /etc/raddb/mods-enabled/realm
  realm suffix {
  	format = "suffix"
  	delimiter = "@"
  	ignore_default = no
  	ignore_null = no
  }
  # Instantiating module "realmpercent" from file /etc/raddb/mods-enabled/realm
  realm realmpercent {
  	format = "suffix"
  	delimiter = "%"
  	ignore_default = no
  	ignore_null = no
  }
  # Instantiating module "ntdomain" from file /etc/raddb/mods-enabled/realm
  realm ntdomain {
  	format = "prefix"
  	delimiter = "\\"
  	ignore_default = no
  	ignore_null = no
  }
  # Loaded module rlm_dynamic_clients
  # Instantiating module "dynamic_clients" from file /etc/raddb/mods-enabled/dynamic_clients
  # Loaded module rlm_attr_filter
  # Instantiating module "attr_filter.post-proxy" from file /etc/raddb/mods-enabled/attr_filter
  attr_filter attr_filter.post-proxy {
  	filename = "/etc/raddb/mods-config/attr_filter/post-proxy"
  	key = "%{Realm}"
  	relaxed = no
  }
reading pairlist file /etc/raddb/mods-config/attr_filter/post-proxy
  # Instantiating module "attr_filter.pre-proxy" from file /etc/raddb/mods-enabled/attr_filter
  attr_filter attr_filter.pre-proxy {
  	filename = "/etc/raddb/mods-config/attr_filter/pre-proxy"
  	key = "%{Realm}"
  	relaxed = no
  }
reading pairlist file /etc/raddb/mods-config/attr_filter/pre-proxy
  # Instantiating module "attr_filter.access_reject" from file /etc/raddb/mods-enabled/attr_filter
  attr_filter attr_filter.access_reject {
  	filename = "/etc/raddb/mods-config/attr_filter/access_reject"
  	key = "%{User-Name}"
  	relaxed = no
  }
reading pairlist file /etc/raddb/mods-config/attr_filter/access_reject
  # Instantiating module "attr_filter.access_challenge" from file /etc/raddb/mods-enabled/attr_filter
  attr_filter attr_filter.access_challenge {
  	filename = "/etc/raddb/mods-config/attr_filter/access_challenge"
  	key = "%{User-Name}"
  	relaxed = no
  }
reading pairlist file /etc/raddb/mods-config/attr_filter/access_challenge
  # Instantiating module "attr_filter.accounting_response" from file /etc/raddb/mods-enabled/attr_filter
  attr_filter attr_filter.accounting_response {
  	filename = "/etc/raddb/mods-config/attr_filter/accounting_response"
  	key = "%{User-Name}"
  	relaxed = no
  }
reading pairlist file /etc/raddb/mods-config/attr_filter/accounting_response
  # Loaded module rlm_expiration
  # Instantiating module "expiration" from file /etc/raddb/mods-enabled/expiration
  # Loaded module rlm_detail
  # Instantiating module "detail" from file /etc/raddb/mods-enabled/detail
  detail {
  	filename = "/var/log//radacct/%{%{Packet-Src-IP-Address}:-%{Packet-Src-IPv6-Address}}/detail-%Y%m%d"
  	header = "%t"
  	permissions = 384
  	locking = no
  	escape_filenames = no
  	log_packet_header = no
  }
  # Instantiating module "echo" from file /etc/raddb/mods-enabled/echo
  exec echo {
  	wait = yes
  	program = "/bin/echo %{User-Name}"
  	input_pairs = "request"
  	output_pairs = "reply"
  	shell_escape = yes
  }
  # Loaded module rlm_replicate
  # Instantiating module "replicate" from file /etc/raddb/mods-enabled/replicate
  # Loaded module rlm_radutmp
  # Instantiating module "radutmp" from file /etc/raddb/mods-enabled/radutmp
  radutmp {
  	filename = "/var/log//radutmp"
  	username = "%{User-Name}"
  	case_sensitive = yes
  	check_with_nas = yes
  	permissions = 384
  	caller_id = yes
  }
  # Loaded module rlm_expr
  # Instantiating module "expr" from file /etc/raddb/mods-enabled/expr
  expr {
  	safe_characters = "@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_: /äéöüàâæçèéêëîïôœùûüaÿÄÉÖÜßÀÂÆÇÈÉÊËÎÏÔŒÙÛÜŸ"
  }
  # Loaded module rlm_dhcp
  # Instantiating module "dhcp" from file /etc/raddb/mods-enabled/dhcp
  # Instantiating module "exec" from file /etc/raddb/mods-enabled/exec
  exec {
  	wait = no
  	input_pairs = "request"
  	shell_escape = yes
  	timeout = 10
  }
  # Loaded module rlm_unpack
  # Instantiating module "unpack" from file /etc/raddb/mods-enabled/unpack
  # Loaded module rlm_preprocess
  # Instantiating module "preprocess" from file /etc/raddb/mods-enabled/preprocess
  preprocess {
  	huntgroups = "/etc/raddb/mods-config/preprocess/huntgroups"
  	hints = "/etc/raddb/mods-config/preprocess/hints"
  	with_ascend_hack = no
  	ascend_channels_per_line = 23
  	with_ntdomain_hack = no
  	with_specialix_jetstream_hack = no
  	with_cisco_vsa_hack = no
  	with_alvarion_vsa_hack = no
  }
reading pairlist file /etc/raddb/mods-config/preprocess/huntgroups
reading pairlist file /etc/raddb/mods-config/preprocess/hints
  # Loaded module rlm_pap
  # Instantiating module "pap" from file /etc/raddb/mods-enabled/pap
  pap {
  	normalise = yes
  }
  # Loaded module rlm_files
  # Instantiating module "files" from file /etc/raddb/mods-enabled/files
  files {
  	filename = "/etc/raddb/mods-config/files/authorize"
  	acctusersfile = "/etc/raddb/mods-config/files/accounting"
  	preproxy_usersfile = "/etc/raddb/mods-config/files/pre-proxy"
  	compat = "cistron"
  }
reading pairlist file /etc/raddb/mods-config/files/authorize
[/etc/raddb/mods-config/files/authorize]:68 Cistron compatibility checks for entry cwlu ...
[/etc/raddb/mods-config/files/authorize]:71 Cistron compatibility checks for entry DEFAULT ...
[/etc/raddb/mods-config/files/authorize]:72 Cistron compatibility checks for entry student ...
[/etc/raddb/mods-config/files/authorize]:189 Cistron compatibility checks for entry DEFAULT ...
[/etc/raddb/mods-config/files/authorize]:196 Cistron compatibility checks for entry DEFAULT ...
[/etc/raddb/mods-config/files/authorize]:202 Cistron compatibility checks for entry DEFAULT ...
reading pairlist file /etc/raddb/mods-config/files/accounting
reading pairlist file /etc/raddb/mods-config/files/pre-proxy
  # Loaded module rlm_mschap
  # Instantiating module "mschap" from file /etc/raddb/mods-enabled/mschap
  mschap {
  	use_mppe = yes
  	require_encryption = no
  	require_strong = no
  	with_ntdomain_hack = yes
   passchange {
   }
  	allow_retry = yes
  }
rlm_mschap (mschap): using internal authentication
  # Loaded module rlm_chap
  # Instantiating module "chap" from file /etc/raddb/mods-enabled/chap
  # Loaded module rlm_digest
  # Instantiating module "digest" from file /etc/raddb/mods-enabled/digest
  # Loaded module rlm_logintime
  # Instantiating module "logintime" from file /etc/raddb/mods-enabled/logintime
  logintime {
  	minimum_timeout = 60
  }
  # Loaded module rlm_cache
  # Instantiating module "cache_eap" from file /etc/raddb/mods-enabled/cache_eap
  cache cache_eap {
  	driver = "rlm_cache_rbtree"
  	key = "%{%{control:State}:-%{%{reply:State}:-%{State}}}"
  	ttl = 15
  	max_entries = 0
  	epoch = 0
  	add_stats = no
  }
rlm_cache (cache_eap): Driver rlm_cache_rbtree (module rlm_cache_rbtree) loaded and linked
  # Instantiating module "auth_log" from file /etc/raddb/mods-enabled/detail.log
  detail auth_log {
  	filename = "/var/log//radacct/%{%{Packet-Src-IP-Address}:-%{Packet-Src-IPv6-Address}}/auth-detail-%Y%m%d"
  	header = "%t"
  	permissions = 384
  	locking = no
  	escape_filenames = no
  	log_packet_header = no
  }
rlm_detail (auth_log): 'User-Password' suppressed, will not appear in detail output
  # Instantiating module "reply_log" from file /etc/raddb/mods-enabled/detail.log
  detail reply_log {
  	filename = "/var/log//radacct/%{%{Packet-Src-IP-Address}:-%{Packet-Src-IPv6-Address}}/reply-detail-%Y%m%d"
  	header = "%t"
  	permissions = 384
  	locking = no
  	escape_filenames = no
  	log_packet_header = no
  }
  # Instantiating module "pre_proxy_log" from file /etc/raddb/mods-enabled/detail.log
  detail pre_proxy_log {
  	filename = "/var/log//radacct/%{%{Packet-Src-IP-Address}:-%{Packet-Src-IPv6-Address}}/pre-proxy-detail-%Y%m%d"
  	header = "%t"
  	permissions = 384
  	locking = no
  	escape_filenames = no
  	log_packet_header = no
  }
  # Instantiating module "post_proxy_log" from file /etc/raddb/mods-enabled/detail.log
  detail post_proxy_log {
  	filename = "/var/log//radacct/%{%{Packet-Src-IP-Address}:-%{Packet-Src-IPv6-Address}}/post-proxy-detail-%Y%m%d"
  	header = "%t"
  	permissions = 384
  	locking = no
  	escape_filenames = no
  	log_packet_header = no
  }
  # Instantiating module "sradutmp" from file /etc/raddb/mods-enabled/sradutmp
  radutmp sradutmp {
  	filename = "/var/log//sradutmp"
  	username = "%{User-Name}"
  	case_sensitive = yes
  	check_with_nas = yes
  	permissions = 420
  	caller_id = no
  }
  # Loaded module rlm_linelog
  # Instantiating module "linelog" from file /etc/raddb/mods-enabled/linelog
  linelog {
  	filename = "/var/log//linelog"
  	escape_filenames = no
  	syslog_severity = "info"
  	permissions = 384
  	format = "This is a log message for %{User-Name}"
  	reference = "messages.%{%{Packet-Type}:-default}"
  }
  # Instantiating module "log_accounting" from file /etc/raddb/mods-enabled/linelog
  linelog log_accounting {
  	filename = "/var/log//linelog-accounting"
  	escape_filenames = no
  	syslog_severity = "info"
  	permissions = 384
  	format = ""
  	reference = "Accounting-Request.%{%{Acct-Status-Type}:-unknown}"
  }
  # Loaded module rlm_eap
  # Instantiating module "eap" from file /etc/raddb/mods-enabled/eap
  eap {
  	default_eap_type = "ttls"
  	timer_expire = 60
  	ignore_unknown_eap_types = no
  	mod_accounting_username_bug = no
  	max_sessions = 1024
  }
   # Linked to sub-module rlm_eap_tls
   tls {
   	tls = "tls-common"
   }
   tls-config tls-common {
   	rsa_key_exchange = no
   	dh_key_exchange = yes
   	rsa_key_length = 512
   	dh_key_length = 512
   	verify_depth = 0
   	pem_file_type = yes
   	private_key_file = "/etc/raddb/certs/AuthCert.pem"
   	certificate_file = "/etc/raddb/certs/AuthCert.pem"
   	ca_file = "/etc/raddb/certs/cacrl/cacrl.pem"
   	private_key_password = <<< secret >>>
   	dh_file = "/etc/raddb/certs/dh"
   	random_file = "/etc/raddb/certs/random"
   	fragment_size = 1024
   	include_length = yes
   	check_crl = no
   	check_all_crl = no
   	cipher_list = "DEFAULT"
   	ecdh_curve = "prime256v1"
    cache {
    	enable = yes
    	lifetime = 24
    	max_entries = 255
    }
    verify {
    	tmpdir = "/etc/raddb/certs/tmp"
    	client = "/check_client_cert.sh /etc/raddb/certs/tmp  %{TLS-Client-Cert-Filename} /etc/raddb/certs/AuthCert.pem"
    }
    ocsp {
    	enable = no
    	override_cert_url = yes
    	url = "http://127.0.0.1/ocsp/"
    	use_nonce = yes
    	timeout = 0
    	softfail = no
    }
   }
   # Linked to sub-module rlm_eap_ttls
   ttls {
   	tls = "tls-common"
   	default_eap_type = "md5"
   	copy_request_to_tunnel = no
   	use_tunneled_reply = no
   	virtual_server = "inner-tunnel"
   	include_length = yes
   	require_client_cert = no
   }
Using cached TLS configuration from previous invocation
 } # modules
radiusd: #### Loading Virtual Servers ####
server { # from file /etc/raddb/radiusd.conf
} # server
server inner-tunnel { # from file /etc/raddb/sites-enabled/inner-tunnel
 # Loading authenticate {...}
 # Loading authorize {...}
 # Loading session {...}
 # Loading post-auth {...}
} # server inner-tunnel
server default { # from file /etc/raddb/sites-enabled/default
 # Loading authenticate {...}
 # Loading authorize {...}
 # Loading preacct {...}
 # Loading accounting {...}
 # Loading session {...}
 # Loading post-auth {...}
} # server default
radiusd: #### Opening IP addresses and Ports ####
listen {
  	type = "auth"
  	ipaddr = *
  	port = 0
   limit {
   	max_connections = 16
   	lifetime = 0
   	idle_timeout = 30
   }
}
listen {
  	type = "acct"
  	ipaddr = *
  	port = 0
   limit {
   	max_connections = 16
   	lifetime = 0
   	idle_timeout = 30
   }
}
Listening on auth address * port 1812 bound to server default
Listening on acct address * port 1813 bound to server default
Ready to process requests
(0) Received Access-Request Id 125 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 263
(0)   User-Name = 'student'
(0)   Framed-MTU = 1400
(0)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(0)   Calling-Station-Id = '00-25-9C-94-85-84'
(0)   Cisco-AVPair = 'ssid=CAMPUS'
(0)   Service-Type = Login-User
(0)   Cisco-AVPair = 'service-type=Login'
(0)   Message-Authenticator = 0xbb1898762731522dc4e039459f3fae45
(0)   EAP-Message = 0x0201000c016169726c696e65
(0)   NAS-Port-Type = Wireless-802.11
(0)   NAS-Port = 436
(0)   NAS-Port-Id = '436'
(0)   NAS-IP-Address = xxx.xxx.xxx.2
(0)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(0) # Executing section authorize from file /etc/raddb/sites-enabled/default
(0)   authorize {
(0)     policy filter_username {
(0)       if (!&User-Name) {
(0)       if (!&User-Name)  -> FALSE
(0)       if (&User-Name =~ / /) {
(0)       if (&User-Name =~ / /)  -> FALSE
(0)       if (&User-Name =~ /@.*@/ ) {
(0)       if (&User-Name =~ /@.*@/ )  -> FALSE
(0)       if (&User-Name =~ /\.\./ ) {
(0)       if (&User-Name =~ /\.\./ )  -> FALSE
(0)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(0)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(0)       if (&User-Name =~ /\.$/)  {
(0)       if (&User-Name =~ /\.$/)   -> FALSE
(0)       if (&User-Name =~ /@\./)  {
(0)       if (&User-Name =~ /@\./)   -> FALSE
(0)     } # policy filter_username = notfound
(0)     [preprocess] = ok
(0)     [chap] = noop
(0)     [mschap] = noop
(0) suffix: Checking for suffix after "@"
(0) suffix: No '@' in User-Name = "student", looking up realm NULL
(0) suffix: No such realm "NULL"
(0)     [suffix] = noop
(0)     update control {
(0)       EAP-TLS-Require-Client-Cert := Yes
(0)     } # update control = noop
(0) eap: Peer sent code Response (2) ID 1 length 12
(0) eap: EAP-Identity reply, returning 'ok' so we can short-circuit the rest of authorize
(0)     [eap] = ok
(0)   } # authorize = ok
(0) Found Auth-Type = EAP
(0) # Executing group from file /etc/raddb/sites-enabled/default
(0)   authenticate {
(0) eap: Peer sent method Identity (1)
(0) eap: Calling eap_ttls to process EAP data
(0) eap_ttls: Flushing SSL sessions (of #0)
(0) eap_ttls: Requiring client certificate
(0) eap_ttls: Initiate
(0) eap_ttls: Requiring client certificate
(0) eap_ttls: Start returned 1
(0) eap: EAP session adding &reply:State = 0x9bcdbfd59bcfaaec
(0)     [eap] = handled
(0)   } # authenticate = handled
(0) Using Post-Auth-Type Challenge
(0) Post-Auth-Type sub-section not found.  Ignoring.
(0) # Executing group from file /etc/raddb/sites-enabled/default
(0) Sent Access-Challenge Id 125 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(0)   EAP-Message = 0x010200061520
(0)   Message-Authenticator = 0x00000000000000000000000000000000
(0)   State = 0x9bcdbfd59bcfaaec19ea252da52603d3
(0) Finished request
Waking up in 4.9 seconds.
(1) Received Access-Request Id 126 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 351
(1)   User-Name = 'student'
(1)   Framed-MTU = 1400
(1)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(1)   Calling-Station-Id = '00-25-9C-94-85-84'
(1)   Cisco-AVPair = 'ssid=CAMPUS'
(1)   Service-Type = Login-User
(1)   Cisco-AVPair = 'service-type=Login'
(1)   Message-Authenticator = 0x1e7d6b8992e939b8afb5559f2bd93c49
(1)   EAP-Message = 0x0202005215800000004816030100430100003f030155a38da0a8295960790984953dd5d6fda60f005413166d2da99c8ea0aa6bb2b700001800390038003300320016001300660035002f000a000500040100
(1)   NAS-Port-Type = Wireless-802.11
(1)   NAS-Port = 436
(1)   NAS-Port-Id = '436'
(1)   State = 0x9bcdbfd59bcfaaec19ea252da52603d3
(1)   NAS-IP-Address = xxx.xxx.xxx.2
(1)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(1) session-state: No cached attributes
(1) # Executing section authorize from file /etc/raddb/sites-enabled/default
(1)   authorize {
(1)     policy filter_username {
(1)       if (!&User-Name) {
(1)       if (!&User-Name)  -> FALSE
(1)       if (&User-Name =~ / /) {
(1)       if (&User-Name =~ / /)  -> FALSE
(1)       if (&User-Name =~ /@.*@/ ) {
(1)       if (&User-Name =~ /@.*@/ )  -> FALSE
(1)       if (&User-Name =~ /\.\./ ) {
(1)       if (&User-Name =~ /\.\./ )  -> FALSE
(1)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(1)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(1)       if (&User-Name =~ /\.$/)  {
(1)       if (&User-Name =~ /\.$/)   -> FALSE
(1)       if (&User-Name =~ /@\./)  {
(1)       if (&User-Name =~ /@\./)   -> FALSE
(1)     } # policy filter_username = notfound
(1)     [preprocess] = ok
(1)     [chap] = noop
(1)     [mschap] = noop
(1) suffix: Checking for suffix after "@"
(1) suffix: No '@' in User-Name = "student", looking up realm NULL
(1) suffix: No such realm "NULL"
(1)     [suffix] = noop
(1)     update control {
(1)       EAP-TLS-Require-Client-Cert := Yes
(1)     } # update control = noop
(1) eap: Peer sent code Response (2) ID 2 length 82
(1) eap: Continuing tunnel setup
(1)     [eap] = ok
(1)   } # authorize = ok
(1) Found Auth-Type = EAP
(1) # Executing group from file /etc/raddb/sites-enabled/default
(1)   authenticate {
(1) eap: Expiring EAP session with state 0x9bcdbfd59bcfaaec
(1) eap: Finished EAP session with state 0x9bcdbfd59bcfaaec
(1) eap: Previous EAP request found for state 0x9bcdbfd59bcfaaec, released from the list
(1) eap: Peer sent method TTLS (21)
(1) eap: EAP TTLS (21)
(1) eap: Calling eap_ttls to process EAP data
(1) eap_ttls: Authenticate
(1) eap_ttls: processing EAP-TLS
(1) eap_ttls: TLS Length 72
(1) eap_ttls: Length Included
(1) eap_ttls: eaptls_verify returned 11 
(1) eap_ttls: (other): before/accept initialization
(1) eap_ttls: TLS_accept: before/accept initialization
(1) eap_ttls: <<< Unknown TLS version [length 0005] 
(1) eap_ttls: <<< TLS 1.0 Handshake [length 0043], ClientHello 
(1) eap_ttls: TLS_accept: SSLv3 read client hello A
(1) eap_ttls: >>> Unknown TLS version [length 0005] 
(1) eap_ttls: >>> TLS 1.0 Handshake [length 004a], ServerHello 
(1) eap_ttls: TLS_accept: SSLv3 write server hello A
(1) eap_ttls: >>> Unknown TLS version [length 0005] 
(1) eap_ttls: >>> TLS 1.0 Handshake [length 13c6], Certificate 
(1) eap_ttls: TLS_accept: SSLv3 write certificate A
(1) eap_ttls: >>> Unknown TLS version [length 0005] 
(1) eap_ttls: >>> TLS 1.0 Handshake [length 020d], ServerKeyExchange 
(1) eap_ttls: TLS_accept: SSLv3 write key exchange A
(1) eap_ttls: >>> Unknown TLS version [length 0005] 
(1) eap_ttls: >>> TLS 1.0 Handshake [length 010b], CertificateRequest 
(1) eap_ttls: TLS_accept: SSLv3 write certificate request A
(1) eap_ttls: TLS_accept: SSLv3 flush data
(1) eap_ttls: TLS_accept: Need to read more data: SSLv3 read client certificate A
(1) eap_ttls: TLS_accept: Need to read more data: SSLv3 read client certificate A
In SSL Handshake Phase 
In SSL Accept mode  
(1) eap_ttls: eaptls_process returned 13 
(1) eap: EAP session adding &reply:State = 0x9bcdbfd59aceaaec
(1)     [eap] = handled
(1)   } # authenticate = handled
(1) Using Post-Auth-Type Challenge
(1) Post-Auth-Type sub-section not found.  Ignoring.
(1) # Executing group from file /etc/raddb/sites-enabled/default
(1) Sent Access-Challenge Id 126 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(1)   EAP-Message = 0x010303f615c00000173c160301004a0200004603011fa4e35bb881349722b8f6061450071533b4af7a88311fea6eff087cd823e58c2093c65df3253f3fa2c2fac8c19ccbb47e845394db8f7659602f0f704dc8cec08900390016030113c60b0013c20013bf00068b308206873082056fa0030201020212
(1)   Message-Authenticator = 0x00000000000000000000000000000000
(1)   State = 0x9bcdbfd59aceaaec19ea252da52603d3
(1) Finished request
Waking up in 4.9 seconds.
(2) Received Access-Request Id 127 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(2)   User-Name = 'student'
(2)   Framed-MTU = 1400
(2)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(2)   Calling-Station-Id = '00-25-9C-94-85-84'
(2)   Cisco-AVPair = 'ssid=CAMPUS'
(2)   Service-Type = Login-User
(2)   Cisco-AVPair = 'service-type=Login'
(2)   Message-Authenticator = 0x8543fb6b668999949e221e74cf6abe49
(2)   EAP-Message = 0x020300061500
(2)   NAS-Port-Type = Wireless-802.11
(2)   NAS-Port = 436
(2)   NAS-Port-Id = '436'
(2)   State = 0x9bcdbfd59aceaaec19ea252da52603d3
(2)   NAS-IP-Address = xxx.xxx.xxx.2
(2)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(2) session-state: No cached attributes
(2) # Executing section authorize from file /etc/raddb/sites-enabled/default
(2)   authorize {
(2)     policy filter_username {
(2)       if (!&User-Name) {
(2)       if (!&User-Name)  -> FALSE
(2)       if (&User-Name =~ / /) {
(2)       if (&User-Name =~ / /)  -> FALSE
(2)       if (&User-Name =~ /@.*@/ ) {
(2)       if (&User-Name =~ /@.*@/ )  -> FALSE
(2)       if (&User-Name =~ /\.\./ ) {
(2)       if (&User-Name =~ /\.\./ )  -> FALSE
(2)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(2)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(2)       if (&User-Name =~ /\.$/)  {
(2)       if (&User-Name =~ /\.$/)   -> FALSE
(2)       if (&User-Name =~ /@\./)  {
(2)       if (&User-Name =~ /@\./)   -> FALSE
(2)     } # policy filter_username = notfound
(2)     [preprocess] = ok
(2)     [chap] = noop
(2)     [mschap] = noop
(2) suffix: Checking for suffix after "@"
(2) suffix: No '@' in User-Name = "student", looking up realm NULL
(2) suffix: No such realm "NULL"
(2)     [suffix] = noop
(2)     update control {
(2)       EAP-TLS-Require-Client-Cert := Yes
(2)     } # update control = noop
(2) eap: Peer sent code Response (2) ID 3 length 6
(2) eap: Continuing tunnel setup
(2)     [eap] = ok
(2)   } # authorize = ok
(2) Found Auth-Type = EAP
(2) # Executing group from file /etc/raddb/sites-enabled/default
(2)   authenticate {
(2) eap: Expiring EAP session with state 0x9bcdbfd59aceaaec
(2) eap: Finished EAP session with state 0x9bcdbfd59aceaaec
(2) eap: Previous EAP request found for state 0x9bcdbfd59aceaaec, released from the list
(2) eap: Peer sent method TTLS (21)
(2) eap: EAP TTLS (21)
(2) eap: Calling eap_ttls to process EAP data
(2) eap_ttls: Authenticate
(2) eap_ttls: processing EAP-TLS
(2) eap_ttls: Received TLS ACK
(2) eap_ttls: Received TLS ACK
(2) eap_ttls: ACK handshake fragment handler
(2) eap_ttls: eaptls_verify returned 1 
(2) eap_ttls: eaptls_process returned 13 
(2) eap: EAP session adding &reply:State = 0x9bcdbfd599c9aaec
(2)     [eap] = handled
(2)   } # authenticate = handled
(2) Using Post-Auth-Type Challenge
(2) Post-Auth-Type sub-section not found.  Ignoring.
(2) # Executing group from file /etc/raddb/sites-enabled/default
(2) Sent Access-Challenge Id 127 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(2)   EAP-Message = 0x010403f615c00000173c617279300e0603551d0f0101ff0404030205a0301d0603551d250416301406082b0601050507030206082b06010505070301302a0603551d1104233021880f2b06010401eb10864a01ff30112e0fa00e06052b1b070106a00504033cbf643081f806082b060105050701010481
(2)   Message-Authenticator = 0x00000000000000000000000000000000
(2)   State = 0x9bcdbfd599c9aaec19ea252da52603d3
(2) Finished request
Waking up in 4.9 seconds.
(3) Received Access-Request Id 128 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(3)   User-Name = 'student'
(3)   Framed-MTU = 1400
(3)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(3)   Calling-Station-Id = '00-25-9C-94-85-84'
(3)   Cisco-AVPair = 'ssid=CAMPUS'
(3)   Service-Type = Login-User
(3)   Cisco-AVPair = 'service-type=Login'
(3)   Message-Authenticator = 0x909e7cb645be59cfbf72e50343deff49
(3)   EAP-Message = 0x020400061500
(3)   NAS-Port-Type = Wireless-802.11
(3)   NAS-Port = 436
(3)   NAS-Port-Id = '436'
(3)   State = 0x9bcdbfd599c9aaec19ea252da52603d3
(3)   NAS-IP-Address = xxx.xxx.xxx.2
(3)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(3) session-state: No cached attributes
(3) # Executing section authorize from file /etc/raddb/sites-enabled/default
(3)   authorize {
(3)     policy filter_username {
(3)       if (!&User-Name) {
(3)       if (!&User-Name)  -> FALSE
(3)       if (&User-Name =~ / /) {
(3)       if (&User-Name =~ / /)  -> FALSE
(3)       if (&User-Name =~ /@.*@/ ) {
(3)       if (&User-Name =~ /@.*@/ )  -> FALSE
(3)       if (&User-Name =~ /\.\./ ) {
(3)       if (&User-Name =~ /\.\./ )  -> FALSE
(3)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(3)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(3)       if (&User-Name =~ /\.$/)  {
(3)       if (&User-Name =~ /\.$/)   -> FALSE
(3)       if (&User-Name =~ /@\./)  {
(3)       if (&User-Name =~ /@\./)   -> FALSE
(3)     } # policy filter_username = notfound
(3)     [preprocess] = ok
(3)     [chap] = noop
(3)     [mschap] = noop
(3) suffix: Checking for suffix after "@"
(3) suffix: No '@' in User-Name = "student", looking up realm NULL
(3) suffix: No such realm "NULL"
(3)     [suffix] = noop
(3)     update control {
(3)       EAP-TLS-Require-Client-Cert := Yes
(3)     } # update control = noop
(3) eap: Peer sent code Response (2) ID 4 length 6
(3) eap: Continuing tunnel setup
(3)     [eap] = ok
(3)   } # authorize = ok
(3) Found Auth-Type = EAP
(3) # Executing group from file /etc/raddb/sites-enabled/default
(3)   authenticate {
(3) eap: Expiring EAP session with state 0x9bcdbfd599c9aaec
(3) eap: Finished EAP session with state 0x9bcdbfd599c9aaec
(3) eap: Previous EAP request found for state 0x9bcdbfd599c9aaec, released from the list
(3) eap: Peer sent method TTLS (21)
(3) eap: EAP TTLS (21)
(3) eap: Calling eap_ttls to process EAP data
(3) eap_ttls: Authenticate
(3) eap_ttls: processing EAP-TLS
(3) eap_ttls: Received TLS ACK
(3) eap_ttls: Received TLS ACK
(3) eap_ttls: ACK handshake fragment handler
(3) eap_ttls: eaptls_verify returned 1 
(3) eap_ttls: eaptls_process returned 13 
(3) eap: EAP session adding &reply:State = 0x9bcdbfd598c8aaec
(3)     [eap] = handled
(3)   } # authenticate = handled
(3) Using Post-Auth-Type Challenge
(3) Post-Auth-Type sub-section not found.  Ignoring.
(3) # Executing group from file /etc/raddb/sites-enabled/default
(3) Sent Access-Challenge Id 128 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(3)   EAP-Message = 0x010503f615c00000173c6566656e636520616e6420537061636520436f6d70616e7931193017060355040b13105445535420454e5649524f4e4d454e54311530130603550403130c45414453205465737420434130820122300d06092a864886f70d01010105000382010f003082010a0282010100abf6
(3)   Message-Authenticator = 0x00000000000000000000000000000000
(3)   State = 0x9bcdbfd598c8aaec19ea252da52603d3
(3) Finished request
Waking up in 4.9 seconds.
(4) Received Access-Request Id 129 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(4)   User-Name = 'student'
(4)   Framed-MTU = 1400
(4)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(4)   Calling-Station-Id = '00-25-9C-94-85-84'
(4)   Cisco-AVPair = 'ssid=CAMPUS'
(4)   Service-Type = Login-User
(4)   Cisco-AVPair = 'service-type=Login'
(4)   Message-Authenticator = 0x7f008e8914d2ebd4183f21849db4632c
(4)   EAP-Message = 0x020500061500
(4)   NAS-Port-Type = Wireless-802.11
(4)   NAS-Port = 436
(4)   NAS-Port-Id = '436'
(4)   State = 0x9bcdbfd598c8aaec19ea252da52603d3
(4)   NAS-IP-Address = xxx.xxx.xxx.2
(4)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(4) session-state: No cached attributes
(4) # Executing section authorize from file /etc/raddb/sites-enabled/default
(4)   authorize {
(4)     policy filter_username {
(4)       if (!&User-Name) {
(4)       if (!&User-Name)  -> FALSE
(4)       if (&User-Name =~ / /) {
(4)       if (&User-Name =~ / /)  -> FALSE
(4)       if (&User-Name =~ /@.*@/ ) {
(4)       if (&User-Name =~ /@.*@/ )  -> FALSE
(4)       if (&User-Name =~ /\.\./ ) {
(4)       if (&User-Name =~ /\.\./ )  -> FALSE
(4)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(4)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(4)       if (&User-Name =~ /\.$/)  {
(4)       if (&User-Name =~ /\.$/)   -> FALSE
(4)       if (&User-Name =~ /@\./)  {
(4)       if (&User-Name =~ /@\./)   -> FALSE
(4)     } # policy filter_username = notfound
(4)     [preprocess] = ok
(4)     [chap] = noop
(4)     [mschap] = noop
(4) suffix: Checking for suffix after "@"
(4) suffix: No '@' in User-Name = "student", looking up realm NULL
(4) suffix: No such realm "NULL"
(4)     [suffix] = noop
(4)     update control {
(4)       EAP-TLS-Require-Client-Cert := Yes
(4)     } # update control = noop
(4) eap: Peer sent code Response (2) ID 5 length 6
(4) eap: Continuing tunnel setup
(4)     [eap] = ok
(4)   } # authorize = ok
(4) Found Auth-Type = EAP
(4) # Executing group from file /etc/raddb/sites-enabled/default
(4)   authenticate {
(4) eap: Expiring EAP session with state 0x9bcdbfd598c8aaec
(4) eap: Finished EAP session with state 0x9bcdbfd598c8aaec
(4) eap: Previous EAP request found for state 0x9bcdbfd598c8aaec, released from the list
(4) eap: Peer sent method TTLS (21)
(4) eap: EAP TTLS (21)
(4) eap: Calling eap_ttls to process EAP data
(4) eap_ttls: Authenticate
(4) eap_ttls: processing EAP-TLS
(4) eap_ttls: Received TLS ACK
(4) eap_ttls: Received TLS ACK
(4) eap_ttls: ACK handshake fragment handler
(4) eap_ttls: eaptls_verify returned 1 
(4) eap_ttls: eaptls_process returned 13 
(4) eap: EAP session adding &reply:State = 0x9bcdbfd59fcbaaec
(4)     [eap] = handled
(4)   } # authenticate = handled
(4) Using Post-Auth-Type Challenge
(4) Post-Auth-Type sub-section not found.  Ignoring.
(4) # Executing group from file /etc/raddb/sites-enabled/default
(4) Sent Access-Challenge Id 129 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(4)   EAP-Message = 0x010603f615c00000173c20646f206e6f74207472757374308180060b2b06010401ff300306026a3071306f06082b06010505070202306330331a2c4575726f7065616e204165726f737061636520446566656e636520616e6420537061636520436f6d70616e7930030201011a2c54455354206d656469
(4)   Message-Authenticator = 0x00000000000000000000000000000000
(4)   State = 0x9bcdbfd59fcbaaec19ea252da52603d3
(4) Finished request
Waking up in 4.9 seconds.
(5) Received Access-Request Id 130 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(5)   User-Name = 'student'
(5)   Framed-MTU = 1400
(5)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(5)   Calling-Station-Id = '00-25-9C-94-85-84'
(5)   Cisco-AVPair = 'ssid=CAMPUS'
(5)   Service-Type = Login-User
(5)   Cisco-AVPair = 'service-type=Login'
(5)   Message-Authenticator = 0x47292d5e469bdb12cae59436a4e46b84
(5)   EAP-Message = 0x020600061500
(5)   NAS-Port-Type = Wireless-802.11
(5)   NAS-Port = 436
(5)   NAS-Port-Id = '436'
(5)   State = 0x9bcdbfd59fcbaaec19ea252da52603d3
(5)   NAS-IP-Address = xxx.xxx.xxx.2
(5)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(5) session-state: No cached attributes
(5) # Executing section authorize from file /etc/raddb/sites-enabled/default
(5)   authorize {
(5)     policy filter_username {
(5)       if (!&User-Name) {
(5)       if (!&User-Name)  -> FALSE
(5)       if (&User-Name =~ / /) {
(5)       if (&User-Name =~ / /)  -> FALSE
(5)       if (&User-Name =~ /@.*@/ ) {
(5)       if (&User-Name =~ /@.*@/ )  -> FALSE
(5)       if (&User-Name =~ /\.\./ ) {
(5)       if (&User-Name =~ /\.\./ )  -> FALSE
(5)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(5)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(5)       if (&User-Name =~ /\.$/)  {
(5)       if (&User-Name =~ /\.$/)   -> FALSE
(5)       if (&User-Name =~ /@\./)  {
(5)       if (&User-Name =~ /@\./)   -> FALSE
(5)     } # policy filter_username = notfound
(5)     [preprocess] = ok
(5)     [chap] = noop
(5)     [mschap] = noop
(5) suffix: Checking for suffix after "@"
(5) suffix: No '@' in User-Name = "student", looking up realm NULL
(5) suffix: No such realm "NULL"
(5)     [suffix] = noop
(5)     update control {
(5)       EAP-TLS-Require-Client-Cert := Yes
(5)     } # update control = noop
(5) eap: Peer sent code Response (2) ID 6 length 6
(5) eap: Continuing tunnel setup
(5)     [eap] = ok
(5)   } # authorize = ok
(5) Found Auth-Type = EAP
(5) # Executing group from file /etc/raddb/sites-enabled/default
(5)   authenticate {
(5) eap: Expiring EAP session with state 0x9bcdbfd59fcbaaec
(5) eap: Finished EAP session with state 0x9bcdbfd59fcbaaec
(5) eap: Previous EAP request found for state 0x9bcdbfd59fcbaaec, released from the list
(5) eap: Peer sent method TTLS (21)
(5) eap: EAP TTLS (21)
(5) eap: Calling eap_ttls to process EAP data
(5) eap_ttls: Authenticate
(5) eap_ttls: processing EAP-TLS
(5) eap_ttls: Received TLS ACK
(5) eap_ttls: Received TLS ACK
(5) eap_ttls: ACK handshake fragment handler
(5) eap_ttls: eaptls_verify returned 1 
(5) eap_ttls: eaptls_process returned 13 
(5) eap: EAP session adding &reply:State = 0x9bcdbfd59ecaaaec
(5)     [eap] = handled
(5)   } # authenticate = handled
(5) Using Post-Auth-Type Challenge
(5) Post-Auth-Type sub-section not found.  Ignoring.
(5) # Executing group from file /etc/raddb/sites-enabled/default
(5) Sent Access-Challenge Id 130 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(5)   EAP-Message = 0x010703f615c00000173c5f23c6763fbe6fdd273e034fb3ef4cc6dbc9af18a7fe9bdc9ddaf9d1048b12e9492b5adcfc8c80cd5f749b62b43973d45f5eeac198a70c93ea431cb7c657ce8358dc884b12bb165499ae668887c6fab20d0dfbd1c47ee0bc4de3413caea8bc123b78da5a72b3495966d4ca3d8e
(5)   Message-Authenticator = 0x00000000000000000000000000000000
(5)   State = 0x9bcdbfd59ecaaaec19ea252da52603d3
(5) Finished request
Waking up in 4.9 seconds.
(6) Received Access-Request Id 131 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(6)   User-Name = 'student'
(6)   Framed-MTU = 1400
(6)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(6)   Calling-Station-Id = '00-25-9C-94-85-84'
(6)   Cisco-AVPair = 'ssid=CAMPUS'
(6)   Service-Type = Login-User
(6)   Cisco-AVPair = 'service-type=Login'
(6)   Message-Authenticator = 0x716976d3dd634b7728acb1bb137021e1
(6)   EAP-Message = 0x020700061500
(6)   NAS-Port-Type = Wireless-802.11
(6)   NAS-Port = 436
(6)   NAS-Port-Id = '436'
(6)   State = 0x9bcdbfd59ecaaaec19ea252da52603d3
(6)   NAS-IP-Address = xxx.xxx.xxx.2
(6)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(6) session-state: No cached attributes
(6) # Executing section authorize from file /etc/raddb/sites-enabled/default
(6)   authorize {
(6)     policy filter_username {
(6)       if (!&User-Name) {
(6)       if (!&User-Name)  -> FALSE
(6)       if (&User-Name =~ / /) {
(6)       if (&User-Name =~ / /)  -> FALSE
(6)       if (&User-Name =~ /@.*@/ ) {
(6)       if (&User-Name =~ /@.*@/ )  -> FALSE
(6)       if (&User-Name =~ /\.\./ ) {
(6)       if (&User-Name =~ /\.\./ )  -> FALSE
(6)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(6)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(6)       if (&User-Name =~ /\.$/)  {
(6)       if (&User-Name =~ /\.$/)   -> FALSE
(6)       if (&User-Name =~ /@\./)  {
(6)       if (&User-Name =~ /@\./)   -> FALSE
(6)     } # policy filter_username = notfound
(6)     [preprocess] = ok
(6)     [chap] = noop
(6)     [mschap] = noop
(6) suffix: Checking for suffix after "@"
(6) suffix: No '@' in User-Name = "student", looking up realm NULL
(6) suffix: No such realm "NULL"
(6)     [suffix] = noop
(6)     update control {
(6)       EAP-TLS-Require-Client-Cert := Yes
(6)     } # update control = noop
(6) eap: Peer sent code Response (2) ID 7 length 6
(6) eap: Continuing tunnel setup
(6)     [eap] = ok
(6)   } # authorize = ok
(6) Found Auth-Type = EAP
(6) # Executing group from file /etc/raddb/sites-enabled/default
(6)   authenticate {
(6) eap: Expiring EAP session with state 0x9bcdbfd59ecaaaec
(6) eap: Finished EAP session with state 0x9bcdbfd59ecaaaec
(6) eap: Previous EAP request found for state 0x9bcdbfd59ecaaaec, released from the list
(6) eap: Peer sent method TTLS (21)
(6) eap: EAP TTLS (21)
(6) eap: Calling eap_ttls to process EAP data
(6) eap_ttls: Authenticate
(6) eap_ttls: processing EAP-TLS
(6) eap_ttls: Received TLS ACK
(6) eap_ttls: Received TLS ACK
(6) eap_ttls: ACK handshake fragment handler
(6) eap_ttls: eaptls_verify returned 1 
(6) eap_ttls: eaptls_process returned 13 
(6) eap: EAP session adding &reply:State = 0x9bcdbfd59dc5aaec
(6)     [eap] = handled
(6)   } # authenticate = handled
(6) Using Post-Auth-Type Challenge
(6) Post-Auth-Type sub-section not found.  Ignoring.
(6) # Executing group from file /etc/raddb/sites-enabled/default
(6) Sent Access-Challenge Id 131 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(6)   EAP-Message = 0x010803aa15800000173c8ba6a40484a4bc00dab30a60ccdd92a848f87c1942082eea078e3389aa55bc97197a336850e513e5a6bcea0fe737e33a63fbde18a8624d266b8a140124a31014dd1babde5cb973cd8788868f782d98c443ee22e856fe50459d56a676aa381fc829f0513986de5c643acc170378
(6)   Message-Authenticator = 0x00000000000000000000000000000000
(6)   State = 0x9bcdbfd59dc5aaec19ea252da52603d3
(6) Finished request
Waking up in 4.8 seconds.
(7) Received Access-Request Id 132 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 1679
(7)   User-Name = 'student'
(7)   Framed-MTU = 1400
(7)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(7)   Calling-Station-Id = '00-25-9C-94-85-84'
(7)   Cisco-AVPair = 'ssid=CAMPUS'
(7)   Service-Type = Login-User
(7)   Cisco-AVPair = 'service-type=Login'
(7)   Message-Authenticator = 0x9c585e791741ce48d0b5098df28541ff
(7)   EAP-Message = 0x0208057815c00000159c16030113c60b0013c20013bf00068b308206873082056fa003020102021211215ad55946647e3915b964010827e2e9f2300d06092a864886f70d01010505003077310b300906035504061302444531363034060355040a132d4575726f7065616e204165726f6e617574696320
(7)   NAS-Port-Type = Wireless-802.11
(7)   NAS-Port = 436
(7)   NAS-Port-Id = '436'
(7)   State = 0x9bcdbfd59dc5aaec19ea252da52603d3
(7)   NAS-IP-Address = xxx.xxx.xxx.2
(7)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(7) session-state: No cached attributes
(7) # Executing section authorize from file /etc/raddb/sites-enabled/default
(7)   authorize {
(7)     policy filter_username {
(7)       if (!&User-Name) {
(7)       if (!&User-Name)  -> FALSE
(7)       if (&User-Name =~ / /) {
(7)       if (&User-Name =~ / /)  -> FALSE
(7)       if (&User-Name =~ /@.*@/ ) {
(7)       if (&User-Name =~ /@.*@/ )  -> FALSE
(7)       if (&User-Name =~ /\.\./ ) {
(7)       if (&User-Name =~ /\.\./ )  -> FALSE
(7)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(7)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(7)       if (&User-Name =~ /\.$/)  {
(7)       if (&User-Name =~ /\.$/)   -> FALSE
(7)       if (&User-Name =~ /@\./)  {
(7)       if (&User-Name =~ /@\./)   -> FALSE
(7)     } # policy filter_username = notfound
(7)     [preprocess] = ok
(7)     [chap] = noop
(7)     [mschap] = noop
(7) suffix: Checking for suffix after "@"
(7) suffix: No '@' in User-Name = "student", looking up realm NULL
(7) suffix: No such realm "NULL"
(7)     [suffix] = noop
(7)     update control {
(7)       EAP-TLS-Require-Client-Cert := Yes
(7)     } # update control = noop
(7) eap: Peer sent code Response (2) ID 8 length 1400
(7) eap: Continuing tunnel setup
(7)     [eap] = ok
(7)   } # authorize = ok
(7) Found Auth-Type = EAP
(7) # Executing group from file /etc/raddb/sites-enabled/default
(7)   authenticate {
(7) eap: Expiring EAP session with state 0x9bcdbfd59dc5aaec
(7) eap: Finished EAP session with state 0x9bcdbfd59dc5aaec
(7) eap: Previous EAP request found for state 0x9bcdbfd59dc5aaec, released from the list
(7) eap: Peer sent method TTLS (21)
(7) eap: EAP TTLS (21)
(7) eap: Calling eap_ttls to process EAP data
(7) eap_ttls: Authenticate
(7) eap_ttls: processing EAP-TLS
(7) eap_ttls: TLS Length 5532
(7) eap_ttls: Received EAP-TLS First Fragment of the message
(7) eap_ttls: eaptls_verify returned 9 
(7) eap_ttls: eaptls_process returned 13 
(7) eap: EAP session adding &reply:State = 0x9bcdbfd59cc4aaec
(7)     [eap] = handled
(7)   } # authenticate = handled
(7) Using Post-Auth-Type Challenge
(7) Post-Auth-Type sub-section not found.  Ignoring.
(7) # Executing group from file /etc/raddb/sites-enabled/default
(7) Sent Access-Challenge Id 132 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(7)   EAP-Message = 0x010900061500
(7)   Message-Authenticator = 0x00000000000000000000000000000000
(7)   State = 0x9bcdbfd59cc4aaec19ea252da52603d3
(7) Finished request
Waking up in 4.8 seconds.
(8) Received Access-Request Id 133 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 1679
(8)   User-Name = 'student'
(8)   Framed-MTU = 1400
(8)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(8)   Calling-Station-Id = '00-25-9C-94-85-84'
(8)   Cisco-AVPair = 'ssid=CAMPUS'
(8)   Service-Type = Login-User
(8)   Cisco-AVPair = 'service-type=Login'
(8)   Message-Authenticator = 0xbcd7ca03fa688510616e87cac8f26033
(8)   EAP-Message = 0x0209057815403016801448aaa68d782a31cc872387e93c1c179a039146fb300d06092a864886f70d010105050003820101003e934713857a355f608f8f3f7f9916a3215c740cf0712bee056451e15b897b5d665b9640d44ee329aecc9fac099fd8a25555002ab05cb1a1fd4fe12f37871543c6a5e3d4b9
(8)   NAS-Port-Type = Wireless-802.11
(8)   NAS-Port = 436
(8)   NAS-Port-Id = '436'
(8)   State = 0x9bcdbfd59cc4aaec19ea252da52603d3
(8)   NAS-IP-Address = xxx.xxx.xxx.2
(8)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(8) session-state: No cached attributes
(8) # Executing section authorize from file /etc/raddb/sites-enabled/default
(8)   authorize {
(8)     policy filter_username {
(8)       if (!&User-Name) {
(8)       if (!&User-Name)  -> FALSE
(8)       if (&User-Name =~ / /) {
(8)       if (&User-Name =~ / /)  -> FALSE
(8)       if (&User-Name =~ /@.*@/ ) {
(8)       if (&User-Name =~ /@.*@/ )  -> FALSE
(8)       if (&User-Name =~ /\.\./ ) {
(8)       if (&User-Name =~ /\.\./ )  -> FALSE
(8)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(8)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(8)       if (&User-Name =~ /\.$/)  {
(8)       if (&User-Name =~ /\.$/)   -> FALSE
(8)       if (&User-Name =~ /@\./)  {
(8)       if (&User-Name =~ /@\./)   -> FALSE
(8)     } # policy filter_username = notfound
(8)     [preprocess] = ok
(8)     [chap] = noop
(8)     [mschap] = noop
(8) suffix: Checking for suffix after "@"
(8) suffix: No '@' in User-Name = "student", looking up realm NULL
(8) suffix: No such realm "NULL"
(8)     [suffix] = noop
(8)     update control {
(8)       EAP-TLS-Require-Client-Cert := Yes
(8)     } # update control = noop
(8) eap: Peer sent code Response (2) ID 9 length 1400
(8) eap: Continuing tunnel setup
(8)     [eap] = ok
(8)   } # authorize = ok
(8) Found Auth-Type = EAP
(8) # Executing group from file /etc/raddb/sites-enabled/default
(8)   authenticate {
(8) eap: Expiring EAP session with state 0x9bcdbfd59cc4aaec
(8) eap: Finished EAP session with state 0x9bcdbfd59cc4aaec
(8) eap: Previous EAP request found for state 0x9bcdbfd59cc4aaec, released from the list
(8) eap: Peer sent method TTLS (21)
(8) eap: EAP TTLS (21)
(8) eap: Calling eap_ttls to process EAP data
(8) eap_ttls: Authenticate
(8) eap_ttls: processing EAP-TLS
(8) eap_ttls: More fragments to follow
(8) eap_ttls: eaptls_verify returned 10 
(8) eap_ttls: eaptls_process returned 13 
(8) eap: EAP session adding &reply:State = 0x9bcdbfd593c7aaec
(8)     [eap] = handled
(8)   } # authenticate = handled
(8) Using Post-Auth-Type Challenge
(8) Post-Auth-Type sub-section not found.  Ignoring.
(8) # Executing group from file /etc/raddb/sites-enabled/default
(8) Sent Access-Challenge Id 133 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(8)   EAP-Message = 0x010a00061500
(8)   Message-Authenticator = 0x00000000000000000000000000000000
(8)   State = 0x9bcdbfd593c7aaec19ea252da52603d3
(8) Finished request
Waking up in 4.8 seconds.
(9) Received Access-Request Id 134 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 1679
(9)   User-Name = 'student'
(9)   Framed-MTU = 1400
(9)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(9)   Calling-Station-Id = '00-25-9C-94-85-84'
(9)   Cisco-AVPair = 'ssid=CAMPUS'
(9)   Service-Type = Login-User
(9)   Cisco-AVPair = 'service-type=Login'
(9)   Message-Authenticator = 0x31161572c7523ccf41bd9ae9753bd931
(9)   EAP-Message = 0x020a05781540206d656469756d2073772063627020706f6c696379202d20646f206e6f74207472757374307c060b2b06010401ff3003060269306d306b06082b06010505070202305f30331a2c4575726f7065616e204165726f737061636520446566656e636520616e6420537061636520436f6d7061
(9)   NAS-Port-Type = Wireless-802.11
(9)   NAS-Port = 436
(9)   NAS-Port-Id = '436'
(9)   State = 0x9bcdbfd593c7aaec19ea252da52603d3
(9)   NAS-IP-Address = xxx.xxx.xxx.2
(9)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(9) session-state: No cached attributes
(9) # Executing section authorize from file /etc/raddb/sites-enabled/default
(9)   authorize {
(9)     policy filter_username {
(9)       if (!&User-Name) {
(9)       if (!&User-Name)  -> FALSE
(9)       if (&User-Name =~ / /) {
(9)       if (&User-Name =~ / /)  -> FALSE
(9)       if (&User-Name =~ /@.*@/ ) {
(9)       if (&User-Name =~ /@.*@/ )  -> FALSE
(9)       if (&User-Name =~ /\.\./ ) {
(9)       if (&User-Name =~ /\.\./ )  -> FALSE
(9)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(9)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(9)       if (&User-Name =~ /\.$/)  {
(9)       if (&User-Name =~ /\.$/)   -> FALSE
(9)       if (&User-Name =~ /@\./)  {
(9)       if (&User-Name =~ /@\./)   -> FALSE
(9)     } # policy filter_username = notfound
(9)     [preprocess] = ok
(9)     [chap] = noop
(9)     [mschap] = noop
(9) suffix: Checking for suffix after "@"
(9) suffix: No '@' in User-Name = "student", looking up realm NULL
(9) suffix: No such realm "NULL"
(9)     [suffix] = noop
(9)     update control {
(9)       EAP-TLS-Require-Client-Cert := Yes
(9)     } # update control = noop
(9) eap: Peer sent code Response (2) ID 10 length 1400
(9) eap: Continuing tunnel setup
(9)     [eap] = ok
(9)   } # authorize = ok
(9) Found Auth-Type = EAP
(9) # Executing group from file /etc/raddb/sites-enabled/default
(9)   authenticate {
(9) eap: Expiring EAP session with state 0x9bcdbfd593c7aaec
(9) eap: Finished EAP session with state 0x9bcdbfd593c7aaec
(9) eap: Previous EAP request found for state 0x9bcdbfd593c7aaec, released from the list
(9) eap: Peer sent method TTLS (21)
(9) eap: EAP TTLS (21)
(9) eap: Calling eap_ttls to process EAP data
(9) eap_ttls: Authenticate
(9) eap_ttls: processing EAP-TLS
(9) eap_ttls: More fragments to follow
(9) eap_ttls: eaptls_verify returned 10 
(9) eap_ttls: eaptls_process returned 13 
(9) eap: EAP session adding &reply:State = 0x9bcdbfd592c6aaec
(9)     [eap] = handled
(9)   } # authenticate = handled
(9) Using Post-Auth-Type Challenge
(9) Post-Auth-Type sub-section not found.  Ignoring.
(9) # Executing group from file /etc/raddb/sites-enabled/default
(9) Sent Access-Challenge Id 134 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(9)   EAP-Message = 0x010b00061500
(9)   Message-Authenticator = 0x00000000000000000000000000000000
(9)   State = 0x9bcdbfd592c6aaec19ea252da52603d3
(9) Finished request
Waking up in 4.8 seconds.
(10) Received Access-Request Id 135 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 1639
(10)   User-Name = 'student'
(10)   Framed-MTU = 1400
(10)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(10)   Calling-Station-Id = '00-25-9C-94-85-84'
(10)   Cisco-AVPair = 'ssid=CAMPUS'
(10)   Service-Type = Login-User
(10)   Cisco-AVPair = 'service-type=Login'
(10)   Message-Authenticator = 0x9cfd51842c173b94013e93baad56352e
(10)   EAP-Message = 0x020b055015006d70616e7931193017060355040b13105445535420454e5649524f4e4d454e54311a30180603550403131145414453205445535420526f6f74204341301e170d3038303432353030303030305a170d3238303432353030303030305a307c310b3009060355040613024445313630340603
(10)   NAS-Port-Type = Wireless-802.11
(10)   NAS-Port = 436
(10)   NAS-Port-Id = '436'
(10)   State = 0x9bcdbfd592c6aaec19ea252da52603d3
(10)   NAS-IP-Address = xxx.xxx.xxx.2
(10)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(10) session-state: No cached attributes
(10) # Executing section authorize from file /etc/raddb/sites-enabled/default
(10)   authorize {
(10)     policy filter_username {
(10)       if (!&User-Name) {
(10)       if (!&User-Name)  -> FALSE
(10)       if (&User-Name =~ / /) {
(10)       if (&User-Name =~ / /)  -> FALSE
(10)       if (&User-Name =~ /@.*@/ ) {
(10)       if (&User-Name =~ /@.*@/ )  -> FALSE
(10)       if (&User-Name =~ /\.\./ ) {
(10)       if (&User-Name =~ /\.\./ )  -> FALSE
(10)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(10)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(10)       if (&User-Name =~ /\.$/)  {
(10)       if (&User-Name =~ /\.$/)   -> FALSE
(10)       if (&User-Name =~ /@\./)  {
(10)       if (&User-Name =~ /@\./)   -> FALSE
(10)     } # policy filter_username = notfound
(10)     [preprocess] = ok
(10)     [chap] = noop
(10)     [mschap] = noop
(10) suffix: Checking for suffix after "@"
(10) suffix: No '@' in User-Name = "student", looking up realm NULL
(10) suffix: No such realm "NULL"
(10)     [suffix] = noop
(10)     update control {
(10)       EAP-TLS-Require-Client-Cert := Yes
(10)     } # update control = noop
(10) eap: Peer sent code Response (2) ID 11 length 1360
(10) eap: Continuing tunnel setup
(10)     [eap] = ok
(10)   } # authorize = ok
(10) Found Auth-Type = EAP
(10) # Executing group from file /etc/raddb/sites-enabled/default
(10)   authenticate {
(10) eap: Expiring EAP session with state 0x9bcdbfd592c6aaec
(10) eap: Finished EAP session with state 0x9bcdbfd592c6aaec
(10) eap: Previous EAP request found for state 0x9bcdbfd592c6aaec, released from the list
(10) eap: Peer sent method TTLS (21)
(10) eap: EAP TTLS (21)
(10) eap: Calling eap_ttls to process EAP data
(10) eap_ttls: Authenticate
(10) eap_ttls: processing EAP-TLS
(10) eap_ttls: eaptls_verify returned 7 
(10) eap_ttls: Done initial handshake
(10) eap_ttls: <<< Unknown TLS version [length 0005] 
(10) eap_ttls: <<< TLS 1.0 Handshake [length 13c6], Certificate 
(10) eap_ttls: TLS Verify creating certificate attributes
(10) eap_ttls: chain-depth=2, 
(10) eap_ttls: error=0
(10) eap_ttls: --> User-Name = student
(10) eap_ttls: --> BUF-Name = EADS TEST Root CA
(10) eap_ttls: --> subject = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS TEST Root CA
(10) eap_ttls: --> issuer  = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS TEST Root CA
(10) eap_ttls: --> verify return:1
(10) eap_ttls: TLS Verify creating certificate attributes
(10) eap_ttls:   TLS-Cert-Serial := '07'
(10) eap_ttls:   TLS-Cert-Expiration := '190525193644Z'
(10) eap_ttls:   TLS-Cert-Subject := '/C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS Test CA'
(10) eap_ttls:   TLS-Cert-Issuer := '/C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS TEST Root CA'
(10) eap_ttls:   TLS-Cert-Common-Name := 'EADS Test CA'
(10) eap_ttls: chain-depth=1, 
(10) eap_ttls: error=0
(10) eap_ttls: --> User-Name = student
(10) eap_ttls: --> BUF-Name = EADS Test CA
(10) eap_ttls: --> subject = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS Test CA
(10) eap_ttls: --> issuer  = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS TEST Root CA
(10) eap_ttls: --> verify return:1
(10) eap_ttls: TLS Verify creating certificate attributes
(10) eap_ttls:   TLS-Client-Cert-Serial := '11215ad55946647e3915b964010827e2e9f2'
(10) eap_ttls:   TLS-Client-Cert-Expiration := '170129141552Z'
(10) eap_ttls:   TLS-Client-Cert-Subject := '/C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/OU=Aircraft/OU=AIB/CN=D-AXYZ.A359.AIB.auth'
(10) eap_ttls:   TLS-Client-Cert-Issuer := '/C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS Test CA'
(10) eap_ttls:   TLS-Client-Cert-Common-Name := 'D-AXYZ.A359.AIB.auth'
(10) eap_ttls:     TLS-Client-Cert-X509v3-Extended-Key-Usage += 'TLS Web Client Authentication, TLS Web Server Authentication'
(10) eap_ttls:     TLS-Client-Cert-X509v3-Subject-Key-Identifier += '5B:09:9D:C9:0F:7F:68:FE:6D:B6:CF:B1:9F:BF:BD:F8:6C:89:25:42'
(10) eap_ttls:     TLS-Client-Cert-X509v3-Authority-Key-Identifier += 'keyid:48:AA:A6:8D:78:2A:31:CC:87:23:87:E9:3C:1C:17:9A:03:91:46:FB '
(10) eap_ttls: Verifying client certificate: /check_client_cert.sh /etc/raddb/certs/tmp  %{TLS-Client-Cert-Filename} /etc/raddb/certs/AuthCert.pem
(10) eap_ttls: Executing: /check_client_cert.sh /etc/raddb/certs/tmp  %{TLS-Client-Cert-Filename} /etc/raddb/certs/AuthCert.pem:
(10) eap_ttls: EXPAND %{TLS-Client-Cert-Filename}
(10) eap_ttls:    --> /etc/raddb/certs/tmp/radiusd.client.XXDju6gZ
(10) eap_ttls: Program returned code (0) and output 'Last client-OU: AIB; Last server-OU: AIB EKU:     TLS Web Client Authentication, TLS Web Server Authentication'
(10) eap_ttls: Client certificate CN D-AXYZ.A359.AIB.auth passed external validation
(10) eap_ttls: chain-depth=0, 
(10) eap_ttls: error=0
(10) eap_ttls: --> User-Name = student
(10) eap_ttls: --> BUF-Name = D-AXYZ.A359.AIB.auth
(10) eap_ttls: --> subject = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/OU=Aircraft/OU=AIB/CN=D-AXYZ.A359.AIB.auth
(10) eap_ttls: --> issuer  = /C=DE/O=European Aeronautic Defence and Space Company/OU=TEST ENVIRONMENT/CN=EADS Test CA
(10) eap_ttls: --> verify return:1
(10) eap_ttls: TLS_accept: SSLv3 read client certificate A
(10) eap_ttls: <<< Unknown TLS version [length 0005] 
(10) eap_ttls: <<< TLS 1.0 Handshake [length 0086], ClientKeyExchange 
(10) eap_ttls: TLS_accept: SSLv3 read client key exchange A
(10) eap_ttls: <<< Unknown TLS version [length 0005] 
(10) eap_ttls: <<< TLS 1.0 Handshake [length 0106], CertificateVerify 
(10) eap_ttls: TLS_accept: SSLv3 read certificate verify A
(10) eap_ttls: <<< Unknown TLS version [length 0005] 
(10) eap_ttls: <<< TLS 1.0 ChangeCipherSpec [length 0001] 
(10) eap_ttls: <<< Unknown TLS version [length 0005] 
(10) eap_ttls: <<< TLS 1.0 Handshake [length 0010], Finished 
(10) eap_ttls: TLS_accept: SSLv3 read finished A
(10) eap_ttls: >>> Unknown TLS version [length 0005] 
(10) eap_ttls: >>> TLS 1.0 ChangeCipherSpec [length 0001] 
(10) eap_ttls: TLS_accept: SSLv3 write change cipher spec A
(10) eap_ttls: >>> Unknown TLS version [length 0005] 
(10) eap_ttls: >>> TLS 1.0 Handshake [length 0010], Finished 
(10) eap_ttls: TLS_accept: SSLv3 write finished A
(10) eap_ttls: TLS_accept: SSLv3 flush data
  TLS: adding session 93c65df3253f3fa2c2fac8c19ccbb47e845394db8f7659602f0f704dc8cec089 to cache
(10) eap_ttls: (other): SSL negotiation finished successfully
SSL Connection Established 
(10) eap_ttls: eaptls_process returned 13 
(10) eap: EAP session adding &reply:State = 0x9bcdbfd591c1aaec
(10)     [eap] = handled
(10)   } # authenticate = handled
(10) Using Post-Auth-Type Challenge
(10) Post-Auth-Type sub-section not found.  Ignoring.
(10) # Executing group from file /etc/raddb/sites-enabled/default
(10) Sent Access-Challenge Id 135 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(10)   EAP-Message = 0x010c004515800000003b1403010001011603010030abda9f1cae570cd3812fb7da16c2565f2c2bbb01bfe0ba0ec29f62dd7db404e0454ec96830171a81a67216eb760d8e97
(10)   Message-Authenticator = 0x00000000000000000000000000000000
(10)   State = 0x9bcdbfd591c1aaec19ea252da52603d3
(10) Finished request
Waking up in 4.7 seconds.
(11) Received Access-Request Id 136 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 428
(11)   User-Name = 'student'
(11)   Framed-MTU = 1400
(11)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(11)   Calling-Station-Id = '00-25-9C-94-85-84'
(11)   Cisco-AVPair = 'ssid=CAMPUS'
(11)   Service-Type = Login-User
(11)   Cisco-AVPair = 'service-type=Login'
(11)   Message-Authenticator = 0xaa530fbdd5ef0587bfaa519bd78293b7
(11)   EAP-Message = 0x020c009f15800000009517030100901bff4f07430717e3ddf1e64c6815701812681fcf9824fc2c02b4f641fc643640f750157f2a95290b91aa6ad0777e34bff74364001caa14b058007bc8bfaa6e33763ea05e71b92b0caa0a90f3e76451f1ace888a14ab2ebbf8b0dfd5bef5d125037866d2955eafa99
(11)   NAS-Port-Type = Wireless-802.11
(11)   NAS-Port = 436
(11)   NAS-Port-Id = '436'
(11)   State = 0x9bcdbfd591c1aaec19ea252da52603d3
(11)   NAS-IP-Address = xxx.xxx.xxx.2
(11)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(11) session-state: No cached attributes
(11) # Executing section authorize from file /etc/raddb/sites-enabled/default
(11)   authorize {
(11)     policy filter_username {
(11)       if (!&User-Name) {
(11)       if (!&User-Name)  -> FALSE
(11)       if (&User-Name =~ / /) {
(11)       if (&User-Name =~ / /)  -> FALSE
(11)       if (&User-Name =~ /@.*@/ ) {
(11)       if (&User-Name =~ /@.*@/ )  -> FALSE
(11)       if (&User-Name =~ /\.\./ ) {
(11)       if (&User-Name =~ /\.\./ )  -> FALSE
(11)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(11)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(11)       if (&User-Name =~ /\.$/)  {
(11)       if (&User-Name =~ /\.$/)   -> FALSE
(11)       if (&User-Name =~ /@\./)  {
(11)       if (&User-Name =~ /@\./)   -> FALSE
(11)     } # policy filter_username = notfound
(11)     [preprocess] = ok
(11)     [chap] = noop
(11)     [mschap] = noop
(11) suffix: Checking for suffix after "@"
(11) suffix: No '@' in User-Name = "student", looking up realm NULL
(11) suffix: No such realm "NULL"
(11)     [suffix] = noop
(11)     update control {
(11)       EAP-TLS-Require-Client-Cert := Yes
(11)     } # update control = noop
(11) eap: Peer sent code Response (2) ID 12 length 159
(11) eap: Continuing tunnel setup
(11)     [eap] = ok
(11)   } # authorize = ok
(11) Found Auth-Type = EAP
(11) # Executing group from file /etc/raddb/sites-enabled/default
(11)   authenticate {
(11) eap: Expiring EAP session with state 0x9bcdbfd591c1aaec
(11) eap: Finished EAP session with state 0x9bcdbfd591c1aaec
(11) eap: Previous EAP request found for state 0x9bcdbfd591c1aaec, released from the list
(11) eap: Peer sent method TTLS (21)
(11) eap: EAP TTLS (21)
(11) eap: Calling eap_ttls to process EAP data
(11) eap_ttls: Authenticate
(11) eap_ttls: processing EAP-TLS
(11) eap_ttls: TLS Length 149
(11) eap_ttls: Length Included
(11) eap_ttls: eaptls_verify returned 11 
(11) eap_ttls: <<< Unknown TLS version [length 0005] 
(11) eap_ttls: eaptls_process returned 7 
(11) eap_ttls: Session established.  Proceeding to decode tunneled attributes
(11) eap_ttls: Got tunneled request
(11) eap_ttls:   User-Name = 'student'
(11) eap_ttls:   MS-CHAP-Challenge = 0xd90494ec392b33daf7f1eb9b0b8d4cd5
(11) eap_ttls:   MS-CHAP2-Response = 0xd000d4956dcf2c2b84603298a98823bb9e090000000000000000865f505be820f1ec7950de9a16dba17abf42798990f37c18
(11) eap_ttls: Sending tunneled request
(11) Virtual server inner-tunnel received request
(11)   User-Name = 'student'
(11)   MS-CHAP-Challenge = 0xd90494ec392b33daf7f1eb9b0b8d4cd5
(11)   MS-CHAP2-Response = 0xd000d4956dcf2c2b84603298a98823bb9e090000000000000000865f505be820f1ec7950de9a16dba17abf42798990f37c18
(11) server inner-tunnel {
(11)   # Executing section authorize from file /etc/raddb/sites-enabled/inner-tunnel
(11)     authorize {
(11)       [chap] = noop
(11) mschap: Found MS-CHAP attributes.  Setting 'Auth-Type  = mschap'
(11)       [mschap] = ok
(11)       [unix] = notfound
(11) suffix: Checking for suffix after "@"
(11) suffix: No '@' in User-Name = "student", looking up realm NULL
(11) suffix: No such realm "NULL"
(11)       [suffix] = noop
(11)       update control {
(11)         &Proxy-To-Realm := LOCAL
(11)       } # update control = noop
(11) eap: No EAP-Message, not doing EAP
(11)       [eap] = noop
(11) files: users: Matched entry student at line 72
(11)       [files] = ok
(11)       [expiration] = noop
(11)       [logintime] = noop
(11) pap: WARNING: Auth-Type already set.  Not setting to PAP
(11)       [pap] = noop
(11)     } # authorize = ok
(11)   Found Auth-Type = MSCHAP
(11)   # Executing group from file /etc/raddb/sites-enabled/inner-tunnel
(11)     Auth-Type MS-CHAP {
(11) mschap: Found Cleartext-Password, hashing to create NT-Password
(11) mschap: Found Cleartext-Password, hashing to create LM-Password
(11) mschap: Creating challenge hash with username: student
(11) mschap: Client is using MS-CHAPv2
(11) mschap: Adding MS-CHAPv2 MPPE keys
(11)       [mschap] = ok
(11)     } # Auth-Type MS-CHAP = ok
(11)   # Executing section post-auth from file /etc/raddb/sites-enabled/inner-tunnel
(11)   Login OK: [student/<via Auth-Type = MSCHAP>] (from client CAMPUS-MANAGEMENT port 0 via TLS tunnel)
(11) } # server inner-tunnel
(11) Virtual server sending reply
(11)   MS-CHAP2-Success = 0xd0533d42464137423734363741393941363446413343444139413532413941393036414538324632433831
(11)   MS-MPPE-Recv-Key = 0x82c3ebfbaed78f1663ed5ab09d860e3d
(11)   MS-MPPE-Send-Key = 0x6e5945017cec36eaf4344452e661045b
(11)   MS-MPPE-Encryption-Policy = Encryption-Allowed
(11)   MS-MPPE-Encryption-Types = RC4-40or128-bit-Allowed
(11) eap_ttls: Got tunneled Access-Accept
(11) eap_ttls: Got MS-CHAP2-Success, tunneling it to the client in a challenge
(11) eap_ttls: Sending tunneled reply attributes
(11) eap_ttls:   MS-CHAP2-Success = 0xd0533d42464137423734363741393941363446413343444139413532413941393036414538324632433831
>>> Unknown TLS version [length 0005] 
(11) eap: EAP session adding &reply:State = 0x9bcdbfd590c0aaec
(11)     [eap] = handled
(11)   } # authenticate = handled
(11) Using Post-Auth-Type Challenge
(11) Post-Auth-Type sub-section not found.  Ignoring.
(11) # Executing group from file /etc/raddb/sites-enabled/default
(11) Sent Access-Challenge Id 136 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 0
(11)   EAP-Message = 0x010d005f1580000000551703010050293b2bb1e66356a33e08eaf1f4f04236f3276581708230c47338b9d20131710feb1c2f336e8db9269b229c6bafc124802f075c57dbfc129ea57880d006d6512adf95f3f813b3deb29555e52a1a6bad27
(11)   Message-Authenticator = 0x00000000000000000000000000000000
(11)   State = 0x9bcdbfd590c0aaec19ea252da52603d3
(11) Finished request
Waking up in 2.1 seconds.
(12) Received Access-Request Id 137 from xxx.xxx.xxx.2:1645 to xxx.xxx.yyy.109:1812 length 275
(12)   User-Name = 'student'
(12)   Framed-MTU = 1400
(12)   Called-Station-Id = 'A4-18-75-02-72-C0:CAMPUS'
(12)   Calling-Station-Id = '00-25-9C-94-85-84'
(12)   Cisco-AVPair = 'ssid=CAMPUS'
(12)   Service-Type = Login-User
(12)   Cisco-AVPair = 'service-type=Login'
(12)   Message-Authenticator = 0x9fe78dca8c0679f88d3e530cab540384
(12)   EAP-Message = 0x020d00061500
(12)   NAS-Port-Type = Wireless-802.11
(12)   NAS-Port = 436
(12)   NAS-Port-Id = '436'
(12)   State = 0x9bcdbfd590c0aaec19ea252da52603d3
(12)   NAS-IP-Address = xxx.xxx.xxx.2
(12)   NAS-Identifier = 'PN=Z420H0100210_SER=420H01000024_SW=ADX581828400002_CFG=ADX591829400002'
(12) session-state: No cached attributes
(12) # Executing section authorize from file /etc/raddb/sites-enabled/default
(12)   authorize {
(12)     policy filter_username {
(12)       if (!&User-Name) {
(12)       if (!&User-Name)  -> FALSE
(12)       if (&User-Name =~ / /) {
(12)       if (&User-Name =~ / /)  -> FALSE
(12)       if (&User-Name =~ /@.*@/ ) {
(12)       if (&User-Name =~ /@.*@/ )  -> FALSE
(12)       if (&User-Name =~ /\.\./ ) {
(12)       if (&User-Name =~ /\.\./ )  -> FALSE
(12)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))  {
(12)       if ((&User-Name =~ /@/) && (&User-Name !~ /@(.+)\.(.+)$/))   -> FALSE
(12)       if (&User-Name =~ /\.$/)  {
(12)       if (&User-Name =~ /\.$/)   -> FALSE
(12)       if (&User-Name =~ /@\./)  {
(12)       if (&User-Name =~ /@\./)   -> FALSE
(12)     } # policy filter_username = notfound
(12)     [preprocess] = ok
(12)     [chap] = noop
(12)     [mschap] = noop
(12) suffix: Checking for suffix after "@"
(12) suffix: No '@' in User-Name = "student", looking up realm NULL
(12) suffix: No such realm "NULL"
(12)     [suffix] = noop
(12)     update control {
(12)       EAP-TLS-Require-Client-Cert := Yes
(12)     } # update control = noop
(12) eap: Peer sent code Response (2) ID 13 length 6
(12) eap: Continuing tunnel setup
(12)     [eap] = ok
(12)   } # authorize = ok
(12) Found Auth-Type = EAP
(12) # Executing group from file /etc/raddb/sites-enabled/default
(12)   authenticate {
(12) eap: Expiring EAP session with state 0x9bcdbfd590c0aaec
(12) eap: Finished EAP session with state 0x9bcdbfd590c0aaec
(12) eap: Previous EAP request found for state 0x9bcdbfd590c0aaec, released from the list
(12) eap: Peer sent method TTLS (21)
(12) eap: EAP TTLS (21)
(12) eap: Calling eap_ttls to process EAP data
(12) eap_ttls: Authenticate
(12) eap_ttls: processing EAP-TLS
(12) eap_ttls: Received TLS ACK
(12) eap_ttls: Received TLS ACK
(12) eap_ttls: ERROR: Invalid ACK received: 0
(12) eap_ttls: eaptls_verify returned 0 
(12) eap_ttls: eaptls_process returned 0 
(12) eap: ERROR: Failed continuing EAP TTLS (21) session. EAP sub-module failed
(12) eap: Failed in EAP select
(12)     [eap] = invalid
(12)   } # authenticate = invalid
(12) Failed to authenticate the user
(12) Login incorrect (eap_ttls: Invalid ACK received: 0): [student/<via Auth-Type = EAP>] (from client CAMPUS-MANAGEMENT port 436 cli 00-25-9C-94-85-84)
(12) Using Post-Auth-Type Reject
(12) # Executing group from file /etc/raddb/sites-enabled/default
(12)   Post-Auth-Type REJECT {
(12) attr_filter.access_reject: EXPAND %{User-Name}
(12) attr_filter.access_reject:    --> student
(12) attr_filter.access_reject: Matched entry DEFAULT at line 18
(12)     [attr_filter.access_reject] = updated
(12)     [eap] = noop
(12)     policy remove_reply_message_if_eap {
(12)       if (&reply:EAP-Message && &reply:Reply-Message) {
(12)       if (&reply:EAP-Message && &reply:Reply-Message)  -> FALSE
(12)       else {
(12)         [noop] = noop
(12)       } # else = noop
(12)     } # policy remove_reply_message_if_eap = noop
(12)   } # Post-Auth-Type REJECT = updated
(12) Delaying response for 1.000000 seconds
Waking up in 0.6 seconds.
Waking up in 0.3 seconds.
(12) <delay>: Sending delayed response
(12) <delay>: Sent Access-Reject Id 137 from xxx.xxx.yyy.109:1812 to xxx.xxx.xxx.2:1645 length 44
(12) <delay>:   EAP-Message = 0x040d0004
(12) <delay>:   Message-Authenticator = 0x00000000000000000000000000000000
Waking up in 1.1 seconds.
(0) <done>: Cleaning up request packet ID 125 with timestamp +372
(1) <done>: Cleaning up request packet ID 126 with timestamp +372
(2) <done>: Cleaning up request packet ID 127 with timestamp +372
(3) <done>: Cleaning up request packet ID 128 with timestamp +372
(4) <done>: Cleaning up request packet ID 129 with timestamp +372
(5) <done>: Cleaning up request packet ID 130 with timestamp +372
(6) <done>: Cleaning up request packet ID 131 with timestamp +372
(7) <done>: Cleaning up request packet ID 132 with timestamp +372
(8) <done>: Cleaning up request packet ID 133 with timestamp +372
(9) <done>: Cleaning up request packet ID 134 with timestamp +373
(10) <done>: Cleaning up request packet ID 135 with timestamp +373
Waking up in 2.5 seconds.
(11) <done>: Cleaning up request packet ID 136 with timestamp +375
(12) <delay>: Cleaning up request packet ID 137 with timestamp +375
Ready to process requests
Ready to process requests
Signalled to terminate
Exiting normally
  SSL: Removing session 93c65df3253f3fa2c2fac8c19ccbb47e845394db8f7659602f0f704dc8cec089 from the cache
-------------- next part --------------
/*
 * tls.c
 *
 * Version:     $Id: 9df48b4c445e94fe2cb9465191d1195ee7df4e19 $
 *
 *   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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * Copyright 2001  hereUare Communications, Inc. <raghud at hereuare.com>
 * Copyright 2003  Alan DeKok <aland at freeradius.org>
 * Copyright 2006  The FreeRADIUS server project
 */

RCSID("$Id: 9df48b4c445e94fe2cb9465191d1195ee7df4e19 $")
USES_APPLE_DEPRECATED_API	/* OpenSSL API has been deprecated by Apple */

#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/process.h>
#include <freeradius-devel/rad_assert.h>

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#include <ctype.h>

#ifdef WITH_TLS
#  ifdef HAVE_OPENSSL_RAND_H
#    include <openssl/rand.h>
#  endif

#  ifdef HAVE_OPENSSL_OCSP_H
#    include <openssl/ocsp.h>
#  endif

#  ifdef HAVE_OPENSSL_EVP_H
#    include <openssl/evp.h>
#  endif
#  include <openssl/ssl.h>

#define LOG_PREFIX "tls"

#ifdef ENABLE_OPENSSL_VERSION_CHECK
typedef struct libssl_defect {
	uint64_t	high;
	uint64_t	low;

	char const	*id;
	char const	*name;
	char const	*comment;
} libssl_defect_t;

/* Record critical defects in libssl here (newest first)*/
static libssl_defect_t libssl_defects[] =
{
	{
		.low		= 0x010001000,		/* 1.0.1  */
		.high		= 0x01000106f,		/* 1.0.1f */
		.id		= "CVE-2014-0160",
		.name		= "Heartbleed",
		.comment	= "For more information see http://heartbleed.com"
	}
};
#endif /* ENABLE_OPENSSL_VERSION_CHECK */

FR_NAME_NUMBER const fr_tls_status_table[] = {
	{ "invalid",			FR_TLS_INVALID },
	{ "request",			FR_TLS_REQUEST },
	{ "response",			FR_TLS_RESPONSE },
	{ "success",			FR_TLS_SUCCESS },
	{ "fail",			FR_TLS_FAIL },
	{ "noop",			FR_TLS_NOOP },

	{ "start",			FR_TLS_START },
	{ "ok",				FR_TLS_OK },
	{ "ack",			FR_TLS_ACK },
	{ "first fragment",		FR_TLS_FIRST_FRAGMENT },
	{ "more fragments",		FR_TLS_MORE_FRAGMENTS },
	{ "length included",		FR_TLS_LENGTH_INCLUDED },
	{ "more fragments with length",	FR_TLS_MORE_FRAGMENTS_WITH_LENGTH },
	{ "handled",			FR_TLS_HANDLED },
	{  NULL , 			-1},
};

/* index we use to store cached session VPs
 * needs to be dynamic so we can supply a "free" function
 */
int fr_tls_ex_index_vps = -1;
int fr_tls_ex_index_certs = -1;

/* Session */
static void 		session_close(tls_session_t *ssn);
static void 		session_init(tls_session_t *ssn);

/* record */
static void 		record_init(record_t *buf);
static void 		record_close(record_t *buf);
static unsigned int 	record_plus(record_t *buf, void const *ptr,
				    unsigned int size);
static unsigned int 	record_minus(record_t *buf, void *ptr,
				     unsigned int size);

#ifdef PSK_MAX_IDENTITY_LEN
static bool identity_is_safe(const char *identity)
{
	char c;

	if (!identity) return true;

	while ((c = *(identity++)) != '\0') {
		if (isalpha((int) c) || isdigit((int) c) || isspace((int) c) ||
		    (c == '@') || (c == '-') || (c == '_') || (c == '.')) {
			continue;
		}

		return false;
	}

	return true;
}


/*
 *	When a client uses TLS-PSK to talk to a server, this callback
 *	is used by the server to determine the PSK to use.
 */
static unsigned int psk_server_callback(SSL *ssl, const char *identity,
					unsigned char *psk,
					unsigned int max_psk_len)
{
	unsigned int psk_len = 0;
	fr_tls_server_conf_t *conf;
	REQUEST *request;

	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
						       FR_TLS_EX_INDEX_CONF);
	if (!conf) return 0;

	request = (REQUEST *)SSL_get_ex_data(ssl,
					     FR_TLS_EX_INDEX_REQUEST);
	if (request && conf->psk_query) {
		size_t hex_len;
		VALUE_PAIR *vp;
		char buffer[2 * PSK_MAX_PSK_LEN + 4]; /* allow for too-long keys */

		/*
		 *	The passed identity is weird.  Deny it.
		 */
		if (!identity_is_safe(identity)) {
			RWDEBUG("Invalid characters in PSK identity %s", identity);
			return 0;
		}

		vp = pairmake_packet("TLS-PSK-Identity", identity, T_OP_SET);
		if (!vp) return 0;

		hex_len = radius_xlat(buffer, sizeof(buffer), request, conf->psk_query,
				      NULL, NULL);
		if (!hex_len) {
			RWDEBUG("PSK expansion returned an empty string.");
			return 0;
		}

		/*
		 *	The returned key is truncated at MORE than
		 *	OpenSSL can handle.  That way we can detect
		 *	the truncation, and complain about it.
		 */
		if (hex_len > (2 * max_psk_len)) {
			RWDEBUG("Returned PSK is too long (%u > %u)",
				(unsigned int) hex_len, 2 * max_psk_len);
			return 0;
		}

		/*
		 *	Leave the TLS-PSK-Identity in the request, and
		 *	convert the expansion from printable string
		 *	back to hex.
		 */
		return fr_hex2bin(psk, max_psk_len, buffer, hex_len);
	}

	if (!conf->psk_identity) {
		DEBUG("No static PSK identity set.  Rejecting the user");
		return 0;
	}

	/*
	 *	No REQUEST, or no dynamic query.  Just look for a
	 *	static identity.
	 */
	if (strcmp(identity, conf->psk_identity) != 0) {
		ERROR("Supplied PSK identity %s does not match configuration.  Rejecting.",
		      identity);
		return 0;
	}

	psk_len = strlen(conf->psk_password);
	if (psk_len > (2 * max_psk_len)) return 0;

	return fr_hex2bin(psk, max_psk_len, conf->psk_password, psk_len);
}

static unsigned int psk_client_callback(SSL *ssl, UNUSED char const *hint,
					char *identity, unsigned int max_identity_len,
					unsigned char *psk, unsigned int max_psk_len)
{
	unsigned int psk_len;
	fr_tls_server_conf_t *conf;

	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
						       FR_TLS_EX_INDEX_CONF);
	if (!conf) return 0;

	psk_len = strlen(conf->psk_password);
	if (psk_len > (2 * max_psk_len)) return 0;

	strlcpy(identity, conf->psk_identity, max_identity_len);

	return fr_hex2bin(psk, max_psk_len, conf->psk_password, psk_len);
}

#endif

static int _tls_session_free(tls_session_t *ssn)
{
	/*
	 *	Free any opaque TTLS or PEAP data.
	 */
	if ((ssn->opaque) && (ssn->free_opaque)) {
		ssn->free_opaque(ssn->opaque);
		ssn->opaque = NULL;
	}

	session_close(ssn);

	return 0;
}

tls_session_t *tls_new_client_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, int fd)
{
	int verify_mode;
	tls_session_t *ssn = NULL;
	REQUEST *request;

	ssn = talloc_zero(ctx, tls_session_t);
	if (!ssn) return NULL;

	talloc_set_destructor(ssn, _tls_session_free);

	ssn->ctx = conf->ctx;

	SSL_CTX_set_mode(ssn->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY);

	ssn->ssl = SSL_new(ssn->ctx);
	if (!ssn->ssl) {
		talloc_free(ssn);
		return NULL;
	}

	request = request_alloc(ssn);
	SSL_set_ex_data(ssn->ssl, FR_TLS_EX_INDEX_REQUEST, (void *)request);

	/*
	 *	Add the message callback to identify what type of
	 *	message/handshake is passed
	 */
	SSL_set_msg_callback(ssn->ssl, cbtls_msg);
	SSL_set_msg_callback_arg(ssn->ssl, ssn);
	SSL_set_info_callback(ssn->ssl, cbtls_info);

	/*
	 *	Always verify the peer certificate.
	 */
	DEBUG2("Requiring Server certificate");
	verify_mode = SSL_VERIFY_PEER;
	verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
	SSL_set_verify(ssn->ssl, verify_mode, cbtls_verify);

	SSL_set_ex_data(ssn->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
	SSL_set_ex_data(ssn->ssl, FR_TLS_EX_INDEX_SSN, (void *)ssn);
	SSL_set_fd(ssn->ssl, fd);
	if (SSL_connect(ssn->ssl) <= 0) {
		int err;
		while ((err = ERR_get_error())) {
			ERROR("tls: %s", ERR_error_string(err, NULL));
		}
		talloc_free(ssn);

		return NULL;
	}

	ssn->mtu = conf->fragment_size;

	return ssn;
}


/** Create a new TLS session
 *
 * Configures a new TLS session, configuring options, setting callbacks etc...
 *
 * @param ctx to alloc session data in. Should usually be NULL unless the lifetime of the
 *	session is tied to another talloc'd object.
 * @param conf to use to configure the tls session.
 * @param request The current #REQUEST.
 * @param client_cert Whether to require a client_cert.
 * @return a new session on success, or NULL on error.
 */
tls_session_t *tls_new_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, REQUEST *request, bool client_cert)
{
	tls_session_t	*state = NULL;
	SSL		*new_tls = NULL;
	int		verify_mode = 0;
	VALUE_PAIR	*vp;

	rad_assert(request != NULL);

	RDEBUG2("Initiating new EAP-TLS session");

	/*
	 *	Manually flush the sessions every so often.  If HALF
	 *	of the session lifetime has passed since we last
	 *	flushed, then flush it again.
	 *
	 *	FIXME: Also do it every N sessions?
	 */
	if (conf->session_cache_enable &&
	    ((conf->session_last_flushed + ((int)conf->session_timeout * 1800)) <= request->timestamp)){
		RDEBUG2("Flushing SSL sessions (of #%ld)", SSL_CTX_sess_number(conf->ctx));

		SSL_CTX_flush_sessions(conf->ctx, request->timestamp);
		conf->session_last_flushed = request->timestamp;
	}

	if ((new_tls = SSL_new(conf->ctx)) == NULL) {
		RERROR("Error creating new SSL session: %s", ERR_error_string(ERR_get_error(), NULL));
		return NULL;
	}

	/* We use the SSL's "app_data" to indicate a call-back */
	SSL_set_app_data(new_tls, NULL);

	if ((state = talloc_zero(ctx, tls_session_t)) == NULL) {
		RERROR("Error allocating memory for SSL state");
		return NULL;
	}
	session_init(state);
	talloc_set_destructor(state, _tls_session_free);

	state->ctx = conf->ctx;
	state->ssl = new_tls;

	/*
	 *	Initialize callbacks
	 */
	state->record_init = record_init;
	state->record_close = record_close;
	state->record_plus = record_plus;
	state->record_minus = record_minus;

	/*
	 *	Create & hook the BIOs to handle the dirty side of the
	 *	SSL.  This is *very important* as we want to handle
	 *	the transmission part.  Now the only IO interface
	 *	that SSL is aware of, is our defined BIO buffers.
	 *
	 *	This means that all SSL IO is done to/from memory,
	 *	and we can update those BIOs from the packets we've
	 *	received.
	 */
	state->into_ssl = BIO_new(BIO_s_mem());
	state->from_ssl = BIO_new(BIO_s_mem());
	SSL_set_bio(state->ssl, state->into_ssl, state->from_ssl);

	/*
	 *	Add the message callback to identify what type of
	 *	message/handshake is passed
	 */
	SSL_set_msg_callback(new_tls, cbtls_msg);
	SSL_set_msg_callback_arg(new_tls, state);
	SSL_set_info_callback(new_tls, cbtls_info);

	/*
	 *	In Server mode we only accept.
	 */
	SSL_set_accept_state(state->ssl);

	/*
	 *	Verify the peer certificate, if asked.
	 */
	if (client_cert) {
		RDEBUG2("Setting verify mode to require certificate from client");
		verify_mode = SSL_VERIFY_PEER;
		verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
		verify_mode |= SSL_VERIFY_CLIENT_ONCE;
	}
	SSL_set_verify(state->ssl, verify_mode, cbtls_verify);

	SSL_set_ex_data(state->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
	SSL_set_ex_data(state->ssl, FR_TLS_EX_INDEX_SSN, (void *)state);
	state->length_flag = conf->include_length;

	/*
	 *	We use default fragment size, unless the Framed-MTU
	 *	tells us it's too big.  Note that we do NOT account
	 *	for the EAP-TLS headers if conf->fragment_size is
	 *	large, because that config item looks to be confusing.
	 *
	 *	i.e. it should REALLY be called MTU, and the code here
	 *	should figure out what that means for TLS fragment size.
	 *	asking the administrator to know the internal details
	 *	of EAP-TLS in order to calculate fragment sizes is
	 *	just too much.
	 */
	state->mtu = conf->fragment_size;
	vp = pairfind(request->packet->vps, PW_FRAMED_MTU, 0, TAG_ANY);
	if (vp && (vp->vp_integer > 100) && (vp->vp_integer < state->mtu)) {
		state->mtu = vp->vp_integer;
	}

	if (conf->session_cache_enable) state->allow_session_resumption = true; /* otherwise it's false */

	return state;
}

/*
 *	Print out some text describing the error.
 */
static int int_ssl_check(REQUEST *request, SSL *s, int ret, char const *text)
{
	int e;
	unsigned long l;

	if ((l = ERR_get_error()) != 0) {
		char const *p = ERR_error_string(l, NULL);

		if (p) ROPTIONAL(REDEBUG, ERROR, "SSL says: %s", p);
	}

	e = SSL_get_error(s, ret);
	switch (e) {
	/*
	 *	These seem to be harmless and already "dealt
	 *	with" by our non-blocking environment. NB:
	 *	"ZERO_RETURN" is the clean "error"
	 *	indicating a successfully closed SSL
	 *	tunnel. We let this happen because our IO
	 *	loop should not appear to have broken on
	 *	this condition - and outside the IO loop, the
	 *	"shutdown" state is checked.
	 *
	 *	Don't print anything if we ignore the error.
	 */
	case SSL_ERROR_NONE:
	case SSL_ERROR_WANT_READ:
	case SSL_ERROR_WANT_WRITE:
	case SSL_ERROR_WANT_X509_LOOKUP:
	case SSL_ERROR_ZERO_RETURN:
		break;

	/*
	 *	These seem to be indications of a genuine
	 *	error that should result in the SSL tunnel
	 *	being regarded as "dead".
	 */
	case SSL_ERROR_SYSCALL:
		ROPTIONAL(REDEBUG, ERROR, "%s failed in a system call (%d), TLS session failed", text, ret);
		return 0;

	case SSL_ERROR_SSL:
		ROPTIONAL(REDEBUG, ERROR, "%s failed inside of TLS (%d), TLS session failed", text, ret);
		return 0;

	/*
	 *	For any other errors that (a) exist, and (b)
	 *	crop up - we need to interpret what to do with
	 *	them - so "politely inform" the caller that
	 *	the code needs updating here.
	 */
	default:
		ROPTIONAL(REDEBUG, ERROR, "FATAL SSL error: %d", e);
		return 0;
	}

	return 1;
}

/*
 * We are the server, we always get the dirty data
 * (Handshake data is also considered as dirty data)
 * During handshake, since SSL API handles itself,
 * After clean-up, dirty_out will be filled with
 * the data required for handshaking. So we check
 * if dirty_out is empty then we simply send it back.
 * As of now, if handshake is successful, then we keep going,
 * otherwise we fail.
 *
 * Fill the Bio with the dirty data to clean it
 * Get the cleaned data from SSL, if it is not Handshake data
 */
int tls_handshake_recv(REQUEST *request, tls_session_t *ssn)
{
	int err;

	if (ssn->invalid_hb_used) return 0;

	err = BIO_write(ssn->into_ssl, ssn->dirty_in.data, ssn->dirty_in.used);
	if (err != (int) ssn->dirty_in.used) {
		REDEBUG("Failed writing %zd bytes to SSL BIO: %d", ssn->dirty_in.used, err);
		record_init(&ssn->dirty_in);
		return 0;
	}
	record_init(&ssn->dirty_in);

	err = SSL_read(ssn->ssl, ssn->clean_out.data + ssn->clean_out.used,
		       sizeof(ssn->clean_out.data) - ssn->clean_out.used);
	if (err > 0) {
		ssn->clean_out.used += err;
		return 1;
	}

	if (!int_ssl_check(request, ssn->ssl, err, "SSL_read")) return 0;

	/* Some Extra STATE information for easy debugging */
	if (SSL_is_init_finished(ssn->ssl)) RDEBUG2("SSL Connection Established");
	if (SSL_in_init(ssn->ssl)) RDEBUG2("In SSL Handshake Phase");
	if (SSL_in_before(ssn->ssl)) RDEBUG2("Before SSL Handshake Phase");
	if (SSL_in_accept_init(ssn->ssl)) RDEBUG2("In SSL Accept mode");
	if (SSL_in_connect_init(ssn->ssl)) RDEBUG2("In SSL Connect mode");

	err = BIO_ctrl_pending(ssn->from_ssl);
	if (err > 0) {
		err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
			       sizeof(ssn->dirty_out.data));
		if (err > 0) {
			ssn->dirty_out.used = err;

		} else if (BIO_should_retry(ssn->from_ssl)) {
			record_init(&ssn->dirty_in);
			RDEBUG2("Asking for more data in tunnel");
			return 1;

		} else {
			int_ssl_check(request, ssn->ssl, err, "BIO_read");
			record_init(&ssn->dirty_in);
			return 0;
		}
	} else {
		RDEBUG2("SSL Application Data");
		/* Its clean application data, do whatever we want */
		record_init(&ssn->clean_out);
	}

	/* We are done with dirty_in, reinitialize it */
	record_init(&ssn->dirty_in);
	return 1;
}

/*
 *	Take cleartext user data, and encrypt it into the output buffer,
 *	to send to the client at the other end of the SSL connection.
 */
int tls_handshake_send(REQUEST *request, tls_session_t *ssn)
{
	int err;

	/*
	 *	If there's un-encrypted data in 'clean_in', then write
	 *	that data to the SSL session, and then call the BIO function
	 *	to get that encrypted data from the SSL session, into
	 *	a buffer which we can then package into an EAP packet.
	 *
	 *	Based on Server's logic this clean_in is expected to
	 *	contain the data to send to the client.
	 */
	if (ssn->clean_in.used > 0) {
		int written;

		written = SSL_write(ssn->ssl, ssn->clean_in.data, ssn->clean_in.used);
		record_minus(&ssn->clean_in, NULL, written);

		/* Get the dirty data from Bio to send it */
		err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
			       sizeof(ssn->dirty_out.data));
		if (err > 0) {
			ssn->dirty_out.used = err;
		} else {
			int_ssl_check(request, ssn->ssl, err, "handshake_send");
		}
	}

	return 1;
}

static void session_init(tls_session_t *ssn)
{
	ssn->ssl = NULL;
	ssn->into_ssl = ssn->from_ssl = NULL;
	record_init(&ssn->clean_in);
	record_init(&ssn->clean_out);
	record_init(&ssn->dirty_in);
	record_init(&ssn->dirty_out);

	memset(&ssn->info, 0, sizeof(ssn->info));

	ssn->mtu = 0;
	ssn->fragment = false;
	ssn->tls_msg_len = 0;
	ssn->length_flag = false;
	ssn->opaque = NULL;
	ssn->free_opaque = NULL;
}

static void session_close(tls_session_t *ssn)
{
	SSL_set_quiet_shutdown(ssn->ssl, 1);
	SSL_shutdown(ssn->ssl);

	if (ssn->ssl) {
		SSL_free(ssn->ssl);
		ssn->ssl = NULL;
	}

	record_close(&ssn->clean_in);
	record_close(&ssn->clean_out);
	record_close(&ssn->dirty_in);
	record_close(&ssn->dirty_out);
	session_init(ssn);
}

static void record_init(record_t *rec)
{
	rec->used = 0;
}

static void record_close(record_t *rec)
{
	rec->used = 0;
}


/*
 *	Copy data to the intermediate buffer, before we send
 *	it somewhere.
 */
static unsigned int record_plus(record_t *rec, void const *ptr,
				unsigned int size)
{
	unsigned int added = MAX_RECORD_SIZE - rec->used;

	if(added > size)
		added = size;
	if(added == 0)
		return 0;
	memcpy(rec->data + rec->used, ptr, added);
	rec->used += added;
	return added;
}

/*
 *	Take data from the buffer, and give it to the caller.
 */
static unsigned int record_minus(record_t *rec, void *ptr,
				 unsigned int size)
{
	unsigned int taken = rec->used;

	if(taken > size)
		taken = size;
	if(taken == 0)
		return 0;
	if(ptr)
		memcpy(ptr, rec->data, taken);
	rec->used -= taken;

	/*
	 *	This is pretty bad...
	 */
	if(rec->used > 0)
		memmove(rec->data, rec->data + taken, rec->used);
	return taken;
}

void tls_session_information(tls_session_t *tls_session)
{
	char const *str_write_p, *str_version, *str_content_type = "";
	char const *str_details1 = "", *str_details2= "";
	REQUEST *request;
	char buffer[32];

	/*
	 *	Don't print this out in the normal course of
	 *	operations.
	 */
	if (rad_debug_lvl == 0) {
		return;
	}

	str_write_p = tls_session->info.origin ? ">>>" : "<<<";

	switch (tls_session->info.version) {
	case SSL2_VERSION:
		str_version = "SSL 2.0 ";
		break;
	case SSL3_VERSION:
		str_version = "SSL 3.0 ";
		break;
	case TLS1_VERSION:
		str_version = "TLS 1.0 ";
		break;
#ifdef TLS1_1_VERSION
	case TLS1_1_VERSION:
		str_version = "TLS 1.1 ";
		break;
#endif
#ifdef TLS1_2_VERSION
	case TLS1_2_VERSION:
		str_version = "TLS 1.2 ";
		break;
#endif
#ifdef TLS1_3_VERSON
	case TLS1_3_VERSION:
		str_version = "TLS 1.3 ";
		break;
#endif

	default:
		sprintf(buffer, "UNKNOWN TLS VERSION ?%04X?", tls_session->info.version);
		str_version = buffer;
		break;
	}

	if (tls_session->info.version == SSL3_VERSION ||
	    tls_session->info.version == TLS1_VERSION) {
		switch (tls_session->info.content_type) {
		case SSL3_RT_CHANGE_CIPHER_SPEC:
			str_content_type = "ChangeCipherSpec";
			break;

		case SSL3_RT_ALERT:
			str_content_type = "Alert";
			break;

		case SSL3_RT_HANDSHAKE:
			str_content_type = "Handshake";
			break;

		case SSL3_RT_APPLICATION_DATA:
			str_content_type = "ApplicationData";
			break;

		default:
			str_content_type = "UnknownContentType";
			break;
		}

		if (tls_session->info.content_type == SSL3_RT_ALERT) {
			str_details1 = ", ???";

			if (tls_session->info.record_len == 2) {

				switch (tls_session->info.alert_level) {
				case SSL3_AL_WARNING:
					str_details1 = ", warning";
					break;
				case SSL3_AL_FATAL:
					str_details1 = ", fatal";
					break;
				}

				str_details2 = " ???";
				switch (tls_session->info.alert_description) {
				case SSL3_AD_CLOSE_NOTIFY:
					str_details2 = " close_notify";
					break;

				case SSL3_AD_UNEXPECTED_MESSAGE:
					str_details2 = " unexpected_message";
					break;

				case SSL3_AD_BAD_RECORD_MAC:
					str_details2 = " bad_record_mac";
					break;

				case TLS1_AD_DECRYPTION_FAILED:
					str_details2 = " decryption_failed";
					break;

				case TLS1_AD_RECORD_OVERFLOW:
					str_details2 = " record_overflow";
					break;

				case SSL3_AD_DECOMPRESSION_FAILURE:
					str_details2 = " decompression_failure";
					break;

				case SSL3_AD_HANDSHAKE_FAILURE:
					str_details2 = " handshake_failure";
					break;

				case SSL3_AD_BAD_CERTIFICATE:
					str_details2 = " bad_certificate";
					break;

				case SSL3_AD_UNSUPPORTED_CERTIFICATE:
					str_details2 = " unsupported_certificate";
					break;

				case SSL3_AD_CERTIFICATE_REVOKED:
					str_details2 = " certificate_revoked";
					break;

				case SSL3_AD_CERTIFICATE_EXPIRED:
					str_details2 = " certificate_expired";
					break;

				case SSL3_AD_CERTIFICATE_UNKNOWN:
					str_details2 = " certificate_unknown";
					break;

				case SSL3_AD_ILLEGAL_PARAMETER:
					str_details2 = " illegal_parameter";
					break;

				case TLS1_AD_UNKNOWN_CA:
					str_details2 = " unknown_ca";
					break;

				case TLS1_AD_ACCESS_DENIED:
					str_details2 = " access_denied";
					break;

				case TLS1_AD_DECODE_ERROR:
					str_details2 = " decode_error";
					break;

				case TLS1_AD_DECRYPT_ERROR:
					str_details2 = " decrypt_error";
					break;

				case TLS1_AD_EXPORT_RESTRICTION:
					str_details2 = " export_restriction";
					break;

				case TLS1_AD_PROTOCOL_VERSION:
					str_details2 = " protocol_version";
					break;

				case TLS1_AD_INSUFFICIENT_SECURITY:
					str_details2 = " insufficient_security";
					break;

				case TLS1_AD_INTERNAL_ERROR:
					str_details2 = " internal_error";
					break;

				case TLS1_AD_USER_CANCELLED:
					str_details2 = " user_canceled";
					break;

				case TLS1_AD_NO_RENEGOTIATION:
					str_details2 = " no_renegotiation";
					break;
				}
			}
		}

		if (tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
			str_details1 = "???";

			if (tls_session->info.record_len > 0) switch (tls_session->info.handshake_type) {
			case SSL3_MT_HELLO_REQUEST:
				str_details1 = ", HelloRequest";
				break;

			case SSL3_MT_CLIENT_HELLO:
				str_details1 = ", ClientHello";
				break;

			case SSL3_MT_SERVER_HELLO:
				str_details1 = ", ServerHello";
				break;

			case SSL3_MT_CERTIFICATE:
				str_details1 = ", Certificate";
				break;

			case SSL3_MT_SERVER_KEY_EXCHANGE:
				str_details1 = ", ServerKeyExchange";
				break;

			case SSL3_MT_CERTIFICATE_REQUEST:
				str_details1 = ", CertificateRequest";
				break;

			case SSL3_MT_SERVER_DONE:
				str_details1 = ", ServerHelloDone";
				break;

			case SSL3_MT_CERTIFICATE_VERIFY:
				str_details1 = ", CertificateVerify";
				break;

			case SSL3_MT_CLIENT_KEY_EXCHANGE:
				str_details1 = ", ClientKeyExchange";
				break;

			case SSL3_MT_FINISHED:
				str_details1 = ", Finished";
				break;
			}
		}
	}

	snprintf(tls_session->info.info_description,
		 sizeof(tls_session->info.info_description),
		 "%s %s%s [length %04lx]%s%s\n",
		 str_write_p, str_version, str_content_type,
		 (unsigned long)tls_session->info.record_len,
		 str_details1, str_details2);

	request = SSL_get_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_REQUEST);
	if (request) {
		RDEBUG2("%s", tls_session->info.info_description);
	} else {
		DEBUG2("%s", tls_session->info.info_description);
	}
}

static CONF_PARSER cache_config[] = {
	{ "enable", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, session_cache_enable), "no" },

	{ "lifetime", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, session_timeout), "24" },
	{ "name", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, session_id_name), NULL },

	{ "max_entries", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, session_cache_size), "255" },
	{ "persist_dir", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, session_cache_path), NULL },
	{ NULL, -1, 0, NULL, NULL }	   /* end the list */
};

static CONF_PARSER verify_config[] = {
	{ "tmpdir", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, verify_tmp_dir), NULL },
	{ "client", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, verify_client_cert_cmd), NULL },
	{ NULL, -1, 0, NULL, NULL }	   /* end the list */
};

#ifdef HAVE_OPENSSL_OCSP_H
static CONF_PARSER ocsp_config[] = {
	{ "enable", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, ocsp_enable), "no" },
	{ "override_cert_url", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, ocsp_override_url), "no" },
	{ "url", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, ocsp_url), NULL },
	{ "use_nonce", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, ocsp_use_nonce), "yes" },
	{ "timeout", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, ocsp_timeout), "yes" },
	{ "softfail", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, ocsp_softfail), "no" },
	{ NULL, -1, 0, NULL, NULL }	   /* end the list */
};
#endif

static CONF_PARSER tls_server_config[] = {
	{ "rsa_key_exchange", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, rsa_key), "no" },
	{ "dh_key_exchange", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, dh_key), "yes" },
	{ "rsa_key_length", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, rsa_key_length), "512" },
	{ "dh_key_length", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, dh_key_length), "512" },
	{ "verify_depth", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, verify_depth), "0" },
	{ "CA_path", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, fr_tls_server_conf_t, ca_path), NULL },
	{ "ca_path", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, ca_path), NULL },
	{ "pem_file_type", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, file_type), "yes" },
	{ "private_key_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, private_key_file), NULL },
	{ "certificate_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, certificate_file), NULL },
	{ "CA_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, fr_tls_server_conf_t, ca_file), NULL },
	{ "ca_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, ca_file), NULL },
	{ "private_key_password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, fr_tls_server_conf_t, private_key_password), NULL },
#ifdef PSK_MAX_IDENTITY_LEN
	{ "psk_identity", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, psk_identity), NULL },
	{ "psk_hexphrase", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, fr_tls_server_conf_t, psk_password), NULL },
	{ "psk_query", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, psk_query), NULL },
#endif
	{ "dh_file", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, dh_file), NULL },
	{ "random_file", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, random_file), NULL },
	{ "fragment_size", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, fragment_size), "1024" },
	{ "include_length", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, include_length), "yes" },
	{ "check_crl", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, check_crl), "no" },
#ifdef X509_V_FLAG_CRL_CHECK_ALL
	{ "check_all_crl", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, check_all_crl), "no" },
#endif
	{ "allow_expired_crl", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, allow_expired_crl), NULL },
	{ "check_cert_cn", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, check_cert_cn), NULL },
	{ "cipher_list", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, cipher_list), NULL },
	{ "check_cert_issuer", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, check_cert_issuer), NULL },
	{ "require_client_cert", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, require_client_cert), NULL },

#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
	{ "ecdh_curve", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, ecdh_curve), "prime256v1" },
#endif
#endif

#ifdef SSL_OP_NO_TLSv1
	{ "disable_tlsv1", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1), NULL },
#endif

#ifdef SSL_OP_NO_TLSv1_1
	{ "disable_tlsv1_1", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1_1), NULL },
#endif

#ifdef SSL_OP_NO_TLSv1_2
	{ "disable_tlsv1_2", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1_2), NULL },
#endif

	{ "cache", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) cache_config },

	{ "verify", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) verify_config },

#ifdef HAVE_OPENSSL_OCSP_H
	{ "ocsp", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) ocsp_config },
#endif

	{ NULL, -1, 0, NULL, NULL }	   /* end the list */
};


static CONF_PARSER tls_client_config[] = {
	{ "rsa_key_exchange", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, rsa_key), "no" },
	{ "dh_key_exchange", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, dh_key), "yes" },
	{ "rsa_key_length", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, rsa_key_length), "512" },
	{ "dh_key_length", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, dh_key_length), "512" },
	{ "verify_depth", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, verify_depth), "0" },
	{ "ca_path", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, ca_path), NULL },
	{ "pem_file_type", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, file_type), "yes" },
	{ "private_key_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, private_key_file), NULL },
	{ "certificate_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, certificate_file), NULL },
	{ "ca_file", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT, fr_tls_server_conf_t, ca_file), NULL },
	{ "private_key_password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, fr_tls_server_conf_t, private_key_password), NULL },
	{ "dh_file", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, dh_file), NULL },
	{ "random_file", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, random_file), NULL },
	{ "fragment_size", FR_CONF_OFFSET(PW_TYPE_INTEGER, fr_tls_server_conf_t, fragment_size), "1024" },
	{ "include_length", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, include_length), "yes" },
	{ "check_crl", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, check_crl), "no" },
	{ "check_cert_cn", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, check_cert_cn), NULL },
	{ "cipher_list", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, cipher_list), NULL },
	{ "check_cert_issuer", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, check_cert_issuer), NULL },

#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
	{ "ecdh_curve", FR_CONF_OFFSET(PW_TYPE_STRING, fr_tls_server_conf_t, ecdh_curve), "prime256v1" },
#endif
#endif

#ifdef SSL_OP_NO_TLSv1
	{ "disable_tlsv1", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1), NULL },
#endif

#ifdef SSL_OP_NO_TLSv1_1
	{ "disable_tlsv1_1", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1_1), NULL },
#endif

#ifdef SSL_OP_NO_TLSv1_2
	{ "disable_tlsv1_2", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, fr_tls_server_conf_t, disable_tlsv1_2), NULL },
#endif

	{ NULL, -1, 0, NULL, NULL }	   /* end the list */
};


/*
 *	TODO: Check for the type of key exchange * like conf->dh_key
 */
static int load_dh_params(SSL_CTX *ctx, char *file)
{
	DH *dh = NULL;
	BIO *bio;

	if (!file) return 0;

	if ((bio = BIO_new_file(file, "r")) == NULL) {
		ERROR(LOG_PREFIX ": Unable to open DH file - %s", file);
		return -1;
	}

	dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
	BIO_free(bio);
	if (!dh) {
		WARN(LOG_PREFIX ": Unable to set DH parameters.  DH cipher suites may not work!");
		WARN(LOG_PREFIX ": Fix this by running the OpenSSL command listed in eap.conf");
		return 0;
	}

	if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
		ERROR(LOG_PREFIX ": Unable to set DH parameters");
		DH_free(dh);
		return -1;
	}

	DH_free(dh);
	return 0;
}


/*
 *	Print debugging messages, and free data.
 */
#define MAX_SESSION_SIZE (256)

static void cbtls_remove_session(SSL_CTX *ctx, SSL_SESSION *sess)
{
	size_t			size;
	char			buffer[2 * MAX_SESSION_SIZE + 1];
	fr_tls_server_conf_t	*conf;

	size = sess->session_id_length;
	if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;

	fr_bin2hex(buffer, sess->session_id, size);

	conf = (fr_tls_server_conf_t *)SSL_CTX_get_app_data(ctx);
	if (!conf) {
		DEBUG(LOG_PREFIX ": Failed to find TLS configuration in session");
		return;
	}

	{
		int rv;
		char filename[256];

		DEBUG2(LOG_PREFIX ": Removing session %s from the cache", buffer);

		/* remove session and any cached VPs */
		snprintf(filename, sizeof(filename), "%s%c%s.asn1",
			 conf->session_cache_path, FR_DIR_SEP, buffer);
		rv = unlink(filename);
		if (rv != 0) {
			DEBUG2(LOG_PREFIX ": Could not remove persisted session file %s: %s",
			       filename, fr_syserror(errno));
		}
		/* VPs might be absent; might not have been written to disk yet */
		snprintf(filename, sizeof(filename), "%s%c%s.vps",
			 conf->session_cache_path, FR_DIR_SEP, buffer);
		unlink(filename);
	}

	return;
}

static int cbtls_new_session(SSL *ssl, SSL_SESSION *sess)
{
	size_t			size;
	char			buffer[2 * MAX_SESSION_SIZE + 1];
	fr_tls_server_conf_t	*conf;
	unsigned char		*sess_blob = NULL;

	REQUEST			*request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);

	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
	if (!conf) {
		RWDEBUG("Failed to find TLS configuration in session");
		return 0;
	}

	size = sess->session_id_length;
	if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;

	fr_bin2hex(buffer, sess->session_id, size);

	{
		int fd, rv, todo, blob_len;
		char filename[256];
		unsigned char *p;

		RDEBUG2("Serialising session %s, and storing in cache", buffer);

		/* find out what length data we need */
		blob_len = i2d_SSL_SESSION(sess, NULL);
		if (blob_len < 1) {
			/* something went wrong */
			RWDEBUG("Session serialisation failed, couldn't determine required buffer length");
			return 0;
		}


		/* Do not convert to TALLOC - Thread safety */
		/* alloc and convert to ASN.1 */
		sess_blob = malloc(blob_len);
		if (!sess_blob) {
			RWDEBUG("Session serialisation failed, couldn't allocate buffer (%d bytes)", blob_len);
			return 0;
		}
		/* openssl mutates &p */
		p = sess_blob;
		rv = i2d_SSL_SESSION(sess, &p);
		if (rv != blob_len) {
			RWDEBUG("Session serialisation failed");
			goto error;
		}

		/* open output file */
		snprintf(filename, sizeof(filename), "%s%c%s.asn1",
			 conf->session_cache_path, FR_DIR_SEP, buffer);
		fd = open(filename, O_RDWR|O_CREAT|O_EXCL, 0600);
		if (fd < 0) {
			RERROR("Session serialisation failed, failed opening session file %s: %s",
			      filename, fr_syserror(errno));
			goto error;
		}

		todo = blob_len;
		p = sess_blob;
		while (todo > 0) {
			rv = write(fd, p, todo);
			if (rv < 1) {
				RWDEBUG("Failed writing session: %s", fr_syserror(errno));
				close(fd);
				goto error;
			}
			p += rv;
			todo -= rv;
		}
		close(fd);
		RWDEBUG("Wrote session %s to %s (%d bytes)", buffer, filename, blob_len);
	}

error:
	free(sess_blob);

	return 0;
}

static SSL_SESSION *cbtls_get_session(SSL *ssl, unsigned char *data, int len, int *copy)
{
	size_t			size;
	char			buffer[2 * MAX_SESSION_SIZE + 1];
	fr_tls_server_conf_t	*conf;
	TALLOC_CTX		*talloc_ctx;

	SSL_SESSION		*sess = NULL;
	unsigned char		*sess_data = NULL;
	PAIR_LIST		*pairlist = NULL;

	REQUEST			*request = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);

	rad_assert(request != NULL);

	size = len;
	if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;

	fr_bin2hex(buffer, data, size);

	RDEBUG2("Peer requested cached session: %s", buffer);

	*copy = 0;

	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
	if (!conf) {
		RWDEBUG("Failed to find TLS configuration in session");
		return NULL;
	}

	talloc_ctx = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_TALLOC);

	{
		int		rv, fd, todo;
		char		filename[256];
		unsigned char	*p;
		struct stat	st;
		VALUE_PAIR	*vps = NULL;

		/* read in the cached VPs from the .vps file */
		snprintf(filename, sizeof(filename), "%s%c%s.vps",
			 conf->session_cache_path, FR_DIR_SEP, buffer);
		rv = pairlist_read(talloc_ctx, filename, &pairlist, 1);
		if (rv < 0) {
			/* not safe to un-persist a session w/o VPs */
			RWDEBUG("Failed loading persisted VPs for session %s", buffer);
			goto err;
		}

		/* load the actual SSL session */
		snprintf(filename, sizeof(filename), "%s%c%s.asn1", conf->session_cache_path, FR_DIR_SEP, buffer);
		fd = open(filename, O_RDONLY);
		if (fd < 0) {
			RWDEBUG("No persisted session file %s: %s", filename, fr_syserror(errno));
			goto err;
		}

		rv = fstat(fd, &st);
		if (rv < 0) {
			RWDEBUG("Failed stating persisted session file %s: %s", filename, fr_syserror(errno));
			close(fd);
			goto err;
		}

		sess_data = talloc_array(NULL, unsigned char, st.st_size);
		if (!sess_data) {
			RWDEBUG("Failed allocating buffer for persisted session (%d bytes)", (int) st.st_size);
			close(fd);
			goto err;
		}

		p = sess_data;
		todo = st.st_size;
		while (todo > 0) {
			rv = read(fd, p, todo);
			if (rv < 1) {
				RWDEBUG("Failed reading persisted session: %s", fr_syserror(errno));
				close(fd);
				goto err;
			}
			todo -= rv;
			p += rv;
		}
		close(fd);

		/* openssl mutates &p */
		p = sess_data;
		sess = d2i_SSL_SESSION(NULL, (unsigned char const **)(void **) &p, st.st_size);

		if (!sess) {
			RWDEBUG("Failed loading persisted session: %s", ERR_error_string(ERR_get_error(), NULL));
			goto err;
		}

		/* move the cached VPs into the session */
		pairfilter(talloc_ctx, &vps, &pairlist->reply, 0, 0, TAG_ANY);

		SSL_SESSION_set_ex_data(sess, fr_tls_ex_index_vps, vps);
		RWDEBUG("Successfully restored session %s", buffer);
		rdebug_pair_list(L_DBG_LVL_2, request, vps, "reply:");
	}
err:
	if (sess_data) talloc_free(sess_data);
	if (pairlist) pairlist_free(&pairlist);

	return sess;
}

#ifdef HAVE_OPENSSL_OCSP_H
/*
 * This function extracts the OCSP Responder URL
 * from an existing x509 certificate.
 */
static int ocsp_parse_cert_url(X509 *cert, char **phost, char **pport,
			       char **ppath, int *pssl)
{
	int			i;

	AUTHORITY_INFO_ACCESS	*aia;
	ACCESS_DESCRIPTION	*ad;

	aia = X509_get_ext_d2i(cert, NID_info_access, NULL, NULL);

	for (i = 0; i < sk_ACCESS_DESCRIPTION_num(aia); i++) {
		ad = sk_ACCESS_DESCRIPTION_value(aia, i);
		if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
			if (ad->location->type == GEN_URI) {
			  if(OCSP_parse_url((char *) ad->location->d.ia5->data,
						  phost, pport, ppath, pssl))
					return 1;
			}
		}
	}
	return 0;
}

/*
 * This function sends a OCSP request to a defined OCSP responder
 * and checks the OCSP response for correctness.
 */

/* Maximum leeway in validity period: default 5 minutes */
#define MAX_VALIDITY_PERIOD     (5 * 60)

static int ocsp_check(REQUEST *request, X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
		      fr_tls_server_conf_t *conf)
{
	OCSP_CERTID	*certid;
	OCSP_REQUEST	*req;
	OCSP_RESPONSE	*resp = NULL;
	OCSP_BASICRESP	*bresp = NULL;
	char		*host = NULL;
	char		*port = NULL;
	char		*path = NULL;
	char		hostheader[1024];
	int		use_ssl = -1;
	long		nsec = MAX_VALIDITY_PERIOD, maxage = -1;
	BIO		*cbio, *bio_out;
	int		ocsp_ok = 0;
	int		status;
	ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
	int		reason;
#if OPENSSL_VERSION_NUMBER >= 0x1000003f
	OCSP_REQ_CTX	*ctx;
	int		rc;
	struct timeval	now;
	struct timeval	when;
#endif

	/*
	 * Create OCSP Request
	 */
	certid = OCSP_cert_to_id(NULL, client_cert, issuer_cert);
	req = OCSP_REQUEST_new();
	OCSP_request_add0_id(req, certid);
	if (conf->ocsp_use_nonce) OCSP_request_add1_nonce(req, NULL, 8);

	/*
	 * Send OCSP Request and get OCSP Response
	 */

	/* Get OCSP responder URL */
	if (conf->ocsp_override_url) {
		char *url;

		memcpy(&url, &conf->ocsp_url, sizeof(url));
		/* Reading the libssl src, they do a strdup on the URL, so it could of been const *sigh* */
		OCSP_parse_url(url, &host, &port, &path, &use_ssl);
	} else {
		ocsp_parse_cert_url(client_cert, &host, &port, &path, &use_ssl);
	}

	if (!host || !port || !path) {
		RWDEBUG("ocsp: Host / port / path missing.  Not doing OCSP");
		ocsp_ok = 2;
		goto ocsp_skip;
	}

	RDEBUG2("ocsp: Using responder URL \"http://%s:%s%s\"", host, port, path);

	/* Check host and port length are sane, then create Host: HTTP header */
	if ((strlen(host) + strlen(port) + 2) > sizeof(hostheader)) {
		RWDEBUG("ocsp: Host and port too long");
		goto ocsp_skip;
	}
	snprintf(hostheader, sizeof(hostheader), "%s:%s", host, port);

	/* Setup BIO socket to OCSP responder */
	cbio = BIO_new_connect(host);

	bio_out = NULL;
	if (rad_debug_lvl) {
		if (default_log.dst == L_DST_STDOUT) {
			bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
		} else if (default_log.dst == L_DST_STDERR) {
			bio_out = BIO_new_fp(stderr, BIO_NOCLOSE);
		}
	}

	BIO_set_conn_port(cbio, port);
#if OPENSSL_VERSION_NUMBER < 0x1000003f
	BIO_do_connect(cbio);

	/* Send OCSP request and wait for response */
	resp = OCSP_sendreq_bio(cbio, path, req);
	if (!resp) {
		REDEBUG("ocsp: Couldn't get OCSP response");
		ocsp_ok = 2;
		goto ocsp_end;
	}
#else
	if (conf->ocsp_timeout)
		BIO_set_nbio(cbio, 1);

	rc = BIO_do_connect(cbio);
	if ((rc <= 0) && ((!conf->ocsp_timeout) || !BIO_should_retry(cbio))) {
		REDEBUG("ocsp: Couldn't connect to OCSP responder");
		ocsp_ok = 2;
		goto ocsp_end;
	}

	ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
	if (!ctx) {
		REDEBUG("ocsp: Couldn't create OCSP request");
		ocsp_ok = 2;
		goto ocsp_end;
	}

	if (!OCSP_REQ_CTX_add1_header(ctx, "Host", hostheader)) {
		REDEBUG("ocsp: Couldn't set Host header");
		ocsp_ok = 2;
		goto ocsp_end;
	}

	if (!OCSP_REQ_CTX_set1_req(ctx, req)) {
		REDEBUG("ocsp: Couldn't add data to OCSP request");
		ocsp_ok = 2;
		goto ocsp_end;
	}

	gettimeofday(&when, NULL);
	when.tv_sec += conf->ocsp_timeout;

	do {
		rc = OCSP_sendreq_nbio(&resp, ctx);
		if (conf->ocsp_timeout) {
			gettimeofday(&now, NULL);
			if (!timercmp(&now, &when, <))
				break;
		}
	} while ((rc == -1) && BIO_should_retry(cbio));

	if (conf->ocsp_timeout && (rc == -1) && BIO_should_retry(cbio)) {
		REDEBUG("ocsp: Response timed out");
		ocsp_ok = 2;
		goto ocsp_end;
	}

	OCSP_REQ_CTX_free(ctx);

	if (rc == 0) {
		REDEBUG("ocsp: Couldn't get OCSP response");
		ocsp_ok = 2;
		goto ocsp_end;
	}
#endif

	/* Verify OCSP response status */
	status = OCSP_response_status(resp);
	if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
		REDEBUG("ocsp: Response status: %s", OCSP_response_status_str(status));
		goto ocsp_end;
	}
	bresp = OCSP_response_get1_basic(resp);
	if (conf->ocsp_use_nonce && OCSP_check_nonce(req, bresp)!=1) {
		REDEBUG("ocsp: Response has wrong nonce value");
		goto ocsp_end;
	}
	if (OCSP_basic_verify(bresp, NULL, store, 0)!=1){
		REDEBUG("ocsp: Couldn't verify OCSP basic response");
		goto ocsp_end;
	}

	/*	Verify OCSP cert status */
	if (!OCSP_resp_find_status(bresp, certid, &status, &reason, &rev, &thisupd, &nextupd)) {
		REDEBUG("ocsp: No Status found");
		goto ocsp_end;
	}

	if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
		if (bio_out) {
			BIO_puts(bio_out, "WARNING: Status times invalid.\n");
			ERR_print_errors(bio_out);
		}
		goto ocsp_end;
	}

	if (bio_out) {
		BIO_puts(bio_out, "\tThis Update: ");
		ASN1_GENERALIZEDTIME_print(bio_out, thisupd);
		BIO_puts(bio_out, "\n");
		if (nextupd) {
			BIO_puts(bio_out, "\tNext Update: ");
			ASN1_GENERALIZEDTIME_print(bio_out, nextupd);
			BIO_puts(bio_out, "\n");
		}
	}

	switch (status) {
	case V_OCSP_CERTSTATUS_GOOD:
		RDEBUG2("ocsp: Cert status: good");
		ocsp_ok = 1;
		break;

	default:
		/* REVOKED / UNKNOWN */
		REDEBUG("ocsp: Cert status: %s", OCSP_cert_status_str(status));
		if (reason != -1) REDEBUG("ocsp: Reason: %s", OCSP_crl_reason_str(reason));

		if (bio_out) {
			BIO_puts(bio_out, "\tRevocation Time: ");
			ASN1_GENERALIZEDTIME_print(bio_out, rev);
			BIO_puts(bio_out, "\n");
		}
		break;
	}

ocsp_end:
	/* Free OCSP Stuff */
	OCSP_REQUEST_free(req);
	OCSP_RESPONSE_free(resp);
	free(host);
	free(port);
	free(path);
	BIO_free_all(cbio);
	if (bio_out) BIO_free(bio_out);
	OCSP_BASICRESP_free(bresp);

 ocsp_skip:
	switch (ocsp_ok) {
	case 1:
		RDEBUG2("ocsp: Certificate is valid");
		break;

	case 2:
		if (conf->ocsp_softfail) {
			RWDEBUG("ocsp: Unable to check certificate, assuming it's valid");
			RWDEBUG("ocsp: This may be insecure");
			ocsp_ok = 1;
		} else {
			REDEBUG("ocsp: Unable to check certificate, failing");
			ocsp_ok = 0;
		}
		break;

	default:
		REDEBUG("ocsp: Certificate has been expired/revoked");
		break;
	}

	return ocsp_ok;
}
#endif	/* HAVE_OPENSSL_OCSP_H */

/*
 *	For creating certificate attributes.
 */
static char const *cert_attr_names[8][2] = {
	{ "TLS-Client-Cert-Serial",			"TLS-Cert-Serial" },
	{ "TLS-Client-Cert-Expiration",			"TLS-Cert-Expiration" },
	{ "TLS-Client-Cert-Subject",			"TLS-Cert-Subject" },
	{ "TLS-Client-Cert-Issuer",			"TLS-Cert-Issuer" },
	{ "TLS-Client-Cert-Common-Name",		"TLS-Cert-Common-Name" },
	{ "TLS-Client-Cert-Subject-Alt-Name-Email",	"TLS-Cert-Subject-Alt-Name-Email" },
	{ "TLS-Client-Cert-Subject-Alt-Name-Dns",	"TLS-Cert-Subject-Alt-Name-Dns" },
	{ "TLS-Client-Cert-Subject-Alt-Name-Upn",	"TLS-Cert-Subject-Alt-Name-Upn" }
};

#define FR_TLS_SERIAL		(0)
#define FR_TLS_EXPIRATION	(1)
#define FR_TLS_SUBJECT		(2)
#define FR_TLS_ISSUER		(3)
#define FR_TLS_CN		(4)
#define FR_TLS_SAN_EMAIL       	(5)
#define FR_TLS_SAN_DNS          (6)
#define FR_TLS_SAN_UPN          (7)

/*
 *	Before trusting a certificate, you must make sure that the
 *	certificate is 'valid'. There are several steps that your
 *	application can take in determining if a certificate is
 *	valid. Commonly used steps are:
 *
 *	1.Verifying the certificate's signature, and verifying that
 *	the certificate has been issued by a trusted Certificate
 *	Authority.
 *
 *	2.Verifying that the certificate is valid for the present date
 *	(i.e. it is being presented within its validity dates).
 *
 *	3.Verifying that the certificate has not been revoked by its
 *	issuing Certificate Authority, by checking with respect to a
 *	Certificate Revocation List (CRL).
 *
 *	4.Verifying that the credentials presented by the certificate
 *	fulfill additional requirements specific to the application,
 *	such as with respect to access control lists or with respect
 *	to OCSP (Online Certificate Status Processing).
 *
 *	NOTE: This callback will be called multiple times based on the
 *	depth of the root certificate chain
 */
int cbtls_verify(int ok, X509_STORE_CTX *ctx)
{
	char		subject[1024]; /* Used for the subject name */
	char		issuer[1024]; /* Used for the issuer name */
	char		attribute[1024];
	char		value[1024];
	char		common_name[1024];
	char		cn_str[1024];
	char		buf[64];
	X509		*client_cert;
	X509_CINF	*client_inf;
	STACK_OF(X509_EXTENSION) *ext_list;
	SSL		*ssl;
	int		err, depth, lookup, loc;
	fr_tls_server_conf_t *conf;
	int		my_ok = ok;

	ASN1_INTEGER	*sn = NULL;
	ASN1_TIME	*asn_time = NULL;
	VALUE_PAIR	**certs;
	char **identity;
#ifdef HAVE_OPENSSL_OCSP_H
	X509_STORE	*ocsp_store = NULL;
	X509		*issuer_cert;
#endif
	VALUE_PAIR	*vp;
	TALLOC_CTX	*talloc_ctx;

	REQUEST		*request;

	client_cert = X509_STORE_CTX_get_current_cert(ctx);
	err = X509_STORE_CTX_get_error(ctx);
	depth = X509_STORE_CTX_get_error_depth(ctx);

	lookup = depth;

	/*
	 *	Log client/issuing cert.  If there's an error, log
	 *	issuing cert.
	 */
	if ((lookup > 1) && !my_ok) lookup = 1;

	/*
	 * Retrieve the pointer to the SSL of the connection currently treated
	 * and the application specific data stored into the SSL object.
	 */
	ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
	if (!conf) return 1;

	request = (REQUEST *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
	rad_assert(request != NULL);
	certs = (VALUE_PAIR **)SSL_get_ex_data(ssl, fr_tls_ex_index_certs);

	identity = (char **)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_IDENTITY);
#ifdef HAVE_OPENSSL_OCSP_H
	ocsp_store = (X509_STORE *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_STORE);
#endif

	talloc_ctx = SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_TALLOC);

	/*
	 *	Get the Serial Number
	 */
	buf[0] = '\0';
	sn = X509_get_serialNumber(client_cert);

	RDEBUG2("Creating attributes from certificate OIDs");
	RINDENT();

	/*
	 *	For this next bit, we create the attributes *only* if
	 *	we're at the client or issuing certificate, AND we
	 *	have a user identity.  i.e. we don't create the
	 *	attributes for RadSec connections.
	 */
	if (certs && identity &&
	    (lookup <= 1) && sn && ((size_t) sn->length < (sizeof(buf) / 2))) {
		char *p = buf;
		int i;

		for (i = 0; i < sn->length; i++) {
			sprintf(p, "%02x", (unsigned int)sn->data[i]);
			p += 2;
		}
		vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_SERIAL][lookup], buf, T_OP_SET);
		rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
	}


	/*
	 *	Get the Expiration Date
	 */
	buf[0] = '\0';
	asn_time = X509_get_notAfter(client_cert);
	if (certs && identity && (lookup <= 1) && asn_time &&
	    (asn_time->length < (int) sizeof(buf))) {
		memcpy(buf, (char*) asn_time->data, asn_time->length);
		buf[asn_time->length] = '\0';
		vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_EXPIRATION][lookup], buf, T_OP_SET);
		rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
	}

	/*
	 *	Get the Subject & Issuer
	 */
	subject[0] = issuer[0] = '\0';
	X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
			  sizeof(subject));
	subject[sizeof(subject) - 1] = '\0';
	if (certs && identity && (lookup <= 1) && subject[0]) {
		vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_SUBJECT][lookup], subject, T_OP_SET);
		rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
	}

	X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
			  sizeof(issuer));
	issuer[sizeof(issuer) - 1] = '\0';
	if (certs && identity && (lookup <= 1) && issuer[0]) {
		vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_ISSUER][lookup], issuer, T_OP_SET);
		rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
	}

	/*
	 *	Get the Common Name, if there is a subject.
	 */
	X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
				  NID_commonName, common_name, sizeof(common_name));
	common_name[sizeof(common_name) - 1] = '\0';
	if (certs && identity && (lookup <= 1) && common_name[0] && subject[0]) {
		vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_CN][lookup], common_name, T_OP_SET);
		rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
	}

	/*
	 *	Get the RFC822 Subject Alternative Name
	 */
	loc = X509_get_ext_by_NID(client_cert, NID_subject_alt_name, 0);
	if (certs && (lookup <= 1) && (loc >= 0)) {
		X509_EXTENSION *ext = NULL;
		GENERAL_NAMES *names = NULL;
		int i;

		if ((ext = X509_get_ext(client_cert, loc)) &&
		    (names = X509V3_EXT_d2i(ext))) {
			for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
				GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);

				switch (name->type) {
#ifdef GEN_EMAIL
				case GEN_EMAIL:
					vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_SAN_EMAIL][lookup],
						      (char *) ASN1_STRING_data(name->d.rfc822Name), T_OP_SET);
					rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
					break;
#endif	/* GEN_EMAIL */
#ifdef GEN_DNS
				case GEN_DNS:
					vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_SAN_DNS][lookup],
						      (char *) ASN1_STRING_data(name->d.dNSName), T_OP_SET);
					rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
					break;
#endif	/* GEN_DNS */
#ifdef GEN_OTHERNAME
				case GEN_OTHERNAME:
					/* look for a MS UPN */
					if (NID_ms_upn == OBJ_obj2nid(name->d.otherName->type_id)) {
					    /* we've got a UPN - Must be ASN1-encoded UTF8 string */
					    if (name->d.otherName->value->type == V_ASN1_UTF8STRING) {
						    vp = pairmake(talloc_ctx, certs, cert_attr_names[FR_TLS_SAN_UPN][lookup],
								  (char *) ASN1_STRING_data(name->d.otherName->value->value.utf8string), T_OP_SET);
						    rdebug_pair(L_DBG_LVL_2, request, vp, NULL);
						break;
					    } else {
						RWARN("Invalid UPN in Subject Alt Name (should be UTF-8)");
						break;
					    }
					}
					break;
#endif	/* GEN_OTHERNAME */
				default:
					/* XXX TODO handle other SAN types */
					break;
				}
			}
		}
		if (names != NULL)
			sk_GENERAL_NAME_free(names);
	}

	/*
	 *	If the CRL has expired, that might still be OK.
	 */
	if (!my_ok &&
	    (conf->allow_expired_crl) &&
	    (err == X509_V_ERR_CRL_HAS_EXPIRED)) {
		my_ok = 1;
		X509_STORE_CTX_set_error( ctx, 0 );
	}

	if (!my_ok) {
		char const *p = X509_verify_cert_error_string(err);
		RERROR("SSL says error %d : %s", err, p);
		REXDENT();
		return my_ok;
	}

	if (lookup == 0) {
		client_inf = client_cert->cert_info;
		ext_list = client_inf->extensions;
	} else {
		ext_list = NULL;
	}

	/*
	 *	Grab the X509 extensions, and create attributes out of them.
	 *	For laziness, we re-use the OpenSSL names
	 */
	if (certs && (sk_X509_EXTENSION_num(ext_list) > 0)) {
		int i, len;
		char *p;
		BIO *out;

		out = BIO_new(BIO_s_mem());
		strlcpy(attribute, "TLS-Client-Cert-", sizeof(attribute));

		for (i = 0; i < sk_X509_EXTENSION_num(ext_list); i++) {
			ASN1_OBJECT *obj;
			X509_EXTENSION *ext;

			ext = sk_X509_EXTENSION_value(ext_list, i);

			obj = X509_EXTENSION_get_object(ext);
			i2a_ASN1_OBJECT(out, obj);
			len = BIO_read(out, attribute + 16 , sizeof(attribute) - 16 - 1);
			if (len <= 0) continue;

			attribute[16 + len] = '\0';

			for (p = attribute + 16; *p != '\0'; p++) {
				if (*p == ' ') *p = '-';
			}

			X509V3_EXT_print(out, ext, 0, 0);
			len = BIO_read(out, value , sizeof(value) - 1);
			if (len <= 0) continue;

			value[len] = '\0';

			vp = pairmake(talloc_ctx, certs, attribute, value, T_OP_ADD);
			if (!vp) {
				RDEBUG3("Skipping %s += '%s'.  Please check that both the "
					"attribute and value are defined in the dictionaries",
					attribute, value);
			} else {
				/*
				 *	rdebug_pair_list indents (so pre REXDENT())
				 */
				REXDENT();
				rdebug_pair_list(L_DBG_LVL_2, request, vp, NULL);
				RINDENT();
			}
		}

		BIO_free_all(out);
	}

	REXDENT();

	switch (ctx->error) {
	case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
		RERROR("issuer=%s", issuer);
		break;

	case X509_V_ERR_CERT_NOT_YET_VALID:
	case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
		RERROR("notBefore=");
#if 0
		ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
#endif
		break;

	case X509_V_ERR_CERT_HAS_EXPIRED:
	case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
		RERROR("notAfter=");
#if 0
		ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
#endif
		break;
	}

	/*
	 *	If we're at the actual client cert, apply additional
	 *	checks.
	 */
	if (depth == 0) {
		/*
		 *	If the conf tells us to, check cert issuer
		 *	against the specified value and fail
		 *	verification if they don't match.
		 */
		if (conf->check_cert_issuer &&
		    (strcmp(issuer, conf->check_cert_issuer) != 0)) {
			AUTH(LOG_PREFIX ": Certificate issuer (%s) does not match specified value (%s)!",
			     issuer, conf->check_cert_issuer);
			my_ok = 0;
		}

		/*
		 *	If the conf tells us to, check the CN in the
		 *	cert against xlat'ed value, but only if the
		 *	previous checks passed.
		 */
		if (my_ok && conf->check_cert_cn) {
			if (radius_xlat(cn_str, sizeof(cn_str), request, conf->check_cert_cn, NULL, NULL) < 0) {
				/* if this fails, fail the verification */
				my_ok = 0;
			} else {
				RDEBUG2("checking certificate CN (%s) with xlat'ed value (%s)", common_name, cn_str);
				if (strcmp(cn_str, common_name) != 0) {
					AUTH(LOG_PREFIX ": Certificate CN (%s) does not match specified value (%s)!",
					     common_name, cn_str);
					my_ok = 0;
				}
			}
		} /* check_cert_cn */

#ifdef HAVE_OPENSSL_OCSP_H
		if (my_ok && conf->ocsp_enable){
			RDEBUG2("Starting OCSP Request");
			if (X509_STORE_CTX_get1_issuer(&issuer_cert, ctx, client_cert) != 1) {
				RERROR("Couldn't get issuer_cert for %s", common_name);
			} else {
				my_ok = ocsp_check(request, ocsp_store, issuer_cert, client_cert, conf);
			}
		}
#endif

		while (conf->verify_client_cert_cmd) {
			char filename[256];
			int fd;
			FILE *fp;

			snprintf(filename, sizeof(filename), "%s/%s.client.XXXXXXXX",
				 conf->verify_tmp_dir, progname);
			fd = mkstemp(filename);
			if (fd < 0) {
				RDEBUG("Failed creating file in %s: %s",
				       conf->verify_tmp_dir, fr_syserror(errno));
				break;
			}

			fp = fdopen(fd, "w");
			if (!fp) {
				close(fd);
				RDEBUG("Failed opening file %s: %s",
				       filename, fr_syserror(errno));
				break;
			}

			if (!PEM_write_X509(fp, client_cert)) {
				fclose(fp);
				RDEBUG("Failed writing certificate to file");
				goto do_unlink;
			}
			fclose(fp);

			if (!pairmake_packet("TLS-Client-Cert-Filename",
					     filename, T_OP_SET)) {
				RDEBUG("Failed creating TLS-Client-Cert-Filename");

				goto do_unlink;
			}

			RDEBUG("Verifying client certificate: %s", conf->verify_client_cert_cmd);
			if (radius_exec_program(request, NULL, 0, NULL, request, conf->verify_client_cert_cmd,
						request->packet->vps,
						true, true, EXEC_TIMEOUT) != 0) {
				AUTH(LOG_PREFIX ": Certificate CN (%s) fails external verification!", common_name);
				my_ok = 0;
			} else {
				RDEBUG("Client certificate CN %s passed external validation", common_name);
			}

		do_unlink:
			unlink(filename);
			break;
		}


	} /* depth == 0 */

	if (RDEBUG_ENABLED3) {
		RDEBUG3("chain-depth   : %d", depth);
		RDEBUG3("error         : %d", err);

		if (identity) RDEBUG3("identity      : %s", *identity);
		RDEBUG3("common name   : %s", common_name);
		RDEBUG3("subject       : %s", subject);
		RDEBUG3("issuer        : %s", issuer);
		RDEBUG3("verify return : %d", my_ok);
	}
	return my_ok;
}


#ifdef HAVE_OPENSSL_OCSP_H
/*
 * 	Create Global X509 revocation store and use it to verify
 * 	OCSP responses
 *
 * 	- Load the trusted CAs
 * 	- Load the trusted issuer certificates
 */
static X509_STORE *init_revocation_store(fr_tls_server_conf_t *conf)
{
	X509_STORE *store = NULL;

	store = X509_STORE_new();

	/* Load the CAs we trust */
	if (conf->ca_file || conf->ca_path)
		if(!X509_STORE_load_locations(store, conf->ca_file, conf->ca_path)) {
			ERROR(LOG_PREFIX ": X509_STORE error %s", ERR_error_string(ERR_get_error(), NULL));
			ERROR(LOG_PREFIX ": Error reading Trusted root CA list %s",conf->ca_file );
			return NULL;
		}

#ifdef X509_V_FLAG_CRL_CHECK
	if (conf->check_crl)
		X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
#endif
#ifdef X509_V_FLAG_CRL_CHECK_ALL
	if (conf->check_all_crl)
		X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK_ALL);
#endif
	return store;
}
#endif	/* HAVE_OPENSSL_OCSP_H */

#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
static int set_ecdh_curve(SSL_CTX *ctx, char const *ecdh_curve)
{
	int      nid;
	EC_KEY  *ecdh;

	if (!ecdh_curve || !*ecdh_curve) return 0;

	nid = OBJ_sn2nid(ecdh_curve);
	if (!nid) {
		ERROR(LOG_PREFIX ": Unknown ecdh_curve \"%s\"", ecdh_curve);
		return -1;
	}

	ecdh = EC_KEY_new_by_curve_name(nid);
	if (!ecdh) {
		ERROR(LOG_PREFIX ": Unable to create new curve \"%s\"", ecdh_curve);
		return -1;
	}

	SSL_CTX_set_tmp_ecdh(ctx, ecdh);

	SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);

	EC_KEY_free(ecdh);

	return 0;
}
#endif
#endif

/*
 * DIE OPENSSL DIE DIE DIE
 *
 * What a palaver, just to free some data attached the
 * session. We need to do this because the "remove" callback
 * is called when refcount > 0 sometimes, if another thread
 * is using the session
 */
static void sess_free_vps(UNUSED void *parent, void *data_ptr,
				UNUSED CRYPTO_EX_DATA *ad, UNUSED int idx,
				UNUSED long argl, UNUSED void *argp)
{
	VALUE_PAIR *vp = data_ptr;
	if (!vp) return;

	DEBUG2(LOG_PREFIX ": Freeing cached session VPs");

	pairfree(&vp);
}

static void sess_free_certs(UNUSED void *parent, void *data_ptr,
				UNUSED CRYPTO_EX_DATA *ad, UNUSED int idx,
				UNUSED long argl, UNUSED void *argp)
{
	VALUE_PAIR **certs = data_ptr;
	if (!certs) return;

	DEBUG2(LOG_PREFIX ": Freeing cached session Certificates");

	pairfree(certs);
}

/** Add all the default ciphers and message digests reate our context.
 *
 * This should be called exactly once from main, before reading the main config
 * or initialising any modules.
 */
void tls_global_init(void)
{
	SSL_load_error_strings();	/* readable error messages (examples show call before library_init) */
	SSL_library_init();		/* initialize library */
	OpenSSL_add_all_algorithms();	/* required for SHA2 in OpenSSL < 0.9.8o and 1.0.0.a */
	OPENSSL_config(NULL);

	/*
	 *	Initialize the index for the certificates.
	 */
	fr_tls_ex_index_certs = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, sess_free_certs);
}

#ifdef ENABLE_OPENSSL_VERSION_CHECK
/** Check for vulnerable versions of libssl
 *
 * @param acknowledged The highest CVE number a user has confirmed is not present in the system's libssl.
 * @return 0 if the CVE specified by the user matches the most recent CVE we have, else -1.
 */
int tls_global_version_check(char const *acknowledged)
{
	uint64_t v;

	if ((strcmp(acknowledged, libssl_defects[0].id) != 0) && (strcmp(acknowledged, "yes") != 0)) {
		bool bad = false;
		size_t i;

		/* Check for bad versions */
		v = (uint64_t) SSLeay();

		for (i = 0; i < (sizeof(libssl_defects) / sizeof(*libssl_defects)); i++) {
			libssl_defect_t *defect = &libssl_defects[i];

			if ((v >= defect->low) && (v <= defect->high)) {
				ERROR("Refusing to start with libssl version %s (in range %s)",
				      ssl_version(), ssl_version_range(defect->low, defect->high));
				ERROR("Security advisory %s (%s)", defect->id, defect->name);
				ERROR("%s", defect->comment);

				bad = true;
			}
		}

		if (bad) {
			INFO("Once you have verified libssl has been correctly patched, "
			     "set security.allow_vulnerable_openssl = '%s'", libssl_defects[0].id);
			return -1;
		}
	}

	return 0;
}
#endif

/** Free any memory alloced by libssl
 *
 */
void tls_global_cleanup(void)
{
	ERR_remove_state(0);
	ENGINE_cleanup();
	CONF_modules_unload(1);
	ERR_free_strings();
	EVP_cleanup();
	CRYPTO_cleanup_all_ex_data();
}

/** Create SSL context
 *
 * - Load the trusted CAs
 * - Load the Private key & the certificate
 * - Set the Context options & Verify options
 */
SSL_CTX *tls_init_ctx(fr_tls_server_conf_t *conf, int client)
{
	SSL_CTX		*ctx;
	X509_STORE	*certstore;
	int		verify_mode = SSL_VERIFY_NONE;
	int		ctx_options = 0;
	int		ctx_tls_versions = 0;
	int		type;

	/*
	 *	SHA256 is in all versions of OpenSSL, but isn't
	 *	initialized by default.  It's needed for WiMAX
	 *	certificates.
	 */
#ifdef HAVE_OPENSSL_EVP_SHA256
	EVP_add_digest(EVP_sha256());
#endif

	ctx = SSL_CTX_new(SSLv23_method()); /* which is really "all known SSL / TLS methods".  Idiots. */
	if (!ctx) {
		int err;
		while ((err = ERR_get_error())) {
			ERROR(LOG_PREFIX ": Failed creating SSL context: %s", ERR_error_string(err, NULL));
			return NULL;
		}
	}

	/*
	 * Save the config on the context so that callbacks which
	 * only get SSL_CTX* e.g. session persistence, can get it
	 */
	SSL_CTX_set_app_data(ctx, conf);

	/*
	 * Identify the type of certificates that needs to be loaded
	 */
	if (conf->file_type) {
		type = SSL_FILETYPE_PEM;
	} else {
		type = SSL_FILETYPE_ASN1;
	}

	/*
	 * Set the password to load private key
	 */
	if (conf->private_key_password) {
#ifdef __APPLE__
		/*
		 * We don't want to put the private key password in eap.conf, so  check
		 * for our special string which indicates we should get the password
		 * programmatically.
		 */
		char const* special_string = "Apple:UseCertAdmin";
		if (strncmp(conf->private_key_password, special_string, strlen(special_string)) == 0) {
			char cmd[256];
			char *password;
			long const max_password_len = 128;
			snprintf(cmd, sizeof(cmd) - 1, "/usr/sbin/certadmin --get-private-key-passphrase \"%s\"",
				 conf->private_key_file);

			DEBUG2(LOG_PREFIX ":  Getting private key passphrase using command \"%s\"", cmd);

			FILE* cmd_pipe = popen(cmd, "r");
			if (!cmd_pipe) {
				ERROR(LOG_PREFIX ": %s command failed: Unable to get private_key_password", cmd);
				ERROR(LOG_PREFIX ": Error reading private_key_file %s", conf->private_key_file);
				return NULL;
			}

			rad_const_free(conf->private_key_password);
			password = talloc_array(conf, char, max_password_len);
			if (!password) {
				ERROR(LOG_PREFIX ": Can't allocate space for private_key_password");
				ERROR(LOG_PREFIX ": Error reading private_key_file %s", conf->private_key_file);
				pclose(cmd_pipe);
				return NULL;
			}

			fgets(password, max_password_len, cmd_pipe);
			pclose(cmd_pipe);

			/* Get rid of newline at end of password. */
			password[strlen(password) - 1] = '\0';

			DEBUG3(LOG_PREFIX ": Password from command = \"%s\"", password);
			conf->private_key_password = password;
		}
#endif

		{
			char *password;

			memcpy(&password, &conf->private_key_password, sizeof(password));
			SSL_CTX_set_default_passwd_cb_userdata(ctx, password);
			SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
		}
	}

#ifdef PSK_MAX_IDENTITY_LEN
	if (!client) {
		/*
		 *	No dynamic query exists.  There MUST be a
		 *	statically configured identity and password.
		 */
		if (conf->psk_query && !*conf->psk_query) {
			ERROR(LOG_PREFIX ": Invalid PSK Configuration: psk_query cannot be empty");
			return NULL;
		}

		/*
		 *	Set the callback only if we can check things.
		 */
		if (conf->psk_identity || conf->psk_query) {
			SSL_CTX_set_psk_server_callback(ctx, psk_server_callback);
		}

	} else if (conf->psk_query) {
		ERROR(LOG_PREFIX ": Invalid PSK Configuration: psk_query cannot be used for outgoing connections");
		return NULL;
	}

	/*
	 *	Now check that if PSK is being used, the config is valid.
	 */
	if ((conf->psk_identity && !conf->psk_password) ||
	    (!conf->psk_identity && conf->psk_password) ||
	    (conf->psk_identity && !*conf->psk_identity) ||
	    (conf->psk_password && !*conf->psk_password)) {
		ERROR(LOG_PREFIX ": Invalid PSK Configuration: psk_identity or psk_password are empty");
		return NULL;
	}

	if (conf->psk_identity) {
		size_t psk_len, hex_len;
		uint8_t buffer[PSK_MAX_PSK_LEN];

		if (conf->certificate_file ||
		    conf->private_key_password || conf->private_key_file ||
		    conf->ca_file || conf->ca_path) {
			ERROR(LOG_PREFIX ": When PSKs are used, No certificate configuration is permitted");
			return NULL;
		}

		if (client) {
			SSL_CTX_set_psk_client_callback(ctx,
							psk_client_callback);
		}

		psk_len = strlen(conf->psk_password);
		if (strlen(conf->psk_password) > (2 * PSK_MAX_PSK_LEN)) {
			ERROR(LOG_PREFIX ": psk_hexphrase is too long (max %d)", PSK_MAX_PSK_LEN);
			return NULL;
		}

		/*
		 *	Check the password now, so that we don't have
		 *	errors at run-time.
		 */
		hex_len = fr_hex2bin(buffer, sizeof(buffer), conf->psk_password, psk_len);
		if (psk_len != (2 * hex_len)) {
			ERROR(LOG_PREFIX ": psk_hexphrase is not all hex");
			return NULL;
		}

		goto post_ca;
	}
#else
	(void) client;	/* -Wunused */
#endif

	/*
	 *	Load our keys and certificates
	 *
	 *	If certificates are of type PEM then we can make use
	 *	of cert chain authentication using openssl api call
	 *	SSL_CTX_use_certificate_chain_file.  Please see how
	 *	the cert chain needs to be given in PEM from
	 *	openSSL.org
	 */
	if (!conf->certificate_file) goto load_ca;

	if (type == SSL_FILETYPE_PEM) {
		if (!(SSL_CTX_use_certificate_chain_file(ctx, conf->certificate_file))) {
			ERROR(LOG_PREFIX ": Error reading certificate file %s:%s", conf->certificate_file,
			      ERR_error_string(ERR_get_error(), NULL));
			return NULL;
		}

	} else if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
		ERROR(LOG_PREFIX ": Error reading certificate file %s:%s",
		      conf->certificate_file,
		      ERR_error_string(ERR_get_error(), NULL));
		return NULL;
	}

	/* Load the CAs we trust */
load_ca:
	if (conf->ca_file || conf->ca_path) {
		if (!SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) {
			ERROR(LOG_PREFIX ": SSL error %s", ERR_error_string(ERR_get_error(), NULL));
			ERROR(LOG_PREFIX ": Error reading Trusted root CA list %s",conf->ca_file );
			return NULL;
		}
	}
	if (conf->ca_file && *conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));

	if (conf->private_key_file) {
		if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
			ERROR(LOG_PREFIX ": Failed reading private key file %s:%s",
			      conf->private_key_file,
			      ERR_error_string(ERR_get_error(), NULL));
			return NULL;
		}

		/*
		 * Check if the loaded private key is the right one
		 */
		if (!SSL_CTX_check_private_key(ctx)) {
			ERROR(LOG_PREFIX ": Private key does not match the certificate public key");
			return NULL;
		}
	}

#ifdef PSK_MAX_IDENTITY_LEN
post_ca:
#endif

	/*
	 *	We never want SSLv2 or SSLv3.
	 */
	ctx_options |= SSL_OP_NO_SSLv2;
	ctx_options |= SSL_OP_NO_SSLv3;

	/*
	 *	As of 3.0.5, we always allow TLSv1.1 and TLSv1.2.
	 *	Though they can be *globally* disabled if necessary.x
	 */
#ifdef SSL_OP_NO_TLSv1
	if (conf->disable_tlsv1) ctx_options |= SSL_OP_NO_TLSv1;

	ctx_tls_versions |= SSL_OP_NO_TLSv1;
#endif
#ifdef SSL_OP_NO_TLSv1_1
	if (conf->disable_tlsv1_1) ctx_options |= SSL_OP_NO_TLSv1_1;

	ctx_tls_versions |= SSL_OP_NO_TLSv1_1;
#endif
#ifdef SSL_OP_NO_TLSv1_2
	if (conf->disable_tlsv1_2) ctx_options |= SSL_OP_NO_TLSv1_2;

	ctx_tls_versions |= SSL_OP_NO_TLSv1_2;
#endif

	if ((ctx_options & ctx_tls_versions) == ctx_tls_versions) {
		ERROR(LOG_PREFIX ": You have disabled all available TLS versions.  EAP will not work");
		return NULL;
	}

#ifdef SSL_OP_NO_TICKET
	ctx_options |= SSL_OP_NO_TICKET;
#endif

	/*
	 *	SSL_OP_SINGLE_DH_USE must be used in order to prevent
	 *	small subgroup attacks and forward secrecy. Always
	 *	using
	 *
	 *	SSL_OP_SINGLE_DH_USE has an impact on the computer
	 *	time needed during negotiation, but it is not very
	 *	large.
	 */
	ctx_options |= SSL_OP_SINGLE_DH_USE;

	/*
	 *	SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS to work around issues
	 *	in Windows Vista client.
	 *	http://www.openssl.org/~bodo/tls-cbc.txt
	 *	http://www.nabble.com/(RADIATOR)-Radiator-Version-3.16-released-t2600070.html
	 */
	ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;

	SSL_CTX_set_options(ctx, ctx_options);

	/*
	 *	TODO: Set the RSA & DH
	 *	SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
	 *	SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
	 */

	/*
	 *	set the message callback to identify the type of
	 *	message.  For every new session, there can be a
	 *	different callback argument.
	 *
	 *	SSL_CTX_set_msg_callback(ctx, cbtls_msg);
	 */

	/*
	 *	Set eliptical curve crypto configuration.
	 */
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL
#ifndef OPENSSL_NO_ECDH
	if (set_ecdh_curve(ctx, conf->ecdh_curve) < 0) {
		return NULL;
	}
#endif
#endif

	/* Set Info callback */
	SSL_CTX_set_info_callback(ctx, cbtls_info);

	/*
	 *	Callbacks, etc. for session resumption.
	 */
	if (conf->session_cache_enable) {
		/*
		 *	Cache sessions on disk if requested.
		 */
		if (conf->session_cache_path) {
			SSL_CTX_sess_set_new_cb(ctx, cbtls_new_session);
			SSL_CTX_sess_set_get_cb(ctx, cbtls_get_session);
			SSL_CTX_sess_set_remove_cb(ctx, cbtls_remove_session);
		}

		SSL_CTX_set_quiet_shutdown(ctx, 1);
		if (fr_tls_ex_index_vps < 0)
			fr_tls_ex_index_vps = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, sess_free_vps);
	}

	/*
	 *	Check the certificates for revocation.
	 */
#ifdef X509_V_FLAG_CRL_CHECK
	if (conf->check_crl) {
		certstore = SSL_CTX_get_cert_store(ctx);
		if (certstore == NULL) {
			ERROR(LOG_PREFIX ": SSL error %s", ERR_error_string(ERR_get_error(), NULL));
			ERROR(LOG_PREFIX ": Error reading Certificate Store");
	    		return NULL;
		}
		X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);

#ifdef X509_V_FLAG_CRL_CHECK_ALL
		if (conf->check_all_crl)
			X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK_ALL);
#endif
	}
#endif

	/*
	 *	Set verify modes
	 *	Always verify the peer certificate
	 */
	verify_mode |= SSL_VERIFY_PEER;
	verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
	verify_mode |= SSL_VERIFY_CLIENT_ONCE;
	SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);

	if (conf->verify_depth) {
		SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
	}

	/* Load randomness */
	if (conf->random_file) {
		if (!(RAND_load_file(conf->random_file, 1024*10))) {
			ERROR(LOG_PREFIX ": SSL error %s", ERR_error_string(ERR_get_error(), NULL));
			ERROR(LOG_PREFIX ": Error loading randomness");
			return NULL;
		}
	}

	/*
	 * Set the cipher list if we were told to
	 */
	if (conf->cipher_list) {
		if (!SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
			ERROR(LOG_PREFIX ": Error setting cipher list");
			return NULL;
		}
	}

	/*
	 *	Setup session caching
	 */
	if (conf->session_cache_enable) {
		/*
		 *	Create a unique context Id per EAP-TLS configuration.
		 */
		if (conf->session_id_name) {
			snprintf(conf->session_context_id, sizeof(conf->session_context_id),
				 "FR eap %s", conf->session_id_name);
		} else {
			snprintf(conf->session_context_id, sizeof(conf->session_context_id),
				 "FR eap %p", conf);
		}

		/*
		 *	Cache it, and DON'T auto-clear it.
		 */
		SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | SSL_SESS_CACHE_NO_AUTO_CLEAR);

		SSL_CTX_set_session_id_context(ctx,
					       (unsigned char *) conf->session_context_id,
					       (unsigned int) strlen(conf->session_context_id));

		/*
		 *	Our timeout is in hours, this is in seconds.
		 */
		SSL_CTX_set_timeout(ctx, conf->session_timeout * 3600);

		/*
		 *	Set the maximum number of entries in the
		 *	session cache.
		 */
		SSL_CTX_sess_set_cache_size(ctx, conf->session_cache_size);

	} else {
		SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
	}

	return ctx;
}


/*
 *	Free TLS client/server config
 *	Should not be called outside this code, as a callback is
 *	added to automatically free the data when the CONF_SECTION
 *	is freed.
 */
static int _tls_server_conf_free(fr_tls_server_conf_t *conf)
{
	if (conf->ctx) SSL_CTX_free(conf->ctx);

#ifdef HAVE_OPENSSL_OCSP_H
	if (conf->ocsp_store) X509_STORE_free(conf->ocsp_store);
	conf->ocsp_store = NULL;
#endif

#ifndef NDEBUG
	memset(conf, 0, sizeof(*conf));
#endif
	return 0;
}

static fr_tls_server_conf_t *tls_server_conf_alloc(TALLOC_CTX *ctx)
{
	fr_tls_server_conf_t *conf;

	conf = talloc_zero(ctx, fr_tls_server_conf_t);
	if (!conf) {
		ERROR(LOG_PREFIX ": Out of memory");
		return NULL;
	}

	talloc_set_destructor(conf, _tls_server_conf_free);

	return conf;
}

fr_tls_server_conf_t *tls_server_conf_parse(CONF_SECTION *cs)
{
	fr_tls_server_conf_t *conf;

	/*
	 *	If cs has already been parsed there should be a cached copy
	 *	of conf already stored, so just return that.
	 */
	conf = cf_data_find(cs, "tls-conf");
	if (conf) {
		DEBUG(LOG_PREFIX ": Using cached TLS configuration from previous invocation");
		return conf;
	}

	conf = tls_server_conf_alloc(cs);

	if (cf_section_parse(cs, conf, tls_server_config) < 0) {
	error:
		talloc_free(conf);
		return NULL;
	}

	/*
	 *	Save people from their own stupidity.
	 */
	if (conf->fragment_size < 100) conf->fragment_size = 100;

	if (!conf->private_key_file) {
		ERROR(LOG_PREFIX ": TLS Server requires a private key file");
		goto error;
	}

	if (!conf->certificate_file) {
		ERROR(LOG_PREFIX ": TLS Server requires a certificate file");
		goto error;
	}

	/*
	 *	Initialize TLS
	 */
	conf->ctx = tls_init_ctx(conf, 0);
	if (conf->ctx == NULL) {
		goto error;
	}

#ifdef HAVE_OPENSSL_OCSP_H
	/*
	 * 	Initialize OCSP Revocation Store
	 */
	if (conf->ocsp_enable) {
		conf->ocsp_store = init_revocation_store(conf);
		if (conf->ocsp_store == NULL) goto error;
	}
#endif /*HAVE_OPENSSL_OCSP_H*/
	{
		char *dh_file;

		memcpy(&dh_file, &conf->dh_file, sizeof(dh_file));
		if (load_dh_params(conf->ctx, dh_file) < 0) {
			goto error;
		}
	}

	if (conf->verify_tmp_dir) {
		if (chmod(conf->verify_tmp_dir, S_IRWXU) < 0) {
			ERROR(LOG_PREFIX ": Failed changing permissions on %s: %s",
			      conf->verify_tmp_dir, fr_syserror(errno));
			goto error;
		}
	}

	if (conf->verify_client_cert_cmd && !conf->verify_tmp_dir) {
		ERROR(LOG_PREFIX ": You MUST set the verify directory in order to use verify_client_cmd");
		goto error;
	}

	/*
	 *	Cache conf in cs in case we're asked to parse this again.
	 */
	cf_data_add(cs, "tls-conf", conf, NULL);

	return conf;
}

fr_tls_server_conf_t *tls_client_conf_parse(CONF_SECTION *cs)
{
	fr_tls_server_conf_t *conf;

	conf = cf_data_find(cs, "tls-conf");
	if (conf) {
		DEBUG2(LOG_PREFIX ": Using cached TLS configuration from previous invocation");
		return conf;
	}

	conf = tls_server_conf_alloc(cs);

	if (cf_section_parse(cs, conf, tls_client_config) < 0) {
	error:
		talloc_free(conf);
		return NULL;
	}

	/*
	 *	Save people from their own stupidity.
	 */
	if (conf->fragment_size < 100) conf->fragment_size = 100;

	/*
	 *	Initialize TLS
	 */
	conf->ctx = tls_init_ctx(conf, 1);
	if (conf->ctx == NULL) {
		goto error;
	}

	{
		char *dh_file;

		memcpy(&dh_file, &conf->dh_file, sizeof(dh_file));
		if (load_dh_params(conf->ctx, dh_file) < 0) {
			goto error;
		}
	}

	cf_data_add(cs, "tls-conf", conf, NULL);

	return conf;
}

int tls_success(tls_session_t *ssn, REQUEST *request)
{
	VALUE_PAIR *vp, *vps = NULL;
	fr_tls_server_conf_t *conf;
	TALLOC_CTX *talloc_ctx;

	conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssn->ssl, FR_TLS_EX_INDEX_CONF);
	rad_assert(conf != NULL);

	talloc_ctx = SSL_get_ex_data(ssn->ssl, FR_TLS_EX_INDEX_TALLOC);

	/*
	 *	If there's no session resumption, delete the entry
	 *	from the cache.  This means either it's disabled
	 *	globally for this SSL context, OR we were told to
	 *	disable it for this user.
	 *
	 *	This also means you can't turn it on just for one
	 *	user.
	 */
	if ((!ssn->allow_session_resumption) ||
	    (((vp = pairfind(request->config, PW_ALLOW_SESSION_RESUMPTION, 0, TAG_ANY)) != NULL) &&
	     (vp->vp_integer == 0))) {
		SSL_CTX_remove_session(ssn->ctx,
				       ssn->ssl->session);
		ssn->allow_session_resumption = false;

		/*
		 *	If we're in a resumed session and it's
		 *	not allowed,
		 */
		if (SSL_session_reused(ssn->ssl)) {
			RDEBUG("Forcibly stopping session resumption as it is not allowed");
			return -1;
		}

	/*
	 *	Else resumption IS allowed, so we store the
	 *	user data in the cache.
	 */
	} else if (!SSL_session_reused(ssn->ssl)) {
		size_t size;
		VALUE_PAIR **certs;
		char buffer[2 * MAX_SESSION_SIZE + 1];

		size = ssn->ssl->session->session_id_length;
		if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;

		fr_bin2hex(buffer, ssn->ssl->session->session_id, size);

		vp = paircopy_by_num(talloc_ctx, request->reply->vps, PW_USER_NAME, 0, TAG_ANY);
		if (vp) pairadd(&vps, vp);

		vp = paircopy_by_num(talloc_ctx, request->packet->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY);
		if (vp) pairadd(&vps, vp);

		vp = paircopy_by_num(talloc_ctx, request->packet->vps, PW_STRIPPED_USER_DOMAIN, 0, TAG_ANY);
		if (vp) pairadd(&vps, vp);

		vp = paircopy_by_num(talloc_ctx, request->reply->vps, PW_CHARGEABLE_USER_IDENTITY, 0, TAG_ANY);
		if (vp) pairadd(&vps, vp);

		vp = paircopy_by_num(talloc_ctx, request->reply->vps, PW_CACHED_SESSION_POLICY, 0, TAG_ANY);
		if (vp) pairadd(&vps, vp);

		certs = (VALUE_PAIR **)SSL_get_ex_data(ssn->ssl, fr_tls_ex_index_certs);

		/*
		 *	Hmm... the certs should probably be session data.
		 */
		if (certs) {
			/*
			 *	@todo: some go into reply, others into
			 *	request
			 */
			pairadd(&vps, paircopy(talloc_ctx, *certs));

			/*
			 *	Save the certs in the packet, so that we can see them.
			 */
			pairadd(&request->packet->vps, paircopy(request->packet, *certs));
		}

		if (vps) {
			SSL_SESSION_set_ex_data(ssn->ssl->session, fr_tls_ex_index_vps, vps);
			rdebug_pair_list(L_DBG_LVL_2, request, vps, "  caching ");

			if (conf->session_cache_path) {
				/* write the VPs to the cache file */
				char filename[256], buf[1024];
				FILE *vp_file;

				RDEBUG2("Saving session %s in the disk cache", buffer);

				snprintf(filename, sizeof(filename), "%s%c%s.vps", conf->session_cache_path,
					 FR_DIR_SEP, buffer);
				vp_file = fopen(filename, "w");
				if (vp_file == NULL) {
					RWDEBUG("Could not write session VPs to persistent cache: %s",
						fr_syserror(errno));
				} else {
					VALUE_PAIR *prev = NULL;
					vp_cursor_t cursor;
					/* generate a dummy user-style entry which is easy to read back */
					fprintf(vp_file, "# SSL cached session\n");
					fprintf(vp_file, "%s\n\t", buffer);

					for (vp = fr_cursor_init(&cursor, &vps);
					     vp;
					     vp = fr_cursor_next(&cursor)) {
						/*
						 *	Terminate the previous line.
						 */
						if (prev) fprintf(vp_file, ",\n\t");

						/*
						 *	Write this one.
						 */
						vp_prints(buf, sizeof(buf), vp);
						fputs(buf, vp_file);
						prev = vp;
					}

					/*
					 *	Terminate the final line.
					 */
					fprintf(vp_file, "\n");
					fclose(vp_file);
				}
			} else {
				RDEBUG("Failed to find 'persist_dir' in TLS configuration.  Session will not be cached on disk.");
			}
		} else {
			RDEBUG2("No information to cache: session caching will be disabled for session %s", buffer);
			SSL_CTX_remove_session(ssn->ctx, ssn->ssl->session);
		}

	/*
	 *	Else the session WAS allowed.  Copy the cached reply.
	 */
	} else {
		size_t size;
		char buffer[2 * MAX_SESSION_SIZE + 1];

		size = ssn->ssl->session->session_id_length;
		if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;

		fr_bin2hex(buffer, ssn->ssl->session->session_id, size);

		/*
		 *	The "restore VPs from OpenSSL cache" code is
		 *	now in eaptls_process()
		 */

		if (conf->session_cache_path) {
			/* "touch" the cached session/vp file */
			char filename[256];

			snprintf(filename, sizeof(filename), "%s%c%s.asn1",
				 conf->session_cache_path, FR_DIR_SEP, buffer);
			utime(filename, NULL);
			snprintf(filename, sizeof(filename), "%s%c%s.vps",
				 conf->session_cache_path, FR_DIR_SEP, buffer);
			utime(filename, NULL);
		}

		/*
		 *	Mark the request as resumed.
		 */
		pairmake_packet("EAP-Session-Resumed", "1", T_OP_SET);
	}

	return 0;
}


void tls_fail(tls_session_t *ssn)
{
	/*
	 *	Force the session to NOT be cached.
	 */
	SSL_CTX_remove_session(ssn->ctx, ssn->ssl->session);
}

fr_tls_status_t tls_application_data(tls_session_t *ssn, REQUEST *request)

{
	int err;
	VALUE_PAIR **certs;

	/*
	 *	Decrypt the complete record.
	 */
	err = BIO_write(ssn->into_ssl, ssn->dirty_in.data,
			ssn->dirty_in.used);
	if (err != (int) ssn->dirty_in.used) {
		record_init(&ssn->dirty_in);
		RDEBUG("Failed writing %zd bytes to SSL BIO: %d", ssn->dirty_in.used, err);
		return FR_TLS_FAIL;
	}

	/*
	 *      Clear the dirty buffer now that we are done with it
	 *      and init the clean_out buffer to store decrypted data
	 */
	record_init(&ssn->dirty_in);
	record_init(&ssn->clean_out);

	/*
	 *      Read (and decrypt) the tunneled data from the
	 *      SSL session, and put it into the decrypted
	 *      data buffer.
	 */
	err = SSL_read(ssn->ssl, ssn->clean_out.data, sizeof(ssn->clean_out.data));
	if (err < 0) {
		int code;

		RDEBUG("SSL_read Error");

		code = SSL_get_error(ssn->ssl, err);
		switch (code) {
		case SSL_ERROR_WANT_READ:
			DEBUG("Error in fragmentation logic: SSL_WANT_READ");
			return FR_TLS_MORE_FRAGMENTS;

		case SSL_ERROR_WANT_WRITE:
			DEBUG("Error in fragmentation logic: SSL_WANT_WRITE");
			break;

		default:
			DEBUG("Error in fragmentation logic: %s", ERR_error_string(code, NULL));

			/*
			 *	FIXME: Call int_ssl_check?
			 */
			break;
		}
		return FR_TLS_FAIL;
	}

	if (err == 0) RWDEBUG("No data inside of the tunnel");

	/*
	 *	Passed all checks, successfully decrypted data
	 */
	ssn->clean_out.used = err;

	/*
	 *	Add the certificates to intermediate packets, so that
	 *	the inner tunnel policies can use them.
	 */
	certs = (VALUE_PAIR **)SSL_get_ex_data(ssn->ssl, fr_tls_ex_index_certs);

	if (certs) pairadd(&request->packet->vps, paircopy(request->packet, *certs));

	return FR_TLS_OK;
}


/*
 * Acknowledge received is for one of the following messages sent earlier
 * 1. Handshake completed Message, so now send, EAP-Success
 * 2. Alert Message, now send, EAP-Failure
 * 3. Fragment Message, now send, next Fragment
 */
fr_tls_status_t tls_ack_handler(tls_session_t *ssn, REQUEST *request)
{
	if (ssn == NULL){
		REDEBUG("Unexpected ACK received:  No ongoing SSL session");
		return FR_TLS_INVALID;
	}
	if (!ssn->info.initialized) {
		RDEBUG("No SSL info available.  Waiting for more SSL data");
		return FR_TLS_REQUEST;
	}

	if ((ssn->info.content_type == handshake) && (ssn->info.origin == 0)) {
		REDEBUG("Unexpected ACK received:  We sent no previous messages");
		return FR_TLS_INVALID;
	}

	switch (ssn->info.content_type) {
	case alert:
		RDEBUG2("Peer ACKed our alert");
		return FR_TLS_FAIL;

	case handshake:
		if ((ssn->info.handshake_type == handshake_finished) && (ssn->dirty_out.used == 0)) {
			RDEBUG2("Peer ACKed our handshake fragment.  handshake is finished");

			/*
			 *	From now on all the content is
			 *	application data set it here as nobody else
			 *	sets it.
			 */
			ssn->info.content_type = application_data;
			return FR_TLS_SUCCESS;
		} /* else more data to send */

		RDEBUG2("Peer ACKed our handshake fragment");
		/* Fragmentation handler, send next fragment */
		return FR_TLS_REQUEST;

	case application_data:
		RDEBUG2("Peer ACKed our application data fragment");
		return FR_TLS_REQUEST;

		/*
		 *	For the rest of the conditions, switch over
		 *	to the default section below.
		 */
	default:
		REDEBUG("Invalid ACK received: %d", ssn->info.content_type);
		return FR_TLS_INVALID;
	}
}
#endif	/* WITH_TLS */



More information about the Freeradius-Users mailing list