Group PJMEDIA_TRANSPORT_SRTP

group PJMEDIA_TRANSPORT_SRTP

Media transport adapter to add SRTP feature to existing transports.

This module implements SRTP as described by RFC 3711, using RFC 4568 as key exchange method. It implements Media Transport to integrate with the rest of PJMEDIA framework.

As we know, media transport is separated from the stream object (which does the encoding/decoding of PCM frames, (de)packetization of RTP/RTCP packets, and de-jitter buffering). The connection between stream and media transport is established when the stream is created (we need to specify media transport during stream creation), and the interconnection can be depicted from the diagram below:

https://raw.githubusercontent.com/pjsip/pjproject/master/pjmedia/docs/media-transport.PNG

I think the diagram above is self-explanatory.

SRTP functionality is implemented as some kind of “adapter”, which is plugged between the stream and the actual media transport that does sending/receiving RTP/RTCP packets. When SRTP is used, the interconnection between stream and transport is like the diagram below:

https://raw.githubusercontent.com/pjsip/pjproject/master/pjmedia/docs/media-srtp-transport.PNG

So to stream, the SRTP transport behaves as if it is a media transport (because it is a media transport), and to the media transport it behaves as if it is a stream. The SRTP object then forwards RTP packets back and forth between stream and the actual transport, encrypting/decrypting the RTP/RTCP packets as necessary.

The neat thing about this design is the SRTP “adapter” then can be used to encrypt any kind of media transports. We currently have UDP and ICE media transports that can benefit SRTP, and we could add SRTP to any media transports that will be added in the future.

Enums

enum pjmedia_srtp_crypto_option

Crypto option.

Values:

enumerator PJMEDIA_SRTP_NO_ENCRYPTION

When this flag is specified, encryption will be disabled.

enumerator PJMEDIA_SRTP_NO_AUTHENTICATION

When this flag is specified, authentication will be disabled.

enum pjmedia_srtp_use

This enumeration specifies the behavior of the SRTP transport regarding media security offer and answer.

Values:

enumerator PJMEDIA_SRTP_DISABLED

When this flag is specified, SRTP will be disabled, and the transport will reject RTP/SAVP offer.

enumerator PJMEDIA_SRTP_UNKNOWN

When this flag is specified, SRTP setting is unknown. This is to set the initial remote’s SRTP usage. It will be set later after remote’s policy in the SDP is received.

enumerator PJMEDIA_SRTP_OPTIONAL

When this flag is specified, SRTP will be advertised as optional and incoming SRTP offer will be accepted.

enumerator PJMEDIA_SRTP_MANDATORY

When this flag is specified, the transport will require that RTP/SAVP media shall be used.

enum pjmedia_srtp_keying_method

This enumeration specifies SRTP keying methods.

Values:

enumerator PJMEDIA_SRTP_KEYING_SDES

Session Description (SDES).

enumerator PJMEDIA_SRTP_KEYING_DTLS_SRTP

DTLS-SRTP.

enumerator PJMEDIA_SRTP_KEYINGS_COUNT

Number of keying method.

Functions

pj_status_t pjmedia_srtp_init_lib(pjmedia_endpt *endpt)

Initialize SRTP library. This function should be called before any SRTP functions, however calling pjmedia_transport_srtp_create() will also invoke this function. This function will also register SRTP library deinitialization to pj_atexit(), so the deinitialization of SRTP library will be performed automatically by PJLIB destructor.

Parameters:

endpt – The media endpoint instance.

Returns:

PJ_SUCCESS on success.

void pjmedia_srtp_setting_default(pjmedia_srtp_setting *opt)

Initialize SRTP setting with its default values.

Parameters:

opt – SRTP setting to be initialized.

pj_status_t pjmedia_srtp_enum_crypto(unsigned *count, pjmedia_srtp_crypto crypto[])

Enumerate available SRTP crypto name.

Parameters:
  • count – On input, specifies the maximum length of crypto array. On output, the number of available crypto initialized by this function.

  • crypto – The SRTP crypto array output.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_srtp_enum_keying(unsigned *count, pjmedia_srtp_keying_method keying[])

Enumerate available SRTP keying methods.

Parameters:
  • count – On input, specifies the maximum length of keying method array. On output, the number of available keying method initialized by this function.

  • keying – The SRTP keying method array output.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_create(pjmedia_endpt *endpt, pjmedia_transport *tp, const pjmedia_srtp_setting *opt, pjmedia_transport **p_tp)

Create an SRTP media transport.

Parameters:
  • endpt – The media endpoint instance.

  • tp – The actual media transport to send and receive RTP/RTCP packets. This media transport will be kept as member transport of this SRTP instance.

  • opt – Optional settings. If NULL is given, default settings will be used.

  • p_tp – Pointer to receive the transport SRTP instance.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_get_setting(pjmedia_transport *srtp, pjmedia_srtp_setting *opt)

Get current SRTP media transport setting.

Parameters:
  • srtp – The SRTP transport.

  • opt – Structure to receive the SRTP setting

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_modify_setting(pjmedia_transport *srtp, const pjmedia_srtp_setting *opt)

Modify SRTP media transport setting.

Parameters:
  • srtp – The SRTP transport.

  • opt – New setting

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_dtls_get_fingerprint(pjmedia_transport *srtp, const char *hash, char *buf, pj_size_t *len)

Get fingerprint of local DTLS-SRTP certificate.

Parameters:
  • srtp – The SRTP transport.

  • hash – Fingerprint hash algorithm, currently valid values are “SHA-256” and “SHA-1”.

  • buf – Buffer for fingerprint output. The output will be formatted as “SHA-256/1 XX:XX:XX…” and null terminated.

  • len – On input, the size of the buffer. On output, the length of the fingerprint.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_dtls_start_nego(pjmedia_transport *srtp, const pjmedia_srtp_dtls_nego_param *param)

Manually start DTLS-SRTP negotiation with the given parameters. Application only needs to call this function when the SRTP transport is used without SDP offer/answer. When SDP offer/answer framework is used, the DTLS-SRTP negotiation will be handled by pjmedia_transport_media_create(), pjmedia_transport_media_start(), pjmedia_transport_media_encode_sdp(), and pjmedia_transport_media_stop().

When the negotiation completes, application will be notified via SRTP callback on_srtp_nego_complete(), if set. If the negotiation is successful, SRTP will be automatically started.

Note that if the SRTP member transport is an ICE transport, application should only call this function after ICE negotiation is completed successfully.

Parameters:
  • srtp – The SRTP transport.

  • param – DTLS-SRTP nego parameter.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_start(pjmedia_transport *srtp, const pjmedia_srtp_crypto *tx, const pjmedia_srtp_crypto *rx)

Manually start SRTP session with the given parameters. Application only needs to call this function when the SRTP transport is used without SDP offer/answer. When SDP offer/answer framework is used, the SRTP transport will be started/stopped by pjmedia_transport_media_start() and pjmedia_transport_media_stop() respectively.

Please note that even if an RTP stream is only one direction, application will still need to provide both crypto suites, because it is needed by RTCP.

If application specifies the crypto keys, the keys for transmit and receive direction MUST be different.

Parameters:
  • srtp – The SRTP transport.

  • tx – Crypto suite setting for transmit direction.

  • rx – Crypto suite setting for receive direction.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_stop(pjmedia_transport *srtp)

Stop SRTP session.

Parameters:

srtp – The SRTP media transport.

Returns:

PJ_SUCCESS on success.

pj_status_t pjmedia_transport_srtp_decrypt_pkt(pjmedia_transport *tp, pj_bool_t is_rtp, void *pkt, int *pkt_len)

This is a utility function to decrypt SRTP packet using SRTP transport. This function is not part of SRTP transport’s API, but it can be used to decrypt SRTP packets from non-network (for example, from a saved file) without having to use the transport framework. See pcaputil.c in the samples collection on how to use this function.

Parameters:
  • tp – The SRTP transport.

  • is_rtp – Set to non-zero if the packet is SRTP, otherwise set to zero if the packet is SRTCP.

  • pkt – On input, it contains SRTP or SRTCP packet. On output, it contains the decrypted RTP/RTCP packet.

  • pkt_len – On input, specify the length of the buffer. On output, it will be filled with the actual length of decrypted packet.

Returns:

PJ_SUCCESS on success.

pjmedia_transport *pjmedia_transport_srtp_get_member(pjmedia_transport *srtp)

Query member transport of SRTP.

Parameters:

srtp – The SRTP media transport.

Returns:

member media transport.

struct pjmedia_srtp_crypto
#include <transport_srtp.h>

This structure describes an individual crypto setting.

Public Members

pj_str_t key

Optional key. If empty, a random key will be autogenerated.

pj_str_t name

Crypto name.

unsigned flags

Flags, bitmask from pjmedia_srtp_crypto_option

struct pjmedia_srtp_cb
#include <transport_srtp.h>

Structure containing callbacks to receive SRTP notifications.

Public Members

void (*on_srtp_nego_complete)(pjmedia_transport *tp, pj_status_t status)

This callback will be called when SRTP negotiation completes. This callback will be invoked when the negotiation is done outside of the SDP signalling, such as in DTLS-SRTP.

Param tp:

PJMEDIA SRTP transport.

Param status:

Operation status.

struct pjmedia_srtp_roc
#include <transport_srtp.h>

RTP sequence rollover counter settings.

Public Members

pj_uint32_t ssrc

The synchronization source.

pj_uint32_t roc

The rollover counter.

struct pjmedia_srtp_setting
#include <transport_srtp.h>

Settings to be given when creating SRTP transport. Application should call pjmedia_srtp_setting_default() to initialize this structure with its default values.

Public Members

pjmedia_srtp_use use

Specify the usage policy. Default is PJMEDIA_SRTP_OPTIONAL.

pj_bool_t close_member_tp

Specify whether the SRTP transport should close the member transport when it is destroyed. Default: PJ_TRUE.

unsigned crypto_count

Specify the number of crypto suite settings. If set to zero, all available cryptos will be enabled. Default: zero.

pjmedia_srtp_crypto crypto[PJMEDIA_SRTP_MAX_CRYPTOS]

Specify individual crypto suite setting and its priority order.

Notes for DTLS-SRTP keying:

  • Currently only supports these cryptos: AES_CM_128_HMAC_SHA1_80, AES_CM_128_HMAC_SHA1_32, AEAD_AES_256_GCM, and AEAD_AES_128_GCM.

  • SRTP key is not configurable.

unsigned keying_count

Specify the number of enabled keying methods. If set to zero, all keyings will be enabled. Maximum value is PJMEDIA_SRTP_MAX_KEYINGS.

Default is zero (all keyings are enabled with priority order: SDES, DTLS-SRTP).

pjmedia_srtp_keying_method keying[PJMEDIA_SRTP_KEYINGS_COUNT]

Specify enabled keying methods and its priority order. Keying method with higher priority will be given earlier chance to process the SDP, for example as currently only one keying is supported in the SDP offer, keying with first priority will be likely used in the SDP offer.

pjmedia_srtp_roc rx_roc

RTP sequence rollover counter initialization value for incoming direction. This is useful to maintain ROC after media transport recreation such as in IP change scenario.

pjmedia_srtp_roc prev_rx_roc

The previous value of RTP sequence rollover counter. This is useful in situations when we expect the remote to reset/maintain ROC but for some reason, they don’t. Thus, when we encounter SRTP packet unprotect failure during probation, we will retry to unprotect with this ROC value as well. Set prev_rx_roc.ssrc to 0 to disable this feature.

pjmedia_srtp_roc tx_roc

RTP sequence rollover counter initialization value for outgoing direction. This is useful to maintain ROC after media transport recreation such as in IP change scenario.

pjmedia_srtp_cb cb

Specify SRTP callback.

void *user_data

Specify SRTP transport user data.

struct pjmedia_srtp_info
#include <transport_srtp.h>

This structure specifies SRTP transport specific info. This will fit into buffer field of pjmedia_transport_specific_info.

Public Members

pj_bool_t active

Specify whether the SRTP transport is active for SRTP session.

pjmedia_srtp_crypto rx_policy

Specify the policy used by the SRTP session for receive direction.

pjmedia_srtp_crypto tx_policy

Specify the policy used by the SRTP session for transmit direction.

pjmedia_srtp_use use

Specify the usage policy.

pjmedia_srtp_use peer_use

Specify the peer’s usage policy.

pjmedia_srtp_roc rx_roc

RTP sequence rollover counter info for incoming direction.

pjmedia_srtp_roc tx_roc

RTP sequence rollover counter info for outgoing direction.

struct pjmedia_srtp_dtls_nego_param
#include <transport_srtp.h>

This structure specifies DTLS-SRTP negotiation parameters.

Public Members

pj_str_t rem_fingerprint

Fingerprint of remote certificate, should be formatted as “SHA-256/1 XX:XX:XX…”. If this is not set, fingerprint verification will not be performed.

pj_sockaddr rem_addr

Remote address and port.

pj_sockaddr rem_rtcp

Remote RTCP address and port.

pj_bool_t is_role_active

Set to PJ_TRUE if our role is active. Active role will initiates the DTLS negotiation. Passive role will wait for incoming DTLS negotiation packet.