internal static SecurityPackageInfoClass[] EnumerateSecurityPackages(SSPIInterface SecModule)
 {
     GlobalLog.Enter("EnumerateSecurityPackages");
     if (SecModule.SecurityPackages == null)
     {
         lock (SecModule) {
             if (SecModule.SecurityPackages == null)
             {
                 int moduleCount = 0;
                 SafeFreeContextBuffer arrayBaseHandle = null;
                 try {
                     int errorCode = SecModule.EnumerateSecurityPackages(out moduleCount, out arrayBaseHandle);
                     GlobalLog.Print("SSPIWrapper::arrayBase: " + (arrayBaseHandle.DangerousGetHandle().ToString("x")));
                     if (errorCode != 0)
                     {
                         throw new Win32Exception(errorCode);
                     }
                     SecurityPackageInfoClass[] securityPackages = new SecurityPackageInfoClass[moduleCount];
                     if (Logging.On)
                     {
                         Logging.PrintInfo(Logging.Web, SR.GetString(SR.net_log_sspi_enumerating_security_packages));
                     }
                     int i;
                     for (i = 0; i < moduleCount; i++)
                     {
                         securityPackages[i] = new SecurityPackageInfoClass(arrayBaseHandle, i);
                         if (Logging.On)
                         {
                             Logging.PrintInfo(Logging.Web, "    " + securityPackages[i].Name);
                         }
                     }
                     SecModule.SecurityPackages = securityPackages;
                 }
                 finally {
                     if (arrayBaseHandle != null)
                     {
                         arrayBaseHandle.Close();
                     }
                 }
             }
         }
     }
     GlobalLog.Leave("EnumerateSecurityPackages");
     return(SecModule.SecurityPackages);
 }
 internal static SecurityPackageInfoClass[] EnumerateSecurityPackages(SSPIInterface SecModule)
 {
     if (SecModule.SecurityPackages == null)
     {
         lock (SecModule)
         {
             if (SecModule.SecurityPackages == null)
             {
                 int pkgnum = 0;
                 SafeFreeContextBuffer pkgArray = null;
                 try
                 {
                     int error = SecModule.EnumerateSecurityPackages(out pkgnum, out pkgArray);
                     if (error != 0)
                     {
                         throw new Win32Exception(error);
                     }
                     SecurityPackageInfoClass[] classArray = new SecurityPackageInfoClass[pkgnum];
                     if (Logging.On)
                     {
                         Logging.PrintInfo(Logging.Web, SR.GetString("net_log_sspi_enumerating_security_packages"));
                     }
                     for (int i = 0; i < pkgnum; i++)
                     {
                         classArray[i] = new SecurityPackageInfoClass(pkgArray, i);
                         if (Logging.On)
                         {
                             Logging.PrintInfo(Logging.Web, "    " + classArray[i].Name);
                         }
                     }
                     SecModule.SecurityPackages = classArray;
                 }
                 finally
                 {
                     if (pkgArray != null)
                     {
                         pkgArray.Close();
                     }
                 }
             }
         }
     }
     return(SecModule.SecurityPackages);
 }
        internal static unsafe int AcceptSecurityContext(SecurDll dll, ref SafeFreeCredentials inCredentials, ref SafeDeleteContext refContext, ContextFlags inFlags, Endianness endianness, SecurityBuffer inSecBuffer, SecurityBuffer[] inSecBuffers, SecurityBuffer outSecBuffer, ref ContextFlags outFlags)
        {
            if (inCredentials == null)
            {
                throw new ArgumentNullException("inCredentials");
            }
            SecurityBufferDescriptor inputBuffer = null;

            if (inSecBuffer != null)
            {
                inputBuffer = new SecurityBufferDescriptor(1);
            }
            else if (inSecBuffers != null)
            {
                inputBuffer = new SecurityBufferDescriptor(inSecBuffers.Length);
            }
            SecurityBufferDescriptor outputBuffer = new SecurityBufferDescriptor(1);
            bool       flag   = (inFlags & ContextFlags.AllocateMemory) != ContextFlags.Zero;
            int        num    = -1;
            SSPIHandle handle = new SSPIHandle();

            if (refContext != null)
            {
                handle = refContext._handle;
            }
            GCHandle[]            handleArray    = null;
            GCHandle              handle2        = new GCHandle();
            SafeFreeContextBuffer handleTemplate = null;

            try
            {
                handle2 = GCHandle.Alloc(outSecBuffer.token, GCHandleType.Pinned);
                SecurityBufferStruct[] structArray = new SecurityBufferStruct[(inputBuffer == null) ? 1 : inputBuffer.Count];
                try
                {
                    SecurityBufferStruct[] structArray3;
                    if (((structArray3 = structArray) == null) || (structArray3.Length == 0))
                    {
                        ptrRef = null;
                        goto Label_00A5;
                    }
                    fixed(IntPtr *ptrRef = structArray3)
                    {
Label_00A5:
                        if (inputBuffer != null)
                        {
                            inputBuffer.UnmanagedPointer = (void *)ptrRef;
                            handleArray = new GCHandle[inputBuffer.Count];
                            for (int i = 0; i < inputBuffer.Count; i++)
                            {
                                SecurityBuffer buffer2 = (inSecBuffer != null) ? inSecBuffer : inSecBuffers[i];
                                if (buffer2 != null)
                                {
                                    structArray[i].count = buffer2.size;
                                    structArray[i].type  = buffer2.type;
                                    if (buffer2.unmanagedToken != null)
                                    {
                                        structArray[i].token = buffer2.unmanagedToken.DangerousGetHandle();
                                    }
                                    else if ((buffer2.token == null) || (buffer2.token.Length == 0))
                                    {
                                        structArray[i].token = IntPtr.Zero;
                                    }
                                    else
                                    {
                                        handleArray[i]       = GCHandle.Alloc(buffer2.token, GCHandleType.Pinned);
                                        structArray[i].token = Marshal.UnsafeAddrOfPinnedArrayElement(buffer2.token, buffer2.offset);
                                    }
                                }
                            }
                        }
                        SecurityBufferStruct[] structArray2 = new SecurityBufferStruct[1];
                        try
                        {
                            SecurityBufferStruct[] structArray4;
                            if (((structArray4 = structArray2) == null) || (structArray4.Length == 0))
                            {
                                ptrRef2 = null;
                                goto Label_01CB;
                            }
                            fixed(IntPtr *ptrRef2 = structArray4)
                            {
Label_01CB:
                                outputBuffer.UnmanagedPointer = (void *)ptrRef2;
                                structArray2[0].count         = outSecBuffer.size;
                                structArray2[0].type          = outSecBuffer.type;
                                if ((outSecBuffer.token == null) || (outSecBuffer.token.Length == 0))
                                {
                                    structArray2[0].token = IntPtr.Zero;
                                }
                                else
                                {
                                    structArray2[0].token = Marshal.UnsafeAddrOfPinnedArrayElement(outSecBuffer.token, outSecBuffer.offset);
                                }
                                if (flag)
                                {
                                    handleTemplate = SafeFreeContextBuffer.CreateEmptyHandle(dll);
                                }
                                switch (dll)
                                {
                                case SecurDll.SECURITY:
                                    if ((refContext == null) || refContext.IsInvalid)
                                    {
                                        refContext = new SafeDeleteContext_SECURITY();
                                    }
                                    num = MustRunAcceptSecurityContext_SECURITY(ref inCredentials, handle.IsZero ? null : ((void *)&handle), inputBuffer, inFlags, endianness, refContext, outputBuffer, ref outFlags, handleTemplate);
                                    break;

                                case SecurDll.SECUR32:
                                    if ((refContext == null) || refContext.IsInvalid)
                                    {
                                        refContext = new SafeDeleteContext_SECUR32();
                                    }
                                    num = MustRunAcceptSecurityContext_SECUR32(ref inCredentials, handle.IsZero ? null : ((void *)&handle), inputBuffer, inFlags, endianness, refContext, outputBuffer, ref outFlags, handleTemplate);
                                    break;

                                case SecurDll.SCHANNEL:
                                    if ((refContext == null) || refContext.IsInvalid)
                                    {
                                        refContext = new SafeDeleteContext_SCHANNEL();
                                    }
                                    num = MustRunAcceptSecurityContext_SCHANNEL(ref inCredentials, handle.IsZero ? null : ((void *)&handle), inputBuffer, inFlags, endianness, refContext, outputBuffer, ref outFlags, handleTemplate);
                                    break;

                                default:
                                    throw new ArgumentException(SR.GetString("net_invalid_enum", new object[] { "SecurDll" }), "Dll");
                                }
                                outSecBuffer.size = structArray2[0].count;
                                outSecBuffer.type = structArray2[0].type;
                                if (outSecBuffer.size > 0)
                                {
                                    outSecBuffer.token = new byte[outSecBuffer.size];
                                    Marshal.Copy(structArray2[0].token, outSecBuffer.token, 0, outSecBuffer.size);
                                    return(num);
                                }
                                outSecBuffer.token = null;
                                return(num);
                            }
                        }
                        finally
                        {
                            ptrRef2 = null;
                        }
                        return(num);
                    }
                }
                finally
                {
                    ptrRef = null;
                }
            }
            finally
            {
                if (handleArray != null)
                {
                    for (int j = 0; j < handleArray.Length; j++)
                    {
                        if (handleArray[j].IsAllocated)
                        {
                            handleArray[j].Free();
                        }
                    }
                }
                if (handle2.IsAllocated)
                {
                    handle2.Free();
                }
                if (handleTemplate != null)
                {
                    handleTemplate.Close();
                }
            }
            return(num);
        }