public void JwtSecurityToken_Constructor()
        {
            Console.WriteLine(string.Format("Entering: '{0}'", MethodBase.GetCurrentMethod()));
            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name              = "ClaimsSet with all Reserved claim types, ensures that users can add as they see fit",
                Claims            = ClaimSets.AllReserved,
                ExpectedException = ExpectedException.NoExceptionExpected,
            });

            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name               = "All null params",
                Issuer             = null,
                Audience           = null,
                Claims             = null,
                SigningCredentials = null,
                ExpectedException  = ExpectedException.NoExceptionExpected,
            });

            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name              = "NotBefore > Expires, .Net datetime",
                NotBefore         = DateTime.UtcNow + TimeSpan.FromHours(1),
                Expires           = DateTime.UtcNow,
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10401"),
            });

            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name              = "NotBefore > Expires, UnixEpoch - 1 ms",
                NotBefore         = DateTime.UtcNow,
                Expires           = EpochTime.UnixEpoch - TimeSpan.FromMilliseconds(1),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10401"),
            });

            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name              = "NotBefore > Expires, UnixEpoch - 1 s",
                NotBefore         = DateTime.UtcNow,
                Expires           = EpochTime.UnixEpoch - TimeSpan.FromSeconds(1),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10401"),
            });

            RunConstructionTest(
                new JwtSecurityTokenTestVariation
            {
                Name      = "NotBefore == DateItime.MinValue",
                NotBefore = DateTime.MinValue,
                Expires   = DateTime.UtcNow,
            });
        }
コード例 #2
0
        public void AsymmetricSignatureProvider_Publics()
        {
            AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey, KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SignatureAlgorithm);

            SignatureProvider_SignVariation(provider, null, null, ExpectedException.ArgumentNullException());
            SignatureProvider_SignVariation(provider, new byte[0], null, ExpectedException.ArgumentException("IDX10624:"));
            SignatureProvider_SignVariation(provider, new byte[1], null, ExpectedException.InvalidOperationException("IDX10620:"));

            SignatureProvider_VerifyVariation(provider, null, null, ExpectedException.ArgumentNullException());
            SignatureProvider_VerifyVariation(provider, new byte[1], null, ExpectedException.ArgumentNullException());
            SignatureProvider_VerifyVariation(provider, new byte[0], new byte[1], ExpectedException.ArgumentException("IDX10625:"));
            SignatureProvider_VerifyVariation(provider, new byte[1], new byte[0], ExpectedException.ArgumentException("IDX10626:"));
        }
コード例 #3
0
        public void SymmetricSignatureProvider_Publics()
        {
            SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2.SigningKey as SymmetricSecurityKey, KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2.SignatureAlgorithm);

            SignatureProvider_SignVariation(provider, null, null, ExpectedException.ArgumentNullException());
            SignatureProvider_SignVariation(provider, new byte[0], null, ExpectedException.ArgumentException("IDX10624:"));
            SignatureProvider_SignVariation(provider, new byte[1], null, ExpectedException.NoExceptionExpected);

            SignatureProvider_VerifyVariation(provider, null, null, ExpectedException.ArgumentNullException());
            SignatureProvider_VerifyVariation(provider, new byte[0], null, ExpectedException.ArgumentNullException());
            SignatureProvider_VerifyVariation(provider, new byte[0], new byte[0], ExpectedException.ArgumentException("IDX10625:"));
            SignatureProvider_VerifyVariation(provider, new byte[1], new byte[0], ExpectedException.ArgumentException("IDX10626:"));
            SignatureProvider_VerifyVariation(provider, new byte[1], new byte[1], ExpectedException.NoExceptionExpected);

            provider.Dispose();
            SignatureProvider_SignVariation(provider, new byte[1], new byte[1], ExpectedException.ObjectDisposedException);
            SignatureProvider_VerifyVariation(provider, new byte[1], new byte[1], ExpectedException.ObjectDisposedException);
        }
        public void JwtSecurityToken_EncodedStringConstruction()
        {
            Console.WriteLine("Entering: " + MethodBase.GetCurrentMethod());
            string[] tokenParts = EncodedJwts.Asymmetric_LocalSts.Split('.');

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: OverClaims",
                EncodedString     = EncodedJwts.OverClaims,
                ExpectedException = ExpectedException.NoExceptionExpected,
            });


            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: InvalidPayloadFormat",
                EncodedString     = EncodedJwts.InvalidPayload,
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(FormatException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: null",
                EncodedString     = null,
                ExpectedException = ExpectedException.ArgumentNullException(),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: string.Empty",
                EncodedString     = string.Empty,
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10002:"),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: single character: '1'",
                EncodedString     = "1",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: two parts each a single character: '1.2'",
                EncodedString     = "1.2",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: header is not encoded properly: '123'",
                EncodedString     = string.Format("{0}.{1}.{2}", "123", tokenParts[1], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(ArgumentException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: header is not encoded properly: '123=='",
                EncodedString     = string.Format("{0}.{1}.{2}", "123==", tokenParts[1], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: payload is not encoded correctly: '123'",
                EncodedString     = string.Format("{1}.{0}.{2}", "123", tokenParts[0], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(ArgumentException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: payload is not encoded properly: '123=='",
                EncodedString     = string.Format("{1}.{0}.{2}", "123==", tokenParts[0], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: valid encoding, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = string.Format("{0}.{1}.", tokenParts[0], tokenParts[1]),
                ExpectedException = ExpectedException.NoExceptionExpected,
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: valid encoding, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = string.Format("{0}.{1}.{2}.{3}", tokenParts[0], tokenParts[1], tokenParts[2], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });
        }
        public void JwtSecurityToken_EncodedStringConstruction()
        {
            Console.WriteLine("Entering: " + MethodBase.GetCurrentMethod());
            string[] tokenParts = EncodedJwts.Asymmetric_LocalSts.Split('.');

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: OverClaims",
                EncodedString     = EncodedJwts.OverClaims,
                ExpectedException = ExpectedException.NoExceptionExpected,
            });


            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: InvalidPayloadFormat",
                EncodedString     = EncodedJwts.InvalidPayload,
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(FormatException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: null",
                EncodedString     = null,
                ExpectedException = ExpectedException.ArgumentNullException(),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: string.Empty",
                EncodedString     = string.Empty,
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10002:"),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: single character: '1'",
                EncodedString     = "1",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });
            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: two parts each a single character: '1.2'",
                EncodedString     = "1.2",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: header is not encoded properly: '123'",
                EncodedString     = string.Format("{0}.{1}.{2}", "123", tokenParts[1], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(ArgumentException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: header is not encoded properly: '123=='",
                EncodedString     = string.Format("{0}.{1}.{2}", "123==", tokenParts[1], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: payload is not encoded correctly: '123'",
                EncodedString     = string.Format("{1}.{0}.{2}", "123", tokenParts[0], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(ArgumentException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: payload is not encoded properly: '123=='",
                EncodedString     = string.Format("{1}.{0}.{2}", "123==", tokenParts[0], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: valid encoding, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = string.Format("{0}.{1}.", tokenParts[0], tokenParts[1]),
                ExpectedException = ExpectedException.NoExceptionExpected,
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: invalid for header, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = "iJsL8.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8yZWU5NDA1Mi1iZjM0LTRlODMtYWE2Zi1mODIxMjVjNjVkNzQvIiwiaWF0IjoxNDI2Nzk2MDA1LCJuYmYiOjE0MjY3OTYwMDUsImV4cCI6MTQyNjc5OTkwNSwidmVyIjoiMS4wIiwidGlkIjoiMmVlOTQwNTItYmYzNC00ZTgzLWFhNmYtZjgyMTI1YzY1ZDc0IiwiYW1yIjpbInB3ZCJdLCJvaWQiOiJlMWUxZTY0Ny01ZTJhLTQyMGYtYjY3Ny1lYWU3MWMxNjQ4ZWIiLCJ1cG4iOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwicHVpZCI6IjEwMDMzRkZGOENEQzE0OUEiLCJzdWIiOiJWUjgzRllKTDhXR0o1MmVRMk8yMG81N0ZwNGR0OW5uUFVRT0ZtM3FWenpzIiwiZ2l2ZW5fbmFtZSI6IkRhdmlkIiwiZmFtaWx5X25hbWUiOiJNdXJyYXkiLCJuYW1lIjoiZGF2aWRtdSIsImdyb3VwcyI6WyI0NjVmYjhhNS03Njk5LTQyMDQtYjNlNy0zNzBlYjFkNDhjYjkiXSwidW5pcXVlX25hbWUiOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwiYXBwaWQiOiIxOTUwYTI1OC0yMjdiLTRlMzEtYTljZi03MTc0OTU5NDVmYzIiLCJhcHBpZGFjciI6IjAiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJhY3IiOiIxIn0.iJsL8",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(System.FormatException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: invalid for payload, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = tokenParts[0] + ".eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8yZWU5NDA1Mi1iZjM0LTRlODMtYWE2Zi1mODIxMjVjNjVkNzQvIiwiaWF0IjoxNDI2Nzk2MDA1LCJuYmYiOjE0MjY3OTYwMDUsImV4cCI6MTQyNjc5OTkwNSwidmVyIjoiMS4wIiwidGlkIjoiMmVlOTQwNTItYmYzNC00ZTgzLWFhNmYtZjgyMTI1YzY1ZDc0IiwiYW1yIjpbInB3ZCJdLCJvaWQiOiJlMWUxZTY0Ny01ZTJhLTQyMGYtYjY3Ny1lYWU3MWMxNjQ4ZWIiLCJ1cG4iOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwicHVpZCI6IjEwMDMzRkZGOENEQzE0OUEiLCJzdWIiOiJWUjgzRllKTDhXR0o1MmVRMk8yMG81N0ZwNGR0OW5uUFVRT0ZtM3FWenpzIiwiZ2l2ZW5fbmFtZSI6IkRhdmlkIiwiZmFtaWx5X25hbWUiOiJNdXJyYXkiLCJuYW1lIjoiZGF2aWRtdSIsImdyb3VwcyI6WyI0NjVmYjhhNS03Njk5LTQyMDQtYjNlNy0zNzBlYjFkNDhjYjkiXSwidW5pcXVlX25hbWUiOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwiYXBwaWQiOiIxOTUwYTI1OC0yMjdiLTRlMzEtYTljZi03MTc0OTU5NDVmYzIiLCJhcHBpZGFjciI6IjAiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJhY3IiOiIxIn0.iJsL8",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(System.FormatException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: valid encoding, NO signature (JWT_AsymmetricSigned_AcsV2)",
                EncodedString     = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8yZWU5NDA1Mi1iZjM0LTRlODMtYWE2Zi1mODIxMjVjNjVkNzQvIiwiaWF0IjoxNDI2Nzk2MDA1LCJuYmYiOjE0MjY3OTYwMDUsImV4cCI6MTQyNjc5OTkwNSwidmVyIjoiMS4wIiwidGlkIjoiMmVlOTQwNTItYmYzNC00ZTgzLWFhNmYtZjgyMTI1YzY1ZDc0IiwiYW1yIjpbInB3ZCJdLCJvaWQiOiJlMWUxZTY0Ny01ZTJhLTQyMGYtYjY3Ny1lYWU3MWMxNjQ4ZWIiLCJ1cG4iOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwicHVpZCI6IjEwMDMzRkZGOENEQzE0OUEiLCJzdWIiOiJWUjgzRllKTDhXR0o1MmVRMk8yMG81N0ZwNGR0OW5uUFVRT0ZtM3FWenpzIiwiZ2l2ZW5fbmFtZSI6IkRhdmlkIiwiZmFtaWx5X25hbWUiOiJNdXJyYXkiLCJuYW1lIjoiZGF2aWRtdSIsImdyb3VwcyI6WyI0NjVmYjhhNS03Njk5LTQyMDQtYjNlNy0zNzBlYjFkNDhjYjkiXSwidW5pcXVlX25hbWUiOiJkYXZpZEBzb29jaGkub25taWNyb3NvZnQuY29tIiwiYXBwaWQiOiIxOTUwYTI1OC0yMjdiLTRlMzEtYTljZi03MTc0OTU5NDVmYzIiLCJhcHBpZGFjciI6IjAiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJhY3IiOiIxIn0.iJsL8",
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10703:", inner: typeof(System.FormatException)),
            });

            RunEncodedTest(new JwtSecurityTokenTestVariation
            {
                Name              = "EncodedString: valid encoding, invalid encoding on signature",
                EncodedString     = string.Format("{0}.{1}.{2}.{3}", tokenParts[0], tokenParts[1], tokenParts[2], tokenParts[2]),
                ExpectedException = ExpectedException.ArgumentException(substringExpected: "IDX10709:"),
            });
        }
コード例 #6
0
        public void SignatureProviderFactory_Tests()
        {
            SignatureProviderFactory factory = new SignatureProviderFactory();

            // Asymmetric / Symmetric both need signature alg specified
            FactoryCreateFor("Siging:    - algorithm string.Empty", KeyingMaterial.AsymmetricKey_1024, string.Empty, factory, ExpectedException.ArgumentException());
            FactoryCreateFor("Verifying: - algorithm string.Empty", KeyingMaterial.AsymmetricKey_1024, string.Empty, factory, ExpectedException.ArgumentException());

            // Keytype not supported
            FactoryCreateFor("Siging:    - SecurityKey type not Asymmetric or Symmetric", NotAsymmetricOrSymmetricSecurityKey.New, SecurityAlgorithms.HmacSha256Signature, factory, ExpectedException.ArgumentException("IDX10600:"));
            FactoryCreateFor("Verifying: - SecurityKey type not Asymmetric or Symmetric", NotAsymmetricOrSymmetricSecurityKey.New, SecurityAlgorithms.RsaSha256Signature, factory, ExpectedException.ArgumentException("IDX10600:"));

            // Private keys missing
            FactoryCreateFor("Siging:    - SecurityKey without private key", KeyingMaterial.DefaultAsymmetricKey_Public_2048, SecurityAlgorithms.RsaSha256Signature, factory, ExpectedException.InvalidOperationException(substringExpected: "IDX10614:", inner: typeof(NotSupportedException)));
            FactoryCreateFor("Verifying: - SecurityKey without private key", KeyingMaterial.DefaultAsymmetricKey_Public_2048, SecurityAlgorithms.RsaSha256Signature, factory, ExpectedException.NoExceptionExpected);

            // Key size checks
            FactoryCreateFor("Siging:    - AsymmetricKeySize Key to small", KeyingMaterial.AsymmetricKey_1024, SecurityAlgorithms.RsaSha256Signature, factory, ExpectedException.ArgumentOutOfRangeException("IDX10630:"));

            SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying = 2048;
            FactoryCreateFor("Verifying: - AsymmetricKeySize Key to small", KeyingMaterial.AsymmetricKey_1024, SecurityAlgorithms.RsaSha256Signature, factory, ExpectedException.ArgumentOutOfRangeException("IDX10631:"));
            SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying = SignatureProviderFactory.AbsoluteMinimumAsymmetricKeySizeInBitsForVerifying;

            SignatureProviderFactory.MinimumSymmetricKeySizeInBits = 512;
            FactoryCreateFor("Siging:    - SymmetricKeySize Key to small", KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature, factory, ExpectedException.ArgumentOutOfRangeException("IDX10603:"));
            FactoryCreateFor("Verifying: - SymmetricKeySize Key to small", KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature, factory, ExpectedException.ArgumentOutOfRangeException("IDX10603"));
            SignatureProviderFactory.MinimumSymmetricKeySizeInBits = SignatureProviderFactory.AbsoluteMinimumSymmetricKeySizeInBits;

            ExpectedException expectedException = ExpectedException.ArgumentOutOfRangeException("IDX10613:");

            // setting keys too small
            try
            {
                Console.WriteLine(string.Format("Testcase: '{0}'", "SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning < AbsoluteMinimumAsymmetricKeySizeInBitsForSigning"));
                SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning = SignatureProviderFactory.AbsoluteMinimumAsymmetricKeySizeInBitsForSigning - 10;
                expectedException.ProcessNoException();
                SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForSigning = SignatureProviderFactory.AbsoluteMinimumAsymmetricKeySizeInBitsForSigning;
            }
            catch (Exception ex)
            {
                expectedException.ProcessException(ex);
            }

            expectedException = ExpectedException.ArgumentOutOfRangeException("IDX10627:");
            try
            {
                Console.WriteLine(string.Format("Testcase: '{0}'", "SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying < AbsoluteMinimumAsymmetricKeySizeInBitsForVerifying"));
                SignatureProviderFactory.MinimumAsymmetricKeySizeInBitsForVerifying = SignatureProviderFactory.AbsoluteMinimumAsymmetricKeySizeInBitsForVerifying - 10;
                expectedException.ProcessNoException();
            }
            catch (Exception ex)
            {
                expectedException.ProcessException(ex);
            }

            expectedException = ExpectedException.ArgumentOutOfRangeException("IDX10628:");
            try
            {
                Console.WriteLine(string.Format("Testcase: '{0}'", "SignatureProviderFactory.MinimumSymmetricKeySizeInBits < AbsoluteMinimumSymmetricKeySizeInBits"));
                SignatureProviderFactory.MinimumSymmetricKeySizeInBits = SignatureProviderFactory.AbsoluteMinimumSymmetricKeySizeInBits - 10;
                expectedException.ProcessNoException();
            }
            catch (Exception ex)
            {
                expectedException.ProcessException(ex);
            }
        }
コード例 #7
0
        public void SymmetricSignatureProvider_ConstructorTests()
        {
            // no errors
            SymmetricSignatureProvider_ConstructorVariation("Creates with no errors", KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature, ExpectedException.NoExceptionExpected);

            // null, empty algorithm digest
            SymmetricSignatureProvider_ConstructorVariation("Constructor:   - NUll key", null, SecurityAlgorithms.HmacSha256Signature, ExpectedException.ArgumentNullException());
            SymmetricSignatureProvider_ConstructorVariation("Constructor:   - algorithm == string.Empty", KeyingMaterial.DefaultSymmetricSecurityKey_256, string.Empty, ExpectedException.ArgumentException());

            // GetKeyedHashAlgorithm throws
            SymmetricSecurityKey key = new FaultingSymmetricSecurityKey(KeyingMaterial.DefaultSymmetricSecurityKey_256, new CryptographicException("hi from inner"));

            SymmetricSignatureProvider_ConstructorVariation("Constructor:   - SecurityKey.GetKeyedHashAlgorithm throws", key, SecurityAlgorithms.HmacSha256Signature, ExpectedException.InvalidOperationException("IDX10632:", typeof(CryptographicException)));

            // Key returns null KeyedHash
            key = new FaultingSymmetricSecurityKey(KeyingMaterial.DefaultSymmetricSecurityKey_256, null);
            SymmetricSignatureProvider_ConstructorVariation("Constructor:   - SecurityKey returns null KeyedHashAlgorithm", key, SecurityAlgorithms.HmacSha256Signature, ExpectedException.InvalidOperationException("IDX10633:"));

            //_keyedHash.Key = _key.GetSymmetricKey() is null;
            KeyedHashAlgorithm keyedHashAlgorithm = KeyingMaterial.DefaultSymmetricSecurityKey_256.GetKeyedHashAlgorithm(SecurityAlgorithms.HmacSha256Signature);

            key = new FaultingSymmetricSecurityKey(KeyingMaterial.DefaultSymmetricSecurityKey_256, null, null, keyedHashAlgorithm, null);
            SymmetricSignatureProvider_ConstructorVariation("Constructor:   - key returns null bytes to pass to _keyedHashKey", key, SecurityAlgorithms.HmacSha256Signature, ExpectedException.InvalidOperationException("IDX10634:", typeof(NullReferenceException)));
        }
コード例 #8
0
        public void AsymmetricSignatureProvider_Constructor()
        {
            AsymmetricSecurityKey privateKey = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey;
            AsymmetricSecurityKey publicKey  = KeyingMaterial.DefaultX509SigningCreds_Public_2048_RsaSha2_Sha2.SigningKey as AsymmetricSecurityKey;
            string sha2SignatureAlgorithm    = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.SignatureAlgorithm;

            // no errors
            AsymmetricConstructorVariation("Signing:  - Creates with no errors", privateKey, sha2SignatureAlgorithm, expectedException: ExpectedException.NoExceptionExpected);
            AsymmetricConstructorVariation("Verifying: - Creates with no errors (Private Key)", privateKey, sha2SignatureAlgorithm, expectedException: ExpectedException.NoExceptionExpected);
            AsymmetricConstructorVariation("Verifying: - Creates with no errors (Public Key)", publicKey, sha2SignatureAlgorithm, expectedException: ExpectedException.NoExceptionExpected);

            // null, empty algorithm digest
            AsymmetricConstructorVariation("Signing:   - NUll key", null, sha2SignatureAlgorithm, expectedException: ExpectedException.ArgumentNullException());
            AsymmetricConstructorVariation("Signing:   - SignatureAlorithm == null", privateKey, null, expectedException: ExpectedException.ArgumentNullException());
            AsymmetricConstructorVariation("Signing:   - SignatureAlorithm == whitespace", privateKey, "    ", expectedException: ExpectedException.ArgumentException("IDX10002"));

            // Private keys missing
            AsymmetricConstructorVariation("Signing:   - SecurityKey without private key", publicKey, sha2SignatureAlgorithm, expectedException: ExpectedException.InvalidOperationException(inner: typeof(NotSupportedException)));
            AsymmetricConstructorVariation("Verifying: - SecurityKey without private key", publicKey, sha2SignatureAlgorithm, expectedException: ExpectedException.NoExceptionExpected);

            // _formatter not created
            AsymmetricConstructorVariation("Signing:   - key cannot create _formatter", KeyingMaterial.DefaultAsymmetricKey_2048, "SecurityAlgorithms.RsaSha256Signature", expectedException: ExpectedException.InvalidOperationException(substringExpected: "IDX10618", inner: typeof(NotSupportedException)));

            // _deformatter not created
            AsymmetricConstructorVariation("Verifying: - key cannot create _deformatter", KeyingMaterial.DefaultAsymmetricKey_Public_2048, "SecurityAlgorithms.RsaSha256Signature", expectedException: ExpectedException.InvalidOperationException(substringExpected: "IDX10618", inner: typeof(NotSupportedException)));

            Console.WriteLine("Test missing: key.GetHashAlgorithmForSignature( signingCredentials.SignatureAlgorithm );"); //TODO: Should this be fixed?
        }