Skip to content

Commit 2add134

Browse files
committed
Add a method to construct OID from integers
1 parent 73489de commit 2add134

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

gssapi/raw/oids.pyx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
GSSAPI="BASE" # This ensures that a full module is generated by Cython
22

3+
import six
4+
35
from libc.string cimport memcmp, memcpy
46
from libc.stdlib cimport free, malloc
57

@@ -51,6 +53,27 @@ cdef class OID:
5153
memcpy(self.raw_oid.elements, byte_str, self.raw_oid.length)
5254
return 0
5355

56+
@classmethod
57+
def from_int_seq(cls, integer_sequence):
58+
elements=cls._encode_asn1ber(integer_sequence)
59+
return cls(elements=elements)
60+
61+
@staticmethod
62+
def _encode_asn1ber(oid_seq):
63+
if isinstance(oid_seq, six.string_types):
64+
oid_seq = oid_seq.split('.')
65+
oid_seq = [int(x) for x in oid_seq]
66+
if len(oid_seq) < 2:
67+
raise ValueError("Sequence must be 2 or more elements long.")
68+
byte_seq = bytearray([oid_seq[0] * 40 + oid_seq[1]])
69+
for element in oid_seq[2:]:
70+
element_seq = [element & 0x7f]
71+
while element > 127:
72+
element >>= 7
73+
element_seq.insert(0, (element & 0x7f) | 0x80)
74+
byte_seq.extend(element_seq)
75+
return bytes(byte_seq)
76+
5477
def __dealloc__(self):
5578
# NB(directxman12): MIT Kerberos has gss_release_oid
5679
# for this purpose, but it's not in the RFC

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def gssapi_modules(lst):
187187
keywords=['gssapi', 'security'],
188188
install_requires=[
189189
'enum34',
190-
'decorator'
190+
'decorator',
191+
'six'
191192
],
192193
tests_require=[
193194
'tox'

0 commit comments

Comments
 (0)