Skip to content

Commit 56254f1

Browse files
committed
Merge branch 'fix-oid-roots'
2 parents 8719cc6 + 685a84f commit 56254f1

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
UNRELEASED
5+
----------
6+
7+
* Fix OID encoding/decoding for the first octet according to ITU-T X.690
8+
49
2.7.0 (2023-01-17)
510
------------------
611

src/asn1.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def _encode_object_identifier(self, oid): # type: (str) -> bytes
388388
if not self._re_oid.match(oid):
389389
raise Error('Illegal object identifier')
390390
cmps = list(map(int, oid.split('.')))
391-
if cmps[0] > 39 or cmps[1] > 39:
391+
if (cmps[0] <= 1 and cmps[1] > 39) or cmps[0] > 2:
392392
raise Error('Illegal object identifier')
393393
cmps = [40 * cmps[0] + cmps[1]] + cmps[2:]
394394
cmps.reverse()
@@ -686,9 +686,12 @@ def _decode_object_identifier(bytes_data): # type: (bytes) -> str
686686
if not byte & 0x80:
687687
result.append(value)
688688
value = 0
689-
if len(result) == 0 or result[0] > 1599:
689+
if len(result) == 0:
690690
raise Error('ASN1 syntax error')
691-
result = [result[0] // 40, result[0] % 40] + result[1:]
691+
if result[0] // 40 <= 1:
692+
result = [result[0] // 40, result[0] % 40] + result[1:]
693+
else:
694+
result = [2, result[0] - 80] + result[1:]
692695
result = list(map(str, result))
693696
return str('.'.join(result))
694697

tests/test_asn1.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ def test_object_identifier(self):
177177
def test_long_object_identifier(self):
178178
enc = asn1.Encoder()
179179
enc.start()
180-
enc.write('39.2.3', asn1.Numbers.ObjectIdentifier)
180+
enc.write('2.1482.3', asn1.Numbers.ObjectIdentifier)
181181
res = enc.output()
182182
assert res == b'\x06\x03\x8c\x1a\x03'
183183
enc.start()
184+
enc.write('2.999.3', asn1.Numbers.ObjectIdentifier)
185+
res = enc.output()
186+
assert res == b'\x06\x03\x88\x37\x03'
187+
enc.start()
184188
enc.write('1.39.3', asn1.Numbers.ObjectIdentifier)
185189
res = enc.output()
186190
assert res == b'\x06\x02\x4f\x03'
@@ -341,7 +345,7 @@ def test_error_object_identifier(self):
341345
enc = asn1.Encoder()
342346
enc.start()
343347
pytest.raises(asn1.Error, enc.write, '1', asn1.Numbers.ObjectIdentifier)
344-
pytest.raises(asn1.Error, enc.write, '40.2.3', asn1.Numbers.ObjectIdentifier)
348+
pytest.raises(asn1.Error, enc.write, '3.2.3', asn1.Numbers.ObjectIdentifier)
345349
pytest.raises(asn1.Error, enc.write, '1.40.3', asn1.Numbers.ObjectIdentifier)
346350
pytest.raises(asn1.Error, enc.write, '1.2.3.', asn1.Numbers.ObjectIdentifier)
347351
pytest.raises(asn1.Error, enc.write, '.1.2.3', asn1.Numbers.ObjectIdentifier)
@@ -521,7 +525,11 @@ def test_long_object_identifier(self):
521525
buf = b'\x06\x03\x8c\x1a\x03'
522526
dec.start(buf)
523527
tag, val = dec.read()
524-
assert val == u'39.2.3'
528+
assert val == u'2.1482.3'
529+
buf = b'\x06\x03\x88\x37\x03'
530+
dec.start(buf)
531+
tag, val = dec.read()
532+
assert val == u'2.999.3'
525533
buf = b'\x06\x02\x4f\x03'
526534
dec.start(buf)
527535
tag, val = dec.read()
@@ -745,13 +753,7 @@ def test_error_non_normalized_negative_integer(self):
745753
pytest.raises(asn1.Error, dec.read)
746754

747755
def test_error_non_normalised_object_identifier(self):
748-
buf = b'\x06\x02\x80\x01'
749-
dec = asn1.Decoder()
750-
dec.start(buf)
751-
pytest.raises(asn1.Error, dec.read)
752-
753-
def test_error_object_identifier_with_too_large_first_component(self):
754-
buf = b'\x06\x02\x8c\x40'
756+
buf = b'\x06\x02\x01\x80'
755757
dec = asn1.Decoder()
756758
dec.start(buf)
757759
pytest.raises(asn1.Error, dec.read)
@@ -1079,12 +1081,22 @@ def test_octet_string(self):
10791081
def test_null(self):
10801082
TestEncoderDecoder.assert_encode_decode(None, asn1.Numbers.Null)
10811083

1082-
def test_object_identifier(self):
1084+
def test_real_object_identifier(self):
10831085
TestEncoderDecoder.assert_encode_decode(
10841086
'1.2.840.113554.1.2.1.1',
10851087
asn1.Numbers.ObjectIdentifier
10861088
)
10871089

1090+
def test_long_object_identifier(self):
1091+
for v in \
1092+
(
1093+
'2.60.3',
1094+
'2.999.3',
1095+
'1.39.3',
1096+
'1.2.300000'
1097+
):
1098+
TestEncoderDecoder.assert_encode_decode(v, asn1.Numbers.ObjectIdentifier)
1099+
10881100
def test_enumerated(self):
10891101
for v in (1, 2, 42):
10901102
TestEncoderDecoder.assert_encode_decode(v, asn1.Numbers.Enumerated)

0 commit comments

Comments
 (0)