예제 #1
0
        internal static SafeCspHandle AcquireCsp(string keyContainer,
                                                 string providerName,
                                                 ProviderType providerType,
                                                 CryptAcquireContextFlags flags,
                                                 bool throwPlatformException)
        {
            Contract.Ensures(Contract.Result <SafeCspHandle>() != null &&
                             !Contract.Result <SafeCspHandle>().IsInvalid&&
                             !Contract.Result <SafeCspHandle>().IsClosed);

            SafeCspHandle cspHandle = null;

            if (!UnsafeNativeMethods.CryptAcquireContext(out cspHandle,
                                                         keyContainer,
                                                         providerName,
                                                         providerType,
                                                         flags))
            {
                // If the platform doesn't have the specified CSP, we'll either get a ProviderTypeNotDefined
                // or a KeysetNotDefined error depending on the CAPI version.
                int error = Marshal.GetLastWin32Error();

                if (throwPlatformException && (error == (int)CapiNative.ErrorCode.ProviderTypeNotDefined ||
                                               error == (int)CapiNative.ErrorCode.KeysetNotDefined))
                {
                    throw new PlatformNotSupportedException(SR.Cryptography_PlatformNotSupported);
                }
                else
                {
                    throw new CryptographicException(error);
                }
            }

            return(cspHandle);
        }
예제 #2
0
        /*
         * SafeCritical - we're not exposing out anything that we want to prevent untrusted code from getting at
         */
        public CapiHashAlgorithm(
            string provider,
            CapiNative.ProviderType providerType,
            CapiNative.AlgorithmId algorithm)
        {
            Contract.Requires(!string.IsNullOrEmpty(provider));
            Contract.Requires((CapiNative.AlgorithmClass)((uint)algorithm & (uint)CapiNative.AlgorithmClass.Hash) == CapiNative.AlgorithmClass.Hash);

            m_algorithmId = algorithm;
            m_cspHandle   = CapiNative.AcquireCsp(null,
                                                  provider,
                                                  providerType,
                                                  CapiNative.CryptAcquireContextFlags.VerifyContext,
                                                  true);
            m_hashHandle = Initialize();
        }
예제 #3
0
        internal void SetParentCsp(SafeCspHandle parentCsp)
        {
            bool addedRef = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                parentCsp.DangerousAddRef(ref addedRef);
                IntPtr rawParentHandle = parentCsp.DangerousGetHandle();
                ParentCsp = rawParentHandle;
            }
            finally
            {
                if (addedRef)
                {
                    parentCsp.DangerousRelease();
                }
            }
        }
예제 #4
0
 public static extern bool CryptCreateHash(SafeCspHandle hProv,
                                           AlgorithmId Algid,
                                           SafeCapiKeyHandle hKey,
                                           int dwFlags,
                                           [Out] out SafeCapiHashHandle phHash);
예제 #5
0
 public static extern bool CryptAcquireContext([Out] out SafeCspHandle phProv,
                                               string?pszContainer,
                                               string pszProvider,
                                               ProviderType dwProvType,
                                               CryptAcquireContextFlags dwFlags);