More CER goodness.

This commit is contained in:
antiduh
2014-06-25 01:26:59 +00:00
parent 26888a8b7b
commit 3ee755128f
2 changed files with 68 additions and 23 deletions

View File

@@ -239,14 +239,36 @@ namespace NSspi
internal SecPkgContext_Sizes QueryBufferSizes() internal SecPkgContext_Sizes QueryBufferSizes()
{ {
SecPkgContext_Sizes sizes = new SecPkgContext_Sizes(); SecPkgContext_Sizes sizes = new SecPkgContext_Sizes();
SecurityStatus status; SecurityStatus status = SecurityStatus.InternalError;
bool gotRef = false;
// TODO SAFE_CER RuntimeHelpers.PrepareConstrainedRegions();
status = ContextNativeMethods.QueryContextAttributes_Sizes( try
ref this.ContextHandle.rawHandle, {
ContextQueryAttrib.Sizes, this.ContextHandle.DangerousAddRef( ref gotRef );
ref sizes }
); catch ( Exception )
{
if ( gotRef )
{
this.ContextHandle.DangerousRelease();
gotRef = false;
}
throw;
}
finally
{
if ( gotRef )
{
status = ContextNativeMethods.QueryContextAttributes_Sizes(
ref this.ContextHandle.rawHandle,
ContextQueryAttrib.Sizes,
ref sizes
);
this.ContextHandle.DangerousRelease();
}
}
if( status != SecurityStatus.OK ) if( status != SecurityStatus.OK )
{ {
@@ -259,8 +281,9 @@ namespace NSspi
internal string QueryContextString(ContextQueryAttrib attrib) internal string QueryContextString(ContextQueryAttrib attrib)
{ {
SecPkgContext_String stringAttrib; SecPkgContext_String stringAttrib;
SecurityStatus status; SecurityStatus status = SecurityStatus.InternalError;
string result; string result = null;
bool gotRef = false;
if( attrib != ContextQueryAttrib.Names && attrib != ContextQueryAttrib.Authority ) if( attrib != ContextQueryAttrib.Names && attrib != ContextQueryAttrib.Authority )
{ {
@@ -269,25 +292,45 @@ namespace NSspi
stringAttrib = new SecPkgContext_String(); stringAttrib = new SecPkgContext_String();
// TODO SAFE_CER RuntimeHelpers.PrepareConstrainedRegions();
status = ContextNativeMethods.QueryContextAttributes_String( try
ref this.ContextHandle.rawHandle, {
attrib, this.ContextHandle.DangerousAddRef( ref gotRef );
ref stringAttrib }
); catch ( Exception )
{
if ( gotRef )
{
this.ContextHandle.DangerousRelease();
gotRef = false;
}
throw;
}
finally
{
if ( gotRef )
{
status = ContextNativeMethods.QueryContextAttributes_String(
ref this.ContextHandle.rawHandle,
attrib,
ref stringAttrib
);
this.ContextHandle.DangerousRelease();
if ( status == SecurityStatus.OK )
{
result = Marshal.PtrToStringUni( stringAttrib.StringResult );
ContextNativeMethods.FreeContextBuffer( stringAttrib.StringResult );
}
}
}
if( status == SecurityStatus.Unsupported ) if( status == SecurityStatus.Unsupported )
{ {
return null; return null;
} }
else if( status == SecurityStatus.OK ) else if( status != SecurityStatus.OK )
{
// TODO handle this safely.
result = Marshal.PtrToStringUni( stringAttrib.StringResult );
ContextNativeMethods.FreeContextBuffer( stringAttrib.StringResult );
}
else
{ {
throw new SSPIException( "Failed to query the context's associated user name", status ); throw new SSPIException( "Failed to query the context's associated user name", status );
} }

View File

@@ -125,6 +125,7 @@ namespace NSspi
int qualityOfProtection int qualityOfProtection
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )]
[DllImport( "Secur32.dll", EntryPoint = "QueryContextAttributes", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "QueryContextAttributes", CharSet = CharSet.Unicode )]
public static extern SecurityStatus QueryContextAttributes_Sizes( public static extern SecurityStatus QueryContextAttributes_Sizes(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
@@ -132,6 +133,7 @@ namespace NSspi
ref SecPkgContext_Sizes sizes ref SecPkgContext_Sizes sizes
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success)]
[DllImport( "Secur32.dll", EntryPoint = "QueryContextAttributes", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "QueryContextAttributes", CharSet = CharSet.Unicode )]
public static extern SecurityStatus QueryContextAttributes_String( public static extern SecurityStatus QueryContextAttributes_String(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
@@ -139,7 +141,7 @@ namespace NSspi
ref SecPkgContext_String names ref SecPkgContext_String names
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )]
[DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "FreeContextBuffer", CharSet = CharSet.Unicode )]
public static extern SecurityStatus FreeContextBuffer( IntPtr handle ); public static extern SecurityStatus FreeContextBuffer( IntPtr handle );