Example #1
0
        private PwGroupV3 ConvertGroup(PwGroup fromGroup, PwDatabaseV3 dbTo)
        {
            PwGroupV3 toGroup = new PwGroupV3();

            toGroup.Name = fromGroup.Name;
            //todo remove
            Android.Util.Log.Debug("KP2A", "save kdb: group " + fromGroup.Name);

            toGroup.TCreation   = new PwDate(ConvertTime(fromGroup.CreationTime));
            toGroup.TLastAccess = new PwDate(ConvertTime(fromGroup.LastAccessTime));
            toGroup.TLastMod    = new PwDate(ConvertTime(fromGroup.LastModificationTime));
            if (fromGroup.Expires)
            {
                toGroup.TExpire = new PwDate(ConvertTime(fromGroup.ExpiryTime));
            }
            else
            {
                toGroup.TExpire = new PwDate(ConvertTime(_expireNever));
            }

            toGroup.Icon = dbTo.IconFactory.GetIcon((int)fromGroup.IconId);
            AdditionalGroupData groupData;

            if (_groupData.TryGetValue(fromGroup.Uuid, out groupData))
            {
                toGroup.Id    = new PwGroupIdV3(groupData.Id);
                toGroup.Flags = groupData.Flags;
            }
            else
            {
                //group was added
                do
                {
                    toGroup.Id = new PwGroupIdV3(new Random().Next());
                } while (_groupData.Values.Any(gd => gd.Id == toGroup.Id.Id));                 //retry if id already exists
                //store to block new id and reuse when saving again (without loading in between)
                _groupData[fromGroup.Uuid] = new AdditionalGroupData
                {
                    Id = toGroup.Id.Id
                };
            }

            return(toGroup);
        }
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 void AssignParent(PwGroup kpParent, PwDatabaseV3 dbV3, Dictionary <int, PwGroupV3> groupV3s)
        {
            PwGroupV3 parentV3;

            if (kpParent.ParentGroup == null)
            {
                parentV3 = dbV3.RootGroup;
            }
            else
            {
                parentV3 = groupV3s[_groupData[kpParent.Uuid].Id];
            }

            foreach (PwGroup g in kpParent.Groups.OrderBy(g => g.Name))
            {
                PwGroupV3 groupV3 = groupV3s[_groupData[g.Uuid].Id];

                parentV3.ChildGroups.Add(groupV3);
                groupV3.Parent = parentV3;

                AssignParent(g, dbV3, groupV3s);
            }
        }
Example #4
0
        private PwGroupV3 ConvertGroup(PwGroup fromGroup, PwDatabaseV3 dbTo)
        {
            PwGroupV3 toGroup = new PwGroupV3();
            toGroup.Name = fromGroup.Name;
            //todo remove
            Android.Util.Log.Debug("KP2A", "save kdb: group " + fromGroup.Name);

            toGroup.TCreation = new PwDate(ConvertTime(fromGroup.CreationTime));
            toGroup.TLastAccess= new PwDate(ConvertTime(fromGroup.LastAccessTime));
            toGroup.TLastMod = new PwDate(ConvertTime(fromGroup.LastModificationTime));
            if (fromGroup.Expires)
            {
                toGroup.TExpire = new PwDate(ConvertTime(fromGroup.ExpiryTime));
            }
            else
            {
                toGroup.TExpire = new PwDate(ConvertTime(_expireNever));
            }

            toGroup.Icon = dbTo.IconFactory.GetIcon((int) fromGroup.IconId);
            AdditionalGroupData groupData;
            if (_groupData.TryGetValue(fromGroup.Uuid, out groupData))
            {
                toGroup.Id = new PwGroupIdV3(groupData.Id);
                toGroup.Flags = groupData.Flags;
            }
            else
            {
                //group was added
                do
                {
                    toGroup.Id = new PwGroupIdV3(new Random().Next());
                } while (_groupData.Values.Any(gd => gd.Id == toGroup.Id.Id)); //retry if id already exists
                //store to block new id and reuse when saving again (without loading in between)
                _groupData[fromGroup.Uuid] = new AdditionalGroupData
                    {
                        Id = toGroup.Id.Id
                    };

            }

            return toGroup;
        }
Example #5
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 #6
0
        private void AssignParent(PwGroup kpParent, PwDatabaseV3 dbV3, Dictionary<int, PwGroupV3> groupV3s)
        {
            PwGroupV3 parentV3;
            if (kpParent.ParentGroup == null)
            {
                parentV3 = dbV3.RootGroup;
            }
            else
            {
                parentV3 = groupV3s[_groupData[kpParent.Uuid].Id];
            }

            foreach (PwGroup g in kpParent.Groups.OrderBy(g => g.Name))
            {
                PwGroupV3 groupV3 = groupV3s[_groupData[g.Uuid].Id];

                parentV3.ChildGroups.Add(groupV3);
                groupV3.Parent = parentV3;

                AssignParent(g, dbV3, groupV3s);
            }
        }
Example #7
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);
            db.NumRounds = (long) kpDatabase.KeyEncryptionRounds;
            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();
        }
Example #8
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();
        }