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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.IdentityModel
{
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Principal;
using System.Text;
enum EXTENDED_NAME_FORMAT
{
NameUnknown = 0,
NameFullyQualifiedDN = 1,
NameSamCompatible = 2,
NameDisplay = 3,
NameUniqueId = 6,
NameCanonical = 7,
NameUserPrincipalName = 8,
NameCanonicalEx = 9,
NameServicePrincipalName = 10,
NameDnsDomainName = 12
}
enum TokenInformationClass : uint
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert
}
enum Win32Error
{
ERROR_SUCCESS = 0,
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_NO_TOKEN = 1008,
ERROR_NONE_MAPPED = 1332,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct CREDUI_INFO
{
public int cbSize;
public IntPtr hwndParent;
public string pszMessageText;
public string pszCaptionText;
public IntPtr hbmBanner;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal class SEC_WINNT_AUTH_IDENTITY_EX
{
public uint Version;
public uint Length;
public string User;
public uint UserLength;
public string Domain;
public uint DomainLength;
public string Password;
public uint PasswordLength;
public uint Flags;
public string PackageList;
public uint PackageListLength;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SID_AND_ATTRIBUTES
{
internal IntPtr Sid;
internal uint Attributes;
internal static readonly long SizeOf = (long)Marshal.SizeOf(typeof(SID_AND_ATTRIBUTES));
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct TOKEN_GROUPS
{
internal uint GroupCount;
internal SID_AND_ATTRIBUTES Groups; // SID_AND_ATTRIBUTES Groups[ANYSIZE_ARRAY];
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PLAINTEXTKEYBLOBHEADER
{
internal byte bType;
internal byte bVersion;
internal short reserved;
internal int aiKeyAlg;
internal int keyLength;
internal static readonly int SizeOf = Marshal.SizeOf(typeof(PLAINTEXTKEYBLOBHEADER));
};
[StructLayout(LayoutKind.Sequential)]
internal struct LUID
{
internal uint LowPart;
internal uint HighPart;
}
[StructLayout(LayoutKind.Sequential)]
internal struct LUID_AND_ATTRIBUTES
{
internal LUID Luid;
internal uint Attributes;
}
[StructLayout(LayoutKind.Sequential)]
internal struct TOKEN_PRIVILEGE
{
internal uint PrivilegeCount;
internal LUID_AND_ATTRIBUTES Privilege;
internal static readonly uint Size = (uint)Marshal.SizeOf(typeof(TOKEN_PRIVILEGE));
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct UNICODE_INTPTR_STRING
{
internal UNICODE_INTPTR_STRING(int length, int maximumLength, IntPtr buffer)
{
this.Length = (ushort)length;
this.MaxLength = (ushort)maximumLength;
this.Buffer = buffer;
}
internal ushort Length;
internal ushort MaxLength;
internal IntPtr Buffer;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct KERB_CERTIFICATE_S4U_LOGON
{
internal KERB_LOGON_SUBMIT_TYPE MessageType;
internal uint Flags;
internal UNICODE_INTPTR_STRING UserPrincipalName;
// OPTIONAL, certificate mapping hints: username or username@domain
internal UNICODE_INTPTR_STRING DomainName; // used to locate the forest
// OPTIONAL, certificate mapping hints: if missing, using the local machine's domain
internal uint CertificateLength; // for the client certificate
internal IntPtr Certificate; // for the client certificate, BER encoded
internal static int Size = Marshal.SizeOf(typeof(KERB_CERTIFICATE_S4U_LOGON));
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct TOKEN_SOURCE
{
private const int TOKEN_SOURCE_LENGTH = 8;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = TOKEN_SOURCE_LENGTH)]
internal char[] Name;
internal LUID SourceIdentifier;
}
internal enum KERB_LOGON_SUBMIT_TYPE
{
KerbInteractiveLogon = 2,
KerbSmartCardLogon = 6,
KerbWorkstationUnlockLogon = 7,
KerbSmartCardUnlockLogon = 8,
KerbProxyLogon = 9,
KerbTicketLogon = 10,
KerbTicketUnlockLogon = 11,
//#if (_WIN32_WINNT >= 0x0501) -- Disabled until IIS fixes their target version.
KerbS4ULogon = 12,
//#endif
//#if (_WIN32_WINNT >= 0x0600)
KerbCertificateLogon = 13,
KerbCertificateS4ULogon = 14,
KerbCertificateUnlockLogon = 15,
//#endif
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct QUOTA_LIMITS
{
internal IntPtr PagedPoolLimit;
internal IntPtr NonPagedPoolLimit;
internal IntPtr MinimumWorkingSetSize;
internal IntPtr MaximumWorkingSetSize;
internal IntPtr PagefileLimit;
internal IntPtr TimeLimit;
}
internal enum SECURITY_IMPERSONATION_LEVEL
{
Anonymous = 0,
Identification = 1,
Impersonation = 2,
Delegation = 3,
}
internal enum TokenType : int
{
TokenPrimary = 1,
TokenImpersonation
}
internal enum SecurityLogonType : int
{
Interactive = 2,
Network,
Batch,
Service,
Proxy,
Unlock
}
[SuppressUnmanagedCodeSecurity]
static class NativeMethods
{
const string ADVAPI32 = "advapi32.dll";
const string KERNEL32 = "kernel32.dll";
const string SECUR32 = "secur32.dll";
const string CREDUI = "credui.dll";
// Error codes from ntstatus.h
//internal const uint STATUS_SOME_NOT_MAPPED = 0x00000107;
internal const uint STATUS_NO_MEMORY = 0xC0000017;
//internal const uint STATUS_NONE_MAPPED = 0xC0000073;
internal const uint STATUS_INSUFFICIENT_RESOURCES = 0xC000009A;
internal const uint STATUS_ACCESS_DENIED = 0xC0000022;
// From WinStatus.h
internal const uint STATUS_ACCOUNT_RESTRICTION = 0xC000006E;
internal static byte[] LsaSourceName = new byte[] { (byte)'W', (byte)'C', (byte)'F' }; // we set the source name to "WCF".
internal static byte[] LsaKerberosName = new byte[] { (byte)'K', (byte)'e', (byte)'r', (byte)'b', (byte)'e', (byte)'r', (byte)'o', (byte)'s' };
internal const uint KERB_CERTIFICATE_S4U_LOGON_FLAG_CHECK_DUPLICATES = 0x1;
internal const uint KERB_CERTIFICATE_S4U_LOGON_FLAG_CHECK_LOGONHOURS = 0x2;
// Error codes from WinError.h
internal const int ERROR_ACCESS_DENIED = 0x5;
internal const int ERROR_BAD_LENGTH = 0x18;
internal const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
internal const uint SE_GROUP_ENABLED = 0x00000004;
internal const uint SE_GROUP_USE_FOR_DENY_ONLY = 0x00000010;
internal const uint SE_GROUP_LOGON_ID = 0xC0000000;
internal const int PROV_RSA_AES = 24;
internal const int KP_IV = 1;
internal const uint CRYPT_DELETEKEYSET = 0x00000010;
internal const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
internal const byte PLAINTEXTKEYBLOB = 0x8;
internal const byte CUR_BLOB_VERSION = 0x2;
internal const int ALG_CLASS_DATA_ENCRYPT = (3 << 13);
internal const int ALG_TYPE_BLOCK = (3 << 9);
internal const int CALG_AES_128 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 14);
internal const int CALG_AES_192 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 15);
internal const int CALG_AES_256 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | 16);
[DllImport(ADVAPI32, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool LogonUser(
[In] string lpszUserName,
[In] string lpszDomain,
[In] string lpszPassword,
[In] uint dwLogonType,
[In] uint dwLogonProvider,
[Out] out SafeCloseHandle phToken
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool GetTokenInformation(
[In] IntPtr tokenHandle,
[In] uint tokenInformationClass,
[In] SafeHGlobalHandle tokenInformation,
[In] uint tokenInformationLength,
[Out] out uint returnLength);
[DllImport(ADVAPI32, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool CryptAcquireContextW(
[Out] out SafeProvHandle phProv,
[In] string pszContainer,
[In] string pszProvider,
[In] uint dwProvType,
[In] uint dwFlags
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal unsafe static extern bool CryptImportKey(
[In] SafeProvHandle hProv,
[In] void* pbData,
[In] uint dwDataLen,
[In] IntPtr hPubKey,
[In] uint dwFlags,
[Out] out SafeKeyHandle phKey
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool CryptGetKeyParam(
[In] SafeKeyHandle phKey,
[In] uint dwParam,
[In] IntPtr pbData,
[In, Out] ref uint dwDataLen,
[In] uint dwFlags
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal unsafe static extern bool CryptSetKeyParam(
[In] SafeKeyHandle phKey,
[In] uint dwParam,
[In] void* pbData,
[In] uint dwFlags
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
unsafe internal static extern bool CryptEncrypt(
[In] SafeKeyHandle phKey,
[In] IntPtr hHash,
[In] bool final,
[In] uint dwFlags,
[In] void* pbData,
[In, Out] ref int dwDataLen,
[In] int dwBufLen
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
unsafe internal static extern bool CryptDecrypt(
[In] SafeKeyHandle phKey,
[In] IntPtr hHash,
[In] bool final,
[In] uint dwFlags,
[In] void* pbData,
[In, Out] ref int dwDataLen
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern bool CryptDestroyKey(
[In] IntPtr phKey
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern bool CryptReleaseContext(
[In] IntPtr hProv,
[In] uint dwFlags
);
[DllImport(ADVAPI32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool LookupPrivilegeValueW(
[In] string lpSystemName,
[In] string lpName,
[Out] out LUID Luid
);
[DllImport(ADVAPI32, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern bool AdjustTokenPrivileges(
[In] SafeCloseHandle tokenHandle,
[In] bool disableAllPrivileges,
[In] ref TOKEN_PRIVILEGE newState,
[In] uint bufferLength,
[Out] out TOKEN_PRIVILEGE previousState,
[Out] out uint returnLength
);
[DllImport(ADVAPI32, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern bool RevertToSelf();
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceConsumption(ResourceScope.Process)]
[ResourceExposure(ResourceScope.Process)]
internal static extern bool OpenProcessToken(
[In] IntPtr processToken,
[In] TokenAccessLevels desiredAccess,
[Out] out SafeCloseHandle tokenHandle
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool OpenThreadToken(
[In] IntPtr threadHandle,
[In] TokenAccessLevels desiredAccess,
[In] bool openAsSelf,
[Out] out SafeCloseHandle tokenHandle
);
[DllImport(KERNEL32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.Process)]
internal static extern IntPtr GetCurrentProcess();
[DllImport(KERNEL32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern IntPtr GetCurrentThread();
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool DuplicateTokenEx(
[In] SafeCloseHandle existingTokenHandle,
[In] TokenAccessLevels desiredAccess,
[In] IntPtr tokenAttributes,
[In] SECURITY_IMPERSONATION_LEVEL impersonationLevel,
[In] TokenType tokenType,
[Out] out SafeCloseHandle duplicateTokenHandle
);
[DllImport(ADVAPI32, CharSet = CharSet.Auto, SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool SetThreadToken(
[In] IntPtr threadHandle,
[In] SafeCloseHandle threadToken
);
[DllImport(SECUR32, CharSet = CharSet.Auto, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
internal static extern int LsaRegisterLogonProcess(
[In] ref UNICODE_INTPTR_STRING logonProcessName,
[Out] out SafeLsaLogonProcessHandle lsaHandle,
[Out] out IntPtr securityMode
);
[DllImport(SECUR32, CharSet = CharSet.Auto, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
internal static extern int LsaConnectUntrusted(
[Out] out SafeLsaLogonProcessHandle lsaHandle
);
[DllImport(ADVAPI32, CharSet = CharSet.Unicode, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
internal static extern int LsaNtStatusToWinError(
[In] int status
);
[DllImport(SECUR32, CharSet = CharSet.Auto, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
internal static extern int LsaLookupAuthenticationPackage(
[In] SafeLsaLogonProcessHandle lsaHandle,
[In] ref UNICODE_INTPTR_STRING packageName,
[Out] out uint authenticationPackage
);
[DllImport(ADVAPI32, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool AllocateLocallyUniqueId(
[Out] out LUID Luid
);
[DllImport(SECUR32, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern int LsaFreeReturnBuffer(
IntPtr handle
);
[DllImport(SECUR32, CharSet = CharSet.Auto, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
internal static extern int LsaLogonUser(
[In] SafeLsaLogonProcessHandle LsaHandle,
[In] ref UNICODE_INTPTR_STRING OriginName,
[In] SecurityLogonType LogonType,
[In] uint AuthenticationPackage,
[In] IntPtr AuthenticationInformation,
[In] uint AuthenticationInformationLength,
[In] IntPtr LocalGroups,
[In] ref TOKEN_SOURCE SourceContext,
[Out] out SafeLsaReturnBufferHandle ProfileBuffer,
[Out] out uint ProfileBufferLength,
[Out] out LUID LogonId,
[Out] out SafeCloseHandle Token,
[Out] out QUOTA_LIMITS Quotas,
[Out] out int SubStatus
);
[DllImport(SECUR32, CharSet = CharSet.Auto, SetLastError = false)]
[ResourceExposure(ResourceScope.None)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern int LsaDeregisterLogonProcess(
[In] IntPtr handle
);
[DllImport(CREDUI, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal unsafe static extern uint SspiPromptForCredentials(
string pszTargetName,
ref CREDUI_INFO pUiInfo,
uint dwAuthError,
string pszPackage,
IntPtr authIdentity,
out IntPtr ppAuthIdentity,
[MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
uint dwFlags
);
[DllImport(CREDUI, CharSet = CharSet.Unicode, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
[return: MarshalAs(UnmanagedType.U1)]
internal unsafe static extern bool SspiIsPromptingNeeded(uint ErrorOrNtStatus);
[DllImport(SECUR32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.U1)]
internal extern static bool TranslateName(
string input,
EXTENDED_NAME_FORMAT inputFormat,
EXTENDED_NAME_FORMAT outputFormat,
StringBuilder outputString,
out uint size
);
}
}
|