-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-18233: Add SSLSocket.get_verified_chain() and SSLSocket.get_unverified_chain() #17938
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
Open
chrisburr
wants to merge
7
commits into
python:main
Choose a base branch
from
chrisburr:fix-issue-18233
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33bfd92
📜🤖 Added by blurb_it.
blurb-it[bot] 7008f61
bpo-18233: Add SSLSocket.getpeercertchain()
chrisburr 4243cc6
bpo-18233: Always return peer certificate from SSLSocket.getpeercertc…
chrisburr a7c641d
bpo-18233: Only support getpeercertchain(validate=True) with OpenSSL …
chrisburr 3324381
bpo-18233: Review of SSLSocket.get_peer_cert_chain and SSLSocket.get_…
chrisburr 1f8dfd6
bpo-18233: Rename SSLSocket.get_peer_cert_chain to SSLSocket.get_unve…
chrisburr dd39c49
bpo-18233: Update added in version to 3.10
chrisburr 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
bpo-18233: Add SSLSocket.getpeercertchain()
Based on the patch provided by Christian Heimes (christian.heimes) and updated by Mariusz Masztalerczuk (mmasztalerczuk).
- Loading branch information
commit 7008f61a1b93b321905d1ecefa10f5fa12d46b28
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2096,6 +2096,133 @@ _ssl__SSLSocket_cipher_impl(PySSLSocket *self) | |
| return cipher_to_tuple(current); | ||
| } | ||
|
|
||
| /*[clinic input] | ||
| _ssl._SSLSocket.getpeercertchain | ||
| der as binary_mode: bool = False | ||
| validate: bool = True | ||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| _ssl__SSLSocket_getpeercertchain_impl(PySSLSocket *self, int binary_mode, | ||
| int validate) | ||
| /*[clinic end generated code: output=8094e6d78d27eb9a input=f4dcd181d0d163eb]*/ | ||
| { | ||
| int len, i; | ||
| PyObject *retval = NULL, *ci=NULL; | ||
| STACK_OF(X509) *peer_chain; /* reference */ | ||
|
|
||
| assert((self->ctx != NULL) && (self->ctx->ctx != NULL)); | ||
| if (self->ssl == NULL) { | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| /* The peer just transmits the intermediate cert chain EXCLUDING the root | ||
| * CA certificate as this side is suppose to have a copy of the root | ||
| * certificate for verification. */ | ||
| if (validate) { | ||
| #ifdef OPENSSL_VERSION_1_1 | ||
| peer_chain = SSL_get0_verified_chain(self->ssl); | ||
| long ret = SSL_get_verify_result(self->ssl); | ||
| if (ret != X509_V_OK) { | ||
| #ifdef SSL_R_CERTIFICATE_VERIFY_FAILED | ||
|
Member
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.
|
||
| long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED); | ||
| #else | ||
| long e = ERR_PACK(ERR_LIB_SSL, 0, 134); | ||
| #endif | ||
| fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e); | ||
| return NULL; | ||
| } | ||
| #else | ||
| X509 *peer_cert = SSL_get_peer_certificate(self->ssl); | ||
| if (peer_cert == NULL) | ||
| Py_RETURN_NONE; | ||
|
|
||
| STACK_OF(X509) *chain = SSL_get_peer_cert_chain(self->ssl); | ||
| if (chain == NULL) { | ||
| X509_free(peer_cert); | ||
| Py_RETURN_NONE; | ||
| } | ||
| X509_STORE_CTX *store_ctx; | ||
|
|
||
| /* Initialize a store context with store (for root CA certs), the | ||
| * peer's cert and the peer's chain with intermediate CA certs. */ | ||
| if ((store_ctx = X509_STORE_CTX_new()) == NULL) { | ||
| X509_free(peer_cert); | ||
| _setSSLError(NULL, 0, __FILE__, __LINE__); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (!X509_STORE_CTX_init(store_ctx, | ||
| SSL_CTX_get_cert_store(self->ctx->ctx), | ||
| peer_cert, chain)) { | ||
| #ifdef SSL_R_CERTIFICATE_VERIFY_FAILED | ||
| long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED); | ||
| #else | ||
| long e = ERR_PACK(ERR_LIB_SSL, 0, 134); | ||
| #endif | ||
| fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e); | ||
| X509_free(peer_cert); | ||
| X509_STORE_CTX_free(store_ctx); | ||
| goto end; | ||
| } | ||
| X509_free(peer_cert); | ||
|
|
||
| /* Validate peer cert using its intermediate CA certs and the | ||
| * context's root CA certs. */ | ||
| if (X509_verify_cert(store_ctx) <= 0) { | ||
| // _setX509StoreContextError(self, store_ctx, __FILE__, __LINE__); | ||
| #ifdef SSL_R_CERTIFICATE_VERIFY_FAILED | ||
| long e = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_CERTIFICATE_VERIFY_FAILED); | ||
| #else | ||
| long e = ERR_PACK(ERR_LIB_SSL, 0, 134); | ||
| #endif | ||
| fill_and_set_sslerror(self, PySSLCertVerificationErrorObject, PY_SSL_ERROR_SSL, NULL, __LINE__, e); | ||
| X509_STORE_CTX_free(store_ctx); | ||
| goto end; | ||
| } | ||
|
|
||
| /* Get chain from store context */ | ||
| peer_chain = X509_STORE_CTX_get1_chain(store_ctx); | ||
| X509_STORE_CTX_free(store_ctx); | ||
| #endif | ||
| } else { | ||
| peer_chain = SSL_get_peer_cert_chain(self->ssl); | ||
| } | ||
|
|
||
| if (peer_chain == NULL) { | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| len = sk_X509_num(peer_chain); | ||
|
|
||
| if ((retval = PyTuple_New(len)) == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| for (i = 0; i < len; i++){ | ||
| X509 *cert = sk_X509_value(peer_chain, i); | ||
| if (binary_mode) { | ||
| ci = _certificate_to_der(cert); | ||
| } else { | ||
| ci = _decode_certificate(cert); | ||
| } | ||
|
|
||
| if (ci == NULL) { | ||
| Py_CLEAR(retval); | ||
| goto end; | ||
| } | ||
| PyTuple_SET_ITEM(retval, i, ci); | ||
| } | ||
|
|
||
| end: | ||
| #ifndef OPENSSL_VERSION_1_1 | ||
| if (validate && (peer_chain != NULL)) { | ||
| sk_X509_pop_free(peer_chain, X509_free); | ||
| } | ||
| #endif | ||
| return retval; | ||
| } | ||
|
|
||
| /*[clinic input] | ||
| _ssl._SSLSocket.version | ||
| [clinic start generated code]*/ | ||
|
|
@@ -3000,6 +3127,7 @@ static PyMethodDef PySSLMethods[] = { | |
| _SSL__SSLSOCKET_GET_CHANNEL_BINDING_METHODDEF | ||
| _SSL__SSLSOCKET_CIPHER_METHODDEF | ||
| _SSL__SSLSOCKET_SHARED_CIPHERS_METHODDEF | ||
| _SSL__SSLSOCKET_GETPEERCERTCHAIN_METHODDEF | ||
| _SSL__SSLSOCKET_VERSION_METHODDEF | ||
| _SSL__SSLSOCKET_SELECTED_NPN_PROTOCOL_METHODDEF | ||
| _SSL__SSLSOCKET_SELECTED_ALPN_PROTOCOL_METHODDEF | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
That's not necessarily correct.
SSL_get_peer_cert_chain()returns whatever the client sends. It may just be the EE cert (end entity), EE+intermediates, EE+intermediate+root, EE with intermediates for primary and alternative chains, or whatever the admin for the site has configured. I guess the peer chain can even include unrelated junk.It's also worth mentioning that the chain is not available with TLS session resumption.