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
impl Context
Sourcepub fn decrypt_rtcp(&mut self, encrypted: &[u8]) -> Result<BytesMut>
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).
Sourcepub fn encrypt_rtcp(&mut self, decrypted: &[u8]) -> Result<BytesMut>
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
impl Context
Sourcepub fn decrypt_rtp_with_header(
&mut self,
encrypted: &[u8],
header: &Header,
) -> Result<BytesMut>
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).
Sourcepub fn decrypt_rtp(&mut self, encrypted: &[u8]) -> Result<BytesMut>
pub fn decrypt_rtp(&mut self, encrypted: &[u8]) -> Result<BytesMut>
DecryptRTP decrypts a RTP packet with an encrypted payload
Sourcepub fn encrypt_rtp_with_header(
&mut self,
plaintext: &[u8],
header: &Header,
) -> Result<BytesMut>
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.
Sourcepub fn encrypt_rtp(&mut self, plaintext: &[u8]) -> Result<BytesMut>
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
impl Context
Sourcepub 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>
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.
Sourcepub 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>
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.
Sourcepub fn profile(&self) -> ProtectionProfile
pub fn profile(&self) -> ProtectionProfile
The protection profile the context was created with.
Sourcepub fn set_cryptex(&mut self, cryptex: Cryptex)
pub fn set_cryptex(&mut self, cryptex: Cryptex)
Sets how this context uses Cryptex (RFC 9335). It applies from the next packet.
Sourcepub fn encrypted_header_extensions(&self) -> EncryptedHeaderExtensions
pub fn encrypted_header_extensions(&self) -> EncryptedHeaderExtensions
The header extension elements this context encrypts and decrypts as RFC 6904 has it.
Sourcepub fn set_encrypted_header_extensions(
&mut self,
ids: EncryptedHeaderExtensions,
)
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.
Sourcepub fn srtcp_encryption(&self) -> bool
pub fn srtcp_encryption(&self) -> bool
Whether SRTCP packets are encrypted when protected.
Sourcepub fn set_srtcp_encryption(&mut self, encrypt: bool)
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.
Sourcepub fn key_derivation_rate(&self) -> KeyDerivationRate
pub fn key_derivation_rate(&self) -> KeyDerivationRate
The key derivation rate the context was created with.
Sourcepub fn key_lifetime(&self) -> KeyLifetime
pub fn key_lifetime(&self) -> KeyLifetime
How many packets a master key of this context may protect.
Sourcepub fn set_key_lifetime(&mut self, lifetime: KeyLifetime) -> Result<()>
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.
Sourcepub fn key_usage(&self) -> KeyUsage
pub fn key_usage(&self) -> KeyUsage
How much of the lifetime of the master key packets are protected with is used.
Sourcepub fn key_usage_of(&self, mki: &[u8]) -> Option<KeyUsage>
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.
Sourcepub fn poll_event(&mut self) -> Option<ContextEvent>
pub fn poll_event(&mut self) -> Option<ContextEvent>
The next thing the context has to tell the application; see ContextEvent.
Sourcepub fn discarded_packets(&self) -> (u64, u64)
pub fn discarded_packets(&self) -> (u64, u64)
How many received packets were discarded so far: (replays, authentication failures) (RFC 3711 section 3.3).
Sourcepub fn add_master_key(
&mut self,
mki: &[u8],
master_key: &[u8],
master_salt: &[u8],
) -> Result<()>
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.
Sourcepub fn remove_master_key(&mut self, mki: &[u8]) -> Result<()>
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.
Sourcepub fn set_sending_mki(&mut self, mki: &[u8]) -> Result<()>
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.
Sourcepub fn sending_mki(&self) -> Option<&[u8]>
pub fn sending_mki(&self) -> Option<&[u8]>
The MKI of the master key packets are protected with; None when the context uses no
MKI.