Example #1
0
        private bool Read()
        {
            if (!base.ReadHeader())
            {
                return(false);
            }
            for (int i = 0; i < (int)this.dataSizeForRead; i += base.Read(this.dataBufferForRead, i, (int)this.dataSizeForRead - i))
            {
            }
            for (int j = 0; j < (int)this.tokenSizeForRead; j += base.Read(this.tokenBufferForRead, j, (int)this.tokenSizeForRead - j))
            {
            }
            SecurityBufferClass securityBufferClass = this.securityBuffers[0];

            securityBufferClass.offset = 0;
            securityBufferClass.size   = (int)this.dataSizeForRead;
            securityBufferClass.token  = this.dataBufferForRead;
            securityBufferClass.type   = 1;
            securityBufferClass        = this.securityBuffers[1];
            securityBufferClass.offset = 0;
            securityBufferClass.size   = (int)this.tokenSizeForRead;
            securityBufferClass.token  = this.tokenBufferForRead;
            securityBufferClass.type   = 2;
            return(true);
        }
        internal static int InitializeSecurityContext(ref SecurityHandle credential, ref SecurityHandle context, string targetName, int requirements, Endianness datarep, SecurityBufferClass inputBuffer, ref SecurityHandle newContext, SecurityBufferClass outputBuffer, ref int attributes, ref long timestamp)
        {
            SecurityBufferClass[] inputBuffers = null;
            SecurityBufferClass[] array        = null;
            if (inputBuffer != null)
            {
                inputBuffers = new SecurityBufferClass[]
                {
                    inputBuffer
                };
            }
            if (outputBuffer != null)
            {
                array = new SecurityBufferClass[]
                {
                    outputBuffer
                };
            }
            int result = SSPIWrapper.InitializeSecurityContext(ref credential, ref context, targetName, requirements, datarep, inputBuffers, ref newContext, array, ref attributes, ref timestamp);

            outputBuffer.type  = array[0].type;
            outputBuffer.size  = array[0].size;
            outputBuffer.token = array[0].token;
            return(result);
        }
Example #3
0
 public                     SecurityBufferClass[] marshall()
 {
     SecurityBufferClass[] array = new SecurityBufferClass[this.count];
     for (int i = 0; i < this.count; i++)
     {
         array[i] = new SecurityBufferClass(IntPtrHelper.Add(this.securityBufferArray, SecurityBuffer.Size * i));
     }
     return(array);
 }
 private static void CleanSecurityBuffers(SecurityBufferClass[] securityBuffers)
 {
     for (int i = 0; i < securityBuffers.Length; i++)
     {
         SecurityBufferClass securityBufferClass = securityBuffers[i];
         securityBufferClass.type   = 0;
         securityBufferClass.offset = (securityBufferClass.size = 0);
         securityBufferClass.token  = null;
     }
 }
        private int ReadInBlockMode(byte[] buffer, int offset, int size)
        {
            while (this.dataSizeForRead <= 0)
            {
                if (!base.ReadHeader())
                {
                    return(0);
                }
                for (int i = 0; i < (int)this.dataSizeForRead; i += base.Read(this.dataBufferForRead, i, (int)this.dataSizeForRead - i))
                {
                }
                for (int j = 0; j < (int)this.tokenSizeForRead; j += base.Read(this.tokenBufferForRead, j, (int)this.tokenSizeForRead - j))
                {
                }
                this.securityBuffers[0].offset = 0;
                this.securityBuffers[0].size   = (int)this.dataSizeForRead;
                this.securityBuffers[0].token  = this.dataBufferForRead;
                this.securityBuffers[0].type   = 1;
                this.securityBuffers[1].offset = 0;
                this.securityBuffers[1].size   = (int)this.tokenSizeForRead;
                this.securityBuffers[1].token  = this.tokenBufferForRead;
                this.securityBuffers[1].type   = 2;
                int num = SSPIWrapper.DecryptMessage(ref this.securityContext.Handle, this.securityBuffers, ++this.sequenceNumberForRead);
                if (num != 0)
                {
                    throw new XmlaStreamException(new Win32Exception(num).Message);
                }
                SecurityBufferClass securityBufferClass = this.securityBuffers[0];
                if (securityBufferClass.offset + securityBufferClass.size > 65535)
                {
                    throw new XmlaStreamException(XmlaSR.InternalError);
                }
                this.dataOffsetForRead = (ushort)securityBufferClass.offset;
                this.dataSizeForRead   = (ushort)securityBufferClass.size;
            }
            ushort num2 = 0;
            int    num3 = (int)this.dataOffsetForRead;
            int    num4 = (int)(this.dataOffsetForRead + this.dataSizeForRead);
            int    num5 = offset;
            int    num6 = offset + size;

            while (num3 < num4 && num5 < num6)
            {
                buffer[num5] = this.dataBufferForRead[num3];
                num3++;
                num5++;
                num2 += 1;
            }
            this.dataSizeForRead   -= num2;
            this.dataOffsetForRead += num2;
            return((int)num2);
        }
        private int ExtractDecryptedAndExtraData(SecurityBufferClass[] securityBuffers, byte[] readBuffer, int readOffset, int readSize)
        {
            int result = 0;
            int num    = 0;
            int num2   = 0;

            for (int i = 0; i < securityBuffers.Length; i++)
            {
                SecurityBufferClass securityBufferClass = securityBuffers[i];
                if (securityBufferClass.type == 1)
                {
                    num++;
                    if (TcpStream.TRACESWITCH.TraceVerbose)
                    {
                        StackTrace stackTrace = new StackTrace();
                        stackTrace.GetFrame(1).GetMethod();
                    }
                    if (securityBufferClass.token != null)
                    {
                        if (securityBufferClass.size <= readSize)
                        {
                            Array.Copy(securityBufferClass.token, securityBufferClass.offset, readBuffer, readOffset, securityBufferClass.size);
                            result = securityBufferClass.size;
                        }
                        else
                        {
                            Array.Copy(securityBufferClass.token, securityBufferClass.offset, readBuffer, readOffset, readSize);
                            result = readSize;
                            this.streamDecryptedDataForRead.Add(new ArraySegment <byte>(securityBufferClass.token, securityBufferClass.offset + readSize, securityBufferClass.size - readSize));
                        }
                    }
                    else
                    {
                        result = 0;
                    }
                }
                else if (securityBufferClass.type == 5)
                {
                    num2++;
                    if (TcpStream.TRACESWITCH.TraceVerbose)
                    {
                        StackTrace stackTrace2 = new StackTrace();
                        stackTrace2.GetFrame(1).GetMethod();
                    }
                    if (securityBufferClass.token != null)
                    {
                        this.streamEncryptedDataAccumulatorForRead.Add(new ArraySegment <byte>(securityBufferClass.token, securityBufferClass.offset, securityBufferClass.size));
                    }
                }
            }
            return(result);
        }
Example #7
0
 public override void Write(byte[] buffer, int offset, int size)
 {
     if (this.disposed)
     {
         throw new ObjectDisposedException(null);
     }
     try
     {
         for (int i = Math.Min(65535, size); i > 0; i = Math.Min(65535, size))
         {
             SecurityBufferClass securityBufferClass = this.securityBuffers[0];
             securityBufferClass.offset = offset;
             securityBufferClass.size   = i;
             securityBufferClass.token  = buffer;
             securityBufferClass.type   = 1;
             securityBufferClass        = this.securityBuffers[1];
             securityBufferClass.offset = 0;
             securityBufferClass.size   = this.maxTokenSize;
             securityBufferClass.token  = this.tokenBufferForWrite;
             securityBufferClass.type   = 2;
             int num = SSPIWrapper.MakeSignature(ref this.securityContext.Handle, 0, this.securityBuffers, ++this.sequenceNumberForWrite);
             if (num != 0)
             {
                 throw new XmlaStreamException(new Win32Exception(num).Message);
             }
             base.Write();
             offset += i;
             size   -= i;
         }
     }
     catch (XmlaStreamException)
     {
         throw;
     }
     catch (IOException innerException)
     {
         throw new XmlaStreamException(innerException);
     }
     catch (SocketException innerException2)
     {
         throw new XmlaStreamException(innerException2);
     }
     catch (Win32Exception innerException3)
     {
         throw new XmlaStreamException(innerException3);
     }
 }
        public byte[] GetOutgoingBlob(byte[] incomingBlob, out bool handshakeComplete)
        {
            handshakeComplete = true;
            if (this.m_SecurityContext.Handle.Initialized && incomingBlob == null)
            {
                return(null);
            }
            SecurityBufferClass inputBuffer = null;

            if (incomingBlob != null)
            {
                inputBuffer = new SecurityBufferClass(incomingBlob, BufferType.Token);
            }
            SecurityBufferClass securityBufferClass = new SecurityBufferClass(this.m_TokenSize, BufferType.Token);
            int num  = SSPIWrapper.InitializeSecurityContext(ref this.m_CredentialsHandle.Handle, ref this.m_SecurityContext.Handle, this.m_RemotePeerId, (int)this.m_RequestedFlags, this.m_Endianness, inputBuffer, ref this.m_SecurityContext.Handle, securityBufferClass, ref this.m_ContextFlags, ref this.m_SecurityContext.TimeStamp);
            int num2 = num & -2147483648;

            if (num2 != 0)
            {
                throw new Win32Exception(num);
            }
            if (num != 0 && this.m_SecurityContext.Handle.Initialized)
            {
                handshakeComplete = false;
            }
            else
            {
                switch (this.m_SecurityContext.SecurityContextMode)
                {
                case SecurityContextMode.stream:
                    this.CheckIfFlagWasObtainedIfRequested(ContextFlags.Stream);
                    break;
                }
                this.CheckIfFlagWasObtainedIfRequested(ContextFlags.MutualAuth);
                this.CheckIfFlagWasObtainedIfRequested(ContextFlags.ReplayDetect);
                this.CheckIfFlagWasObtainedIfRequested(ContextFlags.SequenceDetect);
                this.CheckIfFlagWasObtainedIfRequested(ContextFlags.Confidentiality);
            }
            return(securityBufferClass.token);
        }
 private void WriteInBlockMode(byte[] buffer, int offset, int size)
 {
     for (int i = Math.Min(this.maxEncryptionBufferSize, size); i > 0; i = Math.Min(this.maxEncryptionBufferSize, size))
     {
         SecurityBufferClass securityBufferClass = this.securityBuffers[0];
         securityBufferClass.offset = offset;
         securityBufferClass.size   = i;
         securityBufferClass.token  = buffer;
         securityBufferClass.type   = 1;
         securityBufferClass        = this.securityBuffers[1];
         securityBufferClass.offset = 0;
         securityBufferClass.size   = this.maxTokenSize;
         securityBufferClass.token  = this.tokenBufferForWrite;
         securityBufferClass.type   = 2;
         int num = SSPIWrapper.EncryptMessage(ref this.securityContext.Handle, this.securityBuffers, ++this.sequenceNumberForWrite);
         if (num != 0)
         {
             throw new XmlaStreamException(new Win32Exception(num).Message);
         }
         base.Write();
         offset += i;
         size   -= i;
     }
 }
Example #10
0
        public override int Read(byte[] buffer, int offset, int size)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(null);
            }
            int result;

            try
            {
                if (size == 0)
                {
                    result = 0;
                }
                else
                {
                    while (this.dataSizeForRead <= 0)
                    {
                        if (!this.Read())
                        {
                            result = 0;
                            return(result);
                        }
                        int num = SSPIWrapper.VerifySignature(ref this.securityContext.Handle, 0, this.securityBuffers, ++this.sequenceNumberForRead);
                        if (num != 0)
                        {
                            throw new XmlaStreamException(new Win32Exception(num).Message);
                        }
                        SecurityBufferClass securityBufferClass = this.securityBuffers[0];
                        if (securityBufferClass.offset + securityBufferClass.size > 65535)
                        {
                            throw new XmlaStreamException(XmlaSR.InternalError);
                        }
                        this.dataOffsetForRead = (ushort)securityBufferClass.offset;
                        this.dataSizeForRead   = (ushort)securityBufferClass.size;
                    }
                    ushort num2 = 0;
                    int    num3 = (int)this.dataOffsetForRead;
                    int    num4 = (int)(this.dataOffsetForRead + this.dataSizeForRead);
                    int    num5 = offset;
                    int    num6 = offset + size;
                    while (num3 < num4 && num5 < num6)
                    {
                        buffer[num5] = this.dataBufferForRead[num3];
                        num3++;
                        num5++;
                        num2 += 1;
                    }
                    this.dataSizeForRead   -= num2;
                    this.dataOffsetForRead += num2;
                    result = (int)num2;
                }
            }
            catch (XmlaStreamException)
            {
                throw;
            }
            catch (IOException innerException)
            {
                throw new XmlaStreamException(innerException);
            }
            catch (SocketException innerException2)
            {
                throw new XmlaStreamException(innerException2);
            }
            catch (Win32Exception innerException3)
            {
                throw new XmlaStreamException(innerException3);
            }
            return(result);
        }
        internal TcpSecureStream(TcpStream tcpStream, SecurityContext securityContext) : base(tcpStream)
        {
            if (securityContext == null)
            {
                throw new ArgumentNullException("securityContext");
            }
            this.securityContext = securityContext;
            try
            {
                switch (this.securityContext.SecurityContextMode)
                {
                case SecurityContextMode.block:
                {
                    Sizes sizes = (Sizes)SSPIWrapper.QueryContextAttributes(securityContext, ContextAttribute.Sizes);
                    this.maxTokenSize            = Math.Max(sizes.cbSecurityTrailer, sizes.cbMaxSignature);
                    this.maxEncryptionBufferSize = Math.Min(sizes.cbMaxToken, 65535);
                    if (this.maxTokenSize > 65535)
                    {
                        throw new XmlaStreamException(XmlaSR.TcpStream_MaxSignatureExceedsProtocolLimit);
                    }
                    this.tokenBufferForWrite = new byte[this.maxTokenSize];
                    this.tokenBufferForRead  = new byte[this.maxTokenSize];
                    this.securityBuffers     = new SecurityBufferClass[]
                    {
                        new SecurityBufferClass(null, BufferType.Data),
                        new SecurityBufferClass(this.tokenBufferForWrite, BufferType.Token)
                    };
                    break;
                }

                case SecurityContextMode.stream:
                {
                    this.streamSizes = (StreamSizes)SSPIWrapper.QueryContextAttributes(securityContext, ContextAttribute.StreamSizes);
                    if (this.streamSizes.cbMaxMessage > 65535)
                    {
                        throw new XmlaStreamException(XmlaSR.TcpStream_MaxSignatureExceedsProtocolLimit);
                    }
                    this.streamHeaderForWrite  = new ArraySegment <byte>(new byte[this.streamSizes.cbHeader]);
                    this.streamTrailerForWrite = new ArraySegment <byte>(new byte[this.streamSizes.cbTrailer]);
                    this.streamEncryptedDataAccumulatorForRead            = new List <ArraySegment <byte> >();
                    this.streamEncryptedDataAccumulatorForReadFreeBuffers = new List <ArraySegment <byte> >();
                    this.streamDecryptedDataForRead = new List <ArraySegment <byte> >();
                    SecurityBufferClass securityBufferClass  = new SecurityBufferClass(this.streamHeaderForWrite.Array, BufferType.Header);
                    SecurityBufferClass securityBufferClass2 = new SecurityBufferClass(null, BufferType.Data);
                    SecurityBufferClass securityBufferClass3 = new SecurityBufferClass(this.streamTrailerForWrite.Array, BufferType.Trailer);
                    SecurityBufferClass securityBufferClass4 = new SecurityBufferClass(null, BufferType.Empty);
                    this.securityBuffers = new SecurityBufferClass[]
                    {
                        securityBufferClass,
                        securityBufferClass2,
                        securityBufferClass3,
                        securityBufferClass4
                    };
                    break;
                }

                default:
                    throw new XmlaStreamException("SecurityContextMode " + this.securityContext.SecurityContextMode + " not configured!");
                }
            }
            catch (XmlaStreamException)
            {
                throw;
            }
            catch (IOException innerException)
            {
                throw new XmlaStreamException(innerException);
            }
            catch (SocketException innerException2)
            {
                throw new XmlaStreamException(innerException2);
            }
            catch (Win32Exception innerException3)
            {
                throw new XmlaStreamException(innerException3);
            }
        }