Example #1
0
        private PwEntry ConvertEntry(PwEntryV3 fromEntry)
        {
            PwEntry toEntry = new PwEntry(false, false);

            toEntry.Uuid = new PwUuid(fromEntry.Uuid.ToArray());
            String modTime = Android.Text.Format.DateFormat.GetTimeFormat(Application.Context).Format(fromEntry.TCreation.JDate);

            Android.Util.Log.Debug("KP2A", modTime);
            toEntry.CreationTime         = ConvertTime(fromEntry.TCreation);
            toEntry.LastAccessTime       = ConvertTime(fromEntry.TLastAccess);
            toEntry.LastModificationTime = ConvertTime(fromEntry.TLastMod);

            toEntry.ExpiryTime = ConvertTime(fromEntry.TExpire);

            toEntry.ExpiryTime = new DateTime(toEntry.ExpiryTime.Year, toEntry.ExpiryTime.Month, toEntry.ExpiryTime.Day, toEntry.ExpiryTime.Hour, toEntry.ExpiryTime.Minute, toEntry.ExpiryTime.Second);
            toEntry.Expires    = !(Math.Abs((toEntry.ExpiryTime - _expireNever).TotalMilliseconds) < 500);

            if (fromEntry.Icon != null)
            {
                toEntry.IconId = (PwIcon)fromEntry.Icon.IconId;
            }
            SetFieldIfAvailable(toEntry, PwDefs.TitleField, false, fromEntry.Title);
            Android.Util.Log.Debug("KP2A", "load kdb: entry " + toEntry.Strings.ReadSafe(PwDefs.TitleField));
            SetFieldIfAvailable(toEntry, PwDefs.UserNameField, false, fromEntry.Username);
            SetFieldIfAvailable(toEntry, PwDefs.UrlField, false, fromEntry.Url);
            SetFieldIfAvailable(toEntry, PwDefs.PasswordField, true, fromEntry.Password);
            SetFieldIfAvailable(toEntry, PwDefs.NotesField, true, fromEntry.Additional);

            if ((fromEntry.GetBinaryData() != null) && (fromEntry.GetBinaryData().Length > 0))
            {
                toEntry.Binaries.Set(fromEntry.BinaryDesc, new ProtectedBinary(true, fromEntry.GetBinaryData()));
            }
            return(toEntry);
        }
Example #2
0
        private PwEntryV3 ConvertEntry(PwEntry fromEntry, PwDatabaseV3 dbTo)
        {
            PwEntryV3 toEntry = new PwEntryV3();

            toEntry.Uuid                 = fromEntry.Uuid.UuidBytes;
            toEntry.CreationTime         = ConvertTime(fromEntry.CreationTime);
            toEntry.LastAccessTime       = ConvertTime(fromEntry.LastAccessTime);
            toEntry.LastModificationTime = ConvertTime(fromEntry.LastModificationTime);

            if (fromEntry.Expires)
            {
                toEntry.ExpiryTime = ConvertTime(fromEntry.ExpiryTime);
            }
            else
            {
                toEntry.ExpiryTime = ConvertTime(_expireNever);
            }


            toEntry.Icon = dbTo.IconFactory.GetIcon((int)fromEntry.IconId);
            toEntry.SetTitle(GetString(fromEntry, PwDefs.TitleField), dbTo);
            Android.Util.Log.Debug("KP2A", "save kdb: entry " + fromEntry.Strings.ReadSafe(PwDefs.TitleField));
            toEntry.SetUsername(GetString(fromEntry, PwDefs.UserNameField), dbTo);
            toEntry.SetUrl(GetString(fromEntry, PwDefs.UrlField), dbTo);
            var pwd = GetString(fromEntry, PwDefs.PasswordField);

            if (pwd != null)
            {
                toEntry.SetPassword(pwd, dbTo);
            }
            toEntry.SetNotes(GetString(fromEntry, PwDefs.NotesField), dbTo);
            if (fromEntry.Binaries.Any())
            {
                var binaryData = fromEntry.Binaries.First().Value.ReadData();
                toEntry.SetBinaryData(binaryData, 0, binaryData.Length);
            }

            return(toEntry);
        }
Example #3
0
        private PwEntry ConvertEntry(PwEntryV3 fromEntry)
        {
            PwEntry toEntry = new PwEntry(false, false);
            toEntry.Uuid = new PwUuid(fromEntry.Uuid.ToArray());
            String modTime = Android.Text.Format.DateFormat.GetTimeFormat(Application.Context).Format(fromEntry.TCreation.JDate);
            Android.Util.Log.Debug("KP2A", modTime);
            toEntry.CreationTime = ConvertTime(fromEntry.TCreation);
            toEntry.LastAccessTime = ConvertTime(fromEntry.TLastAccess);
            toEntry.LastModificationTime = ConvertTime(fromEntry.TLastMod);

            toEntry.ExpiryTime = ConvertTime(fromEntry.TExpire);

            toEntry.ExpiryTime = new DateTime(toEntry.ExpiryTime.Year, toEntry.ExpiryTime.Month, toEntry.ExpiryTime.Day, toEntry.ExpiryTime.Hour, toEntry.ExpiryTime.Minute, toEntry.ExpiryTime.Second);
            toEntry.Expires = !(Math.Abs((toEntry.ExpiryTime - _expireNever).TotalMilliseconds) < 500);

            if (fromEntry.Icon != null)
                toEntry.IconId = (PwIcon) fromEntry.Icon.IconId;
            SetFieldIfAvailable(toEntry, PwDefs.TitleField, false, fromEntry.Title);
            Android.Util.Log.Debug("KP2A", "load kdb: entry " + toEntry.Strings.ReadSafe(PwDefs.TitleField));
            SetFieldIfAvailable(toEntry, PwDefs.UserNameField, false, fromEntry.Username);
            SetFieldIfAvailable(toEntry, PwDefs.UrlField, false, fromEntry.Url);
            SetFieldIfAvailable(toEntry, PwDefs.PasswordField, true, fromEntry.Password);
            SetFieldIfAvailable(toEntry, PwDefs.NotesField, true, fromEntry.Additional);

            if ((fromEntry.GetBinaryData() != null) && (fromEntry.GetBinaryData().Length > 0))
            {
                toEntry.Binaries.Set(fromEntry.BinaryDesc, new ProtectedBinary(true, fromEntry.GetBinaryData()));
            }
            return toEntry;
        }
Example #4
0
        private PwEntryV3 ConvertEntry(PwEntry fromEntry, PwDatabaseV3 dbTo)
        {
            PwEntryV3 toEntry = new PwEntryV3();
            toEntry.Uuid = fromEntry.Uuid.UuidBytes;
            toEntry.CreationTime = ConvertTime(fromEntry.CreationTime);
            toEntry.LastAccessTime = ConvertTime(fromEntry.LastAccessTime);
            toEntry.LastModificationTime = ConvertTime(fromEntry.LastModificationTime);

            if (fromEntry.Expires)
            {
                toEntry.ExpiryTime = ConvertTime(fromEntry.ExpiryTime);
            }
            else
            {
                toEntry.ExpiryTime = ConvertTime(_expireNever);
            }

            toEntry.Icon = dbTo.IconFactory.GetIcon((int) fromEntry.IconId);
            toEntry.SetTitle(GetString(fromEntry, PwDefs.TitleField), dbTo);
            Android.Util.Log.Debug("KP2A", "save kdb: entry " + fromEntry.Strings.ReadSafe(PwDefs.TitleField));
            toEntry.SetUsername(GetString(fromEntry, PwDefs.UserNameField), dbTo);
            toEntry.SetUrl(GetString(fromEntry, PwDefs.UrlField), dbTo);
            var pwd = GetString(fromEntry, PwDefs.PasswordField);
            if (pwd != null)
                toEntry.SetPassword(pwd, dbTo);
            toEntry.SetNotes(GetString(fromEntry, PwDefs.NotesField), dbTo);
            if (fromEntry.Binaries.Any())
            {
                var binaryData = fromEntry.Binaries.First().Value.ReadData();
                toEntry.SetBinaryData(binaryData, 0, binaryData.Length);
            }

            return toEntry;
        }
Example #5
0
        public void Save(PwDatabase kpDatabase, Stream stream)
        {
            PwDatabaseV3 db       = new PwDatabaseV3();
            KcpPassword  pwd      = kpDatabase.MasterKey.GetUserKey <KcpPassword>();
            string       password = pwd != null?pwd.Password.ReadString() : "";

            KcpKeyFile keyfile         = kpDatabase.MasterKey.GetUserKey <KcpKeyFile>();
            Stream     keyfileContents = null;

            if (keyfile != null)
            {
                keyfileContents = new MemoryStream(keyfile.RawFileData.ReadData());
            }
            db.SetMasterKey(password, keyfileContents);

            AesKdf kdf = new AesKdf();

            if (!kdf.Uuid.Equals(kpDatabase.KdfParameters.KdfUuid))
            {
                db.NumRounds = (uint)PwDefs.DefaultKeyEncryptionRounds;
            }
            else
            {
                ulong uRounds = kpDatabase.KdfParameters.GetUInt64(
                    AesKdf.ParamRounds, PwDefs.DefaultKeyEncryptionRounds);
                uRounds = Math.Min(uRounds, 0xFFFFFFFEUL);

                db.NumRounds = (uint)uRounds;
            }


            db.Name = kpDatabase.Name;
            if (kpDatabase.DataCipherUuid.Equals(StandardAesEngine.AesUuid))
            {
                db.Algorithm = PwEncryptionAlgorithm.Rjindal;
            }
            else
            {
                db.Algorithm = PwEncryptionAlgorithm.Twofish;
            }

            //create groups
            db.Groups.Clear();
            var fromGroups = kpDatabase.RootGroup.GetGroups(true);
            Dictionary <int, PwGroupV3> groupV3s = new Dictionary <int, PwGroupV3>(fromGroups.Count());

            foreach (PwGroup g in fromGroups)
            {
                if (g == kpDatabase.RootGroup)
                {
                    continue;
                }
                PwGroupV3 groupV3 = ConvertGroup(g, db);
                db.Groups.Add(groupV3);
                groupV3s[groupV3.Id.Id] = groupV3;
            }

            //traverse again and assign parents
            db.RootGroup       = ConvertGroup(kpDatabase.RootGroup, db);
            db.RootGroup.Level = -1;

            AssignParent(kpDatabase.RootGroup, db, groupV3s);

            foreach (PwEntry e in kpDatabase.RootGroup.GetEntries(true))
            {
                PwEntryV3 entryV3 = ConvertEntry(e, db);
                entryV3.Parent = groupV3s[_groupData[e.ParentGroup.Uuid].Id];
                entryV3.Parent.ChildEntries.Add(entryV3);
                entryV3.GroupId = entryV3.Parent.Id.Id;
                db.Entries.Add(entryV3);
            }

            //add meta stream entries:
            if (db.Groups.Any())
            {
                foreach (var metaEntry in _metaStreams)
                {
                    metaEntry.GroupId = db.Groups.First().Id.Id;
                    db.Entries.Add(metaEntry);
                }
            }


            HashingStreamEx hashedStream = new HashingStreamEx(stream, true, null);
            PwDbV3Output    output       = new PwDbV3Output(db, hashedStream);

            output.Output();
            hashedStream.Close();
            HashOfLastStream = hashedStream.Hash;

            kpDatabase.HashOfLastIO = kpDatabase.HashOfFileOnDisk = HashOfLastStream;
            stream.Close();
        }