public static byte[] QueryContextAttributes(
            SSPIInterface SecModule,
            SecurityContext securityContext,
            ContextAttribute contextAttribute,
            int bytesRequired)
        {
            GlobalLog.Enter("QueryContextAttributes#1");

            byte[] attributeBuffer = new byte[bytesRequired];

            GCHandle pinnedBuffer       = GCHandle.Alloc(attributeBuffer, GCHandleType.Pinned);
            IntPtr   addrOfPinnedBuffer = pinnedBuffer.AddrOfPinnedObject();

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

            pinnedBuffer.Free();

            GlobalLog.Leave("QueryContextAttributes#1");
            return(attributeBuffer);
        }
Beispiel #2
0
        public static object QueryContextAttributes(SSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, out int errorCode)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(null, contextAttribute);
            }

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

            switch (contextAttribute)
            {
            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES:
                nativeBlockSize = SecPkgContext_Sizes.SizeOf;
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES:
                nativeBlockSize = SecPkgContext_StreamSizes.SizeOf;
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO:
                handleType = typeof(SafeFreeContextBuffer);
                unsafe
                {
                    nativeBlockSize = sizeof(SecPkgContext_NegotiationInfoW);
                }
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT:
                handleType = typeof(SafeFreeCertContext);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT:
                handleType = typeof(SafeFreeCertContext);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX:
                nativeBlockSize = Marshal.SizeOf <Interop.SspiCli.SecPkgContext_IssuerListInfoEx>();
                handleType      = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO:
                nativeBlockSize = Marshal.SizeOf <SecPkgContext_ConnectionInfo>();
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_APPLICATION_PROTOCOL:
                nativeBlockSize = Marshal.SizeOf <Interop.SecPkgContext_ApplicationProtocol>();
                break;

            default:
                throw new ArgumentException(System.StringsHelper.Format(Strings.net_invalid_enum, nameof(contextAttribute)), nameof(contextAttribute));
            }

            SafeHandle sspiHandle = null;
            object     attribute  = null;

            try
            {
                var nativeBuffer = new byte[nativeBlockSize];
                errorCode = secModule.QueryContextAttributes(securityContext, contextAttribute, nativeBuffer, handleType, out sspiHandle);
                if (errorCode != 0)
                {
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Exit(null, $"ERROR = {ErrorDescription(errorCode)}");
                    }
                    return(null);
                }

                switch (contextAttribute)
                {
                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES:
                    attribute = new SecPkgContext_Sizes(nativeBuffer);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES:
                    attribute = new SecPkgContext_StreamSizes(nativeBuffer);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO:
                    attribute = new SecurityPackageInfoClass(sspiHandle, 0);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO:
                    unsafe
                    {
                        fixed(void *ptr = &nativeBuffer[0])
                        {
                            attribute = new NegotiationInfoClass(sspiHandle, (int)((SecPkgContext_NegotiationInfoW *)ptr)->NegotiationState);
                        }
                    }
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT:
                // Fall-through to RemoteCertificate is intentional.
                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT:
                    attribute  = sspiHandle;
                    sspiHandle = null;
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX:
                    attribute  = new Interop.SspiCli.SecPkgContext_IssuerListInfoEx(sspiHandle, nativeBuffer);
                    sspiHandle = null;
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO:
                    attribute = new SecPkgContext_ConnectionInfo(nativeBuffer);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_APPLICATION_PROTOCOL:
                    unsafe
                    {
                        fixed(void *ptr = nativeBuffer)
                        {
                            attribute = Marshal.PtrToStructure <Interop.SecPkgContext_ApplicationProtocol>(new IntPtr(ptr));
                        }
                    }
                    break;

                default:
                    // Will return null.
                    break;
                }
            }
            finally
            {
                if (sspiHandle != null)
                {
                    sspiHandle.Dispose();
                }
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(null, attribute);
            }
            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);
        }
Beispiel #4
0
        public static object QueryContextAttributes(SSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, out int errorCode)
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Enter(nameof(QueryContextAttributes), contextAttribute.ToString());
            }

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

            switch (contextAttribute)
            {
            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES:
                nativeBlockSize = SecPkgContext_Sizes.SizeOf;
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES:
                nativeBlockSize = SecPkgContext_StreamSizes.SizeOf;
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO:
                handleType      = typeof(SafeFreeContextBuffer);
                nativeBlockSize = Marshal.SizeOf <SecPkgContext_NegotiationInfoW>();
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET:
                handleType = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT:
                handleType = typeof(SafeFreeCertContext);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT:
                handleType = typeof(SafeFreeCertContext);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX:
                nativeBlockSize = Marshal.SizeOf <Interop.SspiCli.SecPkgContext_IssuerListInfoEx>();
                handleType      = typeof(SafeFreeContextBuffer);
                break;

            case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO:
                nativeBlockSize = Marshal.SizeOf <SecPkgContext_ConnectionInfo>();
                break;

            default:
                throw new ArgumentException(SR.Format(SR.net_invalid_enum, nameof(contextAttribute)), nameof(contextAttribute));
            }

            SafeHandle sspiHandle = null;
            object     attribute  = null;

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

                switch (contextAttribute)
                {
                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SIZES:
                    attribute = new SecPkgContext_Sizes(nativeBuffer);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_STREAM_SIZES:
                    attribute = new SecPkgContext_StreamSizes(nativeBuffer);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NAMES:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_PACKAGE_INFO:
                    attribute = new SecurityPackageInfoClass(sspiHandle, 0);
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_NEGOTIATION_INFO:
                    unsafe
                    {
                        fixed(void *ptr = nativeBuffer)
                        {
                            attribute = new NegotiationInfoClass(sspiHandle, Marshal.ReadInt32(new IntPtr(ptr), SecPkgContext_NegotiationInfoW.NegotiationStateOffest));
                        }
                    }
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CLIENT_SPECIFIED_TARGET:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_LOCAL_CERT_CONTEXT:
                // Fall-through to RemoteCertificate is intentional.
                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT:
                    attribute  = sspiHandle;
                    sspiHandle = null;
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_ISSUER_LIST_EX:
                    attribute  = new Interop.SspiCli.SecPkgContext_IssuerListInfoEx(sspiHandle, nativeBuffer);
                    sspiHandle = null;
                    break;

                case Interop.SspiCli.ContextAttribute.SECPKG_ATTR_CONNECTION_INFO:
                    attribute = new SecPkgContext_ConnectionInfo(nativeBuffer);
                    break;

                default:
                    // Will return null.
                    break;
                }
            }
            finally
            {
                if (sspiHandle != null)
                {
                    sspiHandle.Dispose();
                }
            }

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Leave(nameof(QueryContextAttributes), LoggingHash.ObjectToString(attribute));
            }

            return(attribute);
        }
Beispiel #5
0
        public static object QueryContextAttributes(SSPIInterface secModule, SafeDeleteContext securityContext, Interop.SspiCli.ContextAttribute contextAttribute, out int errorCode)
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Enter("QueryContextAttributes", contextAttribute.ToString());
            }

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

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

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

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

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

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

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

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

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

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

            case Interop.SspiCli.ContextAttribute.ConnectionInfo:
                nativeBlockSize = Marshal.SizeOf <SslConnectionInfo>();
                break;

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

            SafeHandle sspiHandle = null;
            object     attribute  = null;

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

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

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

                case Interop.SspiCli.ContextAttribute.Names:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.PackageInfo:
                    attribute = new SecurityPackageInfoClass(sspiHandle, 0);
                    break;

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

                case Interop.SspiCli.ContextAttribute.ClientSpecifiedSpn:
                    attribute = Marshal.PtrToStringUni(sspiHandle.DangerousGetHandle());
                    break;

                case Interop.SspiCli.ContextAttribute.LocalCertificate:
                // Fall-through to RemoteCertificate is intentional.
                case Interop.SspiCli.ContextAttribute.RemoteCertificate:
                    attribute  = sspiHandle;
                    sspiHandle = null;
                    break;

                case Interop.SspiCli.ContextAttribute.IssuerListInfoEx:
                    attribute  = new Interop.SspiCli.IssuerListInfoEx(sspiHandle, nativeBuffer);
                    sspiHandle = null;
                    break;

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

                default:
                    // Will return null.
                    break;
                }
            }
            finally
            {
                if (sspiHandle != null)
                {
                    sspiHandle.Dispose();
                }
            }

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Leave("QueryContextAttributes", LoggingHash.ObjectToString(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;
        }