Ejemplo n.º 1
0
        private static void InsertJsonWebKeys(DocumentManagementDbContext context)
        {
            if (!context.JsonWebKeys.Any())
            {
                var serializedRsa = string.Empty;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (var provider = new RSACryptoServiceProvider())
                    {
                        serializedRsa = provider.ToXmlStringNetCore(true);
                    }
                }
                else
                {
                    using (var rsa = new RSAOpenSsl())
                    {
                        serializedRsa = rsa.ToXmlStringNetCore(true);
                    }
                }

                context.JsonWebKeys.AddRange(new[]
                {
                    new OfficeDocumentJsonWebKey
                    {
                        Kid           = "1",
                        Kty           = KeyType.RSA,
                        SerializedKey = serializedRsa,
                    }
                });
            }
        }
Ejemplo n.º 2
0
        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 (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider())
                {
                    rsaCryptoServiceProvider.ImportParameters(rsaParameters);
                    return(rsaCryptoServiceProvider.ToXmlStringNetCore(false));
                }
            }
            else
            {
                using (var rsaCryptoServiceProvider = new RSAOpenSsl())
                {
                    rsaCryptoServiceProvider.ImportParameters(rsaParameters);
                    return(rsaCryptoServiceProvider.ToXmlStringNetCore(false));
                }
            }
        }
        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 (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (var provider = new RSACryptoServiceProvider())
                    {
                        serializedRsa = provider.ToXmlStringNetCore(true);
                    }
                }
                else
                {
                    using (var rsa = new RSAOpenSsl())
                    {
                        serializedRsa = rsa.ToXmlStringNetCore(true);
                    }
                }

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

            return(true);
        }
Ejemplo n.º 4
0
        public DefaultJsonWebKeyRepository(ICollection <JsonWebKey> jsonWebKeys)
        {
            if (jsonWebKeys != null)
            {
                _jsonWebKeys = jsonWebKeys;
                return;
            }

            var serializedRsa = string.Empty;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var provider = new RSACryptoServiceProvider())
                {
                    serializedRsa = provider.ToXmlStringNetCore(true);
                }
            }
            else
            {
                using (var rsa = new RSAOpenSsl())
                {
                    serializedRsa = rsa.ToXmlStringNetCore(true);
                }
            }

            _jsonWebKeys = new List <JsonWebKey>
            {
                new JsonWebKey
                {
                    Alg    = AllAlg.RS256,
                    KeyOps = new []
                    {
                        KeyOperations.Sign,
                        KeyOperations.Verify
                    },
                    Kid           = "1",
                    Kty           = KeyType.RSA,
                    Use           = Use.Sig,
                    SerializedKey = serializedRsa,
                },
                new JsonWebKey
                {
                    Alg    = AllAlg.RSA1_5,
                    KeyOps = new []
                    {
                        KeyOperations.Encrypt,
                        KeyOperations.Decrypt
                    },
                    Kid           = "2",
                    Kty           = KeyType.RSA,
                    Use           = Use.Enc,
                    SerializedKey = serializedRsa,
                }
            };
        }
Ejemplo n.º 5
0
        private static void NewCertificate()
        {
            var privateSerializedRsa = string.Empty;
            var publicSerializedRsa  = string.Empty;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                using (var provider = new RSACryptoServiceProvider())
                {
                    privateSerializedRsa = provider.ToXmlStringNetCore(true);
                    publicSerializedRsa  = provider.ToXmlStringNetCore(false);
                }
            }
            else
            {
                using (var rsa = new RSAOpenSsl())
                {
                    privateSerializedRsa = rsa.ToXmlStringNetCore(true);
                    publicSerializedRsa  = rsa.ToXmlStringNetCore(false);
                }
            }

            var locationPath       = GetLocationPath();
            var publicKeyFilePath  = Path.Combine(locationPath, "puk.txt");
            var privateKeyFilePath = Path.Combine(locationPath, "prk.txt");

            if (File.Exists(publicKeyFilePath))
            {
                File.Delete(publicKeyFilePath);
            }

            if (File.Exists(privateKeyFilePath))
            {
                File.Delete(privateKeyFilePath);
            }

            File.WriteAllText(publicKeyFilePath, publicSerializedRsa);
            File.WriteAllText(privateKeyFilePath, privateSerializedRsa);
        }
Ejemplo n.º 6
0
        private static void InsertJsonWebKeys(SimpleIdentityServerContext context)
        {
            if (!context.JsonWebKeys.Any())
            {
                var serializedRsa = string.Empty;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    using (var provider = new RSACryptoServiceProvider())
                    {
                        serializedRsa = provider.ToXmlStringNetCore(true);
                    }
                }
                else
                {
                    using (var rsa = new RSAOpenSsl())
                    {
                        serializedRsa = rsa.ToXmlStringNetCore(true);
                    }
                }

                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,
                    }
                });
            }
        }