Exemple #1
0
        public void OnDeserialized(StreamingContext ctx)
        {
            object[]         context       = (object[])ctx.Context;
            GetNodesResponse nodesResponse = (GetNodesResponse)context[0];

            if (context.Length == 1)
            {
                // Add key from incoming sharing.
                if (SharingKey != null && nodesResponse.SharedKeys.Any(x => x.Id == Id) == false)
                {
                    nodesResponse.SharedKeys.Add(new SharedKey(Id, SharingKey));
                }
                return;
            }
            else
            {
                byte[] masterKey = (byte[])context[1];

                CreationDate = SerializedCreationDate.ToDateTime();

                if (Type == NodeType.File || Type == NodeType.Directory)
                {
                    // 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
                    string serializedKey = SerializedKey.Split('/')[0];
                    int    splitPosition = serializedKey.IndexOf(":", StringComparison.InvariantCulture);
                    byte[] encryptedKey  = serializedKey.Substring(splitPosition + 1).FromBase64();

                    // If node is shared, we need to retrieve shared masterkey
                    if (nodesResponse.SharedKeys != null)
                    {
                        string    handle    = serializedKey.Substring(0, splitPosition);
                        SharedKey sharedKey = nodesResponse.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);
                            }
                        }
                    }

                    FullKey = Crypto.DecryptKey(encryptedKey, masterKey);

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

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

                    Attributes = Crypto.DecryptAttributes(SerializedAttributes.FromBase64(), Key);
                }
            }
        }
Exemple #2
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);
            }
        }