home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.7)
-
- import win32security
- import sspicon
- error = win32security.error
-
- class _BaseAuth(object):
-
- def __init__(self):
- self.reset()
-
-
- def reset(self):
- self.ctxt = None
- self.authenticated = False
- self.next_seq_num = 0
-
-
- def _get_next_seq_num(self):
- ret = self.next_seq_num
- self.next_seq_num = self.next_seq_num + 1
- return ret
-
-
- def encrypt(self, data):
- pkg_size_info = self.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES)
- trailersize = pkg_size_info['SecurityTrailer']
- encbuf = win32security.PySecBufferDescType()
- encbuf.append(win32security.PySecBufferType(len(data), sspicon.SECBUFFER_DATA))
- encbuf.append(win32security.PySecBufferType(trailersize, sspicon.SECBUFFER_TOKEN))
- encbuf[0].Buffer = data
- self.ctxt.EncryptMessage(0, encbuf, self._get_next_seq_num())
- return (encbuf[0].Buffer, encbuf[1].Buffer)
-
-
- def decrypt(self, data, trailer):
- encbuf = win32security.PySecBufferDescType()
- encbuf.append(win32security.PySecBufferType(len(data), sspicon.SECBUFFER_DATA))
- encbuf.append(win32security.PySecBufferType(len(trailer), sspicon.SECBUFFER_TOKEN))
- encbuf[0].Buffer = data
- encbuf[1].Buffer = trailer
- self.ctxt.DecryptMessage(encbuf, self._get_next_seq_num())
- return encbuf[0].Buffer
-
-
- def sign(self, data):
- pkg_size_info = self.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES)
- sigsize = pkg_size_info['MaxSignature']
- sigbuf = win32security.PySecBufferDescType()
- sigbuf.append(win32security.PySecBufferType(len(data), sspicon.SECBUFFER_DATA))
- sigbuf.append(win32security.PySecBufferType(sigsize, sspicon.SECBUFFER_TOKEN))
- sigbuf[0].Buffer = data
- self.ctxt.MakeSignature(0, sigbuf, self._get_next_seq_num())
- return sigbuf[1].Buffer
-
-
- def verify(self, data, sig):
- sigbuf = win32security.PySecBufferDescType()
- sigbuf.append(win32security.PySecBufferType(len(data), sspicon.SECBUFFER_DATA))
- sigbuf.append(win32security.PySecBufferType(len(sig), sspicon.SECBUFFER_TOKEN))
- sigbuf[0].Buffer = data
- sigbuf[1].Buffer = sig
- self.ctxt.VerifySignature(sigbuf, self._get_next_seq_num())
-
-
-
- class ClientAuth(_BaseAuth):
-
- def __init__(self, pkg_name, client_name = None, auth_info = None, targetspn = None, scflags = None, datarep = sspicon.SECURITY_NETWORK_DREP):
- if scflags is None:
- scflags = sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_SEQUENCE_DETECT | sspicon.ISC_REQ_REPLAY_DETECT | sspicon.ISC_REQ_CONFIDENTIALITY
- self.scflags = scflags
- self.datarep = datarep
- self.targetspn = targetspn
- self.pkg_info = win32security.QuerySecurityPackageInfo(pkg_name)
- (self.credentials, self.credentials_expiry) = win32security.AcquireCredentialsHandle(client_name, self.pkg_info['Name'], sspicon.SECPKG_CRED_OUTBOUND, None, auth_info)
- _BaseAuth.__init__(self)
-
-
- def authorize(self, sec_buffer_in):
- if sec_buffer_in is not None and type(sec_buffer_in) != win32security.PySecBufferDescType:
- sec_buffer_new = win32security.PySecBufferDescType()
- tokenbuf = win32security.PySecBufferType(self.pkg_info['MaxToken'], sspicon.SECBUFFER_TOKEN)
- tokenbuf.Buffer = sec_buffer_in
- sec_buffer_new.append(tokenbuf)
- sec_buffer_in = sec_buffer_new
- sec_buffer_out = win32security.PySecBufferDescType()
- tokenbuf = win32security.PySecBufferType(self.pkg_info['MaxToken'], sspicon.SECBUFFER_TOKEN)
- sec_buffer_out.append(tokenbuf)
- ctxtin = self.ctxt
- if self.ctxt is None:
- self.ctxt = win32security.PyCtxtHandleType()
- (err, attr, exp) = win32security.InitializeSecurityContext(self.credentials, ctxtin, self.targetspn, self.scflags, self.datarep, sec_buffer_in, self.ctxt, sec_buffer_out)
- self.ctxt_attr = attr
- self.ctxt_expiry = exp
- if err in (sspicon.SEC_I_COMPLETE_NEEDED, sspicon.SEC_I_COMPLETE_AND_CONTINUE):
- self.ctxt.CompleteAuthToken(sec_buffer_out)
- self.authenticated = err == 0
- return (err, sec_buffer_out)
-
-
-
- class ServerAuth(_BaseAuth):
-
- def __init__(self, pkg_name, spn = None, scflags = None, datarep = sspicon.SECURITY_NETWORK_DREP):
- self.spn = spn
- self.datarep = datarep
- if scflags is None:
- scflags = sspicon.ASC_REQ_INTEGRITY | sspicon.ASC_REQ_SEQUENCE_DETECT | sspicon.ASC_REQ_REPLAY_DETECT | sspicon.ASC_REQ_CONFIDENTIALITY
- self.scflags = scflags
- self.pkg_info = win32security.QuerySecurityPackageInfo(pkg_name)
- (self.credentials, self.credentials_expiry) = win32security.AcquireCredentialsHandle(spn, self.pkg_info['Name'], sspicon.SECPKG_CRED_INBOUND, None, None)
- _BaseAuth.__init__(self)
-
-
- def authorize(self, sec_buffer_in):
- if sec_buffer_in is not None and type(sec_buffer_in) != win32security.PySecBufferDescType:
- sec_buffer_new = win32security.PySecBufferDescType()
- tokenbuf = win32security.PySecBufferType(self.pkg_info['MaxToken'], sspicon.SECBUFFER_TOKEN)
- tokenbuf.Buffer = sec_buffer_in
- sec_buffer_new.append(tokenbuf)
- sec_buffer_in = sec_buffer_new
- sec_buffer_out = win32security.PySecBufferDescType()
- tokenbuf = win32security.PySecBufferType(self.pkg_info['MaxToken'], sspicon.SECBUFFER_TOKEN)
- sec_buffer_out.append(tokenbuf)
- ctxtin = self.ctxt
- if self.ctxt is None:
- self.ctxt = win32security.PyCtxtHandleType()
- (err, attr, exp) = win32security.AcceptSecurityContext(self.credentials, ctxtin, sec_buffer_in, self.scflags, self.datarep, self.ctxt, sec_buffer_out)
- self.ctxt_attr = attr
- self.ctxt_expiry = exp
- if err in (sspicon.SEC_I_COMPLETE_NEEDED, sspicon.SEC_I_COMPLETE_AND_CONTINUE):
- self.ctxt.CompleteAuthToken(sec_buffer_out)
- self.authenticated = err == 0
- return (err, sec_buffer_out)
-
-
- if __name__ == '__main__':
- sspiclient = ClientAuth('NTLM')
- sspiserver = ServerAuth('NTLM')
- sec_buffer = None
- while None:
- (err, sec_buffer) = sspiclient.authorize(sec_buffer)
- (err, sec_buffer) = sspiserver.authorize(sec_buffer)
- if err == 0:
- break
- continue
- continue
- data = 'hello'.encode('ascii')
- sig = sspiclient.sign(data)
- sspiserver.verify(data, sig)
- (data, key) = sspiclient.encrypt(data)
- print 'cool!'
- return None
-