public async Task SendAsync(byte[] bytes)
        {
            logger.LogTrace($"Sending auxiliary stream buffer: {bytes.Length} bytes");

            var writer = new BEWriter();

            writer.Write(new byte[] { 0xde, 0xad });
            writer.Write((ushort)bytes.Length);
            writer.Write(_cryptoContext.Encrypt(bytes));
            writer.Write(_cryptoContext.CalculateMessageSignature(writer.ToArray()));

            var buffer = writer.ToArray();

            await _client.GetStream().WriteAsync(buffer, 0, buffer.Length);
        }
        public async Task SendAsync(IMessage message)
        {
            var cryptoMessage = message as ICryptoMessage;

            if (cryptoMessage != null)
            {
                cryptoMessage.Crypto = _crypto;
            }

            var writer = new BEWriter();

            message.Serialize(writer);
            var serialized = writer.ToArray();

            if (_addressOrHostname == null)
            {
                await _client.SendAsync(serialized, serialized.Length, new IPEndPoint(IPAddress.Broadcast, 5050));

                await _client.SendAsync(serialized, serialized.Length, new IPEndPoint(MULTICAST_ADDR, 5050));
            }
            else
            {
                await _client.SendAsync(serialized, serialized.Length);
            }
        }
Ejemplo n.º 3
0
        public override void Serialize(BEWriter writer)
        {
            var messageWriter = new BEWriter();

            base.Serialize(messageWriter);

            var message = messageWriter.ToArray();

            var initVectorSource = message.Take(16).ToArray();
            var initVector       = Crypto.CreateDerivedInitVector(initVectorSource);

            var fragmentWriter = new BEWriter();

            fragmentWriter.WriteWithPaddingAlignment(
                Fragment,
                payloadSizeAlignment);

            var encryptedFragment = Crypto.EncryptWithoutPadding(fragmentWriter.ToArray(), initVector);

            Header.Serialize(writer);
            writer.Write(encryptedFragment);

            var signature = Crypto.CalculateMessageSignature(writer.ToArray());

            writer.Write(signature);
        }
        public async Task SendAsyncStreaming(RtpPacket message)
        {
            var writer = new BEWriter();

            message.Serialize(writer);
            var serialized = writer.ToArray();

            await _streamingProtoClient.SendAsync(serialized, serialized.Length);
        }
Ejemplo n.º 5
0
        public virtual void Serialize(BEWriter writer)
        {
            var payloadWriter = new BEWriter();

            SerializePayload(payloadWriter);

            var payload = payloadWriter.ToArray();

            Header.PayloadLength = (ushort)payload.Length;

            Header.Serialize(writer);
            writer.Write(payload);
        }
        public async Task SendAsyncControl(RtpPacket message)
        {
            var writer = new BEWriter();

            message.Serialize(writer);
            byte[] serialized = writer.ToArray();

            var finalWriter = new LEWriter();

            finalWriter.Write((uint)serialized.Length);
            finalWriter.Write(serialized);

            byte[] prefixedSerialized = finalWriter.ToArray();
            await _controlProtoClient.GetStream().WriteAsync(prefixedSerialized, 0, prefixedSerialized.Length);
        }
Ejemplo n.º 7
0
        public byte[] Encrypt(byte[] data)
        {
            var writer = new BEWriter();

            writer.WriteWithPaddingAlignment(data, 16);

            var padded = writer.ToArray();

            var output = new byte[padded.Length];

            for (var i = 0; i < padded.Length; i += 16)
            {
                _clientCipher.ProcessBlock(padded, i, output, i);
            }

            return(output);
        }
        public override void Serialize(BEWriter writer)
        {
            var protectedPayloadWriter = new BEWriter();

            SerializeProtectedPayload(protectedPayloadWriter);

            var protectedPayload = protectedPayloadWriter.ToArray();

            Header.ProtectedPayloadLength = (ushort)protectedPayload.Length;

            var encryptedPayload = protectedPayload.Length > 0 ?
                                   Crypto.Encrypt(protectedPayload, InitVector) : new byte[] {};

            base.Serialize(writer);
            writer.Write(encryptedPayload);

            var signature = Crypto.CalculateMessageSignature(writer.ToArray());

            writer.Write(signature);
        }
        private Task SendFragmentAsync(SessionMessageBase message, uint sequenceNumber)
        {
            logger.LogTrace($"Sending {message.GetType().Name} message");

            var fragment = new SessionFragmentMessage();

            fragment.Header.ChannelId          = message.Header.ChannelId;
            fragment.Header.RequestAcknowledge = message.Header.RequestAcknowledge;
            fragment.Header.SessionMessageType = message.Header.SessionMessageType;
            fragment.Header.Version            = message.Header.Version;

            fragment.Header.SequenceNumber      = sequenceNumber;
            fragment.Header.SourceParticipantId = _participantId;

            var writer = new BEWriter();

            message.Serialize(writer);
            fragment.Fragment = writer.ToArray();

            return(_transport.SendAsync(fragment));
        }
        private byte[] ReadAndDecryptChunk(Stream stream)
        {
            var reader = new BEReader(stream);

            // 0xde, 0xad
            reader.ReadBytes(2);

            var length = reader.ReadUInt16();

            var encryptedPayloadLength = length + BinaryExtensions.CalculatePaddingSize(length, 16);

            var encryptedPayloadBytes    = new byte[encryptedPayloadLength];
            var encryptedPayloadPosition = 0;

            while (encryptedPayloadPosition < encryptedPayloadLength - 1)
            {
                var received = reader.ReadBytes(encryptedPayloadLength - encryptedPayloadPosition);
                received.CopyTo(encryptedPayloadBytes, encryptedPayloadPosition);
                encryptedPayloadPosition += received.Length;
            }

            var signature = reader.ReadBytes(32);

            var bodyWriter = new BEWriter();

            bodyWriter.Write(new byte[] { 0xde, 0xad });
            bodyWriter.Write(length);
            bodyWriter.Write(encryptedPayloadBytes);

            var messageSignature = _cryptoContext.CalculateMessageSignature(bodyWriter.ToArray());

            if (!signature.SequenceEqual(messageSignature))
            {
                throw new InvalidDataException("Invalid message signature.");
            }

            var decryptedPayload = _cryptoContext.Decrypt(encryptedPayloadBytes);

            return(decryptedPayload.Take(length).ToArray());
        }
Ejemplo n.º 11
0
        public SessionMessageBase AssembleFragment(SessionMessageBase message, uint sequenceNumber)
        {
            FragmentMessage fragment = message as FragmentMessage;

            SessionMessageType messageType = fragment.Header.SessionMessageType;
            int sequenceBegin = (int)fragment.SequenceBegin;
            int sequenceEnd   = (int)fragment.SequenceEnd;

            _fragmentQueue[(int)sequenceNumber] = fragment.Data;

            IEnumerable <int> neededSequences = Enumerable.Range(sequenceBegin, sequenceEnd - sequenceBegin);

            BEWriter writer = new BEWriter();

            foreach (int seq in neededSequences)
            {
                try
                {
                    byte[] data = _fragmentQueue[seq];
                    writer.Write(data);
                }
                catch (KeyNotFoundException)
                {
                    return(null);
                }
            }

            // Pop obsolete fragment data
            foreach (int seq in neededSequences)
            {
                _fragmentQueue.Remove(seq);
            }

            SessionMessageBase assembled = SessionMessageTransport.CreateFromMessageType(messageType);

            assembled.Deserialize(new BEReader(writer.ToArray()));
            return(assembled);
        }