Exemplo n.º 1
0
        /// <summary>
        /// Queries a string-valued context attribute by the named attribute.
        /// </summary>
        /// <param name="attrib">The string-valued attribute to query.</param>
        /// <returns></returns>
        private string QueryContextString(ContextQueryAttrib attrib)
        {
            SecPkgContext_String stringAttrib;
            SecurityStatus       status = SecurityStatus.InternalError;
            string result = null;
            bool   gotRef = false;

            if (attrib != ContextQueryAttrib.Names && attrib != ContextQueryAttrib.Authority)
            {
                throw new InvalidOperationException("QueryContextString can only be used to query context Name and Authority attributes");
            }

            stringAttrib = new SecPkgContext_String();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                this.ContextHandle.DangerousAddRef(ref gotRef);
            }
            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)
            {
                return(null);
            }
            else if (status != SecurityStatus.OK)
            {
                throw new SspiException("Failed to query the context's associated user name", status);
            }

            return(result);
        }
 internal static extern SecurityStatus QueryContextAttributes_String(
     ref RawSspiHandle contextHandle,
     ContextQueryAttrib attrib,
     ref SecPkgContext_String names
     );
Exemplo n.º 3
0
        internal static SecurityStatus SafeQueryContextAttribute(
            SafeContextHandle handle,
            ContextQueryAttrib attribute,
            ref byte[] buffer
            )
        {
            bool gotRef = false;

            SecurityStatus status = SecurityStatus.InternalError;

            RuntimeHelpers.PrepareConstrainedRegions();

            int    pointerSize  = System.Environment.Is64BitOperatingSystem ? 8 : 4; //NOTE: update this when 128 bit processors exist
            IntPtr alloc_buffer = Marshal.AllocHGlobal(sizeof(uint) + pointerSize);  //NOTE: this is at most 4 + sizeof(void*) bytes

            //see struct SecPkgContext_SessionKey
            // https://msdn.microsoft.com/en-us/library/windows/desktop/aa380096(v=vs.85).aspx
            try
            {
                handle.DangerousAddRef(ref gotRef);
            }
            catch (Exception)
            {
                if (gotRef)
                {
                    handle.DangerousRelease();
                    gotRef = false;
                    buffer = null;
                }

                throw;
            }
            finally
            {
                if (gotRef)
                {
                    status = ContextNativeMethods.QueryContextAttributes(
                        ref handle.rawHandle,
                        attribute,
                        alloc_buffer
                        );
                    if (status == SecurityStatus.OK)
                    {
                        KeyStruct key = new KeyStruct();

                        Marshal.PtrToStructure(alloc_buffer, key);   // fit to the proper size, read a byte[]

                        byte[] sizedBuffer = new byte[key.size];

                        for (int i = 0; i < key.size; i++)
                        {
                            sizedBuffer[i] = Marshal.ReadByte(key.data, i);
                        }

                        buffer = sizedBuffer;
                    }
                    handle.DangerousRelease();
                }
            }
            Marshal.FreeHGlobal(alloc_buffer);
            return(status);
        }
Exemplo n.º 4
0
 internal static extern SecurityStatus QueryContextAttributes(
     ref RawSspiHandle contextHandle,
     ContextQueryAttrib attrib,
     IntPtr attribute
     );
Exemplo n.º 5
0
        /// <summary>
        /// Queries a string-valued context attribute by the named attribute.
        /// </summary>
        /// <param name="attrib">The string-valued attribute to query.</param>
        /// <returns></returns>
        private string QueryContextString(ContextQueryAttrib attrib)
        {
            SecPkgContext_String stringAttrib;
            SecurityStatus status = SecurityStatus.InternalError;
            string result = null;
            bool gotRef = false;

            if( attrib != ContextQueryAttrib.Names && attrib != ContextQueryAttrib.Authority )
            {
                throw new InvalidOperationException( "QueryContextString can only be used to query context Name and Authority attributes" );
            }

            stringAttrib = new SecPkgContext_String();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                this.ContextHandle.DangerousAddRef( ref gotRef );
            }
            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 )
            {
                return null;
            }
            else if( status != SecurityStatus.OK )
            {
                throw new SSPIException( "Failed to query the context's associated user name", status );
            }

            return result;
        }
 internal static extern SecurityStatus QueryContextAttributes_String(
     ref RawSspiHandle contextHandle,
     ContextQueryAttrib attrib,
     ref SecPkgContext_String names
 );