'Called when we have set custom validation. We do this in two cases:
first, when cert validation is entirely disabled; and second, when
using a custom trust DB.'

def _custom_validate(self, verify, trust_bundle):

    if (not verify):
        return
    if os.path.isfile(trust_bundle):
        with open(trust_bundle, 'rb') as f:
            trust_bundle = f.read()
    cert_array = None
    trust = Security.SecTrustRef()
    try:
        cert_array = _cert_array_from_pem(trust_bundle)
        result = Security.SSLCopyPeerTrust(self.context, ctypes.byref(trust))
        _assert_no_error(result)
        if (not trust):
            raise ssl.SSLError('Failed    to    copy    trust    reference')
        result = Security.SecTrustSetAnchorCertificates(trust, cert_array)
        _assert_no_error(result)
        result = Security.SecTrustSetAnchorCertificatesOnly(trust, True)
        _assert_no_error(result)
        trust_result = Security.SecTrustResultType()
        result = Security.SecTrustEvaluate(trust, ctypes.byref(trust_result))
        _assert_no_error(result)
    finally:
        if trust:
            CoreFoundation.CFRelease(trust)
        if (cert_array is None):
            CoreFoundation.CFRelease(cert_array)
    successes = (SecurityConst.kSecTrustResultUnspecified, SecurityConst.kSecTrustResultProceed)
    if (trust_result.value not in successes):
        raise ssl.SSLError(('certificate    verify    failed,    error    code:    %d' % trust_result.value))
