Exemple #1
0
        internal static byte[] GetHashProperty(SafeCspHashHandle hashHandle, HashProperty property)
        {
            Contract.Assert(hashHandle != null && !hashHandle.IsInvalid, "keyHandle != null && !keyHandle.IsInvalid");

            int bufferSize = 0;

            byte[] buffer = null;

            // Figure out how big of a buffer we need to hold the property
            if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, buffer, ref bufferSize, 0))
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != (int)ErrorCode.MoreData)
                {
                    throw new CryptographicException(errorCode);
                }
            }

            // Now get the property bytes directly
            buffer = new byte[bufferSize];
            if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, buffer, ref bufferSize, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return(buffer);
        }
Exemple #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);
     }
 }
Exemple #3
0
 internal static void SetHashProperty(SafeCspHashHandle hashHandle, CapiNative.HashProperty property, byte[] value)
 {
     if (!CapiNative.UnsafeNativeMethods.CryptSetHashParam(hashHandle, property, value, 0))
     {
         throw new CryptographicException(Marshal.GetLastWin32Error());
     }
 }
        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);
        }
Exemple #5
0
 internal static int GetHashPropertyInt32(SafeCspHashHandle hashHandle, CapiNative.HashProperty property)
 {
     byte[] hashProperty = CapiNative.GetHashProperty(hashHandle, property);
     if (hashProperty.Length != 4)
     {
         return(0);
     }
     return(BitConverter.ToInt32(hashProperty, 0));
 }
Exemple #6
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);
        }
Exemple #7
0
        internal static void SetHashProperty(SafeCspHashHandle hashHandle,
                                             HashProperty property,
                                             byte[] value)
        {
            Contract.Assert(hashHandle != null && !hashHandle.IsInvalid, "hashHandle != null && !hashHandle.IsInvalid");

            if (!UnsafeNativeMethods.CryptSetHashParam(hashHandle, property, value, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
        }
Exemple #8
0
        internal static bool VerifySignature(SafeCspHandle cspHandle,
                                             SafeCspKeyHandle keyHandle,
                                             AlgorithmID signatureAlgorithm,
                                             AlgorithmID hashAlgorithm,
                                             byte[] hashValue,
                                             byte[] signature)
        {
            Contract.Assert(cspHandle != null && !cspHandle.IsInvalid, "cspHandle != null && !cspHandle.IsInvalid");
            Contract.Assert(keyHandle != null && !keyHandle.IsInvalid, "keyHandle != null && !keyHandle.IsInvalid");
            Contract.Assert(((AlgorithmClass)signatureAlgorithm & AlgorithmClass.Signature) == AlgorithmClass.Signature, "Invalid signature algorithm");
            Contract.Assert(((AlgorithmClass)hashAlgorithm & AlgorithmClass.Hash) == AlgorithmClass.Hash, "Invalid hash algorithm");
            Contract.Assert(hashValue != null, "hashValue != null");
            Contract.Assert(signature != null, "signature != null");

            // CAPI and the CLR have inverse byte orders for signatures, so we need to reverse before verifying
            byte[] signatureValue = new byte[signature.Length];
            Array.Copy(signature, signatureValue, signatureValue.Length);
            Array.Reverse(signatureValue);

            using (SafeCspHashHandle hashHandle = CreateHashAlgorithm(cspHandle, hashAlgorithm)) {
                // Make sure the hash value is the correct size and import it into the CSP
                if (hashValue.Length != GetHashPropertyInt32(hashHandle, HashProperty.HashSize))
                {
                    throw new CryptographicException((int)ErrorCode.BadHash);
                }
                SetHashProperty(hashHandle, HashProperty.HashValue, hashValue);

                // Do the signature verification.  A TRUE result means that the signature was valid.  A FALSE
                // result either means an invalid signature or some other error, so we need to check the last
                // error to see which occured.
                if (UnsafeNativeMethods.CryptVerifySignature(hashHandle,
                                                             signatureValue,
                                                             signatureValue.Length,
                                                             keyHandle,
                                                             null,
                                                             0))
                {
                    return(true);
                }
                else
                {
                    int error = Marshal.GetLastWin32Error();

                    if (error != (int)ErrorCode.BadSignature)
                    {
                        throw new CryptographicException(error);
                    }

                    return(false);
                }
            }
        }
Exemple #9
0
        internal static SafeCspHashHandle CreateHashAlgorithm(SafeCspHandle cspHandle, AlgorithmID algorithm)
        {
            Contract.Assert(cspHandle != null && !cspHandle.IsInvalid, "cspHandle != null && !cspHandle.IsInvalid");
            Contract.Assert(((AlgorithmClass)algorithm & AlgorithmClass.Hash) == AlgorithmClass.Hash, "Invalid hash algorithm");

            SafeCspHashHandle hashHandle = null;

            if (!UnsafeNativeMethods.CryptCreateHash(cspHandle, algorithm, IntPtr.Zero, 0, out hashHandle))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return(hashHandle);
        }
        internal static byte[] GetHashProperty(SafeCspHashHandle hashHandle, CapiNative.HashProperty property)
        {
            int num = 0;

            byte[] array = null;
            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, array, ref num, 0))
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != 234)
                {
                    throw new CryptographicException(lastWin32Error);
                }
            }
            array = new byte[num];
            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, array, ref num, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(array);
        }
Exemple #11
0
        internal static byte[] GetHashProperty(SafeCspHashHandle hashHandle, CapiNative.HashProperty property)
        {
            int pdwDataLen = 0;

            byte[] pbData1 = (byte[])null;
            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, pbData1, out pdwDataLen, 0))
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != 234)
                {
                    throw new CryptographicException(lastWin32Error);
                }
            }
            byte[] pbData2 = new byte[pdwDataLen];
            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, pbData2, out pdwDataLen, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(pbData2);
        }
 internal static extern bool CryptVerifySignature(SafeCspHashHandle hHash,
                                                  [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature,
                                                  int dwSigLen,
                                                  SafeCspKeyHandle hPubKey,
                                                  string sDescription,
                                                  int dwFlags);
Exemple #13
0
 internal static extern bool CryptCreateHash(SafeCspHandle hProv, CapiNative.AlgorithmID Algid, IntPtr hKey, int dwFlags, out SafeCspHashHandle phHash);
Exemple #14
0
 internal static int GetHashPropertyInt32(SafeCspHashHandle hashHandle, HashProperty property)
 {
     byte[] rawProperty = GetHashProperty(hashHandle, property);
     Contract.Assert(rawProperty.Length == sizeof(int) || rawProperty.Length == 0, "Unexpected property size");
     return(rawProperty.Length == sizeof(int) ? BitConverter.ToInt32(rawProperty, 0) : 0);
 }
Exemple #15
0
 internal static extern bool CryptVerifySignature(SafeCspHashHandle hHash,
                                                  [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature,
                                                  int dwSigLen,
                                                  SafeCspKeyHandle hPubKey,
                                                  string sDescription,
                                                  int dwFlags);
Exemple #16
0
 internal static extern bool CryptSetHashParam(SafeCspHashHandle hHash,
                                               HashProperty dwParam,
                                               [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbData,
                                               int dwFlags);
Exemple #17
0
 internal static extern bool CryptCreateHash(SafeCspHandle hProv,
                                             AlgorithmID Algid,
                                             IntPtr hKey,                        // SafeCspKeyHandle
                                             int dwFlags,
                                             [Out] out SafeCspHashHandle phHash);
        internal static void SetHashProperty(SafeCspHashHandle hashHandle,
                                             HashProperty property,
                                             byte[] value) {
            Contract.Assert(hashHandle != null && !hashHandle.IsInvalid, "hashHandle != null && !hashHandle.IsInvalid");

            if (!UnsafeNativeMethods.CryptSetHashParam(hashHandle, property, value, 0)) {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
        }
        internal static byte[] GetHashProperty(SafeCspHashHandle hashHandle, HashProperty property) {
            Contract.Assert(hashHandle != null && !hashHandle.IsInvalid, "keyHandle != null && !keyHandle.IsInvalid");

            int bufferSize = 0;
            byte[] buffer = null;

            // Figure out how big of a buffer we need to hold the property
            if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, buffer, ref bufferSize, 0)) {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != (int)ErrorCode.MoreData) {
                    throw new CryptographicException(errorCode);
                }
            }

            // Now get the property bytes directly
            buffer = new byte[bufferSize];
            if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, buffer, ref bufferSize, 0)) {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return buffer;
        }
 internal static extern bool CryptSetHashParam(SafeCspHashHandle hHash,
                                               HashProperty dwParam,
                                               [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbData,
                                               int dwFlags);
Exemple #21
0
 internal static extern bool CryptGetHashParam(SafeCspHashHandle hHash, CapiNative.HashProperty dwParam, [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbData, [In, Out] ref int pdwDataLen, int dwFlags);
Exemple #22
0
 protected override bool ReleaseHandle()
 {
     return(SafeCspHashHandle.CryptDestroyHash(this.handle));
 }
 internal static int GetHashPropertyInt32(SafeCspHashHandle hashHandle, HashProperty property) {
     byte[] rawProperty = GetHashProperty(hashHandle, property);
     Contract.Assert(rawProperty.Length == sizeof(int) || rawProperty.Length == 0, "Unexpected property size");
     return rawProperty.Length == sizeof(int) ? BitConverter.ToInt32(rawProperty, 0) : 0;
 }