<s> """<STR_LIT>""" <EOL> import re <EOL> __version__ = '<STR_LIT>' <EOL> class CertificateError ( ValueError ) : <EOL> pass <EOL> def _dnsname_to_pat ( dn ) : <EOL> pats = [ ] <EOL> for frag in dn . split ( r'<STR_LIT:.>' ) : <EOL> if frag == '<STR_LIT:*>' : <EOL> pats . append ( '<STR_LIT>' ) <EOL> else : <EOL> frag = re . escape ( frag ) <EOL> pats . append ( frag . replace ( r'<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return re . compile ( r'<STR_LIT>' + r'<STR_LIT>' . join ( pats ) + r'<STR_LIT>' , re . IGNORECASE ) <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_to_pat ( value ) . match ( hostname ) : <EOL> return <EOL> dnsnames . append ( value ) <EOL> if not san : <EOL> for sub in cert . get ( '<STR_LIT>' , ( ) ) : <EOL> for key , value in sub : <EOL> if key == '<STR_LIT>' : <EOL> if _dnsname_to_pat ( value ) . match ( 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>
