Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made more fields public + added get_canonicalized_header function to authenticated_message #12

Merged
merged 10 commits into from
May 15, 2024
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 @@ -151,7 +151,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 @@ -33,8 +33,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 @@ -331,18 +331,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