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);
        }
Пример #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);
     }
 }
Пример #3
0
        internal static byte[] GetKeyProperty(SafeCspKeyHandle keyHandle, KeyProperty property)
        {
            Contract.Assert(keyHandle != null && !keyHandle.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.CryptGetKeyParam(keyHandle, 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.CryptGetKeyParam(keyHandle, property, buffer, ref bufferSize, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return(buffer);
        }
Пример #4
0
 internal static int GetKeyPropertyInt32(SafeCspKeyHandle keyHandle, CapiNative.KeyProperty property)
 {
     byte[] keyProperty = CapiNative.GetKeyProperty(keyHandle, property);
     if (keyProperty.Length != 4)
     {
         return(0);
     }
     return(BitConverter.ToInt32(keyProperty, 0));
 }
Пример #5
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);
                }
            }
        }
Пример #6
0
        internal static byte[] GetKeyProperty(SafeCspKeyHandle keyHandle, CapiNative.KeyProperty property)
        {
            int pdwDataLen = 0;

            byte[] pbData1 = (byte[])null;
            if (!CapiNative.UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, pbData1, out pdwDataLen, 0))
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != 234)
                {
                    throw new CryptographicException(lastWin32Error);
                }
            }
            byte[] pbData2 = new byte[pdwDataLen];
            if (!CapiNative.UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, pbData2, out pdwDataLen, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(pbData2);
        }
        internal static byte[] GetKeyProperty(SafeCspKeyHandle keyHandle, CapiNative.KeyProperty property)
        {
            int num = 0;

            byte[] array = null;
            if (!CapiNative.UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, array, ref num, 0))
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != 234)
                {
                    throw new CryptographicException(lastWin32Error);
                }
            }
            array = new byte[num];
            if (!CapiNative.UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, array, ref num, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(array);
        }
Пример #8
0
 internal static int GetKeyPropertyInt32(SafeCspKeyHandle keyHandle, KeyProperty property) {
     byte[] rawProperty = GetKeyProperty(keyHandle, property);
     Contract.Assert(rawProperty.Length == sizeof(int) || rawProperty.Length == 0, "Unexpected property size");
     return rawProperty.Length == sizeof(int) ? BitConverter.ToInt32(rawProperty, 0) : 0;
 }
Пример #9
0
 internal static int GetKeyPropertyInt32(SafeCspKeyHandle keyHandle, KeyProperty property)
 {
     byte[] rawProperty = GetKeyProperty(keyHandle, property);
     Contract.Assert(rawProperty.Length == sizeof(int) || rawProperty.Length == 0, "Unexpected property size");
     return(rawProperty.Length == sizeof(int) ? BitConverter.ToInt32(rawProperty, 0) : 0);
 }
 protected override bool ReleaseHandle()
 {
     return(SafeCspKeyHandle.CryptDestroyKey(this.handle));
 }
Пример #11
0
 internal static extern bool CryptImportKey(SafeCspHandle hProv,
                                            [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbData,
                                            int pdwDataLen,
                                            IntPtr hPubKey,                      // SafeCspKeyHandle
                                            KeyGenerationFlags dwFlags,
                                            [Out] out SafeCspKeyHandle phKey);
Пример #12
0
 internal static extern bool CryptVerifySignature(SafeCspHashHandle hHash,
                                                  [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature,
                                                  int dwSigLen,
                                                  SafeCspKeyHandle hPubKey,
                                                  string sDescription,
                                                  int dwFlags);
Пример #13
0
 internal static extern bool CryptGenKey(SafeCspHandle hProv,
                                         int Algid,
                                         uint dwFlags,
                                         [Out] out SafeCspKeyHandle phKey);
Пример #14
0
 internal static extern bool CryptGetKeyParam(SafeCspKeyHandle hKey,
                                              KeyProperty dwParam,
                                              [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbData,
                                              [In, Out] ref int pdwDataLen,
                                              int dwFlags);
Пример #15
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;
                }
            }
        }
Пример #16
0
        internal static byte[] GetKeyProperty(SafeCspKeyHandle keyHandle, KeyProperty property) {
            Contract.Assert(keyHandle != null && !keyHandle.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.CryptGetKeyParam(keyHandle, 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.CryptGetKeyParam(keyHandle, property, buffer, ref bufferSize, 0)) {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return buffer;
        }
Пример #17
0
 internal static extern bool CryptImportKey(SafeCspHandle hProv, [MarshalAs(UnmanagedType.LPArray), In] byte[] pbData, int pdwDataLen, IntPtr hPubKey, CapiNative.KeyGenerationFlags dwFlags, out SafeCspKeyHandle phKey);
Пример #18
0
 internal static extern bool CryptVerifySignature(SafeCspHashHandle hHash,
                                                  [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature,
                                                  int dwSigLen,
                                                  SafeCspKeyHandle hPubKey,
                                                  string sDescription,
                                                  int dwFlags);
Пример #19
0
 internal static extern bool CryptGetKeyParam(SafeCspKeyHandle hKey,
                                              KeyProperty dwParam,
                                              [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbData,
                                              [In, Out] ref int pdwDataLen,
                                              int dwFlags);