Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the CertificateProvider class.
        /// This initializes the default certificate store locations.
        /// </summary>
        public CertificateProvider()
        {
            //
            // initialize storeLocations list and also update the cache
            //
            lock (s_staticLock)
            {
                if (s_storeLocations == null)
                {
                    s_pathCache = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    s_storeLocations =
                        new List<X509StoreLocation>();

                    //
                    // create and cache CurrentUser store-location
                    //
                    X509StoreLocation user =
                        new X509StoreLocation(StoreLocation.CurrentUser);
                    s_storeLocations.Add(user);
                    AddItemToCache(StoreLocation.CurrentUser.ToString(),
                                  user);

                    //
                    // create and cache LocalMachine store-location
                    //
                    X509StoreLocation machine =
                        new X509StoreLocation(StoreLocation.LocalMachine);
                    s_storeLocations.Add(machine);
                    AddItemToCache(StoreLocation.LocalMachine.ToString(),
                                   machine);

                    AddItemToCache("", s_storeLocations);
                }
            }
        } // constructor
Ejemplo n.º 2
0
        public static void WriteSendAsTrustedIssuerProperty(X509Certificate2 cert, string certPath, bool addProperty)
        {
            if (DownLevelHelper.TrustedIssuerSupported())
            {
                IntPtr propertyPtr = IntPtr.Zero;
                Security.NativeMethods.CRYPT_DATA_BLOB dataBlob = new Security.NativeMethods.CRYPT_DATA_BLOB();
                dataBlob.cbData = 0;
                dataBlob.pbData = IntPtr.Zero;
                X509Certificate certFromStore = null;

                try
                {
                    if (certPath != null)
                    {
                        //try to open the store and get the cert out
                        //in case the store handle is already released
                        string[] pathElements = GetPathElements(certPath);

                        //certpath is in the format: Microsoft.Powershell.Security\
                        //Certificate::CurrentUser(LocalMachine)\my\HashID
                        //obtained pathElements[0] is Microsoft.Powershell.Security
                        //obtained pathElements[1] is Certificate::CurrentUser
                        //obtained pathElements[2] is MY
                        //obtained pathElements[3] is HashID

                        bool fUserContext = String.Equals(pathElements[1], "Certificate::CurrentUser", StringComparison.OrdinalIgnoreCase);

                        X509StoreLocation storeLocation =
                            new X509StoreLocation(fUserContext ? StoreLocation.CurrentUser : StoreLocation.LocalMachine);

                        //get certificate from the store pathElements[2]
                        X509NativeStore store = null;

                        store = new X509NativeStore(storeLocation, pathElements[2]);
                        store.Open(true); //including archival flag

                        IntPtr certContext = store.GetCertByName(pathElements[3]);

                        if (certContext != IntPtr.Zero)
                        {
                            certFromStore = new X509Certificate2(certContext);
                            store.FreeCert(certContext);
                        }
                    }

                    if (addProperty) //should add the property
                    {
                        propertyPtr = Marshal.AllocHGlobal(Marshal.SizeOf(dataBlob));
                        Marshal.StructureToPtr(dataBlob, propertyPtr, false);
                    }

                    //set property
                    if (!Security.NativeMethods.CertSetCertificateContextProperty(
                                certFromStore != null ? certFromStore.Handle : cert.Handle,
                                Security.NativeMethods.CertPropertyId.CERT_SEND_AS_TRUSTED_ISSUER_PROP_ID,
                                0,
                                propertyPtr))
                    {
                        throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                finally
                {
                    if (propertyPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(propertyPtr);
                    }
                }
            }
            else
            {
                throw Marshal.GetExceptionForHR(Security.NativeMethods.NTE_NOT_SUPPORTED);
            }
        }
Ejemplo n.º 3
0
        //#region tracer

        /// <summary>
        /// Initializes a new instance of the X509NativeStore class.
        /// </summary>
        public X509NativeStore(X509StoreLocation StoreLocation, string StoreName)
        {
            _storeLocation = StoreLocation;
            _storeName = StoreName;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// gets the X509NativeStore at the specified path.
        /// Adds to cache if not already there.
        /// </summary>
        ///
        /// <param name="storePath"> path to the store </param>
        ///
        /// <param name="storeName"> name of store (path leaf element) </param>
        ///
        /// <param name="storeLocation"> location of store (CurrentUser or LocalMachine) </param>
        ///
        /// <returns> X509NativeStore object </returns>
        ///
        private X509NativeStore GetStore(string storePath,
                                   string storeName,
                                   X509StoreLocation storeLocation)
        {
            if (!storeLocation.StoreNames.ContainsKey(storeName))
            {
                ThrowItemNotFound(storePath, CertificateProviderItem.Store);
            }
            if (s_storeCache != null)
            {
                if (s_storeCache.Location != storeLocation ||
                    !String.Equals(
                                s_storeCache.StoreName,
                                storeName,
                                StringComparison.OrdinalIgnoreCase))
                {
                    s_storeCache = null;
                }
            }

            if (s_storeCache == null)
            {
                s_storeCache = new X509NativeStore(storeLocation, storeName);
            }

            return s_storeCache;
        }
Ejemplo n.º 5
0
		public CertificateProvider()
		{
			lock (CertificateProvider.staticLock)
			{
				if (CertificateProvider.storeLocations == null)
				{
					CertificateProvider.pathCache = new Hashtable(StringComparer.OrdinalIgnoreCase);
					CertificateProvider.storeLocations = new List<X509StoreLocation>();
					X509StoreLocation x509StoreLocation = new X509StoreLocation(StoreLocation.CurrentUser);
					CertificateProvider.storeLocations.Add(x509StoreLocation);
					CertificateProvider.AddItemToCache(StoreLocation.CurrentUser.ToString(), x509StoreLocation);
					X509StoreLocation x509StoreLocation1 = new X509StoreLocation(StoreLocation.LocalMachine);
					CertificateProvider.storeLocations.Add(x509StoreLocation1);
					CertificateProvider.AddItemToCache(StoreLocation.LocalMachine.ToString(), x509StoreLocation1);
					CertificateProvider.AddItemToCache("", CertificateProvider.storeLocations);
				}
			}
		}
Ejemplo n.º 6
0
 public X509NativeStore(X509StoreLocation StoreLocation, string StoreName)
 {
     this.storeLocation = StoreLocation;
     this.storeName     = StoreName;
 }
Ejemplo n.º 7
0
        public static void WriteSendAsTrustedIssuerProperty(X509Certificate2 cert, string certPath, bool addProperty)
        {
            IntPtr        handle;
            StoreLocation storeLocation;

            if (!DownLevelHelper.IsWin8AndAbove())
            {
                throw Marshal.GetExceptionForHR(-2146893783);
            }
            else
            {
                IntPtr zero = IntPtr.Zero;
                NativeMethods.CRYPT_DATA_BLOB cRYPTDATABLOB = new NativeMethods.CRYPT_DATA_BLOB();
                cRYPTDATABLOB.cbData = 0;
                cRYPTDATABLOB.pbData = IntPtr.Zero;
                X509Certificate x509Certificate2 = null;
                try
                {
                    if (certPath != null)
                    {
                        string[] pathElements = SendAsTrustedIssuerProperty.GetPathElements(certPath);
                        bool     flag         = string.Equals(pathElements[0], "CurrentUser", StringComparison.OrdinalIgnoreCase);
                        if (flag)
                        {
                            storeLocation = StoreLocation.CurrentUser;
                        }
                        else
                        {
                            storeLocation = StoreLocation.LocalMachine;
                        }
                        X509StoreLocation x509StoreLocation = new X509StoreLocation(storeLocation);
                        X509NativeStore   x509NativeStore   = new X509NativeStore(x509StoreLocation, pathElements[1]);
                        x509NativeStore.Open(true);
                        IntPtr certByName = x509NativeStore.GetCertByName(pathElements[2]);
                        if (certByName != IntPtr.Zero)
                        {
                            x509Certificate2 = new X509Certificate2(certByName);
                            x509NativeStore.FreeCert(certByName);
                        }
                    }
                    if (addProperty)
                    {
                        zero = Marshal.AllocHGlobal(Marshal.SizeOf(cRYPTDATABLOB));
                        Marshal.StructureToPtr(cRYPTDATABLOB, zero, false);
                    }
                    if (x509Certificate2 != null)
                    {
                        handle = x509Certificate2.Handle;
                    }
                    else
                    {
                        handle = cert.Handle;
                    }
                    if (!NativeMethods.CertSetCertificateContextProperty(handle, NativeMethods.CertPropertyId.CERT_SEND_AS_TRUSTED_ISSUER_PROP_ID, 0, zero))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(zero);
                    }
                }
                return;
            }
        }
Ejemplo n.º 8
0
		public static void WriteSendAsTrustedIssuerProperty(X509Certificate2 cert, string certPath, bool addProperty)
		{
			IntPtr handle;
			StoreLocation storeLocation;
			if (!DownLevelHelper.IsWin8AndAbove())
			{
				throw Marshal.GetExceptionForHR(-2146893783);
			}
			else
			{
				IntPtr zero = IntPtr.Zero;
				NativeMethods.CRYPT_DATA_BLOB cRYPTDATABLOB = new NativeMethods.CRYPT_DATA_BLOB();
				cRYPTDATABLOB.cbData = 0;
				cRYPTDATABLOB.pbData = IntPtr.Zero;
				X509Certificate x509Certificate2 = null;
				try
				{
					if (certPath != null)
					{
						string[] pathElements = SendAsTrustedIssuerProperty.GetPathElements(certPath);
						bool flag = string.Equals(pathElements[0], "CurrentUser", StringComparison.OrdinalIgnoreCase);
						if (flag)
						{
							storeLocation = StoreLocation.CurrentUser;
						}
						else
						{
							storeLocation = StoreLocation.LocalMachine;
						}
						X509StoreLocation x509StoreLocation = new X509StoreLocation(storeLocation);
						X509NativeStore x509NativeStore = new X509NativeStore(x509StoreLocation, pathElements[1]);
						x509NativeStore.Open(true);
						IntPtr certByName = x509NativeStore.GetCertByName(pathElements[2]);
						if (certByName != IntPtr.Zero)
						{
							x509Certificate2 = new X509Certificate2(certByName);
							x509NativeStore.FreeCert(certByName);
						}
					}
					if (addProperty)
					{
						zero = Marshal.AllocHGlobal(Marshal.SizeOf(cRYPTDATABLOB));
						Marshal.StructureToPtr(cRYPTDATABLOB, zero, false);
					}
					if (x509Certificate2 != null)
					{
						handle = x509Certificate2.Handle;
					}
					else
					{
						handle = cert.Handle;
					}
					if (!NativeMethods.CertSetCertificateContextProperty(handle, NativeMethods.CertPropertyId.CERT_SEND_AS_TRUSTED_ISSUER_PROP_ID, 0, zero))
					{
						throw new Win32Exception(Marshal.GetLastWin32Error());
					}
				}
				finally
				{
					if (zero != IntPtr.Zero)
					{
						Marshal.FreeHGlobal(zero);
					}
				}
				return;
			}
		}