summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2013-11-17 19:59:14 +0100
committerChristian Heimes <christian@cheimes.de>2013-11-17 19:59:14 +0100
commita6bc95aa0252399c026e749c4a294e2a22912f72 (patch)
tree077efeeb0ba8ac1dc3cc17d9c71239675de2f1bf /Lib
parent35e4ad71baae5469ee678718bac0f9caf7fae740 (diff)
downloadcpython-git-a6bc95aa0252399c026e749c4a294e2a22912f72.tar.gz
Issue #19448: Add private API to SSL module to lookup ASN.1 objects by OID, NID, short name and long name.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ssl.py26
-rw-r--r--Lib/test/test_ssl.py38
2 files changed, 62 insertions, 2 deletions
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 36af637fa0..91956c6a11 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -91,7 +91,7 @@ import textwrap
import re
import sys
import os
-import collections
+from collections import namedtuple
import _ssl # if we can't import it, let the error propagate
@@ -102,6 +102,7 @@ from _ssl import (
SSLSyscallError, SSLEOFError,
)
from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
+from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
from _ssl import RAND_status, RAND_egd, RAND_add, RAND_bytes, RAND_pseudo_bytes
def _import_symbols(prefix):
@@ -256,7 +257,7 @@ def match_hostname(cert, hostname):
"subjectAltName fields were found")
-DefaultVerifyPaths = collections.namedtuple("DefaultVerifyPaths",
+DefaultVerifyPaths = namedtuple("DefaultVerifyPaths",
"cafile capath openssl_cafile_env openssl_cafile openssl_capath_env "
"openssl_capath")
@@ -274,6 +275,27 @@ def get_default_verify_paths():
*parts)
+class _ASN1Object(namedtuple("_ASN1Object", "nid shortname longname oid")):
+ """ASN.1 object identifier lookup
+ """
+ __slots__ = ()
+
+ def __new__(cls, oid):
+ return super().__new__(cls, *_txt2obj(oid, name=False))
+
+ @classmethod
+ def fromnid(cls, nid):
+ """Create _ASN1Object from OpenSSL numeric ID
+ """
+ return super().__new__(cls, *_nid2obj(nid))
+
+ @classmethod
+ def fromname(cls, name):
+ """Create _ASN1Object from short name, long name or OID
+ """
+ return super().__new__(cls, *_txt2obj(name, name=True))
+
+
class SSLContext(_SSLContext):
"""An SSLContext holds various SSL-related configuration options and
data, such as certificates and possibly a private key."""
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index b1cb8c5424..61a4e779c5 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -539,6 +539,44 @@ class BasicSocketTests(unittest.TestCase):
self.assertIsInstance(ca[0][0], bytes)
self.assertIsInstance(ca[0][1], int)
+ def test_asn1object(self):
+ expected = (129, 'serverAuth', 'TLS Web Server Authentication',
+ '1.3.6.1.5.5.7.3.1')
+
+ val = ssl._ASN1Object('1.3.6.1.5.5.7.3.1')
+ self.assertEqual(val, expected)
+ self.assertEqual(val.nid, 129)
+ self.assertEqual(val.shortname, 'serverAuth')
+ self.assertEqual(val.longname, 'TLS Web Server Authentication')
+ self.assertEqual(val.oid, '1.3.6.1.5.5.7.3.1')
+ self.assertIsInstance(val, ssl._ASN1Object)
+ self.assertRaises(ValueError, ssl._ASN1Object, 'serverAuth')
+
+ val = ssl._ASN1Object.fromnid(129)
+ self.assertEqual(val, expected)
+ self.assertIsInstance(val, ssl._ASN1Object)
+ self.assertRaises(ValueError, ssl._ASN1Object.fromnid, -1)
+ self.assertRaises(ValueError, ssl._ASN1Object.fromnid, 100000)
+ for i in range(1000):
+ try:
+ obj = ssl._ASN1Object.fromnid(i)
+ except ValueError:
+ pass
+ else:
+ self.assertIsInstance(obj.nid, int)
+ self.assertIsInstance(obj.shortname, str)
+ self.assertIsInstance(obj.longname, str)
+ self.assertIsInstance(obj.oid, (str, type(None)))
+
+ val = ssl._ASN1Object.fromname('TLS Web Server Authentication')
+ self.assertEqual(val, expected)
+ self.assertIsInstance(val, ssl._ASN1Object)
+ self.assertEqual(ssl._ASN1Object.fromname('serverAuth'), expected)
+ self.assertEqual(ssl._ASN1Object.fromname('1.3.6.1.5.5.7.3.1'),
+ expected)
+ self.assertRaises(ValueError, ssl._ASN1Object.fromname, 'serverauth')
+
+
class ContextTests(unittest.TestCase):
@skip_if_broken_ubuntu_ssl