| 1 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 2 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 3 |
|
| 4 |
import enum |
| 5 |
import struct |
| 6 |
import typing |
| 7 |
|
| 8 |
|
| 9 |
def _pack_value(addr_type: typing.Optional["AddressType"], b: typing.Optional[bytes]) -> bytes: |
| 10 |
"""Packs an type/data entry into the byte structure required.""" |
| 11 |
if not b: |
| 12 |
b = b"" |
| 13 |
|
| 14 |
return (struct.pack("<I", addr_type) if addr_type is not None else b"") + struct.pack("<I", len(b)) + b |
| 15 |
|
| 16 |
|
| 17 |
def _unpack_value(b_mem: memoryview, offset: int) -> typing.Tuple[bytes, int]: |
| 18 |
"""Unpacks a raw C struct value to a byte string.""" |
| 19 |
length = struct.unpack("<I", b_mem[offset : offset + 4].tobytes())[0] |
| 20 |
new_offset = offset + length + 4 |
| 21 |
|
| 22 |
data = b"" |
| 23 |
if length: |
| 24 |
data = b_mem[offset + 4 : offset + 4 + length].tobytes() |
| 25 |
|
| 26 |
return data, new_offset |
| 27 |
|
| 28 |
|
| 29 |
class AddressType(enum.IntEnum): |
| 30 |
unspecified = 0 # GSS_C_AF_UNSPEC |
| 31 |
local = 1 # GSS_C_AF_LOCAL |
| 32 |
inet = 2 # GSS_C_AF_INET |
| 33 |
implink = 3 # GSS_C_AF_IMPLINK |
| 34 |
pup = 4 # GSS_C_AF_PUP |
| 35 |
chaos = 5 # GSS_C_AF_CHAOS |
| 36 |
ns = 6 # GSS_C_AF_NS |
| 37 |
nbs = 8 # GSS_C_AF_NBS |
| 38 |
ecma = 8 # GSS_C_AF_ECMA |
| 39 |
datakit = 9 # GSS_C_AF_DATAKIT |
| 40 |
ccitt = 10 # GSS_C_AF_CCITT |
| 41 |
sna = 11 # GSS_C_AF_SNA |
| 42 |
decnet = 12 # GSS_C_AF_DECnet |
| 43 |
dli = 13 # GSS_C_AF_DLI |
| 44 |
lat = 14 # GSS_C_AF_LAT |
| 45 |
hylink = 15 # GSS_C_AF_HYLINK |
| 46 |
appletalk = 16 # GSS_C_AF_APPLETALK |
| 47 |
bsc = 17 # GSS_C_AF_BSC |
| 48 |
dss = 18 # GSS_C_AF_DSS |
| 49 |
osi = 19 # GSS_C_AF_OSI |
| 50 |
x25 = 21 # GSS_C_AF_X25 |
| 51 |
inet6 = 24 # GSS_C_AF_INET6 |
| 52 |
nulladdr = 255 # GSS_C_AF_NULLADDR |
| 53 |
|
| 54 |
|
| 55 |
class GssChannelBindings: |
| 56 |
"""Python representation of a GSSAPI Channel Binding data structure. |
| 57 |
|
| 58 |
A common representation for a GSSAPI Channel Binding data structure that can be passed into a context to bind |
| 59 |
against that security context. Channel bindings are tags that identify the particular data channel that is used. |
| 60 |
Because these tags are specific to the originator and recipient applications, they offer more proof of a valid |
| 61 |
identity. Most HTTPS based authentications just set the application data to b'tls-server-end-point:<cert hash>'. |
| 62 |
|
| 63 |
Args: |
| 64 |
initiator_addrtype: The address type of the initiator address. |
| 65 |
initiator_address: The initiator's address. |
| 66 |
acceptor_addrtype: The address type of the acceptor address. |
| 67 |
acceptor_address: The acceptor's address. |
| 68 |
application_data: Any extra application data to set on the bindings struct. |
| 69 |
""" |
| 70 |
|
| 71 |
def __init__( |
| 72 |
self, |
| 73 |
initiator_addrtype: AddressType = AddressType.unspecified, |
| 74 |
initiator_address: typing.Optional[bytes] = None, |
| 75 |
acceptor_addrtype: AddressType = AddressType.unspecified, |
| 76 |
acceptor_address: typing.Optional[bytes] = None, |
| 77 |
application_data: typing.Optional[bytes] = None, |
| 78 |
) -> None: |
| 79 |
self.initiator_addrtype = AddressType(initiator_addrtype) |
| 80 |
self.initiator_address = initiator_address |
| 81 |
self.acceptor_addrtype = AddressType(acceptor_addrtype) |
| 82 |
self.acceptor_address = acceptor_address |
| 83 |
self.application_data = application_data |
| 84 |
|
| 85 |
def __repr__(self) -> str: |
| 86 |
return ( |
| 87 |
"{0}.{1} initiator_addrtype={2}|initiator_address={3}|acceptor_addrtype={4}|acceptor_address={5}|" |
| 88 |
"application_data={6}".format( |
| 89 |
type(self).__module__, |
| 90 |
type(self).__name__, |
| 91 |
repr(self.initiator_addrtype), |
| 92 |
repr(self.initiator_address), |
| 93 |
repr(self.acceptor_addrtype), |
| 94 |
repr(self.acceptor_address), |
| 95 |
repr(self.application_data), |
| 96 |
) |
| 97 |
) |
| 98 |
|
| 99 |
def __str__(self) -> str: |
| 100 |
return "{0} initiator_addr({1}.{2}|{3!r}) | acceptor_addr({4}.{5}|{6!r}) | application_data({7!r})".format( |
| 101 |
type(self).__name__, |
| 102 |
type(self.initiator_addrtype).__name__, |
| 103 |
self.initiator_addrtype.name, |
| 104 |
self.initiator_address, |
| 105 |
type(self.acceptor_addrtype).__name__, |
| 106 |
self.acceptor_addrtype.name, |
| 107 |
self.acceptor_address, |
| 108 |
self.application_data, |
| 109 |
) |
| 110 |
|
| 111 |
def __eq__(self, other: object) -> bool: |
| 112 |
if not isinstance(other, (bytes, GssChannelBindings)): |
| 113 |
return False |
| 114 |
|
| 115 |
if isinstance(other, GssChannelBindings): |
| 116 |
other = other.pack() |
| 117 |
|
| 118 |
return self.pack() == other |
| 119 |
|
| 120 |
def pack(self) -> bytes: |
| 121 |
"""Pack struct into a byte string.""" |
| 122 |
return b"".join( |
| 123 |
[ |
| 124 |
_pack_value(self.initiator_addrtype, self.initiator_address), |
| 125 |
_pack_value(self.acceptor_addrtype, self.acceptor_address), |
| 126 |
_pack_value(None, self.application_data), |
| 127 |
] |
| 128 |
) |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
def unpack(b_data: bytes) -> "GssChannelBindings": |
| 132 |
b_mem = memoryview(b_data) |
| 133 |
|
| 134 |
initiator_addrtype = struct.unpack("<I", b_mem[:4].tobytes())[0] |
| 135 |
initiator_address, offset = _unpack_value(b_mem, 4) |
| 136 |
|
| 137 |
acceptor_addrtype = struct.unpack("<I", b_mem[offset : offset + 4].tobytes())[0] |
| 138 |
acceptor_address, offset = _unpack_value(b_mem, offset + 4) |
| 139 |
|
| 140 |
application_data = _unpack_value(b_mem, offset)[0] |
| 141 |
|
| 142 |
return GssChannelBindings( |
| 143 |
initiator_addrtype=initiator_addrtype, |
| 144 |
initiator_address=initiator_address, |
| 145 |
acceptor_addrtype=acceptor_addrtype, |
| 146 |
acceptor_address=acceptor_address, |
| 147 |
application_data=application_data, |
| 148 |
) |