示例#1
0
        public static RecordType ReadRecord(ref ReadableBuffer messageBuffer, State.IConnectionState state)
        {
            if (messageBuffer.Length < RecordHeaderLength)
            {
                Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.decode_error, "The message buffer length is smaller than the record header length");
            }
            var recordType = messageBuffer.ReadBigEndian <RecordType>();
            var version    = messageBuffer.Slice(sizeof(RecordType)).ReadBigEndian <ushort>();
            var size       = messageBuffer.Slice(sizeof(RecordType) + sizeof(ushort)).ReadBigEndian <ushort>();

            if (state?.ReadKey == null)
            {
                messageBuffer = messageBuffer.Slice(RecordHeaderLength);
                return(recordType);
            }
            if ((TlsVersion)state.TlsRecordVersion == TlsVersion.Tls12)
            {
                state.ReadKey.DecryptWithAuthData(ref messageBuffer);
                return(recordType);
            }
            else
            {
                messageBuffer = messageBuffer.Slice(RecordHeaderLength);
                state.ReadKey.Decrypt(ref messageBuffer);
                RemovePadding(ref messageBuffer);
                recordType    = messageBuffer.Slice(messageBuffer.Length - sizeof(RecordType)).ReadBigEndian <RecordType>();
                messageBuffer = messageBuffer.Slice(0, messageBuffer.Length - sizeof(RecordType));
                return(recordType);
            }
        }
示例#2
0
        public static void WriteRecord(ref WritableBuffer buffer, RecordType recordType, ReadableBuffer plainText, State.IConnectionState state)
        {
            buffer.Ensure(RecordHeaderLength);
            if (state.WriteKey == null)
            {
                buffer.WriteBigEndian(recordType);
                buffer.WriteBigEndian(TlsRecordVersion);
                buffer.WriteBigEndian((ushort)plainText.Length);
                buffer.Append(plainText);
                return;
            }
            buffer.WriteBigEndian(RecordType.Application);
            buffer.WriteBigEndian(TlsRecordVersion);
            var totalSize = plainText.Length + state.WriteKey.Overhead + sizeof(RecordType);

            buffer.WriteBigEndian((ushort)totalSize);
            state.WriteKey.Encrypt(ref buffer, plainText, recordType);
        }
示例#3
0
 public static void WriteAlert(ref WritableBuffer output, AlertLevel level, AlertDescription description, State.IConnectionState connectionState)
 {
     connectionState.FrameWriter.StartFrame(RecordType.Alert, ref output);
     output.WriteBigEndian(level);
     output.WriteBigEndian(description);
     connectionState.FrameWriter.FinishFrame(ref output);
 }
示例#4
0
        public static void WriteAlert(ref WritableBuffer output, AlertLevel level, AlertDescription description, State.IConnectionState connectionState)
        {
            var buffer = new byte[sizeof(AlertLevel) + sizeof(AlertDescription)];
            var span   = new Span <byte>(buffer);

            span.Write(level);
            span = span.Slice(sizeof(AlertLevel));
            span.Write(description);
            RecordProcessor.WriteRecord(ref output, RecordType.Alert, buffer, connectionState);
        }