aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cryptography/x509/oid.py24
-rw-r--r--tests/test_x509.py73
-rw-r--r--tests/test_x509_ext.py31
3 files changed, 92 insertions, 36 deletions
diff --git a/src/cryptography/x509/oid.py b/src/cryptography/x509/oid.py
index ead40169..ba77a8b8 100644
--- a/src/cryptography/x509/oid.py
+++ b/src/cryptography/x509/oid.py
@@ -12,6 +12,30 @@ class ObjectIdentifier(object):
def __init__(self, dotted_string):
self._dotted_string = dotted_string
+ nodes = self._dotted_string.split(".")
+ intnodes = []
+
+ # There must be at least 2 nodes, the first node must be 0..2, and
+ # if less than 2, the second node cannot have a value outside the
+ # range 0..39. All nodes must be integers.
+ for node in nodes:
+ try:
+ intnodes.append(int(node, 0))
+ except ValueError:
+ raise ValueError(
+ "Malformed OID: %s (non-integer nodes)" % (
+ self._dotted_string))
+
+ if intnodes[0] > 2:
+ raise ValueError(
+ "Malformed OID: %s (first node outside valid range)" % (
+ self._dotted_string))
+
+ if intnodes[0] < 2 and intnodes[1] >= 40:
+ raise ValueError(
+ "Malformed OID: %s (second node outside valid range)" % (
+ self._dotted_string))
+
def __eq__(self, other):
if not isinstance(other, ObjectIdentifier):
return NotImplemented
diff --git a/tests/test_x509.py b/tests/test_x509.py
index 0a1870d5..164aff37 100644
--- a/tests/test_x509.py
+++ b/tests/test_x509.py
@@ -3188,15 +3188,15 @@ class TestNameAttribute(object):
def test_init_bad_value(self):
with pytest.raises(TypeError):
x509.NameAttribute(
- x509.ObjectIdentifier('oid'),
+ x509.ObjectIdentifier('2.999.1'),
b'bytes'
)
def test_eq(self):
assert x509.NameAttribute(
- x509.ObjectIdentifier('oid'), u'value'
+ x509.ObjectIdentifier('2.999.1'), u'value'
) == x509.NameAttribute(
- x509.ObjectIdentifier('oid'), u'value'
+ x509.ObjectIdentifier('2.999.1'), u'value'
)
def test_ne(self):
@@ -3206,12 +3206,12 @@ class TestNameAttribute(object):
x509.ObjectIdentifier('2.5.4.5'), u'value'
)
assert x509.NameAttribute(
- x509.ObjectIdentifier('oid'), u'value'
+ x509.ObjectIdentifier('2.999.1'), u'value'
) != x509.NameAttribute(
- x509.ObjectIdentifier('oid'), u'value2'
+ x509.ObjectIdentifier('2.999.1'), u'value2'
)
assert x509.NameAttribute(
- x509.ObjectIdentifier('oid'), u'value'
+ x509.ObjectIdentifier('2.999.2'), u'value'
) != object()
def test_repr(self):
@@ -3230,64 +3230,83 @@ class TestNameAttribute(object):
class TestObjectIdentifier(object):
def test_eq(self):
- oid1 = x509.ObjectIdentifier('oid')
- oid2 = x509.ObjectIdentifier('oid')
+ oid1 = x509.ObjectIdentifier('2.999.1')
+ oid2 = x509.ObjectIdentifier('2.999.1')
assert oid1 == oid2
def test_ne(self):
- oid1 = x509.ObjectIdentifier('oid')
- assert oid1 != x509.ObjectIdentifier('oid1')
+ oid1 = x509.ObjectIdentifier('2.999.1')
+ assert oid1 != x509.ObjectIdentifier('2.999.2')
assert oid1 != object()
def test_repr(self):
oid = x509.ObjectIdentifier("2.5.4.3")
assert repr(oid) == "<ObjectIdentifier(oid=2.5.4.3, name=commonName)>"
- oid = x509.ObjectIdentifier("oid1")
- assert repr(oid) == "<ObjectIdentifier(oid=oid1, name=Unknown OID)>"
+ oid = x509.ObjectIdentifier("2.999.1")
+ assert repr(oid) == "<ObjectIdentifier(oid=2.999.1, name=Unknown OID)>"
def test_name_property(self):
oid = x509.ObjectIdentifier("2.5.4.3")
assert oid._name == 'commonName'
- oid = x509.ObjectIdentifier("oid1")
+ oid = x509.ObjectIdentifier("2.999.1")
assert oid._name == 'Unknown OID'
+ def test_invalid_input(self):
+ with pytest.raises(ValueError):
+ x509.ObjectIdentifier("notavalidform")
+
+ def test_invalid_node1(self):
+ with pytest.raises(ValueError):
+ x509.ObjectIdentifier("7.1.37")
+
+ def test_invalid_node2(self):
+ with pytest.raises(ValueError):
+ x509.ObjectIdentifier("1.50.200")
+
+ def test_valid(self):
+ x509.ObjectIdentifier("0.35.200")
+ x509.ObjectIdentifier("1.39.999")
+ x509.ObjectIdentifier("2.5.29.3")
+ x509.ObjectIdentifier("2.999.37.5.22.8")
+ x509.ObjectIdentifier("2.25.305821105408246119474742976030998643995")
+
class TestName(object):
def test_eq(self):
name1 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
])
name2 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
])
assert name1 == name2
def test_ne(self):
name1 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
])
name2 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
])
assert name1 != name2
assert name1 != object()
def test_hash(self):
name1 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
])
name2 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
])
name3 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2'),
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1'),
])
assert hash(name1) == hash(name2)
diff --git a/tests/test_x509_ext.py b/tests/test_x509_ext.py
index 8f469366..751de08d 100644
--- a/tests/test_x509_ext.py
+++ b/tests/test_x509_ext.py
@@ -18,7 +18,8 @@ from cryptography.hazmat.backends.interfaces import (
)
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.x509.oid import (
- AuthorityInformationAccessOID, ExtendedKeyUsageOID, ExtensionOID, NameOID
+ AuthorityInformationAccessOID, ExtendedKeyUsageOID,
+ ExtensionOID, NameOID
)
from .hazmat.primitives.test_ec import _skip_curve_unsupported
@@ -603,8 +604,14 @@ class TestAuthorityKeyIdentifier(object):
def test_authority_cert_serial_number_not_integer(self):
dirname = x509.DirectoryName(
x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(
+ x509.ObjectIdentifier('2.999.1'),
+ u'value1'
+ ),
+ x509.NameAttribute(
+ x509.ObjectIdentifier('2.999.2'),
+ u'value2'
+ ),
])
)
with pytest.raises(TypeError):
@@ -617,8 +624,14 @@ class TestAuthorityKeyIdentifier(object):
def test_authority_issuer_not_none_serial_none(self):
dirname = x509.DirectoryName(
x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'),
- x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'),
+ x509.NameAttribute(
+ x509.ObjectIdentifier('2.999.1'),
+ u'value1'
+ ),
+ x509.NameAttribute(
+ x509.ObjectIdentifier('2.999.2'),
+ u'value2'
+ ),
])
)
with pytest.raises(ValueError):
@@ -1166,10 +1179,10 @@ class TestDirectoryName(object):
def test_eq(self):
name = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1')
])
name2 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1')
])
gn = x509.DirectoryName(x509.Name([name]))
gn2 = x509.DirectoryName(x509.Name([name2]))
@@ -1177,10 +1190,10 @@ class TestDirectoryName(object):
def test_ne(self):
name = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1')
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.1'), u'value1')
])
name2 = x509.Name([
- x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value2')
+ x509.NameAttribute(x509.ObjectIdentifier('2.999.2'), u'value2')
])
gn = x509.DirectoryName(x509.Name([name]))
gn2 = x509.DirectoryName(x509.Name([name2]))