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

Split repository traits, Preserve metadata hashes #274

Merged
merged 17 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 221 additions & 113 deletions src/client.rs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ pub fn hash_preference<'a>(
Err(Error::NoSupportedHashAlgorithm)
}

#[cfg(test)]
pub(crate) fn calculate_hash(data: &[u8], hash_alg: HashAlgorithm) -> HashValue {
let mut context = hash_alg.digest_context().unwrap();
context.update(data);
HashValue::new(context.finish().as_ref().to_vec())
}

/// Calculate the size and hash digest from a given `Read`.
pub fn calculate_hashes<R: Read>(
mut read: R,
Expand Down
51 changes: 50 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,39 @@ pub trait Metadata: Debug + PartialEq + Serialize + DeserializeOwned {
fn expires(&self) -> &DateTime<Utc>;
}

/// A piece of raw metadata with attached signatures.
/// Unverified raw metadata with attached signatures and type information identifying the
/// metadata's type and serialization format.
#[derive(Debug, Clone, PartialEq)]
pub struct RawSignedMetadata<D, M> {
bytes: Vec<u8>,
_marker: PhantomData<(D, M)>,
}

impl<D, M> RawSignedMetadata<D, M>
where
D: DataInterchange,
M: Metadata,
{
/// Create a new [`RawSignedMetadata`] using the provided `bytes`.
pub fn new(bytes: Vec<u8>) -> Self {
Self {
bytes,
_marker: PhantomData,
}
}

/// Access this metadata's inner raw bytes.
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}

/// Parse this metadata.
pub fn parse(&self) -> Result<SignedMetadata<D, M>> {
D::from_slice(&self.bytes)
}
}

/// A piece of metadata with attached signatures.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignedMetadata<D, M> {
signatures: Vec<Signature>,
Expand Down Expand Up @@ -280,6 +312,23 @@ where
})
}

/// Serialize this metadata to canonical bytes suitable for serialization. Note that this
/// method is only intended to serialize signed metadata generated by this crate, not to
/// re-serialize metadata that was originally obtained from a remote source.
///
/// TUF metadata hashes are on the raw bytes of the metadata, so it is not guaranteed that the
/// hash of the returned bytes will match a hash included in, for example, a snapshot metadata
/// file, as:
/// * Parsing metadata removes unknown fields, which would not be included in the returned
/// bytes,
/// * DataInterchange implementations only guarantee the bytes are canonical for the purpose of
/// a signature. Metadata obtained from a remote source may have included different whitespace
/// or ordered fields in a way that is not preserved when parsing that metadata.
pub fn to_raw(&self) -> Result<RawSignedMetadata<D, M>> {
let bytes = D::canonicalize(&D::serialize(self)?)?;
Ok(RawSignedMetadata::new(bytes))
}

/// Append a signature to this signed metadata. Will overwrite signature by keys with the same
/// ID.
///
Expand Down
Loading