1#[cfg(test)]
2mod integrity_test;
3
4use crate::attributes::*;
5use crate::checks::*;
6use crate::message::*;
7use shared::error::*;
8
9use md5::{Digest, Md5};
10use ring::hmac;
11use std::fmt;
12
13pub(crate) const CREDENTIALS_SEP: &str = ":";
15
16#[derive(Default, Clone)]
23pub struct MessageIntegrity(pub Vec<u8>);
24
25fn new_hmac(key: &[u8], message: &[u8]) -> Vec<u8> {
26 let mac = hmac::Key::new(hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY, key);
27 hmac::sign(&mac, message).as_ref().to_vec()
28}
29
30impl fmt::Display for MessageIntegrity {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 write!(f, "KEY: 0x{:x?}", self.0)
33 }
34}
35
36impl Setter for MessageIntegrity {
37 fn add_to(&self, m: &mut Message) -> Result<()> {
41 for a in &m.attributes.0 {
42 if a.typ == ATTR_FINGERPRINT {
45 return Err(Error::ErrFingerprintBeforeIntegrity);
46 }
47 }
48 let length = m.length;
52 m.length += (MESSAGE_INTEGRITY_SIZE + ATTRIBUTE_HEADER_SIZE) as u32;
54 m.write_length(); let v = new_hmac(&self.0, &m.raw); m.length = length; m.add(ATTR_MESSAGE_INTEGRITY, &v);
59
60 Ok(())
61 }
62}
63
64pub(crate) const MESSAGE_INTEGRITY_SIZE: usize = 20;
65
66impl MessageIntegrity {
67 pub fn new_long_term_integrity(username: String, realm: String, password: String) -> Self {
70 let s = [username, realm, password].join(CREDENTIALS_SEP);
71
72 let mut h = Md5::new();
73 h.update(s.as_bytes());
74
75 MessageIntegrity(h.finalize().as_slice().to_vec())
76 }
77
78 pub fn new_short_term_integrity(password: String) -> Self {
81 MessageIntegrity(password.as_bytes().to_vec())
82 }
83
84 pub fn check(&self, m: &mut Message) -> Result<()> {
88 let v = m.get(ATTR_MESSAGE_INTEGRITY)?;
89
90 let length = m.length as usize;
94 let mut after_integrity = false;
95 let mut size_reduced = 0;
96
97 for a in &m.attributes.0 {
98 if after_integrity {
99 size_reduced += nearest_padded_value_length(a.length as usize);
100 size_reduced += ATTRIBUTE_HEADER_SIZE;
101 }
102 if a.typ == ATTR_MESSAGE_INTEGRITY {
103 after_integrity = true;
104 }
105 }
106 m.length -= size_reduced as u32;
107 m.write_length();
108 let start_of_hmac = MESSAGE_HEADER_SIZE + m.length as usize
110 - (ATTRIBUTE_HEADER_SIZE + MESSAGE_INTEGRITY_SIZE);
111 let b = &m.raw[..start_of_hmac]; let expected = new_hmac(&self.0, b);
113 m.length = length as u32;
114 m.write_length(); check_hmac(&v, &expected)
116 }
117}