internal void WriteMessage(
            ContentType type,
            byte[] message,
            int offset,
            int len)
        {
            if (type == ContentType.handshake)
            {
                UpdateHandshakeData(message, offset, len);
            }

            Stream cOut = writeCompression.Compress(buffer);

            byte[] ciphertext;
            if (cOut == buffer)
            {
                ciphertext = writeCipher.EncodePlaintext(type, message, offset, len);
            }
            else
            {
                cOut.Write(message, offset, len);
                cOut.Flush();
                ciphertext = writeCipher.EncodePlaintext(type, buffer.ToArray(), 0, (int)buffer.Position);
                buffer.SetLength(0);
            }

            byte[] writeMessage = new byte[ciphertext.Length + 5];
            TlsUtilities.WriteUint8((byte)type, writeMessage, 0);
            TlsUtilities.WriteVersion(writeMessage, 1);
            TlsUtilities.WriteUint16(ciphertext.Length, writeMessage, 3);
            Array.Copy(ciphertext, 0, writeMessage, 5, ciphertext.Length);
            outStr.Write(writeMessage, 0, writeMessage.Length);
            outStr.Flush();
        }
示例#2
0
 internal virtual void WriteRecord(byte type, byte[] plaintext, int plaintextOffset, int plaintextLength)
 {
     if (mWriteVersion != null)
     {
         CheckType(type, 80);
         CheckLength(plaintextLength, mPlaintextLimit, 80);
         if (plaintextLength < 1 && type != 23)
         {
             throw new TlsFatalAlert(80);
         }
         if (type == 22)
         {
             UpdateHandshakeData(plaintext, plaintextOffset, plaintextLength);
         }
         Stream stream = mWriteCompression.Compress(mBuffer);
         byte[] array;
         if (stream == mBuffer)
         {
             array = mWriteCipher.EncodePlaintext(mWriteSeqNo++, type, plaintext, plaintextOffset, plaintextLength);
         }
         else
         {
             stream.Write(plaintext, plaintextOffset, plaintextLength);
             stream.Flush();
             byte[] bufferContents = GetBufferContents();
             CheckLength(bufferContents.Length, plaintextLength + 1024, 80);
             array = mWriteCipher.EncodePlaintext(mWriteSeqNo++, type, bufferContents, 0, bufferContents.Length);
         }
         CheckLength(array.Length, mCiphertextLimit, 80);
         byte[] array2 = new byte[array.Length + 5];
         TlsUtilities.WriteUint8(type, array2, 0);
         TlsUtilities.WriteVersion(mWriteVersion, array2, 1);
         TlsUtilities.WriteUint16(array.Length, array2, 3);
         Array.Copy(array, 0, array2, 5, array.Length);
         mOutput.Write(array2, 0, array2.Length);
         mOutput.Flush();
     }
 }