2019-02-28 12:32:52 +00:00
|
|
|
|
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use crate::hashes::{Algo, Hash};
|
2024-01-06 18:14:37 +00:00
|
|
|
|
use crate::util::text_node_codecs::{Codec, ColonSeparatedHex};
|
2024-06-21 14:27:43 +00:00
|
|
|
|
use xso::error::Error;
|
2019-02-28 12:32:52 +00:00
|
|
|
|
|
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// Indicates which of the end points should initiate the TCP connection establishment.
|
|
|
|
|
Setup, "setup", {
|
|
|
|
|
/// The endpoint will initiate an outgoing connection.
|
|
|
|
|
Active => "active",
|
|
|
|
|
|
|
|
|
|
/// The endpoint will accept an incoming connection.
|
|
|
|
|
Passive => "passive",
|
|
|
|
|
|
|
|
|
|
/// The endpoint is willing to accept an incoming connection or to initiate an outgoing
|
|
|
|
|
/// connection.
|
|
|
|
|
Actpass => "actpass",
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/// The endpoint does not want the connection to be established for the time being.
|
|
|
|
|
///
|
|
|
|
|
/// Note that this value isn’t used, as per the XEP.
|
|
|
|
|
Holdconn => "holdconn",
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: use a hashes::Hash instead of two different fields here.
|
|
|
|
|
generate_element!(
|
|
|
|
|
/// Fingerprint of the key used for a DTLS handshake.
|
|
|
|
|
Fingerprint, "fingerprint", JINGLE_DTLS,
|
|
|
|
|
attributes: [
|
|
|
|
|
/// The hash algorithm used for this fingerprint.
|
|
|
|
|
hash: Required<Algo> = "hash",
|
|
|
|
|
|
|
|
|
|
/// Indicates which of the end points should initiate the TCP connection establishment.
|
|
|
|
|
setup: Required<Setup> = "setup"
|
|
|
|
|
],
|
|
|
|
|
text: (
|
|
|
|
|
/// Hash value of this fingerprint.
|
2024-01-06 18:14:37 +00:00
|
|
|
|
value: ColonSeparatedHex
|
2019-02-28 12:32:52 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2019-10-12 15:11:34 +00:00
|
|
|
|
impl Fingerprint {
|
|
|
|
|
/// Create a new Fingerprint from a Setup and a Hash.
|
|
|
|
|
pub fn from_hash(setup: Setup, hash: Hash) -> Fingerprint {
|
|
|
|
|
Fingerprint {
|
|
|
|
|
hash: hash.algo,
|
|
|
|
|
setup,
|
|
|
|
|
value: hash.hash,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a new Fingerprint from a Setup and parsing the hash.
|
2019-10-22 23:32:41 +00:00
|
|
|
|
pub fn from_colon_separated_hex(
|
|
|
|
|
setup: Setup,
|
|
|
|
|
algo: &str,
|
|
|
|
|
hash: &str,
|
|
|
|
|
) -> Result<Fingerprint, Error> {
|
2019-10-12 15:11:34 +00:00
|
|
|
|
let algo = algo.parse()?;
|
2024-06-21 14:27:43 +00:00
|
|
|
|
let hash = Hash::from_colon_separated_hex(algo, hash).map_err(Error::text_parse_error)?;
|
2019-10-12 15:11:34 +00:00
|
|
|
|
Ok(Fingerprint::from_hash(setup, hash))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 16:48:37 +00:00
|
|
|
|
#[cfg(test)]
|
2019-02-28 12:32:52 +00:00
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2024-07-24 18:28:22 +00:00
|
|
|
|
use minidom::Element;
|
2019-02-28 12:32:52 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Setup, 1);
|
2024-02-27 11:27:31 +00:00
|
|
|
|
assert_size!(Fingerprint, 28);
|
2019-02-28 12:32:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Setup, 1);
|
2024-02-27 11:27:31 +00:00
|
|
|
|
assert_size!(Fingerprint, 56);
|
2019-02-28 12:32:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_ex1() {
|
|
|
|
|
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>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let fingerprint = Fingerprint::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(fingerprint.setup, Setup::Actpass);
|
|
|
|
|
assert_eq!(fingerprint.hash, Algo::Sha_256);
|
2019-10-22 23:32:41 +00:00
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
);
|
2019-02-28 12:32:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|