An implementation of the TLS 1.0 record layer.
Ejemplo n.º 1
0
        internal virtual byte[] DecodeAndVerify(byte type, Stream input, int len)
        {
            RecordStream.CheckLength(len, this.mCiphertextLimit, 22);
            byte[]    array    = TlsUtilities.ReadFully(len, input);
            TlsCipher arg_34_0 = this.mReadCipher;
            long      seqNo;

            this.mReadSeqNo = (seqNo = this.mReadSeqNo) + 1L;
            byte[] array2 = arg_34_0.DecodeCiphertext(seqNo, type, array, 0, array.Length);
            RecordStream.CheckLength(array2.Length, this.mCompressedLimit, 22);
            Stream stream = this.mReadCompression.Decompress(this.mBuffer);

            if (stream != this.mBuffer)
            {
                stream.Write(array2, 0, array2.Length);
                stream.Flush();
                array2 = this.GetBufferContents();
            }
            RecordStream.CheckLength(array2.Length, this.mPlaintextLimit, 30);
            if (array2.Length < 1 && type != 23)
            {
                throw new TlsFatalAlert(47);
            }
            return(array2);
        }
Ejemplo n.º 2
0
 public TlsProtocolHandler(
     Stream inStr,
     Stream outStr,
     SecureRandom sr)
 {
     this.random = sr;
     this.rs     = new RecordStream(this, inStr, outStr);
 }
Ejemplo n.º 3
0
 public TlsProtocol(SecureRandom secureRandom)
 {
     mBlocking     = false;
     mInputBuffers = new ByteQueueStream();
     mOutputBuffer = new ByteQueueStream();
     mRecordStream = new RecordStream(this, (Stream)(object)mInputBuffers, (Stream)(object)mOutputBuffer);
     mSecureRandom = secureRandom;
 }
Ejemplo n.º 4
0
 public TlsProtocol(System.IO.Stream input, System.IO.Stream output, SecureRandom secureRandom)
 {
     this.mApplicationDataQueue = new ByteQueue();
     this.mAlertQueue           = new ByteQueue(2);
     this.mHandshakeQueue       = new ByteQueue();
     this.mAppDataSplitEnabled  = true;
     this.mBlocking             = true;
     this.mRecordStream         = new RecordStream(this, input, output);
     this.mSecureRandom         = secureRandom;
 }
Ejemplo n.º 5
0
 public TlsProtocol(SecureRandom secureRandom)
 {
     this.mApplicationDataQueue = new ByteQueue();
     this.mAlertQueue           = new ByteQueue(2);
     this.mHandshakeQueue       = new ByteQueue();
     this.mAppDataSplitEnabled  = true;
     this.mBlocking             = true;
     this.mBlocking             = false;
     this.mInputBuffers         = new ByteQueueStream();
     this.mOutputBuffer         = new ByteQueueStream();
     this.mRecordStream         = new RecordStream(this, this.mInputBuffers, this.mOutputBuffer);
     this.mSecureRandom         = secureRandom;
 }
Ejemplo n.º 6
0
        /*
         * Both streams can be the same object
         */
        public TlsProtocolHandler(
            Stream inStr,
            Stream outStr)
        {
            /*
             * We use a threaded seed generator to generate a good random
             * seed. If the user has a better random seed, he should use
             * the constructor with a SecureRandom.
             *
             * Hopefully, 20 bytes in fast mode are good enough.
             */
            byte[] seed = new ThreadedSeedGenerator().GenerateSeed(20, true);

            this.random = new SecureRandom(seed);
            this.rs     = new RecordStream(this, inStr, outStr);
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
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);
        }
 public TlsProtocol(Stream input, Stream output, SecureRandom secureRandom)
 {
     this.mRecordStream = new RecordStream(this, input, output);
     this.mSecureRandom = secureRandom;
 }
Ejemplo n.º 10
0
 /// <remarks>Both streams can be the same object</remarks>
 public TlsProtocolHandler(
     Stream			inStr,
     Stream			outStr,
     SecureRandom	sr)
 {
     this.rs = new RecordStream(this, inStr, outStr);
     this.random = sr;
 }
Ejemplo n.º 11
0
 public TlsProtocol(SecureRandom secureRandom)
 {
     this.mBlocking = false;
     this.mInputBuffers = new ByteQueueStream();
     this.mOutputBuffer = new ByteQueueStream();
     this.mRecordStream = new RecordStream(this, mInputBuffers, mOutputBuffer);
     this.mSecureRandom = secureRandom;
 }
Ejemplo n.º 12
0
 public TlsProtocol(Stream input, Stream output, SecureRandom secureRandom)
 {
     this.mRecordStream = new RecordStream(this, input, output);
     this.mSecureRandom = secureRandom;
 }
Ejemplo n.º 13
0
        /*
        * Both streams can be the same object
        */
        public TlsProtocolHandler(
			Stream	inStr,
			Stream	outStr)
        {
            /*
             * We use a threaded seed generator to generate a good random
             * seed. If the user has a better random seed, he should use
             * the constructor with a SecureRandom.
             *
             * Hopefully, 20 bytes in fast mode are good enough.
             */
            byte[] seed = new ThreadedSeedGenerator().GenerateSeed(20, true);

            this.random = new SecureRandom(seed);
            this.rs = new RecordStream(this, inStr, outStr);
        }
Ejemplo n.º 14
0
 public HandshakeHashUpdateStream(RecordStream mOuter)
 {
     this.mOuter = mOuter;
 }