public static unsafe object QueryContextAttributes(SSPIInterface SecModule, SafeDeleteContext securityContext, ContextAttribute contextAttribute, out int errorCode)
        {
            int  size       = IntPtr.Size;
            Type handleType = null;

            switch (contextAttribute)
            {
            case ContextAttribute.Sizes:
                size = SecSizes.SizeOf;
                break;

            case ContextAttribute.Names:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.StreamSizes:
                size = StreamSizes.SizeOf;
                break;

            case ContextAttribute.PackageInfo:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.NegotiationInfo:
                handleType = typeof(SafeFreeContextBuffer);
                size       = Marshal.SizeOf(typeof(NegotiationInfo));
                break;

            case ContextAttribute.RemoteCertificate:
                handleType = typeof(SafeFreeCertContext);
                break;

            case ContextAttribute.LocalCertificate:
                handleType = typeof(SafeFreeCertContext);
                break;

            case ContextAttribute.ClientSpecifiedSpn:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.IssuerListInfoEx:
                size       = Marshal.SizeOf(typeof(IssuerListInfoEx));
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.ConnectionInfo:
                size = Marshal.SizeOf(typeof(SslConnectionInfo));
                break;

            default:
                throw new ArgumentException(SR.GetString("net_invalid_enum", new object[] { "ContextAttribute" }), "contextAttribute");
            }
            SafeHandle refHandle = null;
            object     obj2      = null;

            try
            {
                byte[] buffer = new byte[size];
                errorCode = SecModule.QueryContextAttributes(securityContext, contextAttribute, buffer, handleType, out refHandle);
                if (errorCode != 0)
                {
                    return(null);
                }
                ContextAttribute attribute2 = contextAttribute;
                if (attribute2 <= ContextAttribute.NegotiationInfo)
                {
                    switch (attribute2)
                    {
                    case ContextAttribute.Sizes:
                        return(new SecSizes(buffer));

                    case ContextAttribute.Names:
                        if (!ComNetOS.IsWin9x)
                        {
                            return(Marshal.PtrToStringUni(refHandle.DangerousGetHandle()));
                        }
                        return(Marshal.PtrToStringAnsi(refHandle.DangerousGetHandle()));

                    case ContextAttribute.Lifespan:
                    case ContextAttribute.DceInfo:
                        return(obj2);

                    case ContextAttribute.StreamSizes:
                        return(new StreamSizes(buffer));

                    case ContextAttribute.PackageInfo:
                        return(new SecurityPackageInfoClass(refHandle, 0));

                    case (ContextAttribute.PackageInfo | ContextAttribute.Names):
                        return(obj2);

                    case ContextAttribute.NegotiationInfo:
                        goto Label_0229;
                    }
                    return(obj2);
                }
                switch (attribute2)
                {
                case ContextAttribute.RemoteCertificate:
                case ContextAttribute.LocalCertificate:
                    obj2      = refHandle;
                    refHandle = null;
                    return(obj2);

                case ContextAttribute.ClientSpecifiedSpn:
                    goto Label_0266;

                case ContextAttribute.IssuerListInfoEx:
                    obj2      = new IssuerListInfoEx(refHandle, buffer);
                    refHandle = null;
                    return(obj2);

                case ContextAttribute.ConnectionInfo:
                    return(new SslConnectionInfo(buffer));

                default:
                    return(obj2);
                }
Label_0229:
                try
                {
                    byte[] buffer2;
                    if (((buffer2 = buffer) == null) || (buffer2.Length == 0))
                    {
                        fixed(IntPtr *ptrRef = null)
                        {
                        }
                    }
                    return(new NegotiationInfoClass(refHandle, Marshal.ReadInt32(new IntPtr((void *)ptrRef), NegotiationInfo.NegotiationStateOffest)));
                }
                finally
                {
                    ptrRef = null;
                }
Label_0266:
                return(Marshal.PtrToStringUni(refHandle.DangerousGetHandle()));
            }
            finally
            {
                if (refHandle != null)
                {
                    refHandle.Close();
                }
            }
            return(obj2);
        }
        public static object QueryContextAttributes(SSPIInterface SecModule, SafeDeleteContext securityContext, ContextAttribute contextAttribute, out int errorCode)
        {
            GlobalLog.Enter("QueryContextAttributes", contextAttribute.ToString());

            int  nativeBlockSize = IntPtr.Size;
            Type handleType      = null;

            switch (contextAttribute)
            {
            case ContextAttribute.Sizes:
                nativeBlockSize = SecSizes.SizeOf;
                break;

            case ContextAttribute.StreamSizes:
                nativeBlockSize = StreamSizes.SizeOf;
                break;

            case ContextAttribute.Names:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.PackageInfo:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.NegotiationInfo:
                handleType      = typeof(SafeFreeContextBuffer);
                nativeBlockSize = Marshal.SizeOf(typeof(NegotiationInfo));
                break;

            case ContextAttribute.ClientSpecifiedSpn:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.RemoteCertificate:
                handleType = typeof(SafeFreeCertContext);
                break;

            case ContextAttribute.LocalCertificate:
                handleType = typeof(SafeFreeCertContext);
                break;

            case ContextAttribute.IssuerListInfoEx:
                nativeBlockSize = Marshal.SizeOf(typeof(IssuerListInfoEx));
                handleType      = typeof(SafeFreeContextBuffer);
                break;

            case ContextAttribute.ConnectionInfo:
                nativeBlockSize = Marshal.SizeOf(typeof(SslConnectionInfo));
                break;

            default:
                throw new ArgumentException(SR.GetString(SR.net_invalid_enum, "ContextAttribute"), "contextAttribute");
            }

            SafeHandle SspiHandle = null;
            object     attribute  = null;

            try {
                byte[] nativeBuffer = new byte[nativeBlockSize];
                errorCode = SecModule.QueryContextAttributes(securityContext, contextAttribute, nativeBuffer, handleType, out SspiHandle);
                if (errorCode != 0)
                {
                    GlobalLog.Leave("Win32:QueryContextAttributes", "ERROR = " + ErrorDescription(errorCode));
                    return(null);
                }

                switch (contextAttribute)
                {
                case ContextAttribute.Sizes:
                    attribute = new SecSizes(nativeBuffer);
                    break;

                case ContextAttribute.StreamSizes:
                    attribute = new StreamSizes(nativeBuffer);
                    break;

                case ContextAttribute.Names:
                    attribute = Marshal.PtrToStringUni(SspiHandle.DangerousGetHandle());
                    break;

                case ContextAttribute.PackageInfo:
                    attribute = new SecurityPackageInfoClass(SspiHandle, 0);
                    break;

                case ContextAttribute.NegotiationInfo:
                    unsafe
                    {
                        fixed(void *ptr = nativeBuffer)
                        {
                            attribute = new NegotiationInfoClass(SspiHandle, Marshal.ReadInt32(new IntPtr(ptr), NegotiationInfo.NegotiationStateOffest));
                        }
                    }
                    break;

                case ContextAttribute.ClientSpecifiedSpn:
                    attribute = Marshal.PtrToStringUni(SspiHandle.DangerousGetHandle());
                    break;

                case ContextAttribute.LocalCertificate:
                    goto case ContextAttribute.RemoteCertificate;

                case ContextAttribute.RemoteCertificate:
                    attribute  = SspiHandle;
                    SspiHandle = null;
                    break;

                case ContextAttribute.IssuerListInfoEx:
                    attribute  = new IssuerListInfoEx(SspiHandle, nativeBuffer);
                    SspiHandle = null;
                    break;

                case ContextAttribute.ConnectionInfo:
                    attribute = new SslConnectionInfo(nativeBuffer);
                    break;

                default:
                    // will return null
                    break;
                }
            }
            finally {
                if (SspiHandle != null)
                {
                    SspiHandle.Close();
                }
            }
            GlobalLog.Leave("QueryContextAttributes", ValidationHelper.ToString(attribute));
            return(attribute);
        }
        public static object QueryContextAttributes(SSPIInterface SecModule, SafeDeleteContext securityContext, ContextAttribute contextAttribute, out int errorCode) {
            GlobalLog.Enter("QueryContextAttributes", contextAttribute.ToString());

            int nativeBlockSize = IntPtr.Size;
            Type    handleType = null;

            switch (contextAttribute) {
                case ContextAttribute.Sizes:
                    nativeBlockSize = SecSizes.SizeOf;
                    break;
                case ContextAttribute.StreamSizes:
                    nativeBlockSize = StreamSizes.SizeOf;
                    break;

                case ContextAttribute.Names:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.PackageInfo:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.NegotiationInfo:
                    handleType = typeof(SafeFreeContextBuffer);
                    nativeBlockSize = Marshal.SizeOf(typeof(NegotiationInfo));
                    break;

                case ContextAttribute.ClientSpecifiedSpn:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.RemoteCertificate:
                    handleType = typeof(SafeFreeCertContext);
                    break;

                case ContextAttribute.LocalCertificate:
                    handleType = typeof(SafeFreeCertContext);
                    break;

                case ContextAttribute.IssuerListInfoEx:
                    nativeBlockSize = Marshal.SizeOf(typeof(IssuerListInfoEx));
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.ConnectionInfo:
                    nativeBlockSize = Marshal.SizeOf(typeof(SslConnectionInfo));
                    break;

                default:
                    throw new ArgumentException(SR.GetString(SR.net_invalid_enum, "ContextAttribute"), "contextAttribute");
            }

            SafeHandle SspiHandle = null;
            object attribute = null;

            try {
                byte[] nativeBuffer = new byte[nativeBlockSize];
                errorCode = SecModule.QueryContextAttributes(securityContext, contextAttribute, nativeBuffer, handleType, out SspiHandle);
                if (errorCode != 0) {
                    GlobalLog.Leave("Win32:QueryContextAttributes", "ERROR = " + ErrorDescription(errorCode));
                    return null;
                }

                switch (contextAttribute) {
                    case ContextAttribute.Sizes:
                        attribute = new SecSizes(nativeBuffer);
                        break;
                  
                    case ContextAttribute.StreamSizes:
                        attribute = new StreamSizes(nativeBuffer);
                        break;
                    
                    case ContextAttribute.Names:
                        attribute = Marshal.PtrToStringUni(SspiHandle.DangerousGetHandle());
                        break;
                    
                    case ContextAttribute.PackageInfo:
                        attribute = new SecurityPackageInfoClass(SspiHandle, 0);
                        break;
                    
                    case ContextAttribute.NegotiationInfo:
                        unsafe {
                            fixed (void* ptr=nativeBuffer) {
                                attribute = new NegotiationInfoClass(SspiHandle, Marshal.ReadInt32(new IntPtr(ptr), NegotiationInfo.NegotiationStateOffest));
                            }
                        }
                        break;
                    
                    case ContextAttribute.ClientSpecifiedSpn:
                        attribute = Marshal.PtrToStringUni(SspiHandle.DangerousGetHandle());
                        break;
                    
                    case ContextAttribute.LocalCertificate:
                        goto case ContextAttribute.RemoteCertificate;
                    case ContextAttribute.RemoteCertificate:
                        attribute = SspiHandle;
                        SspiHandle = null;
                        break;
                    
                    case ContextAttribute.IssuerListInfoEx:
                        attribute =  new IssuerListInfoEx(SspiHandle, nativeBuffer);
                        SspiHandle = null;
                        break;
                    
                    case ContextAttribute.ConnectionInfo:
                        attribute = new SslConnectionInfo(nativeBuffer);
                        break;
                    default:
                        // will return null
                        break;
                }
            }
            finally {
                if (SspiHandle != null) {
                    SspiHandle.Close();
                }
            }
            GlobalLog.Leave("QueryContextAttributes", ValidationHelper.ToString(attribute));
            return attribute;
        }
        public static object QueryContextAttributes(
            SSPIInterface SecModule,
            SecurityContext securityContext,
            ContextAttribute contextAttribute)
        {
            GlobalLog.Enter("QueryContextAttributes#2");

            int nativeBlockSize;

            switch (contextAttribute)
            {
            case ContextAttribute.StreamSizes:
                nativeBlockSize = 20;
                break;

            case ContextAttribute.Names:
                nativeBlockSize = IntPtr.Size;
                break;

            case ContextAttribute.PackageInfo:
                nativeBlockSize = IntPtr.Size;
                break;

            case ContextAttribute.RemoteCertificate:
                nativeBlockSize = IntPtr.Size;
                break;

            case ContextAttribute.LocalCertificate:
                nativeBlockSize = IntPtr.Size;
                break;

            case ContextAttribute.IssuerListInfoEx:
                nativeBlockSize = Marshal.SizeOf(typeof(IssuerListInfoEx));
                break;

            default:
                nativeBlockSize = IntPtr.Size;
                GlobalLog.Assert(false,
                                 "contextAttribute unexpected value", "");
                break;
            }

            IntPtr nativeBlock = Marshal.AllocHGlobal((IntPtr)nativeBlockSize);

            int errorCode =
                SecModule.QueryContextAttributes(
                    ref securityContext.Handle,
                    (int)contextAttribute,
                    nativeBlock);

            object attribute = null;

            if (errorCode == 0)
            {
                switch (contextAttribute)
                {
                case ContextAttribute.StreamSizes:
                    attribute = new StreamSizes(nativeBlock);
                    break;

                case ContextAttribute.Names:

                    IntPtr unmanagedString = Marshal.ReadIntPtr(nativeBlock);

                    if (ComNetOS.IsWin9x)
                    {
                        attribute = Marshal.PtrToStringAnsi(unmanagedString);
                    }
                    else
                    {
                        attribute = Marshal.PtrToStringUni(unmanagedString);
                    }
                    //SecModule.FreeContextBuffer(unmanagedString);
                    break;

                case ContextAttribute.PackageInfo:

                    IntPtr unmanagedBlock = Marshal.ReadIntPtr(nativeBlock);

                    attribute = new SecurityPackageInfoClass(SecModule, unmanagedBlock);
                    //SecModule.FreeContextBuffer(unmanagedBlock);

                    break;

                case ContextAttribute.LocalCertificate:
                    goto case ContextAttribute.RemoteCertificate;

                case ContextAttribute.RemoteCertificate:
                    IntPtr contextHandle = Marshal.ReadIntPtr(nativeBlock);
                    if (contextHandle == ((IntPtr)0))
                    {
                        Debug.Assert(false,
                                     "contextHandle == 0",
                                     "QueryContextAttributes: marshalled certificate context is null on success"
                                     );
                    }
                    else
                    {
                        attribute = new CertificateContextHandle(contextHandle);
                    }
                    break;

                case ContextAttribute.IssuerListInfoEx:
                    IssuerListInfoEx issuerList = new IssuerListInfoEx();

                    issuerList.issuerArray = Marshal.ReadIntPtr(nativeBlock, 0);
                    issuerList.issuerCount = Marshal.ReadInt32(nativeBlock, 4);

                    attribute = issuerList;
                    break;

                default:
                    // will return null
                    break;
                }
            }
            else
            {
                //Win32Exception win32Exception = new Win32Exception(errorCode);
                //Console.WriteLine(win32Exception.Message);
            }

            Marshal.FreeHGlobal(nativeBlock);

            GlobalLog.Leave("QueryContextAttributes#2");
            return(attribute);
        }
        public static unsafe object QueryContextAttributes(SSPIInterface SecModule, SafeDeleteContext securityContext, ContextAttribute contextAttribute, out int errorCode)
        {
            int size = IntPtr.Size;
            Type handleType = null;
            switch (contextAttribute)
            {
                case ContextAttribute.Sizes:
                    size = SecSizes.SizeOf;
                    break;

                case ContextAttribute.Names:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.StreamSizes:
                    size = StreamSizes.SizeOf;
                    break;

                case ContextAttribute.PackageInfo:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.NegotiationInfo:
                    handleType = typeof(SafeFreeContextBuffer);
                    size = Marshal.SizeOf(typeof(NegotiationInfo));
                    break;

                case ContextAttribute.RemoteCertificate:
                    handleType = typeof(SafeFreeCertContext);
                    break;

                case ContextAttribute.LocalCertificate:
                    handleType = typeof(SafeFreeCertContext);
                    break;

                case ContextAttribute.ClientSpecifiedSpn:
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.IssuerListInfoEx:
                    size = Marshal.SizeOf(typeof(IssuerListInfoEx));
                    handleType = typeof(SafeFreeContextBuffer);
                    break;

                case ContextAttribute.ConnectionInfo:
                    size = Marshal.SizeOf(typeof(SslConnectionInfo));
                    break;

                default:
                    throw new ArgumentException(SR.GetString("net_invalid_enum", new object[] { "ContextAttribute" }), "contextAttribute");
            }
            SafeHandle refHandle = null;
            object obj2 = null;
            try
            {
                byte[] buffer = new byte[size];
                errorCode = SecModule.QueryContextAttributes(securityContext, contextAttribute, buffer, handleType, out refHandle);
                if (errorCode != 0)
                {
                    return null;
                }
                ContextAttribute attribute2 = contextAttribute;
                if (attribute2 <= ContextAttribute.NegotiationInfo)
                {
                    switch (attribute2)
                    {
                        case ContextAttribute.Sizes:
                            return new SecSizes(buffer);

                        case ContextAttribute.Names:
                            if (!ComNetOS.IsWin9x)
                            {
                                return Marshal.PtrToStringUni(refHandle.DangerousGetHandle());
                            }
                            return Marshal.PtrToStringAnsi(refHandle.DangerousGetHandle());

                        case ContextAttribute.Lifespan:
                        case ContextAttribute.DceInfo:
                            return obj2;

                        case ContextAttribute.StreamSizes:
                            return new StreamSizes(buffer);

                        case ContextAttribute.PackageInfo:
                            return new SecurityPackageInfoClass(refHandle, 0);

                        case (ContextAttribute.PackageInfo | ContextAttribute.Names):
                            return obj2;

                        case ContextAttribute.NegotiationInfo:
                            goto Label_0229;
                    }
                    return obj2;
                }
                switch (attribute2)
                {
                    case ContextAttribute.RemoteCertificate:
                    case ContextAttribute.LocalCertificate:
                        obj2 = refHandle;
                        refHandle = null;
                        return obj2;

                    case ContextAttribute.ClientSpecifiedSpn:
                        goto Label_0266;

                    case ContextAttribute.IssuerListInfoEx:
                        obj2 = new IssuerListInfoEx(refHandle, buffer);
                        refHandle = null;
                        return obj2;

                    case ContextAttribute.ConnectionInfo:
                        return new SslConnectionInfo(buffer);

                    default:
                        return obj2;
                }
            Label_0229:
                try
                {
                    byte[] buffer2;
                    if (((buffer2 = buffer) == null) || (buffer2.Length == 0))
                    {
                        fixed (IntPtr* ptrRef = null)
                        {
                        }
                    }
                    return new NegotiationInfoClass(refHandle, Marshal.ReadInt32(new IntPtr((void*) ptrRef), NegotiationInfo.NegotiationStateOffest));
                }
                finally
                {
                    ptrRef = null;
                }
            Label_0266:
                return Marshal.PtrToStringUni(refHandle.DangerousGetHandle());
            }
            finally
            {
                if (refHandle != null)
                {
                    refHandle.Close();
                }
            }
            return obj2;
        }