Newer
Older
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
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
namespace GitHub.Authentication.CredentialManagement
{
public class Credential : IDisposable
{
const int maxPasswordLengthInBytes = NativeMethods.CREDUI_MAX_PASSWORD_LENGTH * 2;
static readonly object _lockObject = new object();
static readonly SecurityPermission _unmanagedCodePermission;
CredentialType _type;
string _target;
SecureString _password;
string _username;
string _description;
DateTime _lastWriteTime;
PersistenceType _persistanceType;
static Credential()
{
lock (_lockObject)
{
_unmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
}
}
public Credential() : this(null, (string)null)
{}
public Credential(
string username,
SecureString password,
string target = null)
{
Username = username;
SecurePassword = password;
Target = target;
Type = CredentialType.Generic;
PersistenceType = PersistenceType.LocalComputer;
_lastWriteTime = DateTime.MinValue;
}
public Credential(
string username,
string password,
string target = null)
{
Username = username;
Password = password;
Target = target;
Type = CredentialType.Generic;
PersistenceType = PersistenceType.LocalComputer;
_lastWriteTime = DateTime.MinValue;
}
bool disposed;
void Dispose(bool disposing)
{
if (disposing)
{
if (disposed) return;
SecurePassword.Clear();
SecurePassword.Dispose();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void CheckNotDisposed()
{
if (disposed)
{
throw new ObjectDisposedException("Credential object is already disposed.");
}
}
public string Username
{
get
{
CheckNotDisposed();
return _username;
}
set
{
CheckNotDisposed();
_username = value;
}
}
public string Password
{
get
{
return SecureStringHelper.CreateString(SecurePassword);
}
set
{
CheckNotDisposed();
SecurePassword = SecureStringHelper.CreateSecureString(string.IsNullOrEmpty(value) ? string.Empty : value);
}
}
public SecureString SecurePassword
{
get
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
return _password == null ? new SecureString() : _password.Copy();
}
set
{
CheckNotDisposed();
if (_password != null)
{
_password.Clear();
_password.Dispose();
}
_password = null == value ? new SecureString() : value.Copy();
}
}
public string Target
{
get
{
CheckNotDisposed();
return _target;
}
set
{
CheckNotDisposed();
_target = value;
}
}
public string Description
{
get
{
CheckNotDisposed();
return _description;
}
set
{
CheckNotDisposed();
_description = value;
}
}
public DateTime LastWriteTime
{
get
{
return LastWriteTimeUtc.ToLocalTime();
}
}
public DateTime LastWriteTimeUtc
{
get
{
CheckNotDisposed();
return _lastWriteTime;
}
private set { _lastWriteTime = value; }
}
public CredentialType Type
{
get
{
CheckNotDisposed();
return _type;
}
set
{
CheckNotDisposed();
_type = value;
}
}
public PersistenceType PersistenceType
{
get
{
CheckNotDisposed();
return _persistanceType;
}
set
{
CheckNotDisposed();
_persistanceType = value;
}
}
public bool Save()
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
byte[] passwordBytes = Encoding.Unicode.GetBytes(Password);
ValidatePasswordLength(passwordBytes);
var credential = new NativeMethods.CREDENTIAL
{
TargetName = Target,
UserName = Username,
CredentialBlob = Marshal.StringToCoTaskMemUni(Password),
CredentialBlobSize = passwordBytes.Length,
Comment = Description,
Type = (int)Type,
Persist = (int)PersistenceType
};
bool result = NativeMethods.CredWrite(ref credential, 0);
if (!result)
{
return false;
}
LastWriteTimeUtc = DateTime.UtcNow;
return true;
}
public bool Save(byte[] passwordBytes)
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
ValidatePasswordLength(passwordBytes);
var blob = Marshal.AllocCoTaskMem(passwordBytes.Length);
Marshal.Copy(passwordBytes, 0, blob, passwordBytes.Length);
var credential = new NativeMethods.CREDENTIAL
{
TargetName = Target,
UserName = Username,
CredentialBlob = blob,
CredentialBlobSize = passwordBytes.Length,
Comment = Description,
Type = (int)Type,
Persist = (int)PersistenceType
};
bool result = NativeMethods.CredWrite(ref credential, 0);
Marshal.FreeCoTaskMem(blob);
if (!result)
{
return false;
}
LastWriteTimeUtc = DateTime.UtcNow;
return true;
}
public bool Delete()
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
if (string.IsNullOrEmpty(Target))
{
throw new InvalidOperationException("Target must be specified to delete a credential.");
}
StringBuilder target = string.IsNullOrEmpty(Target) ? new StringBuilder() : new StringBuilder(Target);
bool result = NativeMethods.CredDelete(target, Type, 0);
return result;
}
public bool Load()
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
IntPtr credPointer;
bool result = NativeMethods.CredRead(Target, Type, 0, out credPointer);
if (!result)
{
return false;
}
using (NativeMethods.CriticalCredentialHandle credentialHandle = new NativeMethods.CriticalCredentialHandle(credPointer))
{
LoadInternal(credentialHandle.GetCredential());
}
return true;
}
public bool Exists()
{
CheckNotDisposed();
_unmanagedCodePermission.Demand();
if (string.IsNullOrEmpty(Target))
{
throw new InvalidOperationException("Target must be specified to check existance of a credential.");
}
using (Credential existing = new Credential { Target = Target, Type = Type })
{
return existing.Load();
}
}
internal void LoadInternal(NativeMethods.CREDENTIAL credential)
{
Username = credential.UserName;
if (credential.CredentialBlobSize > 0)
{
Password = Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2);
}
Target = credential.TargetName;
Type = (CredentialType)credential.Type;
PersistenceType = (PersistenceType)credential.Persist;
Description = credential.Comment;
LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten);
}
static void ValidatePasswordLength(byte[] passwordBytes)
{
if (passwordBytes.Length > maxPasswordLengthInBytes)
{
var message = string.Format(CultureInfo.InvariantCulture,
"The password length ({0} bytes) exceeds the maximum password length ({1} bytes).",
passwordBytes.Length,
maxPasswordLengthInBytes);
throw new ArgumentOutOfRangeException(message);
}
}
}
}