Implemented safe access to the safeCredHandle when reading credential name.
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.DirectoryServices.AccountManagement;
|
using System.DirectoryServices.AccountManagement;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.ConstrainedExecution;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -116,27 +117,51 @@ namespace NSspi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Name
|
public string Name
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
QueryNameAttribCarrier carrier = new QueryNameAttribCarrier();
|
QueryNameAttribCarrier carrier = new QueryNameAttribCarrier();
|
||||||
|
|
||||||
SecurityStatus status;
|
SecurityStatus status = SecurityStatus.InternalError;
|
||||||
string name = null;
|
string name = null;
|
||||||
|
bool gotRef = false;
|
||||||
|
|
||||||
status = CredentialNativeMethods.QueryCredentialsAttribute_Name(
|
RuntimeHelpers.PrepareConstrainedRegions();
|
||||||
ref this.safeCredHandle.rawHandle,
|
try
|
||||||
CredentialQueryAttrib.Names,
|
|
||||||
ref carrier
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( status == SecurityStatus.OK )
|
|
||||||
{
|
{
|
||||||
name = Marshal.PtrToStringUni( carrier.Name );
|
this.safeCredHandle.DangerousAddRef( ref gotRef );
|
||||||
NativeMethods.FreeContextBuffer( carrier.Name );
|
|
||||||
}
|
}
|
||||||
else
|
catch( Exception )
|
||||||
|
{
|
||||||
|
if( gotRef == true )
|
||||||
|
{
|
||||||
|
this.safeCredHandle.DangerousRelease();
|
||||||
|
gotRef = false;
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if( gotRef )
|
||||||
|
{
|
||||||
|
status = CredentialNativeMethods.QueryCredentialsAttribute_Name(
|
||||||
|
ref this.safeCredHandle.rawHandle,
|
||||||
|
CredentialQueryAttrib.Names,
|
||||||
|
ref carrier
|
||||||
|
);
|
||||||
|
|
||||||
|
this.safeCredHandle.DangerousRelease();
|
||||||
|
|
||||||
|
if( status == SecurityStatus.OK && carrier.Name != IntPtr.Zero )
|
||||||
|
{
|
||||||
|
name = Marshal.PtrToStringUni( carrier.Name );
|
||||||
|
NativeMethods.FreeContextBuffer( carrier.Name );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( status.IsError() )
|
||||||
{
|
{
|
||||||
throw new SSPIException( "Failed to query credential name", status );
|
throw new SSPIException( "Failed to query credential name", status );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.ConstrainedExecution;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -63,6 +64,7 @@ namespace NSspi
|
|||||||
/// <param name="attributeName"></param>
|
/// <param name="attributeName"></param>
|
||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )]
|
||||||
[DllImport( "Secur32.dll", EntryPoint = "QueryCredentialsAttributes", CharSet = CharSet.Unicode )]
|
[DllImport( "Secur32.dll", EntryPoint = "QueryCredentialsAttributes", CharSet = CharSet.Unicode )]
|
||||||
public static extern SecurityStatus QueryCredentialsAttribute_Name(
|
public static extern SecurityStatus QueryCredentialsAttribute_Name(
|
||||||
ref RawSspiHandle credentialHandle,
|
ref RawSspiHandle credentialHandle,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Runtime.ConstrainedExecution;
|
||||||
|
|
||||||
namespace NSspi
|
namespace NSspi
|
||||||
{
|
{
|
||||||
@@ -23,13 +24,9 @@ namespace NSspi
|
|||||||
_In_ PVOID pvContextBuffer
|
_In_ PVOID pvContextBuffer
|
||||||
);
|
);
|
||||||
*/
|
*/
|
||||||
[DllImport(
|
|
||||||
"Secur32.dll",
|
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success)]
|
||||||
EntryPoint = "FreeContextBuffer",
|
[DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )]
|
||||||
CallingConvention = CallingConvention.Winapi,
|
|
||||||
CharSet = CharSet.Unicode,
|
|
||||||
SetLastError = true
|
|
||||||
)]
|
|
||||||
public static extern SecurityStatus FreeContextBuffer( IntPtr buffer );
|
public static extern SecurityStatus FreeContextBuffer( IntPtr buffer );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user