/// <summary>
            /// Simulates a GetKeyAsync method of KeyVault SDK.
            /// </summary>
            /// <param name="name"></param>
            /// <param name="version"></param>
            /// <param name="cancellationToken"></param>
            /// <returns></returns>
            public override Task <Response <KeyVaultKey> > GetKeyAsync(string name, string version = null, CancellationToken cancellationToken = default)
            {
                Console.WriteLine("Accessing Key via Test GetKeyAsync");

                // simulate a RequestFailed Exception
                if (name.Contains(KeyVaultTestConstants.ValidateRequestFailedEx))
                {
                    throw new RequestFailedException("Service Unavailable");
                }

                // simulate a case to return a Null Key.
                if (name.Contains(KeyVaultTestConstants.ValidateNullKeyVaultKey))
                {
                    Mock <Response <KeyVaultKey> > mockedResponseNullKeyVault = new Mock <Response <KeyVaultKey> >();
                    mockedResponseNullKeyVault.SetupGet(r => r.Value).Returns((KeyVaultKey)null);
                    return(Task.FromResult(mockedResponseNullKeyVault.Object));
                }

                this.keyinfo.TryGetValue(name, out string recoverlevel);
                KeyProperties tp      = KeyModelFactory.KeyProperties(recoveryLevel: recoverlevel);
                JsonWebKey    jwk     = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: "invalid", keyOps: new[] { KeyOperation.Sign, KeyOperation.Verify });
                KeyVaultKey   mockKey = KeyModelFactory.KeyVaultKey(properties: tp, key: jwk);

                Mock <Response <KeyVaultKey> > mockedResponseKeyVault = new Mock <Response <KeyVaultKey> >();

                mockedResponseKeyVault.SetupGet(r => r.Value).Returns(mockKey);

                return(Task.FromResult(mockedResponseKeyVault.Object));
            }
        public void SupportsOperationUnauthorizedOperation()
        {
            JsonWebKey jwk = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: "invalid", keyOps: new[] { KeyOperation.Verify });

            EcCryptographyProvider client = new EcCryptographyProvider(jwk, null);

            Assert.IsFalse(client.SupportsOperation(KeyOperation.Sign));
        }
Example #3
0
        public void SignReturnsNullOnUnsupported()
        {
            JsonWebKey jwk = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: "invalid", keyOps: new[] { KeyOperation.Sign });

            EcCryptographyProvider client = new EcCryptographyProvider(new KeyVaultKey {
                Key = jwk
            });
            SignResult result = client.Sign(default, new byte[] { 0xff }, default);
        public void SupportsOperationUnsupportedCurve()
        {
            JsonWebKey jwk = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: "invalid", keyOps: new[] { KeyOperation.Sign, KeyOperation.Verify });

            EcCryptographyProvider client = new EcCryptographyProvider(jwk, null);

            // The provider caches the original allow key operations to facilitate tracing. Operation will still be sent to the service.
            Assert.IsTrue(client.SupportsOperation(KeyOperation.Sign));
        }
        public void SupportsOperation(string operationValue, bool supported)
        {
            JsonWebKey jwk = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: KeyCurveName.P256, keyOps: new[] { KeyOperation.Sign, KeyOperation.Verify });

            EcCryptographyProvider client    = new EcCryptographyProvider(jwk, null);
            KeyOperation           operation = new KeyOperation(operationValue);

            Assert.AreEqual(supported, client.SupportsOperation(operation));
        }
Example #6
0
        public void SupportsOperationUnsupportedCurve()
        {
            JsonWebKey jwk = KeyModelFactory.JsonWebKey(KeyType.Ec, curveName: "invalid", keyOps: new[] { KeyOperation.Sign, KeyOperation.Verify });

            EcCryptographyProvider client = new EcCryptographyProvider(new KeyVaultKey {
                Key = jwk
            });

            Assert.IsFalse(client.SupportsOperation(KeyOperation.Sign));
        }