Ejemplo n.º 1
0
        public static async Task UploadAttachment(this Vault vault, PasswordRecord record, IAttachmentUploadTask uploadTask)
        {
            var fileStream = uploadTask.Stream;

            if (fileStream == null)
            {
                throw new KeeperInvalidParameter("Vault::UploadAttachment", "uploadTask", "GetStream()", "null");
            }
            var thumbStream = uploadTask.Thumbnail?.Stream;
            var command     = new RequestUploadCommand();

            command.fileCount      = 1;
            command.thumbnailCount = thumbStream != null ? 1 : 0;

            var rs = await vault.Auth.ExecuteAuthCommand <RequestUploadCommand, RequestUpoadResponse>(command);

            if (rs.fileUploads == null || rs.fileUploads.Length < 1)
            {
                throw new KeeperInvalidParameter("Vault::UploadAttachment", "request_upload", "file_uploads", "empty");
            }

            UploadParameters fileUpload  = rs.fileUploads[0];
            UploadParameters thumbUpload = null;

            if (rs.thumbnailUploads != null && rs.thumbnailUploads.Length > 0)
            {
                thumbUpload = rs.thumbnailUploads[0];
            }

            var key  = CryptoUtils.GenerateEncryptionKey();
            var atta = new AttachmentFile {
                Id           = fileUpload.fileID,
                Name         = uploadTask.Name,
                Title        = uploadTask.Title,
                Key          = key.Base64UrlEncode(),
                Type         = uploadTask.MimeType,
                LastModified = DateTimeOffset.Now,
            };
            var transform = new EncryptAesV1Transform(key);

            using (var cryptoStream = new CryptoStream(fileStream, transform, CryptoStreamMode.Read)) {
                await UploadSingleFile(fileUpload, cryptoStream);

                atta.Size = transform.EncryptedBytes;
            }
            if (thumbUpload != null)
            {
                try {
                    transform = new EncryptAesV1Transform(key);
                    using (var cryptoStream = new CryptoStream(thumbStream, transform, CryptoStreamMode.Read))
                    {
                        await UploadSingleFile(thumbUpload, cryptoStream);
                    }
                    var thumbnail = new AttachmentFileThumb
                    {
                        Id   = thumbUpload.fileID,
                        Type = uploadTask.Thumbnail.MimeType,
                        Size = uploadTask.Thumbnail.Size
                    };
                    var ts = new AttachmentFileThumb[] { thumbnail };
                    if (atta.Thumbnails == null)
                    {
                        atta.Thumbnails = ts;
                    }
                    else
                    {
                        atta.Thumbnails = atta.Thumbnails.Concat(ts).ToArray();
                    }
                }
                catch (Exception e) {
                    Trace.TraceError("Upload Thumbname: {0}: \"{1}\"", e.GetType().Name, e.Message);
                }
            }

            record.Attachments.Add(atta);
        }
Ejemplo n.º 2
0
        internal static void DecryptRecords(this Vault vault)
        {
            var uids = new HashSet <string>();

            uids.UnionWith(vault.records.Keys);
            uids.ExceptWith(vault.keeperRecords.Keys);
            if (uids.Count > 0)
            {
                var dataSerializer  = new DataContractJsonSerializer(typeof(RecordData));
                var extraSerializer = new DataContractJsonSerializer(typeof(RecordExtra));

                foreach (var uid in uids)
                {
                    if (vault.records.TryGetValue(uid, out SyncDownRecord sdr))
                    {
                        try
                        {
                            var record = new PasswordRecord(uid);

                            var unencrypted_data = CryptoUtils.DecryptAesV1(sdr.data.Base64UrlDecode(), sdr.unencryptedRecordKey);
                            using (var ms = new MemoryStream(unencrypted_data))
                            {
                                var data = (RecordData)dataSerializer.ReadObject(ms);
                                record.Title    = data.title;
                                record.Login    = data.secret1;
                                record.Password = data.secret2;
                                record.Link     = data.link;
                                record.Notes    = data.notes;
                                if (data.custom != null)
                                {
                                    foreach (var cr in data.custom)
                                    {
                                        record.Custom.Add(new CustomField
                                        {
                                            Name  = cr.name,
                                            Value = cr.value,
                                            Type  = cr.type
                                        });
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(sdr.extra))
                            {
                                var unencrypted_extra = CryptoUtils.DecryptAesV1(sdr.extra.Base64UrlDecode(), sdr.unencryptedRecordKey);
                                using (var ms = new MemoryStream(unencrypted_extra))
                                {
                                    var extra = (RecordExtra)extraSerializer.ReadObject(ms);
                                    if (extra.files != null && extra.files.Length > 0)
                                    {
                                        foreach (var file in extra.files)
                                        {
                                            var atta = new AttachmentFile
                                            {
                                                Id           = file.id,
                                                Key          = file.key,
                                                Name         = file.name,
                                                Title        = file.title ?? "",
                                                Type         = file.type ?? "",
                                                Size         = file.size ?? 0,
                                                LastModified = file.lastModified != null?file.lastModified.Value.FromUnixTimeMilliseconds() : DateTimeOffset.Now
                                            };
                                            if (file.thumbs != null)
                                            {
                                                atta.Thumbnails = file.thumbs
                                                                  .Select(t => new AttachmentFileThumb
                                                {
                                                    Id   = t.id,
                                                    Type = t.type,
                                                    Size = t.size ?? 0
                                                })
                                                                  .ToArray();
                                            }
                                            record.Attachments.Add(atta);
                                        }
                                    }
                                }
                            }

                            vault.keeperRecords.Add(uid, record);
                        }
                        catch (Exception e)
                        {
                            Trace.TraceError("Decrypt Record: UID: {0}, {1}: \"{2}\"", uid, e.GetType().Name, e.Message);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static async Task DownloadAttachment(this Vault vault, PasswordRecord record, string attachment, Stream destination)
        {
            if (record.Attachments == null)
            {
                throw new KeeperInvalidParameter("Vault::DownloadAttachment", "record", record.Uid, "has no attachments");
            }
            AttachmentFile attachmentFile = null;

            if (string.IsNullOrEmpty(attachment))
            {
                if (record.Attachments.Count == 1)
                {
                    attachmentFile = record.Attachments[0];
                }
                else
                {
                    throw new KeeperInvalidParameter("Vault::DownloadAttachment", "attachment", "", "is empty");
                }
            }
            else
            {
                attachmentFile = record.Attachments
                                 .FirstOrDefault(x =>
                {
                    if (attachment == x.Id || attachment == x.Name || attachment == x.Title)
                    {
                        return(true);
                    }
                    if (x.Thumbnails != null)
                    {
                        var thumbId = x.Thumbnails.Select(y => y.Id).FirstOrDefault(y => y == attachment);
                        if (!string.IsNullOrEmpty(thumbId))
                        {
                            return(true);
                        }
                    }
                    return(false);
                });
            }
            if (attachmentFile == null)
            {
                throw new KeeperInvalidParameter("Vault::DownloadAttachment", "attachment", attachment, "not found");
            }

            var attachmentId = attachmentFile.Id;

            if (attachmentFile.Thumbnails != null)
            {
                foreach (var th in attachmentFile.Thumbnails)
                {
                    if (th.Id == attachment)
                    {
                        attachmentId = th.Id;
                        break;
                    }
                }
            }

            var request = await CreateAttachmentDownloadRequest(vault, record.Uid, attachmentId);

            using (var response = await request.GetResponseAsync() as HttpWebResponse)
            {
                using (var stream = response.GetResponseStream())
                {
                    var transform = new DecryptAesV1Transform(attachmentFile.Key.Base64UrlDecode());
                    using (var decodeStream = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                    {
                        if (destination != null)
                        {
                            await decodeStream.CopyToAsync(destination);
                        }
                    }
                }
            }
        }