git-svn-id: https://duplicati.googlecode.com/svn/trunk@11 59da171f-624f-0410-aa54-27559c288bec
121 lines
4.6 KiB
Python
121 lines
4.6 KiB
Python
# Some tests of the win32security sspi functions.
|
|
# Stolen from Roger's original test_sspi.c, a version of which is in "Demos"
|
|
# See also the other SSPI demos.
|
|
import win32security, sspi, sspicon, win32api
|
|
import unittest
|
|
|
|
class TestSSPI(unittest.TestCase):
|
|
|
|
def assertRaisesHRESULT(self, hr, func, *args):
|
|
try:
|
|
return func(*args)
|
|
raise RuntimeError, "expecting %s failure" % (hr,)
|
|
except win32security.error, (hr_got, func, msg):
|
|
self.failUnlessEqual(hr_got, hr)
|
|
|
|
def _doAuth(self, pkg_name):
|
|
sspiclient=sspi.ClientAuth(pkg_name,targetspn=win32api.GetUserName())
|
|
sspiserver=sspi.ServerAuth(pkg_name)
|
|
|
|
sec_buffer=None
|
|
err = 1
|
|
while err != 0:
|
|
err, sec_buffer = sspiclient.authorize(sec_buffer)
|
|
err, sec_buffer = sspiserver.authorize(sec_buffer)
|
|
return sspiclient, sspiserver
|
|
|
|
def _doTestImpersonate(self, pkg_name):
|
|
# Just for the sake of code exercising!
|
|
sspiclient, sspiserver = self._doAuth(pkg_name)
|
|
sspiserver.ctxt.ImpersonateSecurityContext()
|
|
sspiserver.ctxt.RevertSecurityContext()
|
|
|
|
def testImpersonateKerberos(self):
|
|
self._doTestImpersonate("Kerberos")
|
|
|
|
def testImpersonateNTLM(self):
|
|
self._doTestImpersonate("NTLM")
|
|
|
|
def _doTestEncrypt(self, pkg_name):
|
|
|
|
sspiclient, sspiserver = self._doAuth(pkg_name)
|
|
|
|
pkg_size_info=sspiclient.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES)
|
|
msg='some data to be encrypted ......'
|
|
|
|
trailersize=pkg_size_info['SecurityTrailer']
|
|
encbuf=win32security.PySecBufferDescType()
|
|
encbuf.append(win32security.PySecBufferType(len(msg), sspicon.SECBUFFER_DATA))
|
|
encbuf.append(win32security.PySecBufferType(trailersize, sspicon.SECBUFFER_TOKEN))
|
|
encbuf[0].Buffer=msg
|
|
sspiclient.ctxt.EncryptMessage(0,encbuf,1)
|
|
sspiserver.ctxt.DecryptMessage(encbuf,1)
|
|
self.failUnlessEqual(msg, encbuf[0].Buffer)
|
|
# and test the higher-level functions
|
|
data, sig = sspiclient.encrypt("hello")
|
|
self.assertEqual(sspiserver.decrypt(data, sig), "hello")
|
|
|
|
data, sig = sspiserver.encrypt("hello")
|
|
self.assertEqual(sspiclient.decrypt(data, sig), "hello")
|
|
|
|
def testEncryptNTLM(self):
|
|
self._doTestEncrypt("NTLM")
|
|
|
|
def testEncryptKerberos(self):
|
|
self._doTestEncrypt("Kerberos")
|
|
|
|
def _doTestSign(self, pkg_name):
|
|
|
|
sspiclient, sspiserver = self._doAuth(pkg_name)
|
|
|
|
pkg_size_info=sspiclient.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES)
|
|
msg='some data to be encrypted ......'
|
|
|
|
sigsize=pkg_size_info['MaxSignature']
|
|
sigbuf=win32security.PySecBufferDescType()
|
|
sigbuf.append(win32security.PySecBufferType(len(msg), sspicon.SECBUFFER_DATA))
|
|
sigbuf.append(win32security.PySecBufferType(sigsize, sspicon.SECBUFFER_TOKEN))
|
|
sigbuf[0].Buffer=msg
|
|
sspiclient.ctxt.MakeSignature(0,sigbuf,0)
|
|
sspiserver.ctxt.VerifySignature(sigbuf,0)
|
|
# and test the higher-level functions
|
|
sspiclient.next_seq_num = 1
|
|
sspiserver.next_seq_num = 1
|
|
key = sspiclient.sign("hello")
|
|
sspiserver.verify("hello", key)
|
|
key = sspiclient.sign("hello")
|
|
self.assertRaisesHRESULT(sspicon.SEC_E_MESSAGE_ALTERED,
|
|
sspiserver.verify, "hellox", key)
|
|
|
|
# and the other way
|
|
key = sspiserver.sign("hello")
|
|
sspiclient.verify("hello", key)
|
|
key = sspiserver.sign("hello")
|
|
self.assertRaisesHRESULT(sspicon.SEC_E_MESSAGE_ALTERED,
|
|
sspiclient.verify, "hellox", key)
|
|
|
|
def testSignNTLM(self):
|
|
self._doTestSign("NTLM")
|
|
|
|
def testSignKerberos(self):
|
|
self._doTestSign("Kerberos")
|
|
|
|
def testSequenceSign(self):
|
|
# Only Kerberos supports sequence detection.
|
|
sspiclient, sspiserver = self._doAuth("Kerberos")
|
|
key = sspiclient.sign("hello")
|
|
sspiclient.sign("hello")
|
|
self.assertRaisesHRESULT(sspicon.SEC_E_OUT_OF_SEQUENCE,
|
|
sspiserver.verify, 'hello', key)
|
|
|
|
def testSequenceEncrypt(self):
|
|
# Only Kerberos supports sequence detection.
|
|
sspiclient, sspiserver = self._doAuth("Kerberos")
|
|
blob, key = sspiclient.encrypt("hello",)
|
|
blob, key = sspiclient.encrypt("hello")
|
|
self.assertRaisesHRESULT(sspicon.SEC_E_OUT_OF_SEQUENCE,
|
|
sspiserver.decrypt, blob, key)
|
|
|
|
if __name__=='__main__':
|
|
unittest.main()
|