-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOSHCredStore.cs
More file actions
458 lines (427 loc) · 18.4 KB
/
Copy pathPOSHCredStore.cs
File metadata and controls
458 lines (427 loc) · 18.4 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Decompiled with JetBrains decompiler
// Type: VMware.Security.CredentialStore.CredentialStore
// Assembly: VMware.Security.CredentialStore, Version=5.8.0.0, Culture=neutral, PublicKeyToken=null
// MVID: B602E82B-CA33-42BA-BBE5-BCAA5F312917
// Assembly location: C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\VMware.Security.CredentialStore.dll
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Schema;
namespace POSHCredStore
{
internal class CredentialStore : ICredentialStore
{
private bool _isUsernameCaseSensitive = true;
private static readonly string s_DefaultCredentialFilePath = "%APPDATA%\\credstore\\credentials.xml";
private const int s_AquireLockSleepIntervalMilliseconds = 500;
private const int s_AquireLockTimeoutSeconds = 20;
private const string s_CredentialElementXPath = "/Credentials/passwordEntry";
private const string s_CredentialEntryElementName = "passwordEntry";
private const string s_CredentialsElementXPath = "/Credentials";
private const string s_HostElementName = "host";
private const string s_PasswordElementName = "password";
private const string s_UsernameElementName = "username";
private const string s_VersionElementName = "version";
private const string s_VersionElementXPath = "/Credentials/version";
private const string s_CredentialsElementName = "Credentials";
private const bool DEFAULT_USERNAME_CASE_SENSITIVITY = true;
private const string s_LatestDocumentVersionSupported = "2.0";
private static readonly string s_CredentialFilePath;
private readonly string _credentialFilePath;
private bool _objectAlreadyDisposed;
static CredentialStore()
{
string str = ConfigurationManager.AppSettings["DefaultCredentialStoreFilePath"];
if (!string.IsNullOrEmpty(str))
CredentialStore.s_DefaultCredentialFilePath = str;
CredentialStore.s_CredentialFilePath = Environment.ExpandEnvironmentVariables(CredentialStore.s_DefaultCredentialFilePath);
}
public CredentialStore()
: this(new FileInfo(CredentialStore.s_CredentialFilePath), true)
{
}
public CredentialStore(bool isUsernameCaseSensitive)
: this(new FileInfo(CredentialStore.s_CredentialFilePath), isUsernameCaseSensitive)
{
}
public CredentialStore(FileInfo file)
: this(file, true)
{
}
public CredentialStore(FileInfo file, bool isUsernameCaseSensitive)
{
if (file == null)
file = new FileInfo(CredentialStore.s_CredentialFilePath);
this._credentialFilePath = file.FullName;
this._isUsernameCaseSensitive = isUsernameCaseSensitive;
if (file.Directory.Exists)
return;
string directoryName = Path.GetDirectoryName(Environment.ExpandEnvironmentVariables(CredentialStore.s_DefaultCredentialFilePath));
if (!file.DirectoryName.Equals(directoryName, StringComparison.OrdinalIgnoreCase))
throw new DirectoryNotFoundException(file.DirectoryName);
file.Directory.Create();
}
~CredentialStore()
{
this.Dispose(false);
}
public bool AddPassword(string host, string username, char[] password)
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
if (string.IsNullOrEmpty(host))
throw new ArgumentException("Host name cannot be empty.", "host");
if (string.IsNullOrEmpty(username))
throw new ArgumentException("User name cannot be empty.", "username");
if (password == null)
password = new char[0];
FileStream fileStream = (FileStream) null;
bool flag;
try
{
if (!File.Exists(this._credentialFilePath))
{
using (File.Create(this._credentialFilePath, 8192, FileOptions.RandomAccess, CredentialStore.GetSecuritySettings()))
fileStream = this.OpenFile(FileShare.None);
CredentialStore.InitializeCredentialsDocument((Stream) fileStream);
}
else
fileStream = this.OpenFile(FileShare.None);
XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream) fileStream);
XmlNode xmlNode = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, this._isUsernameCaseSensitive);
flag = xmlNode == null;
if (xmlNode == null)
xmlNode = (XmlNode) credentialsXmlDocument.CreateElement("passwordEntry");
else
xmlNode.RemoveAll();
CredentialStore.FillCredentialNode(xmlNode, host, username, password);
Array.Clear((Array) password, 0, password.Length);
credentialsXmlDocument.SelectSingleNode("/Credentials").AppendChild(xmlNode);
CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream) fileStream);
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
return flag;
}
public bool RemovePassword(string host, string username)
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
bool flag = false;
if (File.Exists(this._credentialFilePath))
{
FileStream fileStream = (FileStream) null;
try
{
fileStream = this.OpenFile(FileShare.None);
XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream) fileStream);
XmlNode credentialNode = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, this._isUsernameCaseSensitive);
flag = credentialNode != null;
if (credentialNode != null)
{
credentialsXmlDocument.SelectSingleNode("/Credentials").RemoveChild(credentialNode);
CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream) fileStream);
}
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
return flag;
}
public void ClearPasswords()
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
if (!File.Exists(this._credentialFilePath))
return;
FileStream fileStream = (FileStream) null;
try
{
fileStream = this.OpenFile(FileShare.None);
XmlDocument credentialsXmlDocument = this.LoadCredentialsDocument((Stream) fileStream);
XmlNode xmlNode = credentialsXmlDocument.SelectSingleNode("/Credentials");
foreach (XmlNode oldChild in credentialsXmlDocument.SelectNodes("/Credentials/passwordEntry"))
xmlNode.RemoveChild(oldChild);
CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, (Stream) fileStream);
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
public IEnumerable<string> GetHosts()
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
Dictionary<string, string> dictionary = new Dictionary<string, string>();
if (File.Exists(this._credentialFilePath))
{
FileStream fileStream = (FileStream) null;
try
{
fileStream = this.OpenFile(FileShare.Read);
foreach (XmlNode node in this.LoadCredentialsDocument((Stream) fileStream).SelectNodes("/Credentials/passwordEntry"))
{
if (CredentialStore.IsValidPasswordEntryNode(node))
{
string innerText = node["host"].InnerText;
string key = innerText.ToLower();
if (!dictionary.ContainsKey(key))
dictionary[key] = innerText;
}
}
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
return (IEnumerable<string>) dictionary.Values;
}
public IEnumerable<string> GetUsernames(string host)
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
List<string> list = new List<string>();
if (File.Exists(this._credentialFilePath))
{
FileStream fileStream = (FileStream) null;
try
{
fileStream = this.OpenFile(FileShare.Read);
foreach (XmlNode node in this.LoadCredentialsDocument((Stream) fileStream).SelectNodes("/Credentials/passwordEntry"))
{
if (CredentialStore.IsValidPasswordEntryNode(node) && node["host"].InnerText.Equals(host, StringComparison.OrdinalIgnoreCase))
list.Add(node["username"].InnerText);
}
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
return (IEnumerable<string>) list;
}
public char[] GetPassword(string host, string username)
{
if (this._objectAlreadyDisposed)
throw new ObjectDisposedException("CredentialStore");
char[] chArray = (char[]) null;
if (File.Exists(this._credentialFilePath))
{
FileStream fileStream = (FileStream) null;
try
{
fileStream = this.OpenFile(FileShare.Read);
chArray = CredentialStore.GetPasswordInternal(this.LoadCredentialsDocument((Stream) fileStream), host, username, this._isUsernameCaseSensitive);
}
finally
{
if (fileStream != null)
fileStream.Dispose();
}
}
return chArray;
}
public void Close()
{
this.Dispose(true);
GC.SuppressFinalize((object) this);
}
private static void SaveCredentialsDocument(XmlDocument credentialsXmlDocument, Stream credentialFile)
{
credentialFile.Position = 0L;
credentialFile.SetLength(0L);
credentialsXmlDocument.Save(credentialFile);
}
private static void FillCredentialNode(XmlNode element, string host, string username, char[] password)
{
XmlElement element1 = element.OwnerDocument.CreateElement("host");
element1.InnerText = host;
element.AppendChild((XmlNode) element1);
XmlElement element2 = element.OwnerDocument.CreateElement("username");
element2.InnerText = username;
element.AppendChild((XmlNode) element2);
XmlElement element3 = element.OwnerDocument.CreateElement("password");
element3.InnerText = CredentialStore.EncryptPassword(password);
element.AppendChild((XmlNode) element3);
}
private static string EncryptPassword(char[] password)
{
return Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(password), (byte[]) null, DataProtectionScope.CurrentUser));
}
private static void ValidateCredentialsDocument(XmlDocument credentialsXmlDocument)
{
bool flag = true;
XmlDeclaration xmlDeclaration = (XmlDeclaration) credentialsXmlDocument.FirstChild;
if (!(flag & xmlDeclaration.Version.StartsWith("1.") & xmlDeclaration.Encoding == "UTF-8" & credentialsXmlDocument.SelectSingleNode("/Credentials") != null & credentialsXmlDocument.SelectSingleNode("/Credentials/version") != null))
throw new XmlSchemaValidationException("The credentials .xml file is not well formed.");
}
private static XmlNode GetCredentialNode(XmlDocument credentialsXmlDocument, string host, string username, bool isUsernameCaseSensitive)
{
XmlNode xmlNode = (XmlNode) null;
foreach (XmlNode node in credentialsXmlDocument.SelectNodes("/Credentials/passwordEntry"))
{
if (CredentialStore.IsValidPasswordEntryNode(node) && node["host"].InnerText.Equals(host, StringComparison.OrdinalIgnoreCase))
{
string innerText = node["username"].InnerText;
StringComparison comparisonType = isUsernameCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
if (string.Equals(innerText, username, comparisonType))
{
xmlNode = node;
break;
}
}
}
return xmlNode;
}
private static char[] GetPasswordInternal(XmlDocument credentialsXmlDocument, string host, string username, bool isUsernameCaseSensitive)
{
char[] chArray = (char[]) null;
XmlNode credentialNode = CredentialStore.GetCredentialNode(credentialsXmlDocument, host, username, isUsernameCaseSensitive);
if (credentialNode != null)
chArray = CredentialStore.DecryptPassword(credentialNode["password"].InnerText);
return chArray;
}
private static char[] DecryptPassword(string password)
{
return Encoding.UTF8.GetChars(ProtectedData.Unprotect(Convert.FromBase64String(password), (byte[]) null, DataProtectionScope.CurrentUser));
}
private static int GetHashCode(string s)
{
if (s == null)
throw new ArgumentNullException("s");
int num = 0;
for (int index = 0; index < s.Length; ++index)
num = 31 * num + (int) s[index];
return num;
}
private static char[] UnobfuscatePassword(string password, string host, string username)
{
byte[] array = Convert.FromBase64String(password);
byte num = (byte) (CredentialStore.GetHashCode(host.ToLower() + username) % 256);
for (int index = 0; index < array.Length; ++index)
array[index] ^= num;
int length = Array.IndexOf<byte>(array, (byte) 0);
if (length < 0)
throw new FormatException("Invalid password format. " + string.Format("Host: {0}, Username: {1}", (object) host, (object) username));
byte[] bytes = new byte[length];
Array.Copy((Array) array, (Array) bytes, bytes.Length);
return Encoding.UTF8.GetChars(bytes);
}
private static FileSecurity GetSecuritySettings()
{
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.SetAccessRuleProtection(true, false);
fileSecurity.AddAccessRule((FileSystemAccessRule) fileSecurity.AccessRuleFactory((IdentityReference) new NTAccount(WindowsIdentity.GetCurrent().Name), -1, false, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
return fileSecurity;
}
private static void InitializeCredentialsDocument(Stream credentialsFile)
{
XmlDocument credentialsXmlDocument = new XmlDocument();
credentialsXmlDocument.AppendChild((XmlNode) credentialsXmlDocument.CreateXmlDeclaration("1.0", "UTF-8", (string) null));
XmlElement element1 = credentialsXmlDocument.CreateElement("Credentials");
XmlElement element2 = credentialsXmlDocument.CreateElement("version");
element2.InnerText = "2.0";
element1.AppendChild((XmlNode) element2);
credentialsXmlDocument.AppendChild((XmlNode) element1);
CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, credentialsFile);
}
private XmlDocument LoadCredentialsDocument(Stream credentialsFile)
{
XmlDocument xmlDocument = new XmlDocument();
credentialsFile.Position = 0L;
try
{
Stream input = credentialsFile;
XmlReaderSettings settings = new XmlReaderSettings()
{
CloseInput = false,
ProhibitDtd = true
};
using (XmlReader reader = XmlReader.Create(input, settings))
xmlDocument.Load(reader);
}
catch (XmlException ex)
{
throw new XmlException(string.Format("Corrupted credential store file content: \"{0}\".", (object) ((FileStream) credentialsFile).Name));
}
CredentialStore.ValidateCredentialsDocument(xmlDocument);
if (xmlDocument.SelectSingleNode("/Credentials/version").InnerText != "2.0")
xmlDocument = this.UpdateDocumentVersion(xmlDocument, credentialsFile);
return xmlDocument;
}
private XmlDocument UpdateDocumentVersion(XmlDocument document, Stream stream)
{
string innerText = document.SelectSingleNode("/Credentials/version").InnerText;
if (innerText != "1.0")
throw new ArgumentException(string.Format("Cannot convert document version {0} to version {1}", (object) innerText, (object) "2.0"));
XmlDocument credentialsXmlDocument = document;
try
{
XmlDocument xmlDocument = (XmlDocument) credentialsXmlDocument.CloneNode(true);
xmlDocument.SelectSingleNode("/Credentials/version").InnerText = "2.0";
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/Credentials/passwordEntry");
if (xmlNodeList != null)
{
foreach (XmlNode xmlNode in xmlNodeList)
{
char[] password = CredentialStore.UnobfuscatePassword(xmlNode["password"].InnerText, xmlNode["host"].InnerText, xmlNode["username"].InnerText);
xmlNode["password"].InnerText = CredentialStore.EncryptPassword(password);
}
credentialsXmlDocument = xmlDocument;
CredentialStore.SaveCredentialsDocument(credentialsXmlDocument, stream);
}
}
catch
{
}
return credentialsXmlDocument;
}
private FileStream OpenFile(FileShare fileShare)
{
DateTime dateTime = DateTime.Now.AddSeconds(20.0);
while (true)
{
try
{
if (DateTime.Now > dateTime)
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
throw new TimeoutException("Could not aquire access to credential file: " + this._credentialFilePath + ". Either the current user account (" + (current != null ? current.Name : "<no name>") + ") does not have permissions to read the file or another process/thread has locked the file.");
}
return File.Open(this._credentialFilePath, FileMode.Open, FileAccess.ReadWrite, fileShare);
}
catch (UnauthorizedAccessException ex)
{
Thread.Sleep(500);
}
}
}
private static bool IsValidPasswordEntryNode(XmlNode node)
{
return true & node.Name == "passwordEntry" & node["host"] != null & node["username"] != null & node["password"] != null;
}
private void Dispose(bool disposing)
{
int num = disposing ? 1 : 0;
this._objectAlreadyDisposed = true;
}
}
}