1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using KeePass.App;
using KeePass.App.Configuration;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;
using KeePassLib;
using KeePassLib.Cryptography;
using KeePassLib.Keys;
using KeePassLib.Native;
using KeePassLib.Security;
using KeePassLib.Serialization;
using KeePassLib.Utility;
namespace KeePass.Forms
{
public partial class KeyCreationForm : Form
{
private CompositeKey m_pKey = null;
private bool m_bCreatingNew = false;
private IOConnectionInfo m_ioInfo = new IOConnectionInfo();
private PwInputControlGroup m_icgPassword = new PwInputControlGroup();
public CompositeKey CompositeKey
{
get
{
Debug.Assert(m_pKey != null);
return m_pKey;
}
}
public KeyCreationForm()
{
InitializeComponent();
Program.Translation.ApplyTo(this);
}
public void InitEx(IOConnectionInfo ioInfo, bool bCreatingNew)
{
if(ioInfo != null) m_ioInfo = ioInfo;
m_bCreatingNew = bCreatingNew;
}
private void OnFormLoad(object sender, EventArgs e)
{
GlobalWindowManager.AddWindow(this);
BannerFactory.CreateBannerEx(this, m_bannerImage,
Properties.Resources.B48x48_KGPG_Sign, KPRes.CreateMasterKey,
m_ioInfo.GetDisplayName());
this.Icon = new Icon("/usr/share/keepass2/KeePass.ico");
this.Text = KPRes.CreateMasterKey;
FontUtil.AssignDefaultBold(m_cbPassword);
FontUtil.AssignDefaultBold(m_cbKeyFile);
FontUtil.AssignDefaultBold(m_cbUserAccount);
m_ttRect.SetToolTip(m_cbHidePassword, KPRes.TogglePasswordAsterisks);
m_ttRect.SetToolTip(m_btnSaveKeyFile, KPRes.KeyFileCreate);
m_ttRect.SetToolTip(m_btnOpenKeyFile, KPRes.KeyFileUseExisting);
m_ttRect.SetToolTip(m_tbRepeatPassword, KPRes.PasswordRepeatHint);
if(!m_bCreatingNew)
m_lblIntro.Text = KPRes.ChangeMasterKeyIntroShort;
m_icgPassword.Attach(m_tbPassword, m_cbHidePassword, m_lblRepeatPassword,
m_tbRepeatPassword, m_lblEstimatedQuality, m_pbPasswordQuality,
m_lblQualityBits, this, true, false);
m_cmbKeyFile.Items.Add(KPRes.NoKeyFileSpecifiedMeta);
foreach(KeyProvider prov in Program.KeyProviderPool)
m_cmbKeyFile.Items.Add(prov.Name);
m_cmbKeyFile.SelectedIndex = 0;
m_cbPassword.Checked = true;
UIUtil.ApplyKeyUIFlags(Program.Config.UI.KeyCreationFlags,
m_cbPassword, m_cbKeyFile, m_cbUserAccount, m_cbHidePassword);
if(WinUtil.IsWindows9x || NativeLib.IsUnix())
{
UIUtil.SetChecked(m_cbUserAccount, false);
UIUtil.SetEnabled(m_cbUserAccount, false);
UIUtil.SetEnabled(m_lblWindowsAccDesc, false);
UIUtil.SetEnabled(m_lblWindowsAccDesc2, false);
}
CustomizeForScreenReader();
EnableUserControls();
}
private void CustomizeForScreenReader()
{
if(!Program.Config.UI.OptimizeForScreenReader) return;
m_cbHidePassword.Text = KPRes.HideUsingAsterisks;
}
private void CleanUpEx()
{
m_icgPassword.Release();
}
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 == 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))
{
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;
}
private void EnableUserControls()
{
m_icgPassword.Enabled = m_cbPassword.Checked;
m_btnOpenKeyFile.Enabled = m_btnSaveKeyFile.Enabled =
m_cmbKeyFile.Enabled = m_cbKeyFile.Checked;
string strKeyFile = m_cmbKeyFile.Text;
if(!m_cbPassword.Checked && !m_cbKeyFile.Checked && !m_cbUserAccount.Checked)
m_btnCreate.Enabled = false;
else if(m_cbKeyFile.Checked && strKeyFile.Equals(KPRes.NoKeyFileSpecifiedMeta))
m_btnCreate.Enabled = false;
else m_btnCreate.Enabled = true;
m_ttRect.SetToolTip(m_cmbKeyFile, strKeyFile);
}
private void OnCheckedPassword(object sender, EventArgs e)
{
EnableUserControls();
if(m_cbPassword.Checked) UIUtil.SetFocus(m_tbPassword, this);
}
private void OnCheckedKeyFile(object sender, EventArgs e)
{
EnableUserControls();
}
private void OnBtnOK(object sender, EventArgs e)
{
if(!CreateCompositeKey()) this.DialogResult = DialogResult.None;
}
private void OnBtnCancel(object sender, EventArgs e)
{
m_pKey = null;
}
private void OnClickKeyFileCreate(object sender, EventArgs e)
{
SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.KeyFileCreate,
UrlUtil.StripExtension(UrlUtil.GetFileName(m_ioInfo.Path)) + "." +
AppDefs.FileExtension.KeyFile, UIUtil.CreateFileTypeFilter("key",
KPRes.KeyFiles, true), 1, "key", true);
if(sfd.ShowDialog() == DialogResult.OK)
{
EntropyForm dlg = new EntropyForm();
if(dlg.ShowDialog() == DialogResult.OK)
{
byte[] pbAdditionalEntropy = dlg.GeneratedEntropy;
try
{
KcpKeyFile.Create(sfd.FileName, pbAdditionalEntropy);
string str = sfd.FileName;
m_cmbKeyFile.Items.Add(str);
m_cmbKeyFile.SelectedIndex = m_cmbKeyFile.Items.Count - 1;
}
catch(Exception exKC)
{
MessageService.ShowWarning(exKC);
}
}
UIUtil.DestroyForm(dlg);
}
EnableUserControls();
}
private void OnClickKeyFileBrowse(object sender, EventArgs e)
{
OpenFileDialog ofd = UIUtil.CreateOpenFileDialog(KPRes.KeyFileUseExisting,
UIUtil.CreateFileTypeFilter("key", KPRes.KeyFiles, true), 2, null,
false, true);
if(ofd.ShowDialog() == DialogResult.OK)
{
string str = ofd.FileName;
m_cmbKeyFile.Items.Add(str);
m_cmbKeyFile.SelectedIndex = m_cmbKeyFile.Items.Count - 1;
}
EnableUserControls();
}
private void OnWinUserCheckedChanged(object sender, EventArgs e)
{
EnableUserControls();
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
GlobalWindowManager.RemoveWindow(this);
}
private void OnBtnHelp(object sender, EventArgs e)
{
AppHelp.ShowHelp(AppDefs.HelpTopics.KeySources, null);
}
private void OnKeyFileSelectedIndexChanged(object sender, EventArgs e)
{
EnableUserControls();
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
CleanUpEx();
}
}
}
|