Modifying the TLS state machine to include certificate exchange successfully
diff --git a/src/test/builder/noseTlsAuthTest.yaml b/src/test/builder/noseTlsAuthTest.yaml
index 5a7f828..dfdd6b4 100644
--- a/src/test/builder/noseTlsAuthTest.yaml
+++ b/src/test/builder/noseTlsAuthTest.yaml
@@ -28,5 +28,17 @@
             EVT_EAP_TLS_CERT_REQ:
                 Actions:
                     - _eapTlsCertReq
+                NextState: ST_EAP_TLS_CHANGE_CIPHER_SPEC
+    ST_EAP_TLS_CHANGE_CIPHER_SPEC:
+        Events:
+            EVT_EAP_TLS_CHANGE_CIPHER_SPEC:
+                Actions:
+                    - _eapTlsChangeCipherSpec
+                NextState: ST_EAP_TLS_FINISHED
+    ST_EAP_TLS_FINISHED:
+        Events:
+            EVT_EAP_TLS_FINISHED:
+                Actions:
+                    - _eapTlsFinished
                 NextState: ST_EAP_TLS_DONE
         
\ No newline at end of file
diff --git a/src/test/fsm/noseTlsAuthHolder.py b/src/test/fsm/noseTlsAuthHolder.py
index 127b10b..4e2fb35 100644
--- a/src/test/fsm/noseTlsAuthHolder.py
+++ b/src/test/fsm/noseTlsAuthHolder.py
@@ -3,25 +3,33 @@
 def initTlsAuthHolderFsmTable(obj,St,Ev):
     return {
 
-    ## CurrentState                          Event                                      Actions                  NextState
+    ## CurrentState                                   Event                                               Actions                          NextState
 
-      (St.ST_EAP_SETUP,                      Ev.EVT_EAP_SETUP                       ):( (obj._eapSetup,),        St.ST_EAP_START),
+      (St.ST_EAP_TLS_HELLO_REQ,                       Ev.EVT_EAP_TLS_HELLO_REQ                        ):( (obj._eapTlsHelloReq,),          St.ST_EAP_TLS_CERT_REQ),
 
-    ## CurrentState                          Event                                      Actions                  NextState
+    ## CurrentState                                   Event                                               Actions                          NextState
 
-      (St.ST_EAP_TLS_HELLO_REQ,              Ev.EVT_EAP_TLS_HELLO_REQ               ):( (obj._eapTlsHelloReq,),  St.ST_EAP_TLS_CERT_REQ),
+      (St.ST_EAP_ID_REQ,                              Ev.EVT_EAP_ID_REQ                               ):( (obj._eapIdReq,),                St.ST_EAP_TLS_HELLO_REQ),
 
-    ## CurrentState                          Event                                      Actions                  NextState
+    ## CurrentState                                   Event                                               Actions                          NextState
 
-      (St.ST_EAP_START,                      Ev.EVT_EAP_START                       ):( (obj._eapStart,),        St.ST_EAP_ID_REQ),
+      (St.ST_EAP_SETUP,                               Ev.EVT_EAP_SETUP                                ):( (obj._eapSetup,),                St.ST_EAP_START),
 
-    ## CurrentState                          Event                                      Actions                  NextState
+    ## CurrentState                                   Event                                               Actions                          NextState
 
-      (St.ST_EAP_ID_REQ,                     Ev.EVT_EAP_ID_REQ                      ):( (obj._eapIdReq,),        St.ST_EAP_TLS_HELLO_REQ),
+      (St.ST_EAP_TLS_FINISHED,                        Ev.EVT_EAP_TLS_FINISHED                         ):( (obj._eapTlsFinished,),          St.ST_EAP_TLS_DONE),
 
-    ## CurrentState                          Event                                      Actions                  NextState
+    ## CurrentState                                   Event                                               Actions                          NextState
 
-      (St.ST_EAP_TLS_CERT_REQ,               Ev.EVT_EAP_TLS_CERT_REQ                ):( (obj._eapTlsCertReq,),   St.ST_EAP_TLS_DONE),
+      (St.ST_EAP_START,                               Ev.EVT_EAP_START                                ):( (obj._eapStart,),                St.ST_EAP_ID_REQ),
+
+    ## CurrentState                                   Event                                               Actions                          NextState
+
+      (St.ST_EAP_TLS_CHANGE_CIPHER_SPEC,              Ev.EVT_EAP_TLS_CHANGE_CIPHER_SPEC               ):( (obj._eapTlsChangeCipherSpec,),  St.ST_EAP_TLS_FINISHED),
+
+    ## CurrentState                                   Event                                               Actions                          NextState
+
+      (St.ST_EAP_TLS_CERT_REQ,                        Ev.EVT_EAP_TLS_CERT_REQ                         ):( (obj._eapTlsCertReq,),           St.ST_EAP_TLS_CHANGE_CIPHER_SPEC),
 
 }
 
diff --git a/src/test/tls/tlsAuthTest.py b/src/test/tls/tlsAuthTest.py
index f39f116..bc764af 100644
--- a/src/test/tls/tlsAuthTest.py
+++ b/src/test/tls/tlsAuthTest.py
@@ -18,8 +18,8 @@
                                                                    'radiusIp': '172.17.0.2' } } } }
             radius_ip = os.getenv('ONOS_AAA_IP') or '172.17.0.2'
             aaa_dict['apps']['org.onosproject.aaa']['AAA']['radiusIp'] = radius_ip
-            self.onos_ctrl.activate()
-            time.sleep(2)
+            #self.onos_ctrl.activate()
+            #time.sleep(2)
             self.onos_load_config(aaa_dict)
 
       def onos_load_config(self, config):
@@ -27,7 +27,7 @@
             if status is False:
                   log.info('Configure request for AAA returned status %d' %code)
                   assert_equal(status, True)
-            time.sleep(2)
+            time.sleep(4)
             
       def test_eap_tls(self):
           tls = TLSAuthTest()
diff --git a/src/test/utils/EapTLS.py b/src/test/utils/EapTLS.py
index a34c749..dfd6f88 100644
--- a/src/test/utils/EapTLS.py
+++ b/src/test/utils/EapTLS.py
@@ -9,7 +9,7 @@
 from nose.tools import *
 from CordTestBase import CordTester
 import re
-
+log.setLevel('DEBUG')
 class TLSAuthTest(EapolPacket, CordTester):
 
     tlsStateTable = Enumeration("TLSStateTable", ("ST_EAP_SETUP",
@@ -17,6 +17,8 @@
                                                   "ST_EAP_ID_REQ",
                                                   "ST_EAP_TLS_HELLO_REQ",
                                                   "ST_EAP_TLS_CERT_REQ",
+                                                  "ST_EAP_TLS_CHANGE_CIPHER_SPEC",
+                                                  "ST_EAP_TLS_FINISHED",
                                                   "ST_EAP_TLS_DONE"
                                                   )
                                 )
@@ -25,6 +27,8 @@
                                                   "EVT_EAP_ID_REQ",
                                                   "EVT_EAP_TLS_HELLO_REQ",
                                                   "EVT_EAP_TLS_CERT_REQ",
+                                                  "EVT_EAP_TLS_CHANGE_CIPHER_SPEC",
+                                                  "EVT_EAP_TLS_FINISHED",
                                                   "EVT_EAP_TLS_DONE"
                                                   )
                                 )
@@ -39,33 +43,27 @@
         self.nextEvent = None
 
     def _eapSetup(self):
-        print 'Inside EAP Setup'
         self.setup()
         self.nextEvent = self.tlsEventTable.EVT_EAP_START
         
     def _eapStart(self):
-        print 'Inside EAP Start'
         self.eapol_start()
         self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ
 
     def _eapIdReq(self):
-        print 'Inside EAP ID Req'
         p = self.eapol_recv()
         code, pkt_id, eaplen = unpack("!BBH", p[0:4])
-        print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
         assert_equal(code, EAP_REQUEST)
         reqtype = unpack("!B", p[4:5])[0]
         reqdata = p[5:4+eaplen]
         assert_equal(reqtype, EAP_TYPE_ID)
-        print "<====== Send EAP Response with identity = %s ================>" % USER
+        log.debug("<====== Send EAP Response with identity = %s ================>" % USER)
         self.eapol_id_req(pkt_id, USER)
         self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ
 
     def _eapTlsHelloReq(self):
-        print 'Inside EAP TLS Hello Req'
         p = self.eapol_recv()
         code, pkt_id, eaplen = unpack("!BBH", p[0:4])
-        print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
         assert_equal(code, EAP_REQUEST)
         reqtype = unpack("!B", p[4:5])[0]
         assert_equal(reqtype, EAP_TYPE_TLS)
@@ -78,84 +76,67 @@
                                                                              )
 
         #reqdata.show()
-        print "------> Sending Client Hello TLS payload of len %d ----------->" %len(reqdata)
+        log.debug("------> Sending Client Hello TLS payload of len %d ----------->" %len(reqdata))
         eap_payload = self.eapTLS(EAP_RESPONSE, pkt_id, TLS_LENGTH_INCLUDED, str(reqdata))
         self.eapol_send(EAPOL_EAPPACKET, eap_payload)
         self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ
 
     def _eapTlsCertReq(self):
-        print 'Inside EAP TLS Cert Req'
         p = self.eapol_recv()
-        print 'Got TLS Cert Req with payload len: %d' %len(p)
         code, pkt_id, eaplen = unpack("!BBH", p[0:4])
-        print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
         assert_equal(code, EAP_REQUEST)
         reqtype = unpack("!B", p[4:5])[0]
         assert_equal(reqtype, EAP_TYPE_TLS)
         rex_pem = re.compile(r'\-+BEGIN[^\-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findall(self.pem_cert)[0].decode("base64")
-        self.pem_priv_key = """-----BEGIN RSA PRIVATE KEY-----
-MIIEpAIBAAKCAQEA84TzkjbcskbKZnrlKcXzSSgi07n+4N7kOM7uIhzpkTuU0HIv
-h4VZS2axxfV6hV3CD9MuKVg2zEhroqK1Js5n4ke230nSP/qiELfCl0R+hzRtbfKL
-tFUr1iHeU0uQ6v3q+Tg1K/Tmmg72uxKrhyHDL7z0BriPjhAHJ5XlQsvR1RCMkqzu
-D9wjSInJxpMMIgLndOclAKv4D1wQtYU7ZpTw+01XBlUhIiXb86qpYL9NqnnRq5JI
-uhmOEuxo2ca63+xaHNhD/udSyc8C0Md/yX6wlONTRFgLLv0pdLUGm1xEjfsydaQ6
-qGd7hzIKUI3hohNKJa/mHLElv7SZolPTogK/EQIDAQABAoIBAADq9FwNtuE5IRQn
-zGtO4q7Y5uCzZ8GDNYr9RKp+P2cbuWDbvVAecYq2NV9QoIiWJOAYZKklOvekIju3
-r0UZLA0PRiIrTg6NrESx3JrjWDK8QNlUO7CPTZ39/K+FrmMkV9lem9yxjJjyC34D
-AQB+YRTx+l14HppjdxNwHjAVQpIx/uO2F5xAMuk32+3K+pq9CZUtrofe1q4Agj9R
-5s8mSy9pbRo9kW9wl5xdEotz1LivFOEiqPUJTUq5J5PeMKao3vdK726XI4Z455Nm
-W2/MA0YV0ug2FYinHcZdvKM6dimH8GLfa3X8xKRfzjGjTiMSwsdjgMa4awY3tEHH
-674jhAECgYEA/zqMrc0zsbNk83sjgaYIug5kzEpN4ic020rSZsmQxSCerJTgNhmg
-utKSCt0Re09Jt3LqG48msahX8ycqDsHNvlEGPQSbMu9IYeO3Wr3fAm75GEtFWePY
-BhM73I7gkRt4s8bUiUepMG/wY45c5tRF23xi8foReHFFe9MDzh8fJFECgYEA9EFX
-4qAik1pOJGNei9BMwmx0I0gfVEIgu0tzeVqT45vcxbxr7RkTEaDoAG6PlbWP6D9a
-WQNLp4gsgRM90ZXOJ4up5DsAWDluvaF4/omabMA+MJJ5kGZ0gCj5rbZbKqUws7x8
-bp+6iBfUPJUbcqNqFmi/08Yt7vrDnMnyMw2A/sECgYEAiiuRMxnuzVm34hQcsbhH
-6ymVqf7j0PW2qK0F4H1ocT9qhzWFd+RB3kHWrCjnqODQoI6GbGr/4JepHUpre1ex
-4UEN5oSS3G0ru0rC3U4C59dZ5KwDHFm7ffZ1pr52ljfQDUsrjjIMRtuiwNK2OoRa
-WSsqiaL+SDzSB+nBmpnAizECgYBdt/y6rerWUx4MhDwwtTnel7JwHyo2MDFS6/5g
-n8qC2Lj6/fMDRE22w+CA2esp7EJNQJGv+b27iFpbJEDh+/Lf5YzIT4MwVskQ5bYB
-JFcmRxUVmf4e09D7o705U/DjCgMH09iCsbLmqQ38ONIRSHZaJtMDtNTHD1yi+jF+
-OT43gQKBgQC/2OHZoko6iRlNOAQ/tMVFNq7fL81GivoQ9F1U0Qr+DH3ZfaH8eIkX
-xT0ToMPJUzWAn8pZv0snA0um6SIgvkCuxO84OkANCVbttzXImIsL7pFzfcwV/ERK
-UM6j0ZuSMFOCr/lGPAoOQU0fskidGEHi1/kW+suSr28TqsyYZpwBDQ==
------END RSA PRIVATE KEY-----
-        """
-        self.der_priv_key = rex_pem.findall(self.pem_priv_key)[0].decode("base64")
         reqdata = TLSRecord(version="TLS_1_0")/TLSHandshake()/TLSCertificateList(
             certificates=[TLSCertificate(data=x509.X509Cert(self.der_cert))])
         #reqdata.show()
-        print "------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata)
+        log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata))
         eap_payload = self.eapTLS(EAP_RESPONSE, pkt_id, TLS_LENGTH_INCLUDED, str(reqdata))
         self.eapol_send(EAPOL_EAPPACKET, eap_payload)
+        self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
+
+    def _eapTlsChangeCipherSpec(self):
+        p = self.eapol_recv()
+        code, pkt_id, eaplen = unpack("!BBH", p[0:4])
+        assert_equal(code, EAP_REQUEST)
+        reqtype = unpack("!B", p[4:5])[0]
+        assert_equal(reqtype, EAP_TYPE_TLS)
+        reqdata = TLSFinished(data="")
+        eap_payload = self.eapTLS(EAP_RESPONSE, pkt_id, TLS_LENGTH_INCLUDED, str(reqdata))
+        self.eapol_send(EAPOL_EAPPACKET, eap_payload)
+        self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
+
+    def _eapTlsFinished(self):
+        p = self.eapol_recv()
+        code, pkt_id, eaplen = unpack("!BBH", p[0:4])
+        log.debug("Code %d, id %d, len %d" %(code, pkt_id, eaplen))
+        assert_equal(code, EAP_REQUEST)
+        reqtype = unpack("!B", p[4:5])[0]
+        assert_equal(reqtype, EAP_TYPE_TLS)
+        #We stop here as certification validation success implies auth success
         self.nextEvent = None