Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X509Store.add_cert no longer raises an error on duplicate cert #787

Merged
merged 2 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ The third digit is only for regressions.
Backward-incompatible changes:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*none*
- ``X509Store.add_cert`` no longer raises an error if you add a duplicate cert.
`#787 <https://github.com/pyca/pyopenssl/pull/787>`_


Deprecations:
Expand Down
11 changes: 10 additions & 1 deletion src/OpenSSL/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,16 @@ def add_cert(self, cert):
if not isinstance(cert, X509):
raise TypeError()

_openssl_assert(_lib.X509_STORE_add_cert(self._store, cert._x509) != 0)
# As of OpenSSL 1.1.0i adding the same cert to the store more than
# once doesn't cause an error. Accordingly, this code now silences
# the error for OpenSSL < 1.1.0i as well.
if _lib.X509_STORE_add_cert(self._store, cert._x509) == 0:
code = _lib.ERR_peek_error()
err_reason = _lib.ERR_GET_REASON(code)
_openssl_assert(
err_reason == _lib.X509_R_CERT_ALREADY_IN_HASH_TABLE
)
_lib.ERR_clear_error()

def add_crl(self, crl):
"""
Expand Down
9 changes: 4 additions & 5 deletions tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2016,16 +2016,15 @@ def test_add_cert_wrong_args(self, cert):
with pytest.raises(TypeError):
store.add_cert(cert)

def test_add_cert_rejects_duplicate(self):
def test_add_cert_accepts_duplicate(self):
"""
`X509Store.add_cert` raises `OpenSSL.crypto.Error` if an attempt is
made to add the same certificate to the store more than once.
`X509Store.add_cert` doesn't raise `OpenSSL.crypto.Error` if an attempt
is made to add the same certificate to the store more than once.
"""
cert = load_certificate(FILETYPE_PEM, cleartextCertificatePEM)
store = X509Store()
store.add_cert(cert)
with pytest.raises(Error):
store.add_cert(cert)
store.add_cert(cert)


class TestPKCS12(object):
Expand Down