Esempio n. 1
0
        public static unsafe int EncryptDecryptHelper(SafeDeleteContext context, SecurityBuffer[] input, uint sequenceNumber, bool encrypt, bool isGssBlob)
        {
            SecurityBufferDescriptor sdcInOut = new SecurityBufferDescriptor(input.Length);

            SecurityBufferStruct[] unmanagedBuffer = new SecurityBufferStruct[input.Length];
            byte[][] buffers = new byte[input.Length][];
            fixed(void *unmanagedBufferPtr = unmanagedBuffer)
            {
                sdcInOut.UnmanagedPointer = unmanagedBufferPtr;
                GCHandle[] pinnedBuffers = new GCHandle[input.Length];
                try
                {
                    for (int i = 0; i < input.Length; ++i)
                    {
                        SecurityBuffer iBuffer = input[i];
                        unmanagedBuffer[i].count = iBuffer.size;
                        unmanagedBuffer[i].type  = iBuffer.type;
                        if (iBuffer.token == null || iBuffer.token.Length == 0)
                        {
                            unmanagedBuffer[i].token = IntPtr.Zero;
                        }
                        else
                        {
                            pinnedBuffers[i]         = GCHandle.Alloc(iBuffer.token, GCHandleType.Pinned);
                            unmanagedBuffer[i].token = Marshal.UnsafeAddrOfPinnedArrayElement(iBuffer.token, iBuffer.offset);
                            buffers[i] = iBuffer.token;
                        }
                    }
                    int errorCode;
                    if (encrypt)
                    {
                        errorCode = SafeDeleteContext.EncryptMessage(context, sdcInOut, sequenceNumber);
                    }
                    else
                    {
                        errorCode = SafeDeleteContext.DecryptMessage(context, sdcInOut, sequenceNumber);
                    }
                    // Marshalling back returned sizes (do not marshal the "token" field)
                    for (int i = 0; i < input.Length; ++i)
                    {
                        SecurityBuffer iBuffer = input[i];
                        iBuffer.size = unmanagedBuffer[i].count;
                        iBuffer.type = unmanagedBuffer[i].type;
                        if (iBuffer.size == 0)
                        {
                            iBuffer.offset = 0;
                            iBuffer.token  = null;
                        }
                        else if (isGssBlob && !encrypt && iBuffer.type == BufferType.Data)
                        {
                            iBuffer.token = DiagnosticUtility.Utility.AllocateByteArray(iBuffer.size);
                            Marshal.Copy(unmanagedBuffer[i].token, iBuffer.token, 0, iBuffer.size);
                        }
                        else
                        {
                            checked
                            {
                                // Find the buffer this is inside of.  Usually they all point inside buffer 0.
                                int j;
                                for (j = 0; j < input.Length; j++)
                                {
                                    if (buffers[j] == null)
                                    {
                                        continue;
                                    }

                                    byte *bufferAddress = (byte *)Marshal.UnsafeAddrOfPinnedArrayElement(buffers[j], 0);
                                    if ((byte *)unmanagedBuffer[i].token >= bufferAddress &&
                                        (byte *)unmanagedBuffer[i].token + iBuffer.size <= bufferAddress + buffers[j].Length)
                                    {
                                        iBuffer.offset = (int)((byte *)unmanagedBuffer[i].token - bufferAddress);
                                        iBuffer.token  = buffers[j];
                                        break;
                                    }
                                }

                                if (j >= input.Length)
                                {
                                    iBuffer.size   = 0;
                                    iBuffer.offset = 0;
                                    iBuffer.token  = null;
                                }
                                if (!(iBuffer.offset >= 0 && iBuffer.offset <= (iBuffer.token == null ? 0 : iBuffer.token.Length)))
                                {
                                    DiagnosticUtility.DebugAssert(SR.GetString(SR.SspiWrapperEncryptDecryptAssert1, iBuffer.offset));
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SspiWrapperEncryptDecryptAssert1, iBuffer.offset)));
                                }
                                if (!(iBuffer.size >= 0 && iBuffer.size <= (iBuffer.token == null ? 0 : iBuffer.token.Length - iBuffer.offset)))
                                {
                                    DiagnosticUtility.DebugAssert(SR.GetString(SR.SspiWrapperEncryptDecryptAssert2, iBuffer.size));
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SspiWrapperEncryptDecryptAssert2, iBuffer.size)));
                                }
                            }
                        }
                    }
                    return(errorCode);
                }
                finally
                {
                    for (int i = 0; i < pinnedBuffers.Length; ++i)
                    {
                        if (pinnedBuffers[i].IsAllocated)
                        {
                            pinnedBuffers[i].Free();
                        }
                    }
                }
            }
        }
        public static unsafe int EncryptDecryptHelper(SafeDeleteContext context, SecurityBuffer[] input, uint sequenceNumber, bool encrypt, bool isGssBlob)
        {
            int num6;

            SecurityBufferStruct[]   structArray2;
            SecurityBufferDescriptor inputOutput = new SecurityBufferDescriptor(input.Length);

            SecurityBufferStruct[] structArray = new SecurityBufferStruct[input.Length];
            byte[][] bufferArray = new byte[input.Length][];
            if (((structArray2 = structArray) == null) || (structArray2.Length == 0))
            {
                fixed(IntPtr *ptrRef = null)
                {
                }
            }
            inputOutput.UnmanagedPointer = (void *)ptrRef;
            GCHandle[] handleArray = new GCHandle[input.Length];
            try
            {
                int num2;
                for (int i = 0; i < input.Length; i++)
                {
                    SecurityBuffer buffer = input[i];
                    structArray[i].count = buffer.size;
                    structArray[i].type  = buffer.type;
                    if ((buffer.token == null) || (buffer.token.Length == 0))
                    {
                        structArray[i].token = IntPtr.Zero;
                    }
                    else
                    {
                        handleArray[i]       = GCHandle.Alloc(buffer.token, GCHandleType.Pinned);
                        structArray[i].token = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.token, buffer.offset);
                        bufferArray[i]       = buffer.token;
                    }
                }
                if (encrypt)
                {
                    num2 = SafeDeleteContext.EncryptMessage(context, inputOutput, sequenceNumber);
                }
                else
                {
                    num2 = SafeDeleteContext.DecryptMessage(context, inputOutput, sequenceNumber);
                }
                for (int j = 0; j < input.Length; j++)
                {
                    SecurityBuffer buffer2 = input[j];
                    buffer2.size = structArray[j].count;
                    buffer2.type = structArray[j].type;
                    if (buffer2.size == 0)
                    {
                        buffer2.offset = 0;
                        buffer2.token  = null;
                        continue;
                    }
                    if ((isGssBlob && !encrypt) && (buffer2.type == BufferType.Data))
                    {
                        buffer2.token = DiagnosticUtility.Utility.AllocateByteArray(buffer2.size);
                        Marshal.Copy(structArray[j].token, buffer2.token, 0, buffer2.size);
                        continue;
                    }
                    int index = 0;
                    while (index < input.Length)
                    {
                        if (bufferArray[index] != null)
                        {
                            byte *numPtr = (byte *)Marshal.UnsafeAddrOfPinnedArrayElement(bufferArray[index], 0);
                            if ((((void *)structArray[j].token) >= numPtr) && ((((void *)structArray[j].token) + buffer2.size) <= (numPtr + bufferArray[index].Length)))
                            {
                                buffer2.offset = (int)((long)((((void *)structArray[j].token) - numPtr) / 1));
                                buffer2.token  = bufferArray[index];
                                break;
                            }
                        }
                        index++;
                    }
                    if (index >= input.Length)
                    {
                        buffer2.size   = 0;
                        buffer2.offset = 0;
                        buffer2.token  = null;
                    }
                    if ((buffer2.offset < 0) || (buffer2.offset > ((buffer2.token == null) ? 0 : buffer2.token.Length)))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.IdentityModel.SR.GetString("SspiWrapperEncryptDecryptAssert1", new object[] { buffer2.offset })));
                    }
                    if ((buffer2.size < 0) || (buffer2.size > ((buffer2.token == null) ? 0 : (buffer2.token.Length - buffer2.offset))))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.IdentityModel.SR.GetString("SspiWrapperEncryptDecryptAssert2", new object[] { buffer2.size })));
                    }
                }
                num6 = num2;
            }
            finally
            {
                for (int k = 0; k < handleArray.Length; k++)
                {
                    if (handleArray[k].IsAllocated)
                    {
                        handleArray[k].Free();
                    }
                }
            }
            return(num6);
        }