Esempio n. 1
0
        public void Open(bool includeArchivedCerts)
        {
            if (this.storeHandle != null && this.archivedCerts != includeArchivedCerts)
            {
                this.storeHandle = null;
            }
            if (this.storeHandle == null)
            {
                this.valid = false;
                this.open  = false;
                NativeMethods.CertOpenStoreFlags certOpenStoreFlag = NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_STORE_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_CONTEXT_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_OPEN_EXISTING_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_MAXIMUM_ALLOWED_FLAG;
                if (includeArchivedCerts)
                {
                    certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG;
                }
                StoreLocation location = this.storeLocation.Location;
                switch (location)
                {
                case StoreLocation.CurrentUser:
                {
                    certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_CURRENT_USER;
                    break;
                }

                case StoreLocation.LocalMachine:
                {
                    certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_LOCAL_MACHINE;
                    break;
                }
                }
                IntPtr intPtr = NativeMethods.CertOpenStore(NativeMethods.CertOpenStoreProvider.CERT_STORE_PROV_SYSTEM, NativeMethods.CertOpenStoreEncodingType.X509_ASN_ENCODING, IntPtr.Zero, certOpenStoreFlag, this.storeName);
                if (IntPtr.Zero != intPtr)
                {
                    this.storeHandle        = new CertificateStoreHandle();
                    this.storeHandle.Handle = intPtr;
                    if (string.Equals(this.storeName, "UserDS", StringComparison.OrdinalIgnoreCase) || NativeMethods.CertControlStore(this.storeHandle.Handle, 0, NativeMethods.CertControlStoreType.CERT_STORE_CTRL_AUTO_RESYNC, IntPtr.Zero))
                    {
                        this.valid         = true;
                        this.open          = true;
                        this.archivedCerts = includeArchivedCerts;
                    }
                    else
                    {
                        this.storeHandle = null;
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }
Esempio n. 2
0
        public void Open(bool includeArchivedCerts)
        {
            if (_storeHandle != null && _archivedCerts != includeArchivedCerts)
            {
                _storeHandle = null;        // release the old handle
            }
            if (_storeHandle == null)
            {
                _valid = false;
                _open = false;

                Security.NativeMethods.CertOpenStoreFlags StoreFlags =
                    Security.NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_STORE_FLAG |
                    Security.NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_CONTEXT_FLAG |
                    Security.NativeMethods.CertOpenStoreFlags.CERT_STORE_OPEN_EXISTING_FLAG |
                    Security.NativeMethods.CertOpenStoreFlags.CERT_STORE_MAXIMUM_ALLOWED_FLAG;

                if (includeArchivedCerts)
                {
                    StoreFlags |= Security.NativeMethods.CertOpenStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG;
                }

                switch (_storeLocation.Location)
                {
                    case StoreLocation.LocalMachine:
                        StoreFlags |= Security.NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_LOCAL_MACHINE;
                        break;

                    case StoreLocation.CurrentUser:
                        StoreFlags |= Security.NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_CURRENT_USER;
                        break;

                    default:
                        //ThrowItemNotFound(storeLocation.ToString(), CertificateProviderItem.StoreLocation);
                        break;
                }

                IntPtr hCertStore = Security.NativeMethods.CertOpenStore(
                                Security.NativeMethods.CertOpenStoreProvider.CERT_STORE_PROV_SYSTEM,
                                Security.NativeMethods.CertOpenStoreEncodingType.X509_ASN_ENCODING,
                                IntPtr.Zero,  // hCryptProv
                                StoreFlags,
                                _storeName);
                if (IntPtr.Zero == hCertStore)
                {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                }

                _storeHandle = new CertificateStoreHandle();
                _storeHandle.Handle = hCertStore;

                //we only do CertControlStore for stores other than UserDS
                if (!String.Equals(
                                _storeName,
                                "UserDS",
                                StringComparison.OrdinalIgnoreCase))
                {
                    if (!Security.NativeMethods.CertControlStore(
                                _storeHandle.Handle,
                                0,
                                Security.NativeMethods.CertControlStoreType.CERT_STORE_CTRL_AUTO_RESYNC,
                                IntPtr.Zero))
                    {
                        _storeHandle = null;
                        throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                    }
                }

                _valid = true;
                _open = true;
                _archivedCerts = includeArchivedCerts;
            }
        }
Esempio n. 3
0
		public void Open(bool includeArchivedCerts)
		{
			if (this.storeHandle != null && this.archivedCerts != includeArchivedCerts)
			{
				this.storeHandle = null;
			}
			if (this.storeHandle == null)
			{
				this.valid = false;
				this.open = false;
				NativeMethods.CertOpenStoreFlags certOpenStoreFlag = NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_STORE_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_SHARE_CONTEXT_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_OPEN_EXISTING_FLAG | NativeMethods.CertOpenStoreFlags.CERT_STORE_MAXIMUM_ALLOWED_FLAG;
				if (includeArchivedCerts)
				{
					certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_STORE_ENUM_ARCHIVED_FLAG;
				}
				StoreLocation location = this.storeLocation.Location;
				switch (location)
				{
					case StoreLocation.CurrentUser:
					{
						certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_CURRENT_USER;
						break;
					}
					case StoreLocation.LocalMachine:
					{
						certOpenStoreFlag = certOpenStoreFlag | NativeMethods.CertOpenStoreFlags.CERT_SYSTEM_STORE_LOCAL_MACHINE;
						break;
					}
				}
				IntPtr intPtr = NativeMethods.CertOpenStore(NativeMethods.CertOpenStoreProvider.CERT_STORE_PROV_SYSTEM, NativeMethods.CertOpenStoreEncodingType.X509_ASN_ENCODING, IntPtr.Zero, certOpenStoreFlag, this.storeName);
				if (IntPtr.Zero != intPtr)
				{
					this.storeHandle = new CertificateStoreHandle();
					this.storeHandle.Handle = intPtr;
					if (string.Equals(this.storeName, "UserDS", StringComparison.OrdinalIgnoreCase) || NativeMethods.CertControlStore(this.storeHandle.Handle, 0, NativeMethods.CertControlStoreType.CERT_STORE_CTRL_AUTO_RESYNC, IntPtr.Zero))
					{
						this.valid = true;
						this.open = true;
						this.archivedCerts = includeArchivedCerts;
					}
					else
					{
						this.storeHandle = null;
						throw new Win32Exception(Marshal.GetLastWin32Error());
					}
				}
				else
				{
					throw new Win32Exception(Marshal.GetLastWin32Error());
				}
			}
		}