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.Diagnostics;
23 using System.Drawing;
24 using System.Windows.Forms;
25 
26 using KeePass.Resources;
27 using KeePass.UI;
28 
29 using KeePassLib;
30 using KeePassLib.Utility;
31 
32 namespace KeePass.App
33 {
34 	public static class AppDefs
35 	{
36 		public static readonly Color ColorControlNormal = SystemColors.Window;
37 		public static readonly Color ColorControlDisabled = SystemColors.Control;
38 		public static readonly Color ColorEditError = Color.FromArgb(255, 192, 192);
39 
40 		public static readonly Color ColorQualityLow = Color.FromArgb(255, 128, 0);
41 		public static readonly Color ColorQualityHigh = Color.FromArgb(0, 255, 0);
42 		public static readonly Color ColorQualityMid = Color.FromArgb(255, 255, 0);
43 
44 		public static readonly string LanguagesDir = "Languages";
45 
46 		public static readonly string PluginsDir = "Plugins";
47 		public static readonly string PluginProductName = "KeePass Plugin";
48 
49 		public static readonly string XslFilesDir = "XSL";
50 		public static readonly string XslFileHtmlFull = "KDBX_DetailsFull_HTML.xsl";
51 		public static readonly string XslFileHtmlLight = "KDBX_DetailsLight_HTML.xsl";
52 		public static readonly string XslFileHtmlTabular = "KDBX_Tabular_HTML.xsl";
53 
54 		public static class FileNames
55 		{
56 			public static readonly string Program = "KeePass.exe";
57 			public static readonly string XmlSerializers = "KeePass.XmlSerializers.dll";
58 
59 			public static readonly string NativeLib32 = "KeePassLibC32.dll";
60 			public static readonly string NativeLib64 = "KeePassLibC64.dll";
61 
62 			public static readonly string ShInstUtil = "ShInstUtil.exe";
63 		}
64 
65 		// internal const string MruNameValueSplitter = @"/::/";
66 
67 		/// <summary>
68 		/// Hot key IDs (used in <c>WM_HOTKEY</c> window messages).
69 		/// </summary>
70 		public static class GlobalHotKeyId
71 		{
72 			public static readonly int AutoType = 195;
73 			public static readonly int AutoTypePassword = 197;
74 			public static readonly int AutoTypeSelected = 196;
75 			public static readonly int ShowWindow = 226;
76 			public static readonly int EntryMenu = 227;
77 
78 			internal const int TempRegTest = 225;
79 		}
80 
81 		public static class HelpTopics
82 		{
83 			internal const string Default = "index";
84 
85 			public static readonly string Acknowledgements = "base/credits";
86 			public static readonly string License = "v2/license";
87 
88 			public static readonly string DatabaseSettings = "v2/dbsettings";
89 			public static readonly string DbSettingsGeneral = "general";
90 			public static readonly string DbSettingsSecurity = "security";
91 			// public static readonly string DbSettingsProtection = "protection";
92 			public static readonly string DbSettingsCompression = "compression";
93 
94 			public static readonly string AutoType = "base/autotype";
95 			public static readonly string AutoTypeObfuscation = "v2/autotype_obfuscation";
96 			public static readonly string AutoTypeWindowFilters = "autowindows";
97 
98 			public static readonly string Entry = "v2/entry";
99 			public static readonly string EntryGeneral = "general";
100 			public static readonly string EntryStrings = "advanced";
101 			public static readonly string EntryAutoType = "autotype";
102 			public static readonly string EntryHistory = "history";
103 
104 			public static readonly string KeySources = "base/keys";
105 			public static readonly string KeySourcesKeyFile = "keyfiles";
106 			public static readonly string KeySourcesUserAccount = "winuser";
107 
108 			public static readonly string PwGenerator = "base/pwgenerator";
109 			public static readonly string Search = "base/search";
110 			public static readonly string IOConnections = "v2/ioconnect";
111 			public static readonly string UrlField = "base/autourl";
112 			public static readonly string CommandLine = "base/cmdline";
113 			public static readonly string FieldRefs = "base/fieldrefs";
114 
115 			public static readonly string ImportExport = "base/importexport";
116 			public static readonly string ImportExportGenericCsv = "genericcsv";
117 			public static readonly string ImportExportSteganos = "imp_steganos";
118 			public static readonly string ImportExportPassKeeper = "imp_passkeeper";
119 			internal const string ImportExportParents = "exp_parents";
120 
121 			public static readonly string Security = "base/security";
122 			internal const string SecurityOptEx = "secoptex";
123 			internal const string SecurityOptAdm = "secoptadm";
124 
125 			public static readonly string AppPolicy = "v2/policy";
126 
127 			public static readonly string Triggers = "v2/triggers";
128 			public static readonly string TriggersEvents = "events";
129 			public static readonly string TriggersConditions = "conditions";
130 			public static readonly string TriggersActions = "actions";
131 
132 			public static readonly string TriggerUIStateUpd = "kb/trigger_uistateupd";
133 
134 			public static readonly string Setup = "v2/setup";
135 			public static readonly string SetupMono = "mono";
136 
137 			// public static readonly string FaqTech = "base/faq_tech";
138 			// public static readonly string FaqTechMemProt = "memprot";
139 
140 			public static readonly string XmlReplace = "v2/xml_replace";
141 
142 			public static readonly string KbFaq = "kb/faq";
143 			public static readonly string KbFaqURtf = "urtf";
144 		}
145 
146 		public static class CommandLineOptions
147 		{
148 			public static readonly string Password = "pw";
149 			public static readonly string KeyFile = "keyfile";
150 			public static readonly string UserAccount = "useraccount";
151 
152 			public static readonly string PasswordEncrypted = "pw-enc";
153 			public static readonly string PasswordStdIn = "pw-stdin";
154 
155 			public static readonly string PreSelect = "preselect";
156 
157 			public static readonly string IoCredUserName = "iousername";
158 			public static readonly string IoCredPassword = "iopassword";
159 			public static readonly string IoCredFromRecent = "iocredfromrecent";
160 			public static readonly string IoCredIsComplete = "ioiscomplete";
161 
162 			// User-friendly Pascal-case (shown in UAC dialog)
163 			public static readonly string FileExtRegister = "RegisterFileExt";
164 			public static readonly string FileExtUnregister = "UnregisterFileExt";
165 
166 			public static readonly string PreLoad = "preload";
167 			// public static readonly string PreLoadRegister = "registerpreload";
168 			// public static readonly string PreLoadUnregister = "unregisterpreload";
169 
170 			public static readonly string ExitAll = "exit-all";
171 			public static readonly string Minimize = "minimize";
172 			public static readonly string AutoType = "auto-type";
173 			public static readonly string AutoTypePassword = "auto-type-password";
174 			public static readonly string AutoTypeSelected = "auto-type-selected";
175 			public static readonly string OpenEntryUrl = "entry-url-open";
176 			public static readonly string LockAll = "lock-all";
177 			public static readonly string UnlockAll = "unlock-all";
178 			public static readonly string Cancel = "cancel";
179 			public static readonly string IpcEvent = "e";
180 			public static readonly string IpcEvent1 = "e1";
181 
182 			public static readonly string Uuid = "uuid";
183 			public static readonly string Scheme = "scheme";
184 			public static readonly string Value = "value";
185 			public static readonly string Activate = "activate";
186 
187 			public static readonly string Help = "?";
188 			public static readonly string HelpLong = "help";
189 
190 			public static readonly string WorkaroundDisable = "wa-disable";
191 			public static readonly string WorkaroundEnable = "wa-enable";
192 
193 			public static readonly string ConfigPathLocal = "cfg-local";
194 
195 			public static readonly string ConfigSetUrlOverride = "set-urloverride";
196 			public static readonly string ConfigClearUrlOverride = "clear-urloverride";
197 			public static readonly string ConfigGetUrlOverride = "get-urloverride";
198 			public static readonly string ConfigAddUrlOverride = "add-urloverride";
199 			public static readonly string ConfigRemoveUrlOverride = "remove-urloverride";
200 
201 			public static readonly string ConfigSetLanguageFile = "set-languagefile";
202 
203 			public static readonly string PlgxCreate = "plgx-create";
204 			public static readonly string PlgxCreateInfo = "plgx-create-info";
205 			public static readonly string PlgxPrereqKP = "plgx-prereq-kp";
206 			public static readonly string PlgxPrereqNet = "plgx-prereq-net";
207 			public static readonly string PlgxPrereqOS = "plgx-prereq-os";
208 			public static readonly string PlgxPrereqPtr = "plgx-prereq-ptr";
209 			public static readonly string PlgxBuildPre = "plgx-build-pre";
210 			public static readonly string PlgxBuildPost = "plgx-build-post";
211 
212 			public static readonly string Debug = "debug";
213 			public static readonly string DebugThrowException = "debug-throwexcp";
214 			// public static readonly string SavePluginCompileRes = "saveplgxcr"; // Now: Debug
215 			public static readonly string ShowAssemblyInfo = "showasminfo";
216 			public static readonly string MakeXmlSerializerEx = "makexmlserializerex";
217 			public static readonly string MakeXspFile = "makexspfile";
218 
219 #if DEBUG
220 			public static readonly string TestGfx = "testgfx";
221 #endif
222 
223 			public static readonly string Version = "version"; // For Unix
224 
225 			// #if (DEBUG && !KeePassLibSD)
226 			// public static readonly string MakePopularPasswordTable = "makepopularpasswordtable";
227 			// #endif
228 		}
229 
230 		public static class FileExtension
231 		{
232 			public static readonly string FileExt = "kdbx";
233 			public static readonly string FileExtId = "kdbxfile";
234 
235 			public static readonly string KeyFile = "keyx";
236 			internal const string KeyFileAlt = "key";
237 		}
238 
239 		public static readonly string AutoRunName = "KeePass Password Safe 2";
240 		public static readonly string PreLoadName = "KeePass 2 PreLoad";
241 
242 		public static readonly string MutexName = "KeePassAppMutex";
243 		public static readonly string MutexNameGlobal = "KeePassAppMutexEx";
244 
245 		// public static readonly string ScriptExtension = "kps";
246 
247 		public static readonly int InvalidWindowValue = -16381;
248 
249 		public static class NamedEntryColor
250 		{
251 			public static readonly Color LightRed = Color.FromArgb(255, 204, 204);
252 			public static readonly Color LightGreen = Color.FromArgb(204, 255, 204);
253 			public static readonly Color LightBlue = Color.FromArgb(153, 204, 255);
254 			public static readonly Color LightYellow = Color.FromArgb(255, 255, 153);
255 		}
256 
257 		public static class FileDialogContext
258 		{
259 			// Values must not contain '@'
260 
261 			public static readonly string Database = "Database";
262 			public static readonly string Sync = "Sync";
263 			public static readonly string KeyFile = "KeyFile";
264 			public static readonly string Import = "Import";
265 			public static readonly string Export = "Export";
266 			public static readonly string Attachments = "Attachments";
267 			public static readonly string Xsl = "Xsl";
268 		}
269 
270 		public static readonly string DefaultTrlAuthor = "Dominik Reichl";
271 		public static readonly string DefaultTrlContact = "https://www.dominik-reichl.de/";
272 
273 		// public static readonly string LanguageInfoFileName = "LanguageInfo.xml";
274 
275 		internal const string Rsa4096PublicKeyXml =
276 			@"<RSAKeyValue><Modulus>9Oa8Bb9if4rSYBxczLVQ3Yyae95dWQrNJ1FlqS7DoF" +
277 			@"RF80tD2hq84vxDE8slVeSHs68KMFnJhPsXFD6nM9oTRBaUlU/alnRTUU+X/cUXbr" +
278 			@"mhYN9DkJhM0OcWk5Vsl9Qxl613sA+hqIwmPc+el/fCM/1vP6JkHo/JTJ2OxQvDKN" +
279 			@"4cC55pHYMZt+HX6AhemsPe7ejTG7l9nN5tHGmD+GrlwuxBTddzFBARmoknFzDPWd" +
280 			@"QHddjuK1mXDs6lWeu73ODlSLSHMc5n0R2xMwGHN4eaiIMGzEbt0lv1aMWz+Iy1H3" +
281 			@"XgFgWGDHX9kx8yefmfcgFIK4Y/xHU5EyGAV68ZHPatv6i4pT4ZuecIb5GSoFzVXq" +
282 			@"8BZjbe+zDI+Wr1u8jLcBH0mySTWkF2gooQLvE1vgZXP1blsA7UFZSVFzYjBt36HQ" +
283 			@"SJLpQ9AjjB5MKpMSlvdb5SnvjzREiFVLoBsY7KH2TMz+IG1Rh3OZTGwjQKXkgRVj" +
284 			@"5XrEMTFRmT1zo2BHWhx8vrY6agVzqsCVqxYRbjeAhgOi6hLDMHSNAVuNg6ZHOKS8" +
285 			@"6x6kmBcBhGJriwY017H3Oxuhfz33ehRFX/C05egCvmR2TAXbqm+CUgrq1bZ96T/y" +
286 			@"s+O5uvKpe7H+EZuWb655Y9WuQSby+q0Vqqny7T6Z2NbEnI8nYHg5ZZP+TijSxeH0" +
287 			@"8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
288 
289 		public static readonly string ColumnIdnGroup = "Group";
290 		public static readonly string ColumnIdnCreationTime = "CreationTime";
291 		public static readonly string ColumnIdnLastModificationTime = "LastModificationTime";
292 		public static readonly string ColumnIdnLastAccessTime = "LastAccessTime";
293 		public static readonly string ColumnIdnExpiryTime = "ExpiryTime";
294 		public static readonly string ColumnIdnUuid = "UUID";
295 		public static readonly string ColumnIdnAttachment = "Attachment";
296 
GetEntryField(PwEntry pe, string strFieldId)297 		public static string GetEntryField(PwEntry pe, string strFieldId)
298 		{
299 			if(pe == null) throw new ArgumentNullException("pe");
300 			if(strFieldId == null) throw new ArgumentNullException("strFieldId");
301 
302 			if(strFieldId == AppDefs.ColumnIdnGroup)
303 				return ((pe.ParentGroup != null) ? pe.ParentGroup.Name : string.Empty);
304 			else if(strFieldId == AppDefs.ColumnIdnCreationTime)
305 				return TimeUtil.ToDisplayString(pe.CreationTime);
306 			else if(strFieldId == AppDefs.ColumnIdnLastModificationTime)
307 				return TimeUtil.ToDisplayString(pe.LastModificationTime);
308 			else if(strFieldId == AppDefs.ColumnIdnLastAccessTime)
309 				return TimeUtil.ToDisplayString(pe.LastAccessTime);
310 			else if(strFieldId == AppDefs.ColumnIdnExpiryTime)
311 			{
312 				if(!pe.Expires) return KPRes.NeverExpires;
313 				return TimeUtil.ToDisplayString(pe.ExpiryTime);
314 			}
315 			else if(strFieldId == AppDefs.ColumnIdnUuid)
316 				return pe.Uuid.ToHexString();
317 			else if(strFieldId == AppDefs.ColumnIdnAttachment)
318 				return pe.Binaries.UCount.ToString();
319 
320 			return pe.Strings.ReadSafe(strFieldId);
321 		}
322 
GetQualityColor(float fQ, bool bToControlBack)323 		internal static Color GetQualityColor(float fQ, bool bToControlBack)
324 		{
325 			if(fQ < 0.0f) { Debug.Assert(false); fQ = 0.0f; }
326 			if(fQ > 1.0f) { Debug.Assert(false); fQ = 1.0f; }
327 
328 			Color clrL = AppDefs.ColorQualityLow;
329 			Color clrH = AppDefs.ColorQualityHigh;
330 			Color clrM = AppDefs.ColorQualityMid;
331 
332 			int iR, iG, iB;
333 			if(fQ <= 0.5f)
334 			{
335 				fQ *= 2.0f;
336 				iR = clrL.R + (int)(fQ * ((int)clrM.R - (int)clrL.R));
337 				iG = clrL.G + (int)(fQ * ((int)clrM.G - (int)clrL.G));
338 				iB = clrL.B + (int)(fQ * ((int)clrM.B - (int)clrL.B));
339 			}
340 			else
341 			{
342 				fQ = (fQ - 0.5f) * 2.0f;
343 				iR = clrM.R + (int)(fQ * ((int)clrH.R - (int)clrM.R));
344 				iG = clrM.G + (int)(fQ * ((int)clrH.G - (int)clrM.G));
345 				iB = clrM.B + (int)(fQ * ((int)clrH.B - (int)clrM.B));
346 			}
347 
348 			if(iR < 0) { Debug.Assert(false); iR = 0; }
349 			if(iR > 255) { Debug.Assert(false); iR = 255; }
350 			if(iG < 0) { Debug.Assert(false); iG = 0; }
351 			if(iG > 255) { Debug.Assert(false); iG = 255; }
352 			if(iB < 0) { Debug.Assert(false); iB = 0; }
353 			if(iB > 255) { Debug.Assert(false); iB = 255; }
354 
355 			Color clrQ = Color.FromArgb(iR, iG, iB);
356 			if(bToControlBack)
357 				return UIUtil.ColorTowards(clrQ, AppDefs.ColorControlNormal, 0.5);
358 			return clrQ;
359 		}
360 
GetKeyFileFilter()361 		internal static string GetKeyFileFilter()
362 		{
363 			return UIUtil.CreateFileTypeFilter(AppDefs.FileExtension.KeyFile +
364 				"|" + AppDefs.FileExtension.KeyFileAlt, KPRes.KeyFiles, true);
365 		}
366 	}
367 }
368