コード例 #1
0
        public void RsaWrapUnwrapKey(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RsaWrapUnwrapKey", theoryData);

            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, false);
                var    wrappedKey      = encryptProvider.WrapKey(theoryData.KeyToWrap);
                var    decryptProvider = new DerivedRsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);

                if (!Utility.AreEqual(unwrappedKey, theoryData.KeyToWrap))
                {
                    context.AddDiff("theoryParams.KeyToWrap != unwrappedKey");
                }

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

            TestUtilities.AssertFailIfErrors(context);
        }
コード例 #2
0
        public void RsaKeyWrapProviderDispose()
        {
            SecurityKey key      = KeyingMaterial.RsaSecurityKey_2048;
            var         provider = new RsaKeyWrapProvider(key, SecurityAlgorithms.RsaPKCS1, false);

            key.CryptoProviderFactory.ReleaseRsaKeyWrapProvider(provider);
        }
コード例 #3
0
 /// <summary>
 /// When finished with a <see cref="RsaKeyWrapProvider"/> call this method for cleanup."/>
 /// </summary>
 /// <param name="provider"><see cref="RsaKeyWrapProvider"/> to be released.</param>
 public virtual void ReleaseRsaKeyWrapProvider(RsaKeyWrapProvider provider)
 {
     if (provider != null)
     {
         provider.Dispose();
     }
 }
        public void Constructors(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.Constructors", theoryData);

            try
            {
                RsaKeyWrapProvider provider = null;
                var keyWrapContext          = Guid.NewGuid().ToString();
                if (theoryData.WillUnwrap)
                {
                    provider = new RsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, theoryData.WillUnwrap)
                    {
                        Context = keyWrapContext
                    };
                    provider.CreateAsymmetricAdapter();

                    if (!provider.Algorithm.Equals(theoryData.UnwrapAlgorithm))
                    {
                        context.AddDiff($"provider.Algorithm != theoryData.UnwrapAlgorithm: {provider.Algorithm} : {theoryData.UnwrapAlgorithm}.");
                    }

                    if (!ReferenceEquals(provider.Key, theoryData.UnwrapKey))
                    {
                        context.AddDiff($"!ReferenceEquals(provider.key, theoryData.UnwrapKey)");
                    }
                }
                else
                {
                    provider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, theoryData.WillUnwrap)
                    {
                        Context = keyWrapContext
                    };

                    provider.WrapKey(Guid.NewGuid().ToByteArray());

                    if (!provider.Algorithm.Equals(theoryData.WrapAlgorithm))
                    {
                        context.AddDiff($"provider.Algorithm != theoryData.WrapAlgorithm: {provider.Algorithm} : {theoryData.WrapAlgorithm}.");
                    }

                    if (!ReferenceEquals(provider.Key, theoryData.WrapKey))
                    {
                        context.AddDiff($"!ReferenceEquals(provider.key, theoryData.WrapKey)");
                    }
                }

                theoryData.ExpectedException.ProcessNoException(context);
                if (!provider.Context.Equals(keyWrapContext))
                {
                    context.AddDiff($"provider.Context != keyWrapContext: {provider.Context} : {keyWrapContext}.");
                }
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
コード例 #5
0
        public void RsaUnwrapParameterCheck(RsaKeyWrapTestParams theoryParams)
        {
            try
            {
                var provider = new RsaKeyWrapProvider(theoryParams.DecryptKey, theoryParams.DecryptAlgorithm, true);
                provider.UnwrapKey(theoryParams.WrappedKey);

                theoryParams.EE.ProcessNoException();
            }
            catch (Exception ex)
            {
                theoryParams.EE.ProcessException(ex);
            }
        }
コード例 #6
0
 public void RsaUnwrapMismatch(RsaKeyWrapTestParams theoryParams)
 {
     try
     {
         var    encryptProvider = new RsaKeyWrapProvider(theoryParams.EncryptKey, theoryParams.EncryptAlgorithm, false);
         byte[] keyToWrap       = Guid.NewGuid().ToByteArray();
         var    wrappedKey      = encryptProvider.WrapKey(keyToWrap);
         var    decryptProvider = new RsaKeyWrapProvider(theoryParams.DecryptKey, theoryParams.DecryptAlgorithm, true);
         byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);
         theoryParams.EE.ProcessNoException();
     }
     catch (Exception ex)
     {
         theoryParams.EE.ProcessException(ex);
     }
 }
コード例 #7
0
        public void RsaWrapUnwrapKey(RsaKeyWrapTestParams theoryParams)
        {
            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryParams.EncryptKey, theoryParams.EncryptAlgorithm, false);
                var    wrappedKey      = encryptProvider.WrapKey(theoryParams.KeyToWrap);
                var    decryptProvider = new DerivedRsaKeyWrapProvider(theoryParams.DecryptKey, theoryParams.DecryptAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);

                Assert.True(Utility.AreEqual(unwrappedKey, theoryParams.KeyToWrap), "theoryParams.KeyToWrap != unwrappedKey");

                theoryParams.EE.ProcessNoException();
            }
            catch (Exception ex)
            {
                theoryParams.EE.ProcessException(ex);
            }
        }
コード例 #8
0
        public void RsaUnwrapParameterCheck(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RsaUnwrapParameterCheck", theoryData);

            try
            {
                var provider = new RsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, true);
                provider.UnwrapKey(theoryData.WrappedKey);

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

            TestUtilities.AssertFailIfErrors(context);
        }
コード例 #9
0
        public void RsaUnwrapMismatch(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RsaUnwrapParameterCheck", theoryData);

            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, false);
                byte[] keyToWrap       = Guid.NewGuid().ToByteArray();
                var    wrappedKey      = encryptProvider.WrapKey(keyToWrap);
                var    decryptProvider = new RsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
コード例 #10
0
        private static void AddUnwrapTamperedTheoryData(
            string testId,
            SecurityKey encrtyptKey,
            SecurityKey decryptKey,
            string algorithm,
            TheoryData <RsaKeyWrapTestParams> theoryData)
        {
            var keyToWrap  = Guid.NewGuid().ToByteArray();
            var provider   = new RsaKeyWrapProvider(encrtyptKey, algorithm, false);
            var wrappedKey = provider.WrapKey(keyToWrap);

            TestUtilities.XORBytes(wrappedKey);
            theoryData.Add(new RsaKeyWrapTestParams
            {
                DecryptAlgorithm = algorithm,
                DecryptKey       = decryptKey,
                EE         = ExpectedException.KeyWrapException("IDX10659:"),
                Provider   = provider,
                WrappedKey = wrappedKey
            });
        }
#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant
        public void Constructors(string testId, SecurityKey key, string algorithm, bool isDecrypt, ExpectedException ee)
        {
            try
            {
                var context  = Guid.NewGuid().ToString();
                var provider = new RsaKeyWrapProvider(key, algorithm, isDecrypt)
                {
                    Context = context
                };

                ee.ProcessNoException();

                Assert.Equal(provider.Algorithm, algorithm);
                Assert.Equal(provider.Context, context);
                Assert.True(ReferenceEquals(provider.Key, key));
            }
            catch (Exception ex)
            {
                ee.ProcessException(ex);
            }
        }