Beispiel #1
0
        internal static SafeCspHashHandle CreateHashAlgorithm(SafeCspHandle cspHandle, CapiNative.AlgorithmID algorithm)
        {
            SafeCspHashHandle phHash = (SafeCspHashHandle)null;

            if (!CapiNative.UnsafeNativeMethods.CryptCreateHash(cspHandle, algorithm, IntPtr.Zero, 0, out phHash))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(phHash);
        }
Beispiel #2
0
 internal static bool VerifySignature(SafeCspHandle cspHandle, SafeCspKeyHandle keyHandle, CapiNative.AlgorithmID signatureAlgorithm, CapiNative.AlgorithmID hashAlgorithm, byte[] hashValue, byte[] signature)
 {
     byte[] numArray = new byte[signature.Length];
     Array.Copy((Array)signature, (Array)numArray, numArray.Length);
     Array.Reverse((Array)numArray);
     using (SafeCspHashHandle hashAlgorithm1 = CapiNative.CreateHashAlgorithm(cspHandle, hashAlgorithm))
     {
         if (hashValue.Length != CapiNative.GetHashPropertyInt32(hashAlgorithm1, CapiNative.HashProperty.HashSize))
         {
             throw new CryptographicException(-2146893822);
         }
         CapiNative.SetHashProperty(hashAlgorithm1, CapiNative.HashProperty.HashValue, hashValue);
         SafeCspHashHandle hHash       = hashAlgorithm1;
         byte[]            pbSignature = numArray;
         int length = pbSignature.Length;
         SafeCspKeyHandle hPubKey = keyHandle;
         // ISSUE: variable of the null type
         __Null local   = null;
         int    dwFlags = 0;
         if (CapiNative.UnsafeNativeMethods.CryptVerifySignature(hHash, pbSignature, length, hPubKey, (string)local, dwFlags))
         {
             return(true);
         }
         int lastWin32Error = Marshal.GetLastWin32Error();
         if (lastWin32Error != -2146893818)
         {
             throw new CryptographicException(lastWin32Error);
         }
         return(false);
     }
 }
Beispiel #3
0
 internal static extern bool CryptCreateHash(SafeCspHandle hProv, CapiNative.AlgorithmID Algid, IntPtr hKey, int dwFlags, out SafeCspHashHandle phHash);
        internal static bool VerifySignature(SafeCspHandle cspHandle, SafeCspKeyHandle keyHandle, CapiNative.AlgorithmID signatureAlgorithm, CapiNative.AlgorithmID hashAlgorithm, byte[] hashValue, byte[] signature)
        {
            byte[] array = new byte[signature.Length];
            Array.Copy(signature, array, array.Length);
            Array.Reverse(array);
            bool result;

            using (SafeCspHashHandle safeCspHashHandle = CapiNative.CreateHashAlgorithm(cspHandle, hashAlgorithm))
            {
                if (hashValue.Length != CapiNative.GetHashPropertyInt32(safeCspHashHandle, CapiNative.HashProperty.HashSize))
                {
                    throw new CryptographicException(-2146893822);
                }
                CapiNative.SetHashProperty(safeCspHashHandle, CapiNative.HashProperty.HashValue, hashValue);
                if (CapiNative.UnsafeNativeMethods.CryptVerifySignature(safeCspHashHandle, array, array.Length, keyHandle, null, 0))
                {
                    result = true;
                }
                else
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error != -2146893818)
                    {
                        throw new CryptographicException(lastWin32Error);
                    }
                    result = false;
                }
            }
            return(result);
        }