AddUserKey() 공개 메소드

/// Deconstructor, clears up the key. /// /// Clears the key. This function also erases all previously stored /// user key data objects. /// Add a user key.
public AddUserKey ( IUserKey pKey ) : void
pKey IUserKey User key to add.
리턴 void
예제 #1
0
    public static void Main(string[] args)
    {
        CompositeKey key = new CompositeKey();
        KcpPassword pw = new KcpPassword("12345");
        key.AddUserKey(pw);
        byte[] pwdata = pw.KeyData.ReadData();
        Console.WriteLine("PW data:");
        Console.WriteLine(string.Join(",", pwdata.Select(x => "0x" + x.ToString("x"))));
        byte[] keydata = key.GenerateKey32(pwdata, 6000).ReadData();
        Console.WriteLine("Key data:");
        Console.WriteLine(string.Join(",", keydata.Select(x => "0x" + x.ToString("x"))));

        PwDatabase db = new PwDatabase();
        db.MasterKey = key;
        KdbxFile kdbx = new KdbxFile(db);
        kdbx.Load(@"..\resources\test.kdbx", KdbxFormat.Default, null);

        var groups = db.RootGroup.GetGroups(true);
        Console.WriteLine("Group count: " + groups.UCount);
        var entries = db.RootGroup.GetEntries(true);
        Console.WriteLine("Entry count: " + entries.UCount);

        CompositeKey key2 = new CompositeKey();
        key2.AddUserKey(pw);
        KcpKeyFile keyfile = new KcpKeyFile(@"..\resources\keyfile.key");
        key2.AddUserKey(keyfile);
        byte[] keyfiledata = keyfile.KeyData.ReadData();
        Console.WriteLine("Key file data:");
        Console.WriteLine(string.Join(",", keyfiledata.Select(x => "0x" + x.ToString("x"))));
        Console.WriteLine("Composite Key data:");
        byte[] key2data = key2.GenerateKey32(keyfiledata, 6000).ReadData();
        Console.WriteLine(string.Join(",", key2data.Select(x => "0x" + x.ToString("x"))));
    }
예제 #2
0
        public static CompositeKey KeyFromCommandLine(CommandLineArgs args)
        {
            if(args == null) throw new ArgumentNullException("args");

            CompositeKey cmpKey = new CompositeKey();
            string strPassword = args[AppDefs.CommandLineOptions.Password];
            string strKeyFile = args[AppDefs.CommandLineOptions.KeyFile];
            string strUserAcc = args[AppDefs.CommandLineOptions.UserAccount];

            if(strPassword != null)
                cmpKey.AddUserKey(new KcpPassword(strPassword));

            if(strKeyFile != null)
            {
                try { cmpKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
                catch(Exception exKey)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exKey);
                    return null;
                }
            }

            if(strUserAcc != null)
            {
                try { cmpKey.AddUserKey(new KcpUserAccount()); }
                catch(Exception exUA)
                {
                    MessageService.ShowWarning(exUA);
                    return null;
                }
            }

            return ((cmpKey.UserKeyCount > 0) ? cmpKey : null);
        }
예제 #3
0
파일: TestBase.cs 프로젝트: pythe/wristpass
 protected static CompositeKey CreateKey(string password, string keyfile)
 {
     CompositeKey key = new CompositeKey();
     key.AddUserKey(new KcpPassword(password));
     if (!String.IsNullOrEmpty(keyfile))
         key.AddUserKey(new KcpKeyFile(keyfile));
     return key;
 }
예제 #4
0
        public static CompositeKey CreateCompositeKey(string keyPassword = null, string keyFile = null, bool userAccount = false)
        {
            var emptyPassword = string.IsNullOrWhiteSpace(keyPassword);
            var emptyKeyFile = string.IsNullOrWhiteSpace(keyFile);

            if(emptyPassword && emptyKeyFile && !userAccount)
                throw new ArgumentException("KeyPass requires at least one form of authentication: Password, KeyFile, or UserAccount");

            var key = new CompositeKey();

            if (!emptyPassword)
                key.AddUserKey(new KcpPassword(keyPassword));

            if (!emptyKeyFile)
                key.AddUserKey(new KcpKeyFile(keyFile));

            if (userAccount)
                key.AddUserKey(new KcpUserAccount());

            return key;
        }
예제 #5
0
파일: KeyUtil.cs 프로젝트: Stoom/KeePass
        public static CompositeKey KeyFromCommandLine(CommandLineArgs args)
        {
            if(args == null) throw new ArgumentNullException("args");

            CompositeKey cmpKey = new CompositeKey();
            string strPassword = args[AppDefs.CommandLineOptions.Password];
            string strPasswordEnc = args[AppDefs.CommandLineOptions.PasswordEncrypted];
            string strPasswordStdIn = args[AppDefs.CommandLineOptions.PasswordStdIn];
            string strKeyFile = args[AppDefs.CommandLineOptions.KeyFile];
            string strUserAcc = args[AppDefs.CommandLineOptions.UserAccount];

            if(strPassword != null)
                cmpKey.AddUserKey(new KcpPassword(strPassword));
            else if(strPasswordEnc != null)
                cmpKey.AddUserKey(new KcpPassword(StrUtil.DecryptString(strPasswordEnc)));
            else if(strPasswordStdIn != null)
            {
                KcpPassword kcpPw = ReadPasswordStdIn(true);
                if(kcpPw != null) cmpKey.AddUserKey(kcpPw);
            }

            if(strKeyFile != null)
            {
                if(Program.KeyProviderPool.IsKeyProvider(strKeyFile))
                {
                    KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
                        IOConnectionInfo.FromPath(args.FileName), false, false);

                    bool bPerformHash;
                    byte[] pbProvKey = Program.KeyProviderPool.GetKey(strKeyFile, ctxKP,
                        out bPerformHash);
                    if((pbProvKey != null) && (pbProvKey.Length > 0))
                    {
                        try { cmpKey.AddUserKey(new KcpCustomKey(strKeyFile, pbProvKey, bPerformHash)); }
                        catch(Exception exCKP)
                        {
                            MessageService.ShowWarning(exCKP);
                            return null;
                        }

                        Array.Clear(pbProvKey, 0, pbProvKey.Length);
                    }
                    else return null; // Provider has shown error message
                }
                else // Key file
                {
                    try { cmpKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
                    catch(Exception exKey)
                    {
                        MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exKey);
                        return null;
                    }
                }
            }

            if(strUserAcc != null)
            {
                try { cmpKey.AddUserKey(new KcpUserAccount()); }
                catch(Exception exUA)
                {
                    MessageService.ShowWarning(exUA);
                    return null;
                }
            }

            if(cmpKey.UserKeyCount > 0)
            {
                ClearKeyOptions(args, true);
                return cmpKey;
            }

            return null;
        }
예제 #6
0
 public static CompositeKey CreateKeyFor(string password)
 {
     CompositeKey key = new CompositeKey();
     key.AddUserKey(new KcpPassword(password));
     return key;
 }
예제 #7
0
        private void CreateDatabase()
        {
            var keyfileCheckbox = FindViewById<CheckBox>(Resource.Id.use_keyfile);
            string password;
            if (!TryGetPassword(out password)) return;

            // Verify that a password or keyfile is set
            if (password.Length == 0 && !keyfileCheckbox.Checked)
            {
                Toast.MakeText(this, Resource.String.error_nopass, ToastLength.Long).Show();
                return;
            }

            //create the key
            CompositeKey newKey = new CompositeKey();
            if (String.IsNullOrEmpty(password) == false)
            {
                newKey.AddUserKey(new KcpPassword(password));
            }
            if (String.IsNullOrEmpty(_keyfileFilename) == false)
            {
                try
                {
                    newKey.AddUserKey(new KcpKeyFile(_keyfileFilename));
                }
                catch (Exception)
                {
                    Toast.MakeText(this, Resource.String.error_adding_keyfile, ToastLength.Long).Show();
                    return;
                }
            }

            // Create the new database
            CreateDb create = new CreateDb(App.Kp2a, this, _ioc, new LaunchGroupActivity(_ioc, this), false, newKey);
            ProgressTask createTask = new ProgressTask(
                App.Kp2a,
                this, create);
            createTask.Run();
        }
예제 #8
0
        private bool CreateCompositeKey()
        {
            m_pKey = new CompositeKey();

            if(m_cbPassword.Checked) // Use a password
            {
                byte[] pb = m_secPassword.ToUtf8();
                m_pKey.AddUserKey(new KcpPassword(pb));
                Array.Clear(pb, 0, pb.Length);
            }

            string strKeyFile = m_cmbKeyFile.Text;
            Debug.Assert(strKeyFile != null); if(strKeyFile == null) strKeyFile = string.Empty;
            bool bIsProvKey = Program.KeyProviderPool.IsKeyProvider(strKeyFile);

            if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                (bIsProvKey == false))
            {
                if(ValidateKeyFileLocation() == false)
                {
                    m_pKey = null;
                    return false;
                }

                try { m_pKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
                catch(Exception)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError);
                    m_pKey = null;
                    return false;
                }
            }
            else if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                (bIsProvKey == true))
            {
                byte[] pbProvKey = Program.KeyProviderPool.GetKey(strKeyFile);

                try { m_pKey.AddUserKey(new KcpCustomKey(pbProvKey)); }
                catch(Exception exCKP)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exCKP);
                    m_pKey = null;
                    return false;
                }

                if((pbProvKey != null) && (pbProvKey.Length > 0))
                    Array.Clear(pbProvKey, 0, pbProvKey.Length);
            }

            if(m_cbUserAccount.Checked)
            {
                try { m_pKey.AddUserKey(new KcpUserAccount()); }
                catch(Exception exUA)
                {
                    MessageService.ShowWarning(exUA);
                    return false;
                }
            }

            return true;
        }
예제 #9
0
        private CompositeKey CreateCompositeKey(string masterKey)
        {
            CompositeKey m_pKey = new CompositeKey ();

             SecureEdit se = new SecureEdit (masterKey);
             byte[] pb = se.ToUtf8 ();
             m_pKey.AddUserKey (new KcpPassword (pb));
             Array.Clear (pb, 0, pb.Length);

             return m_pKey;
        }
예제 #10
0
        private bool CreateCompositeKey()
        {
            m_pKey = new CompositeKey();

            if(m_cbPassword.Checked) // Use a password
            {
                if(m_cbHidePassword.Checked)
                {
                    if(m_secPassword.ContentsEqualTo(m_secRepeat) == false)
                    {
                        MessageService.ShowWarning(KPRes.PasswordRepeatFailed);
                        return false;
                    }
                }

                uint uMinLen = Program.Config.Security.MasterPassword.MinimumLength;
                if(m_secPassword.TextLength < uMinLen)
                {
                    string strML = KPRes.MasterPasswordMinLengthFailed;
                    strML = strML.Replace(@"{PARAM}", uMinLen.ToString());
                    MessageService.ShowWarning(strML);
                    return false;
                }

                byte[] pb = m_secPassword.ToUtf8();

                uint uMinQual = Program.Config.Security.MasterPassword.MinimumQuality;
                if(QualityEstimation.EstimatePasswordBits(pb) < uMinQual)
                {
                    string strMQ = KPRes.MasterPasswordMinQualityFailed;
                    strMQ = strMQ.Replace(@"{PARAM}", uMinQual.ToString());
                    MessageService.ShowWarning(strMQ);
                    Array.Clear(pb, 0, pb.Length);
                    return false;
                }

                m_pKey.AddUserKey(new KcpPassword(pb));
                Array.Clear(pb, 0, pb.Length);
            }

            string strKeyFile = m_cmbKeyFile.Text;
            bool bIsKeyProv = Program.KeyProviderPool.IsKeyProvider(strKeyFile);

            if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                (bIsKeyProv == false))
            {
                try { m_pKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
                catch(Exception exKF)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exKF);
                    return false;
                }
            }
            else if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                (bIsKeyProv == true))
            {
                byte[] pbCustomKey = Program.KeyProviderPool.GetKey(strKeyFile);

                try { m_pKey.AddUserKey(new KcpCustomKey(pbCustomKey)); }
                catch(Exception exCKP)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exCKP);
                    return false;
                }

                if((pbCustomKey != null) && (pbCustomKey.Length > 0))
                    Array.Clear(pbCustomKey, 0, pbCustomKey.Length);
            }

            if(m_cbUserAccount.Checked)
            {
                try { m_pKey.AddUserKey(new KcpUserAccount()); }
                catch(Exception exUA)
                {
                    MessageService.ShowWarning(exUA);
                    return false;
                }
            }

            return true;
        }
예제 #11
0
파일: TestBase.cs 프로젝트: pythe/wristpass
        protected TestKp2aApp SetupAppWithDatabase(string filename)
        {
            TestKp2aApp app = CreateTestKp2aApp();

            IOConnectionInfo ioc = new IOConnectionInfo {Path = filename};
            Database db = app.CreateNewDatabase();
            if (filename.EndsWith(".kdb"))
            {
                db.DatabaseFormat = new KdbDatabaseFormat(app);
            }

            db.KpDatabase = new PwDatabase();

            CompositeKey compositeKey = new CompositeKey();
            compositeKey.AddUserKey(new KcpPassword(DefaultPassword));
            if (!String.IsNullOrEmpty(DefaultKeyfile))
                compositeKey.AddUserKey(new KcpKeyFile(DefaultKeyfile));
            db.KpDatabase.New(ioc, compositeKey);

            db.KpDatabase.KeyEncryptionRounds = 3;
            db.KpDatabase.Name = "Keepass2Android Testing Password Database";

            // Set Database state
            db.Root = db.KpDatabase.RootGroup;
            db.Loaded = true;
            db.SearchHelper = new SearchDbHelper(app);

            // Add a couple default groups
            db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Internet", PwIcon.Key), true);

            mailGroup = new PwGroup(true, true, "eMail", PwIcon.UserCommunication);
            db.KpDatabase.RootGroup.AddGroup(mailGroup, true);

            mailGroup.AddGroup(new PwGroup(true, true, "business", PwIcon.BlackBerry), true);

            mailEntry = new PwEntry(true, true);
            mailEntry.Strings.Set(PwDefs.UserNameField, new ProtectedString(
                                                            true, "*****@*****.**"));
            mailEntry.Strings.Set(PwDefs.TitleField, new ProtectedString(
                                                         true, "[email protected] Entry"));
            mailGroup.AddEntry(mailEntry, true);

            db.KpDatabase.RootGroup.AddGroup(new PwGroup(true, true, "eMail2", PwIcon.UserCommunication), true);

            return app;
        }
예제 #12
0
        private bool CreateCompositeKey(out CompositeKey compositeKey, out string errorMessage)
        {
            errorMessage = null;
            //no need to check for validity of password because if this method is called, the Ok button was enabled (i.e. there was a valid password)
            compositeKey = new CompositeKey();
            compositeKey.AddUserKey(new KcpPassword(_password));
            if (KeyProviderType == KeyProviders.KeyFile)
            {
                try
                {
                    if (_keyFileOrProvider == "")
                        throw new System.IO.FileNotFoundException();
                    var ioc = IOConnectionInfo.UnserializeFromString(_keyFileOrProvider);
                    using (var stream = App.Kp2a.GetFileStorage(ioc).OpenFileForRead(ioc))
                    {
                        byte[] keyfileData = StreamToMemoryStream(stream).ToArray();
                        compositeKey.AddUserKey(new KcpKeyFile(keyfileData, ioc, true));
                    }
                }
                catch (System.IO.FileNotFoundException e)
                {
                    Kp2aLog.Log(e.ToString());
                    errorMessage = App.Kp2a.GetResourceString(UiStringKey.keyfile_does_not_exist);
                    return false;
                }
                catch (Exception e)
                {
                    Kp2aLog.Log(e.ToString());
                    errorMessage = e.Message;
                    return false;
                }
            }
            else if (KeyProviderType == KeyProviders.Otp)
            {
                try
                {
                    var lOtps = GetOtpsFromUi();
                    Kp2aLog.Log("received " + lOtps.Count + " otps.");
                    OathHotpKeyProv.CreateOtpSecret(lOtps, _otpInfo);
                }
                catch (Exception e)
                {
                    Kp2aLog.Log(e.ToString());
                    errorMessage = GetString(Resource.String.OtpKeyError);

                    return false;
                }
                compositeKey.AddUserKey(new KcpCustomKey(OathHotpKeyProv.Name, _otpInfo.Secret, true));
            }
            else if ((KeyProviderType == KeyProviders.OtpRecovery) || (KeyProviderType == KeyProviders.ChalRecovery))
            {
                Spinner stpDataFmtSpinner = FindViewById<Spinner>(Resource.Id.otpsecret_format_spinner);
                EditText secretEdit = FindViewById<EditText>(Resource.Id.pass_otpsecret);

                byte[] pbSecret = EncodingUtil.ParseKey(secretEdit.Text, (OtpDataFmt) stpDataFmtSpinner.SelectedItemPosition);
                if (pbSecret != null)
                {
                    compositeKey.AddUserKey(new KcpCustomKey(OathHotpKeyProv.Name, pbSecret, true));
                }
                else
                {
                    errorMessage = GetString(Resource.String.CouldntParseOtpSecret);
                    return false;
                }
            }
            else if (KeyProviderType == KeyProviders.Challenge)
            {
                compositeKey.AddUserKey(new KcpCustomKey(KeeChallengeProv.Name, _challengeSecret, true));
            }
            return true;
        }
예제 #13
0
        private bool CreateCompositeKey()
        {
            m_pKey = new CompositeKey();

            if(m_secPassword.ContentsEqualTo(m_secRepeat) == false)
            {
                MessageService.ShowWarning(KPRes.PasswordRepeatFailed);
                return false;
            }

            if(m_secPassword.TextLength == 0)
            {
                if(!MessageService.AskYesNo(KPRes.EmptyMasterPw +
                    MessageService.NewParagraph + KPRes.EmptyMasterPwHint +
                    MessageService.NewParagraph + KPRes.EmptyMasterPwQuestion,
                    null, false))
                {
                    return false;
                }
            }

            uint uMinLen = Program.Config.Security.MasterPassword.MinimumLength;
            if(m_secPassword.TextLength < uMinLen)
            {
                string strML = KPRes.MasterPasswordMinLengthFailed;
                strML = strML.Replace(@"{PARAM}", uMinLen.ToString());
                MessageService.ShowWarning(strML);
                return false;
            }

            byte[] pb = m_secPassword.ToUtf8();

            uint uMinQual = Program.Config.Security.MasterPassword.MinimumQuality;
            if(QualityEstimation.EstimatePasswordBits(pb) < uMinQual)
            {
                string strMQ = KPRes.MasterPasswordMinQualityFailed;
                strMQ = strMQ.Replace(@"{PARAM}", uMinQual.ToString());
                MessageService.ShowWarning(strMQ);
                Array.Clear(pb, 0, pb.Length);
                return false;
            }

            string strValRes = Program.KeyValidatorPool.Validate(pb,
                KeyValidationType.MasterPassword);
            if(strValRes != null)
            {
                MessageService.ShowWarning(strValRes);
                Array.Clear(pb, 0, pb.Length);
                return false;
            }

            m_pKey.AddUserKey(new KcpPassword(pb));
            Array.Clear(pb, 0, pb.Length);

            return true;
        }
예제 #14
0
        private bool CreateCompositeKey()
        {
            m_pKey = new CompositeKey();

            if(m_cbPassword.Checked && m_tbPassword.Text != "") // Use a password
            {
                m_pKey.AddUserKey(new KcpPassword(m_tbPassword.Text));
            }

            if(m_cbKeyFile.Checked && m_keyFileBase64.Text != "") // Use the base64 representation of a keyfile
            {
                byte[] pb = Convert.FromBase64String(m_keyFileBase64.Text);
                m_pKey.AddUserKey(new KcpKeyFile(pb));
                Array.Clear(pb, 0, pb.Length);
            }

            if (m_cbUserAccount.Checked && m_userBase64.Text != "") // Use the base64 representation of a Windows User Account
            {
                byte[] pb = Convert.FromBase64String(m_userBase64.Text);

                //MessageBox.Show(m_userBase64.Text, "m_userBase64.Text", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                //StringBuilder hex = new StringBuilder(pb.Length * 2);
                //foreach (byte b in pb)
                //    hex.AppendFormat("{0:x2}", b);
                //MessageBox.Show(hex.ToString(), "m_userBase64 hex", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

                m_pKey.AddUserKey(new KcpUserAccount(pb));
                Array.Clear(pb, 0, pb.Length);
            }

            //string strKeyFile = m_cmbKeyFile.Text;
            //Debug.Assert(strKeyFile != null); if(strKeyFile == null) strKeyFile = string.Empty;
            //bool bIsProvKey = Program.KeyProviderPool.IsKeyProvider(strKeyFile);

            //if(m_cbKeyFile.Checked && !strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta) &&
            //	!bIsProvKey)
            //{
            //	if(!ValidateKeyFile()) return false;

            //	try { m_pKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
            //	catch(Exception)
            //	{
            //		MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError);
            //		return false;
            //	}
            //}
            //else if(m_cbKeyFile.Checked && !strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta) &&
            //	bIsProvKey)
            //{
            //	KeyProvider kp = Program.KeyProviderPool.Get(strKeyFile);
            //	if((kp != null) && m_bSecureDesktop)
            //	{
            //		if(!kp.SecureDesktopCompatible)
            //		{
            //			MessageService.ShowWarning(KPRes.KeyProvIncmpWithSD,
            //				KPRes.KeyProvIncmpWithSDHint);
            //			return false;
            //		}
            //	}

            //	KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
            //		m_ioInfo, false, m_bSecureDesktop);

            //	bool bPerformHash;
            //	byte[] pbProvKey = Program.KeyProviderPool.GetKey(strKeyFile, ctxKP,
            //		out bPerformHash);
            //	if((pbProvKey != null) && (pbProvKey.Length > 0))
            //	{
            //		try { m_pKey.AddUserKey(new KcpCustomKey(strKeyFile, pbProvKey, bPerformHash)); }
            //		catch(Exception exCKP)
            //		{
            //			MessageService.ShowWarning(exCKP);
            //			return false;
            //		}

            //		Array.Clear(pbProvKey, 0, pbProvKey.Length);
            //	}
            //	else return false; // Provider has shown error message
            //}

               //         if (m_cbUserAccount.Checked)
            //{
            //	try { m_pKey.AddUserKey(new KcpUserAccount()); }
            //	catch(Exception exUA)
            //	{
            //		MessageService.ShowWarning(exUA);
            //		return false;
            //	}
            //}

            return true;
        }
예제 #15
0
		private bool CreateCompositeKey()
		{
			m_pKey = new CompositeKey();

			if(m_cbPassword.Checked) // Use a password
			{
				byte[] pb = m_secPassword.ToUtf8();
				m_pKey.AddUserKey(new KcpPassword(pb));
				MemUtil.ZeroByteArray(pb);
			}

			string strKeyFile = m_cmbKeyFile.Text;
			Debug.Assert(strKeyFile != null); if(strKeyFile == null) strKeyFile = string.Empty;
			bool bIsProvKey = Program.KeyProviderPool.IsKeyProvider(strKeyFile);

			if(m_cbKeyFile.Checked && !strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta) &&
				!bIsProvKey)
			{
				if(!ValidateKeyFile()) return false;

				try { m_pKey.AddUserKey(new KcpKeyFile(strKeyFile)); }
				catch(Exception)
				{
					MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError);
					return false;
				}
			}
			else if(m_cbKeyFile.Checked && !strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta) &&
				bIsProvKey)
			{
				KeyProvider kp = Program.KeyProviderPool.Get(strKeyFile);
				if((kp != null) && m_bSecureDesktop)
				{
					if(!kp.SecureDesktopCompatible)
					{
						MessageService.ShowWarning(KPRes.KeyProvIncmpWithSD,
							KPRes.KeyProvIncmpWithSDHint);
						return false;
					}
				}

				KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
					m_ioInfo, false, m_bSecureDesktop);

				bool bPerformHash;
				byte[] pbProvKey = Program.KeyProviderPool.GetKey(strKeyFile, ctxKP,
					out bPerformHash);
				if((pbProvKey != null) && (pbProvKey.Length > 0))
				{
					try { m_pKey.AddUserKey(new KcpCustomKey(strKeyFile, pbProvKey, bPerformHash)); }
					catch(Exception exCKP)
					{
						MessageService.ShowWarning(exCKP);
						return false;
					}

					MemUtil.ZeroByteArray(pbProvKey);
				}
				else return false; // Provider has shown error message
			}

			if(m_cbUserAccount.Checked)
			{
				try { m_pKey.AddUserKey(new KcpUserAccount()); }
				catch(Exception exUA)
				{
					MessageService.ShowWarning(exUA);
					return false;
				}
			}

			return true;
		}
예제 #16
0
        private bool CreateCompositeKey()
        {
            m_pKey = new CompositeKey();

            if(m_cbPassword.Checked) // Use a password
            {
                if(!m_icgPassword.ValidateData(true)) return false;

                uint uPwLen = m_icgPassword.PasswordLength;
                if(uPwLen == 0)
                {
                    if(!MessageService.AskYesNo(KPRes.EmptyMasterPw +
                        MessageService.NewParagraph + KPRes.EmptyMasterPwHint +
                        MessageService.NewParagraph + KPRes.EmptyMasterPwQuestion,
                        null, false))
                    {
                        return false;
                    }
                }

                uint uMinLen = Program.Config.Security.MasterPassword.MinimumLength;
                if(uPwLen < uMinLen)
                {
                    string strML = KPRes.MasterPasswordMinLengthFailed;
                    strML = strML.Replace(@"{PARAM}", uMinLen.ToString());
                    MessageService.ShowWarning(strML);
                    return false;
                }

                byte[] pb = m_icgPassword.GetPasswordUtf8();

                uint uMinQual = Program.Config.Security.MasterPassword.MinimumQuality;
                if(QualityEstimation.EstimatePasswordBits(pb) < uMinQual)
                {
                    string strMQ = KPRes.MasterPasswordMinQualityFailed;
                    strMQ = strMQ.Replace(@"{PARAM}", uMinQual.ToString());
                    MessageService.ShowWarning(strMQ);
                    Array.Clear(pb, 0, pb.Length);
                    return false;
                }

                string strValRes = Program.KeyValidatorPool.Validate(pb,
                    KeyValidationType.MasterPassword);
                if(strValRes != null)
                {
                    MessageService.ShowWarning(strValRes);
                    Array.Clear(pb, 0, pb.Length);
                    return false;
                }

                m_pKey.AddUserKey(new KcpPassword(pb));
                Array.Clear(pb, 0, pb.Length);
            }

            string strKeyFile = m_cmbKeyFile.Text;
            bool bIsKeyProv = Program.KeyProviderPool.IsKeyProvider(strKeyFile);

            if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                !bIsKeyProv)
            {
                try { m_pKey.AddUserKey(new KcpKeyFile(strKeyFile, true)); }
                catch(InvalidDataException exID) // Selected database file
                {
                    MessageService.ShowWarning(strKeyFile, exID);
                    return false;
                }
                catch(Exception exKF)
                {
                    MessageService.ShowWarning(strKeyFile, KPRes.KeyFileError, exKF);
                    return false;
                }
            }
            else if(m_cbKeyFile.Checked && (!strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta)) &&
                bIsKeyProv)
            {
                KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(
                    m_ioInfo, true, false);

                bool bPerformHash;
                byte[] pbCustomKey = Program.KeyProviderPool.GetKey(strKeyFile, ctxKP,
                    out bPerformHash);
                if((pbCustomKey != null) && (pbCustomKey.Length > 0))
                {
                    try { m_pKey.AddUserKey(new KcpCustomKey(strKeyFile, pbCustomKey, bPerformHash)); }
                    catch(Exception exCKP)
                    {
                        MessageService.ShowWarning(exCKP);
                        return false;
                    }

                    Array.Clear(pbCustomKey, 0, pbCustomKey.Length);
                }
                else return false; // Provider has shown error message
            }

            if(m_cbUserAccount.Checked)
            {
                try { m_pKey.AddUserKey(new KcpUserAccount()); }
                catch(Exception exUA)
                {
                    MessageService.ShowWarning(exUA);
                    return false;
                }
            }

            return true;
        }
예제 #17
0
        private void loadMenu()
        {
            if (setDriveLetter()) {

                dbPath = path + "passwords.kdbx";    // drive letter calculated
                masterPath = path + "master";

                // init local hashes
                localPwdHash = new Dictionary<string, string>();
                localURLHash = new Dictionary<string, string>();

                string masterpw = File.ReadAllText(masterPath);
                var ioConnInfo = new IOConnectionInfo { Path = dbPath };
                var compKey = new CompositeKey();
                compKey.AddUserKey(new KcpPassword(masterpw));

                var db = new KeePassLib.PwDatabase();
                db.Open(ioConnInfo, compKey, null);

                foreach (var i in db.RootGroup.GetGroups(false)) {
                    var currMenu = new MenuItem(i.Name);
                    trayMenu.MenuItems.Add(currMenu);
                    loadSubMenuItems(i, currMenu);
                }

                db.Close();

                if (watcher == null) {
                    watchForDbChanges();
                }

            } else {
                const string DbFileNotFound = "Database file not found!";
                trayMenu.MenuItems.Add(DbFileNotFound);
            }

            //trayMenu.MenuItems.Add(MenuSeparator);
            //const string AddItem = "Add New Entry";
            //trayMenu.MenuItems.Add(AddItem, onAddNewItem);

            trayMenu.MenuItems.Add(MenuSeparator);
            const string Exit = "Exit";
            trayMenu.MenuItems.Add(Exit, OnExit);
        }