rtci_stun/
nonce_cookie.rs1use base64::prelude::*;
12
13pub const NONCE_COOKIE_PREFIX: &str = "obMatJos2";
15pub const NONCE_COOKIE_LEN: usize = 13;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
26pub struct SecurityFeatures(u32);
27
28impl SecurityFeatures {
29 pub const NONE: Self = Self(0);
31 pub const PASSWORD_ALGORITHMS: Self = Self(1);
33 pub const USERNAME_ANONYMITY: Self = Self(1 << 1);
35
36 #[must_use]
39 pub fn from_nonce(nonce: &str) -> Option<Self> {
40 let encoded = nonce
41 .strip_prefix(NONCE_COOKIE_PREFIX)?
42 .as_bytes()
43 .get(..NONCE_COOKIE_LEN - NONCE_COOKIE_PREFIX.len())?;
44 let bits = BASE64_STANDARD.decode(encoded).ok()?;
45 let bits: [u8; 3] = bits.try_into().ok()?;
46 Some(Self(u32::from_be_bytes([0, bits[0], bits[1], bits[2]])))
47 }
48
49 #[must_use]
51 pub const fn with(self, other: Self) -> Self {
52 Self(self.0 | other.0)
53 }
54
55 #[must_use]
57 pub const fn contains(self, other: Self) -> bool {
58 self.0 & other.0 == other.0
59 }
60
61 #[must_use]
63 pub const fn password_algorithms(self) -> bool {
64 self.contains(Self::PASSWORD_ALGORITHMS)
65 }
66
67 #[must_use]
69 pub const fn username_anonymity(self) -> bool {
70 self.contains(Self::USERNAME_ANONYMITY)
71 }
72
73 #[must_use]
75 pub const fn bits(self) -> u32 {
76 self.0
77 }
78
79 #[must_use]
81 pub fn cookie(self) -> String {
82 let bytes = self.0.to_be_bytes();
83 format!(
84 "{NONCE_COOKIE_PREFIX}{}",
85 BASE64_STANDARD.encode(&bytes[1..])
86 )
87 }
88}
89
90#[cfg(test)]
91mod nonce_cookie_test {
92 use super::*;
93
94 #[test]
98 fn test_security_features_are_read_from_the_nonce_cookie() {
99 let password = SecurityFeatures::from_nonce("obMatJos2AAABserver-part").unwrap();
101 assert!(password.password_algorithms() && !password.username_anonymity());
102 let anonymity =
104 SecurityFeatures::from_nonce("obMatJos2AAACf//499k954d6OL34oL9FSTvy64sA").unwrap();
105 assert!(!anonymity.password_algorithms() && anonymity.username_anonymity());
106 let both = SecurityFeatures::from_nonce("obMatJos2AAAD").unwrap();
107 assert!(both.password_algorithms() && both.username_anonymity());
108 let other = SecurityFeatures::from_nonce("obMatJos2gAAAserver-part").unwrap();
110 assert!(!other.password_algorithms() && !other.username_anonymity());
111 assert_eq!(other.bits(), 0x80_0000);
112 let none = SecurityFeatures::from_nonce("obMatJos2AAAAf//499k954d6OL34oL9FSTvy64sA");
113 assert_eq!(none, Some(SecurityFeatures::NONE));
114
115 assert_eq!(
116 SecurityFeatures::PASSWORD_ALGORITHMS.cookie(),
117 "obMatJos2AAAB"
118 );
119 assert_eq!(
120 SecurityFeatures::PASSWORD_ALGORITHMS
121 .with(SecurityFeatures::USERNAME_ANONYMITY)
122 .cookie(),
123 "obMatJos2AAAD"
124 );
125 assert_eq!(SecurityFeatures::NONE.cookie().len(), NONCE_COOKIE_LEN);
126 }
127
128 #[test]
130 fn test_a_nonce_without_the_cookie_has_no_security_features() {
131 for nonce in [
132 "",
133 "f//499k954d6OL34oL9FSTvy64sA",
134 "obMatJos2",
135 "obMatJos2AAA",
136 "obMatJos2A*ABxyz",
137 "xobMatJos2AAAB",
138 ] {
139 assert_eq!(SecurityFeatures::from_nonce(nonce), None, "{nonce:?}");
140 }
141 }
142}