using System; using System.Runtime.InteropServices; using static Vanara.PInvoke.AdvApi32; namespace Vanara.PInvoke { /// Functions, enumerations and structures found in Secur32.dll. public static partial class Secur32 { /// The Kerberos authentication package name. [PInvokeData("Ntsecapi.h")] public const string MICROSOFT_KERBEROS_NAME = "Kerberos"; /// The MSV1_0 authentication package name. [PInvokeData("Ntsecapi.h")] public const string MSV1_0_PACKAGE_NAME = "MICROSOFT_AUTHENTICATION_PACKAGE_V1_0"; /// The Negotiate authentication package name. [PInvokeData("Security.h")] public const string NEGOSSP_NAME = "Negotiate"; /// /// The LsaConnectUntrusted function establishes an untrusted connection to the LSA server. /// /// /// Pointer to a handle that receives the connection handle, which must be provided in future authentication services. /// /// /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. /// /// /// /// LsaConnectUntrusted returns a handle to an untrusted connection; it does not verify any information about the caller. The /// handle should be closed using the LsaDeregisterLogonProcess function. /// /// /// If your application simply needs to query information from authentication packages, you can use the handle returned by this /// function in calls to LsaCallAuthenticationPackage and LsaLookupAuthenticationPackage. /// /// Applications with the SeTcbPrivilege privilege may create a trusted connection by calling LsaRegisterLogonProcess. /// // https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-lsaconnectuntrusted NTSTATUS LsaConnectUntrusted( // PHANDLE LsaHandle ); [DllImport(Lib.Secur32, SetLastError = false, ExactSpelling = true)] [PInvokeData("ntsecapi.h", MSDNShortId = "b54917c8-51cd-4891-9613-f37a4a46448b")] // public static extern NTStatus LsaConnectUntrusted(ref IntPtr LsaHandle); public static extern NTStatus LsaConnectUntrusted(out SafeLsaConnectionHandle LsaHandle); /// /// The LsaDeregisterLogonProcess function deletes the caller's logon application context and closes the connection to the LSA server. /// /// Handle obtained from a LsaRegisterLogonProcess or LsaConnectUntrusted call. /// /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. /// [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378269")] public static extern NTStatus LsaDeregisterLogonProcess(LsaConnectionHandle LsaHandle); /// The LsaLookupAuthenticationPackage function obtains the unique identifier of an authentication package. /// Handle obtained from a previous call to LsaRegisterLogonProcess or LsaConnectUntrusted. /// A string that specifies the name of the authentication package. The package name must not exceed 127 bytes in length. The following table lists the names of the Microsoft-provided authentication packages. /// /// ValueMeaning /// MSV1_0_PACKAGE_NAMEThe MSV1_0 authentication package name. /// MICROSOFT_KERBEROS_NAMEThe Kerberos authentication package name. /// NEGOSSP_NAMEThe Negotiate authentication package name. /// /// /// Pointer to a ULONG that receives the authentication package identifier. /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. The following are possible error codes. /// /// Return codeDescription /// STATUS_NO_SUCH_PACKAGEThe specified authentication package is unknown to the LSA. /// STATUS_NAME_TOO_LONGThe authentication package name exceeds 127 bytes. /// [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378297")] public static extern NTStatus LsaLookupAuthenticationPackage(LsaConnectionHandle LsaHandle, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaStringMarshaler))] string PackageName, out int AuthenticationPackage); /// /// The LsaRegisterLogonProcess function establishes a connection to the LSA server and verifies that the caller is a logon application. /// /// /// String identifying the logon application. This should be a printable name suitable for display to administrators. For example, /// the Windows logon application might use the name "User32LogonProcess". This name is used by the LSA during auditing. /// LsaRegisterLogonProcess does not check whether the name is already in use. This string must not exceed 127 bytes. /// /// Pointer that receives a handle used in future authentication function calls. /// The value returned is not meaningful and should be ignored. /// /// If the function succeeds, the return value is STATUS_SUCCESS. /// If the function fails, the return value is an NTSTATUS code. For more information, see LSA Policy Function Return Values. /// The LsaNtStatusToWinError function converts an NTSTATUS code to a Windows error code. /// [DllImport(Lib.Secur32, ExactSpelling = true)] [PInvokeData("Ntsecapi.h", MSDNShortId = "aa378318")] public static extern NTStatus LsaRegisterLogonProcess([In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaStringMarshaler))] string LogonProcessName, out SafeLsaConnectionHandle LsaHandle, out uint SecurityMode); /// Provides a handle to an LSA connection. [StructLayout(LayoutKind.Sequential)] public struct LsaConnectionHandle : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public LsaConnectionHandle(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static LsaConnectionHandle NULL => new LsaConnectionHandle(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(LsaConnectionHandle h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator LsaConnectionHandle(IntPtr h) => new LsaConnectionHandle(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(LsaConnectionHandle h1, LsaConnectionHandle h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(LsaConnectionHandle h1, LsaConnectionHandle h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is LsaConnectionHandle h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a for that is disposed using . public class SafeLsaConnectionHandle : HANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeLsaConnectionHandle(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeLsaConnectionHandle() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator LsaConnectionHandle(SafeLsaConnectionHandle h) => h.handle; /// protected override bool InternalReleaseHandle() => LsaDeregisterLogonProcess(this).Succeeded; } /* AuditComputeEffectivePolicyBySid function AuditComputeEffectivePolicyByToken function AuditEnumerateCategories function AuditEnumeratePerUserPolicy function AuditEnumerateSubCategories function AuditFree function AuditLookupCategoryGuidFromCategoryId function AuditLookupCategoryIdFromCategoryGuid function AuditLookupCategoryNameA function AuditLookupCategoryNameW function AuditLookupSubCategoryNameA function AuditLookupSubCategoryNameW function AuditQueryGlobalSaclA function AuditQueryGlobalSaclW function AuditQueryPerUserPolicy function AuditQuerySecurity function AuditQuerySystemPolicy function AuditSetGlobalSaclA function AuditSetGlobalSaclW function AuditSetPerUserPolicy function AuditSetSecurity function AuditSetSystemPolicy function KERB_CRYPTO_KEY structure LsaCallAuthenticationPackage function LsaCreateTrustedDomainEx function LsaDeleteTrustedDomain function LsaEnumerateLogonSessions function LsaEnumerateTrustedDomains function LsaEnumerateTrustedDomainsEx function LsaGetLogonSessionData function LsaLogonUser function LsaLookupNames function LsaLookupSids function LsaOpenTrustedDomainByName function LsaQueryDomainInformationPolicy function LsaQueryForestTrustInformation function LsaQueryInformationPolicy function LsaQueryTrustedDomainInfoByName function LsaRegisterPolicyChangeNotification function LsaRetrievePrivateData function LsaSetDomainInformationPolicy function LsaSetForestTrustInformation function LsaSetInformationPolicy function LsaSetTrustedDomainInfoByName function LsaStorePrivateData function LsaUnregisterPolicyChangeNotification function PSAM_INIT_NOTIFICATION_ROUTINE callback function PSAM_PASSWORD_FILTER_ROUTINE callback function PSAM_PASSWORD_NOTIFICATION_ROUTINE callback function RtlDecryptMemory function RtlEncryptMemory function RtlGenRandom function */ } }