Giter Site home page Giter Site logo

pylibsrtp's Introduction

pylibsrtp

License Version Python versions Tests Coverage Documentation

What is pylibsrtp?

pylibsrtp is a Python wrapper around libsrtp, making it possible to encrypt and decrypt Secure Real-time Transport Protocol (SRTP) packets from Python code.

SRTP is a profile of the Real-time Transport Protocol (RTP) which provides confidentiality, message authentication, and replay protection. It is defined by RFC 3711.

You can install pylibsrtp with pip:

$ pip install pylibsrtp

To learn more about pylibsrtp please read the documentation.

Example

#!/usr/bin/env python

from pylibsrtp import Policy, Session

key = (b'\x00' * 30)
rtp = b'\x80\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + (b'\xd4' * 160)

# protect RTP
tx_policy = Policy(key=key, ssrc_type=Policy.SSRC_ANY_OUTBOUND)
tx_session = Session(policy=tx_policy)
srtp = tx_session.protect(rtp)

# unprotect RTP
rx_policy = Policy(key=key, ssrc_type=Policy.SSRC_ANY_INBOUND)
rx_session = Session(policy=rx_policy)
rtp2 = rx_session.unprotect(srtp)

# check roundtrip worked!
assert rtp2 == rtp

Building pylibsrtp

If you wish to build pylibsrtp yourself, you will need libsrtp version 2.0 or better.

On Debian/Ubuntu run:

$ apt install libsrtp2-dev

On Fedora/CentOS run:

$ dnf install libsrtp-devel

On OS X run:

$ brew install srtp

License

pylibsrtp is released under the BSD license.

pylibsrtp's People

Contributors

jlaine avatar leodog896 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pylibsrtp's Issues

thread-local storage is not supported for the current target

Running pylibsrtp-0.4.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-sf6_i953/pylibsrtp-0.4.0/egg-dist-tmp-14018osw _configtest.c:1:1: error: thread-local storage is not supported for the current target __thread int some_threadlocal_variable_42; ^ 1 error generated.

mac os 10.13.3, python 3.5

AES-GCM mode

Hello,

Does this lib support AES-GCM mode? I do not see how to enable it. Could you please help?
Failed to decrypt AEAD_AES_128_GCM stream.

pylibsrtp.Error: couldn't initialize

I am trying to run a video transmission from a Raspberry Pi using aiortc, but recently I started to receive this weird error:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-17' coro=<RTCPeerConnection.__connect() done, defined at /home/proto003/.pyenv/versions/3.9.16/lib/python3.9/site-packages/aiortc/rtcpeerconnection.py:1000> exception=Error("couldn't initialize")>
Traceback (most recent call last):
  File "/home/proto003/.pyenv/versions/3.9.16/lib/python3.9/site-packages/aiortc/rtcpeerconnection.py", line 1010, in __connect
    await dtlsTransport.start(self.__remoteDtls[transceiver])
  File "/home/proto003/.pyenv/versions/3.9.16/lib/python3.9/site-packages/aiortc/rtcdtlstransport.py", line 413, in start
    self._rx_srtp = Session(rx_policy)
  File "/home/proto003/.pyenv/versions/3.9.16/lib/python3.9/site-packages/pylibsrtp/__init__.py", line 202, in __init__
    _srtp_assert(lib.srtp_create(srtp, _policy))
  File "/home/proto003/.pyenv/versions/3.9.16/lib/python3.9/site-packages/pylibsrtp/__init__.py", line 55, in _srtp_assert
    raise Error(ERRORS[rc])
pylibsrtp.Error: couldn't initialize

As i discussed here, this couldn't come from a code error, it must be something in the system that broke in the last few months. I believe the easiest way to reproduce this bug is with the aiortc webcam example.

A note that I forgot to mention in the other issue is that the ice gathering state checks as complete, the connection state stays stuck in "connecting" and the other end of the connection displays a dark screen. I tested with pylibsrtp built from source and pre-built from the official RPi wheels repository, with the same result.

This is the function that fails:

def _srtp_assert(rc):
    if rc != lib.srtp_err_status_ok:
        raise Error(ERRORS[rc])

I've added a print('rc:', rc) and this was the outcome:

rc: 0
rc: 0
rc: 5

I hope this information is enough to track whatever is going wrong.

Errors

Hello,

Could you explain more what does this error means?
raise Error(ERRORS[rc])
pylibsrtp.Error: authentication failure

Requires libsrtp2-dev when not building

Thanks for fixing the piwheels issue - working like a charm!

When importing the following RTCPeerConnection and RTCSessionDescription from aiortc I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/env/lib/python3.7/site-packages/aiortc/__init__.py", line 8, in <module>
    from .rtcdtlstransport import (
  File "/home/pi/env/lib/python3.7/site-packages/aiortc/rtcdtlstransport.py", line 12, in <module>
    import pylibsrtp
  File "/home/pi/env/lib/python3.7/site-packages/pylibsrtp/__init__.py", line 4, in <module>
    from ._binding import ffi, lib
ImportError: libsrtp2.so.1: cannot open shared object file: No such file or directory

This can be easily fixed by either installing libsrtp2-dev with apt or by just copying the libsrtp2.so.1 into /usr/lib/. However due to not having sudo permissions in my project during install, is it possible to use a local libsrtp2.so.1 file shipped with the python project or I wondered if there was a solution to get around installing the library with root. Cheers!

Support for changing the master key

Hi,

I was developing a tool to record opus audio from srtp. https://github.com/kamanashisroy/opus_stream_tool

I have cases where the srtp-key changes but the stream ssrc stays the same.

To allow decrypt the audio, I need support for adding/changing new srtp-key in the same session.

Another way to decrypt the stream is to stop when it fails to decrypt with old srtp-key and get the old ROC value. Now we can create a new session with old ROC value and new srtp-key. For this we need the support to specify or get the current ROC value.

Support for non-default ciphers

Thanks for making this glue. It has been great to be able to use libsrtp in Python. Do you ever plan to support the other available ciphers? The default AES_CM_128_HMAC_SHA1_80 is working perfectly, but it would be helpful to be able to switch to AES_CM_128_HMAC_SHA1_32 for testing purposes. I tried changing my installation of pylibsrtp so that init.py calls"
lib.srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(ffi.addressof(self._policy.rtp))
instead of:
lib.srtp_crypto_policy_set_rtp_default(ffi.addressof(self._policy.rtp))

but I end up with error:
AttributeError: cffi library 'pylibsrtp._binding' has no function, constant or global variable named 'srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32'

I am not very versed in calling C libraries from Python. When looking at the strings in _binding.abi3.so included with pylibsrtp from a pip install I see the string srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32 so I don't know where the disconnect is. Could you point me in the right direction to be able to use a different cipher?

Environment:
Debian 10
libsrtp2 v2.2.0 from Debian repo
pylibsrtp v0.6.8 pip installed

huge thanks,
Dennis

Error on M1 Mac: `symbol not found in flat namespace '_srtp_add_stream'`

I'm unable to use aiortc after updating to a new M1 Mac (and Python 3.10) because of an error in pylibsrtp:

Traceback (most recent call last):
  File "/Users/khiner/Development/jaxdsp/server/server.py", line 13, in <module>
    from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription
  File "/Users/khiner/.pyenv/versions/3.10.1/lib/python3.10/site-packages/aiortc/__init__.py", line 11, in <module>
    from .rtcdtlstransport import (
  File "/Users/khiner/.pyenv/versions/3.10.1/lib/python3.10/site-packages/aiortc/rtcdtlstransport.py", line 12, in <module>
    import pylibsrtp
  File "/Users/khiner/.pyenv/versions/3.10.1/lib/python3.10/site-packages/pylibsrtp/__init__.py", line 4, in <module>
    from ._binding import ffi, lib
ImportError: dlopen(/Users/khiner/.pyenv/versions/3.10.1/lib/python3.10/site-packages/pylibsrtp/_binding.abi3.so, 0x0002): symbol not found in flat namespace '_srtp_add_stream'

Any ideas here? Please let me know if any other details would be helpful.

Thanks!

Later version on piwheels for Stretch and Buster

Had some issues installing on various Raspberry Pi Models.

Can successfully build wheels locally on the Pi, but there are various apt packages which need to be installed which takes time and requires a user with root access to install them.

Wondered if it was possible to get an updated version of pylibsrtp uploaded to piwheels for Buster and Stretch?

_binding.abi3.so: undefined symbol: EVP_aes_128_gcm

Hi, @jlaine
after install pylibsrtp , i get a error about _binding.abi3.so: undefined symbol: EVP_aes_128_gcm

$ python 
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylibsrtp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nvidia/py_env/kr214_tf1110/lib/python3.5/site-packages/pylibsrtp-0.6.0-py3.5-linux-aarch64.egg/pylibsrtp/__init__.py", line 3, in <module>
    from ._binding import ffi, lib
ImportError: /home/nvidia/py_env/kr214_tf1110/lib/python3.5/site-packages/pylibsrtp-0.6.0-py3.5-linux-aarch64.egg/pylibsrtp/_binding.abi3.so: undefined symbol: EVP_aes_128_gcm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.