Skip to content

Commit bcf034f

Browse files
committed
bpo-43920: Make load_verify_locations(cadata) error message consistent (pythonGH-25554)
1 parent 69105fa commit bcf034f

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,12 +1045,17 @@ def test_load_verify_cadata(self):
10451045
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
10461046
self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object)
10471047

1048-
with self.assertRaisesRegexp(ssl.SSLError, "no start line"):
1048+
with self.assertRaisesRegexp(
1049+
ssl.SSLError,
1050+
"no start line: cadata does not contain a certificate"
1051+
):
10491052
ctx.load_verify_locations(cadata=u"broken")
1050-
with self.assertRaisesRegexp(ssl.SSLError, "not enough data"):
1053+
with self.assertRaisesRegexp(
1054+
ssl.SSLError,
1055+
"not enough data: cadata does not contain a certificate"
1056+
):
10511057
ctx.load_verify_locations(cadata=b"broken")
10521058

1053-
10541059
def test_load_dh_params(self):
10551060
filename = u'dhpäräm.pem'
10561061
fs_encoding = sys.getfilesystemencoding()

Modules/_ssl.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,7 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
29192919
{
29202920
BIO *biobuf = NULL;
29212921
X509_STORE *store;
2922-
int retval = 0, err, loaded = 0;
2922+
int retval = -1, err, loaded = 0;
29232923

29242924
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
29252925

@@ -2973,23 +2973,32 @@ _add_ca_certs(PySSLContext *self, void *data, Py_ssize_t len,
29732973
}
29742974

29752975
err = ERR_peek_last_error();
2976-
if ((filetype == SSL_FILETYPE_ASN1) &&
2977-
(loaded > 0) &&
2978-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
2979-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
2976+
if (loaded == 0) {
2977+
char *msg = NULL;
2978+
if (filetype == SSL_FILETYPE_PEM) {
2979+
msg = "no start line: cadata does not contain a certificate";
2980+
} else {
2981+
msg = "not enough data: cadata does not contain a certificate";
2982+
}
2983+
_setSSLError(msg, 0, __FILE__, __LINE__);
2984+
retval = -1;
2985+
} else if ((filetype == SSL_FILETYPE_ASN1) &&
2986+
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
2987+
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
29802988
/* EOF ASN1 file, not an error */
29812989
ERR_clear_error();
29822990
retval = 0;
29832991
} else if ((filetype == SSL_FILETYPE_PEM) &&
2984-
(loaded > 0) &&
29852992
(ERR_GET_LIB(err) == ERR_LIB_PEM) &&
29862993
(ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
29872994
/* EOF PEM file, not an error */
29882995
ERR_clear_error();
29892996
retval = 0;
2990-
} else {
2997+
} else if (err != 0) {
29912998
_setSSLError(NULL, 0, __FILE__, __LINE__);
29922999
retval = -1;
3000+
} else {
3001+
retval = 0;
29933002
}
29943003

29953004
BIO_free(biobuf);

0 commit comments

Comments
 (0)