Skip to content
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
6 changes: 3 additions & 3 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ impl Manifest {
Ok((path, manifest))
}

pub(crate) fn message(&self, include_time: bool) -> Result<Message> {
pub(crate) fn message(&self, timestamp: bool) -> Result<Message> {
Ok(Message {
fingerprint: self.fingerprint(),
time: include_time.then(now).transpose()?,
timestamp: timestamp.then(now).transpose()?,
})
}

Expand All @@ -99,7 +99,7 @@ impl Manifest {
keychain: &Keychain,
key: &KeyName,
) -> Result {
let message = self.message(options.time)?;
let message = self.message(options.timestamp)?;

let signature = keychain.sign(key, &message)?;

Expand Down
6 changes: 3 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Message {
pub fingerprint: Fingerprint,
pub time: Option<u64>,
pub timestamp: Option<u64>,
}

impl Message {
Expand All @@ -12,8 +12,8 @@ impl Message {

serializer.field(0, self.fingerprint.as_bytes());

if let Some(time) = self.time {
serializer.field(1, &time.to_le_bytes());
if let Some(timestamp) = self.timestamp {
serializer.field(1, &timestamp.to_le_bytes());
}

serializer.finalize()
Expand Down
2 changes: 1 addition & 1 deletion src/sign_options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub(crate) struct SignOptions {
pub(crate) time: bool,
pub(crate) timestamp: bool,
}
24 changes: 12 additions & 12 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

const TIME: Fe32 = Fe32::T;
const TIMESTAMP: Fe32 = Fe32::T;

#[derive(Clone, Debug, DeserializeFromStr, Eq, PartialEq, SerializeDisplay)]
pub struct Signature {
Expand Down Expand Up @@ -60,9 +60,9 @@ impl Display for Signature {
encoder.bytes(&self.public_key.inner().to_bytes());
encoder.bytes(self.message.fingerprint.as_bytes());
encoder.bytes(&self.signature.to_bytes());
if let Some(time) = self.message.time {
encoder.fe(TIME);
encoder.bytes(&time.to_le_bytes());
if let Some(timestamp) = self.message.timestamp {
encoder.fe(TIMESTAMP);
encoder.bytes(&timestamp.to_le_bytes());
}
write!(f, "{encoder}")
}
Expand All @@ -77,17 +77,17 @@ impl FromStr for Signature {
let fingerprint = decoder.byte_array()?;
let signature = decoder.byte_array()?;

let time = match decoder.fe() {
let timestamp = match decoder.fe() {
None => None,
Some(TIME) => Some(u64::from_le_bytes(decoder.byte_array()?)),
Some(TIMESTAMP) => Some(u64::from_le_bytes(decoder.byte_array()?)),
Some(tag) => return Err(signature_error::Field { tag }.build()),
};

decoder.done()?;
Ok(Self {
message: Message {
fingerprint: Fingerprint::from_bytes(fingerprint),
time,
timestamp,
},
signature: ed25519_dalek::Signature::from_bytes(&signature),
public_key: PublicKey::from_bytes(public_key).context(signature_error::PublicKey)?,
Expand Down Expand Up @@ -117,7 +117,7 @@ mod tests {
let fingerprint = test::FINGERPRINT.parse::<Fingerprint>().unwrap();
let message = Message {
fingerprint,
time: Some(1000),
timestamp: Some(1000),
};
let mut signature = private_key.sign(&message);
signature.message.fingerprint = Fingerprint::from_bytes(default());
Expand All @@ -133,10 +133,10 @@ mod tests {
let fingerprint = test::FINGERPRINT.parse::<Fingerprint>().unwrap();
let message = Message {
fingerprint,
time: Some(1000),
timestamp: Some(1000),
};
let mut signature = private_key.sign(&message);
signature.message.time = Some(2000);
signature.message.timestamp = Some(2000);
assert_matches!(
signature.verify(fingerprint).unwrap_err(),
Error::SignatureInvalid { .. },
Expand All @@ -149,10 +149,10 @@ mod tests {
let fingerprint = test::FINGERPRINT.parse::<Fingerprint>().unwrap();
let message = Message {
fingerprint,
time: Some(1000),
timestamp: Some(1000),
};
let mut signature = private_key.sign(&message);
signature.message.time = None;
signature.message.timestamp = None;
assert_matches!(
signature.verify(fingerprint).unwrap_err(),
Error::SignatureInvalid { .. },
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MANIFEST_PATH_HELP: &str = "\
`filepack.json`, or omitted, in which case manifest named `filepack.json` in the current \
directory is loaded.";

const TIME_HELP: &str = "Include current time in note";
const TIMESTAMP_HELP: &str = "Include current time in signature";

#[derive(Parser)]
#[command(
Expand Down
12 changes: 9 additions & 3 deletions src/subcommand/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub(crate) struct Create {
root: Option<Utf8PathBuf>,
#[arg(help = "Sign manifest", long)]
sign: bool,
#[arg(help = TIME_HELP, long)]
time: bool,
#[arg(help = TIMESTAMP_HELP, long)]
timestamp: bool,
}

impl Create {
Expand Down Expand Up @@ -160,7 +160,13 @@ impl Create {

if self.sign {
let keychain = Keychain::load(&options)?;
manifest.sign(SignOptions { time: self.time }, &keychain, &self.key)?;
manifest.sign(
SignOptions {
timestamp: self.timestamp,
},
&keychain,
&self.key,
)?;
}

manifest.save(&manifest_path)?;
Expand Down
12 changes: 9 additions & 3 deletions src/subcommand/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) struct Sign {
key: KeyName,
#[arg(help = MANIFEST_PATH_HELP)]
path: Option<Utf8PathBuf>,
#[arg(help = TIME_HELP, long)]
time: bool,
#[arg(help = TIMESTAMP_HELP, long)]
timestamp: bool,
}

impl Sign {
Expand All @@ -16,7 +16,13 @@ impl Sign {

let keychain = Keychain::load(&options)?;

manifest.sign(SignOptions { time: self.time }, &keychain, &self.key)?;
manifest.sign(
SignOptions {
timestamp: self.timestamp,
},
&keychain,
&self.key,
)?;

manifest.save(&path)?;

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Signatures {
.iter()
.map(|signature| Output {
public_key: signature.public_key(),
timestamp: signature.message().time,
timestamp: signature.message().timestamp,
})
.collect::<Vec<Output>>();

Expand Down
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn signature_matches() {
let private_key = PRIVATE_KEY.parse::<PrivateKey>().unwrap();
let message = Message {
fingerprint: FINGERPRINT.parse().unwrap(),
time: None,
timestamp: None,
};
let signature = private_key.sign(&message);
assert_eq!(signature.to_string(), SIGNATURE);
Expand Down
10 changes: 5 additions & 5 deletions tests/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn sign_creates_valid_signature() {
let signature = manifest.signatures.first().unwrap();

assert_eq!(signature.public_key(), public_key);
assert!(signature.message().time.is_none());
assert!(signature.message().timestamp.is_none());
}

#[test]
Expand Down Expand Up @@ -285,14 +285,14 @@ fn sign_with_named_key() {
}

#[test]
fn sign_with_time() {
fn sign_with_timestamp() {
use std::time::{SystemTime, UNIX_EPOCH};

let test = Test::new()
.arg("keygen")
.success()
.touch("foo/bar")
.args(["create", "--sign", "--time", "foo"])
.args(["create", "--sign", "--timestamp", "foo"])
.success()
.args(["verify", "foo"])
.stderr("successfully verified 1 file totaling 0 bytes with 1 signature\n")
Expand All @@ -308,13 +308,13 @@ fn sign_with_time() {
let signature = manifest.signatures.first().unwrap();
assert_eq!(signature.public_key(), public_key,);

let time = signature.message().time.unwrap();
let timestamp = signature.message().timestamp.unwrap();
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let one_minute_ago = now - 60;
assert!(time >= one_minute_ago && time <= now);
assert!(timestamp >= one_minute_ago && timestamp <= now);
}

#[test]
Expand Down
14 changes: 10 additions & 4 deletions tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ fn updates_manifest_with_signature() {
.first()
.unwrap()
.message()
.time
.timestamp
.is_none()
);
}

#[test]
fn with_time() {
fn with_timestamp() {
let test = Test::new()
.arg("keygen")
.success()
Expand All @@ -174,7 +174,7 @@ fn with_time() {
let public_key = test.read("keychain/master.public");

let test = test
.args(["sign", "--time", "foo/filepack.json"])
.args(["sign", "--timestamp", "foo/filepack.json"])
.success()
.args(["verify", "foo", "--key", &public_key])
.stderr("successfully verified 1 file totaling 0 bytes with 1 signature\n")
Expand All @@ -183,7 +183,13 @@ fn with_time() {
let manifest_path = test.path().join("foo/filepack.json");
let manifest = Manifest::load(Some(&manifest_path)).unwrap();

let time = manifest.signatures.first().unwrap().message().time.unwrap();
let time = manifest
.signatures
.first()
.unwrap()
.message()
.timestamp
.unwrap();
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions tests/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn signature_with_time() {
.touch("foo/bar")
.args(["create", "foo"])
.success()
.args(["sign", "--time", "foo"])
.args(["sign", "--timestamp", "foo"])
.success();

let public_key = test.read("keychain/master.public");
Expand Down Expand Up @@ -91,7 +91,7 @@ fn tsv_format_with_time() {
.touch("foo/bar")
.args(["create", "foo"])
.success()
.args(["sign", "--time", "foo"])
.args(["sign", "--timestamp", "foo"])
.success();

let public_key = test.read("keychain/master.public");
Expand Down