public static MongoDbKeyParameters Create(RSAParameters source)
        {
            var mongoParameters = new MongoDbKeyParameters();

            mongoParameters.D        = source.D;
            mongoParameters.DP       = source.DP;
            mongoParameters.DQ       = source.DQ;
            mongoParameters.Exponent = source.Exponent;
            mongoParameters.InverseQ = source.InverseQ;
            mongoParameters.Modulus  = source.Modulus;
            mongoParameters.P        = source.P;
            mongoParameters.Q        = source.Q;

            return(mongoParameters);
        }
Exemple #2
0
        public void Configure(OpenIddictServerOptions options)
        {
            var collection = database.GetCollection <MongoDbKey>("Identity_Key6");

            var key = collection.Find(x => x.Id == "Default").FirstOrDefault();

            RsaSecurityKey securityKey;

            if (key == null)
            {
                securityKey = new RsaSecurityKey(RSA.Create(2048))
                {
                    KeyId = RandomHash.New()
                };

                key = new MongoDbKey {
                    Id = "Default", Key = securityKey.KeyId
                };

                if (securityKey.Rsa != null)
                {
                    var parameters = securityKey.Rsa.ExportParameters(true);

                    key.Parameters = MongoDbKeyParameters.Create(parameters);
                }
                else
                {
                    key.Parameters = MongoDbKeyParameters.Create(securityKey.Parameters);
                }

                try
                {
                    collection.InsertOne(key);
                }
                catch (MongoWriteException ex)
                {
                    if (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
                    {
                        key = collection.Find(x => x.Id == "Default").FirstOrDefault();
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (key == null)
            {
                throw new InvalidOperationException("Cannot read key.");
            }

            securityKey = new RsaSecurityKey(key.Parameters.ToParameters())
            {
                KeyId = key.Key
            };

            options.SigningCredentials.Add(
                new SigningCredentials(securityKey,
                                       SecurityAlgorithms.RsaSha256));

            options.EncryptionCredentials.Add(new EncryptingCredentials(securityKey,
                                                                        SecurityAlgorithms.RsaOAEP,
                                                                        SecurityAlgorithms.Aes256CbcHmacSha512));
        }
Exemple #3
0
        private async Task <(SecurityKeyInfo[], SigningCredentials)> GetOrCreateKeyAsync()
        {
            if (cachedKey != null && cachedKeyInfo != null)
            {
                return(cachedKeyInfo, cachedKey);
            }

            var key = await Collection.Find(x => x.Id == "Default").FirstOrDefaultAsync();

            RsaSecurityKey securityKey;

            if (key == null)
            {
                securityKey = new RsaSecurityKey(RSA.Create(2048))
                {
                    KeyId = CryptoRandom.CreateUniqueId(16)
                };

                key = new MongoDbKey {
                    Id = "Default", Key = securityKey.KeyId
                };

                if (securityKey.Rsa != null)
                {
                    var parameters = securityKey.Rsa.ExportParameters(includePrivateParameters: true);

                    key.Parameters = MongoDbKeyParameters.Create(parameters);
                }
                else
                {
                    key.Parameters = MongoDbKeyParameters.Create(securityKey.Parameters);
                }

                try
                {
                    await Collection.InsertOneAsync(key);

                    return(CreateCredentialsPair(securityKey));
                }
                catch (MongoWriteException ex)
                {
                    if (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
                    {
                        key = await Collection.Find(x => x.Id == "Default").FirstOrDefaultAsync();
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (key == null)
            {
                throw new InvalidOperationException("Cannot read key.");
            }

            securityKey = new RsaSecurityKey(key.Parameters.ToParameters())
            {
                KeyId = key.Key
            };

            return(CreateCredentialsPair(securityKey));
        }