-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify SSL certificate before any request
Use a list of root certificates from Mozilla, which is licensed under GLv2, too. In any case, [1] allows us to use the file even under MPL. [1]: http://www.mozilla.org/MPL/2.0/combining-mpl-and-gpl.html Fixes CVE-2013-2073.
- Loading branch information
Apostolos Bessas
committed
May 14, 2013
1 parent
fe0bff8
commit e24ea95
Showing
8 changed files
with
4,022 additions
and
7 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""The match_hostname() function from Python 3.2, essential when using SSL.""" | ||
|
||
# See https://bitbucket.org/brandon/backports.ssl_match_hostname | ||
|
||
import re | ||
|
||
__version__ = '3.2.2' | ||
|
||
|
||
class CertificateError(ValueError): | ||
pass | ||
|
||
|
||
def _dnsname_to_pat(dn): | ||
pats = [] | ||
for frag in dn.split(r'.'): | ||
if frag == '*': | ||
# When '*' is a fragment by itself, it matches a non-empty dotless | ||
# fragment. | ||
pats.append('[^.]+') | ||
else: | ||
# Otherwise, '*' matches any dotless fragment. | ||
frag = re.escape(frag) | ||
pats.append(frag.replace(r'\*', '[^.]*')) | ||
return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE) | ||
|
||
|
||
def match_hostname(cert, hostname): | ||
"""Verify that *cert* (in decoded format as returned by | ||
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 rules | ||
are mostly followed, but IP addresses are not accepted for *hostname*. | ||
CertificateError is raised on failure. On success, the function | ||
returns nothing. | ||
""" | ||
if not cert: | ||
raise ValueError("empty or no certificate") | ||
dnsnames = [] | ||
san = cert.get('subjectAltName', ()) | ||
for key, value in san: | ||
if key == 'DNS': | ||
if _dnsname_to_pat(value).match(hostname): | ||
return | ||
dnsnames.append(value) | ||
if not dnsnames: | ||
# The subject is only checked when there is no dNSName entry | ||
# in subjectAltName | ||
for sub in cert.get('subject', ()): | ||
for key, value in sub: | ||
# XXX according to RFC 2818, the most specific Common Name | ||
# must be used. | ||
if key == 'commonName': | ||
if _dnsname_to_pat(value).match(hostname): | ||
return | ||
dnsnames.append(value) | ||
if len(dnsnames) > 1: | ||
raise CertificateError("hostname %r " | ||
"doesn't match either of %s" | ||
% (hostname, ', '.join(map(repr, dnsnames)))) | ||
elif len(dnsnames) == 1: | ||
raise CertificateError("hostname %r " | ||
"doesn't match %r" | ||
% (hostname, dnsnames[0])) | ||
else: | ||
raise CertificateError("no appropriate commonName or " | ||
"subjectAltName fields were found") |
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
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