protected static bool verified(IPinningInfo pin, RequestDigestSet requestDigestSet) { bool flag = false; try { if (pin.getExpiration().CompareTo(DateTime.Now) <= 0) { return(false); } switch (pin.getMode()) { case PinningMode.STRICT: flag = pin.getCertificate().compareDigests(requestDigestSet.CertificateDigest); break; case PinningMode.PERMISSIVE: flag = pin.getCertificate().compareDigests(requestDigestSet.CertificateDigest); if (!flag) { logFailure(PinningMode.PERMISSIVE, PinningTarget.CERTIFICATE); flag = pin.getSubject().compareDigests(requestDigestSet.SubjectDigest); } break; case PinningMode.ADVISORY: flag = pin.getCertificate().compareDigests(requestDigestSet.CertificateDigest); if (!flag) { logFailure(PinningMode.ADVISORY, PinningTarget.CERTIFICATE); flag = pin.getSubject().compareDigests(requestDigestSet.SubjectDigest); if (!flag) { logFailure(PinningMode.ADVISORY, PinningTarget.SUBJECT); } } break; } } catch (Exception ex) { Console.WriteLine(ex.Message); UnityEngine.Debug.LogException(ex); } return(flag); }
public static bool ThreadSafeBaseValidateCertificate(RequestDigestSet requestDigestSet, X509Certificate certificate, SslPolicyErrors sslPolicyError) { if (certificate != null) { DigestSet digestSet = new DigestSet(); byte[] rawCertData = certificate.GetRawCertData(); digestSet.setSha1(DigestHelper.sha1(rawCertData)); digestSet.setSha256(DigestHelper.sha256(rawCertData)); requestDigestSet.CertificateDigest = digestSet; digestSet = new DigestSet(); rawCertData = certificate.GetPublicKey(); digestSet.setSha1(DigestHelper.sha1(rawCertData)); digestSet.setSha256(DigestHelper.sha256(rawCertData)); requestDigestSet.SubjectDigest = digestSet; return(true); } return(false); }
public static bool ThreadSafeValidateCertificate(IPinset pinset, HttpWebRequest httpWebRequest, X509Certificate certificate, SslPolicyErrors sslPolicyErrors) { try { RequestDigestSet requestDigestSet = new RequestDigestSet(); if (requestDigestSet.ValidateCertificate(certificate, sslPolicyErrors)) { if (pinset == null) { return(sslPolicyErrors == SslPolicyErrors.None); } string text = httpWebRequest.RequestUri.ToString(); IList <IPinningInfo> pinningInfo = pinset.getPinningInfo(text); bool result = true; if (pinningInfo != null) { foreach (IPinningInfo item in pinningInfo) { if (verified(item, requestDigestSet)) { return(true); } if (item.getMode() != PinningMode.ADVISORY) { result = false; } } return(result); } UnityEngine.Debug.LogError("The URL '" + text + "' does not match any patterns in the certificate pinset file. The certificate it is presenting will not be trusted."); } } catch (Exception ex) { Console.WriteLine(ex.Message); UnityEngine.Debug.LogException(ex); } return(false); }