1 /* 2 KeePass Password Safe - The Open-Source Password Manager 3 Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 using System; 21 using System.Collections.Generic; 22 using System.Drawing; 23 using System.IO; 24 using System.Text; 25 using System.Windows.Forms; 26 27 using KeePass.Resources; 28 29 using KeePassLib; 30 using KeePassLib.Interfaces; 31 using KeePassLib.Native; 32 using KeePassLib.Utility; 33 34 namespace KeePass.DataExchange.Formats 35 { 36 internal sealed class KeePassKdb1x : FileFormatProvider 37 { 38 public override bool SupportsImport { get { return true; } } 39 public override bool SupportsExport { get { return true; } } 40 41 public override string FormatName { get { return "KeePass KDB (1.x)"; } } 42 public override string DefaultExtension { get { return KeePassKdb1x.FileExt1; } } 43 public override string ApplicationGroup { get { return PwDefs.ShortProductName; } } 44 45 public override bool SupportsUuids { get { return true; } } 46 public override bool RequiresKey { get { return true; } } 47 48 public override Image SmallIcon 49 { 50 get { return KeePass.Properties.Resources.B16x16_KeePass; } 51 } 52 53 internal const string FileExt1 = "kdb"; 54 internal const string FileExt2 = "pwd"; 55 TryBeginImport()56 public override bool TryBeginImport() 57 { 58 if(NativeLib.IsUnix()) 59 { 60 MessageService.ShowWarning(KPRes.KeePassLibCNotWindows, 61 KPRes.KeePassLibCNotWindowsHint); 62 return false; 63 } 64 65 Exception exLib; 66 if(!KdbFile.IsLibraryInstalled(out exLib)) 67 { 68 MessageService.ShowWarning(KPRes.KeePassLibCNotFound, 69 KPRes.KdbKeePassLibC, exLib); 70 return false; 71 } 72 73 return true; 74 } 75 TryBeginExport()76 public override bool TryBeginExport() 77 { 78 return TryBeginImport(); 79 } 80 Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger)81 public override void Import(PwDatabase pwStorage, Stream sInput, 82 IStatusLogger slLogger) 83 { 84 string strTempFile = Program.TempFilesPool.GetTempFileName(); 85 86 BinaryReader br = new BinaryReader(sInput); 87 byte[] pb = br.ReadBytes((int)sInput.Length); 88 br.Close(); 89 File.WriteAllBytes(strTempFile, pb); 90 91 KdbFile kdb = new KdbFile(pwStorage, slLogger); 92 kdb.Load(strTempFile); 93 94 Program.TempFilesPool.Delete(strTempFile); 95 } 96 Export(PwExportInfo pwExportInfo, Stream sOutput, IStatusLogger slLogger)97 public override bool Export(PwExportInfo pwExportInfo, Stream sOutput, 98 IStatusLogger slLogger) 99 { 100 PwDatabase pd = (pwExportInfo.ContextDatabase ?? new PwDatabase()); 101 102 string strTempFile = Program.TempFilesPool.GetTempFileName(false); 103 104 try 105 { 106 KdbFile kdb = new KdbFile(pd, slLogger); 107 kdb.Save(strTempFile, pwExportInfo.DataGroup); 108 109 byte[] pbKdb = File.ReadAllBytes(strTempFile); 110 sOutput.Write(pbKdb, 0, pbKdb.Length); 111 MemUtil.ZeroByteArray(pbKdb); 112 } 113 catch(Exception exKdb) 114 { 115 if(slLogger != null) slLogger.SetText(exKdb.Message, LogStatusType.Error); 116 117 return false; 118 } 119 finally { Program.TempFilesPool.Delete(strTempFile); } 120 121 return true; 122 } 123 } 124 } 125