Exemple #1
0
        internal virtual void WriteRecord(byte type, byte[] plaintext, int plaintextOffset, int plaintextLength)
        {
            if (this.mWriteVersion == null)
            {
                return;
            }
            RecordStream.CheckType(type, 80);
            RecordStream.CheckLength(plaintextLength, this.mPlaintextLimit, 80);
            if (plaintextLength < 1 && type != 23)
            {
                throw new TlsFatalAlert(80);
            }
            if (type == 22)
            {
                this.UpdateHandshakeData(plaintext, plaintextOffset, plaintextLength);
            }
            Stream stream = this.mWriteCompression.Compress(this.mBuffer);

            byte[] array;
            if (stream == this.mBuffer)
            {
                TlsCipher arg_7B_0 = this.mWriteCipher;
                long      seqNo;
                this.mWriteSeqNo = (seqNo = this.mWriteSeqNo) + 1L;
                array            = arg_7B_0.EncodePlaintext(seqNo, type, plaintext, plaintextOffset, plaintextLength);
            }
            else
            {
                stream.Write(plaintext, plaintextOffset, plaintextLength);
                stream.Flush();
                byte[] bufferContents = this.GetBufferContents();
                RecordStream.CheckLength(bufferContents.Length, plaintextLength + 1024, 80);
                TlsCipher arg_CC_0 = this.mWriteCipher;
                long      seqNo2;
                this.mWriteSeqNo = (seqNo2 = this.mWriteSeqNo) + 1L;
                array            = arg_CC_0.EncodePlaintext(seqNo2, type, bufferContents, 0, bufferContents.Length);
            }
            RecordStream.CheckLength(array.Length, this.mCiphertextLimit, 80);
            byte[] array2 = new byte[array.Length + 5];
            TlsUtilities.WriteUint8(type, array2, 0);
            TlsUtilities.WriteVersion(this.mWriteVersion, array2, 1);
            TlsUtilities.WriteUint16(array.Length, array2, 3);
            Array.Copy(array, 0, array2, 5, array.Length);
            this.mOutput.Write(array2, 0, array2.Length);
            this.mOutput.Flush();
        }
Exemple #2
0
        internal virtual bool ReadRecord()
        {
            byte[] array = TlsUtilities.ReadAllOrNothing(5, this.mInput);
            if (array == null)
            {
                return(false);
            }
            byte b = TlsUtilities.ReadUint8(array, 0);

            RecordStream.CheckType(b, 10);
            if (!this.mRestrictReadVersion)
            {
                int num = TlsUtilities.ReadVersionRaw(array, 1);
                if (((long)num & (long)((ulong)-256)) != 768L)
                {
                    throw new TlsFatalAlert(47);
                }
            }
            else
            {
                ProtocolVersion protocolVersion = TlsUtilities.ReadVersion(array, 1);
                if (this.mReadVersion == null)
                {
                    this.mReadVersion = protocolVersion;
                }
                else if (!protocolVersion.Equals(this.mReadVersion))
                {
                    throw new TlsFatalAlert(47);
                }
            }
            int len = TlsUtilities.ReadUint16(array, 3);

            byte[] array2 = this.DecodeAndVerify(b, this.mInput, len);
            this.mHandler.ProcessRecord(b, array2, 0, array2.Length);
            return(true);
        }