Skip to content

Commit b9ad88b

Browse files
authored
bpo-43920: Make load_verify_locations(cadata) error message consistent (GH-25554)
Signed-off-by: Christian Heimes <[email protected]>
1 parent e9194ea commit b9ad88b

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
@@ -1433,12 +1433,17 @@ def test_load_verify_cadata(self):
14331433
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
14341434
self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object)
14351435

1436-
with self.assertRaisesRegex(ssl.SSLError, "no start line"):
1436+
with self.assertRaisesRegex(
1437+
ssl.SSLError,
1438+
"no start line: cadata does not contain a certificate"
1439+
):
14371440
ctx.load_verify_locations(cadata="broken")
1438-
with self.assertRaisesRegex(ssl.SSLError, "not enough data"):
1441+
with self.assertRaisesRegex(
1442+
ssl.SSLError,
1443+
"not enough data: cadata does not contain a certificate"
1444+
):
14391445
ctx.load_verify_locations(cadata=b"broken")
14401446

1441-
14421447
@unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows")
14431448
def test_load_dh_params(self):
14441449
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
@@ -3791,7 +3791,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
37913791
{
37923792
BIO *biobuf = NULL;
37933793
X509_STORE *store;
3794-
int retval = 0, err, loaded = 0;
3794+
int retval = -1, err, loaded = 0;
37953795

37963796
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
37973797

@@ -3845,23 +3845,32 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
38453845
}
38463846

38473847
err = ERR_peek_last_error();
3848-
if ((filetype == SSL_FILETYPE_ASN1) &&
3849-
(loaded > 0) &&
3850-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
3851-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
3848+
if (loaded == 0) {
3849+
const char *msg = NULL;
3850+
if (filetype == SSL_FILETYPE_PEM) {
3851+
msg = "no start line: cadata does not contain a certificate";
3852+
} else {
3853+
msg = "not enough data: cadata does not contain a certificate";
3854+
}
3855+
_setSSLError(get_state_ctx(self), msg, 0, __FILE__, __LINE__);
3856+
retval = -1;
3857+
} else if ((filetype == SSL_FILETYPE_ASN1) &&
3858+
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
3859+
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
38523860
/* EOF ASN1 file, not an error */
38533861
ERR_clear_error();
38543862
retval = 0;
38553863
} else if ((filetype == SSL_FILETYPE_PEM) &&
3856-
(loaded > 0) &&
38573864
(ERR_GET_LIB(err) == ERR_LIB_PEM) &&
38583865
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
38593866
/* EOF PEM file, not an error */
38603867
ERR_clear_error();
38613868
retval = 0;
3862-
} else {
3869+
} else if (err != 0) {
38633870
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
38643871
retval = -1;
3872+
} else {
3873+
retval = 0;
38653874
}
38663875

38673876
BIO_free(biobuf);

0 commit comments

Comments
 (0)