rtci-turn 0.5.0

Sans-I/O TURN client (RFC 8656, RFC 6062 TCP allocations) for WebRTC
Documentation
//! The long-term credential of one allocation (RFC 8489 §9.2), as snapshots.
//!
//! [`LongTermCredentials`] holds the rules: what a 401 or 438 turns into, which attributes a
//! request carries, which responses are authentic. A [`Credential`] is one state of it and is
//! never changed: a challenge, or a new password, makes a new one. A transaction keeps the
//! snapshot its request was made with, so its response is checked against the key that
//! request was signed with, whatever the server has asked for since.
use std::sync::Arc;

use crypto::{RTCCrypto, SecretVec};
use shared::error::Result;
use stun::attributes::{
    ATTR_MESSAGE_INTEGRITY, ATTR_MESSAGE_INTEGRITY_SHA256, ATTR_NONCE, ATTR_PASSWORD_ALGORITHMS,
    ATTR_REALM,
};
use stun::error_code::{
    CODE_BAD_REQUEST, CODE_STALE_NONCE, CODE_UNAUTHORIZED, ErrorCode, ErrorCodeAttribute,
};
use stun::integrity::long_term_key;
use stun::long_term::{ChallengeError, LongTermCredentials, LongTermResponse};
use stun::message::{CLASS_ERROR_RESPONSE, Getter, Message, Setter};
use stun::nonce_cookie::SecurityFeatures;
use stun::password_algorithm::{
    PASSWORD_ALGORITHM_MD5, PASSWORD_ALGORITHM_SHA256, PasswordAlgorithms,
};
use stun::textattrs::TextAttribute;
use stun::validated::{Rejection, ValidatedMessage, Validator};

/// What the application gave the client to authenticate with.
pub(crate) struct Login {
    pub(crate) username: String,
    pub(crate) password: String,
    /// The password algorithms the client accepts from a server's PASSWORD-ALGORITHMS; empty
    /// for all it implements (SHA-256 and MD5).
    pub(crate) password_algorithms: Vec<u16>,
    /// The user name goes only as USERHASH (`ClientConfig::username_anonymity_required`).
    pub(crate) username_anonymity_required: bool,
}

/// What a server's challenge added to the login.
struct Session {
    credentials: LongTermCredentials,
    /// The 401 or 438 the session was made from (its covered attributes), from which it is
    /// made again for a new password.
    challenge: Message,
    /// The long-term key, for the one check [`LongTermCredentials`] leaves out: a 401 or 438
    /// that carries an integrity attribute.
    key: SecretVec,
}

/// The credential requests are made with: none until the server asked for one.
pub(crate) struct Credential {
    login: Arc<Login>,
    session: Option<Session>,
}

impl Credential {
    /// The credential of a first request: nothing (RFC 8489 §9.2.3.1).
    pub(crate) fn new(login: Arc<Login>) -> Self {
        Self {
            login,
            session: None,
        }
    }

    /// Whether the application gave a credential (a user name or a password) to
    /// authenticate with, asked for or not.
    pub(crate) fn has_login(&self) -> bool {
        !self.login.username.is_empty() || !self.login.password.is_empty()
    }

    /// Whether the server has asked for the credential, so that requests carry it.
    pub(crate) fn is_established(&self) -> bool {
        self.session.is_some()
    }

    /// Whether this credential was made from `login`: the same user name, password and
    /// accepted password algorithms.
    pub(crate) fn is_for(&self, login: &Login) -> bool {
        self.login.username == login.username
            && self.login.password == login.password
            && self.login.password_algorithms == login.password_algorithms
    }

    /// Whether `other` was made from the same login as this credential.
    pub(crate) fn has_login_of(&self, other: &Credential) -> bool {
        self.is_for(&other.login)
    }

    /// The nonce requests carry.
    #[cfg(test)]
    pub(crate) fn nonce(&self) -> Option<&str> {
        self.session.as_ref()?.credentials.nonce()
    }

    fn build(
        login: &Arc<Login>,
        earlier: Option<&Message>,
        new: Option<(&Message, &Message)>,
        crypto: &dyn RTCCrypto,
    ) -> std::result::Result<Self, ChallengeError> {
        let unusable = |err: shared::error::Error| ChallengeError::Unusable(err.to_string());

        // What the new challenge says, less whatever follows an integrity attribute
        // (RFC 8489 §9).
        let new = match new {
            Some((request, response)) => Some((
                request,
                Validator::new()
                    .unauthenticated(response)
                    .map_err(|_| ChallengeError::NotAChallenge)?
                    .into_message(),
            )),
            None => None,
        };
        let Some(latest) = new.as_ref().map(|(_, response)| response).or(earlier) else {
            return Ok(Self::new(login.clone()));
        };

        let mut credentials = LongTermCredentials::new(&login.username, &login.password)
            .map_err(unusable)?
            .with_password_algorithms(&accepted_algorithms(login, latest))
            .with_username_anonymity_required(login.username_anonymity_required);
        let mut challenge = None;
        if let Some((request, mut response)) = new {
            // The state the earlier challenge left, against which the new one is judged: a
            // 401 to a request that carried these very credentials is not answered again.
            // (An earlier challenge the algorithms of this one rule out left no such state.)
            if let Some(earlier) = earlier {
                let _ = credentials.challenge(&Message::new(), earlier, crypto);
            }
            // RFC 8489 §9.2.4 has a 438 (Stale Nonce) carry the REALM. A server that leaves
            // it out of the response to a request that carried one means the realm it
            // already gave: the nonce is what a 438 is about.
            if !response.contains(ATTR_REALM)
                && is_stale_nonce(&response)
                && let Some(realm) = credentials.realm()
            {
                let realm = realm.to_owned();
                response.add(ATTR_REALM, realm.as_bytes());
            }
            credentials.challenge(request, &response, crypto)?;
            challenge = Some(response);
        } else if let Some(earlier) = earlier {
            credentials.challenge(&Message::new(), earlier, crypto)?;
            challenge = Some(earlier.clone());
        }
        let Some(challenge) = challenge else {
            return Ok(Self::new(login.clone()));
        };
        let key = long_term_key(
            credentials
                .password_algorithm()
                .map_or(PASSWORD_ALGORITHM_MD5, |a| a.algorithm),
            credentials.username(),
            credentials.realm().unwrap_or_default(),
            &login.password,
            crypto,
        )
        .map_err(unusable)?;
        Ok(Self {
            login: login.clone(),
            session: Some(Session {
                credentials,
                challenge,
                key: SecretVec::new(key),
            }),
        })
    }

    /// The credential after the 401 (Unauthenticated) or 438 (Stale Nonce) `response` to
    /// `request`, by the rules of RFC 8489 §9.2.5: the realm, nonce, password algorithm and
    /// user name form the server asked for.
    ///
    /// # Errors
    ///
    /// The request is not to be made again: the nonce cookie announces PASSWORD-ALGORITHMS
    /// and the response has none (a bid-down), no offered algorithm is supported, the same
    /// credentials were just refused, or the response has no realm or nonce.
    pub(crate) fn challenged(
        &self,
        request: &Message,
        response: &Message,
        crypto: &dyn RTCCrypto,
    ) -> std::result::Result<Self, ChallengeError> {
        Self::build(
            &self.login,
            self.session.as_ref().map(|s| &s.challenge),
            Some((request, response)),
            crypto,
        )
    }

    /// The same session (realm, nonce, algorithm) for another user name or password.
    ///
    /// # Errors
    ///
    /// Fails when the user name or the password cannot be used (RFC 8489 §9.2.2: they are
    /// OpaqueStrings).
    pub(crate) fn with_login(&self, login: Arc<Login>, crypto: &dyn RTCCrypto) -> Result<Self> {
        Self::build(
            &login,
            self.session.as_ref().map(|s| &s.challenge),
            None,
            crypto,
        )
        .map_err(|err| shared::error::Error::Other(err.to_string()))
    }

    /// The authentication attributes of a request made with this credential; `None` before
    /// the server asked for it.
    pub(crate) fn setter<'a>(&'a self, crypto: &'a dyn RTCCrypto) -> Option<Box<dyn Setter + 'a>> {
        let session = self.session.as_ref()?;
        Some(Box::new(session.credentials.setter(crypto)))
    }

    /// Checks the response to a request made with this credential (RFC 8489 §9.2.5).
    ///
    /// Two kinds of response are discarded "as if [they] were never received"
    /// ([`Checked::NeverReceived`]), whatever the request carried: a 400 (Bad Request)
    /// without an integrity attribute, and, for a request without a credential, a response
    /// other than a 401 or 438 whose NONCE has the nonce cookie with the "password
    /// algorithms" bit and no PASSWORD-ALGORITHMS ("MUST be ignored"; a request with the
    /// credential has it checked by [`LongTermCredentials`]). Neither ends the transaction,
    /// nor counts as a response that failed its integrity check: a request answered by
    /// nothing else times out, as the RFC's note to the 400 rule says.
    ///
    /// Otherwise a request that carried no credential takes any response, less what follows
    /// an integrity attribute: it has no key to check one with, and the 401 that asks for the
    /// credential is unsigned. One that carried it takes a response signed with the same key
    /// and attribute, or a 401 or 438, which a server cannot always sign; a 401 or 438 that is
    /// signed all the same must verify.
    ///
    /// # Errors
    ///
    /// The [`Rejection`] the response is discarded for.
    pub(crate) fn check_response(
        &self,
        response: &Message,
        validator: &Validator<'_>,
        crypto: &dyn RTCCrypto,
    ) -> std::result::Result<Checked, Rejection> {
        if is_unsigned_bad_request(response) {
            return Ok(Checked::NeverReceived);
        }
        let Some(session) = &self.session else {
            let valid = validator.unauthenticated(response)?;
            if !is_challenge(&valid) && promises_password_algorithms_it_lacks(&valid) {
                return Ok(Checked::NeverReceived);
            }
            return Ok(Checked::Valid(valid));
        };
        let valid = match session
            .credentials
            .check_response(response, validator, crypto)?
        {
            LongTermResponse::Authenticated(valid) => valid,
            LongTermResponse::Challenge(unsigned) => {
                if response.contains(ATTR_MESSAGE_INTEGRITY)
                    || response.contains(ATTR_MESSAGE_INTEGRITY_SHA256)
                {
                    validator.authenticate(response, session.key.as_ref(), crypto)?
                } else {
                    unsigned
                }
            }
        };
        Ok(Checked::Valid(valid))
    }
}

/// What [`Credential::check_response`] makes of a response it does not reject.
pub(crate) enum Checked {
    /// The response, to act on: the attributes ahead of its integrity attribute.
    Valid(ValidatedMessage),
    /// RFC 8489 §9.2.5 has it discarded as if it had never arrived: the transaction goes on
    /// (retransmissions continue) and, answered by nothing else, ends in a timeout.
    NeverReceived,
}

/// The code of the ERROR-CODE attribute of `m`, when it is an error response that has one.
fn error_code_of(m: &Message) -> Option<ErrorCode> {
    if m.typ.class != CLASS_ERROR_RESPONSE {
        return None;
    }
    let mut code = ErrorCodeAttribute::default();
    code.get_from(m).ok()?;
    Some(code.code)
}

/// RFC 8489 §9.2.5: "If the response is an error response with an error code of 400 (Bad
/// Request) and does not contain either the MESSAGE-INTEGRITY or MESSAGE-INTEGRITY-SHA256
/// attribute, then the response MUST be discarded, as if it were never received."
fn is_unsigned_bad_request(response: &Message) -> bool {
    error_code_of(response) == Some(CODE_BAD_REQUEST)
        && !response.contains(ATTR_MESSAGE_INTEGRITY)
        && !response.contains(ATTR_MESSAGE_INTEGRITY_SHA256)
}

/// A 401 (Unauthenticated) or 438 (Stale Nonce): the responses §9.2.5 has a client act on
/// unsigned, under their own rules ([`Credential::challenged`]).
fn is_challenge(response: &Message) -> bool {
    matches!(error_code_of(response), Some(c) if c == CODE_UNAUTHORIZED || c == CODE_STALE_NONCE)
}

/// RFC 8489 §9.2.5: "For all other responses, if the NONCE attribute starts with the "nonce
/// cookie" with the STUN Security Feature "Password algorithms" bit set to 1 but
/// PASSWORD-ALGORITHMS is not present, the response MUST be ignored."
fn promises_password_algorithms_it_lacks(response: &Message) -> bool {
    TextAttribute::get_from_as(response, ATTR_NONCE).is_ok_and(|nonce| {
        SecurityFeatures::from_nonce(&nonce.text).is_some_and(|f| f.password_algorithms())
    }) && !response.contains(ATTR_PASSWORD_ALGORITHMS)
}

/// The password algorithms the client takes from the PASSWORD-ALGORITHMS of `challenge`.
///
/// RFC 8489 §9.2.5 has a client use the first algorithm of the server's list it supports.
/// TURN narrows that (RFC 8656 §6): when the list holds both MD5 and SHA-256 and the client
/// supports both, the request carries SHA-256, wherever the server put it. So with SHA-256 on
/// offer and allowed, it is the only one taken.
fn accepted_algorithms(login: &Login, challenge: &Message) -> Vec<u16> {
    let allowed = if login.password_algorithms.is_empty() {
        vec![PASSWORD_ALGORITHM_SHA256, PASSWORD_ALGORITHM_MD5]
    } else {
        login.password_algorithms.clone()
    };
    let mut offered = PasswordAlgorithms::default();
    if allowed.contains(&PASSWORD_ALGORITHM_SHA256)
        && offered.get_from(challenge).is_ok()
        && offered
            .0
            .iter()
            .any(|a| a.algorithm == PASSWORD_ALGORITHM_SHA256)
    {
        vec![PASSWORD_ALGORITHM_SHA256]
    } else {
        allowed
    }
}

/// Whether `m` is a 438 (Stale Nonce) error response.
fn is_stale_nonce(m: &Message) -> bool {
    let mut code = ErrorCodeAttribute::default();
    m.typ.class == CLASS_ERROR_RESPONSE && code.get_from(m).is_ok() && code.code == CODE_STALE_NONCE
}

#[cfg(test)]
impl Credential {
    /// The credential of the scripted server's user ("user", "pass") after its 401 with the
    /// realm "realm" and the nonce "nonce", for tests that start from a granted allocation.
    pub(crate) fn established() -> Arc<Self> {
        use stun::error_code::CODE_UNAUTHORIZED;
        use stun::message::{METHOD_ALLOCATE, MessageType, TransactionId};
        use stun::textattrs::TextAttribute;

        let provider = crypto::default_provider().expect("a built-in crypto provider");
        let mut challenge = Message::new();
        challenge
            .build(&[
                Box::new(TransactionId::new()),
                Box::new(MessageType::new(METHOD_ALLOCATE, CLASS_ERROR_RESPONSE)),
                Box::new(ErrorCodeAttribute {
                    code: CODE_UNAUTHORIZED,
                    reason: b"Unauthorized".to_vec(),
                }),
                Box::new(TextAttribute::new(
                    stun::attributes::ATTR_NONCE,
                    "nonce".to_owned(),
                )),
                Box::new(TextAttribute::new(ATTR_REALM, "realm".to_owned())),
            ])
            .expect("the challenge builds");
        let login = Arc::new(Login {
            username: "user".to_owned(),
            password: "pass".to_owned(),
            password_algorithms: vec![],
            username_anonymity_required: false,
        });
        Arc::new(
            Self::new(login)
                .challenged(&Message::new(), &challenge, provider.crypto())
                .expect("the challenge is taken"),
        )
    }
}