diff options
-rw-r--r-- | docs/x509.rst | 10 | ||||
-rw-r--r-- | src/cryptography/x509.py | 66 | ||||
-rw-r--r-- | tests/test_x509.py | 51 |
3 files changed, 68 insertions, 59 deletions
diff --git a/docs/x509.rst b/docs/x509.rst index 42b95a11..5ad69972 100644 --- a/docs/x509.rst +++ b/docs/x509.rst @@ -181,11 +181,11 @@ X.509 Certificate Object For version 3 X.509 certificates. -.. class:: Attribute +.. class:: NameAttribute .. versionadded:: 0.8 - An X.509 name consists of a list of Attribute objects. + An X.509 name consists of a list of NameAttribute objects. .. attribute:: oid @@ -204,7 +204,7 @@ X.509 Certificate Object .. versionadded:: 0.8 Object identifiers (frequently seen abbreviated as OID) identify the type - of a value (see: :class:`Attribute`). + of a value (see: :class:`NameAttribute`). .. attribute:: value @@ -261,9 +261,9 @@ Exceptions Returns the raw version that was parsed from the certificate. -.. class:: UnknownAttribute +.. class:: UnknownNameAttribute - This is raised when an X.509 certificate has an unknown attribute in a + This is raised when an X.509 certificate has an unknown name attribute in a distinguished name. diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 8cdf7db5..a1f8c5b2 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -12,6 +12,25 @@ import six from cryptography import utils +_OID_NAMES = { + "2.5.4.3": "commonName", + "2.5.4.6": "countryName", + "2.5.4.7": "localityName", + "2.5.4.8": "stateOrProvinceName", + "2.5.4.10": "organizationName", + "2.5.4.11": "organizationalUnitName", + "2.5.4.5": "serialNumber", + "2.5.4.4": "surname", + "2.5.4.42": "givenName", + "2.5.4.12": "title", + "2.5.4.44": "generationQualifier", + "2.5.4.46": "dnQualifier", + "2.5.4.65": "pseudonym", + "0.9.2342.19200300.100.1.25": "domainComponent", + "1.2.840.113549.1.9.1": "emailAddress", +} + + class Version(Enum): v1 = 0 v3 = 2 @@ -31,11 +50,11 @@ class InvalidVersion(Exception): self.parsed_version = parsed_version -class UnknownAttribute(Exception): +class UnknownNameAttribute(Exception): pass -class Attribute(object): +class NameAttribute(object): def __init__(self, oid, value): if not isinstance(oid, ObjectIdentifier): raise TypeError("oid argument must be an ObjectIdentifier object") @@ -47,7 +66,7 @@ class Attribute(object): value = utils.read_only_property("_value") def __eq__(self, other): - if not isinstance(other, Attribute): + if not isinstance(other, NameAttribute): return NotImplemented return ( @@ -60,46 +79,41 @@ class Attribute(object): class ObjectIdentifier(object): - def __init__(self, oid, name): + def __init__(self, oid): self._value = oid - self._name = name def __eq__(self, other): if not isinstance(other, ObjectIdentifier): return NotImplemented - return self._value == other._value and self._name == other._name + return self._value == other._value def __ne__(self, other): return not self == other def __repr__(self): return "<ObjectIdentifier(oid={0}, name={1})>".format( - self._value, self._name + self._value, _OID_NAMES.get(self._value, "Unknown OID") ) value = utils.read_only_property("_value") -OID_COMMON_NAME = ObjectIdentifier("2.5.4.3", "commonName") -OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6", "countryName") -OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7", "localityName") -OID_STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8", "stateOrProvinceName") -OID_ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10", "organizationName") -OID_ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier( - "2.5.4.11", "organizationalUnitName" -) -OID_SERIAL_NUMBER = ObjectIdentifier("2.5.4.5", "serialNumber") -OID_SURNAME = ObjectIdentifier("2.5.4.4", "surname") -OID_GIVEN_NAME = ObjectIdentifier("2.5.4.42", "givenName") -OID_TITLE = ObjectIdentifier("2.5.4.12", "title") -OID_GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44", "generationQualifier") -OID_DN_QUALIFIER = ObjectIdentifier("2.5.4.46", "dnQualifier") -OID_PSEUDONYM = ObjectIdentifier("2.5.4.65", "pseudonym") -OID_DOMAIN_COMPONENT = ObjectIdentifier( - "0.9.2342.19200300.100.1.25", "domainComponent" -) -OID_EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1", "emailAddress") +OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") +OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6") +OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7") +OID_STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8") +OID_ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10") +OID_ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11") +OID_SERIAL_NUMBER = ObjectIdentifier("2.5.4.5") +OID_SURNAME = ObjectIdentifier("2.5.4.4") +OID_GIVEN_NAME = ObjectIdentifier("2.5.4.42") +OID_TITLE = ObjectIdentifier("2.5.4.12") +OID_GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44") +OID_DN_QUALIFIER = ObjectIdentifier("2.5.4.46") +OID_PSEUDONYM = ObjectIdentifier("2.5.4.65") +OID_DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25") +OID_EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1") @six.add_metaclass(abc.ABCMeta) diff --git a/tests/test_x509.py b/tests/test_x509.py index f8066699..cf583247 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -250,48 +250,43 @@ class TestECDSACertificate(object): cert.public_key() -class TestAttribute(object): +class TestNameAttribute(object): def test_eq(self): - assert x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' - ) == x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' + assert x509.NameAttribute( + x509.ObjectIdentifier('oid'), 'value' + ) == x509.NameAttribute( + x509.ObjectIdentifier('oid'), 'value' ) def test_ne(self): - assert x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' - ) != x509.Attribute( - x509.ObjectIdentifier('oid2', 'name'), 'value' + assert x509.NameAttribute( + x509.ObjectIdentifier('2.5.4.3'), 'value' + ) != x509.NameAttribute( + x509.ObjectIdentifier('2.5.4.5'), 'value' ) - assert x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' - ) != x509.Attribute( - x509.ObjectIdentifier('oid', 'name2'), 'value' + assert x509.NameAttribute( + x509.ObjectIdentifier('oid'), 'value' + ) != x509.NameAttribute( + x509.ObjectIdentifier('oid'), 'value2' ) - assert x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' - ) != x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value2' - ) - assert x509.Attribute( - x509.ObjectIdentifier('oid', 'name'), 'value' + assert x509.NameAttribute( + x509.ObjectIdentifier('oid'), 'value' ) != object() class TestObjectIdentifier(object): def test_eq(self): - oid1 = x509.ObjectIdentifier('oid', 'name') - oid2 = x509.ObjectIdentifier('oid', 'name') + oid1 = x509.ObjectIdentifier('oid') + oid2 = x509.ObjectIdentifier('oid') assert oid1 == oid2 def test_ne(self): - oid1 = x509.ObjectIdentifier('oid', 'name') - assert oid1 != x509.ObjectIdentifier('oid1', 'name') - assert oid1 != x509.ObjectIdentifier('oid', 'name1') - assert oid1 != x509.ObjectIdentifier('oid1', 'name1') + oid1 = x509.ObjectIdentifier('oid') + assert oid1 != x509.ObjectIdentifier('oid1') assert oid1 != object() def test_repr(self): - oid = x509.ObjectIdentifier("oid1", "name") - assert repr(oid) == "<ObjectIdentifier(oid=oid1, name=name)>" + 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)>" |