CredentialManagement.cs
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 |
// Copyright (c) 2014, Joel Bennett
// Licensed under MIT license using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles; using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME; namespace CredentialManagement { using System.Management.Automation; using System.Security; public enum CredentialType : uint { None = 0, Generic = 1, DomainPassword = 2, DomainCertificate = 3, DomainVisiblePassword = 4, GenericCertificate = 5, DomainExtended = 6, Maximum = 7, MaximumEx = 1007 } public enum PersistanceType : uint { Session = 1, LocalComputer = 2, Enterprise = 3 } public static class SecureStringHelper { // Methods public static SecureString CreateSecureString(string plainString) { var result = new SecureString(); if (!string.IsNullOrEmpty(plainString)) { foreach (var c in plainString.ToCharArray()) { result.AppendChar(c); } } result.MakeReadOnly(); return result; } public static SecureString CreateSecureString(IntPtr ptrToString, int length = 0) { string password = length > 0 ? Marshal.PtrToStringUni(ptrToString, length) : Marshal.PtrToStringUni(ptrToString); return CreateSecureString(password); } public static string CreateString(SecureString secureString) { string str; IntPtr zero = IntPtr.Zero; if ((secureString == null) || (secureString.Length == 0)) { return string.Empty; } try { zero = Marshal.SecureStringToBSTR(secureString); str = Marshal.PtrToStringBSTR(zero); } finally { if (zero != IntPtr.Zero) { Marshal.ZeroFreeBSTR(zero); } } return str; } } public static class IntPtrHelpers { public static IntPtr Increment(this IntPtr ptr, int cbSize) { return new IntPtr(ptr.ToInt64() + cbSize); } public static IntPtr Increment<T>(this IntPtr ptr) { return ptr.Increment(Marshal.SizeOf(typeof(T))); } public static T ElementAt<T>(this IntPtr ptr, int index) { var offset = Marshal.SizeOf(typeof(T))*index; var offsetPtr = ptr.Increment(offset); return (T)Marshal.PtrToStructure(offsetPtr, typeof(T)); } } public static class Store { private static string FixTarget(string target) { if (!target.Contains(":")) { if (target.Contains("=")) { target = "MicrosoftPowerShell:" + target; } else { target = "MicrosoftPowerShell:user=" + target; } } return target; } public static PSObject[] Find(string filter = "") { uint count = 0; int Flag = 0; IntPtr credentialArray = IntPtr.Zero; PSObject[] output = null; if(string.IsNullOrEmpty(filter)) { filter = null; Flag = 1; } NativeMethods.PSCredentialMarshaler helper = new NativeMethods.PSCredentialMarshaler(); if(NativeMethods.CredEnumerate(filter, Flag, out count, out credentialArray)) { IntPtr cred = IntPtr.Zero; output = new PSObject[count]; for (int n = 0; n < count; n++) { cred = credentialArray.ElementAt<IntPtr>(n); output[n] = (PSObject)helper.MarshalNativeToManaged(cred); } helper.CleanUpNativeData(credentialArray); } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } return output; } public static void Save(PSObject credential) { var cred = credential.BaseObject as PSCredential; if (cred == null) { throw new ArgumentException("Credential object does not contain a PSCredential", "credential"); } if (!NativeMethods.CredWrite(credential, 0)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } public static void Delete(string target, CredentialType type = CredentialType.Generic, bool fix = true) { if(fix) { target = FixTarget(target); } if (!NativeMethods.CredDelete(target, type, 0)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } public static void Get(string target, bool fix = true) { if(fix) { target = FixTarget(target); } PSObject cred; if (!NativeMethods.CredFindBestCredential(target, CredentialType.Generic, 0, out cred)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } public static PSObject Load(string target, CredentialType type = CredentialType.Generic, bool fix = true) { PSObject cred; if(fix) { target = FixTarget(target); } if(!NativeMethods.CredRead(target, type, 0, out cred)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return cred; } } public class NativeMethods { public enum CREDErrorCodes { NO_ERROR = 0, ERROR_NOT_FOUND = 1168, ERROR_NO_SUCH_LOGON_SESSION = 1312, ERROR_INVALID_PARAMETER = 87, ERROR_INVALID_FLAGS = 1004, ERROR_BAD_USERNAME = 2202, SCARD_E_NO_READERS_AVAILABLE = (int)(0x8010002E - 0x100000000), SCARD_E_NO_SMARTCARD = (int)(0x8010000C - 0x100000000), SCARD_W_REMOVED_CARD = (int)(0x80100069 - 0x100000000), SCARD_W_WRONG_CHV = (int)(0x8010006B - 0x100000000) } [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CredRead(string target, CredentialType type, int reservedFlag, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PSCredentialMarshaler))] out PSObject credentialout); [DllImport("Advapi32.dll", EntryPoint = "CredWriteW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CredWrite([In] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PSCredentialMarshaler))] PSObject userCredential, [In] UInt32 flags); [DllImport("advapi32.dll", EntryPoint = "CredDeleteW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CredDelete(string target, CredentialType type, int flags); [DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)] public static extern bool CredFree([In] IntPtr cred); [DllImport("advapi32.dll", EntryPoint = "CredEnumerateW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CredEnumerate(string filter, int flag, out uint count, out IntPtr pCredentials); [DllImport("advapi32.dll", EntryPoint = "CredFindBestCredentialW", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool CredFindBestCredential(string target, CredentialType type, int reservedFlag, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PSCredentialMarshaler))] out PSObject credentialout); [DllImport("ole32.dll")] public static extern void CoTaskMemFree(IntPtr ptr); public class PSCredentialMarshaler : ICustomMarshaler { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private class NATIVECREDENTIAL { public UInt32 Flags; public CredentialType Type = CredentialType.Generic; public string TargetName; public string Comment; public FILETIME LastWritten; public UInt32 CredentialBlobSize; public IntPtr CredentialBlob; public PersistanceType Persist = PersistanceType.Enterprise; public UInt32 AttributeCount; public IntPtr Attributes; public string TargetAlias; public string UserName; } public void CleanUpManagedData(object ManagedObj) { // Nothing to do since all data can be garbage collected. } public void CleanUpNativeData(IntPtr pNativeData) { if (pNativeData == IntPtr.Zero) { return; } CredFree(pNativeData); } public int GetNativeDataSize() { return Marshal.SizeOf(typeof(NATIVECREDENTIAL)); } public IntPtr MarshalManagedToNative(object obj) { PSCredential credential; PSObject credo = obj as PSObject; if (credo != null) { credential = credo.BaseObject as PSCredential; } else { credential = obj as PSCredential; } if (credential == null) { Console.WriteLine("Error: Can't convert!"); return IntPtr.Zero; } var nCred = new NATIVECREDENTIAL() { UserName = credential.UserName, CredentialBlob = Marshal.SecureStringToCoTaskMemUnicode(credential.Password), CredentialBlobSize = (uint)credential.Password.Length * 2, TargetName = "MicrosoftPowerShell:user=" + credential.UserName, Type = CredentialType.Generic, Persist = PersistanceType.Enterprise }; if (credo != null) { foreach (var m in credo.Members) { switch (m.Name) { case "Target": if (m.Value != null) nCred.TargetName = m.Value.ToString(); break; case "TargetAlias": if (m.Value != null) nCred.TargetAlias = m.Value.ToString(); break; case "Type": if (m.Value != null) nCred.Type = (CredentialType)m.Value; break; case "Persistence": if (m.Value != null) nCred.Persist = (PersistanceType)m.Value; break; case "Description": if (m.Value != null) nCred.Comment = m.Value.ToString(); break; case "LastWriteTime": // ignored break; } } } IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(nCred)); Marshal.StructureToPtr(nCred, ptr, false); return ptr; } public object MarshalNativeToManaged(IntPtr pNativeData) { if (pNativeData == IntPtr.Zero) { return null; } var ncred = (NATIVECREDENTIAL)Marshal.PtrToStructure(pNativeData, typeof(NATIVECREDENTIAL)); var securePass = (ncred.CredentialBlob == IntPtr.Zero) ? new SecureString() : SecureStringHelper.CreateSecureString(ncred.CredentialBlob, (int)(ncred.CredentialBlobSize)/2); var credEx = new PSObject(new PSCredential(ncred.UserName ?? "-", securePass)); credEx.Members.Add(new PSNoteProperty("Target", ncred.TargetName)); credEx.Members.Add(new PSNoteProperty("TargetAlias", ncred.TargetAlias)); credEx.Members.Add(new PSNoteProperty("Type", (CredentialType)ncred.Type)); credEx.Members.Add(new PSNoteProperty("Persistence", (PersistanceType)ncred.Persist)); credEx.Members.Add(new PSNoteProperty("Description", ncred.Comment)); credEx.Members.Add(new PSNoteProperty("LastWriteTime", DateTime.FromFileTime((((long)ncred.LastWritten.dwHighDateTime) << 32) + ncred.LastWritten.dwLowDateTime))); return credEx; } public static ICustomMarshaler GetInstance(string cookie) { return new PSCredentialMarshaler(); } } } } |