private static bool IsCertBugPresent(RuntimePlatform platform)
 {
     if (platform == RuntimePlatform.OSXEditor || platform == RuntimePlatform.OSXPlayer)
     {
         Type type = typeof(X509Certificate2).Assembly.GetType("System.Security.Cryptography.X509Certificates.OSX509Certificates");
         if (type != null)
         {
             try
             {
                 InstallRootCerts.AFunctionThatDoesNotExist();
             }
             catch (DllNotFoundException)
             {
                 bool result = true;
                 return(result);
             }
             catch (EntryPointNotFoundException)
             {
                 bool result = false;
                 return(result);
             }
             catch
             {
             }
             return(true);
         }
     }
     return(false);
 }
        public void UpdateRootCertificates(IFirebaseAppPlatform app)
        {
            if (!InstallRootCerts.InstallationRequired)
            {
                return;
            }
            object sync = InstallRootCerts.Sync;

            lock (sync)
            {
                if (!InstallRootCerts._attemptedWebDownload)
                {
                    InstallRootCerts._attemptedWebDownload = true;
                    X509CertificateCollection x509CertificateCollection = null;
                    try
                    {
                        x509CertificateCollection = InstallRootCerts.DecodeWebRootCollection(app);
                    }
                    catch (Exception ex)
                    {
                        Services.Logging.LogMessage(PlatformLogLevel.Error, ex.ToString());
                    }
                    if (x509CertificateCollection != null && x509CertificateCollection.Count != 0)
                    {
                        X509Store x509Store = new X509Store(InstallRootCerts.TrustedRoot);
                        x509Store.Open(OpenFlags.ReadWrite);
                        X509CertificateCollection certificates = x509Store.Certificates;
                        X509CertificateCollection.X509CertificateEnumerator enumerator = x509CertificateCollection.GetEnumerator();
                        try
                        {
                            while (enumerator.MoveNext())
                            {
                                X509Certificate current = enumerator.Current;
                                if (!certificates.Contains(current))
                                {
                                    try
                                    {
                                        x509Store.Add((X509Certificate2)current);
                                    }
                                    catch (Exception ex2)
                                    {
                                        Services.Logging.LogMessage(PlatformLogLevel.Error, ex2.ToString());
                                    }
                                }
                            }
                        }
                        finally
                        {
                            IDisposable disposable;
                            if ((disposable = (enumerator as IDisposable)) != null)
                            {
                                disposable.Dispose();
                            }
                        }
                        x509Store.Close();
                        this.HackRefreshMonoRootStore();
                    }
                }
            }
        }
        private static X509CertificateCollection DecodeCertificateCollectionFromString(string certString)
        {
            X509CertificateCollection x509CertificateCollection = new X509CertificateCollection();

            foreach (byte[] current in InstallRootCerts.DecodeBase64Blobs(certString, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----"))
            {
                x509CertificateCollection.Add(new X509Certificate2(current));
            }
            return(x509CertificateCollection);
        }
        private static X509CertificateCollection DecodeCollection(IFirebaseAppPlatform app)
        {
            string certPemFile = Services.AppConfig.GetCertPemFile(app);

            if (!string.IsNullOrEmpty(certPemFile))
            {
                TextAsset textAsset = Resources.Load(certPemFile) as TextAsset;
                if (textAsset != null)
                {
                    return(InstallRootCerts.DecodeCertificateCollectionFromString(textAsset.text));
                }
            }
            return(new X509CertificateCollection());
        }
 private static X509CertificateCollection DecodeDefaultCollection()
 {
     try
     {
         using (StreamReader streamReader = new StreamReader(typeof(InstallRootCerts).Assembly.GetManifestResourceStream("Firebase.Platform.cacert_pem.txt")))
         {
             return(InstallRootCerts.DecodeCertificateCollectionFromString(streamReader.ReadToEnd()));
         }
     }
     catch (Exception ex)
     {
         Services.Logging.LogMessage(PlatformLogLevel.Error, ex.ToString());
     }
     return(new X509CertificateCollection());
 }
        private InstallRootCerts()
        {
            var __AnonType = FirebaseHandler.RunOnMainThread(() => new
            {
                Platform             = Application.platform,
                IsEditor             = Application.isEditor,
                InstallationRequired = InstallRootCerts.InstallationRequired
            });

            if (__AnonType.InstallationRequired && InstallRootCerts.IsCertBugPresent(__AnonType.Platform))
            {
                if (__AnonType.IsEditor)
                {
                    Services.Logging.LogMessage(PlatformLogLevel.Info, "Using workaround for .NET 4.6 certificate bug.");
                    this._needsCertificateWorkaround = true;
                }
                else
                {
                    Services.Logging.LogMessage(PlatformLogLevel.Warning, "Detected .NET 4.6 certificate bug, Firebase might not work.");
                }
            }
        }
        private static X509CertificateCollection DecodeWebRootCollection(IFirebaseAppPlatform app)
        {
            Uri certUpdateUrl = Services.AppConfig.GetCertUpdateUrl(app);

            if (certUpdateUrl != null)
            {
                FirebaseHttpRequest firebaseHttpRequest = Services.HttpFactory.OpenConnection(certUpdateUrl);
                firebaseHttpRequest.SetRequestMethod("GET");
                if (firebaseHttpRequest.ResponseCode >= 200 && firebaseHttpRequest.ResponseCode < 300)
                {
                    Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("updated certs from {0} completed with code {1}", certUpdateUrl, firebaseHttpRequest.ResponseCode.ToString()));
                    StreamReader streamReader = new StreamReader(firebaseHttpRequest.InputStream);
                    return(InstallRootCerts.DecodeCertificateCollectionFromString(streamReader.ReadToEnd()));
                }
                Services.Logging.LogMessage(PlatformLogLevel.Error, string.Format("error loading updated certs from {0} with code {1}", certUpdateUrl, firebaseHttpRequest.ResponseCode.ToString()));
            }
            else
            {
                Services.Logging.LogMessage(PlatformLogLevel.Warning, "No root cert url to download.");
            }
            return(new X509CertificateCollection());
        }
        private static void InstallDefaultCRLs(string resource_name, string directory)
        {
            Directory.CreateDirectory(directory);
            int num = 0;

            Services.Logging.LogMessage(PlatformLogLevel.Debug, "Installing CRLs in " + directory);
            try
            {
                Stream manifestResourceStream = typeof(InstallRootCerts).Assembly.GetManifestResourceStream(resource_name);
                using (StreamReader streamReader = new StreamReader(manifestResourceStream))
                {
                    foreach (byte[] current in InstallRootCerts.DecodeBase64Blobs(streamReader.ReadToEnd(), "-----BEGIN X509 CRL-----", "-----END X509 CRL-----"))
                    {
                        string str;
                        using (MD5 mD = MD5.Create())
                        {
                            str = BitConverter.ToString(mD.ComputeHash(current)).Replace("-", string.Empty);
                        }
                        string path = Path.Combine(directory, str + ".crl");
                        if (!File.Exists(path))
                        {
                            using (BinaryWriter binaryWriter = new BinaryWriter(new FileStream(path, FileMode.CreateNew)))
                            {
                                binaryWriter.Write(current);
                            }
                            num++;
                        }
                    }
                }
                Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("Installed {0} CRLs", num));
            }
            catch (Exception ex)
            {
                Services.Logging.LogMessage(PlatformLogLevel.Error, string.Format("Error installing CRLs: {0}", ex.ToString()));
            }
        }
 private static void AFunctionThatDoesNotExist()
 {
     InstallRootCerts.AFunctionThatDoesNotExistInternal();
 }
        public X509CertificateCollection Install(IFirebaseAppPlatform app)
        {
            if (!InstallRootCerts.InstallationRequired)
            {
                return(null);
            }
            object sync = InstallRootCerts.Sync;
            X509CertificateCollection result;

            lock (sync)
            {
                X509CertificateCollection x509CertificateCollection;
                if (InstallRootCerts._installedRoots.TryGetValue(app, out x509CertificateCollection))
                {
                    result = x509CertificateCollection;
                }
                else
                {
                    x509CertificateCollection = new X509CertificateCollection();
                    string text = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".mono"), "certs");
                    bool   flag = false;
                    try
                    {
                        flag = (Directory.Exists(text) || Directory.CreateDirectory(text) != null);
                    }
                    catch (Exception)
                    {
                    }
                    if (!flag)
                    {
                        string writeablePath = Services.AppConfig.GetWriteablePath(app);
                        if (!string.IsNullOrEmpty(writeablePath))
                        {
                            Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("Saving root certs in {0} ({1} is not writable)", writeablePath, text));
                            Environment.SetEnvironmentVariable("XDG_CONFIG_HOME", writeablePath);
                            text = writeablePath;
                            this.HackRefreshMonoRootStore();
                        }
                    }
                    X509CertificateCollection value = InstallRootCerts.DecodeDefaultCollection();
                    X509CertificateCollection x509CertificateCollection2 = InstallRootCerts.DecodeCollection(app);
                    if (string.Equals(app.Name, FirebaseHandler.AppUtils.GetDefaultInstanceName()))
                    {
                        x509CertificateCollection2.AddRange(value);
                        x509CertificateCollection = x509CertificateCollection2;
                    }
                    else
                    {
                        x509CertificateCollection.AddRange(value);
                    }
                    InstallRootCerts._installedRoots[app] = x509CertificateCollection2;
                    if (x509CertificateCollection.Count == 0)
                    {
                        result = x509CertificateCollection;
                    }
                    else
                    {
                        InstallRootCerts.InstallDefaultCRLs("Firebase.Platform.cacrl_pem.txt", Path.Combine(text, InstallRootCerts.TrustedRoot));
                        InstallRootCerts.InstallDefaultCRLs("Firebase.Platform.caintermediatecrl_pem.txt", Path.Combine(text, InstallRootCerts.IntermediateCA));
                        Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("Installing {0} certs", x509CertificateCollection2.Count));
                        X509Store x509Store = new X509Store(InstallRootCerts.TrustedRoot);
                        x509Store.Open(OpenFlags.ReadWrite);
                        X509CertificateCollection certificates = x509Store.Certificates;
                        X509CertificateCollection.X509CertificateEnumerator enumerator = x509CertificateCollection.GetEnumerator();
                        try
                        {
                            while (enumerator.MoveNext())
                            {
                                X509Certificate current = enumerator.Current;
                                if (!certificates.Contains(current))
                                {
                                    try
                                    {
                                        x509Store.Add((X509Certificate2)current);
                                    }
                                    catch (Exception ex)
                                    {
                                        Services.Logging.LogMessage(PlatformLogLevel.Error, ex.ToString());
                                    }
                                }
                            }
                        }
                        finally
                        {
                            IDisposable disposable;
                            if ((disposable = (enumerator as IDisposable)) != null)
                            {
                                disposable.Dispose();
                            }
                        }
                        x509Store.Close();
                        result = x509CertificateCollection;
                    }
                }
            }
            return(result);
        }