From 8cf26425504d22dbcf463ff702a167cbe3567e6a Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 21 Mar 2015 09:50:24 -0500 Subject: basic constraints class & extensions interface --- src/cryptography/x509.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index ad7ebbe0..c053dd61 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -141,6 +141,52 @@ class Name(object): return len(self._attributes) +OID_BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19") + + +@six.add_metaclass(abc.ABCMeta) +class Extension(object): + @abc.abstractproperty + def critical(self): + """ + Returns the boolean value of the critical extension field. + """ + + +@utils.register_interface(Extension) +class BasicConstraints(object): + oid = OID_BASIC_CONSTRAINTS + + def __init__(self, ca, path_length, critical): + if not isinstance(ca, bool): + raise TypeError("ca must be a boolean value") + + if not isinstance(critical, bool): + raise TypeError("critical must be a boolean value") + + if path_length is not None and ca is False: + raise ValueError("path_length must be None when ca is False") + + if path_length is not None and (not isinstance(path_length, int) + or path_length < 0): + raise TypeError( + "path_length must be a non-negative integer or None" + ) + + self._ca = ca + self._path_length = path_length + self._critical = critical + + ca = utils.read_only_property("_ca") + path_length = utils.read_only_property("_path_length") + critical = utils.read_only_property("_critical") + + def __repr__(self): + return "".format( + self.ca, self.path_length, self.critical + ) + + OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6") OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7") -- cgit v1.2.3 From d258d378a388dde070ebe6bfb89400bcdce06e04 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 21 Mar 2015 09:58:07 -0500 Subject: right, {} format support was added in 2.7 --- src/cryptography/x509.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index c053dd61..510e9c6f 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -182,8 +182,10 @@ class BasicConstraints(object): critical = utils.read_only_property("_critical") def __repr__(self): - return "".format( - self.ca, self.path_length, self.critical + return ("").format( + ca=self.ca, path_length=self.path_length, critical=self.critical ) -- cgit v1.2.3 From 8589466c0a12835cda03bf91043cf51b657d9e46 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 22 Mar 2015 13:19:31 -0500 Subject: rework BasicConstraints and Extension. --- src/cryptography/x509.py | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 510e9c6f..d64d61f0 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -42,6 +42,7 @@ _OID_NAMES = { "1.2.840.10040.4.3": "dsa-with-sha1", "2.16.840.1.101.3.4.3.1": "dsa-with-sha224", "2.16.840.1.101.3.4.3.2": "dsa-with-sha256", + "2.5.29.19": "basicConstraints", } @@ -144,26 +145,36 @@ class Name(object): OID_BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19") -@six.add_metaclass(abc.ABCMeta) class Extension(object): - @abc.abstractproperty - def critical(self): - """ - Returns the boolean value of the critical extension field. - """ + def __init__(self, oid, critical, value): + if not isinstance(oid, ObjectIdentifier): + raise TypeError( + "oid argument must be an ObjectIdentifier instance." + ) + if not isinstance(critical, bool): + raise TypeError("critical must be a boolean value") -@utils.register_interface(Extension) -class BasicConstraints(object): - oid = OID_BASIC_CONSTRAINTS + self._oid = oid + self._critical = critical + self._value = value - def __init__(self, ca, path_length, critical): + oid = utils.read_only_property("_oid") + critical = utils.read_only_property("_critical") + value = utils.read_only_property("_value") + + def __repr__(self): + return ("").format( + oid=self.oid, critical=self.critical, value=self.value + ) + + +class BasicConstraints(object): + def __init__(self, ca, path_length): if not isinstance(ca, bool): raise TypeError("ca must be a boolean value") - if not isinstance(critical, bool): - raise TypeError("critical must be a boolean value") - if path_length is not None and ca is False: raise ValueError("path_length must be None when ca is False") @@ -175,17 +186,14 @@ class BasicConstraints(object): self._ca = ca self._path_length = path_length - self._critical = critical ca = utils.read_only_property("_ca") path_length = utils.read_only_property("_path_length") - critical = utils.read_only_property("_critical") def __repr__(self): return ("").format( - ca=self.ca, path_length=self.path_length, critical=self.critical + "path_length={path_length})>").format( + ca=self.ca, path_length=self.path_length ) -- cgit v1.2.3 From 611d3d36fb1e33582eefc81cc241140d7a69f733 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 22 Mar 2015 13:31:18 -0500 Subject: doc update --- src/cryptography/x509.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index d64d61f0..4ba6956e 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -175,7 +175,7 @@ class BasicConstraints(object): if not isinstance(ca, bool): raise TypeError("ca must be a boolean value") - if path_length is not None and ca is False: + if path_length is not None and not ca: raise ValueError("path_length must be None when ca is False") if path_length is not None and (not isinstance(path_length, int) -- cgit v1.2.3 From 58b756969211e2972fb6fda44582e55b98c02924 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 22 Mar 2015 23:24:58 -0500 Subject: doc updates and simplification of __repr__ --- src/cryptography/x509.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 4ba6956e..0c773dac 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -164,10 +164,8 @@ class Extension(object): value = utils.read_only_property("_value") def __repr__(self): - return ("").format( - oid=self.oid, critical=self.critical, value=self.value - ) + return ("").format(self) class BasicConstraints(object): @@ -191,10 +189,8 @@ class BasicConstraints(object): path_length = utils.read_only_property("_path_length") def __repr__(self): - return ("").format( - ca=self.ca, path_length=self.path_length - ) + return ("").format(self) OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") -- cgit v1.2.3 From a5c6e9a89242bb42dbc98f29681d2f74aec12b02 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 23 Mar 2015 19:23:43 -0500 Subject: use kwargs for BasicConstraints creation Also check path_length using integer_types --- src/cryptography/x509.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 0c773dac..43ece920 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -176,8 +176,11 @@ class BasicConstraints(object): if path_length is not None and not ca: raise ValueError("path_length must be None when ca is False") - if path_length is not None and (not isinstance(path_length, int) - or path_length < 0): + if ( + path_length is not None and (not isinstance( + path_length, six.integer_types + ) or path_length < 0) + ): raise TypeError( "path_length must be a non-negative integer or None" ) -- cgit v1.2.3 From 5553d576f3bc3f65b84de99a2561360f82fc110f Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 23 Mar 2015 21:08:01 -0500 Subject: review feedback updates --- src/cryptography/x509.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index 43ece920..556e5a3c 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -177,9 +177,8 @@ class BasicConstraints(object): raise ValueError("path_length must be None when ca is False") if ( - path_length is not None and (not isinstance( - path_length, six.integer_types - ) or path_length < 0) + path_length is not None and + (not isinstance(path_length, six.integer_types) or path_length < 0) ): raise TypeError( "path_length must be a non-negative integer or None" -- cgit v1.2.3