2017-04-29 21:14:34 +00:00
|
|
|
|
// Copyright (c) 2017 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-01-13 11:39:51 +00:00
|
|
|
|
use crate::util::error::Error;
|
|
|
|
|
use crate::util::helpers::Base64;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use minidom::IntoAttributeValue;
|
2019-01-25 01:49:56 +00:00
|
|
|
|
use std::num::ParseIntError;
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-05-24 23:30:29 +00:00
|
|
|
|
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// List of the algorithms we support, or Unknown.
|
2017-05-18 22:09:29 +00:00
|
|
|
|
#[allow(non_camel_case_types)]
|
2017-05-29 04:14:49 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2017-05-18 22:09:29 +00:00
|
|
|
|
pub enum Algo {
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// The Secure Hash Algorithm 1, with known vulnerabilities, do not use it.
|
|
|
|
|
///
|
|
|
|
|
/// See https://tools.ietf.org/html/rfc3174
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Sha_1,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The Secure Hash Algorithm 2, in its 256-bit version.
|
|
|
|
|
///
|
|
|
|
|
/// See https://tools.ietf.org/html/rfc6234
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Sha_256,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The Secure Hash Algorithm 2, in its 512-bit version.
|
|
|
|
|
///
|
|
|
|
|
/// See https://tools.ietf.org/html/rfc6234
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Sha_512,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The Secure Hash Algorithm 3, based on Keccak, in its 256-bit version.
|
|
|
|
|
///
|
|
|
|
|
/// See https://keccak.team/files/Keccak-submission-3.pdf
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Sha3_256,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The Secure Hash Algorithm 3, based on Keccak, in its 512-bit version.
|
|
|
|
|
///
|
|
|
|
|
/// See https://keccak.team/files/Keccak-submission-3.pdf
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Sha3_512,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The BLAKE2 hash algorithm, for a 256-bit output.
|
|
|
|
|
///
|
|
|
|
|
/// See https://tools.ietf.org/html/rfc7693
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Blake2b_256,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// The BLAKE2 hash algorithm, for a 512-bit output.
|
|
|
|
|
///
|
|
|
|
|
/// See https://tools.ietf.org/html/rfc7693
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Blake2b_512,
|
2018-08-08 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
/// An unknown hash not in this list, you can probably reject it.
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Unknown(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for Algo {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Algo, Error> {
|
|
|
|
|
Ok(match s {
|
|
|
|
|
"" => return Err(Error::ParseError("'algo' argument can’t be empty.")),
|
|
|
|
|
|
|
|
|
|
"sha-1" => Algo::Sha_1,
|
|
|
|
|
"sha-256" => Algo::Sha_256,
|
|
|
|
|
"sha-512" => Algo::Sha_512,
|
|
|
|
|
"sha3-256" => Algo::Sha3_256,
|
|
|
|
|
"sha3-512" => Algo::Sha3_512,
|
|
|
|
|
"blake2b-256" => Algo::Blake2b_256,
|
|
|
|
|
"blake2b-512" => Algo::Blake2b_512,
|
|
|
|
|
value => Algo::Unknown(value.to_owned()),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 11:22:11 +00:00
|
|
|
|
impl From<Algo> for String {
|
|
|
|
|
fn from(algo: Algo) -> String {
|
|
|
|
|
String::from(match algo {
|
2017-05-18 22:09:29 +00:00
|
|
|
|
Algo::Sha_1 => "sha-1",
|
|
|
|
|
Algo::Sha_256 => "sha-256",
|
|
|
|
|
Algo::Sha_512 => "sha-512",
|
|
|
|
|
Algo::Sha3_256 => "sha3-256",
|
|
|
|
|
Algo::Sha3_512 => "sha3-512",
|
|
|
|
|
Algo::Blake2b_256 => "blake2b-256",
|
|
|
|
|
Algo::Blake2b_512 => "blake2b-512",
|
2017-05-27 11:22:11 +00:00
|
|
|
|
Algo::Unknown(text) => return text,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoAttributeValue for Algo {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(String::from(self))
|
2017-05-18 22:09:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 17:07:07 +00:00
|
|
|
|
generate_element!(
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// This element represents a hash of some data, defined by the hash
|
|
|
|
|
/// algorithm used and the computed value.
|
2017-11-23 15:52:06 +00:00
|
|
|
|
#[derive(PartialEq)]
|
2018-05-14 14:30:28 +00:00
|
|
|
|
Hash, "hash", HASHES,
|
2018-08-02 17:07:07 +00:00
|
|
|
|
attributes: [
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// The algorithm used to create this hash.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
algo: Required<Algo> = "algo"
|
2017-11-23 15:52:06 +00:00
|
|
|
|
],
|
2018-08-02 17:07:07 +00:00
|
|
|
|
text: (
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// The hash value, as a vector of bytes.
|
2018-08-02 17:07:07 +00:00
|
|
|
|
hash: Base64<Vec<u8>>
|
|
|
|
|
)
|
2017-11-23 15:52:06 +00:00
|
|
|
|
);
|
2017-04-23 17:36:12 +00:00
|
|
|
|
|
2018-05-04 17:10:04 +00:00
|
|
|
|
impl Hash {
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// Creates a [Hash] element with the given algo and data.
|
2018-05-04 17:10:04 +00:00
|
|
|
|
pub fn new(algo: Algo, hash: Vec<u8>) -> Hash {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
Hash { algo, hash }
|
2018-05-04 17:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 17:02:03 +00:00
|
|
|
|
/// Like [new](#method.new) but takes base64-encoded data before decoding
|
|
|
|
|
/// it.
|
2018-05-04 17:10:04 +00:00
|
|
|
|
pub fn from_base64(algo: Algo, hash: &str) -> Result<Hash, Error> {
|
|
|
|
|
Ok(Hash::new(algo, base64::decode(hash)?))
|
|
|
|
|
}
|
2019-07-25 15:51:05 +00:00
|
|
|
|
|
2019-07-30 20:14:06 +00:00
|
|
|
|
/// Like [new](#method.new) but takes hex-encoded data before decoding it.
|
|
|
|
|
pub fn from_hex(algo: Algo, hex: &str) -> Result<Hash, ParseIntError> {
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
|
for i in 0..hex.len() / 2 {
|
|
|
|
|
let byte = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16)?;
|
|
|
|
|
bytes.push(byte);
|
|
|
|
|
}
|
|
|
|
|
Ok(Hash::new(algo, bytes))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:51:05 +00:00
|
|
|
|
/// Formats this hash into base64.
|
|
|
|
|
pub fn to_base64(&self) -> String {
|
|
|
|
|
base64::encode(&self.hash[..])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Formats this hash into hexadecimal.
|
|
|
|
|
pub fn to_hex(&self) -> String {
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
|
for byte in self.hash.iter() {
|
|
|
|
|
bytes.push(format!("{:02x}", byte));
|
|
|
|
|
}
|
|
|
|
|
bytes.join("")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Formats this hash into colon-separated hexadecimal.
|
|
|
|
|
pub fn to_colon_hex(&self) -> String {
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
|
for byte in self.hash.iter() {
|
|
|
|
|
bytes.push(format!("{:02x}", byte));
|
|
|
|
|
}
|
|
|
|
|
bytes.join(":")
|
|
|
|
|
}
|
2018-05-04 17:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-25 01:49:56 +00:00
|
|
|
|
/// Helper for parsing and serialising a SHA-1 attribute.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub struct Sha1HexAttribute(Hash);
|
|
|
|
|
|
|
|
|
|
impl FromStr for Sha1HexAttribute {
|
|
|
|
|
type Err = ParseIntError;
|
|
|
|
|
|
|
|
|
|
fn from_str(hex: &str) -> Result<Self, Self::Err> {
|
2019-07-30 20:14:06 +00:00
|
|
|
|
let hash = Hash::from_hex(Algo::Sha_1, hex)?;
|
|
|
|
|
Ok(Sha1HexAttribute(hash))
|
2019-01-25 01:49:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoAttributeValue for Sha1HexAttribute {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2019-07-25 15:51:05 +00:00
|
|
|
|
Some(self.to_hex())
|
2019-01-25 01:49:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DerefMut for Sha1HexAttribute {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Deref for Sha1HexAttribute {
|
|
|
|
|
type Target = Hash;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 03:21:16 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 19:46:11 +00:00
|
|
|
|
use super::*;
|
2018-05-14 14:17:21 +00:00
|
|
|
|
use minidom::Element;
|
2019-04-12 08:58:42 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2017-04-21 03:21:16 +00:00
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Algo, 16);
|
|
|
|
|
assert_size!(Hash, 28);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Algo, 32);
|
|
|
|
|
assert_size!(Hash, 56);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 03:21:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let hash = Hash::try_from(elem).unwrap();
|
2017-05-18 22:09:29 +00:00
|
|
|
|
assert_eq!(hash.algo, Algo::Sha_256);
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
hash.hash,
|
|
|
|
|
base64::decode("2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=").unwrap()
|
|
|
|
|
);
|
2017-04-21 03:21:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:51:05 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn value_serialisation() {
|
|
|
|
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
|
|
|
|
|
let hash = Hash::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(hash.to_base64(), "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
|
|
|
|
|
assert_eq!(hash.to_hex(), "d976ab9b04e53710c0324bf29a5a17dd2e7e55bca536b26dfe5e50c8f6be6285");
|
|
|
|
|
assert_eq!(hash.to_colon_hex(), "d9:76:ab:9b:04:e5:37:10:c0:32:4b:f2:9a:5a:17:dd:2e:7e:55:bc:a5:36:b2:6d:fe:5e:50:c8:f6:be:62:85");
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 03:21:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_unknown() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Hash::try_from(elem).unwrap_err();
|
2017-04-21 03:21:16 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "This is not a hash element.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Hash::try_from(elem).unwrap_err();
|
2017-04-21 03:21:16 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown child in hash element.");
|
|
|
|
|
}
|
|
|
|
|
}
|