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
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2024 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.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using KeePass.Forms;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Interfaces;
using KeePassLib.Resources;
using KeePassLib.Security;
namespace KeePass.DataExchange.Formats
{
internal sealed class PwPrompterDat12 : FileFormatProvider
{
public override bool SupportsImport { get { return true; } }
public override bool SupportsExport { get { return false; } }
public override string FormatName { get { return "Password Prompter DAT"; } }
public override string DefaultExtension { get { return "dat"; } }
public override string ApplicationGroup { get { return KPRes.PasswordManagers; } }
public override bool ImportAppendsToRootGroupOnly { get { return true; } }
public override void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger)
{
SingleLineEditForm dlg = new SingleLineEditForm();
dlg.InitEx(KPRes.Password, KPRes.Import + ": " + this.FormatName,
KPRes.PasswordPrompt, Properties.Resources.B48x48_KGPG_Key2,
string.Empty, null);
dlg.FlagsEx |= SlfFlags.Sensitive;
if(UIUtil.ShowDialogNotValue(dlg, DialogResult.OK)) return;
string strPassword = dlg.ResultString;
UIUtil.DestroyForm(dlg);
byte[] pbPassword = Encoding.Default.GetBytes(strPassword);
BinaryReader br = new BinaryReader(sInput, Encoding.Default);
ushort usFileVersion = br.ReadUInt16();
if(usFileVersion != 0x0100)
throw new Exception(KLRes.FileVersionUnsupported);
uint uEntries = br.ReadUInt32();
uint uKeySize = br.ReadUInt32();
Debug.Assert(uKeySize == 50); // It's a constant
byte btKeyArrayLen = br.ReadByte();
byte[] pbKey = br.ReadBytes(btKeyArrayLen);
byte btValidArrayLen = br.ReadByte();
byte[] pbValid = br.ReadBytes(btValidArrayLen);
if(pbPassword.Length > 0)
{
MangleSetKey(pbPassword);
MangleDecode(pbKey);
}
MangleSetKey(pbKey);
MangleDecode(pbValid);
string strValid = Encoding.Default.GetString(pbValid);
if(strValid != "aacaaaadaaeabaacyuioqaqqaaaaaertaaajkadaadaaxywqea")
throw new Exception(KLRes.InvalidCompositeKey);
for(uint uEntry = 0; uEntry < uEntries; ++uEntry)
{
PwEntry pe = new PwEntry(true, true);
pwStorage.RootGroup.AddEntry(pe, true);
pe.Strings.Set(PwDefs.TitleField, new ProtectedString(
pwStorage.MemoryProtection.ProtectTitle, ReadString(br)));
pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(
pwStorage.MemoryProtection.ProtectUserName, ReadString(br)));
pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
pwStorage.MemoryProtection.ProtectPassword, ReadString(br)));
pe.Strings.Set("Hint", new ProtectedString(false, ReadString(br)));
pe.Strings.Set(PwDefs.NotesField, new ProtectedString(
pwStorage.MemoryProtection.ProtectNotes, ReadString(br)));
pe.Strings.Set(PwDefs.UrlField, new ProtectedString(
pwStorage.MemoryProtection.ProtectUrl, ReadString(br)));
}
br.Close();
sInput.Close();
}
private string ReadString(BinaryReader br)
{
byte btLen = br.ReadByte();
byte[] pbData = br.ReadBytes(btLen);
MangleDecode(pbData);
return Encoding.Default.GetString(pbData);
}
byte[] m_pbMangleKey = null;
private void MangleSetKey(byte[] pbKey)
{
if(pbKey == null) { Debug.Assert(false); return; }
m_pbMangleKey = new byte[pbKey.Length];
Array.Copy(pbKey, m_pbMangleKey, pbKey.Length);
}
private void MangleDecode(byte[] pbData)
{
if(m_pbMangleKey == null) { Debug.Assert(false); return; }
int nKeyIndex = 0, nIndex = 0, nRemLen = pbData.Length;
bool bUp = true;
while(nRemLen > 0)
{
if(nKeyIndex > (m_pbMangleKey.Length - 1))
{
nKeyIndex = m_pbMangleKey.Length - 1;
bUp = false;
}
else if(nKeyIndex < 0)
{
nKeyIndex = 0;
bUp = true;
}
pbData[nIndex] ^= m_pbMangleKey[nKeyIndex];
nKeyIndex += (bUp ? 1 : -1);
++nIndex;
--nRemLen;
}
}
}
}
|