Пример #1
0
        public static bool TryParseProtectedByConnectionId(IAead aead,
                                                           ICipher cipher,
                                                           MemoryCursor cursor,
                                                           PacketConnectionId connectionId,
                                                           PacketNumber largestAcknowledgedPacketNumber,
                                                           out ShortPacket result)
        {
            result = new ShortPacket();

            var startOffset = cursor.AsOffset();
            var firstByte   = PacketFirstByte.Parse(cursor.Peek(1).Span[0]);

            if (!firstByte.IsShortHeader())
            {
                return(false);
            }

            cursor.Move(1);

            if (!connectionId.TrySliceValue(cursor))
            {
                return(false);
            }

            var payload = PacketPayload.SliceShortProtectedPacketBytes(cursor, aead, cipher, startOffset, firstByte, largestAcknowledgedPacketNumber, out var packetNumber);

            result = new ShortPacket(connectionId, packetNumber, payload);

            return(true);
        }
Пример #2
0
        public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor,
                                                                      PacketVersion version,
                                                                      PacketConnectionId destinationConnectionId,
                                                                      PacketConnectionId sourceConnectionId,
                                                                      PacketNumber packetNumber,
                                                                      PacketToken token)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte()
                              .SetInitial()
                              .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);
            token.WriteBytes(cursor);

            var context           = PacketPayload.StartPacketWriting(cursor, startOffset);
            var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor);

            packetNumber.Fill(packetNumberBytes.Span);

            return(context);
        }
Пример #3
0
        public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor,
                                                                      PacketVersion version,
                                                                      PacketConnectionId destinationConnectionId,
                                                                      PacketConnectionId sourceConnectionId,
                                                                      PacketNumber largestAcknowledgedPacketNumber,
                                                                      PacketNumber packetNumber)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte()
                              .SetHandshake()
                              .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);

            var context           = PacketPayload.StartPacketWriting(cursor, startOffset);
            var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor);

            packetNumber
            .EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber)
            .Fill(packetNumberBytes.Span);

            return(context);
        }
Пример #4
0
        public static bool TryParse(MemoryCursor cursor, PacketNumber largestAcknowledgedPacketNumber, out HandshakePacket result)
        {
            result = new HandshakePacket();

            var startOffset = cursor.AsOffset();
            var firstByte   = PacketFirstByte.Parse(cursor.Peek(1).Span[0]);

            if (!firstByte.IsHandshakeType())
            {
                return(false);
            }

            cursor.Move(1);

            var version = PacketVersion.Parse(cursor);
            var destinationConnectionId = PacketConnectionId.Parse(cursor);
            var sourceConnectionId      = PacketConnectionId.Parse(cursor);
            var payload = PacketPayload.SlicePacketBytes(cursor, firstByte, startOffset, largestAcknowledgedPacketNumber, out var packetNumber);

            result = new HandshakePacket(version,
                                         destinationConnectionId,
                                         sourceConnectionId,
                                         packetNumber,
                                         payload);

            return(true);
        }
Пример #5
0
 private ShortPacket(PacketConnectionId destinationConnectionId,
                     PacketNumber number,
                     MemoryBuffer payload)
 {
     DestinationConnectionId = destinationConnectionId;
     Number  = number;
     Payload = payload;
 }
Пример #6
0
 public static LongProtectedWritingContext StartLongProtectedPacketWriting(
     MemoryCursor cursor,
     IAead aead,
     ICipher cipher,
     int startPacketOffset,
     PacketFirstByte firstByte,
     PacketNumber packetNumber,
     PacketNumber?largestAcknowledgedPacketNumber)
 {
     return(new LongProtectedWritingContext(aead, cipher, cursor, startPacketOffset, cursor.AsOffset(), packetNumber, largestAcknowledgedPacketNumber, firstByte));
 }
Пример #7
0
 private HandshakePacket(PacketVersion version,
                         PacketConnectionId destinationConnectionId,
                         PacketConnectionId sourceConnectionId,
                         PacketNumber number,
                         MemoryBuffer payload)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     Number  = number;
     Payload = payload;
 }
Пример #8
0
 private InitialPacket(PacketVersion version,
                       PacketConnectionId destinationConnectionId,
                       PacketConnectionId sourceConnectionId,
                       PacketToken token,
                       PacketNumber number,
                       MemoryBuffer payload)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     Token   = token;
     Number  = number;
     Payload = payload;
 }
Пример #9
0
 public LongProtectedWritingContext(
     IAead aead,
     ICipher cipher,
     MemoryCursor cursor,
     int startPacketOffset,
     int startPayloadOffset,
     PacketNumber packetNumber,
     PacketNumber?largestAcknowledgedPacketNumber,
     PacketFirstByte packetFirstByte)
 {
     this.aead               = aead;
     this.cipher             = cipher;
     this.cursor             = cursor;
     this.startPacketOffset  = startPacketOffset;
     this.startPayloadOffset = startPayloadOffset;
     this.packetNumber       = packetNumber;
     this.largestAcknowledgedPacketNumber = largestAcknowledgedPacketNumber;
     this.packetFirstByte = packetFirstByte;
 }
Пример #10
0
        public static MemoryBuffer SliceShortProtectedPacketBytes(
            MemoryCursor cursor,
            IAead aead,
            ICipher cipher,
            int startPacketOffset,
            PacketFirstByte firstByte,
            PacketNumber largestAcknowledgedPacketNumber,
            out PacketNumber packetNumber)
        {
            var startPacketNumberOffset = cursor.AsOffset();

            cursor.Move(SkipToSampleLength);

            var sample = cursor.Move(SampleLength);
            var mask   = cipher.CreateMask(sample.Span);

            cursor.Set(startPacketOffset);
            firstByte.Mask(mask).Write(cursor);
            cursor.Set(startPacketNumberOffset);

            var packetNumberBytes  = firstByte.Mask(mask).SlicePacketNumberBytes(cursor);
            var headerLength       = cursor - startPacketOffset;
            var headerBytes        = cursor.Peek(-headerLength);
            var startPayloadOffset = cursor.AsOffset();

            cursor.MoveEnd();

            var tagBytes            = cursor.Move(-aead.TagLength);
            var payloadLength       = cursor - startPayloadOffset;
            var payloadBytes        = cursor.Peek(-payloadLength);
            var payload             = cursor.Slice(-payloadLength);
            var encodedPacketNumber = PacketNumber.Parse(packetNumberBytes.Span, mask);

            encodedPacketNumber.Fill(packetNumberBytes.Span);

            packetNumber = encodedPacketNumber.DecodeByLargestAcknowledged(largestAcknowledgedPacketNumber);

            packetNumber.Decrypt(aead, payloadBytes.Span, tagBytes.Span, headerBytes.Span);
            cursor.MoveEnd();

            return(payload);
        }
Пример #11
0
        public static MemoryBuffer SlicePacketBytes(
            MemoryCursor cursor,
            PacketFirstByte firstByte,
            int startPacketOffset,
            PacketNumber?largestAcknowledgedPacketNumber,
            out PacketNumber packetNumber)
        {
            var readLength        = cursor - startPacketOffset;
            var packetLength      = cursor.DecodeVariable32();
            var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor);
            var payloadLength     = packetLength - readLength - packetNumberBytes.Length;

            packetNumber = PacketNumber.Parse(packetNumberBytes.Span);

            if (largestAcknowledgedPacketNumber.HasValue)
            {
                packetNumber = packetNumber.DecodeByLargestAcknowledged(largestAcknowledgedPacketNumber.Value);
            }

            return(cursor.Slice(payloadLength));
        }
Пример #12
0
        public static PacketPayload.LongProtectedWritingContext StartProtectedWriting(IAead aead,
                                                                                      ICipher cipher,
                                                                                      MemoryCursor cursor,
                                                                                      PacketVersion version,
                                                                                      PacketConnectionId destinationConnectionId,
                                                                                      PacketConnectionId sourceConnectionId,
                                                                                      PacketNumber packetNumber,
                                                                                      PacketToken token)
        {
            var startPacketOffset = cursor.AsOffset();
            var firstByte         = new PacketFirstByte()
                                    .SetInitial()
                                    .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);
            token.WriteBytes(cursor);

            return(PacketPayload.StartLongProtectedPacketWriting(cursor, aead, cipher, startPacketOffset, firstByte, packetNumber, null));
        }
Пример #13
0
        public static PacketPayload.ShortProtectedWritingContext StartProtectedWriting(IAead aead,
                                                                                       ICipher cipher,
                                                                                       MemoryCursor cursor,
                                                                                       PacketConnectionId destinationConnectionId,
                                                                                       PacketNumber packetNumber,
                                                                                       PacketNumber largestAcknowledgedPacketNumber)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte().SetShort();

            firstByte.Write(cursor);
            destinationConnectionId.WriteValueBytes(cursor);

            return(PacketPayload.StartShortProtectedPacketWriting(cursor, aead, cipher, startOffset, firstByte, packetNumber, largestAcknowledgedPacketNumber));
        }