<s> """<STR_LIT>""" <EOL> import httplib <EOL> import re <EOL> import socket <EOL> import ssl <EOL> import boto <EOL> class InvalidCertificateException ( httplib . HTTPException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , cert , reason ) : <EOL> """<STR_LIT>""" <EOL> httplib . HTTPException . __init__ ( self ) <EOL> self . host = host <EOL> self . cert = cert <EOL> self . reason = reason <EOL> def __str__ ( self ) : <EOL> return ( '<STR_LIT>' % <EOL> ( self . host , self . reason , self . cert ) ) <EOL> def GetValidHostsForCert ( cert ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in cert : <EOL> return [ x [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] if x [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> else : <EOL> return [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] <EOL> if x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> def ValidateCertificateHostname ( cert , hostname ) : <EOL> """<STR_LIT>""" <EOL> hosts = GetValidHostsForCert ( cert ) <EOL> boto . log . debug ( <EOL> "<STR_LIT>" , <EOL> hostname , hosts ) <EOL> for host in hosts : <EOL> host_re = host . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if re . search ( '<STR_LIT>' % ( host_re , ) , hostname , re . I ) : <EOL> return True <EOL> return False <EOL> class CertValidatingHTTPSConnection ( httplib . HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> default_port = httplib . HTTPS_PORT <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> ca_certs = None , strict = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> httplib . HTTPConnection . __init__ ( self , host , port , strict , ** kwargs ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_certs = ca_certs <EOL> def connect ( self ) : <EOL> "<STR_LIT>" <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( self . host , self . port ) ) <EOL> boto . log . debug ( "<STR_LIT>" , <EOL> self . ca_certs ) <EOL> self . sock = ssl . wrap_socket ( sock , keyfile = self . key_file , <EOL> certfile = self . cert_file , <EOL> cert_reqs = ssl . CERT_REQUIRED , <EOL> ca_certs = self . ca_certs ) <EOL> cert = self . sock . getpeercert ( ) <EOL> hostname = self . host . split ( '<STR_LIT::>' , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if not ValidateCertificateHostname ( cert , hostname ) : <EOL> raise InvalidCertificateException ( hostname , <EOL> cert , <EOL> '<STR_LIT>' '<STR_LIT>' % hostname ) </s>
