Skip to content

Commit 25f86bb

Browse files
authored
Made more fields public + added get_canonicalized_header function to 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
1 parent 9e5d055 commit 25f86bb

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

src/common/headers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ pub(crate) enum AuthenticatedHeader<'x> {
6060

6161
#[derive(Debug, Clone, PartialEq, Eq)]
6262
pub struct Header<'x, T> {
63-
pub(crate) name: &'x [u8],
64-
pub(crate) value: &'x [u8],
65-
pub(crate) header: T,
63+
pub name: &'x [u8],
64+
pub value: &'x [u8],
65+
pub header: T,
6666
}
6767

6868
impl<'x> HeaderParser<'x> {

src/dkim/canonicalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl Canonicalization {
158158
}
159159

160160
impl Signature {
161-
pub(crate) fn canonicalize<'x>(
161+
pub fn canonicalize<'x>(
162162
&self,
163163
mut message: impl HeaderStream<'x>,
164164
) -> (usize, CanonicalHeaders<'x>, Vec<String>, CanonicalBody<'x>) {

src/dkim/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub enum Canonicalization {
3636
#[derive(Debug, PartialEq, Eq, Clone, Default)]
3737
pub struct DkimSigner<T: SigningKey, State = NeedDomain> {
3838
_state: std::marker::PhantomData<State>,
39-
pub(crate) key: T,
40-
pub(crate) template: Signature,
39+
pub key: T,
40+
pub template: Signature,
4141
}
4242

4343
pub struct NeedDomain;

src/dkim/verify.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,36 @@ impl Resolver {
240240
}
241241

242242
impl<'x> AuthenticatedMessage<'x> {
243+
pub async fn get_canonicalized_header(&self) -> Result<Vec<u8>, Error> {
244+
// Based on verify_dkim_ function
245+
// Iterate through possible DKIM headers
246+
let mut data = Vec::with_capacity(256);
247+
for header in &self.dkim_headers {
248+
// Ensure signature is not obviously invalid
249+
let signature = match &header.header {
250+
Ok(signature) => {
251+
if signature.x == 0 || (signature.x > signature.t) {
252+
signature
253+
} else {
254+
continue;
255+
}
256+
}
257+
Err(_err) => {
258+
continue;
259+
}
260+
};
261+
262+
// Get pre-hashed but canonically ordered headers, who's hash is signed
263+
let dkim_hdr_value = header.value.strip_signature();
264+
let headers = self.signed_headers(&signature.h, header.name, &dkim_hdr_value);
265+
signature.ch.canonicalize_headers(headers, &mut data);
266+
267+
return Ok(data);
268+
}
269+
// Return not ok
270+
Err(Error::FailedBodyHashMatch)
271+
}
272+
243273
pub fn signed_headers<'z: 'x>(
244274
&'z self,
245275
headers: &'x [String],

src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,18 @@ pub struct MX {
335335

336336
#[derive(Debug, Clone)]
337337
pub struct AuthenticatedMessage<'x> {
338-
pub(crate) headers: Vec<(&'x [u8], &'x [u8])>,
339-
pub(crate) from: Vec<String>,
340-
pub(crate) raw_message: &'x [u8],
341-
pub(crate) body_offset: usize,
342-
pub(crate) body_hashes: Vec<(Canonicalization, HashAlgorithm, u64, Vec<u8>)>,
343-
pub(crate) dkim_headers: Vec<Header<'x, crate::Result<dkim::Signature>>>,
344-
pub(crate) ams_headers: Vec<Header<'x, crate::Result<arc::Signature>>>,
345-
pub(crate) as_headers: Vec<Header<'x, crate::Result<arc::Seal>>>,
346-
pub(crate) aar_headers: Vec<Header<'x, crate::Result<arc::Results>>>,
347-
pub(crate) received_headers_count: usize,
348-
pub(crate) date_header_present: bool,
349-
pub(crate) message_id_header_present: bool,
338+
pub headers: Vec<(&'x [u8], &'x [u8])>,
339+
pub from: Vec<String>,
340+
pub raw_message: &'x [u8],
341+
pub body_offset: usize,
342+
pub body_hashes: Vec<(Canonicalization, HashAlgorithm, u64, Vec<u8>)>,
343+
pub dkim_headers: Vec<Header<'x, crate::Result<dkim::Signature>>>,
344+
pub ams_headers: Vec<Header<'x, crate::Result<arc::Signature>>>,
345+
pub as_headers: Vec<Header<'x, crate::Result<arc::Seal>>>,
346+
pub aar_headers: Vec<Header<'x, crate::Result<arc::Results>>>,
347+
pub received_headers_count: usize,
348+
pub date_header_present: bool,
349+
pub message_id_header_present: bool,
350350
}
351351

352352
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)