/* * Mentalis.org Security Library * * Copyright © 2002-2005, The KPD-Team * All rights reserved. * http://www.mentalis.org/ * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Neither the name of the KPD-Team, nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.IO; using System.Text; using System.Security; using System.Collections; using System.Runtime.InteropServices; namespace Org.Mentalis.Security.Certificates { /// /// Defines a collection of certificate stores. /// public class CertificateStoreCollection : CertificateStore { /// /// Initializes a new instance of the class. /// /// An array of stores that should be added to the collection. /// is a null reference (Nothing in Visual Basic). /// One of the objects in the array is a instance. This is not allowed to avoid circular dependencies. /// An error occurs while adding a certificate to the collection. public CertificateStoreCollection(CertificateStore[] stores) : base(SspiProvider.CertOpenStore(new IntPtr(SecurityConstants.CERT_STORE_PROV_COLLECTION), 0, 0, 0, null), false) { if (stores == null) throw new ArgumentNullException(); for(int i = 0; i < stores.Length; i++) { if (stores[i].ToString() == this.ToString()) { // used in order to avoid circular dependencies throw new ArgumentException("A certificate store collection cannot hold other certificate store collections."); } } for(int i = 0; i < stores.Length; i++) { if (SspiProvider.CertAddStoreToCollection(this.Handle, stores[i].Handle, 0, 0) == 0) throw new CertificateException("Could not add the store to the collection."); } m_Stores = new ArrayList(); // used to hold references to the certificate stores so they cannot be finalized m_Stores.AddRange(stores); } /// /// Initializes a new instance of the class. /// /// The CertificateStoreCollection whose elements are copied to the new certificate store collection. /// is a null reference (Nothing in Visual Basic). /// An error occurs while adding a certificate to the collection. public CertificateStoreCollection(CertificateStoreCollection collection) : base(SspiProvider.CertOpenStore(new IntPtr(SecurityConstants.CERT_STORE_PROV_COLLECTION), 0, 0, 0, null), false) { if (collection == null) throw new ArgumentNullException(); m_Stores = new ArrayList( collection.m_Stores); // used to hold references to the certificate stores so they cannot be finalized for(int i = 0; i < m_Stores.Count; i++) { if (SspiProvider.CertAddStoreToCollection(this.Handle, ((CertificateStore)m_Stores[i]).Handle, 0, 0) == 0) throw new CertificateException("Could not add the store to the collection."); } } /// /// Adds a certificate store to the collection. /// /// An instance of the class. /// is a null reference (Nothing in Visual Basic). /// The specified certificate store is a instance. This is not allowed to avoid circular dependencies. /// An error occurs while adding the certificate to the collection. public void AddStore(CertificateStore store) { if (store == null) throw new ArgumentNullException(); if (store.ToString() == this.ToString()) // avoid circular dependencies throw new ArgumentException("A certificate store collection cannot hold other certificate store collections."); if (SspiProvider.CertAddStoreToCollection(this.Handle, store.Handle, 0, 0) == 0) throw new CertificateException("Could not add the store to the collection."); m_Stores.Add(store); } /// /// Removes a certificate store from the collection. /// /// An instance of the class. /// is a null reference (Nothing in Visual Basic). public void RemoveStore(CertificateStore store) { if (store == null) throw new ArgumentNullException(); SspiProvider.CertRemoveStoreFromCollection(this.Handle, store.Handle); m_Stores.Remove(store); } /// /// Holds the references to the CertificateStore instances in the collection. This is to avoid CertificateStores finalizing and destroying their handles. /// private ArrayList m_Stores; } }