示例#1
0
        public static void X509CertificateCollectionEnumerator()
        {
            using (X509Certificate2 c1 = new X509Certificate2())
                using (X509Certificate2 c2 = new X509Certificate2())
                    using (X509Certificate2 c3 = new X509Certificate2())
                    {
                        X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
                        X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
                        object ignored;

                        // Not started
                        Assert.Throws <InvalidOperationException>(() => ignored = e.Current);

                        Assert.True(e.MoveNext());
                        Assert.Same(c1, e.Current);

                        Assert.True(e.MoveNext());
                        Assert.Same(c2, e.Current);

                        Assert.True(e.MoveNext());
                        Assert.Same(c3, e.Current);

                        Assert.False(e.MoveNext());
                        Assert.False(e.MoveNext());
                        Assert.False(e.MoveNext());
                        Assert.False(e.MoveNext());
                        Assert.False(e.MoveNext());

                        // ended.
                        Assert.Throws <InvalidOperationException>(() => ignored = e.Current);
                    }
        }
示例#2
0
        public static void X509CertificateCollectionEnumerator()
        {
            using (X509Certificate2 c1 = new X509Certificate2())
            using (X509Certificate2 c2 = new X509Certificate2())
            using (X509Certificate2 c3 = new X509Certificate2())
            {
                X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
                X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
                object ignored;

                // Not started
                Assert.Throws<InvalidOperationException>(() => ignored = e.Current);

                Assert.True(e.MoveNext());
                Assert.Same(c1, e.Current);

                Assert.True(e.MoveNext());
                Assert.Same(c2, e.Current);

                Assert.True(e.MoveNext());
                Assert.Same(c3, e.Current);

                Assert.False(e.MoveNext());
                Assert.False(e.MoveNext());
                Assert.False(e.MoveNext());
                Assert.False(e.MoveNext());
                Assert.False(e.MoveNext());

                // ended.
                Assert.Throws<InvalidOperationException>(() => ignored = e.Current);
            }
        }
示例#3
0
        //
        // virtual methods
        //

        protected virtual AsymmetricAlgorithm GetPublicKey()
        {
            if (KeyInfo == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_KeyInfoRequired"));
            }

            if (m_x509Enum != null)
            {
                AsymmetricAlgorithm key = GetNextCertificatePublicKey();
                if (key != null)
                {
                    return(key);
                }
            }

            if (m_keyInfoEnum == null)
            {
                m_keyInfoEnum = KeyInfo.GetEnumerator();
            }

            // In our implementation, we move to the next KeyInfo clause which is an RSAKeyValue, DSAKeyValue or KeyInfoX509Data
            while (m_keyInfoEnum.MoveNext())
            {
                RSAKeyValue rsaKeyValue = m_keyInfoEnum.Current as RSAKeyValue;
                if (rsaKeyValue != null)
                {
                    return(rsaKeyValue.Key);
                }

                DSAKeyValue dsaKeyValue = m_keyInfoEnum.Current as DSAKeyValue;
                if (dsaKeyValue != null)
                {
                    return(dsaKeyValue.Key);
                }

                KeyInfoX509Data x509Data = m_keyInfoEnum.Current as KeyInfoX509Data;
                if (x509Data != null)
                {
                    m_x509Collection = Utils.BuildBagOfCerts(x509Data, CertUsageType.Verification);
                    if (m_x509Collection.Count > 0)
                    {
                        m_x509Enum = m_x509Collection.GetEnumerator();
                        AsymmetricAlgorithm key = GetNextCertificatePublicKey();
                        if (key != null)
                        {
                            return(key);
                        }
                    }
                }

                GostKeyValue gostKeyValue = m_keyInfoEnum.Current as GostKeyValue;
                if (gostKeyValue != null)
                {
                    return(gostKeyValue.Key);
                }
            }

            return(null);
        }
示例#4
0
        public static void X509Certificate2CollectionEnumeratorModification()
        {
            using (X509Certificate2 c1 = new X509Certificate2())
                using (X509Certificate2 c2 = new X509Certificate2())
                    using (X509Certificate2 c3 = new X509Certificate2())
                    {
                        X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
                        X509Certificate2Collection.X509CertificateEnumerator e = cc.GetEnumerator();

                        cc.Add(c1);

                        // Collection changed.
                        Assert.Throws <InvalidOperationException>(() => e.MoveNext());
                    }
        }
示例#5
0
        public static void X509CertificateCollectionEnumeratorModification()
        {
            using (X509Certificate c1 = new X509Certificate())
            using (X509Certificate c2 = new X509Certificate())
            using (X509Certificate c3 = new X509Certificate())
            {
                X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
                X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();

                cc.Add(c1);

                // Collection changed.
                Assert.Throws<InvalidOperationException>(() => e.MoveNext());
                Assert.Throws<InvalidOperationException>(() => e.Reset());
            }
        }
示例#6
0
        public static void X509CertificateCollectionEnumerator()
        {
            using (X509Certificate2 c1 = new X509Certificate2())
            using (X509Certificate2 c2 = new X509Certificate2())
            using (X509Certificate2 c3 = new X509Certificate2())
            {
                X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
                object ignored;

                X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
                for (int i = 0; i < 2; i++)
                {
                    // Not started
                    Assert.Throws<InvalidOperationException>(() => ignored = e.Current);

                    Assert.True(e.MoveNext());
                    Assert.Same(c1, e.Current);

                    Assert.True(e.MoveNext());
                    Assert.Same(c2, e.Current);

                    Assert.True(e.MoveNext());
                    Assert.Same(c3, e.Current);

                    Assert.False(e.MoveNext());
                    Assert.False(e.MoveNext());
                    Assert.False(e.MoveNext());
                    Assert.False(e.MoveNext());
                    Assert.False(e.MoveNext());

                    // Ended
                    Assert.Throws<InvalidOperationException>(() => ignored = e.Current);

                    e.Reset();
                }

                IEnumerator e2 = cc.GetEnumerator();
                TestNonGenericEnumerator(e2, c1, c2, c3);

                IEnumerator e3 = ((IEnumerable)cc).GetEnumerator();
                TestNonGenericEnumerator(e3, c1, c2, c3);
            }
        }
        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);
        }
        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();
                    }
                }
            }
        }