예제 #1
0
        /// <inheritdoc />
        public Task StoreKeyAsync(SerializedKey key)
        {
            string keyFileContent = JsonConvert.SerializeObject(key);

            return(File.WriteAllTextAsync(
                       Path.Combine(_keyDir.FullName, string.Format(KeyFileFormatPattern, key.KeyId)), keyFileContent));
        }
        public async void ExpiredKey_NewKeyCreated()
        {
            var created              = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(KeyExpirationSec + 1));
            var expiredKey           = CreateTestKey(ExpiredKeyId, created);
            var serializedExpiredKey = new SerializedKey {
                KeyId = ExpiredKeyId
            };

            _serializerMock.Setup(x => x.Deserialize(serializedExpiredKey)).Returns(expiredKey);

            var serializedNewKey = new SerializedKey {
                KeyId = NewKeyId
            };

            _serializerMock
            .Setup(x => x.Serialize(It.Is <RsaKey>(k => k.KeyId != ExpiredKeyId)))
            .Returns(serializedNewKey);

            _repositoryMock
            .Setup(x => x.LoadKeysAsync())
            .Returns(Task.FromResult <IEnumerable <SerializedKey> >(new[] { serializedExpiredKey }));

            var result = await _service.GetAllKeysAsync();

            _repositoryMock.Verify(
                x => x.StoreKeyAsync(It.Is <SerializedKey>(k => k.KeyId == NewKeyId)), Times.Once);
        }
예제 #3
0
        public Task StoreKeyAsync(SerializedKey key)
        {
            if (Keys == null)
            {
                Keys = new List <SerializedKey>();
            }

            Keys.Add(key);
            return(Task.CompletedTask);
        }
        /// <summary>
        /// Persists new key in storage.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task StoreKeyAsync(SerializedKey key)
        {
            var json = KeySerializer.Serialize(key, EncodeJson);

            var path = Path.Combine(_directory.FullName, KeyFilePrefix + key.Id + KeyFileExtension);

            File.WriteAllText(path, json, Encoding.UTF8);

            return(Task.CompletedTask);
        }
        /// <inheritdoc/>
        public Task StoreKeyAsync(SerializedKey key)
        {
            var entity = new Key
            {
                Name    = key.KeyId,
                Created = key.Created,
                Value   = key.Data
            };

            _context.Keys.Add(entity);
            return(_context.SaveChangesAsync());
        }
예제 #6
0
        /// <summary>
        /// Unprotects RsaKeyContainer.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RsaKeyContainer Unprotect(SerializedKey key)
        {
            var data = key.DataProtected ?
                       _dataProtectionProvider.Unprotect(key.Data) :
                       key.Data;

            var item = KeySerializer.Deserialize <RsaKeyContainer>(data);

            if (item.KeyType == KeyType.X509)
            {
                item = KeySerializer.Deserialize <X509KeyContainer>(data);
            }

            return(item);
        }
예제 #7
0
        public override object GetSerialized()
        {
            var serKeys = new List <SerializedKey>();

            foreach (Keyframe key in value.keys)
            {
                var newKey = new SerializedKey();
                newKey.time       = key.time;
                newKey.value      = key.value;
                newKey.inTangent  = key.inTangent;
                newKey.outTangent = key.outTangent;
                serKeys.Add(newKey);
            }
            return(serKeys);
        }
        public async void NoKeys_NewKeyCreated()
        {
            _repositoryMock
            .Setup(x => x.LoadKeysAsync())
            .Returns(Task.FromResult(Enumerable.Empty <SerializedKey>()));
            var serializedNewKey = new SerializedKey {
                KeyId = NewKeyId
            };

            _serializerMock.Setup(x => x.Serialize(It.IsAny <RsaKey>())).Returns(serializedNewKey);

            var result = await _service.GetCurrentKeyAsync();

            _repositoryMock.Verify(
                x => x.StoreKeyAsync(It.Is <SerializedKey>(k => k.KeyId == NewKeyId)), Times.Once);
        }
        public async void SingleActiveKey_ReturnedAsCurrentKey()
        {
            var created       = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(KeyActivationDelaySec + 1));
            var key           = CreateTestKey(ActiveKeyId, created);
            var serializedKey = new SerializedKey();

            _serializerMock.Setup(x => x.Deserialize(serializedKey)).Returns(key);
            _repositoryMock
            .Setup(x => x.LoadKeysAsync())
            .Returns(Task.FromResult <IEnumerable <SerializedKey> >(new[] { serializedKey }));

            var result = await _service.GetCurrentKeyAsync();

            Assert.NotNull(result);
            Assert.Equal(ActiveKeyId, result.KeyId);
        }
예제 #10
0
    public Task StoreKeyAsync(SerializedKey key)
    {
        if (key == null)
        {
            throw new ArgumentNullException(nameof(key));
        }

        Log.LogDebug("Storing signing key with id: {SigningKeyId}", key.Id);

        return(RunAsync(conn =>
                        conn.QuerySingleOrDefaultAsync <long>(
                            "SELECT * FROM idsrv.save_signing_key(@id, @Version, @Created, @Algorithm, @IsX509Certificate, @DataProtected, @Data);",
                            key
                            )
                        ));
    }
        /// <summary>
        /// Persists new key in store.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task StoreKeyAsync(SerializedKey key)
        {
            var entity = new Key
            {
                Id                = key.Id,
                Use               = Use,
                Created           = key.Created,
                Version           = key.Version,
                Algorithm         = key.Algorithm,
                Data              = key.Data,
                DataProtected     = key.DataProtected,
                IsX509Certificate = key.IsX509Certificate
            };

            Context.Keys.Add(entity);
            return(Context.SaveChangesAsync());
        }
        public async void RetiredKey_Deleted()
        {
            var created              = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(KeyRetirementSec + 1));
            var retiredKey           = CreateTestKey(RetiredKeyId, created);
            var serializedRetiredKey = new SerializedKey {
                KeyId = RetiredKeyId
            };

            _serializerMock.Setup(x => x.Deserialize(serializedRetiredKey)).Returns(retiredKey);

            _repositoryMock
            .Setup(x => x.LoadKeysAsync())
            .Returns(Task.FromResult <IEnumerable <SerializedKey> >(new[] { serializedRetiredKey }));

            var result = await _service.GetAllKeysAsync();

            _repositoryMock.Verify(
                x => x.DeleteKeyAsync(RetiredKeyId), Times.AtLeastOnce);
        }
예제 #13
0
    /// <summary>
    /// Persists new key in store.
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public Task StoreKeyAsync(SerializedKey key)
    {
        using var activity = Tracing.StoreActivitySource.StartActivity("SigningKeyStore.StoreKey");

        var entity = new Key
        {
            Id                = key.Id,
            Use               = Use,
            Created           = key.Created,
            Version           = key.Version,
            Algorithm         = key.Algorithm,
            Data              = key.Data,
            DataProtected     = key.DataProtected,
            IsX509Certificate = key.IsX509Certificate
        };

        Context.Keys.Add(entity);
        return(Context.SaveChangesAsync(CancellationTokenProvider.CancellationToken));
    }
    /// <inheritdoc/>
    public KeyContainer Unprotect(SerializedKey key)
    {
        var data = key.DataProtected ?
                   _dataProtectionProvider.Unprotect(key.Data) :
                   key.Data;

        if (key.IsX509Certificate)
        {
            return(KeySerializer.Deserialize <X509KeyContainer>(data));
        }

        if (key.Algorithm.StartsWith("R") || key.Algorithm.StartsWith("P"))
        {
            return(KeySerializer.Deserialize <RsaKeyContainer>(data));
        }

        if (key.Algorithm.StartsWith("E"))
        {
            return(KeySerializer.Deserialize <EcKeyContainer>(data));
        }

        throw new Exception($"Invalid Algorithm: {key.Algorithm} for kid: {key.Id}");
    }
 public Task StoreKeyAsync(SerializedKey key)
 {
     _store.TryAdd(key.KeyId, key);
     return(Task.CompletedTask);
 }
예제 #16
0
        public void OnDeserialized(StreamingContext ctx)
        {
            // Add key from incoming sharing.
            if (SharingKey != null && _sharedKeys.Any(x => x.Id == Id) == false)
            {
                _sharedKeys.Add(new SharedKey(Id, SharingKey));
            }

            CreationDate = SerializedCreationDate.ToDateTime();

            if (Type == NodeType.File || Type == NodeType.Directory)
            {
                // Check if file is not yet decrypted
                if (string.IsNullOrEmpty(SerializedKey))
                {
                    EmptyKey = true;

                    return;
                }

                // There are cases where the SerializedKey property contains multiple keys separated with /
                // This can occur when a folder is shared and the parent is shared too.
                // Both keys are working so we use the first one
                var serializedKey = SerializedKey.Split('/')[0];
                var splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal);
                var encryptedKey  = serializedKey.Substring(splitPosition + 1).FromBase64();

                // If node is shared, we need to retrieve shared masterkey
                if (_sharedKeys != null)
                {
                    var handle    = serializedKey.Substring(0, splitPosition);
                    var sharedKey = _sharedKeys.FirstOrDefault(x => x.Id == handle);
                    if (sharedKey != null)
                    {
                        _masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), _masterKey);
                        if (Type == NodeType.Directory)
                        {
                            SharedKey = _masterKey;
                        }
                        else
                        {
                            SharedKey = Crypto.DecryptKey(encryptedKey, _masterKey);
                        }
                    }
                }

                if (encryptedKey.Length != 16 && encryptedKey.Length != 32)
                {
                    // Invalid key size
                    return;
                }

                FullKey = Crypto.DecryptKey(encryptedKey, _masterKey);

                if (Type == NodeType.File)
                {
                    Crypto.GetPartsFromDecryptedKey(FullKey, out var iv, out var metaMac, out var fileKey);

                    Iv      = iv;
                    MetaMac = metaMac;
                    Key     = fileKey;
                }
                else
                {
                    Key = FullKey;
                }

                Attributes     = Crypto.DecryptAttributes(SerializedAttributes.FromBase64(), Key);
                FileAttributes = DeserializeFileAttributes(SerializedFileAttributes);
            }
        }
        public RsaKey Deserialize(SerializedKey serializedKey)
        {
            string json = DataProtectionCommonExtensions.Unprotect(_protector, serializedKey.Data);

            return(JsonSerializer.Deserialize <RsaKey>(json));
        }
예제 #18
0
 public KeyContainer Unprotect(SerializedKey key)
 {
     return(KeySerializer.Deserialize <RsaKeyContainer>(key.Data));
 }
 public RsaKey Deserialize(SerializedKey serializedKey) =>
 JsonSerializer.Deserialize <RsaKey>(serializedKey.Data);