Skip to content

Commit 387cc11

Browse files
davidbenFidget-Spinner
authored andcommitted
pythongh-100372: Use BIO_eof to detect EOF for SSL_FILETYPE_ASN1 (pythonGH-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. Automerge-Triggered-By: GH:Yhg1s
1 parent bf3bfdb commit 387cc11

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Lib/test/test_ssl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,8 @@ def test_load_verify_cadata(self):
12891289
"not enough data: cadata does not contain a certificate"
12901290
):
12911291
ctx.load_verify_locations(cadata=b"broken")
1292+
with self.assertRaises(ssl.SSLError):
1293+
ctx.load_verify_locations(cadata=cacert_der + b"A")
12921294

12931295
@unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows")
12941296
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
@@ -3930,7 +3930,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
39303930
{
39313931
BIO *biobuf = NULL;
39323932
X509_STORE *store;
3933-
int retval = -1, err, loaded = 0;
3933+
int retval = -1, err, loaded = 0, was_bio_eof = 0;
39343934

39353935
assert(filetype == SSL_FILETYPE_ASN1 || filetype == SSL_FILETYPE_PEM);
39363936

@@ -3958,6 +3958,10 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
39583958
int r;
39593959

39603960
if (filetype == SSL_FILETYPE_ASN1) {
3961+
if (BIO_eof(biobuf)) {
3962+
was_bio_eof = 1;
3963+
break;
3964+
}
39613965
cert = d2i_X509_bio(biobuf, NULL);
39623966
} else {
39633967
cert = PEM_read_bio_X509(biobuf, NULL,
@@ -3993,9 +3997,7 @@ _add_ca_certs(PySSLContext *self, const void *data, Py_ssize_t len,
39933997
}
39943998
_setSSLError(get_state_ctx(self), msg, 0, __FILE__, __LINE__);
39953999
retval = -1;
3996-
} else if ((filetype == SSL_FILETYPE_ASN1) &&
3997-
(ERR_GET_LIB(err) == ERR_LIB_ASN1) &&
3998-
(ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG)) {
4000+
} else if ((filetype == SSL_FILETYPE_ASN1) && was_bio_eof) {
39994001
/* EOF ASN1 file, not an error */
40004002
ERR_clear_error();
40014003
retval = 0;

0 commit comments

Comments
 (0)