Пример #1
0
        public void When_Getting_Rsa_Key_Information_Then_Modulus_And_Exponent_Are_Returned()
        {
            // ARRANGE
            InitializeFakeObjects();
            var serializedRsa = string.Empty;

#if NET461
            using (var provider = new RSACryptoServiceProvider())
            {
                serializedRsa = provider.ToXmlString(true);
            };
#else
            using (var rsa = new RSAOpenSsl())
            {
                serializedRsa = rsa.ToXmlString(true);
            };
#endif
            var jsonWebKey = new JsonWebKey
            {
                Kty           = KeyType.RSA,
                SerializedKey = serializedRsa
            };

            // ACT
            var result = _jsonWebKeyEnricher.GetPublicKeyInformation(jsonWebKey);

            // ASSERT
            Assert.NotNull(result);
            Assert.True(result.ContainsKey(Constants.JsonWebKeyParameterNames.RsaKey.ModulusName));
            Assert.True(result.ContainsKey(Constants.JsonWebKeyParameterNames.RsaKey.ExponentName));
        }
        private static string ExtractRsaKeyInformation(Dictionary <string, object> information)
        {
            var modulusKeyPair  = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ModulusName);
            var exponentKeyPair = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ExponentName);

            if (modulusKeyPair.Equals(default(KeyValuePair <string, object>)) ||
                exponentKeyPair.Equals(default(KeyValuePair <string, object>)))
            {
                throw new InvalidOperationException(ErrorDescriptions.CannotExtractParametersFromJsonWebKey);
            }

            var rsaParameters = new RSAParameters
            {
                Modulus  = modulusKeyPair.Value.ToString().Base64DecodeBytes(),
                Exponent = exponentKeyPair.Value.ToString().Base64DecodeBytes()
            };

#if UAP
            // TODO : Extract RSA Key information ...
            return(null);
#elif NET46 || NET45
            using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
            {
                rsaCryptoServiceProvider.ImportParameters(rsaParameters);
                return(rsaCryptoServiceProvider.ToXmlString(false));
            }
#elif NETSTANDARD
            using (var rsaCryptoServiceProvider = new RSAOpenSsl())
            {
                rsaCryptoServiceProvider.ImportParameters(rsaParameters);
                return(rsaCryptoServiceProvider.ToXmlString(false));
            }
#endif
        }
Пример #3
0
        public async Task <bool> Execute()
        {
            var jsonWebKeys = await _jsonWebKeyRepository.GetAllAsync();

            if (jsonWebKeys == null ||
                !jsonWebKeys.Any())
            {
                return(false);
            }

            foreach (var jsonWebKey in jsonWebKeys)
            {
                var serializedRsa = string.Empty;
#if NET46
                using (var provider = new RSACryptoServiceProvider())
                {
                    serializedRsa = provider.ToXmlString(true);
                }
#else
                using (var rsa = new RSAOpenSsl())
                {
                    serializedRsa = rsa.ToXmlString(true);
                }
#endif

                jsonWebKey.SerializedKey = serializedRsa;
                await _jsonWebKeyRepository.UpdateAsync(jsonWebKey);
            }

            return(true);
        }
Пример #4
0
        public static List <JsonWebKey> GetJsonWebKeys(SharedContext sharedContext)
        {
            var serializedRsa = string.Empty;

#if NET461
            using (var provider = new RSACryptoServiceProvider())
            {
                serializedRsa = provider.ToXmlString(true);
            }
#else
            using (var rsa = new RSAOpenSsl())
            {
                serializedRsa = rsa.ToXmlString(true);
            }
#endif
            return(new List <JsonWebKey>
            {
                sharedContext.EncryptionKey,
                sharedContext.SignatureKey
            });
        }
        private static void InsertJsonWebKeys(SimpleIdentityServerContext context)
        {
            if (!context.JsonWebKeys.Any())
            {
                var serializedRsa = string.Empty;
#if NET46
                using (var provider = new RSACryptoServiceProvider())
                {
                    serializedRsa = provider.ToXmlString(true);
                }
#else
                using (var rsa = new RSAOpenSsl())
                {
                    serializedRsa = rsa.ToXmlString(true);
                }
#endif

                context.JsonWebKeys.AddRange(new[]
                {
                    new JsonWebKey
                    {
                        Alg           = AllAlg.RS256,
                        KeyOps        = "0,1",
                        Kid           = "1",
                        Kty           = KeyType.RSA,
                        Use           = Use.Sig,
                        SerializedKey = serializedRsa,
                    },
                    new JsonWebKey
                    {
                        Alg           = AllAlg.RSA1_5,
                        KeyOps        = "2,3",
                        Kid           = "2",
                        Kty           = KeyType.RSA,
                        Use           = Use.Enc,
                        SerializedKey = serializedRsa,
                    }
                });
            }
        }
Пример #6
0
        private static void InsertJsonWebKeys(SimpleIdentityServerContext context, SharedContext sharedContext)
        {
            if (!context.JsonWebKeys.Any())
            {
                var serializedRsa = string.Empty;
#if NET461
                using (var provider = new RSACryptoServiceProvider())
                {
                    serializedRsa = provider.ToXmlString(true);
                }
#else
                using (var rsa = new RSAOpenSsl())
                {
                    serializedRsa = rsa.ToXmlString(true);
                }
#endif

                context.JsonWebKeys.AddRange(new[]
                {
                    sharedContext.ModelEncryptionKey,
                    sharedContext.ModelSignatureKey
                });
            }
        }