caps.rs

  1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7use try_from::TryFrom;
  8
  9use presence::PresencePayload;
 10use disco::{Feature, Identity, DiscoInfoResult, DiscoInfoQuery};
 11use data_forms::DataForm;
 12use hashes::{Hash, Algo};
 13
 14use minidom::Element;
 15use error::Error;
 16use ns;
 17use base64;
 18
 19use sha1::Sha1;
 20use sha2::{Sha256, Sha512};
 21use sha3::{Sha3_256, Sha3_512};
 22use blake2::Blake2b;
 23use digest::{Digest, VariableOutput};
 24
 25/// Represents a capability hash for a given client.
 26#[derive(Debug, Clone)]
 27pub struct Caps {
 28    /// Deprecated list of additional feature bundles.
 29    pub ext: Option<String>,
 30
 31    /// A URI identifying an XMPP application.
 32    pub node: String,
 33
 34    /// The hash of that application’s
 35    /// [disco#info](../disco/struct.DiscoInfoResult.html).
 36    ///
 37    /// Warning: This protocol is insecure, you may want to switch to
 38    /// [ecaps2](../ecaps2/index.html) instead, see [this
 39    /// email](https://mail.jabber.org/pipermail/security/2009-July/000812.html).
 40    pub hash: Hash,
 41}
 42
 43impl PresencePayload for Caps {}
 44
 45impl TryFrom<Element> for Caps {
 46    type Err = Error;
 47
 48    fn try_from(elem: Element) -> Result<Caps, Error> {
 49        check_self!(elem, "c", CAPS, "caps");
 50        check_no_children!(elem, "caps");
 51        check_no_unknown_attributes!(elem, "caps", ["hash", "ver", "ext", "node"]);
 52        let ver: String = get_attr!(elem, "ver", required);
 53        let hash = Hash {
 54            algo: get_attr!(elem, "hash", required),
 55            hash: base64::decode(&ver)?,
 56        };
 57        Ok(Caps {
 58            ext: get_attr!(elem, "ext", optional),
 59            node: get_attr!(elem, "node", required),
 60            hash: hash,
 61        })
 62    }
 63}
 64
 65impl From<Caps> for Element {
 66    fn from(caps: Caps) -> Element {
 67        Element::builder("c")
 68                .ns(ns::CAPS)
 69                .attr("ext", caps.ext)
 70                .attr("hash", caps.hash.algo)
 71                .attr("node", caps.node)
 72                .attr("ver", base64::encode(&caps.hash.hash))
 73                .build()
 74    }
 75}
 76
 77fn compute_item(field: &str) -> Vec<u8> {
 78    let mut bytes = field.as_bytes().to_vec();
 79    bytes.push(b'<');
 80    bytes
 81}
 82
 83fn compute_items<T, F: Fn(&T) -> Vec<u8>>(things: &[T], encode: F) -> Vec<u8> {
 84    let mut string: Vec<u8> = vec!();
 85    let mut accumulator: Vec<Vec<u8>> = vec!();
 86    for thing in things {
 87        let bytes = encode(thing);
 88        accumulator.push(bytes);
 89    }
 90    // This works using the expected i;octet collation.
 91    accumulator.sort();
 92    for mut bytes in accumulator {
 93        string.append(&mut bytes);
 94    }
 95    string
 96}
 97
 98fn compute_features(features: &[Feature]) -> Vec<u8> {
 99    compute_items(features, |feature| compute_item(&feature.var))
100}
101
102fn compute_identities(identities: &[Identity]) -> Vec<u8> {
103    compute_items(identities, |identity| {
104        let lang = identity.lang.clone().unwrap_or_default();
105        let name = identity.name.clone().unwrap_or_default();
106        let string = format!("{}/{}/{}/{}", identity.category, identity.type_, lang, name);
107        let bytes = string.as_bytes();
108        let mut vec = Vec::with_capacity(bytes.len());
109        vec.extend_from_slice(bytes);
110        vec.push(b'<');
111        vec
112    })
113}
114
115fn compute_extensions(extensions: &[DataForm]) -> Vec<u8> {
116    compute_items(extensions, |extension| {
117        let mut bytes = vec!();
118        // TODO: maybe handle the error case?
119        if let Some(ref form_type) = extension.form_type {
120            bytes.extend_from_slice(form_type.as_bytes());
121        }
122        bytes.push(b'<');
123        for field in extension.fields.clone() {
124            if field.var == "FORM_TYPE" {
125                continue;
126            }
127            bytes.append(&mut compute_item(&field.var));
128            bytes.append(&mut compute_items(&field.values,
129                                            |value| compute_item(value)));
130        }
131        bytes
132    })
133}
134
135/// Applies the caps algorithm on the provided disco#info result, to generate
136/// the hash input.
137///
138/// Warning: This protocol is insecure, you may want to switch to
139/// [ecaps2](../ecaps2/index.html) instead, see [this
140/// email](https://mail.jabber.org/pipermail/security/2009-July/000812.html).
141pub fn compute_disco(disco: &DiscoInfoResult) -> Vec<u8> {
142    let identities_string = compute_identities(&disco.identities);
143    let features_string = compute_features(&disco.features);
144    let extensions_string = compute_extensions(&disco.extensions);
145
146    let mut final_string = vec!();
147    final_string.extend(identities_string);
148    final_string.extend(features_string);
149    final_string.extend(extensions_string);
150    final_string
151}
152
153fn get_hash_vec(hash: &[u8]) -> Vec<u8> {
154    let mut vec = Vec::with_capacity(hash.len());
155    vec.extend_from_slice(hash);
156    vec
157}
158
159/// Hashes the result of [compute_disco()] with one of the supported [hash
160/// algorithms](../hashes/enum.Algo.html).
161pub fn hash_caps(data: &[u8], algo: Algo) -> Result<Hash, String> {
162    Ok(Hash {
163        hash: match algo {
164            Algo::Sha_1 => {
165                let hash = Sha1::digest(data);
166                get_hash_vec(hash.as_slice())
167            },
168            Algo::Sha_256 => {
169                let hash = Sha256::digest(data);
170                get_hash_vec(hash.as_slice())
171            },
172            Algo::Sha_512 => {
173                let hash = Sha512::digest(data);
174                get_hash_vec(hash.as_slice())
175            },
176            Algo::Sha3_256 => {
177                let hash = Sha3_256::digest(data);
178                get_hash_vec(hash.as_slice())
179            },
180            Algo::Sha3_512 => {
181                let hash = Sha3_512::digest(data);
182                get_hash_vec(hash.as_slice())
183            },
184            Algo::Blake2b_256 => {
185                let mut hasher = Blake2b::default();
186                hasher.input(data);
187                let mut buf: [u8; 32] = [0; 32];
188                let hash = hasher.variable_result(&mut buf).unwrap();
189                get_hash_vec(hash)
190            },
191            Algo::Blake2b_512 => {
192                let mut hasher = Blake2b::default();
193                hasher.input(data);
194                let mut buf: [u8; 64] = [0; 64];
195                let hash = hasher.variable_result(&mut buf).unwrap();
196                get_hash_vec(hash)
197            },
198            Algo::Unknown(algo) => return Err(format!("Unknown algorithm: {}.", algo)),
199        },
200        algo: algo,
201    })
202}
203
204/// Helper function to create the query for the disco#info corresponding to a
205/// caps hash.
206pub fn query_caps(caps: Caps) -> DiscoInfoQuery {
207    DiscoInfoQuery {
208        node: Some(format!("{}#{}", caps.node, base64::encode(&caps.hash.hash))),
209    }
210}
211
212#[cfg(test)]
213mod tests {
214    use super::*;
215    use caps;
216    use base64;
217
218    #[test]
219    fn test_parse() {
220        let elem: Element = "<c xmlns='http://jabber.org/protocol/caps' hash='sha-256' node='coucou' ver='K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4='/>".parse().unwrap();
221        let caps = Caps::try_from(elem).unwrap();
222        assert_eq!(caps.node, String::from("coucou"));
223        assert_eq!(caps.hash.algo, Algo::Sha_256);
224        assert_eq!(caps.hash.hash, base64::decode("K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=").unwrap());
225    }
226
227    #[test]
228    fn test_invalid_child() {
229        let elem: Element = "<c xmlns='http://jabber.org/protocol/caps'><hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>K1Njy3HZBThlo4moOD5gBGhn0U0oK7/CbfLlIUDi6o4=</hash></c>".parse().unwrap();
230        let error = Caps::try_from(elem).unwrap_err();
231        let message = match error {
232            Error::ParseError(string) => string,
233            _ => panic!(),
234        };
235        assert_eq!(message, "Unknown child in caps element.");
236    }
237
238    #[test]
239    fn test_simple() {
240        let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
241        let disco = DiscoInfoResult::try_from(elem).unwrap();
242        let caps = caps::compute_disco(&disco);
243        assert_eq!(caps.len(), 50);
244    }
245
246    #[test]
247    fn test_xep_5_2() {
248        let elem: Element = r#"
249<query xmlns='http://jabber.org/protocol/disco#info'
250       node='http://psi-im.org#q07IKJEyjvHSyhy//CH0CxmKi8w='>
251  <identity category='client' name='Exodus 0.9.1' type='pc'/>
252  <feature var='http://jabber.org/protocol/caps'/>
253  <feature var='http://jabber.org/protocol/disco#info'/>
254  <feature var='http://jabber.org/protocol/disco#items'/>
255  <feature var='http://jabber.org/protocol/muc'/>
256</query>
257"#.parse().unwrap();
258        
259        let data = b"client/pc//Exodus 0.9.1<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<";
260        let mut expected = Vec::with_capacity(data.len());
261        expected.extend_from_slice(data);
262        let disco = DiscoInfoResult::try_from(elem).unwrap();
263        let caps = caps::compute_disco(&disco);
264        assert_eq!(caps, expected);
265
266        let sha_1 = caps::hash_caps(&caps, Algo::Sha_1).unwrap();
267        assert_eq!(sha_1.hash, base64::decode("QgayPKawpkPSDYmwT/WM94uAlu0=").unwrap());
268    }
269
270    #[test]
271    fn test_xep_5_3() {
272        let elem: Element = r#"
273<query xmlns='http://jabber.org/protocol/disco#info'
274       node='http://psi-im.org#q07IKJEyjvHSyhy//CH0CxmKi8w='>
275  <identity xml:lang='en' category='client' name='Psi 0.11' type='pc'/>
276  <identity xml:lang='el' category='client' name='Ψ 0.11' type='pc'/>
277  <feature var='http://jabber.org/protocol/caps'/>
278  <feature var='http://jabber.org/protocol/disco#info'/>
279  <feature var='http://jabber.org/protocol/disco#items'/>
280  <feature var='http://jabber.org/protocol/muc'/>
281  <x xmlns='jabber:x:data' type='result'>
282    <field var='FORM_TYPE' type='hidden'>
283      <value>urn:xmpp:dataforms:softwareinfo</value>
284    </field>
285    <field var='ip_version'>
286      <value>ipv4</value>
287      <value>ipv6</value>
288    </field>
289    <field var='os'>
290      <value>Mac</value>
291    </field>
292    <field var='os_version'>
293      <value>10.5.1</value>
294    </field>
295    <field var='software'>
296      <value>Psi</value>
297    </field>
298    <field var='software_version'>
299      <value>0.11</value>
300    </field>
301  </x>
302</query>
303"#.parse().unwrap();
304        let data = b"client/pc/el/\xce\xa8 0.11<client/pc/en/Psi 0.11<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<urn:xmpp:dataforms:softwareinfo<ip_version<ipv4<ipv6<os<Mac<os_version<10.5.1<software<Psi<software_version<0.11<";
305        let mut expected = Vec::with_capacity(data.len());
306        expected.extend_from_slice(data);
307        let disco = DiscoInfoResult::try_from(elem).unwrap();
308        let caps = caps::compute_disco(&disco);
309        assert_eq!(caps, expected);
310
311        let sha_1 = caps::hash_caps(&caps, Algo::Sha_1).unwrap();
312        assert_eq!(sha_1.hash, base64::decode("q07IKJEyjvHSyhy//CH0CxmKi8w=").unwrap());
313    }
314}