#[cfg(feature = "aleo")]
use crate::aleo::AleoGeneratePrivateKeyError;
use crate::JWK;
use base64::DecodeError as Base64Error;
#[cfg(feature = "ring")]
use ring::error::{KeyRejected as KeyRejectedError, Unspecified as RingUnspecified};
#[cfg(feature = "rsa")]
use rsa::errors::Error as RsaError;
use simple_asn1::ASN1EncodeErr as ASN1EncodeError;
use std::array::TryFromSliceError;
use std::char::CharTryFromError;
use std::num::ParseIntError;
use std::string::FromUtf8Error;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("Missing curve in JWK")]
MissingCurve,
#[error("Missing elliptic curve point in JWK")]
MissingPoint,
#[error("Missing key value for symmetric key")]
MissingKeyValue,
#[error("Key type not supported")]
UnsupportedKeyType,
#[error("Key type not implemented for {0}")]
KeyTypeNotImplemented(Box<JWK>),
#[error("Curve not implemented: '{0}'")]
CurveNotImplemented(String),
#[error("Missing private key parameter in JWK")]
MissingPrivateKey,
#[error("Missing modulus in RSA key")]
MissingModulus,
#[error("Missing exponent in RSA key")]
MissingExponent,
#[error("Missing prime in RSA key")]
MissingPrime,
#[error("Invalid key length: {0}")]
InvalidKeyLength(usize),
#[cfg(feature = "ring")]
#[error("{0}")]
KeyRejected(KeyRejectedError),
#[cfg(feature = "ring")]
#[error("{0}")]
RingUnspecified(RingUnspecified),
#[error(transparent)]
FromUtf8(#[from] FromUtf8Error),
#[cfg(feature = "rsa")]
#[error(transparent)]
Rsa(#[from] RsaError),
#[error(transparent)]
ASN1Encode(#[from] ASN1EncodeError),
#[error(transparent)]
Base64(#[from] Base64Error),
#[error(transparent)]
ParseInt(#[from] ParseIntError),
#[cfg(feature = "eip")]
#[error(transparent)]
Eip155(#[from] ssi_crypto::hashes::keccak::Eip155Error),
#[error(transparent)]
CharTryFrom(#[from] CharTryFromError),
#[error(transparent)]
TryFromSlice(#[from] TryFromSliceError),
#[cfg(feature = "aleo")]
#[error(transparent)]
AleoGeneratePrivateKey(#[from] AleoGeneratePrivateKeyError),
#[error("Expected 64 byte uncompressed key or 33 bytes compressed key but found length: {0}")]
P256KeyLength(usize),
#[error("Expected 96 byte uncompressed key or 49 bytes compressed key but found length: {0}")]
P384KeyLength(usize),
#[error("Unable to decompress elliptic curve")]
ECDecompress,
#[cfg(feature = "secp256k1")]
#[error(transparent)]
CryptoErr(#[from] k256::ecdsa::Error),
#[cfg(all(feature = "secp256r1", not(feature = "secp256k1")))]
#[error(transparent)]
CryptoErr(#[from] p256::ecdsa::Error),
#[cfg(all(
feature = "secp384r1",
not(any(feature = "secp256r1", feature = "secp256k1"))
))]
#[error(transparent)]
CryptoErr(#[from] p384::ecdsa::Error),
#[cfg(all(
feature = "ed25519",
not(any(feature = "secp384r1", feature = "secp256r1", feature = "secp256k1"))
))]
#[error(transparent)]
CryptoErr(#[from] ed25519_dalek::ed25519::Error),
#[cfg(feature = "secp256k1")]
#[error(transparent)]
EC(#[from] k256::elliptic_curve::Error),
#[cfg(all(feature = "secp256r1", not(feature = "secp256k1")))]
#[error(transparent)]
EC(#[from] p256::elliptic_curve::Error),
#[cfg(all(
feature = "secp384r1",
not(any(feature = "secp256r1", feature = "secp256k1"))
))]
#[error(transparent)]
EC(#[from] p384::elliptic_curve::Error),
#[error("Unexpected length for publicKeyMultibase")]
MultibaseKeyLength(usize, usize),
#[error(transparent)]
Multibase(#[from] multibase::Error),
#[error("Invalid coordinates")]
InvalidCoordinates,
}
#[cfg(feature = "ring")]
impl From<KeyRejectedError> for Error {
fn from(e: KeyRejectedError) -> Error {
Error::KeyRejected(e)
}
}
#[cfg(feature = "ring")]
impl From<RingUnspecified> for Error {
fn from(e: RingUnspecified) -> Error {
Error::RingUnspecified(e)
}
}