Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
142 additions
and 7 deletions.
- +24 −7 bridgedb/crypto.py
- +118 −0 bridgedb/test/test_crypto.py
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
| @@ -54,6 +54,12 @@ | ||
| from twisted.internet import ssl | ||
| from twisted.python.procutils import which | ||
|
|
||
|
|
||
| from service_identity.cryptography import verify_certificate_hostname | ||
| from cryptography.x509 import load_pem_x509_certificate | ||
This comment has been minimized.
This comment has been minimized. |
||
| from cryptography.hazmat.backends import default_backend | ||
| from service_identity import VerificationError, CertificateError, SubjectAltNameWarning | ||
|
|
||
| #: The hash digest to use for HMACs. | ||
| DIGESTMOD = hashlib.sha1 | ||
|
|
||
| @@ -341,15 +347,26 @@ def verifyHostname(self, connection, x509, errnum, depth, okay): | ||
| commonName = x509.get_subject().commonName | ||
| logging.debug("Received cert at level %d: '%s'" % (depth, commonName)) | ||
|
|
||
| x509 = x509.to_cryptography() | ||
| # We only want to verify that the hostname matches for the level 0 | ||
| # certificate: | ||
| if okay and (depth == 0): | ||
| cn = commonName.replace('*', '.*') | ||
| hostnamesMatch = re.search(cn, self.hostname) | ||
| if not hostnamesMatch: | ||
| try: | ||
| verify_certificate_hostname(x509,self.hostname) | ||
This comment has been minimized. |
||
| logging.debug("Valid certificate subject CN for '%s': '%s'" | ||
| % (self.hostname, commonName)) | ||
| return True | ||
| except VerificationError: | ||
| logging.warn("Invalid certificate subject CN for '%s': '%s'" | ||
| % (self.hostname, commonName)) | ||
| % (self.hostname, commonName)) | ||
| return False | ||
| logging.debug("Valid certificate subject CN for '%s': '%s'" | ||
| % (self.hostname, commonName)) | ||
| return True | ||
| except CertificateError: | ||
| logging.warn("Certificate contains invalid or unexpected data") | ||
| return False | ||
| except SubjectAltNameWarning: | ||
| logging.warn("Certificate contains no SAN, fallback to common name") | ||
| cn = commonName.replace('*', '.*') | ||
| hostnamesMatch = re.search(cn, self.hostname) | ||
| if not hostnamesMatch: | ||
This comment has been minimized.
NullHypothesis
|
||
| return False | ||
| return True | ||
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
Are we actually using this and
default_backend? I don't see it anywhere in the code.