コード例 #1
0
        public static MongoKeyParameters Create(RSAParameters source)
        {
            var mongoParameters = new MongoKeyParameters();

            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);
        }
コード例 #2
0
ファイル: MongoKeyStore.cs プロジェクト: vmnet04/squidex
        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 MongoKey {
                    Id = "Default", Key = securityKey.KeyId
                };

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

                    key.Parameters = MongoKeyParameters.Create(parameters);
                }
                else
                {
                    key.Parameters = MongoKeyParameters.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 ex;
                    }
                }
            }

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

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

            return(CreateCredentialsPair(securityKey));
        }