Skip to content

Commit 129667d

Browse files
miss-islingtondavidbenhugovk
authored
[3.10] gh-100372: Use BIO_eof to detect EOF for SSL_FILETYPE_ASN1 (GH-100373) (#153312)
gh-100372: Use BIO_eof to detect EOF for SSL_FILETYPE_ASN1 (GH-100373) In PEM, we need to parse until error and then suppress `PEM_R_NO_START_LINE`, because PEM allows arbitrary leading and trailing data. DER, however, does not. Parsing until error and suppressing `ASN1_R_HEADER_TOO_LONG` doesn't quite work because that error also covers some cases that should be rejected. Instead, check `BIO_eof` early and stop the loop that way. (cherry picked from commit acfe02f) Automerge-Triggered-By: GH:Yhg1s Co-authored-by: David Benjamin <davidben@google.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 1071290 commit 129667d

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,8 @@ def test_load_verify_cadata(self):
15331533
"not enough data: cadata does not contain a certificate"
15341534
):
15351535
ctx.load_verify_locations(cadata=b"broken")
1536+
with self.assertRaises(ssl.SSLError):
1537+
ctx.load_verify_locations(cadata=cacert_der + b"A")
15361538

15371539
@unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows")
15381540
def test_load_dh_params(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`ssl.SSLContext.load_verify_locations` no longer incorrectly accepts
2+
some cases of trailing data when parsing DER.

Modules/_ssl.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3961,7 +3961,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
39613961
{
39623962
BIO *biobuf = NULL;
39633963
X509_STORE *store;
3964-
int retval = -1, err, loaded = 0;
3964+
int retval = -1, err, loaded = 0, was_bio_eof = 0;
39653965

39663966
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
39673967

@@ -3989,6 +3989,10 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
39893989
int r;
39903990

39913991
if (filetype == SSL_FILETYPE_ASN1) {
3992+
if (BIO_eof(biobuf)) {
3993+
was_bio_eof = 1;
3994+
break;
3995+
}
39923996
cert = d2i_X509_bio(biobuf, NULL);
39933997
} else {
39943998
cert = PEM_read_bio_X509(biobuf, NULL,
@@ -4024,9 +4028,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
40244028
}
40254029
_setSSLError(get_state_ctx(self), msg, 0, __FILE__, __LINE__);
40264030
retval = -1;
4027-
} else if ((filetype == SSL_FILETYPE_ASN1) &&
4028-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
4029-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
4031+
} else if ((filetype == SSL_FILETYPE_ASN1) && was_bio_eof) {
40304032
/* EOF ASN1 file, not an error */
40314033
ERR_clear_error();
40324034
retval = 0;

0 commit comments

Comments
 (0)