Skip to content

Commit 82b6c09

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

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
@@ -1475,12 +1475,17 @@ def test_load_verify_cadata(self):
14751475
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
14761476
self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object)
14771477

1478-
with self.assertRaisesRegex(ssl.SSLError, "no start line"):
1478+
with self.assertRaisesRegex(
1479+
ssl.SSLError,
1480+
"no start line: cadata does not contain a certificate"
1481+
):
14791482
ctx.load_verify_locations(cadata="broken")
1480-
with self.assertRaisesRegex(ssl.SSLError, "not enough data"):
1483+
with self.assertRaisesRegex(
1484+
ssl.SSLError,
1485+
"not enough data: cadata does not contain a certificate"
1486+
):
14811487
ctx.load_verify_locations(cadata=b"broken")
14821488

1483-
14841489
def test_load_dh_params(self):
14851490
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
14861491
ctx.load_dh_params(DHFILE)
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
@@ -4097,7 +4097,7 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
40974097
{
40984098
BIO *biobuf = NULL;
40994099
X509_STORE *store;
4100-
int retval = 0, err, loaded = 0;
4100+
int retval = -1, err, loaded = 0;
41014101

41024102
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
41034103

@@ -4151,23 +4151,32 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
41514151
}
41524152

41534153
err = ERR_peek_last_error();
4154-
if ((filetype == SSL_FILETYPE_ASN1) &&
4155-
(loaded > 0) &&
4156-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
4157-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
4154+
if (loaded == 0) {
4155+
const char *msg = NULL;
4156+
if (filetype == SSL_FILETYPE_PEM) {
4157+
msg = "no start line: cadata does not contain a certificate";
4158+
} else {
4159+
msg = "not enough data: cadata does not contain a certificate";
4160+
}
4161+
_setSSLError(msg, 0, __FILE__, __LINE__);
4162+
retval = -1;
4163+
} else if ((filetype == SSL_FILETYPE_ASN1) &&
4164+
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
4165+
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
41584166
/* EOF ASN1 file, not an error */
41594167
ERR_clear_error();
41604168
retval = 0;
41614169
} else if ((filetype == SSL_FILETYPE_PEM) &&
4162-
(loaded > 0) &&
41634170
(ERR_GET_LIB(err) == ERR_LIB_PEM) &&
41644171
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
41654172
/* EOF PEM file, not an error */
41664173
ERR_clear_error();
41674174
retval = 0;
4168-
} else {
4175+
} else if (err != 0) {
41694176
_setSSLError(NULL, 0, __FILE__, __LINE__);
41704177
retval = -1;
4178+
} else {
4179+
retval = 0;
41714180
}
41724181

41734182
BIO_free(biobuf);

0 commit comments

Comments
 (0)