aboutsummaryrefslogtreecommitdiffstats
path: root/src/cryptography/x509
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2017-10-11 08:11:44 +0800
committerAlex Gaynor <alex.gaynor@gmail.com>2017-10-10 20:11:44 -0400
commited32105be8daa27d39e5ef1f26e3f7bc672a7939 (patch)
treef47159bbb207cb68a963420e5500e8c8627d5cb3 /src/cryptography/x509
parent003f56fbd7fe676f6b6bd0a52bbf83f040b4a168 (diff)
downloadcryptography-ed32105be8daa27d39e5ef1f26e3f7bc672a7939.tar.gz
cryptography-ed32105be8daa27d39e5ef1f26e3f7bc672a7939.tar.bz2
cryptography-ed32105be8daa27d39e5ef1f26e3f7bc672a7939.zip
Backwards incompatible change to DNSName (#3951)
* Backwards incompatible change to DNSName During this release cycle we decided to officially deprecate passing U-labels to our GeneralName constructors. At first we tried changing this in a purely backwards compatible way but get_values_for_type made that untenable. This PR modifies DNSName to take three different types. U-label strings (which raises a deprecation warning), A-label strings (the new preferred type), and bytes (which are assumed to be decodable to unicode strings). The latter, while supported, is primarily intended for use by our parser and allows us to return the actual encoded data in a certificate even if it has not been properly encoded to A-label before the certificate is created. (Of course, if the certificate contains invalid utf8 sequences this will still fail, but let's handle one catastrophic failure at a time). * coverage * don't delete that asterisk from a test. it does things. * no bytes in DNSName. Private constructor for bypassing validation * test unicode in dnsname (yuck) * fix docs * empty commit, you disappoint me codecov * CI is the worst
Diffstat (limited to 'src/cryptography/x509')
-rw-r--r--src/cryptography/x509/general_name.py67
1 files changed, 19 insertions, 48 deletions
diff --git a/src/cryptography/x509/general_name.py b/src/cryptography/x509/general_name.py
index 768be3bb..d4d92c88 100644
--- a/src/cryptography/x509/general_name.py
+++ b/src/cryptography/x509/general_name.py
@@ -131,8 +131,8 @@ def _idna_encode(value):
for prefix in ['*.', '.']:
if value.startswith(prefix):
value = value[len(prefix):]
- return prefix.encode('ascii') + idna.encode(value)
- return idna.encode(value)
+ return prefix + idna.encode(value).decode("ascii")
+ return idna.encode(value).decode("ascii")
@utils.register_interface(GeneralName)
@@ -140,73 +140,44 @@ class DNSName(object):
def __init__(self, value):
if isinstance(value, six.text_type):
try:
- value = value.encode("ascii")
+ value.encode("ascii")
except UnicodeEncodeError:
value = _idna_encode(value)
warnings.warn(
- "DNSName values should be passed as idna-encoded bytes, "
- "not strings. Support for passing unicode strings will be "
- "removed in a future version.",
- utils.DeprecatedIn21,
- stacklevel=2,
- )
- else:
- warnings.warn(
- "DNSName values should be passed as bytes, not strings. "
- "Support for passing unicode strings will be removed in a "
- "future version.",
+ "DNSName values should be passed as an A-label string. "
+ "This means unicode characters should be encoded via "
+ "idna. Support for passing unicode strings (aka U-label) "
+ " will be removed in a future version.",
utils.DeprecatedIn21,
stacklevel=2,
)
- elif not isinstance(value, bytes):
- raise TypeError("value must be bytes")
+ else:
+ raise TypeError("value must be string")
- self._bytes_value = value
+ self._value = value
- bytes_value = utils.read_only_property("_bytes_value")
+ value = utils.read_only_property("_value")
- @property
- def value(self):
- warnings.warn(
- "DNSName.bytes_value should be used instead of DNSName.value; it "
- "contains the DNS name as raw bytes, instead of as an idna-decoded"
- " unicode string. DNSName.value will be removed in a future "
- "version.",
- utils.DeprecatedIn21,
- stacklevel=2
- )
- data = self._bytes_value
- if not data:
- decoded = u""
- elif data.startswith(b"*."):
- # This is a wildcard name. We need to remove the leading wildcard,
- # IDNA decode, then re-add the wildcard. Wildcard characters should
- # always be left-most (RFC 2595 section 2.4).
- decoded = u"*." + idna.decode(data[2:])
- else:
- # Not a wildcard, decode away. If the string has a * in it anywhere
- # invalid this will raise an InvalidCodePoint
- decoded = idna.decode(data)
- if data.startswith(b"."):
- # idna strips leading periods. Name constraints can have that
- # so we need to re-add it. Sigh.
- decoded = u"." + decoded
- return decoded
+ @classmethod
+ def _init_without_validation(cls, value):
+ instance = cls.__new__(cls)
+ instance._value = value
+ return instance
def __repr__(self):
- return "<DNSName(bytes_value={0!r})>".format(self.bytes_value)
+ return "<DNSName(value={0!r})>".format(self.value)
def __eq__(self, other):
if not isinstance(other, DNSName):
return NotImplemented
- return self.bytes_value == other.bytes_value
+ return self.value == other.value
def __ne__(self, other):
return not self == other
def __hash__(self):
- return hash(self.bytes_value)
+ return hash(self.value)
@utils.register_interface(GeneralName)