Example #1
0
        private static Kdb3ErrorCode SetDatabaseKey(Kdb3Manager mgr, CompositeKey pwKey)
        {
            Kdb3ErrorCode e;

            bool bPassword = pwKey.ContainsType(typeof(KcpPassword));
            bool bKeyFile  = pwKey.ContainsType(typeof(KcpKeyFile));

            string strPassword = (bPassword ? (pwKey.GetUserKey(
                                                   typeof(KcpPassword)) as KcpPassword).Password.ReadString() : string.Empty);
            string strKeyFile = (bKeyFile ? (pwKey.GetUserKey(
                                                 typeof(KcpKeyFile)) as KcpKeyFile).Path : string.Empty);

            if (bPassword && bKeyFile)
            {
                e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false);
            }
            else if (bPassword && !bKeyFile)
            {
                e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false);
            }
            else if (!bPassword && bKeyFile)
            {
                e = mgr.SetMasterKey(strKeyFile, true, null, IntPtr.Zero, false);
            }
            else
            {
                Debug.Assert(false); throw new Exception(KLRes.InvalidCompositeKey);
            }

            return(e);
        }
Example #2
0
        private static KdbErrorCode SetDatabaseKey(KdbManager mgr, CompositeKey pwKey)
        {
            string strPassword = null;

            if (pwKey.ContainsType(typeof(KcpPassword)))
            {
                KcpPassword     p  = (pwKey.GetUserKey(typeof(KcpPassword)) as KcpPassword);
                ProtectedString ps = ((p != null) ? p.Password : null);
                if (ps == null)
                {
                    throw new Exception(KPRes.OptionReqOn + @" '" +
                                        KPRes.MasterPasswordRmbWhileOpen + @"'.");
                }
                strPassword = ps.ReadString();
            }

            string strKeyFile = null;

            if (pwKey.ContainsType(typeof(KcpKeyFile)))
            {
                strKeyFile = (pwKey.GetUserKey(typeof(KcpKeyFile)) as KcpKeyFile).Path;
            }

            KdbErrorCode e;

            if (!string.IsNullOrEmpty(strKeyFile))
            {
                e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false);
            }
            else if (strPassword != null)
            {
                e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false);
            }
            else if (pwKey.ContainsType(typeof(KcpUserAccount)))
            {
                throw new Exception(KPRes.KdbWUA);
            }
            else
            {
                throw new Exception(KLRes.InvalidCompositeKey);
            }

            return(e);
        }
Example #3
0
        private void UpdateImportKeyfilePref()
        {
            var prefs           = PreferenceManager.GetDefaultSharedPreferences(Activity);
            var rememberKeyfile = prefs.GetBoolean(GetString(Resource.String.keyfile_key), Resources.GetBoolean(Resource.Boolean.keyfile_default));

            Preference importKeyfile = FindPreference("import_keyfile_prefs");
            Preference exportKeyfile = FindPreference("export_keyfile_prefs");

            importKeyfile.Summary = "";

            if (!rememberKeyfile)
            {
                importKeyfile.Summary = GetString(Resource.String.KeyfileMoveRequiresRememberKeyfile);
                importKeyfile.Enabled = false;
                exportKeyfile.Enabled = false;
                return;
            }
            CompositeKey masterKey = App.Kp2a.CurrentDb.KpDatabase.MasterKey;

            if (masterKey.ContainsType(typeof(KcpKeyFile)))
            {
                IOConnectionInfo iocKeyfile = ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).Ioc;
                if (iocKeyfile.IsLocalFile() && IoUtil.IsInInternalDirectory(iocKeyfile.Path, Activity))
                {
                    importKeyfile.Enabled          = false;
                    exportKeyfile.Enabled          = true;
                    exportKeyfile.PreferenceClick += (sender, args) => { ExportKeyfileFromInternalFolder(); };
                    importKeyfile.Summary          = GetString(Resource.String.FileIsInInternalDirectory);
                }
                else
                {
                    exportKeyfile.Enabled          = false;
                    importKeyfile.Enabled          = true;
                    importKeyfile.PreferenceClick += (sender, args) => { MoveKeyfileToInternalFolder(); };
                }
            }
            else
            {
                exportKeyfile.Enabled = false;
                importKeyfile.Enabled = false;
            }
        }