<s> '''<STR_LIT>''' <EOL> import re <EOL> __version__ = '<STR_LIT>' <EOL> class CertificateError ( ValueError ) : <EOL> pass <EOL> def _dnsname_match ( dn , hostname , max_wildcards = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> pats = [ ] <EOL> if not dn : <EOL> return False <EOL> parts = dn . split ( r'<STR_LIT:.>' ) <EOL> leftmost = parts [ <NUM_LIT:0> ] <EOL> remainder = parts [ <NUM_LIT:1> : ] <EOL> wildcards = leftmost . count ( '<STR_LIT:*>' ) <EOL> if wildcards > max_wildcards : <EOL> raise CertificateError ( <EOL> "<STR_LIT>" + repr ( dn ) ) <EOL> if not wildcards : <EOL> return dn . lower ( ) == hostname . lower ( ) <EOL> if leftmost == '<STR_LIT:*>' : <EOL> pats . append ( '<STR_LIT>' ) <EOL> elif leftmost . startswith ( '<STR_LIT>' ) or hostname . startswith ( '<STR_LIT>' ) : <EOL> pats . append ( re . escape ( leftmost ) ) <EOL> else : <EOL> pats . append ( re . escape ( leftmost ) . replace ( r'<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for frag in remainder : <EOL> pats . append ( re . escape ( frag ) ) <EOL> pat = re . compile ( r'<STR_LIT>' + r'<STR_LIT>' . join ( pats ) + r'<STR_LIT>' , re . IGNORECASE ) <EOL> return pat . match ( hostname ) <EOL> def match_hostname ( cert , hostname ) : <EOL> """<STR_LIT>""" <EOL> if not cert : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> dnsnames = [ ] <EOL> san = cert . get ( '<STR_LIT>' , ( ) ) <EOL> for key , value in san : <EOL> if key == '<STR_LIT>' : <EOL> if _dnsname_match ( value , hostname ) : <EOL> return <EOL> dnsnames . append ( value ) <EOL> if not dnsnames : <EOL> for sub in cert . get ( '<STR_LIT>' , ( ) ) : <EOL> for key , value in sub : <EOL> if key == '<STR_LIT>' : <EOL> if _dnsname_match ( value , hostname ) : <EOL> return <EOL> dnsnames . append ( value ) <EOL> if len ( dnsnames ) > <NUM_LIT:1> : <EOL> raise CertificateError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( hostname , '<STR_LIT:U+002CU+0020>' . join ( map ( repr , dnsnames ) ) ) ) <EOL> elif len ( dnsnames ) == <NUM_LIT:1> : <EOL> raise CertificateError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( hostname , dnsnames [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> raise CertificateError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
