-
Notifications
You must be signed in to change notification settings - Fork 419
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
Allow accessing a connection's verfied certificate chain #894
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d0e4a6
Allow accessing a connection's verfied certificate chain
ShaneHarvey 6851254
TLSv1_METHOD -> SSLv23_METHOD
ShaneHarvey 263c08c
Use X509_up_ref instead of X509_dup
ShaneHarvey ba78fb2
Add _openssl_assert where appropriate
ShaneHarvey 0e07516
SSL_get_peer_cert_chain should not be null
ShaneHarvey 87f19e3
Reformat with black
ShaneHarvey 6d11a89
Fix <OpenSSL.crypto.X509 object at 0x7fdbb59e8050> != <OpenSSL.crypto…
ShaneHarvey cc951ec
Add Changelog entry
ShaneHarvey 63b0401
Remove _add_chain
ShaneHarvey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1712,11 +1712,18 @@ def __init__(self, store, certificate): | |
self._store_ctx = _ffi.gc(store_ctx, _lib.X509_STORE_CTX_free) | ||
self._store = store | ||
self._cert = certificate | ||
self._chain = _ffi.NULL | ||
# Make the store context available for use after instantiating this | ||
# class by initializing it now. Per testing, subsequent calls to | ||
# :meth:`_init` have no adverse affect. | ||
self._init() | ||
|
||
def _add_chain(self, chain): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed once the other comment is addressed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
""" | ||
Internal helper to set the untrusted certification chain (peer chain). | ||
""" | ||
self._chain = chain | ||
|
||
def _init(self): | ||
""" | ||
Set up the store context for a subsequent verification operation. | ||
|
@@ -1725,7 +1732,7 @@ def _init(self): | |
:meth:`_cleanup` will leak memory. | ||
""" | ||
ret = _lib.X509_STORE_CTX_init( | ||
self._store_ctx, self._store._store, self._cert._x509, _ffi.NULL | ||
self._store_ctx, self._store._store, self._cert._x509, self._chain | ||
) | ||
if ret <= 0: | ||
_raise_current_error() | ||
|
@@ -1797,6 +1804,45 @@ def verify_certificate(self): | |
if ret <= 0: | ||
raise self._exception_from_context() | ||
|
||
def get_verified_chain(self): | ||
""" | ||
Verify a certificate in a context and return the complete validated | ||
chain. | ||
|
||
:raises X509StoreContextError: If an error occurred when validating a | ||
certificate in the context. Sets ``certificate`` attribute to | ||
indicate which certificate caused the error. | ||
|
||
.. versionadded:: 20.0 | ||
""" | ||
# Always re-initialize the store context in case | ||
# :meth:`verify_certificate` is called multiple times. | ||
# | ||
# :meth:`_init` is called in :meth:`__init__` so _cleanup is called | ||
# before _init to ensure memory is not leaked. | ||
self._cleanup() | ||
reaperhulk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._init() | ||
ret = _lib.X509_verify_cert(self._store_ctx) | ||
if ret <= 0: | ||
self._cleanup() | ||
raise self._exception_from_context() | ||
|
||
# Note: X509_STORE_CTX_get1_chain returns a deep copy of the chain. | ||
cert_stack = _lib.X509_STORE_CTX_get1_chain(self._store_ctx) | ||
_openssl_assert(cert_stack != _ffi.NULL) | ||
|
||
result = [] | ||
for i in range(_lib.sk_X509_num(cert_stack)): | ||
cert = _lib.sk_X509_value(cert_stack, i) | ||
reaperhulk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_openssl_assert(cert != _ffi.NULL) | ||
pycert = X509._from_raw_x509_ptr(cert) | ||
result.append(pycert) | ||
|
||
# Free the stack but not the members which are freed by the X509 class. | ||
_lib.sk_X509_free(cert_stack) | ||
self._cleanup() | ||
return result | ||
|
||
|
||
def load_certificate(type, buffer): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just set
_chain
directly and remove the setter since it doesn't hold any logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.