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

Serde implementation for KeyPair type #313

Merged
merged 1 commit into from
Dec 20, 2021

Conversation

dr-orlovsky
Copy link
Contributor

@dr-orlovsky dr-orlovsky commented Jun 27, 2021

Serde implementation for KeyPair type (which hadn't it before).

Based on #312 (includes all commits from that PR, will be rebased upon merge)

@TheBlueMatt
Copy link
Member

Why? Is Serde intended to be used for human-readable objects? Users would be totally reasonable to want to write secret key material to disk, and this makes them jump through hoops to do so. I'm all for removing foot guns, but not at the cost of disabling key library features without a flag.

@dr-orlovsky
Copy link
Contributor Author

@TheBlueMatt am I getting you right that you propose to reverse feature flag introduced by this PR, so instead of #[cfg(feature = "serde-secrets")] we should do #[cfg(not(feature = "serde-protect-secrets"))]?

@elichai
Copy link
Member

elichai commented Jul 1, 2021

@TheBlueMatt am I getting you right that you propose to reverse feature flag introduced by this PR, so instead of #[cfg(feature = "serde-secrets")] we should do #[cfg(not(feature = "serde-protect-secrets"))]?

I don't believe this should be guarded by a unique feature, it's one thing to protect against accidental logging secrets via Debug implementation, but it's another thing to prevent serialization.
IMHO Almost any user of serde in this library will probably also want to serialize secret keys and KeyPairs

@dr-orlovsky
Copy link
Contributor Author

Well, I am a user of this library and I do not want serde to default-serialize any private information. So I will prefer to be able to specify a feature which opts out from such thing.

@tcharding tcharding mentioned this pull request Jul 29, 2021
@apoelstra
Copy link
Member

We absolutely cannot do this with feature flags. It would cause users' secret key data serialization to change when their dependencies changed their set of feature flags.

@thomaseizinger
Copy link
Contributor

Similar to how we don't implement Display on SecretKey but on an adapter struct, we could just not implement Serialize on SecretKey itself but provide two adapter structs and make it easy to create them from a SecretKey:

struct SerdeSecretKeyPlain(SecretKey);
struct SerdeSecretKeyRedacted(SecretKey);


impl SecretKey {
	pub fn serialize_plain() -> SerdeSecretKeyPlain;
	pub fn serialize_redacted() -> SerdeSecretKeyPlain;
}

impl Serialize for SerdeSecretKeyPlain {
   // serialize as bytes
}


impl Serialize for SerdeSecretKeyRedacted {
   // serialize as hashed bytes (or something else)
}

Bonus point for adding a trybuild test that ensures none ever implements Serialize on SecretKey directly:

fn can_serialize<T: Serialize>(_t: T)  { }

// this test needs to go into a trybuild configration that is expected to fail
#[test]
fn secret_key_does_not_serialize() {
	can_serialize(SecretKey::random())
}

@apoelstra
Copy link
Member

Sure, but I can't think of a non-test example of secret key usage where you don't want to store them. Secret keys are not ephemeral. If you want to prevent them being stored then I think the onus is on you to create your own adaptor struct.

@real-or-random
Copy link
Collaborator

Sure, but I can't think of a non-test example of secret key usage where you don't want to store them.

One common example is that the secret key is derived from some seed/master key and so you don't need to store the derived key. Another example is ephemeral key exchange.

But I agree, it should be possible to serialize secret keys. Many very legit ways to use secret keys need storage.

@dr-orlovsky dr-orlovsky changed the title Serde re-implementation for secrets and KeyPair type Serde implementation for KeyPair type Sep 27, 2021
@dr-orlovsky
Copy link
Contributor Author

Removed from this PR everything related to preventing serde serialization. Now this PR is only about adding serde to KeyPair. It is still based on #312 since it requires display secret method.

@dr-orlovsky dr-orlovsky force-pushed the extrakeys/serde branch 3 times, most recently from fa096f2 to 656b26a Compare September 27, 2021 13:02
@apoelstra
Copy link
Member

Now that #312 is merged can this be a non-WIP?

@dr-orlovsky dr-orlovsky marked this pull request as ready for review November 8, 2021 12:51
@dr-orlovsky
Copy link
Contributor Author

@apoelstra ready

apoelstra
apoelstra previously approved these changes Nov 12, 2021
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 266ea94

but now it needs a rebase :)

@dr-orlovsky
Copy link
Contributor Author

@apoelstra rebased

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK de77518

@apoelstra apoelstra merged commit ada3f98 into rust-bitcoin:master Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants