-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeCredential.cs
More file actions
271 lines (225 loc) · 8.58 KB
/
NativeCredential.cs
File metadata and controls
271 lines (225 loc) · 8.58 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
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Runtime.InteropServices ;
using Com = System.Runtime.InteropServices.ComTypes ;
namespace SendGmail
{
public enum CredentialType
{
Generic = 1,
DomainPassword = 2,
DomainCertificate = 3,
}
public enum Persistence
{
Session = 1,
LocalMachine = 2,
Enterprise = 3,
}
public interface IBorrowedNativeCredential : IDisposable
{
ref NativeCredential NativeCredential { get ; }
}
public interface IBorrowedNativeCredentials : IEnumerable<NativeCredential>, IDisposable
{
}
[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NativeCredential
{
#region --[Fields]------------------------------------------------
private UInt32 Flags ;
public CredentialType Type ;
public string TargetName ;
public string Comment ;
private Com.FILETIME LastWrittenFT ;
private Int32 CredentialBlobSize ;
private IntPtr CredentialBlobPtr ;
public Persistence Persistence ;
private UInt32 AttributeCount ;
private IntPtr Attributes ;
public string TargetAlias ;
public string UserName ;
#endregion
#region --[Members: Public]---------------------------------------
public DateTime LastWritten
{
get
{
var ticks = ((long)LastWrittenFT.dwHighDateTime << 32) | (uint)LastWrittenFT.dwLowDateTime ;
if (ticks == 0)
return default ;
return DateTime.FromFileTimeUtc (ticks) ;
}
set
{
var ticks = value != default ? value.ToFileTimeUtc () : default ;
LastWrittenFT.dwHighDateTime = (int)(ticks >> 32) ;
LastWrittenFT.dwLowDateTime = (int)(uint)(ticks) ;
}
}
public string CredentialBlobStr
{
get
{
if (CredentialBlobPtr == IntPtr.Zero)
return null ;
return Marshal.PtrToStringUni (CredentialBlobPtr, CredentialBlobSize / 2) ;
}
}
public byte[] CredentialBlobBytes
{
get
{
if (CredentialBlobPtr == IntPtr.Zero)
return null ;
var bytes = new byte[CredentialBlobSize] ;
Marshal.Copy (CredentialBlobPtr, bytes, 0, CredentialBlobSize) ;
return bytes ;
}
}
public unsafe void Save (string credentialBlob)
{
bool succ ;
if (credentialBlob == null)
{
CredentialBlobPtr = default ;
CredentialBlobSize = default ;
succ = CredWrite (ref this, 0) ;
}
else
fixed (char* ptr = credentialBlob)
{
CredentialBlobPtr = new IntPtr (ptr) ;
CredentialBlobSize = credentialBlob.Length * 2 ;
succ = CredWrite (ref this, 0) ;
}
if (succ)
return ;
throw Marshal.GetExceptionForHR (Marshal.GetHRForLastWin32Error ()) ;
}
public unsafe void Save (byte[] credentialBlob, int? length = null)
{
bool succ ;
if (credentialBlob == null)
{
CredentialBlobPtr = default ;
CredentialBlobSize = default ;
succ = CredWrite (ref this, 0) ;
}
else
fixed (byte* ptr = credentialBlob)
{
if (length > credentialBlob.Length)
throw new ArgumentOutOfRangeException (nameof (length)) ;
CredentialBlobPtr = new IntPtr (ptr) ;
CredentialBlobSize = length ?? credentialBlob.Length ;
succ = CredWrite (ref this, 0) ;
}
if (succ)
return ;
throw Marshal.GetExceptionForHR (Marshal.GetHRForLastWin32Error ()) ;
}
public void Delete ()
{
Delete (TargetName, Type) ;
}
#endregion
#region --[Methods: Public, static]-------------------------------
public static void Delete (string target, CredentialType type)
{
if (CredDelete (target, type, 0))
return ;
throw Marshal.GetExceptionForHR (Marshal.GetHRForLastWin32Error ()) ;
}
public static IBorrowedNativeCredentials Enumerate (string filter)
{
if (CredEnumerate (filter, 0, out var count, out var credentials))
{
credentials.Initialize<IntPtr> (count) ;
return credentials ;
}
var error = Marshal.GetLastWin32Error () ;
if (error == 1168)
{
var empty = new EnumeratedCredBuffer () ;
empty.Initialize (0) ;
return empty ;
}
throw Marshal.GetExceptionForHR (Marshal.GetHRForLastWin32Error ()) ;
}
public static IBorrowedNativeCredential Get (string target, CredentialType type)
{
return TryGet (target, type, out var credential) ? credential : null ;
}
public static bool TryGet (string target, CredentialType type, out IBorrowedNativeCredential credential)
{
if (CredRead (target, type, 0, out var handle))
{
credential = handle ;
return true ;
}
var error = Marshal.GetLastWin32Error () ;
if (error == 1168)
{
credential = null ;
return false ;
}
throw Marshal.GetExceptionForHR (Marshal.GetHRForLastWin32Error ()) ;
}
#endregion
#region --[Classes: Private]--------------------------------------
sealed class SafeHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid, IBorrowedNativeCredential
{
private bool m_read ;
private NativeCredential m_value ;
private SafeHandle () : base (true) {}
public ref NativeCredential NativeCredential
{
get
{
if(!m_read)
{
m_value = Marshal.PtrToStructure<NativeCredential> (handle) ;
m_read = true ;
}
return ref m_value ;
}
}
protected override bool ReleaseHandle ()
{
return CredFree (handle) ;
}
}
sealed class EnumeratedCredBuffer : SafeBuffer, IBorrowedNativeCredentials
{
internal EnumeratedCredBuffer () : base (true) {}
protected override bool ReleaseHandle ()
{
return CredFree (handle) ;
}
public IEnumerator<NativeCredential> GetEnumerator ()
{
for (var i = 0UL ; i < ByteLength ; i += (uint) IntPtr.Size)
yield return Marshal.PtrToStructure<NativeCredential> (Read<IntPtr> (i)) ;
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator () ;
}
}
#endregion
#region --[Methods: Interop]--------------------------------------
[DllImport ("advapi32.dll", SetLastError = true)]
private static extern bool CredFree (IntPtr cred) ;
[DllImport ("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CredDelete (string target, CredentialType type, int reservedFlag) ;
[DllImport ("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CredWrite ([In] ref NativeCredential userCredential, UInt32 flags) ;
[DllImport ("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CredRead (string target, CredentialType type, int reservedFlag, out SafeHandle credential) ;
[DllImport ("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CredEnumerate (string filter, UInt32 flags, out UInt32 count, out EnumeratedCredBuffer credentials) ;
#endregion
}
}