8000 gh-144249: Report filename in SSLContext.load_cert_chain errors · romuald/cpython@b196c8f · GitHub
[go: up one dir, main page]

Skip to content

Commit b196c8f

Browse files
committed
pythongh-144249: Report filename in SSLContext.load_cert_chain errors
When user tries to load a certificate chain, attach the related filename to the exception being raised. Improving user experience
1 parent 3e9a5b0 commit b196c8f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Lib/test/test_ssl.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def data_file(*name):
131131
EMPTYCERT = data_file("nullcert.pem")
132132
BADCERT = data_file("badcert.pem")
133133
NONEXISTINGCERT = data_file("XXXnonexisting.pem")
134+
NONEXISTINGKEY = data_file("XXXnonexistingkey.pem")
134135
BADKEY = data_file("badkey.pem")
135136
NOKIACERT = data_file("nokia.pem")
136137
NULLBYTECERT = data_file("nullbytecert.pem")
@@ -1229,6 +1230,11 @@ def test_load_cert_chain(self):
12291230
with self.assertRaises(OSError) as cm:
12301231
ctx.load_cert_chain(NONEXISTINGCERT)
12311232
self.assertEqual(cm.exception.errno, errno.ENOENT)
1233+
self.assertEqual(cm.exception.filename, NONEXISTINGCERT)
1234+
with self.assertRaises(OSError) as cm:
1235+
ctx.load_cert_chain(CERTFILE, keyfile=NONEXISTINGKEY)
1236+
self.assertEqual(cm.exception.errno, errno.ENOENT)
1237+
self.assertEqual(cm.exception.filename, NONEXISTINGKEY)
12321238
with self.assertRaisesRegex(ssl.SSLError, "PEM (lib|routines)"):
12331239
ctx.load_cert_chain(BADCERT)
12341240
with self.assertRaisesRegex(ssl.SSLError, "PEM (lib|routines)"):

Modules/_ssl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,7 +4517,8 @@ load_cert_chain_lock_held(PySSLContext *self, _PySSLPasswordInfo *pw_info,
45174517
/* the password callback has already set the error information */
45184518
}
45194519
else if (errno != 0) {
4520-
PyErr_SetFromErrno(PyExc_OSError);
4520+
PyErr_SetFromErrnoWithFilename(PyExc_OSError,
4521+
PyBytes_AS_STRING(certfile_bytes));
45214522
ERR_clear_error();
45224523
}
45234524
else {
@@ -4537,7 +4538,8 @@ load_cert_chain_lock_held(PySSLContext *self, _PySSLPasswordInfo *pw_info,
45374538
/* the password callback has already set the error information */
45384539
}
45394540
else if (errno != 0) {
4540-
PyErr_SetFromErrno(PyExc_OSError);
4541+
PyErr_SetFromErrnoWithFilename(PyExc_OSError,
4542+
PyBytes_AS_STRING(keyfile_bytes ? keyfile_bytes : certfile_bytes));
45414543
ERR_clear_error();
45424544
}
45434545
else {

0 commit comments

Comments
 (0)
0