Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

Context represents a SRTP cryptographic context Context can only be used for one-way operations it must either used ONLY for encryption or ONLY for decryption

§Key lifetime

A master key protects a limited number of packets (RFC 3711 section 9.2): 2^48 SRTP and 2^31 SRTCP by default, less if set_key_lifetime says so. The context counts them. Shortly before a key runs out it raises ContextEvent::KeyLifetimeExpiring; at the limit it refuses every further packet with Error::ErrExceededMaxPackets, in both directions, and raises ContextEvent::KeyExhausted. Read the events with poll_event and re-key: build new contexts from a new DTLS-SRTP handshake, or, with an MKI, add_master_key and set_sending_mki.

Implementations§

Source§

impl Context

Source

pub fn decrypt_rtcp(&mut self, encrypted: &[u8]) -> Result<BytesMut>

DecryptRTCP decrypts a RTCP packet with an encrypted payload

The packet is authenticated whatever its E flag says, and decrypted only when the flag is set (RFC 3711 section 3.4, RFC 7714 section 9.3).

§Errors

Fails if authentication fails, if the packet is a replay or too short, if it names a master key the context does not have, or with Error::ErrExceededMaxPackets once the master key reached its lifetime (RFC 3711 section 9.2).

Source

pub fn encrypt_rtcp(&mut self, decrypted: &[u8]) -> Result<BytesMut>

EncryptRTCP marshals and encrypts an RTCP packet, writing to the dst buffer provided. If the dst buffer does not have the capacity to hold len(plaintext) + 14 bytes, a new one will be allocated and returned.

The first packet of an SSRC carries SRTCP index 0, and each later one the next index modulo 2^31 (RFC 3711 section 3.4).

§Errors

Fails with Error::ErrExceededMaxPackets once the master key has protected its lifetime of SRTCP packets, 2^31 at most (RFC 3711 section 9.2, RFC 7714 section 9.4): one more would reuse a keystream, or a GCM nonce, under the same key. See ContextEvent::KeyExhausted.

Source§

impl Context

Source

pub fn decrypt_rtp_with_header( &mut self, encrypted: &[u8], header: &Header, ) -> Result<BytesMut>

Decrypts an SRTP packet whose header has already been parsed.

Saves re-parsing when the caller needed the header to route the packet. The header must be the one belonging to encrypted.

§Errors

Fails if authentication fails, if the packet is a replay, if it is too short to hold the profile’s auth tag, if it names a master key the context does not have, or with Error::ErrExceededMaxPackets once the master key reached its lifetime or the packet index would pass 2^48 - 1 (RFC 3711 sections 3.3.1 and 9.2).

Source

pub fn decrypt_rtp(&mut self, encrypted: &[u8]) -> Result<BytesMut>

DecryptRTP decrypts a RTP packet with an encrypted payload

Source

pub fn encrypt_rtp_with_header( &mut self, plaintext: &[u8], header: &Header, ) -> Result<BytesMut>

Encrypts an RTP payload, using an already-parsed header.

Saves re-parsing when the caller has just built the header. Returns the full protected packet, header included.

§Errors

Fails if the SRTP context has no key for this SSRC, if the cipher rejects the input, or with Error::SrtpIndexReused if this packet index (ROC and sequence number) already protected a different packet, or lies more than a window behind the highest index sent. The same packet under the same index (a retransmission) is protected again, which yields the same bytes.

Fails with Error::ErrExceededMaxPackets once the master key reached its lifetime, or the stream its 2^48th packet (RFC 3711 sections 3.3.1 and 9.2); see ContextEvent::KeyExhausted.

With Cryptex required (Cryptex::require), fails for a packet that has CSRCs or a header extension and cannot be protected with Cryptex (an extension that is not RFC 8285’s, or two-byte headers with non-zero “appbits”): nothing leaves with them in the clear.

Source

pub fn encrypt_rtp(&mut self, plaintext: &[u8]) -> Result<BytesMut>

EncryptRTP marshals and encrypts an RTP packet, writing to the dst buffer provided. If the dst buffer does not have the capacity to hold len(plaintext) + 10 bytes, a new one will be allocated and returned.

Source§

impl Context

Source

pub fn new( master_key: &[u8], master_salt: &[u8], profile: ProtectionProfile, srtp_ctx_opt: Option<ContextOption>, srtcp_ctx_opt: Option<ContextOption>, crypto: &dyn RTCCrypto, ) -> Result<Context>

Creates an SRTP context.

The crypto implementation is supplied by the caller; this crate never resolves a default. Borrowed, not owned: it is only needed to build the keyed cipher/MAC objects below, which are what the context retains.

None for a replay option means replay protection with a window of DEFAULT_REPLAY_PROTECTION_WINDOW packets (RFC 3711 section 3.3.2); pass srtp_no_replay_protection / srtcp_no_replay_protection to go without.

The context has one master key, no MKI and a zero key derivation rate, which is what DTLS-SRTP uses; see with_keying for the rest.

Source

pub fn with_keying( master_key: &[u8], master_salt: &[u8], profile: ProtectionProfile, keying: Keying, srtp_ctx_opt: Option<ContextOption>, srtcp_ctx_opt: Option<ContextOption>, provider: Arc<dyn RTCCryptoProvider>, ) -> Result<Context>

Creates an SRTP context that uses a Master Key Identifier, a non-zero key derivation rate, or both (RFC 3711 sections 3.1, 4.3.1 and 8.1).

With Keying::mki set, every packet the context protects carries the MKI of the master key in use, and every packet it unprotects is processed with the master key its MKI names; further keys are added with add_master_key. With a non-zero Keying::key_derivation_rate the session keys are derived again each time the packet index crosses a multiple of the rate.

Unlike new the provider is retained, to key those later derivations, and with a non-zero rate so are the master keys and salts (zeroized when dropped).

With a non-zero rate a received packet whose estimated index falls in another derivation epoch costs a key derivation before it can be authenticated, so a forged packet costs more than with a zero rate (a few AES blocks and key schedules; the keys of the last two epochs are kept).

§Errors

As new, and when the MKI is empty or longer than MAX_MKI_LEN.

Source

pub fn profile(&self) -> ProtectionProfile

The protection profile the context was created with.

Source

pub fn cryptex(&self) -> Cryptex

How this context uses Cryptex (RFC 9335); see Cryptex.

Source

pub fn set_cryptex(&mut self, cryptex: Cryptex)

Sets how this context uses Cryptex (RFC 9335). It applies from the next packet.

Source

pub fn encrypted_header_extensions(&self) -> EncryptedHeaderExtensions

The header extension elements this context encrypts and decrypts as RFC 6904 has it.

Source

pub fn set_encrypted_header_extensions( &mut self, ids: EncryptedHeaderExtensions, )

Sets the header extension elements whose payload is encrypted (RFC 6904): the ids the session negotiated with urn:ietf:params:rtp-hdrext:encrypt. It applies from the next packet, in the direction the context is used for. The empty set turns it off.

A packet protected with Cryptex is not also processed this way (RFC 9335 section 5). When unprotecting, the header passed to decrypt_rtp_with_header was parsed from the protected packet, so its encrypted elements are ciphertext: read them from the returned packet.

Source

pub fn srtcp_encryption(&self) -> bool

Whether SRTCP packets are encrypted when protected.

Source

pub fn set_srtcp_encryption(&mut self, encrypt: bool)

Sets whether SRTCP packets are encrypted when protected (the default) or only authenticated, with the E flag clear (RFC 3711 section 3.4, RFC 7714 section 9.3). Unprotecting follows each packet’s E flag whatever this says; authentication is never optional.

Source

pub fn key_derivation_rate(&self) -> KeyDerivationRate

The key derivation rate the context was created with.

Source

pub fn key_lifetime(&self) -> KeyLifetime

How many packets a master key of this context may protect.

Source

pub fn set_key_lifetime(&mut self, lifetime: KeyLifetime) -> Result<()>

Sets how many packets a master key of this context may protect, for instance the lifetime key management agreed on (RFC 5764 section 4.1.2: 2^31 packets for the DTLS-SRTP profiles).

§Errors

A lifetime of zero, or above 2^48 SRTP or 2^31 SRTCP packets, is refused.

Source

pub fn key_usage(&self) -> KeyUsage

How much of the lifetime of the master key packets are protected with is used.

Source

pub fn key_usage_of(&self, mki: &[u8]) -> Option<KeyUsage>

How much of the lifetime of the master key with this MKI is used; None when the context has no such key.

Source

pub fn poll_event(&mut self) -> Option<ContextEvent>

The next thing the context has to tell the application; see ContextEvent.

Source

pub fn discarded_packets(&self) -> (u64, u64)

How many received packets were discarded so far: (replays, authentication failures) (RFC 3711 section 3.3).

Source

pub fn add_master_key( &mut self, mki: &[u8], master_key: &[u8], master_salt: &[u8], ) -> Result<()>

Adds a master key under mki (RFC 3711 section 8.1). Packets carrying that MKI are unprotected with it from now on; packets are protected with it once set_sending_mki selects it. The rollover counters, the SRTCP indexes and the replay lists carry over (RFC 3711 sections 3.3.1 and 3.4).

A master key must never be one used before (RFC 3711 section 9.2); that is for key management to ensure, the context cannot tell.

§Errors

Fails when the context was not created with an MKI (with_keying), when mki is not of the context’s MKI length or already names a key, when the key or salt length is not the profile’s, or when MAX_MASTER_KEYS keys are held.

Source

pub fn remove_master_key(&mut self, mki: &[u8]) -> Result<()>

Removes the master key under mki, erasing the session keys derived from it. Packets carrying that MKI are refused from now on.

§Errors

Fails when no key has that MKI, or when it is the key packets are protected with.

Source

pub fn set_sending_mki(&mut self, mki: &[u8]) -> Result<()>

Protects packets with the master key under mki from the next packet on.

§Errors

Fails when no key has that MKI.

Source

pub fn sending_mki(&self) -> Option<&[u8]>

The MKI of the master key packets are protected with; None when the context uses no MKI.

Source

pub fn mkis(&self) -> impl Iterator<Item = &[u8]>

The MKIs of the master keys the context holds; empty when it uses no MKI.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Context

§

impl !Sync for Context

§

impl !UnwindSafe for Context

§

impl Forget for Context

§

impl Freeze for Context
where *const u8 is !null: Freeze, PhantomData<MasterKey>: Freeze, *const ArcInner<dyn RTCCryptoProvider> is !null: Freeze, PhantomData<ArcInner<dyn RTCCryptoProvider>>: Freeze, PhantomData<(u32, SrtpSsrcState)>: Freeze, PhantomData<(u32, SrtcpSsrcState)>: Freeze, *const dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send is !null: Freeze, PhantomData<dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send>: Freeze, PhantomData<ContextEvent>: Freeze,

§

impl Move for Context

§

impl Send for Context
where RawVecInner: Send, BoxRaw<dyn Cipher>: Send, Arc<dyn RTCCryptoProvider>: Send, RawTable<(u32, SrtpSsrcState)>: Send, RawTable<(u32, SrtcpSsrcState)>: Send, BoxRaw<dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send>: Send,

§

impl Unpin for Context
where *const u8 is !null: Unpin, Box<dyn Cipher>: Unpin, Arc<dyn RTCCryptoProvider>: Unpin, Box<dyn ReplayDetector>: Unpin, Box<dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send>: Unpin,

§

impl UnsafeUnpin for Context
where *const u8 is !null: UnsafeUnpin, PhantomData<MasterKey>: UnsafeUnpin, *const ArcInner<dyn RTCCryptoProvider> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn RTCCryptoProvider>>: UnsafeUnpin, PhantomData<(u32, SrtpSsrcState)>: UnsafeUnpin, PhantomData<(u32, SrtcpSsrcState)>: UnsafeUnpin, *const dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send is !null: UnsafeUnpin, PhantomData<dyn Fn() -> Box<dyn ReplayDetector> + Sync + Send>: UnsafeUnpin, PhantomData<ContextEvent>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.