Ejemplo n.º 1
0
        public byte[] ReverseTransform(byte[] pkt, int offset, int length)
        {
            // Wrap data into the raw packet for readable format
            this.rawPacket.Wrap(pkt, offset, length);

            // Associate packet to a crypto context
            long ssrc = this.rawPacket.GetSSRC();
            SrtpCryptoContext context = null;

            this.contexts.TryGetValue(ssrc, out context);
            if (context == null)
            {
                context = this.reverseEngine.GetDefaultContext().deriveContext(ssrc, 0, 0);
                context.DeriveSrtpKeys(this.rawPacket.GetSequenceNumber());
                contexts[ssrc] = context;
            }

            bool reversed = context.ReverseTransformPacket(this.rawPacket);

            if (reversed)
            {
                return(this.rawPacket.GetData());
            }

            return(null);
        }
Ejemplo n.º 2
0
        /**
         * Close the transformer and underlying transform engine.
         *
         * The close functions closes all stored crypto contexts. This deletes key
         * data and forces a cleanup of the crypto contexts.
         */
        public void Close()
        {
            forwardEngine.Close();
            if (forwardEngine != reverseEngine)
            {
                reverseEngine.Close();
            }

            var keys = new List <long>(contexts.Keys);

            foreach (var ssrc in keys)
            {
                SrtpCryptoContext context = contexts[ssrc];

                contexts.Remove(ssrc);
                if (context != null)
                {
                    context.Close();
                }
            }
        }
Ejemplo n.º 3
0
        public byte[] Transform(byte[] pkt, int offset, int length)
        {
            // Updates the contents of raw packet with new incoming packet
            this.rawPacket.Wrap(pkt, offset, length);

            // Associate packet to a crypto context
            long ssrc = rawPacket.GetSSRC();
            SrtpCryptoContext context = null;

            contexts.TryGetValue(ssrc, out context);

            if (context == null)
            {
                context = forwardEngine.GetDefaultContext().deriveContext(ssrc, 0, 0);
                context.DeriveSrtpKeys(0);
                contexts[ssrc] = context;
            }

            // Transform RTP packet into SRTP
            context.TransformPacket(this.rawPacket);
            return(this.rawPacket.GetData());
        }
Ejemplo n.º 4
0
        public byte[] ReverseTransform(byte[] pkt, int offset, int length)
        {
            var isLocked = Interlocked.CompareExchange(ref _isLocked, 1, 0) != 0;

            try
            {
                // Wrap data into the raw packet for readable format
                var rawPacket = !isLocked ? this.rawPacket : new RawPacket();
                rawPacket.Wrap(pkt, offset, length);

                // Associate packet to a crypto context
                long ssrc = rawPacket.GetSSRC();
                SrtpCryptoContext context = null;
                this.contexts.TryGetValue(ssrc, out context);
                if (context == null)
                {
                    context = this.reverseEngine.GetDefaultContext().deriveContext(ssrc, 0, 0);
                    context.DeriveSrtpKeys(rawPacket.GetSequenceNumber());
                    contexts.AddOrUpdate(ssrc, context, (a, b) => context);
                }

                byte[] result   = null;
                bool   reversed = context.ReverseTransformPacket(rawPacket);
                if (reversed)
                {
                    result = rawPacket.GetData();
                }

                return(result);
            }
            finally
            {
                //Unlock
                if (!isLocked)
                {
                    Interlocked.CompareExchange(ref _isLocked, 0, 1);
                }
            }
        }
Ejemplo n.º 5
0
        public byte[] Transform(byte[] pkt, int offset, int length)
        {
            var isLocked = Interlocked.CompareExchange(ref _isLocked, 1, 0) != 0;

            try
            {
                // Updates the contents of raw packet with new incoming packet
                var rawPacket = !isLocked ? this.rawPacket : new RawPacket();
                rawPacket.Wrap(pkt, offset, length);

                // Associate packet to a crypto context
                long ssrc = rawPacket.GetSSRC();
                SrtpCryptoContext context = null;
                contexts.TryGetValue(ssrc, out context);

                if (context == null)
                {
                    context = forwardEngine.GetDefaultContext().deriveContext(ssrc, 0, 0);
                    context.DeriveSrtpKeys(0);
                    contexts.AddOrUpdate(ssrc, context, (a, b) => context);
                }

                // Transform RTP packet into SRTP
                context.TransformPacket(rawPacket);
                byte[] result = rawPacket.GetData();

                return(result);
            }
            finally
            {
                //Unlock
                if (!isLocked)
                {
                    Interlocked.CompareExchange(ref _isLocked, 0, 1);
                }
            }
        }