jingle_dtls_srtp.rs

 1// Copyright (c) 2019 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 crate::util::helpers::ColonSeparatedHex;
 8use crate::hashes::Algo;
 9
10generate_attribute!(
11    /// Indicates which of the end points should initiate the TCP connection establishment.
12    Setup, "setup", {
13        /// The endpoint will initiate an outgoing connection.
14        Active => "active",
15
16        /// The endpoint will accept an incoming connection.
17        Passive => "passive",
18
19        /// The endpoint is willing to accept an incoming connection or to initiate an outgoing
20        /// connection.
21        Actpass => "actpass",
22
23        /*
24        /// The endpoint does not want the connection to be established for the time being.
25        ///
26        /// Note that this value isn’t used, as per the XEP.
27        Holdconn => "holdconn",
28        */
29    }
30);
31
32// TODO: use a hashes::Hash instead of two different fields here.
33generate_element!(
34    /// Fingerprint of the key used for a DTLS handshake.
35    Fingerprint, "fingerprint", JINGLE_DTLS,
36    attributes: [
37        /// The hash algorithm used for this fingerprint.
38        hash: Required<Algo> = "hash",
39
40        /// Indicates which of the end points should initiate the TCP connection establishment.
41        setup: Required<Setup> = "setup"
42    ],
43    text: (
44        /// Hash value of this fingerprint.
45        value: ColonSeparatedHex<Vec<u8>>
46    )
47);
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use minidom::Element;
53    use std::convert::TryFrom;
54
55    #[cfg(target_pointer_width = "32")]
56    #[test]
57    fn test_size() {
58        assert_size!(Setup, 1);
59        assert_size!(Fingerprint, 32);
60    }
61
62    #[cfg(target_pointer_width = "64")]
63    #[test]
64    fn test_size() {
65        assert_size!(Setup, 1);
66        assert_size!(Fingerprint, 64);
67    }
68
69    #[test]
70    fn test_ex1() {
71        let elem: Element = "<fingerprint xmlns='urn:xmpp:jingle:apps:dtls:0' hash='sha-256' setup='actpass'>02:1A:CC:54:27:AB:EB:9C:53:3F:3E:4B:65:2E:7D:46:3F:54:42:CD:54:F1:7A:03:A2:7D:F9:B0:7F:46:19:B2</fingerprint>"
72                .parse()
73                .unwrap();
74        let fingerprint = Fingerprint::try_from(elem).unwrap();
75        assert_eq!(fingerprint.setup, Setup::Actpass);
76        assert_eq!(fingerprint.hash, Algo::Sha_256);
77        assert_eq!(fingerprint.value, [2, 26, 204, 84, 39, 171, 235, 156, 83, 63, 62, 75, 101, 46, 125, 70, 63, 84, 66, 205, 84, 241, 122, 3, 162, 125, 249, 176, 127, 70, 25, 178]);
78    }
79}