Skip to content

Commit

Permalink
Made more fields public + added get_canonicalized_header function to …
Browse files Browse the repository at this point in the history
…authenticated_message (#12)

* made authenticated message members pub

* made more things public + added print

* more pub(crate)s -> pub

* fix print on headers midway thru verify dkim

* fixed print again

* debug signature

* print canonicalized header?

* finished debugging and adding get_canonicalized_header function

* reduced pub variables, cleared prints, cleaned code

* fixed comments on function
  • Loading branch information
Divide-By-0 committed May 15, 2024
1 parent 9e5d055 commit 25f86bb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/common/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub(crate) enum AuthenticatedHeader<'x> {

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header<'x, T> {
pub(crate) name: &'x [u8],
pub(crate) value: &'x [u8],
pub(crate) header: T,
pub name: &'x [u8],
pub value: &'x [u8],
pub header: T,
}

impl<'x> HeaderParser<'x> {
Expand Down
2 changes: 1 addition & 1 deletion src/dkim/canonicalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Canonicalization {
}

impl Signature {
pub(crate) fn canonicalize<'x>(
pub fn canonicalize<'x>(
&self,
mut message: impl HeaderStream<'x>,
) -> (usize, CanonicalHeaders<'x>, Vec<String>, CanonicalBody<'x>) {
Expand Down
4 changes: 2 additions & 2 deletions src/dkim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub enum Canonicalization {
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct DkimSigner<T: SigningKey, State = NeedDomain> {
_state: std::marker::PhantomData<State>,
pub(crate) key: T,
pub(crate) template: Signature,
pub key: T,
pub template: Signature,
}

pub struct NeedDomain;
Expand Down
30 changes: 30 additions & 0 deletions src/dkim/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,36 @@ impl Resolver {
}

impl<'x> AuthenticatedMessage<'x> {
pub async fn get_canonicalized_header(&self) -> Result<Vec<u8>, Error> {
// Based on verify_dkim_ function
// Iterate through possible DKIM headers
let mut data = Vec::with_capacity(256);
for header in &self.dkim_headers {
// Ensure signature is not obviously invalid
let signature = match &header.header {
Ok(signature) => {
if signature.x == 0 || (signature.x > signature.t) {
signature
} else {
continue;
}
}
Err(_err) => {
continue;
}
};

// Get pre-hashed but canonically ordered headers, who's hash is signed
let dkim_hdr_value = header.value.strip_signature();
let headers = self.signed_headers(&signature.h, header.name, &dkim_hdr_value);
signature.ch.canonicalize_headers(headers, &mut data);

return Ok(data);
}
// Return not ok
Err(Error::FailedBodyHashMatch)
}

pub fn signed_headers<'z: 'x>(
&'z self,
headers: &'x [String],
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,18 @@ pub struct MX {

#[derive(Debug, Clone)]
pub struct AuthenticatedMessage<'x> {
pub(crate) headers: Vec<(&'x [u8], &'x [u8])>,
pub(crate) from: Vec<String>,
pub(crate) raw_message: &'x [u8],
pub(crate) body_offset: usize,
pub(crate) body_hashes: Vec<(Canonicalization, HashAlgorithm, u64, Vec<u8>)>,
pub(crate) dkim_headers: Vec<Header<'x, crate::Result<dkim::Signature>>>,
pub(crate) ams_headers: Vec<Header<'x, crate::Result<arc::Signature>>>,
pub(crate) as_headers: Vec<Header<'x, crate::Result<arc::Seal>>>,
pub(crate) aar_headers: Vec<Header<'x, crate::Result<arc::Results>>>,
pub(crate) received_headers_count: usize,
pub(crate) date_header_present: bool,
pub(crate) message_id_header_present: bool,
pub headers: Vec<(&'x [u8], &'x [u8])>,
pub from: Vec<String>,
pub raw_message: &'x [u8],
pub body_offset: usize,
pub body_hashes: Vec<(Canonicalization, HashAlgorithm, u64, Vec<u8>)>,
pub dkim_headers: Vec<Header<'x, crate::Result<dkim::Signature>>>,
pub ams_headers: Vec<Header<'x, crate::Result<arc::Signature>>>,
pub as_headers: Vec<Header<'x, crate::Result<arc::Seal>>>,
pub aar_headers: Vec<Header<'x, crate::Result<arc::Results>>>,
pub received_headers_count: usize,
pub date_header_present: bool,
pub message_id_header_present: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down

0 comments on commit 25f86bb

Please sign in to comment.