8000 gh-144249: Report filename in SSLContext.load_cert_chain errors (#144… · python/cpython@1a637b2 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 1a637b2

Browse files
romualdvstinnerpicnixz
authored
gh-144249: Report filename in SSLContext.load_cert_chain errors (#144250)
When user tries to load a certificate chain, attach the related filename to the exception being raised. Improving user experience. Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 6ea3f8c commit 1a637b2

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-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)
7440 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)"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add filename context to :exc:`OSError` exceptions raised by
2+
:func:`ssl.SSLContext.load_cert_chain`, allowing users to have more context.

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