Skip to content

Commit 495ca35

Browse files
authored
Explicitly handling base58 key recovery errors (#396)
1 parent 3c07a69 commit 495ca35

File tree

8 files changed

+85
-55
lines changed

8 files changed

+85
-55
lines changed

common/client-libs/directory-client/models/src/presence/coconodes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub enum ConversionError {
2121
InvalidKeyError,
2222
}
2323

24-
impl From<identity::SignatureError> for ConversionError {
25-
fn from(_: identity::SignatureError) -> Self {
24+
impl From<identity::KeyRecoveryError> for ConversionError {
25+
fn from(_: identity::KeyRecoveryError) -> Self {
2626
ConversionError::InvalidKeyError
2727
}
2828
}

common/client-libs/directory-client/models/src/presence/gateways.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub enum ConversionError {
2424
InvalidAddress(io::Error),
2525
}
2626

27-
impl From<identity::SignatureError> for ConversionError {
28-
fn from(_: identity::SignatureError) -> Self {
27+
impl From<identity::KeyRecoveryError> for ConversionError {
28+
fn from(_: identity::KeyRecoveryError) -> Self {
2929
ConversionError::InvalidKeyError
3030
}
3131
}
3232

33-
impl From<encryption::EncryptionKeyError> for ConversionError {
34-
fn from(_: encryption::EncryptionKeyError) -> Self {
33+
impl From<encryption::KeyRecoveryError> for ConversionError {
34+
fn from(_: encryption::KeyRecoveryError) -> Self {
3535
ConversionError::InvalidKeyError
3636
}
3737
}

common/client-libs/directory-client/models/src/presence/mixnodes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub enum ConversionError {
2424
InvalidAddress(io::Error),
2525
}
2626

27-
impl From<encryption::EncryptionKeyError> for ConversionError {
28-
fn from(_: encryption::EncryptionKeyError) -> Self {
27+
impl From<encryption::KeyRecoveryError> for ConversionError {
28+
fn from(_: encryption::KeyRecoveryError) -> Self {
2929
ConversionError::InvalidKeyError
3030
}
3131
}

common/crypto/src/asymmetric/encryption/mod.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,31 @@ pub const PUBLIC_KEY_SIZE: usize = 32;
2525
/// Size of a X25519 shared secret
2626
pub const SHARED_SECRET_SIZE: usize = 32;
2727

28-
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
29-
pub enum EncryptionKeyError {
30-
InvalidPublicKey,
31-
InvalidPrivateKey,
28+
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
29+
pub enum KeyRecoveryError {
30+
InvalidPublicKeyBytes,
31+
InvalidPrivateKeyBytes,
32+
MalformedString(bs58::decode::Error),
33+
}
34+
35+
impl From<bs58::decode::Error> for KeyRecoveryError {
36+
fn from(err: bs58::decode::Error) -> Self {
37+
KeyRecoveryError::MalformedString(err)
38+
}
3239
}
3340

3441
// required for std::error::Error
35-
impl Display for EncryptionKeyError {
42+
impl Display for KeyRecoveryError {
3643
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3744
match self {
38-
EncryptionKeyError::InvalidPrivateKey => write!(f, "Invalid private key"),
39-
EncryptionKeyError::InvalidPublicKey => write!(f, "Invalid public key"),
45+
KeyRecoveryError::InvalidPrivateKeyBytes => write!(f, "Invalid private key bytes"),
46+
KeyRecoveryError::InvalidPublicKeyBytes => write!(f, "Invalid public key bytes"),
47+
KeyRecoveryError::MalformedString(err) => write!(f, "malformed string - {}", err),
4048
}
4149
}
4250
}
4351

44-
impl std::error::Error for EncryptionKeyError {}
52+
impl std::error::Error for KeyRecoveryError {}
4553

4654
pub struct KeyPair {
4755
pub(crate) private_key: PrivateKey,
@@ -72,7 +80,7 @@ impl KeyPair {
7280
&self.public_key
7381
}
7482

75-
pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Result<Self, EncryptionKeyError> {
83+
pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Result<Self, KeyRecoveryError> {
7684
Ok(KeyPair {
7785
private_key: PrivateKey::from_bytes(priv_bytes)?,
7886
public_key: PublicKey::from_bytes(pub_bytes)?,
@@ -108,9 +116,9 @@ impl PublicKey {
108116
*self.0.as_bytes()
109117
}
110118

111-
pub fn from_bytes(b: &[u8]) -> Result<Self, EncryptionKeyError> {
119+
pub fn from_bytes(b: &[u8]) -> Result<Self, KeyRecoveryError> {
112120
if b.len() != PUBLIC_KEY_SIZE {
113-
return Err(EncryptionKeyError::InvalidPublicKey);
121+
return Err(KeyRecoveryError::InvalidPublicKeyBytes);
114122
}
115123
let mut bytes = [0; PUBLIC_KEY_SIZE];
116124
bytes.copy_from_slice(&b[..PUBLIC_KEY_SIZE]);
@@ -121,16 +129,14 @@ impl PublicKey {
121129
bs58::encode(&self.to_bytes()).into_string()
122130
}
123131

124-
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, EncryptionKeyError> {
125-
let bytes = bs58::decode(val.into())
126-
.into_vec()
127-
.expect("TODO: deal with this failure case");
132+
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, KeyRecoveryError> {
133+
let bytes = bs58::decode(val.into()).into_vec()?;
128134
Self::from_bytes(&bytes)
129135
}
130136
}
131137

132138
impl PemStorableKey for PublicKey {
133-
type Error = EncryptionKeyError;
139+
type Error = KeyRecoveryError;
134140

135141
fn pem_type() -> &'static str {
136142
"X25519 PUBLIC KEY"
@@ -159,9 +165,9 @@ impl PrivateKey {
159165
self.0.to_bytes()
160166
}
161167

162-
pub fn from_bytes(b: &[u8]) -> Result<Self, EncryptionKeyError> {
168+
pub fn from_bytes(b: &[u8]) -> Result<Self, KeyRecoveryError> {
163169
if b.len() != PRIVATE_KEY_SIZE {
164-
return Err(EncryptionKeyError::InvalidPrivateKey);
170+
return Err(KeyRecoveryError::InvalidPrivateKeyBytes);
165171
}
166172
let mut bytes = [0; 32];
167173
bytes.copy_from_slice(&b[..PRIVATE_KEY_SIZE]);
@@ -172,10 +178,8 @@ impl PrivateKey {
172178
bs58::encode(&self.to_bytes()).into_string()
173179
}
174180

175-
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, EncryptionKeyError> {
176-
let bytes = bs58::decode(val.into())
177-
.into_vec()
178-
.expect("TODO: deal with this failure case");
181+
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, KeyRecoveryError> {
182+
let bytes = bs58::decode(val.into()).into_vec()?;
179183
Self::from_bytes(&bytes)
180184
}
181185

@@ -186,7 +190,7 @@ impl PrivateKey {
186190
}
187191

188192
impl PemStorableKey for PrivateKey {
189-
type Error = EncryptionKeyError;
193+
type Error = KeyRecoveryError;
190194

191195
fn pem_type() -> &'static str {
192196
"X25519 PRIVATE KEY"

common/crypto/src/asymmetric/identity/mod.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,36 @@ pub use ed25519_dalek::{Verifier, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATUR
1919
use nymsphinx_types::{DestinationAddressBytes, DESTINATION_ADDRESS_LENGTH};
2020
use pemstore::traits::{PemStorableKey, PemStorableKeyPair};
2121
use rand::{rngs::OsRng, CryptoRng, RngCore};
22+
use std::fmt::{self, Formatter};
23+
24+
#[derive(Debug)]
25+
pub enum KeyRecoveryError {
26+
MalformedBytes(SignatureError),
27+
MalformedString(bs58::decode::Error),
28+
}
29+
30+
impl From<SignatureError> for KeyRecoveryError {
31+
fn from(err: SignatureError) -> Self {
32+
KeyRecoveryError::MalformedBytes(err)
33+
}
34+
}
35+
36+
impl From<bs58::decode::Error> for KeyRecoveryError {
37+
fn from(err: bs58::decode::Error) -> Self {
38+
KeyRecoveryError::MalformedString(err)
39+
}
40+
}
41+
42+
impl fmt::Display for KeyRecoveryError {
43+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
44+
match self {
45+
KeyRecoveryError::MalformedBytes(err) => write!(f, "malformed bytes - {}", err),
46+
KeyRecoveryError::MalformedString(err) => write!(f, "malformed string - {}", err),
47+
}
48+
}
49+
}
50+
51+
impl std::error::Error for KeyRecoveryError {}
2252

2353
/// Keypair for usage in ed25519 EdDSA.
2454
pub struct KeyPair {
@@ -49,7 +79,7 @@ impl KeyPair {
4979
&self.public_key
5080
}
5181

52-
pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Result<Self, SignatureError> {
82+
pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Result<Self, KeyRecoveryError> {
5383
Ok(KeyPair {
5484
private_key: PrivateKey::from_bytes(priv_bytes)?,
5585
public_key: PublicKey::from_bytes(pub_bytes)?,
@@ -96,18 +126,16 @@ impl PublicKey {
96126
self.0.to_bytes()
97127
}
98128

99-
pub fn from_bytes(b: &[u8]) -> Result<Self, SignatureError> {
129+
pub fn from_bytes(b: &[u8]) -> Result<Self, KeyRecoveryError> {
100130
Ok(PublicKey(ed25519_dalek::PublicKey::from_bytes(b)?))
101131
}
102132

103133
pub fn to_base58_string(&self) -> String {
104134
bs58::encode(&self.to_bytes()).into_string()
105135
}
106136

107-
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, SignatureError> {
108-
let bytes = bs58::decode(val.into())
109-
.into_vec()
110-
.expect("TODO: deal with this failure case");
137+
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, KeyRecoveryError> {
138+
let bytes = bs58::decode(val.into()).into_vec()?;
111139
Self::from_bytes(&bytes)
112140
}
113141

@@ -117,7 +145,7 @@ impl PublicKey {
117145
}
118146

119147
impl PemStorableKey for PublicKey {
120-
type Error = SignatureError;
148+
type Error = KeyRecoveryError;
121149

122150
fn pem_type() -> &'static str {
123151
"ED25519 PUBLIC KEY"
@@ -147,18 +175,16 @@ impl PrivateKey {
147175
self.0.to_bytes()
148176
}
149177

150-
pub fn from_bytes(b: &[u8]) -> Result<Self, SignatureError> {
178+
pub fn from_bytes(b: &[u8]) -> Result<Self, KeyRecoveryError> {
151179
Ok(PrivateKey(ed25519_dalek::SecretKey::from_bytes(b)?))
152180
}
153181

154182
pub fn to_base58_string(&self) -> String {
155183
bs58::encode(&self.to_bytes()).into_string()
156184
}
157185

158-
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, SignatureError> {
159-
let bytes = bs58::decode(val.into())
160-
.into_vec()
161-
.expect("TODO: deal with this failure case");
186+
pub fn from_base58_string<S: Into<String>>(val: S) -> Result<Self, KeyRecoveryError> {
187+
let bytes = bs58::decode(val.into()).into_vec()?;
162188
Self::from_bytes(&bytes)
163189
}
164190

@@ -171,7 +197,7 @@ impl PrivateKey {
171197
}
172198

173199
impl PemStorableKey for PrivateKey {
174-
type Error = SignatureError;
200+
type Error = KeyRecoveryError;
175201

176202
fn pem_type() -> &'static str {
177203
"ED25519 PRIVATE KEY"

common/nymsphinx/addressing/src/clients.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const CLIENT_IDENTITY_SIZE: usize = identity::PUBLIC_KEY_LENGTH;
2222
#[derive(Debug)]
2323
pub enum RecipientFormattingError {
2424
MalformedRecipientError,
25-
MalformedIdentityError(identity::SignatureError),
26-
MalformedEncryptionKeyError(encryption::EncryptionKeyError),
27-
MalformedGatewayError(identity::SignatureError),
25+
MalformedIdentityError(identity::KeyRecoveryError),
26+
MalformedEncryptionKeyError(encryption::KeyRecoveryError),
27+
MalformedGatewayError(identity::KeyRecoveryError),
2828
}
2929

3030
impl fmt::Display for RecipientFormattingError {
@@ -51,8 +51,8 @@ impl fmt::Display for RecipientFormattingError {
5151
// since we have Debug and Display might as well slap Error on top of it too
5252
impl std::error::Error for RecipientFormattingError {}
5353

54-
impl From<encryption::EncryptionKeyError> for RecipientFormattingError {
55-
fn from(err: encryption::EncryptionKeyError) -> Self {
54+
impl From<encryption::KeyRecoveryError> for RecipientFormattingError {
55+
fn from(err: encryption::KeyRecoveryError) -> Self {
5656
RecipientFormattingError::MalformedEncryptionKeyError(err)
5757
}
5858
}

common/nymsphinx/src/receiver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct ReconstructedMessage {
3434
pub enum MessageRecoveryError {
3535
InvalidSurbPrefixError,
3636
MalformedSURBError(ReplySURBError),
37-
InvalidRemoteEphemeralKey(encryption::EncryptionKeyError),
37+
InvalidRemoteEphemeralKey(encryption::KeyRecoveryError),
3838
MalformedFragmentError,
3939
InvalidMessagePaddingError,
4040
MalformedReconstructedMessage(Vec<i32>),
@@ -47,8 +47,8 @@ impl From<ReplySURBError> for MessageRecoveryError {
4747
}
4848
}
4949

50-
impl From<encryption::EncryptionKeyError> for MessageRecoveryError {
51-
fn from(err: encryption::EncryptionKeyError) -> Self {
50+
impl From<encryption::KeyRecoveryError> for MessageRecoveryError {
51+
fn from(err: encryption::KeyRecoveryError) -> Self {
5252
MessageRecoveryError::InvalidRemoteEphemeralKey(err)
5353
}
5454
}

network-monitor/src/test_packet.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use crypto::asymmetric::encryption;
16-
use crypto::asymmetric::encryption::EncryptionKeyError;
16+
use crypto::asymmetric::encryption::KeyRecoveryError;
1717
use directory_client::mixmining::MixStatus;
1818
use std::convert::{TryFrom, TryInto};
1919
use std::fmt::{self, Display, Formatter};
@@ -26,8 +26,8 @@ pub(crate) enum TestPacketError {
2626
InvalidNodeKey,
2727
}
2828

29-
impl From<encryption::EncryptionKeyError> for TestPacketError {
30-
fn from(_: EncryptionKeyError) -> Self {
29+
impl From<encryption::KeyRecoveryError> for TestPacketError {
30+
fn from(_: KeyRecoveryError) -> Self {
3131
TestPacketError::InvalidNodeKey
3232
}
3333
}

0 commit comments

Comments
 (0)