Skip to content
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
7d5e606
feat: make mdns::Event clone-able
drHuangMHT Mar 13, 2023
05ce438
Merge branch 'libp2p:master' into master
Mar 13, 2023
a72e5e2
docs: filling CHANGELOG for PR 3606
drHuangMHT Mar 13, 2023
09bef20
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 13, 2023
926c7ca
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
49d26c4
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
4a35406
Merge branch 'master' into master
mergify[bot] Mar 13, 2023
a8ff1eb
feat: change 'mdns::Event' to hold 'SmallVec'
drHuangMHT Mar 16, 2023
4cec5fa
test: adapting test to 'mdns::Event' change
drHuangMHT Mar 16, 2023
a876f2f
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 16, 2023
18811d2
feat(mdns): change 'mdns::Event' to hold 'Vec'
drHuangMHT Mar 16, 2023
abdce7e
doc: add entry for #3621
drHuangMHT Mar 16, 2023
d5c246c
Update protocols/mdns/CHANGELOG.md
Mar 16, 2023
ed128a7
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
0d06f4c
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
9d45cf3
Remove conflict marker to resolve conflicts
drHuangMHT Mar 26, 2023
947b368
add 'try' to method returning 'Result'
drHuangMHT Mar 27, 2023
e3e6355
implement protobuf encoding for RSA keys
drHuangMHT Mar 27, 2023
b91bcc7
fix: change 'encode_unsupported' to 'missing_feature' for branch lead…
drHuangMHT Mar 27, 2023
959b90f
fix: wrongly configured feature attributes when encoding to protobuf
drHuangMHT Mar 27, 2023
20134d1
fix: clippy and fmt
drHuangMHT Mar 27, 2023
d833904
doc: documentation for changed methods
drHuangMHT Mar 27, 2023
0896a61
feat: enforce naming rules
drHuangMHT Mar 27, 2023
593272c
Merge branch 'master' into identity-keypair
thomaseizinger Mar 29, 2023
f559cfe
hide 'proto::KeyType'
drHuangMHT Mar 29, 2023
672b9b9
make 'to_protobuf_encoding' for 'Keypair' infallible
drHuangMHT Mar 29, 2023
33c9b9b
make 'to_protobuf_encoding' for 'PublicKey' infallible
drHuangMHT Mar 29, 2023
f4a764d
revert renamed method and deprecate instead
drHuangMHT Mar 29, 2023
8b8bfc5
split 'rsa::Keypair::try_decode' into two separate method
drHuangMHT Mar 30, 2023
921fc4a
introduce 'ConversionError' for associated 'Error' type in 'TryInto'
drHuangMHT Mar 30, 2023
b555d7f
add 'try' prefix to 'into_[keytype]' for 'libp2p_identity::Keypair'
drHuangMHT Apr 4, 2023
d65ce3e
revert renaming of method in 'libp2p_identity::Keypair'
drHuangMHT Apr 4, 2023
c587b73
recommend key promotion for deprecated methods in 'libp2p_identity::K…
drHuangMHT Apr 4, 2023
2a77a26
revert renaming of method in 'libp2p_identity::secp256k1'
drHuangMHT Apr 4, 2023
7d0ceaa
renaming 'ConversionError' to 'OtherVariantError'
drHuangMHT Apr 4, 2023
c846a3d
revert wrongly renamed 'secp256k1::SecretKey::from_der'
drHuangMHT Apr 4, 2023
a3eae1c
formatting
drHuangMHT Apr 4, 2023
3adb27f
revert removal of methods in 'libp2p_identity::PublicKey' and rename …
drHuangMHT Apr 4, 2023
43a33ce
clippy
drHuangMHT Apr 4, 2023
9ae47cf
remove unnecessary libp2p_identity::PublicKey::try_to_protobuf_encoding
drHuangMHT Apr 4, 2023
b25a446
revert rename of 'libp2p_identity::PublicKey::from_protobuf_encoding'
drHuangMHT Apr 4, 2023
d936772
improve deprecation messages
drHuangMHT Apr 5, 2023
284736c
delegate deprecated methods to suggested ones
drHuangMHT Apr 5, 2023
2f02c04
enforce 'decode' rule for protobuf encoding
drHuangMHT Apr 5, 2023
b0a33fd
formatting
drHuangMHT Apr 5, 2023
cb40674
remove support for DER-encoded RSA key
drHuangMHT Apr 5, 2023
0321368
syncing 'keypair_dummy.rs' with 'keypair.rs'
drHuangMHT Apr 5, 2023
667fa77
remove unnecessary 'Error' trait impl for 'OtherVariantError'
drHuangMHT Apr 5, 2023
c97d661
revert renaming of 'into' methods on 'Keypair' and deprecate instead
drHuangMHT Apr 10, 2023
a04b17f
prepare keys for testing
drHuangMHT Apr 10, 2023
92f6914
Add roundtrip tests
thomaseizinger Apr 10, 2023
a3285d7
Undo mdns changes
thomaseizinger Apr 10, 2023
3762a20
Merge branch 'master' into identity-keypair
thomaseizinger Apr 10, 2023
896117c
Merge branch 'master' into identity-keypair
thomaseizinger Apr 10, 2023
f5cf834
Fix cargo.lock
thomaseizinger Apr 10, 2023
74e7cf3
repetitive 'encoding' method naming removed
drHuangMHT Apr 11, 2023
4285bda
use '.ok()' combinator to replace 'match' expression
drHuangMHT Apr 11, 2023
73d9e55
put link to libp2p spec in 'encode_protobuf' documentation
drHuangMHT Apr 11, 2023
6120997
remove roundtrip test for 'ecdsa::Keypair'
drHuangMHT Apr 11, 2023
785ca7c
remove 'Eq' impl for 'ecdsa::Keypair' and 'ecdsa::SecretKey'
drHuangMHT Apr 11, 2023
faa4d42
correct diff for 'ecdsa::from_bytes'
drHuangMHT Apr 11, 2023
9914eab
deprecation note correction for 'Keypair::rsa_from_pkcs8'
drHuangMHT Apr 11, 2023
1090f14
remove feature cfg for KeyType
drHuangMHT Apr 11, 2023
278f19e
add feature cfg to branches in 'TryInto' impl of 'Keypair'
drHuangMHT Apr 11, 2023
4b103bb
add feature cfg to branches in 'TryInto' impl of 'PublicKey'
drHuangMHT Apr 11, 2023
bd0284b
fix missing target exclusion on 'Keypair::into_rsa'
drHuangMHT Apr 11, 2023
bbf59d8
formatting
drHuangMHT Apr 11, 2023
1a442fa
remove duplicate 'to_protobuf_encoding' in 'keypair_dummy.rs'
drHuangMHT Apr 12, 2023
676b7e6
revert removal of 'into' implementation on 'PublicKey' and deprecate …
drHuangMHT Apr 12, 2023
85f3871
Merge branch 'libp2p:master' into identity-keypair
Apr 14, 2023
b83422c
Merge 'libp2p/master' into identity-keypair
drHuangMHT Apr 14, 2023
ffacd94
clippy fix
drHuangMHT Apr 14, 2023
6c8b5c7
refactor 'match' expression to '?' operator
drHuangMHT Apr 17, 2023
c231e1d
rename test keys
drHuangMHT Apr 17, 2023
4ecffb3
revert 'try_decode_x509' signature change
drHuangMHT Apr 17, 2023
c958c04
revert 'to_protobuf_encoding' signature change
drHuangMHT Apr 17, 2023
7c992fd
Merge remote-tracking branch 'upstream/master' into identity-keypair
drHuangMHT Apr 17, 2023
b613d1c
implement encoding to pkcs#8 for ecdsa keys
drHuangMHT Apr 17, 2023
24a5a86
formatting
drHuangMHT Apr 25, 2023
5e776b5
resolve conflict with upstream
drHuangMHT Apr 25, 2023
aeea24b
remove 'deprecation since'
drHuangMHT Apr 26, 2023
54e2fad
replace 'unwrap' with 'expect'
drHuangMHT Apr 26, 2023
2d244c7
remove redundant 'Deprecated,' message
drHuangMHT Apr 26, 2023
2c188ca
rename 'to_raw_bytes' to 'encode_pkcs8_der'
drHuangMHT Apr 26, 2023
ba9f4ed
fix uncaught deprecated method
drHuangMHT Apr 26, 2023
1b11e4a
downgrade 'p256' and remove 'ecdsa'
drHuangMHT Apr 27, 2023
3a330ac
Merge branch 'master' into identity-keypair
thomaseizinger May 5, 2023
01b4fd6
Undo ecdsa changes
thomaseizinger May 5, 2023
46700cf
Minimize diff
thomaseizinger May 5, 2023
ce9ba0d
Merge branch 'identity-keypair' of https://github.com/drHuangMHT/rust…
drHuangMHT May 5, 2023
7662073
lock file update
drHuangMHT May 5, 2023
e4b6ec7
fix: wrong dependency 'p256' version
drHuangMHT May 5, 2023
75ee978
delete unused test keys
drHuangMHT May 5, 2023
fd7a660
revert RSA changes
drHuangMHT May 5, 2023
96ab553
Merge branch 'master' into identity-keypair
drHuangMHT May 5, 2023
b23e111
remove feature flag on 'decoding_unsupported'
drHuangMHT May 5, 2023
4d7b516
fix: duplicated outdated test
drHuangMHT May 6, 2023
11748b5
upstream sync
drHuangMHT Dec 27, 2023
cffd763
Merge branch 'master' into identity-keypair
drHuangMHT Dec 27, 2023
07df6dc
fix errors introduced by merging manually
drHuangMHT Dec 27, 2023
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
4 changes: 2 additions & 2 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl SignedEnvelope {
use quick_protobuf::MessageWrite;

let envelope = proto::Envelope {
public_key: self.key.to_protobuf_encoding(),
public_key: self.key.try_to_protobuf_encoding().unwrap(),
payload_type: self.payload_type,
payload: self.payload,
signature: self.signature,
Expand All @@ -101,7 +101,7 @@ impl SignedEnvelope {
proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?;

Ok(Self {
key: PublicKey::from_protobuf_encoding(&envelope.public_key)?,
key: PublicKey::try_from_protobuf_encoding(&envelope.public_key)?,
payload_type: envelope.payload_type.to_vec(),
payload: envelope.payload.to_vec(),
signature: envelope.signature.to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,5 @@ fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;

identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
identity::Keypair::try_from_bytes_ed25519(bytes).expect("only errors on wrong length")
}
2 changes: 1 addition & 1 deletion examples/file-sharing/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn new(
Some(seed) => {
let mut bytes = [0u8; 32];
bytes[0] = seed;
identity::Keypair::ed25519_from_bytes(bytes).unwrap()
identity::Keypair::try_from_bytes_ed25519(bytes).unwrap()
}
None => identity::Keypair::generate_ed25519(),
};
Expand Down
2 changes: 1 addition & 1 deletion examples/relay-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;

identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
identity::Keypair::try_from_bytes_ed25519(bytes).expect("only errors on wrong length")
}

#[derive(Debug, Parser)]
Expand Down
32 changes: 23 additions & 9 deletions identity/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use p256::{
use void::Void;

/// An ECDSA keypair.
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub struct Keypair {
secret: SecretKey,
public: PublicKey,
Expand All @@ -60,6 +60,13 @@ impl Keypair {
pub fn secret(&self) -> &SecretKey {
&self.secret
}

/// Try to parse an secret key byte array into a ECDSA `SecretKey`
/// and promote it into a `Keypair`.
pub fn try_from_bytes(pk: impl AsRef<[u8]>) -> Result<Keypair, DecodingError> {
let secret_key = SecretKey::try_from_bytes(pk)?;
Ok(secret_key.into())
}
}

impl fmt::Debug for Keypair {
Expand All @@ -86,7 +93,7 @@ impl From<Keypair> for SecretKey {
}

/// An ECDSA secret key.
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub struct SecretKey(SigningKey);

impl SecretKey {
Expand All @@ -102,14 +109,14 @@ impl SecretKey {
signature.as_bytes().to_owned()
}

/// Encode a secret key into a byte buffer.
/// Convert a secret key into a byte buffer.
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}

/// Decode a secret key from a byte buffer.
pub fn from_bytes(buf: &[u8]) -> Result<Self, DecodingError> {
SigningKey::from_bytes(buf)
/// Try to parse a secret key from a byte buffer.
pub fn try_from_bytes(buf: impl AsRef<[u8]>) -> Result<Self, DecodingError> {
SigningKey::from_bytes(buf.as_ref())
.map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err))
.map(SecretKey)
}
Expand All @@ -136,7 +143,7 @@ impl PublicKey {
}

/// Decode a public key from a byte buffer without compression.
pub fn from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
pub fn try_from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
let enc_pt = EncodedPoint::from_bytes(k)
.map_err(|e| DecodingError::failed_to_parse("ecdsa p256 encoded point", e))?;

Expand All @@ -157,11 +164,11 @@ impl PublicKey {
}

/// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard.
pub fn decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
pub fn try_decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
let buf = Self::del_asn1_header(k).ok_or_else(|| {
DecodingError::failed_to_parse::<Void, _>("ASN.1-encoded ecdsa p256 public key", None)
})?;
Self::from_bytes(buf)
Self::try_from_bytes(buf)
}

// ecPublicKey (ANSI X9.62 public key type) OID: 1.2.840.10045.2.1
Expand Down Expand Up @@ -258,4 +265,11 @@ mod tests {
let invalid_msg = "h3ll0 w0rld".as_bytes();
assert!(!pk.verify(invalid_msg, &sig));
}

#[test]
fn serialize_deserialize() {
let pair = Keypair::generate();
let bytes_secret = pair.secret().to_bytes();
assert_eq!(Keypair::try_from_bytes(bytes_secret).unwrap(), pair)
}
}
23 changes: 12 additions & 11 deletions identity/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl Keypair {
self.0.to_bytes()
}

/// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
/// Try to decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
/// produced by [`Keypair::encode`], zeroing the input on success.
///
/// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
pub fn decode(kp: &mut [u8]) -> Result<Keypair, DecodingError> {
pub fn try_decode(kp: &mut [u8]) -> Result<Keypair, DecodingError> {
ed25519::Keypair::from_bytes(kp)
.map(|k| {
kp.zeroize();
Expand All @@ -70,7 +70,7 @@ impl Keypair {

/// Get the secret key of this keypair.
pub fn secret(&self) -> SecretKey {
SecretKey::from_bytes(&mut self.0.secret.to_bytes())
SecretKey::try_from_bytes(&mut self.0.secret.to_bytes())
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
}
}
Expand All @@ -86,7 +86,7 @@ impl fmt::Debug for Keypair {
impl Clone for Keypair {
fn clone(&self) -> Keypair {
let mut sk_bytes = self.0.secret.to_bytes();
let secret = SecretKey::from_bytes(&mut sk_bytes)
let secret = SecretKey::try_from_bytes(&mut sk_bytes)
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
.0;
let public = ed25519::PublicKey::from_bytes(&self.0.public.to_bytes())
Expand Down Expand Up @@ -166,8 +166,8 @@ impl PublicKey {
self.0.to_bytes()
}

/// Decode a public key from a byte array as produced by `encode`.
pub fn decode(k: &[u8]) -> Result<PublicKey, DecodingError> {
/// Try to decode a public key from a byte array as produced by `encode`.
pub fn try_decode(k: &[u8]) -> Result<PublicKey, DecodingError> {
ed25519::PublicKey::from_bytes(k)
.map_err(|e| DecodingError::failed_to_parse("Ed25519 public key", e))
.map(PublicKey)
Expand All @@ -187,7 +187,8 @@ impl AsRef<[u8]> for SecretKey {
impl Clone for SecretKey {
fn clone(&self) -> SecretKey {
let mut sk_bytes = self.0.to_bytes();
Self::from_bytes(&mut sk_bytes).expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
Self::try_from_bytes(&mut sk_bytes)
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
}
}

Expand All @@ -209,10 +210,10 @@ impl SecretKey {
)
}

/// Create an Ed25519 secret key from a byte slice, zeroing the input on success.
/// Try to create an Ed25519 secret key from a byte slice, zeroing the input on success.
/// If the bytes do not constitute a valid Ed25519 secret key, an error is
/// returned.
pub fn from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
pub fn try_from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
let sk_bytes = sk_bytes.as_mut();
let secret = ed25519::SecretKey::from_bytes(&*sk_bytes)
.map_err(|e| DecodingError::failed_to_parse("Ed25519 secret key", e))?;
Expand All @@ -235,7 +236,7 @@ mod tests {
fn prop() -> bool {
let kp1 = Keypair::generate();
let mut kp1_enc = kp1.encode();
let kp2 = Keypair::decode(&mut kp1_enc).unwrap();
let kp2 = Keypair::try_decode(&mut kp1_enc).unwrap();
eq_keypairs(&kp1, &kp2) && kp1_enc.iter().all(|b| *b == 0)
}
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
Expand All @@ -246,7 +247,7 @@ mod tests {
fn prop() -> bool {
let kp1 = Keypair::generate();
let mut sk = kp1.0.secret.to_bytes();
let kp2 = Keypair::from(SecretKey::from_bytes(&mut sk).unwrap());
let kp2 = Keypair::from(SecretKey::try_from_bytes(&mut sk).unwrap());
eq_keypairs(&kp1, &kp2) && sk == [0u8; 32]
}
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
Expand Down
40 changes: 4 additions & 36 deletions identity/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ pub struct DecodingError {
}

impl DecodingError {
#[cfg(not(all(
feature = "ecdsa",
feature = "rsa",
feature = "secp256k1",
feature = "ed25519",
not(target_arch = "wasm32")
)))]
/// The given feature is supported but not enabled at compile time.
pub(crate) fn missing_feature(feature_name: &'static str) -> Self {
Self {
msg: format!("cargo feature `{feature_name}` is not enabled"),
Expand All @@ -51,6 +45,7 @@ impl DecodingError {
feature = "ed25519",
feature = "rsa"
))]
/// Error occurred when parsing one type into another.
pub(crate) fn failed_to_parse<E, S>(what: &'static str, source: S) -> Self
where
E: Error + Send + Sync + 'static,
Expand Down Expand Up @@ -80,25 +75,6 @@ impl DecodingError {
source: Some(Box::new(source)),
}
}

pub(crate) fn decoding_unsupported(key_type: &'static str) -> Self {
Self {
msg: format!("decoding {key_type} key from Protobuf is unsupported"),
source: None,
}
}

#[cfg(any(
all(feature = "rsa", not(target_arch = "wasm32")),
feature = "secp256k1",
feature = "ecdsa"
))]
pub(crate) fn encoding_unsupported(key_type: &'static str) -> Self {
Self {
msg: format!("encoding {key_type} key to Protobuf is unsupported"),
source: None,
}
}
}

impl fmt::Display for DecodingError {
Expand All @@ -123,18 +99,10 @@ pub struct SigningError {
/// An error during encoding of key material.
impl SigningError {
#[cfg(any(feature = "secp256k1", feature = "rsa"))]
pub(crate) fn new<S: ToString>(msg: S) -> Self {
pub(crate) fn new<S: ToString>(msg: S, source: Option<Box<dyn Error + Send + Sync>>) -> Self {
Self {
msg: msg.to_string(),
source: None,
}
}

#[cfg(feature = "rsa")]
pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
Self {
source: Some(Box::new(source)),
..self
source,
}
}
}
Expand Down
Loading