Beispiel #1
0
        internal static string GetOidFromFriendlyName(string friendlyName, OidGroup oidGroup)
        {
            Contract.Requires(friendlyName != null);
            CRYPT_OID_INFO oidInfo = FindOidInfo(OidKeyType.Name, friendlyName, oidGroup);

            return(oidInfo.pszOID);
        }
Beispiel #2
0
        internal static string GetFriendlyNameFromOid(string oid, OidGroup oidGroup)
        {
            Contract.Requires(oid != null);
            CRYPT_OID_INFO oidInfo = FindOidInfo(OidKeyType.Oid, oid, oidGroup);

            return(oidInfo.pwszName);
        }
Beispiel #3
0
        CRYPT_OID_INFO CryptFindOIDInfo(
            [In]    uint                 dwKeyType,
            [In]    SafeLocalAllocHandle pvKey,
            [In]    OidGroup             dwGroupId) {

            if (pvKey == null)
                throw new ArgumentNullException("pvKey");
            if (pvKey.IsInvalid) 
                throw new CryptographicException(SR.GetString(SR.Cryptography_InvalidHandle), "pvKey");

            CRYPT_OID_INFO pOIDInfo = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO)));
            IntPtr pv = CAPIMethods.CryptFindOIDInfo(dwKeyType, 
                                                     pvKey,
                                                     dwGroupId);

            if (pv != IntPtr.Zero)
                pOIDInfo = (CRYPT_OID_INFO) Marshal.PtrToStructure(pv, typeof(CAPI.CRYPT_OID_INFO));

            return pOIDInfo;
        }
Beispiel #4
0
        CRYPT_OID_INFO CryptFindOIDInfo(
            [In]    uint     dwKeyType,
            [In]    IntPtr   pvKey,
            [In]    OidGroup dwGroupId) {

            if (pvKey == IntPtr.Zero)
                throw new ArgumentNullException("pvKey");

            CRYPT_OID_INFO pOIDInfo = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO)));
            IntPtr pv = CAPIMethods.CryptFindOIDInfo(dwKeyType, 
                                                     pvKey,
                                                     dwGroupId);

            if (pv != IntPtr.Zero)
                pOIDInfo = (CRYPT_OID_INFO) Marshal.PtrToStructure(pv, typeof(CAPI.CRYPT_OID_INFO));

            return pOIDInfo;
        }
Beispiel #5
0
        internal static string GetFriendlyNameFromOid(string oid, OidGroup oidGroup)
        {
            CRYPT_OID_INFO crypt_OID_INFO = X509Utils.FindOidInfo(OidKeyType.Oid, oid, oidGroup);

            return(crypt_OID_INFO.pwszName);
        }
Beispiel #6
0
        internal static string GetOidFromFriendlyName(string friendlyName, OidGroup oidGroup)
        {
            CRYPT_OID_INFO crypt_OID_INFO = X509Utils.FindOidInfo(OidKeyType.Name, friendlyName, oidGroup);

            return(crypt_OID_INFO.pszOID);
        }
Beispiel #7
0
        /// <summary>
        /// Registers the object ids required to access the certificate.
        /// </summary>
        /// <remarks>
        /// This function is used to work around a bug in .NET which results in long delays while OIDs are looked up in Active Directory.
        /// 
        /// CryptFindOIDInfo is supposed to work like this:
        /// 
        /// 1. A table of OID entries is constructed from registry entries with the CRYPT_INSTALL_OID_INFO_BEFORE_FLAG flag.  This table is searched first.
        /// 2. An internal table of OID entries is then searched. Default OIDs that Microsoft knows about.
        /// 3. A table of OIDs constructed from the registry entries without the CRYPT_INSTALL_OID_INFO_BEFORE_FLAG flag is then searched.
        /// 4. Active Directory is searched.        
        ///
        /// When registering the OID information with CryptRegisterOIDInfo(ptrInfo, 0) and hack the registry this is what will happen:
        /// 
        /// 1. Any application that searches for an OID (with the OID flag) will find it at step 2.  The OID will be correct as well as the friendly name since it uses CryptoAPI's internal table.
        /// 2. Any application that searches for a friendly name (with the friendly name flag) will find it as step 2.  The OID info is good as stated above.
        /// 3. .NET code which searches for an OID (with the friendly name flag) will find the entry in step 3 because of our hack.
        /// 4. Any OIDs that isn't found at this point will be searched in the Active Directory.
        /// 
        /// This code needs to be run once for each public key type.
        /// </remarks>
        public static void LocallyRegisterCertificateOIDs(string[] OIDs)
        {
            IntPtr pInfo;
            IntPtr pOID;

            RegistryKey key = null;
            CRYPT_OID_INFO oidInfo = new CRYPT_OID_INFO();

            for (int ii = 0; ii < OIDs.Length; ii++)
            {
                pOID = Marshal.StringToHGlobalAnsi(OIDs[ii]);
                pInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, pOID, 0);
                Marshal.FreeHGlobal(pOID);

                if (pInfo != IntPtr.Zero)
                {
                    Marshal.PtrToStructure(pInfo, oidInfo);
                    
                    if (CryptRegisterOIDInfo(pInfo, CRYPT_INSTALL_OID_INFO_BEFORE_FLAG))
                    {
                        string strRegKey = 
                            "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\CryptDllFindOIDInfo\\" 
                            + oidInfo.pszOID 
                            + "!" 
                            + oidInfo.dwGroupId.ToString();                        
                        
                        key = Registry.LocalMachine.CreateSubKey(strRegKey);
                        key.SetValue("Name", oidInfo.pszOID);
                        key.Close();            
                    }
                }
            }            
        }
        internal static CRYPT_OID_INFO CryptFindOIDInfo(
            [In]    uint                 dwKeyType,
            [In]    SafeLocalAllocHandle pvKey,
            [In]    uint                 dwGroupId) {

            if (pvKey == null)
                throw new ArgumentNullException("pvKey");
            if (pvKey.IsInvalid) 
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "pvKey");

            CRYPT_OID_INFO pOIDInfo = new CRYPT_OID_INFO(Marshal.SizeOf(typeof(CRYPT_OID_INFO)));
            IntPtr pv = CAPISafe.CryptFindOIDInfo(dwKeyType, 
                                                  pvKey,
                                                  dwGroupId);

            if (pv != IntPtr.Zero)
                pOIDInfo = (CRYPT_OID_INFO) Marshal.PtrToStructure(pv, typeof(CAPI.CRYPT_OID_INFO));

            return pOIDInfo;
        }