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

Return signing key in SignedEnvelope.payload #2522

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions core/src/peer_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ impl PeerRecord {
pub fn from_signed_envelope(envelope: SignedEnvelope) -> Result<Self, FromEnvelopeError> {
use prost::Message;

let payload = envelope.payload(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?;
let (payload, signing_key) =
envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?;
let record = peer_record_proto::PeerRecord::decode(payload)?;

let peer_id = PeerId::from_bytes(&record.peer_id)?;

if peer_id != envelope.key.to_peer_id() {
if peer_id != signing_key.to_peer_id() {
return Err(FromEnvelopeError::MismatchedSignature);
}

Expand Down
20 changes: 12 additions & 8 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use unsigned_varint::encode::usize_buffer;
/// For more details see libp2p RFC0002: <https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md>
#[derive(Debug, Clone, PartialEq)]
pub struct SignedEnvelope {
pub(crate) key: PublicKey,
key: PublicKey,
payload_type: Vec<u8>,
payload: Vec<u8>,
signature: Vec<u8>,
Expand Down Expand Up @@ -44,15 +44,19 @@ impl SignedEnvelope {
self.key.verify(&buffer, &self.signature)
}

/// Extract the payload of this [`SignedEnvelope`].
/// Extract the payload and signing key of this [`SignedEnvelope`].
///
/// You must provide the correct domain-separation string and expected payload type in order to get the payload.
/// This guards against accidental mis-use of the payload where the signature was created for a different purpose or payload type.
pub fn payload(
///
/// It is the caller's responsibility to check that the signing key is what
/// is expected. For example, checking that the signing key is from a
/// certain peer.
Comment on lines +52 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about enforcing this constraint via #[must_use]? Something along the lines of the playground below:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1a32dedc2d59c3298dc73a8b48d9d76c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh excellent! I was wondering if there was a way I could leverage #[must_use]. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, unfortunately one can not do it on an anonymous tuple. E.g. instead of the additional struct I would prefer to do:

fn foo() -> Result<(a, #[must_use] b), c> {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was my first thought, but I hadn't considering using a struct instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the above suggestion actually does not work. The compiler reports the field not being used in general, rather than not being used in this particular case.

Also nesting #[must_use] structs doesn't work see rust-lang/rust#39524.

Also @MarcoPolo pointed out that users would likely do something along the lines of let (payload, _) = payload().handle_error(); which would thus circumvent the #[must_use].

Sorry for the noise here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do something like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=92e6fc6cf971873eb590d9ada72f85fa

But I am not sure if it is useful. Assigning _ will silence the must_use warning ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately assigning the return value to anything and not using the key value will also satisfy the must_use lint. e.g. let data = foo().unwrap();

pub fn payload_and_signing_key(
&self,
domain_separation: String,
expected_payload_type: &[u8],
) -> Result<&[u8], ReadPayloadError> {
) -> Result<(&[u8], &PublicKey), ReadPayloadError> {
if self.payload_type != expected_payload_type {
return Err(ReadPayloadError::UnexpectedPayloadType {
expected: expected_payload_type.to_vec(),
Expand All @@ -64,7 +68,7 @@ impl SignedEnvelope {
return Err(ReadPayloadError::InvalidSignature);
}

Ok(&self.payload)
Ok((&self.payload, &self.key))
}

/// Encode this [`SignedEnvelope`] using the protobuf encoding specified in the RFC.
Expand Down Expand Up @@ -221,11 +225,11 @@ mod tests {
)
.expect("Failed to create envelope");

let actual_payload = env
.payload(domain_separation, &payload_type)
let (actual_payload, signing_key) = env
.payload_and_signing_key(domain_separation, &payload_type)
.expect("Failed to extract payload and public key");

assert_eq!(actual_payload, payload);
assert_eq!(env.key, kp.public());
assert_eq!(signing_key, &kp.public());
}
}