AddRange() public method

public AddRange ( System certificates ) : void
certificates System
return void
Example #1
0
        private static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                Console.WriteLine("Enter PIN: ");
                string pin = Console.ReadLine();

                WebRequestHandler handler = new WebRequestHandler();
                handler.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

                using (HttpClient client = new HttpClient(handler, true))
                {
                    client.BaseAddress = new Uri(string.Format(@"https://{0}:{1}", MachineName, RemotePort));

                    X509Store store = null;

                    try
                    {
                        var response = await client.GetAsync("certs/" + pin);
                        response.EnsureSuccessStatusCode();

                        byte[] rawCert = await response.Content.ReadAsByteArrayAsync();

                        X509Certificate2Collection certs = new X509Certificate2Collection();
                        certs.Import(rawCert, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.UserKeySet);

                        store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                        store.Open(OpenFlags.ReadWrite);

                        X509Certificate2Collection oldCerts = new X509Certificate2Collection();

                        foreach (var cert in certs)
                        {
                            oldCerts.AddRange(store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, cert.Subject, false));
                        }
                        store.RemoveRange(certs);
                        store.AddRange(certs);
                        store.Close();

                        Console.WriteLine("Success");
                    }
                    catch (HttpRequestException e)
                    {
                        Console.WriteLine("Error communicating with vcremote. Make sure that vcremote is running in secure mode and that a new client cert has been generated.");
                    }
                    finally
                    {
                        if (store != null)
                        {
                            store.Close();
                        }
                    }
                }
            }).Wait();
        }
		/// <summary>
		/// Imports certificates and keys from a pkcs12-encoded stream.
		/// </summary>
		/// <remarks>
		/// Imports certificates and keys from a pkcs12-encoded stream.
		/// </remarks>
		/// <param name="stream">The raw certificate and key data.</param>
		/// <param name="password">The password to unlock the stream.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="stream"/> is <c>null</c>.</para>
		/// <para>-or-</para>
		/// <para><paramref name="password"/> is <c>null</c>.</para>
		/// </exception>
		/// <exception cref="System.NotSupportedException">
		/// Importing keys is not supported by this cryptography context.
		/// </exception>
		public override void Import (Stream stream, string password)
		{
			if (stream == null)
				throw new ArgumentNullException ("stream");

			if (password == null)
				throw new ArgumentNullException ("password");

			var rawData = ReadAllBytes (stream);
			var store = new X509Store (StoreName.My, StoreLocation);
			var certs = new X509Certificate2Collection ();

			store.Open (OpenFlags.ReadWrite);
			certs.Import (rawData, password, X509KeyStorageFlags.UserKeySet);
			store.AddRange (certs);
			store.Close ();
		}
        /// <summary>
        /// Imports certificates and keys from a pkcs12-encoded stream.
        /// </summary>
        /// <param name="stream">The raw certificate and key data.</param>
        /// <param name="password">The password to unlock the stream.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="stream"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="password"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.NotSupportedException">
        /// Importing keys is not supported by this cryptography context.
        /// </exception>
        public override void Import(Stream stream, string password)
        {
            if (stream == null)
                throw new ArgumentNullException ("stream");

            if (password == null)
                throw new ArgumentNullException ("password");

            byte[] rawData;

            if (stream is MemoryBlockStream) {
                rawData = ((MemoryBlockStream) stream).ToArray ();
            } else if (stream is MemoryStream) {
                rawData = ((MemoryStream) stream).ToArray ();
            } else {
                using (var memory = new MemoryStream ()) {
                    stream.CopyTo (memory, 4096);
                    rawData = memory.ToArray ();
                }
            }
            var store = new X509Store (StoreName.My, StoreLocation);
            var certs = new X509Certificate2Collection ();

            store.Open (OpenFlags.ReadWrite);
            certs.Import (rawData, password, X509KeyStorageFlags.UserKeySet);
            store.AddRange (certs);
            store.Close ();
        }
        private void StoreEntityCertificatesCore(X509Certificate2Collection certs, StoreName storeName, StoreLocation storeLocation)
        {
            X509Store x509Store = null;

            x509Store = new X509Store(storeName, storeLocation);

            x509Store.Open(OpenFlags.ReadWrite | OpenFlags.MaxAllowed);

            x509Store.AddRange(certs);

            x509Store.Close();
        }
Example #5
0
		public void RemoveRange_Empty_Certificate ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.Open (OpenFlags.ReadWrite);
			// note: impossible to add cert_empty, so we add something else
			// to be sure we'll follow the complete code path (loop) of removal
			xs.AddRange (coll);
			xs.RemoveRange (new X509Certificate2Collection (cert_empty));
		}
Example #6
0
		public void RemoveRange_OpenReadOnly_Existing ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.Open (OpenFlags.ReadWrite);
			xs.AddRange (coll);
			xs.Close ();
			xs.Open (OpenFlags.ReadOnly);
			xs.RemoveRange (coll);
		}
Example #7
0
		public void AddRange_OpenReadOnly ()
		{
			X509Store xs = new X509Store ("ReadOnlyStore");
			xs.Open (OpenFlags.ReadOnly);
			xs.AddRange (coll);
		}
Example #8
0
		public void AddRange ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.Open (OpenFlags.ReadWrite);
			xs.AddRange (coll);
			Assert.AreEqual (coll.Count, xs.Certificates.Count, "Count");
		}
Example #9
0
		public void AddRange_Empty_Certificate ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.Open (OpenFlags.ReadWrite);
			xs.AddRange (new X509Certificate2Collection (cert_empty));
		}
Example #10
0
		public void AddRange_Empty_ReadOnly ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.Open (OpenFlags.ReadOnly);
			xs.AddRange (coll_empty);
			Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
		}
Example #11
0
		public void AddRange_Empty_Closed ()
		{
			X509Store xs = new X509Store ("ReadWriteStore");
			xs.AddRange (coll_empty);
			Assert.AreEqual (coll_empty.Count, xs.Certificates.Count, "Count");
		}