1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use super::types as guest_types;
use super::WasiCryptoCtx;

pub use anyhow::Error;

use std::num::TryFromIntError;

#[derive(thiserror::Error, Debug)]
pub enum CryptoError {
    #[error("Success")]
    Success,
    #[error("Guest error")]
    GuestError(#[from] wiggle::GuestError),
    #[error("Not implemented")]
    NotImplemented,
    #[error("Unsupported feature")]
    UnsupportedFeature,
    #[error("Prohibited by local policy")]
    ProhibitedOperation,
    #[error("Unsupported encoding")]
    UnsupportedEncoding,
    #[error("Unsupported algorithm")]
    UnsupportedAlgorithm,
    #[error("Unsupported option")]
    UnsupportedOption,
    #[error("Invalid key")]
    InvalidKey,
    #[error("Verification failed")]
    InvalidLength,
    #[error("Invalid length")]
    VerificationFailed,
    #[error("RNG error")]
    RNGError,
    #[error("Operation failed")]
    AlgorithmFailure,
    #[error("Invalid signature")]
    InvalidSignature,
    #[error("Handle already closed")]
    Closed,
    #[error("Invalid handle")]
    InvalidHandle,
    #[error("Overflow")]
    Overflow,
    #[error("Internal error")]
    InternalError,
    #[error("Too many open handles")]
    TooManyHandles,
    #[error("Selected algorithm doesn't support a key")]
    KeyNotSupported,
    #[error("Selected algorithm requires a key")]
    KeyRequired,
    #[error("Authentication tag did not verify")]
    InvalidTag,
    #[error("Operation invalid for the selected algorithm")]
    InvalidOperation,
    #[error("Nonce required")]
    NonceRequired,
    #[error("Option not set")]
    OptionNotSet,
    #[error("Key not found")]
    KeyNotFound,
    #[error("Parameters missing")]
    ParametersMissing,
    #[error("Incompatible keys")]
    IncompatibleKeys,
    #[error("Expired key")]
    ExpiredKey,
}

impl From<CryptoError> for guest_types::CryptoErrno {
    fn from(e: CryptoError) -> Self {
        match e {
            CryptoError::Success => guest_types::CryptoErrno::Success,
            CryptoError::GuestError(_wiggle_error) => guest_types::CryptoErrno::GuestError,
            CryptoError::NotImplemented => guest_types::CryptoErrno::NotImplemented,
            CryptoError::UnsupportedFeature => guest_types::CryptoErrno::UnsupportedFeature,
            CryptoError::ProhibitedOperation => guest_types::CryptoErrno::ProhibitedOperation,
            CryptoError::UnsupportedEncoding => guest_types::CryptoErrno::UnsupportedEncoding,
            CryptoError::UnsupportedAlgorithm => guest_types::CryptoErrno::UnsupportedAlgorithm,
            CryptoError::UnsupportedOption => guest_types::CryptoErrno::UnsupportedOption,
            CryptoError::InvalidKey => guest_types::CryptoErrno::InvalidKey,
            CryptoError::InvalidLength => guest_types::CryptoErrno::InvalidLength,
            CryptoError::VerificationFailed => guest_types::CryptoErrno::VerificationFailed,
            CryptoError::RNGError => guest_types::CryptoErrno::RngError,
            CryptoError::AlgorithmFailure => guest_types::CryptoErrno::AlgorithmFailure,
            CryptoError::InvalidSignature => guest_types::CryptoErrno::InvalidSignature,
            CryptoError::Closed => guest_types::CryptoErrno::Closed,
            CryptoError::InvalidHandle => guest_types::CryptoErrno::InvalidHandle,
            CryptoError::Overflow => guest_types::CryptoErrno::Overflow,
            CryptoError::InternalError => guest_types::CryptoErrno::InternalError,
            CryptoError::TooManyHandles => guest_types::CryptoErrno::TooManyHandles,
            CryptoError::KeyNotSupported => guest_types::CryptoErrno::KeyNotSupported,
            CryptoError::KeyRequired => guest_types::CryptoErrno::KeyRequired,
            CryptoError::InvalidTag => guest_types::CryptoErrno::InvalidTag,
            CryptoError::InvalidOperation => guest_types::CryptoErrno::InvalidOperation,
            CryptoError::NonceRequired => guest_types::CryptoErrno::NonceRequired,
            CryptoError::OptionNotSet => guest_types::CryptoErrno::OptionNotSet,
            CryptoError::KeyNotFound => guest_types::CryptoErrno::KeyNotFound,
            CryptoError::ParametersMissing => guest_types::CryptoErrno::ParametersMissing,
            CryptoError::IncompatibleKeys => guest_types::CryptoErrno::IncompatibleKeys,
            CryptoError::ExpiredKey => guest_types::CryptoErrno::ExpiredKey,
        }
    }
}

impl From<TryFromIntError> for CryptoError {
    fn from(_: TryFromIntError) -> Self {
        CryptoError::Overflow
    }
}

impl From<TryFromIntError> for guest_types::CryptoErrno {
    fn from(_: TryFromIntError) -> Self {
        CryptoError::Overflow.into()
    }
}

#[macro_export]
macro_rules! ensure {
    ($cond:expr, $err:expr $(,)?) => {
        if !$cond {
            return Err($err);
        }
    };
    ($cond:expr, $fmt:expr, $($arg:tt)*) => {
        if !$cond {
            return Err($fmt, $($arg)*);
        }
    };
}

#[macro_export]
macro_rules! bail {
    ($err:expr $(,)?) => {
        return Err($err);
    };
    ($fmt:expr, $($arg:tt)*) => {
        return Err($fmt, $($arg)*);
    };
}

pub use {bail, ensure};

impl From<CryptoError> for i32 {
    fn from(e: CryptoError) -> Self {
        guest_types::CryptoErrno::from(e).into()
    }
}

impl<'a> wiggle::GuestErrorType for guest_types::CryptoErrno {
    fn success() -> Self {
        guest_types::CryptoErrno::Success
    }
}

impl guest_types::GuestErrorConversion for WasiCryptoCtx {
    fn into_crypto_errno(&self, e: wiggle::GuestError) -> guest_types::CryptoErrno {
        eprintln!("GuestError (witx) {:?}", e);
        guest_types::CryptoErrno::GuestError
    }
}

impl From<wiggle::GuestError> for guest_types::CryptoErrno {
    fn from(e: wiggle::GuestError) -> Self {
        eprintln!("GuestError (impl) {:?}", e);
        guest_types::CryptoErrno::GuestError
    }
}