Ejemplo n.º 1
0
        public static ExtensionType Parse(MemoryCursor cursor)
        {
            var codeBytes = cursor.Move(2);
            var code      = (ushort)NetworkBitConverter.ParseUnaligned(codeBytes.Span);

            return(new ExtensionType(code));
        }
Ejemplo n.º 2
0
        public static SignatureScheme Parse(MemoryCursor cursor)
        {
            var bytes = cursor.Move(2);
            var code  = (ushort)NetworkBitConverter.ParseUnaligned(bytes.Span);

            return(new SignatureScheme(code));
        }
Ejemplo n.º 3
0
        public static NamedGroup Parse(MemoryCursor cursor)
        {
            var codeBytes = cursor.Move(2);
            var code      = (ushort)NetworkBitConverter.ParseUnaligned(codeBytes.Span);

            return(new NamedGroup(code));
        }
Ejemplo n.º 4
0
        public static MemoryBuffer DecryptBytes(MemoryCursor cursor, int startOffsetOfMessage, IAead aead, ulong sequenceNumber)
        {
            var headerLength = cursor - startOffsetOfMessage + 2;
            var lengthBytes  = cursor.Move(2);
            var headerBytes  = cursor.Peek(-headerLength);
            var length       = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);

            if (length > MaxLength)
            {
                throw new EncodingException();
            }

            if (length < aead.TagLength)
            {
                throw new EncodingException();
            }

            var startOffsetOfBody = cursor.AsOffset();
            var encryptedLength   = length - aead.TagLength;
            var encryptedBytes    = cursor.Move(encryptedLength);
            var tag = cursor.Move(aead.TagLength);

            aead.Decrypt(encryptedBytes.Span, tag.Span, sequenceNumber, headerBytes.Span);

            return(new MemoryBuffer(startOffsetOfBody, encryptedLength));
        }
Ejemplo n.º 5
0
        public static MemoryBuffer Slice(MemoryCursor cursor)
        {
            var payloadLengthBytes = cursor.Move(2);
            var payloadLength      = (int)NetworkBitConverter.ParseUnaligned(payloadLengthBytes.Span);
            var startOffsetOfBody  = cursor.AsOffset();

            cursor.Move(payloadLength);

            return(new MemoryBuffer(startOffsetOfBody, payloadLength));
        }
Ejemplo n.º 6
0
        public static SignatureScheme Parse(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> remainings)
        {
            if (bytes.Length < 2)
            {
                throw new EncodingException();
            }

            var codeBytes = bytes.Slice(0, 2);
            var code      = (ushort)NetworkBitConverter.ParseUnaligned(codeBytes.Span);

            remainings = bytes.Slice(2);

            return(new SignatureScheme(code));
        }
Ejemplo n.º 7
0
        public static bool TrySlice(MemoryCursor cursor, ExtensionType type)
        {
            var bytes = cursor.Peek(2);
            var code  = (ushort)NetworkBitConverter.ParseUnaligned(bytes.Span);

            if (type.code != code)
            {
                return(false);
            }

            cursor.Move(2);

            return(true);
        }
Ejemplo n.º 8
0
        public static Cipher Parse(ref ReadOnlyMemory <byte> bytes)
        {
            if (bytes.Length < 2)
            {
                throw new EncodingException();
            }

            var codeBytes = bytes.Slice(0, 2);
            var code      = (ushort)NetworkBitConverter.ParseUnaligned(codeBytes.Span);

            bytes = bytes.Slice(2);

            return(new Cipher(code));
        }
Ejemplo n.º 9
0
        public static PacketNumber Parse(ReadOnlySpan <byte> bytes, ValueBuffer?mask = null)
        {
            if (!mask.HasValue)
            {
                return(new PacketNumber(NetworkBitConverter.ParseUnaligned(bytes)));
            }

            Span <byte> buffer = stackalloc byte[bytes.Length];

            bytes.CopyTo(buffer);

            Mask(buffer, mask.Value);

            return(new PacketNumber(NetworkBitConverter.ParseUnaligned(buffer)));
        }
Ejemplo n.º 10
0
        public static MemoryBuffer SliceBytes(MemoryCursor cursor)
        {
            var lengthBytes = cursor.Move(2);
            var length      = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);

            if (length > MaxLength)
            {
                throw new EncodingException();
            }

            var startOffsetOfBody = cursor.AsOffset();

            cursor.Move(length);

            return(new MemoryBuffer(startOffsetOfBody, length));
        }
Ejemplo n.º 11
0
        public static ReadOnlyMemory <byte> Slice(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> afterPayloadBytes)
        {
            if (bytes.Length < 2)
            {
                throw new EncodingException();
            }

            var payloadLengthBytes = bytes.Slice(0, 2);
            var payloadLength      = (int)NetworkBitConverter.ParseUnaligned(payloadLengthBytes.Span);

            if (bytes.Length < payloadLength + 2)
            {
                throw new EncodingException();
            }

            afterPayloadBytes = bytes.Slice(payloadLength + 2);

            return(bytes.Slice(2, payloadLength));
        }
Ejemplo n.º 12
0
        public static ReadOnlyMemory <byte> SliceHandshakeBytes(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> afterHandshakeBytes)
        {
            if (bytes.Length < 3)
            {
                throw new EncodingException();
            }

            var lengthBytes      = bytes.Slice(0, 3);
            var length           = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);
            var afterLengthBytes = bytes.Slice(3);

            if (afterLengthBytes.Length < length)
            {
                throw new EncodingException();
            }

            afterHandshakeBytes = afterLengthBytes.Slice(length);

            return(afterLengthBytes.Slice(0, length));
        }
Ejemplo n.º 13
0
        public static ReadOnlyMemory <byte> SliceVectorBytes(ReadOnlyMemory <byte> data, Range range, out ReadOnlyMemory <byte> remainings)
        {
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

            if (data.Length < lengthSizeInBytes || lengthSizeInBytes > 4)
            {
                throw new EncodingException();
            }

            var lengthBytes      = data.Slice(0, lengthSizeInBytes);
            var length           = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);
            var afterLengthBytes = data.Slice(lengthSizeInBytes);

            if (afterLengthBytes.Length < length || length < range.Start.Value || length > range.End.Value)
            {
                throw new EncodingException();
            }

            remainings = afterLengthBytes.Slice(length);

            return(afterLengthBytes.Slice(0, length));
        }
Ejemplo n.º 14
0
        public static MemoryBuffer SliceVectorBytes(MemoryCursor cursor, Range range)
        {
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

            if (lengthSizeInBytes > 4)
            {
                throw new EncodingException();
            }

            var lengthBytes = cursor.Move(lengthSizeInBytes);
            var length      = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);

            if (length < range.Start.Value || length > range.End.Value)
            {
                throw new EncodingException();
            }

            var startOffset = cursor.AsOffset();

            cursor.Move(length);

            return(new MemoryBuffer(startOffset, length));
        }