Skip to content

Commit e259a77

Browse files
authored
[3.9] bpo-43920: Make load_verify_locations(cadata) error message consistent (GH-25554) (GH-25555)
Signed-off-by: Christian Heimes <[email protected]>. (cherry picked from commit b9ad88b) Co-authored-by: Christian Heimes <[email protected]>
1 parent d4fff1f commit e259a77

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

Lib/test/test_ssl.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,12 +1478,17 @@ def test_load_verify_cadata(self):
14781478
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
14791479
self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object)
14801480

1481-
with self.assertRaisesRegex(ssl.SSLError, "no start line"):
1481+
with self.assertRaisesRegex(
1482+
ssl.SSLError,
1483+
"no start line: cadata does not contain a certificate"
1484+
):
14821485
ctx.load_verify_locations(cadata="broken")
1483-
with self.assertRaisesRegex(ssl.SSLError, "not enough data"):
1486+
with self.assertRaisesRegex(
1487+
ssl.SSLError,
1488+
"not enough data: cadata does not contain a certificate"
1489+
):
14841490
ctx.load_verify_locations(cadata=b"broken")
14851491

1486-
14871492
@unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows")
14881493
def test_load_dh_params(self):
14891494
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OpenSSL 3.0.0: :meth:`~ssl.SSLContext.load_verify_locations` now returns a
2+
consistent error message when cadata contains no valid certificate.

Modules/_ssl.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4095,7 +4095,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
40954095
{
40964096
BIO *biobuf = NULL;
40974097
X509_STORE *store;
4098-
int retval = 0, err, loaded = 0;
4098+
int retval = -1, err, loaded = 0;
40994099

41004100
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
41014101

@@ -4149,23 +4149,32 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
41494149
}
41504150

41514151
err = ERR_peek_last_error();
4152-
if ((filetype == SSL_FILETYPE_ASN1) &&
4153-
(loaded > 0) &&
4154-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
4155-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
4152+
if (loaded == 0) {
4153+
const char *msg = NULL;
4154+
if (filetype == SSL_FILETYPE_PEM) {
4155+
msg = "no start line: cadata does not contain a certificate";
4156+
} else {
4157+
msg = "not enough data: cadata does not contain a certificate";
4158+
}
4159+
_setSSLError(msg, 0, __FILE__, __LINE__);
4160+
retval = -1;
4161+
} else if ((filetype == SSL_FILETYPE_ASN1) &&
4162+
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
4163+
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
41564164
/* EOF ASN1 file, not an error */
41574165
ERR_clear_error();
41584166
retval = 0;
41594167
} else if ((filetype == SSL_FILETYPE_PEM) &&
4160-
(loaded > 0) &&
41614168
(ERR_GET_LIB(err) == ERR_LIB_PEM) &&
41624169
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
41634170
/* EOF PEM file, not an error */
41644171
ERR_clear_error();
41654172
retval = 0;
4166-
} else {
4173+
} else if (err != 0) {
41674174
_setSSLError(NULL, 0, __FILE__, __LINE__);
41684175
retval = -1;
4176+
} else {
4177+
retval = 0;
41694178
}
41704179

41714180
BIO_free(biobuf);

0 commit comments

Comments
 (0)