Skip to content

Commit a5c8edd

Browse files
sigmarisDirectXMan12
authored andcommitted
Display OIDs in dotted form
This commit causes `repr` for OIDs to display the OID in dotted form instead of as the raw ASN.1 BER-encoded bytes. Also-Authored-By: Simo Sorce <simo@redhat.com>
1 parent 8be62cb commit a5c8edd

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

gssapi/raw/oids.pyx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,33 @@ cdef class OID:
7070
def __bytes__(self):
7171
return (<char*>self.raw_oid.elements)[:self.raw_oid.length]
7272

73+
def _decode_asn1ber(self):
74+
ber_encoding = self.__bytes__()
75+
# NB(directxman12): indexing a byte string yields an int in Python 3,
76+
# but yields a substring in Python 2
77+
if six.PY2:
78+
ber_encoding = [ord(c) for c in ber_encoding]
79+
80+
decoded = [ber_encoding[0] // 40, ber_encoding[0] % 40]
81+
pos = 1
82+
value = 0
83+
while pos < len(ber_encoding):
84+
byte = ber_encoding[pos]
85+
if byte & 0x80:
86+
# This is one of the leading bytes
87+
value <<= 7
88+
value += ((byte & 0x7f) * 128)
89+
else:
90+
# This is the last byte of this value
91+
value += (byte & 0x7f)
92+
decoded.append(value)
93+
value = 0
94+
pos += 1
95+
return decoded
96+
7397
def __repr__(self):
74-
return "<OID {0}>".format([ord(c) for c in self.__bytes__()])
98+
dotted_oid = '.'.join(str(x) for x in self._decode_asn1ber())
99+
return "<OID {0}>".format(dotted_oid)
75100

76101
def __hash__(self):
77102
return hash(self.__bytes__())

gssapi/tests/test_raw.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,23 @@ def test_basic_wrap_unwrap(self):
780780
unwrapped_message.should_be_a(bytes)
781781
unwrapped_message.shouldnt_be_empty()
782782
unwrapped_message.should_be(b'test message')
783+
784+
785+
TEST_OIDS = {'SPNEGO': {'bytes': b'\053\006\001\005\005\002',
786+
'string': '1.3.6.1.5.5.2'},
787+
'KRB5': {'bytes': b'\052\206\110\206\367\022\001\002\002',
788+
'string': '1.2.840.113554.1.2.2'},
789+
'KRB5_OLD': {'bytes': b'\053\005\001\005\002',
790+
'string': '1.3.5.1.5.2'},
791+
'KRB5_WRONG': {'bytes': b'\052\206\110\202\367\022\001\002\002',
792+
'string': '1.2.840.48018.1.2.2'},
793+
'IAKERB': {'bytes': b'\053\006\001\005\002\005',
794+
'string': '1.3.6.1.5.2.5'}}
795+
796+
797+
class TestOIDTransforms(unittest.TestCase):
798+
def test_decode_from_bytes(self):
799+
for oid in TEST_OIDS.values():
800+
o = gb.OID(elements=oid['bytes'])
801+
text = repr(o)
802+
text.should_be("<OID {0}>".format(oid['string']))

0 commit comments

Comments
 (0)