]> granicus.if.org Git - python/commitdiff
bpo-25404: SSLContext.load_dh_params() non-ASCII path (GH-3459)
authorChristian Heimes <christian@python.org>
Sun, 25 Feb 2018 08:48:02 +0000 (09:48 +0100)
committerGitHub <noreply@github.com>
Sun, 25 Feb 2018 08:48:02 +0000 (09:48 +0100)
SSLContext.load_dh_params() now supports non-ASCII path.

Signed-off-by: Christian Heimes <christian@python.org>
Lib/test/test_ssl.py
Misc/NEWS.d/next/Library/2017-09-08-11-04-10.bpo-25404.pXetCl.rst [new file with mode: 0644]
Modules/_ssl.c

index f172520011f4670ffa0babd90495e94301c72ebc..b59fe73f04c7a0f77b7105d111493e689d4ffca2 100644 (file)
@@ -14,7 +14,7 @@ import gc
 import os
 import errno
 import pprint
-import tempfile
+import shutil
 import urllib2
 import traceback
 import weakref
@@ -1000,6 +1000,10 @@ class ContextTests(unittest.TestCase):
         self.assertEqual(cm.exception.errno, errno.ENOENT)
         with self.assertRaises(ssl.SSLError) as cm:
             ctx.load_dh_params(CERTFILE)
+        with support.temp_dir() as d:
+            fname = os.path.join(d, u'dhpäräm.pem')
+            shutil.copy(DHFILE, fname)
+            ctx.load_dh_params(fname)
 
     @skip_if_broken_ubuntu_ssl
     def test_session_stats(self):
diff --git a/Misc/NEWS.d/next/Library/2017-09-08-11-04-10.bpo-25404.pXetCl.rst b/Misc/NEWS.d/next/Library/2017-09-08-11-04-10.bpo-25404.pXetCl.rst
new file mode 100644 (file)
index 0000000..f816d7a
--- /dev/null
@@ -0,0 +1 @@
+SSLContext.load_dh_params() now supports non-ASCII path.
index f70af266731ad073ce534fe94a99071c15fc0a61..ec61a700b042663fb6c0d9fd7035549795c31bd2 100644 (file)
@@ -2983,13 +2983,25 @@ load_dh_params(PySSLContext *self, PyObject *filepath)
 {
     BIO *bio;
     DH *dh;
-    char *path = PyBytes_AsString(filepath);
-    if (!path) {
-        return NULL;
+    PyObject *filepath_bytes = NULL;
+
+    if (PyString_Check(filepath)) {
+        Py_INCREF(filepath);
+        filepath_bytes = filepath;
+    } else {
+        PyObject *u = PyUnicode_FromObject(filepath);
+        if (!u)
+            return NULL;
+        filepath_bytes = PyUnicode_AsEncodedString(
+            u, Py_FileSystemDefaultEncoding, NULL);
+        Py_DECREF(u);
+        if (!filepath_bytes)
+            return NULL;
     }
 
-    bio = BIO_new_file(path, "r");
+    bio = BIO_new_file(PyBytes_AS_STRING(filepath_bytes), "r");
     if (bio == NULL) {
+        Py_DECREF(filepath_bytes);
         ERR_clear_error();
         PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, filepath);
         return NULL;
@@ -2998,6 +3010,7 @@ load_dh_params(PySSLContext *self, PyObject *filepath)
     PySSL_BEGIN_ALLOW_THREADS
     dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
     BIO_free(bio);
+    Py_DECREF(filepath_bytes);
     PySSL_END_ALLOW_THREADS
     if (dh == NULL) {
         if (errno != 0) {