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
169
170
171
use bs58::decode::DecodeError;
use ed25519_dalek::SignatureError;
use identity;
use osaka_dns;
use packet::{RoutingDirection, RoutingKey};
use prost;
use proto;
use snow::SnowError;
use std::fmt;
use std::io;

#[derive(Debug)]
pub enum Error {
    Io(io::Error),
    Snow(SnowError),
    Bs58(DecodeError),
    Ed25519(SignatureError),
    Dns(osaka_dns::Error),
    Proto(prost::DecodeError),
    Fmt(std::fmt::Error),

    InvalidLen,
    InvalidAddress,
    InvalidAddressType {
        need: String,
        got:  String,
    },
    TooSmall {
        need: usize,
        got:  usize,
    },
    PktMisaligned {
        len: usize,
    },
    DecryptedInvalidPayloadLen,
    PayloadUnencrypted,
    WrongRoute {
        dest: RoutingKey,
        this: RoutingKey,
    },
    WrongDirection {
        dir: RoutingDirection,
    },
    InvalidCookie,
    InvalidVersion {
        version: u8,
    },
    InvalidFrameType {
        typ: u8,
    },
    Underflow {
        prev: u64,
        this: u64,
    },
    Overflow,
    AntiReplay,
    NoSecrets,
    SecretsfileAlreadyExists,
    OutOfOptions,
    SecurityViolation,
    BrokenChain,
    DelegationDenied,
    AccessDenied,
    NoMatchingGrant,
    OutgoingConnectFailed {
        identity: identity::Identity,
        cr:       Option<proto::ConnectResponse>,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(e) => e.fmt(f),
            Error::Snow(e) => e.fmt(f),
            Error::Bs58(e) => e.fmt(f),
            Error::Ed25519(e) => e.fmt(f),
            Error::Dns(e) => write!(f, "{:?}", e),
            Error::Proto(e) => e.fmt(f),
            Error::Fmt(e) => e.fmt(f),

            Error::InvalidLen => write!(f, "invalid address: length"),
            Error::InvalidAddress => write!(f, "invalid address"),
            Error::InvalidAddressType { need, got } => write!(f, "invalid address type. expected {} got {}", need, got),
            Error::TooSmall { need, got } => write!(f, "packet too small, expected {}, got {}", need, got),
            Error::PktMisaligned { len } => write!(f, "packet not padded correctly to 256 bytes: {}", len),
            Error::DecryptedInvalidPayloadLen => write!(f, "decrypted payload size header is bigger than payload"),
            Error::PayloadUnencrypted => write!(f, "refusing to send unencrypted payload"),
            Error::WrongRoute { dest, this } => write!(
                f,
                "trying to decrypt packet for route {:#x} but the encryption state is for route {:#x}",
                dest, this
            ),
            Error::WrongDirection { dir } => write!(
                f,
                "packet arrived with the same routing direction we're sending with {:?}",
                dir
            ),
            Error::InvalidCookie => write!(f, "invalid cookie. probably a replay"),
            Error::InvalidVersion { version } => write!(f, "invalid version: {}", version),
            Error::InvalidFrameType { typ } => write!(f, "invalid frame type: {}", typ),
            Error::Underflow { prev, this } => write!(
                f,
                "stream underflow: incomming packet {} is too far ahead of previous {}",
                this, prev
            ),

            Error::Overflow => write!(f, "stream overflow: stream consumer is too slow"),
            Error::AntiReplay => write!(f, "packet dropped by anti-replay"),

            Error::NoSecrets => write!(
                f,
                "no secrets available. run carrier gen or see the SECRETS section in help"
            ),
            Error::SecretsfileAlreadyExists => write!(f, "~/.devguard/secret exists, refusing to overwrite"),
            Error::OutOfOptions => write!(f, "out of connect options, no broker responded"),

            Error::SecurityViolation => write!(f, "peer violated a security barrier"),

            Error::BrokenChain => write!(f, "invalid certificate chain: cert does not attest previous identity"),
            Error::DelegationDenied => write!(f, "cert does not allow delegating to more certs"),
            Error::AccessDenied => write!(f, "access denied: no certs left"),
            Error::NoMatchingGrant => write!(f, "access denied: no matching grant in cert"),
            Error::OutgoingConnectFailed { identity, cr } => {
                write!(f, "outgoing connection  to {} failed: {:?}", identity, cr)
            }
        }
    }
}

impl std::convert::From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Error::Io(error)
    }
}

impl std::convert::From<std::fmt::Error> for Error {
    fn from(error: std::fmt::Error) -> Self {
        Error::Fmt(error)
    }
}

impl std::convert::From<DecodeError> for Error {
    fn from(error: DecodeError) -> Self {
        Error::Bs58(error)
    }
}

impl std::convert::From<SnowError> for Error {
    fn from(error: SnowError) -> Self {
        Error::Snow(error)
    }
}

impl std::convert::From<SignatureError> for Error {
    fn from(error: SignatureError) -> Self {
        Error::Ed25519(error)
    }
}

impl std::convert::From<osaka_dns::Error> for Error {
    fn from(error: osaka_dns::Error) -> Self {
        Error::Dns(error)
    }
}

impl std::convert::From<prost::DecodeError> for Error {
    fn from(error: prost::DecodeError) -> Self {
        Error::Proto(error)
    }
}