Beispiel #1
0
        static bool RunRSAExceptionTest(Session session)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(session);

            rsa.ImportParameters(RsaSignatureTestData.GetKeyParameters());

            byte[] r = new byte[16]; r[0] = 9; // Fake MD5 hash value

            try
            {
                if (rsa.VerifyHash(r, MechanismType.MD5, RsaSignatureTestData.GetSignatureValue()))
                {
                    Log.Comment("Should fail for a faked hash");
                    return(false);
                }
            }
            catch (CryptographicException)
            { }

            try
            {
                rsa.VerifyHash(RsaSignatureTestData.GetHashValue(), (MechanismType)(0xffffffff), RsaSignatureTestData.GetSignatureValue());
                Log.Comment("Shoul throw");
                return(false);
            }
            catch (CryptographicException)
            {
            }

            try
            {
                rsa.VerifyHash(null, MechanismType.SHA_1, null);
                Log.Comment("Shoul throw");
                return(false);
            }
            catch (ArgumentNullException)
            {
            }


            byte[] hash = RsaSignatureTestData.GetHashValue();
            byte[] sig  = RsaSignatureTestData.GetSignatureValue();

            hash[0] = 9;

            if (rsa.VerifyHash(hash, MechanismType.SHA_1, sig))
            {
                Log.Comment("VerifyHash should return false, because I changed one byte.");
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        static bool RunBasicScenario(Session session)
        {
            try
            {
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(session))
                {
                    Log.Comment("Importing RSA public key");
                    rsa.ImportParameters(RsaSignatureTestData.GetKeyParameters());

                    Log.Comment("RSA key size: " + rsa.KeySize);

                    Log.Comment("Verifying SHA-1 signature");
                    bool verified = rsa.VerifyHash(RsaSignatureTestData.GetHashValue(), MechanismType.SHA_1, RsaSignatureTestData.GetSignatureValue());
                    Log.Comment("Complete verifying SHA-1 signature");

                    if (!verified)
                    {
                        Log.Comment("Signature failed to verify - fail.");
                        return(false);
                    }

#if DEFINED_BASE64TRANSFORM
                    byte[] signature = RsaSignatureTestData.GetSignatureValue();

                    string before = Convert.ToBase64String(signature);
                    byte[] after  = ConvertToByteArray(before);

                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, new FromBase64Transform(), CryptoStreamMode.Write);
                    cs.Write(after, 0, after.Length);
                    cs.Close();

                    if (!Compare(ms.ToArray(), signature))
                    {
                        Log.Comment("Base64 Transform failed.");
                        return(false);
                    }
#endif
                }
            }
            catch (Exception e)
            {
                Log.Comment("Fail - got an exception");
                Log.Exception("", e);
                return(false);
            }
            return(true);
        }