File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff 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__())
Original file line number Diff line number Diff 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' ]))
You can’t perform that action at this time.
0 commit comments