Esempio n. 1
0
        private void OnOTPSetup(object sender, EventArgs e)
        {
            if (m_host.MainWindow.GetSelectedEntriesCount() != 1)
            {
                return;
            }
            PwEntry pe = m_host.MainWindow.GetSelectedEntry(true);

            if (!OTPDAO.EnsureOTPSetupPossible(pe))
            {
                return;
            }
            var otpSetup = new KeePassOTPSetup();

            Tools.GlobalWindowManager(otpSetup);
            otpSetup.OTP      = OTPDAO.GetOTP(pe);
            otpSetup.EntryUrl = pe.Strings.GetSafe(PwDefs.UrlField).ReadString();
            otpSetup.InitEx();
            if (otpSetup.ShowDialog(m_host.MainWindow) == DialogResult.OK)
            {
                OTPDAO.SaveOTP(otpSetup.OTP, pe);
            }
            otpSetup.Dispose();
        }
Esempio n. 2
0
        public override void MigrateToKeePassOTP(bool bRemove, out int EntriesOverall, out int EntriesMigrated)
        {
            EntriesOverall = EntriesMigrated = -1;
            if (!m_bInitialized)
            {
                return;
            }
            EntriesOverall = EntriesMigrated = 0;

            List <PwEntry> lEntries = m_db.RootGroup.GetEntries(true).Where(x => x.Strings.Exists("TOTP Seed")).ToList();

            EntriesOverall = lEntries.Count;
            if (lEntries.Count == 0)
            {
                return;
            }

            if (!OTPDAO.EnsureOTPSetupPossible(lEntries[0]))
            {
                return;
            }
            OTPDAO.OTPHandler_Base handler = OTPDAO.GetOTPHandler(lEntries[0]);
            InitLogger("KeeTrayTOTP -> KeePassOTP", lEntries.Count);
            try
            {
                foreach (PwEntry pe in lEntries)
                {
                    IncreaseLogger();
                    string seed     = pe.Strings.ReadSafe("TOTP Seed");
                    string settings = pe.Strings.ReadSafe("TOTP Settings");
                    if (string.IsNullOrEmpty(settings))
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: not defined");
                        continue;
                    }
                    var parameters = settings.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parameters.Count() < 2)
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + settings);
                        continue;
                    }
                    var otp = OTPDAO.GetOTP(pe);
                    otp.OTPSeed      = new ProtectedString(true, MigrateString(seed));
                    otp.TOTPTimestep = MigrateInt(parameters[0], 30);
                    int l = MigrateInt(parameters[1], -1);
                    if (l == -1)
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + settings);
                        continue;
                    }
                    otp.Length = l;
                    if ((parameters.Count() > 2) && !string.IsNullOrEmpty(parameters[2]))
                    {
                        otp.TimeCorrectionUrl = parameters[2];
                    }
                    if (otp.Valid)
                    {
                        EntriesMigrated++;
                        try
                        {
                            handler.IgnoreBuffer = true;
                            OTPDAO.SaveOTP(otp, pe);
                        }
                        finally { handler.IgnoreBuffer = false; }
                        if (bRemove)
                        {
                            pe.Strings.Remove("TOTP Seed");
                            pe.Strings.Remove("TOTP Settings");
                        }
                    }
                    else
                    {
                        string s = string.Empty;
                        for (int i = 0; i < parameters.Count(); i++)
                        {
                            if (parameters[i].ToLowerInvariant().StartsWith("key="))
                            {
                                s += "key=<secret>";
                            }
                            else
                            {
                                s += parameters[i];
                            }
                            if (i < parameters.Count() - 1)
                            {
                                s += "&";
                            }
                        }
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + s);
                    }
                }
            }
            finally
            {
                EndLogger();
            }
            MigratePlaceholder(OtherPluginPlaceholder, Config.Placeholder, false);
        }
Esempio n. 3
0
        private void MigrateToKeePassOTP_Totp(bool bRemove, out int EntriesOverall, out int EntriesMigrated)
        {
            EntriesOverall = EntriesMigrated = 0;
            Dictionary <PwEntry, KPOTPEncoding> dEntries = new Dictionary <PwEntry, KPOTPEncoding>();

            foreach (KeyValuePair <KPOTPEncoding, string> kvp in m_dTotpStrings)
            {
                List <PwEntry> lHelp = m_db.RootGroup.GetEntries(true).Where(x => x.Strings.Exists(kvp.Value)).ToList();
                foreach (PwEntry pe in lHelp)
                {
                    if (!dEntries.ContainsKey(pe))
                    {
                        dEntries[pe] = kvp.Key;
                    }
                }
            }
            EntriesOverall = dEntries.Count;
            if (dEntries.Count == 0)
            {
                return;
            }

            if (!OTPDAO.EnsureOTPSetupPossible(dEntries.Keys.First()))
            {
                return;
            }
            OTPDAO.OTPHandler_Base handler = OTPDAO.GetOTPHandler(dEntries.Keys.First());
            InitLogger("KeePass -> KeePassOTP (TOTP)", dEntries.Count);
            try
            {
                foreach (KeyValuePair <PwEntry, KPOTPEncoding> kvp in dEntries)
                {
                    IncreaseLogger();
                    KPOTPEncoding enc = kvp.Value;
                    PwEntry       pe  = kvp.Key;

                    var otp = OTPDAO.GetOTP(pe);
                    otp.Encoding = enc;
                    otp.OTPSeed  = new ProtectedString(true, MigrateString(pe.Strings.ReadSafe(m_dTotpStrings[enc])));

                    string hash = pe.Strings.ReadSafe(TOTPHASH).ToLowerInvariant();
                    if (hash.Contains("sha-512"))
                    {
                        otp.Hash = KPOTPHash.SHA512;
                    }
                    else if (hash.Contains("sha-256"))
                    {
                        otp.Hash = KPOTPHash.SHA256;
                    }
                    else
                    {
                        otp.Hash = KPOTPHash.SHA1;
                    }

                    otp.Length       = MigrateInt(pe.Strings.ReadSafe(TOTPLENGTH), 6);
                    otp.TOTPTimestep = MigrateInt(pe.Strings.ReadSafe(TOTPPERIOD), 30);

                    otp.HOTPCounter = MigrateInt(pe.Strings.ReadSafe(HOTP_COUNTER), 0);
                    if (otp.Valid)
                    {
                        EntriesMigrated++;
                        try
                        {
                            handler.IgnoreBuffer = true;
                            OTPDAO.SaveOTP(otp, pe);
                        }
                        finally { handler.IgnoreBuffer = false; }
                        if (bRemove)
                        {
                            pe.Strings.Remove(m_dTotpStrings[enc]);
                            pe.Strings.Remove(TOTPHASH);
                            pe.Strings.Remove(TOTPLENGTH);
                            pe.Strings.Remove(TOTPPERIOD);
                        }
                    }
                    else
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + m_dHotpStrings[enc]);
                    }
                }
            }
            finally
            {
                EndLogger();
            }
            MigratePlaceholder(PLACEHOLDER_TOTP, Config.Placeholder);
        }
Esempio n. 4
0
        public override void MigrateToKeePassOTP(bool bRemove, out int EntriesOverall, out int EntriesMigrated)
        {
            EntriesOverall = EntriesMigrated = -1;
            if (!m_bInitialized)
            {
                return;
            }
            EntriesOverall = EntriesMigrated = 0;

            List <PwEntry> lEntries = m_db.RootGroup.GetEntries(true).Where(x => x.Strings.Exists("otp")).ToList();

            EntriesOverall = lEntries.Count;
            if (lEntries.Count == 0)
            {
                return;
            }

            if (!OTPDAO.EnsureOTPSetupPossible(lEntries[0]))
            {
                return;
            }
            OTPDAO.OTPHandler_Base handler = OTPDAO.GetOTPHandler(lEntries[0]);
            InitLogger("KeeOTP -> KeePassOTP", lEntries.Count);
            try
            {
                foreach (PwEntry pe in lEntries)
                {
                    IncreaseLogger();
                    string old = pe.Strings.ReadSafe("otp");
                    if (string.IsNullOrEmpty(old))
                    {
                        continue;
                    }
                    var parameters = old.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                    var otp        = OTPDAO.GetOTP(pe);
                    foreach (var parameter in parameters)
                    {
                        var kvp = parameter.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        if (kvp.Length != 2)
                        {
                            continue;
                        }
                        switch (kvp[0].ToLowerInvariant())
                        {
                        case "key":
                            otp.OTPSeed = new ProtectedString(true, MigrateString(kvp[1]));
                            break;

                        case "type":
                            otp.Type = kvp[1].ToLower() != "hotp" ? KPOTPType.TOTP : KPOTPType.HOTP;
                            break;

                        case "step":
                            otp.TOTPTimestep = MigrateInt(kvp[1], 30);
                            break;

                        case "counter":
                            otp.HOTPCounter = MigrateInt(kvp[1], 0);
                            break;

                        case "size":
                            otp.Length = MigrateInt(kvp[1], 6);
                            break;

                        default: break;
                        }
                    }
                    if (otp.Valid)
                    {
                        EntriesMigrated++;
                        try
                        {
                            handler.IgnoreBuffer = true;
                            OTPDAO.SaveOTP(otp, pe);
                        }
                        finally { handler.IgnoreBuffer = false; }
                        pe.Touch(true);
                        //Only remove setting if we migrated to OTP-DB
                        //Do not remove if KeePassOTP stores OTP data within the entry
                        if (bRemove && Config.UseDBForOTPSeeds(pe.GetDB()))
                        {
                            pe.Strings.Remove("otp");
                        }
                    }
                    else
                    {
                        string s = string.Empty;
                        for (int i = 0; i < parameters.Count(); i++)
                        {
                            if (parameters[i].ToLowerInvariant().StartsWith("key="))
                            {
                                s += "key=<secret>";
                            }
                            else
                            {
                                s += parameters[i];
                            }
                            if (i < parameters.Count() - 1)
                            {
                                s += "&";
                            }
                        }
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + s);
                    }
                }
            }
            finally { EndLogger(); }
            MigratePlaceholder(OtherPluginPlaceholder, Config.Placeholder, false);
        }
Esempio n. 5
0
        private void MigrateToKeePassOTP_Hotp(bool bRemove, out int EntriesOverall, out int EntriesMigrated)
        {
            EntriesOverall = EntriesMigrated = 0;
            List <PwEntry> lEntries = m_db.RootGroup.GetEntries(true).Where(x => x.Strings.Exists(HOTP_COUNTER)).ToList();

            EntriesOverall = lEntries.Count;
            if (lEntries.Count == 0)
            {
                return;
            }

            if (!OTPDAO.EnsureOTPSetupPossible(lEntries[0]))
            {
                return;
            }
            OTPDAO.OTPHandler_Base handler = OTPDAO.GetOTPHandler(lEntries[0]);
            InitLogger("KeePass -> KeePassOTP (HOTP)", lEntries.Count);
            try
            {
                foreach (PwEntry pe in lEntries)
                {
                    IncreaseLogger();
                    KPOTPEncoding enc    = KPOTPEncoding.BASE32;
                    bool          bFound = false;
                    string        seed   = null;
                    foreach (KeyValuePair <KPOTPEncoding, string> kvp in m_dHotpStrings)
                    {
                        if (pe.Strings.Exists(kvp.Value))
                        {
                            enc    = kvp.Key;
                            seed   = pe.Strings.ReadSafe(kvp.Value);
                            bFound = true;
                            break;
                        }
                    }
                    if (!bFound)
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: not defined");
                        continue;
                    }
                    var otp = OTPDAO.GetOTP(pe);
                    otp.Type        = KPOTPType.HOTP;
                    otp.Encoding    = enc;
                    otp.OTPSeed     = new ProtectedString(true, MigrateString(seed));
                    otp.HOTPCounter = MigrateInt(pe.Strings.ReadSafe(HOTP_COUNTER), 0);
                    if (otp.Valid)
                    {
                        EntriesMigrated++;
                        try
                        {
                            handler.IgnoreBuffer = true;
                            OTPDAO.SaveOTP(otp, pe);
                        }
                        finally { handler.IgnoreBuffer = false; }
                        if (bRemove)
                        {
                            pe.Strings.Remove(m_dHotpStrings[enc]);
                            pe.Strings.Remove(HOTP_COUNTER);
                        }
                    }
                    else
                    {
                        PluginDebug.AddError("Migration of entry failed",
                                             "Uuid: " + pe.Uuid.ToHexString(),
                                             "OTP data: " + m_dHotpStrings[enc]);
                    }
                }
            }
            finally
            {
                EndLogger();
            }
            MigratePlaceholder(PLACEHOLDER_HOTP, Config.Placeholder);
        }