Пример #1
0
        public void ReleaseSignatureProviders(SignatureProviderTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.ReleaseSignatureProviders", theoryData);
            var cryptoProviderFactory = new CryptoProviderFactory();

            try
            {
                if (theoryData.CustomCryptoProvider != null)
                {
                    cryptoProviderFactory.CustomCryptoProvider = theoryData.CustomCryptoProvider;
                }
                cryptoProviderFactory.ReleaseSignatureProvider(theoryData.SigningSignatureProvider);
                if (theoryData.CustomCryptoProvider != null && theoryData.SigningSignatureProvider != null && !((CustomCryptoProvider)theoryData.CustomCryptoProvider).ReleaseCalled)
                {
                    context.Diffs.Add("Release wasn't called on the CustomCryptoProvider.");
                }
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
 public static void AddRsaAlgorithmVariations(SignatureProviderTheoryData theoryData, TheoryData <SignatureProviderTheoryData> variations)
 {
     foreach (var algorithm in RsaSigningAlgorithms)
     {
         variations.Add(new SignatureProviderTheoryData
         {
             SigningAlgorithm = algorithm.Item1,
             SigningKey       = theoryData.SigningKey,
             TestId           = theoryData.TestId + algorithm.Item1 + algorithm.Item2,
             VerifyAlgorithm  = algorithm.Item2,
             VerifyKey        = theoryData.VerifyKey
         });
     }
 }
Пример #3
0
        public void SignVerify(SignatureProviderTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.SignVerify", theoryData);
            var bytes   = Guid.NewGuid().ToByteArray();

            try
            {
                var providerForSigningDirect = new AsymmetricSignatureProvider(theoryData.SigningKey, theoryData.SigningAlgorithm, true);
                providerForSigningDirect.ValidKeySize();

                var providerForVerifyingDirect = new AsymmetricSignatureProvider(theoryData.VerifyKey, theoryData.VerifyAlgorithm, false);
                providerForVerifyingDirect.ValidKeySize();

                var providerForSigningFromFactory   = theoryData.SigningKey.CryptoProviderFactory.CreateForSigning(theoryData.SigningKey, theoryData.SigningAlgorithm);
                var providerForVerifyingFromFactory = theoryData.VerifyKey.CryptoProviderFactory.CreateForVerifying(theoryData.VerifyKey, theoryData.VerifyAlgorithm);

                byte[] signatureDirect      = providerForSigningDirect.Sign(bytes);
                byte[] signatureFromFactory = providerForSigningFromFactory.Sign(bytes);

                if (!providerForVerifyingDirect.Verify(bytes, signatureDirect))
                {
                    context.AddDiff($"providerForVerifyingDirect.Verify (signatureDirect) - FAILED. signingKey : signingAlgorithm '{theoryData.SigningKey}' : '{theoryData.SigningAlgorithm}. verifyKey : verifyAlgorithm '{theoryData.VerifyKey}' : '{theoryData.VerifyAlgorithm}");
                }

                if (!providerForVerifyingDirect.Verify(bytes, signatureFromFactory))
                {
                    context.AddDiff($"providerForVerifyingDirect.Verify (signatureFromFactory) - FAILED. signingKey : signingAlgorithm '{theoryData.SigningKey}' : '{theoryData.SigningAlgorithm}. verifyKey : verifyAlgorithm '{theoryData.VerifyKey}' : '{theoryData.VerifyAlgorithm}");
                }

                if (!providerForVerifyingFromFactory.Verify(bytes, signatureDirect))
                {
                    context.AddDiff($"providerForVerifyingFromFactory.Verify (signatureDirect) - FAILED. signingKey : signingAlgorithm '{theoryData.SigningKey}' : '{theoryData.SigningAlgorithm}. verifyKey : verifyAlgorithm '{theoryData.VerifyKey}' : '{theoryData.VerifyAlgorithm}");
                }

                if (!providerForVerifyingFromFactory.Verify(bytes, signatureFromFactory))
                {
                    context.AddDiff($"providerForVerifyingFromFactory.Verify (signatureFromFactory) - FAILED. signingKey : signingAlgorithm '{theoryData.SigningKey}' : '{theoryData.SigningAlgorithm}. verifyKey : verifyAlgorithm '{theoryData.VerifyKey}' : '{theoryData.VerifyAlgorithm}");
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
        public void SymmetricSignatureProviderConstructorParams(SignatureProviderTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.SymmetricSignatureProviderConstructorParams", theoryData);

            try
            {
                new SymmetricSignatureProvider(theoryData.SigningKey, theoryData.SigningAlgorithm);
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Пример #5
0
        public void ReleaseSignatureProviders(SignatureProviderTheoryData theoryData)
        {
            IdentityModelEventSource.ShowPII = true;
            var context = TestUtilities.WriteHeader($"{this}.ReleaseSignatureProviders", theoryData);
            var cryptoProviderFactory = new CryptoProviderFactory();

            try
            {
                cryptoProviderFactory.ReleaseSignatureProvider(theoryData.SigningSignatureProvider);
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Пример #6
0
        public void CreateAndReleaseSignatureProviders(SignatureProviderTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.CreateAndReleaseSignatureProvidersTheoryData", theoryData);
            var cryptoProviderFactory = new CryptoProviderFactory()
            {
                CacheSignatureProviders = false
            };

            try
            {
                var signatureProvider = cryptoProviderFactory.CreateForSigning(theoryData.SigningKey, theoryData.SigningAlgorithm);
                if (cryptoProviderFactory.CryptoProviderCache.TryGetSignatureProvider(theoryData.SigningKey, theoryData.SigningAlgorithm, theoryData.SignatureProviderType, true, out var _))
                {
                    context.Diffs.Add("A SignatureProvider was added to CryptoProviderFactory.CryptoProviderCache, but CryptoProviderFactory.CacheSignatureProviders is false.");
                }

                cryptoProviderFactory.ReleaseSignatureProvider(signatureProvider);

                // If the signatureProvider is cached Dispose() will not be called on it.
                if (signatureProvider.GetType().Equals(typeof(AsymmetricSignatureProvider)))
                {
                    var disposeCalled = (bool)typeof(AsymmetricSignatureProvider).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance).GetValue((AsymmetricSignatureProvider)signatureProvider);
                    if (!disposeCalled)
                    {
                        context.Diffs.Add("Dispose wasn't called on the AsymmetricSignatureProvider.");
                    }
                }
                else // signatureProvider.GetType().Equals(typeof(SymmetricSignatureProvider))
                {
                    var disposeCalled = (bool)typeof(SymmetricSignatureProvider).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance).GetValue((SymmetricSignatureProvider)signatureProvider);
                    if (!disposeCalled)
                    {
                        context.Diffs.Add("Dispose wasn't called on the SymmetricSignatureProvider.");
                    }
                }
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            theoryData.ExpectedException.ProcessNoException(context);
            TestUtilities.AssertFailIfErrors(context);
        }
Пример #7
0
        public void FaultingSymmetricSignatureProviders(SignatureProviderTheoryData theoryData)
        {
            IdentityModelEventSource.ShowPII = true;
            var context = TestUtilities.WriteHeader($"{this}.FaultingSymmetricSignatureProviders", theoryData);

            try
            {
                var bytes = new byte[256];
                var signingSignatureProvider = theoryData.CryptoProviderFactory.CreateForSigning(theoryData.SigningKey, theoryData.SigningAlgorithm) as SymmetricSignatureProvider;
                var signedBytes = signingSignatureProvider.Sign(bytes);
                var verifyingSignatureProvider = theoryData.CryptoProviderFactory.CreateForVerifying(theoryData.VerifyKey, theoryData.VerifyAlgorithm) as SymmetricSignatureProvider;
                if (theoryData.VerifySpecifyingLength)
                {
                    verifyingSignatureProvider.Verify(bytes, signedBytes);
                }
                else
                {
                    verifyingSignatureProvider.Verify(bytes, signedBytes, bytes.Length - 1);
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            var signProviderFound = theoryData.CryptoProviderFactory.CryptoProviderCache.TryGetSignatureProvider(theoryData.SigningKey, theoryData.SigningAlgorithm, theoryData.SigningSignatureProviderType, true, out SignatureProvider signingProvider);

            if (signProviderFound != theoryData.ShouldFindSignSignatureProvider)
            {
                context.Diffs.Add($"signingProviderFound '{signProviderFound}' != theoryData.ShouldFindSignSignatureProvider: '{theoryData.ShouldFindSignSignatureProvider}'");
            }

            var verifyProviderFound = theoryData.CryptoProviderFactory.CryptoProviderCache.TryGetSignatureProvider(theoryData.VerifyKey, theoryData.VerifyAlgorithm, theoryData.VerifySignatureProviderType, false, out SignatureProvider verifyingProvider);

            if (verifyProviderFound != theoryData.ShouldFindVerifySignatureProvider)
            {
                context.Diffs.Add($"verifySignatureProviderFound '{verifyProviderFound}' != theoryData.ShouldFindVerifySignatureProvider: '{theoryData.ShouldFindVerifySignatureProvider}'");
            }

            TestUtilities.AssertFailIfErrors(context);
        }