'internal method to validate the peer\'s SSL certificate, and to check the
commonName of the certificate to ensure it matches the hostname we
used to make this connection.  Does not support subjectAltName records
in certificates.
raises TTransportException if the certificate fails validation.'

def _validate_cert(self):

    cert = self.handle.getpeercert()
    self.peercert = cert
    if ('subject' not in cert):
        raise TTransportException(type=TTransportException.NOT_OPEN, message=('No    SSL    certificate    found    from    %s:%s' % (self.host, self.port)))
    fields = cert['subject']
    for field in fields:
        if (not isinstance(field, tuple)):
            continue
        cert_pair = field[0]
        if (len(cert_pair) < 2):
            continue
        (cert_key, cert_value) = cert_pair[0:2]
        if (cert_key != 'commonName'):
            continue
        certhost = cert_value
        if (certhost == self.host):
            self.is_valid = True
            return
        else:
            raise TTransportException(type=TTransportException.UNKNOWN, message=('Hostname    we    connected    to    "%s"    doesn\'t    match    certificate    provided    commonName    "%s"' % (self.host, certhost)))
    raise TTransportException(type=TTransportException.UNKNOWN, message=('Could    not    validate    SSL    certificate    from    host    "%s".        Cert=%s' % (self.host, cert)))
