Skip to content

Commit 3b95407

Browse files
tiranmcepl
authored andcommitted
bpo-43920: Make load_verify_locations(cadata) error message consistent (pythonGH-25554) (pythonGH-25556)
Signed-off-by: Christian Heimes <[email protected]>. (cherry picked from commit b9ad88b) Co-authored-by: Christian Heimes <[email protected]>
1 parent 5b46889 commit 3b95407

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
@@ -1191,12 +1191,17 @@ def test_load_verify_cadata(self):
11911191
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
11921192
self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object)
11931193

1194-
with self.assertRaisesRegex(ssl.SSLError, "no start line"):
1194+
with self.assertRaisesRegex(
1195+
ssl.SSLError,
1196+
"no start line: cadata does not contain a certificate"
1197+
):
11951198
ctx.load_verify_locations(cadata="broken")
1196-
with self.assertRaisesRegex(ssl.SSLError, "not enough data"):
1199+
with self.assertRaisesRegex(
1200+
ssl.SSLError,
1201+
"not enough data: cadata does not contain a certificate"
1202+
):
11971203
ctx.load_verify_locations(cadata=b"broken")
11981204

1199-
12001205
def test_load_dh_params(self):
12011206
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
12021207
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
@@ -3648,7 +3648,7 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
36483648
{
36493649
BIO *biobuf = NULL;
36503650
X509_STORE *store;
3651-
int retval = 0, err, loaded = 0;
3651+
int retval = -1, err, loaded = 0;
36523652

36533653
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
36543654

@@ -3702,23 +3702,32 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
37023702
}
37033703

37043704
err = ERR_peek_last_error();
3705-
if ((filetype == SSL_FILETYPE_ASN1) &&
3706-
(loaded > 0) &&
3707-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
3708-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
3705+
if (loaded == 0) {
3706+
const char *msg = NULL;
3707+
if (filetype == SSL_FILETYPE_PEM) {
3708+
msg = "no start line: cadata does not contain a certificate";
3709+
} else {
3710+
msg = "not enough data: cadata does not contain a certificate";
3711+
}
3712+
_setSSLError(msg, 0, __FILE__, __LINE__);
3713+
retval = -1;
3714+
} else if ((filetype == SSL_FILETYPE_ASN1) &&
3715+
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
3716+
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
37093717
/* EOF ASN1 file, not an error */
37103718
ERR_clear_error();
37113719
retval = 0;
37123720
} else if ((filetype == SSL_FILETYPE_PEM) &&
3713-
(loaded > 0) &&
37143721
(ERR_GET_LIB(err) == ERR_LIB_PEM) &&
37153722
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
37163723
/* EOF PEM file, not an error */
37173724
ERR_clear_error();
37183725
retval = 0;
3719-
} else {
3726+
} else if (err != 0) {
37203727
_setSSLError(NULL, 0, __FILE__, __LINE__);
37213728
retval = -1;
3729+
} else {
3730+
retval = 0;
37223731
}
37233732

37243733
BIO_free(biobuf);

0 commit comments

Comments
 (0)