Skip to content

Commit

Permalink
bpo-25404: SSLContext.load_dh_params() non-ASCII path (GH-3459)
Browse files Browse the repository at this point in the history
SSLContext.load_dh_params() now supports non-ASCII path.

Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran committed Feb 25, 2018
1 parent 8d4d173 commit 6e8f395
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Lib/test/test_ssl.py
Expand Up @@ -14,7 +14,7 @@
import os
import errno
import pprint
import tempfile
import shutil
import urllib2
import traceback
import weakref
Expand Down Expand Up @@ -1000,6 +1000,10 @@ def test_load_dh_params(self):
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):
Expand Down
@@ -0,0 +1 @@
SSLContext.load_dh_params() now supports non-ASCII path.
21 changes: 17 additions & 4 deletions Modules/_ssl.c
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 6e8f395

Please sign in to comment.