summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/error.rs
blob: d1e9e962f2eac481e4789324eca5701984fe1676 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Maps various errors to status codes.

use std::io;
use libc::c_char;

use sequoia_openpgp as openpgp;

use crate::MoveIntoRaw;
use crate::RefRaw;

/// Complex errors.
///
/// This wraps [`anyhow::Error`]s.
///
/// [`anyhow::Error`]: https://docs.rs/failure/0.1.5/failure/struct.Error.html
#[crate::ffi_wrapper_type(prefix = "pgp_", derive = "Display")]
pub struct Error(anyhow::Error);

impl<T> From<anyhow::Result<T>> for Status {
    fn from(f: anyhow::Result<T>) -> crate::error::Status {
        match f {
            Ok(_) =>  crate::error::Status::Success,
            Err(e) => crate::error::Status::from(&e),
        }
    }
}

impl crate::MoveResultIntoRaw<crate::error::Status> for ::anyhow::Result<()>
{
    fn move_into_raw(self, errp: Option<&mut *mut crate::error::Error>)
                     -> crate::error::Status {
        match self {
            Ok(_) => crate::error::Status::Success,
            Err(e) => {
                let status = crate::error::Status::from(&e);
                if let Some(errp) = errp {
                    *errp = e.move_into_raw();
                }
                status
            },
        }
    }
}

/// Returns the error status code.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_error_status(error: *const Error)
                                       -> Status {
    error.ref_raw().into()
}

/// XXX: Reorder and name-space before release.
#[derive(PartialEq, Debug)]
#[repr(C)]
pub enum Status {
    /// The operation was successful.
    Success = 0,

    /// An unknown error occurred.
    UnknownError = -1,

    /// The network policy was violated by the given action.
    NetworkPolicyViolation = -2,

    /// An IO error occurred.
    IoError = -3,

    /// A given argument is invalid.
    InvalidArgument = -15,

    /// The requested operation is invalid.
    InvalidOperation = -4,

    /// The packet is malformed.
    MalformedPacket = -5,

    /// Packet size exceeds the configured limit.
    PacketTooLarge = -29,

    /// Unsupported packet type.
    UnsupportedPacketType = -14,

    /// Unsupported hash algorithm.
    UnsupportedHashAlgorithm = -9,

    /// Unsupported public key algorithm.
    UnsupportedPublicKeyAlgorithm = -18,

    /// Unsupported elliptic curve.
    UnsupportedEllipticCurve = -21,

    /// Unsupported symmetric algorithm.
    UnsupportedSymmetricAlgorithm = -10,

    /// Unsupported AEAD algorithm.
    UnsupportedAEADAlgorithm = -26,

    /// Unsupported Compression algorithm.
    UnsupportedCompressionAlgorithm = -28,

    /// Unsupported signature type.
    UnsupportedSignatureType = -20,

    /// Invalid password.
    InvalidPassword = -11,

    /// Invalid session key.
    InvalidSessionKey = -12,

    /// Missing session key.
    MissingSessionKey = -27,

    /// Malformed Cert.
    MalformedCert = -13,

    // XXX: Skipping UnsupportedPacketType = -14.

    // XXX: Skipping InvalidArgument = -15.

    /// Malformed MPI.
    MalformedMPI = -16,

    // XXX: Skipping UnknownPublicKeyAlgorithm = -17.
    // XXX: Skipping UnsupportedPublicKeyAlgorithm = -18

    /// Bad signature.
    BadSignature = -19,

    /// Message has been manipulated.
    ManipulatedMessage = -25,

    // XXX: Skipping UnsupportedSignatureType = -20
    // XXX: Skipping UnsupportedEllipticCurve = -21

    /// Malformed message.
    MalformedMessage = -22,

    /// Index out of range.
    IndexOutOfRange = -23,

    /// Cert not supported.
    UnsupportedCert = -24,

    // XXX: Skipping ManipulatedMessage = -25
    // XXX: Skipping UnsupportedAEADAlgorithm = -26
    // XXX: Skipping MissingSessionKey = -27
    // XXX: Skipping UnsupportedCompressionAlgorithm = -28
    // XXX: Skipping PacketTooLarge = -29

    /// Expired.
    Expired = -30,

    /// Not yet live.
    NotYetLive = -31,

    /// No binding signature.
    NoBindingSignature = -32,

    /// Invalid key.
    InvalidKey = -33,

    /// Policy violation.
    PolicyViolation = -34,
}

/// Returns the error message.
///
/// The returned value must *not* be freed.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_status_to_string(status: Status) -> *const c_char {
    use crate::error::Status::*;

    match status {
        Success => "Success\x00",
        UnknownError => "An unknown error occurred\x00",
        NetworkPolicyViolation =>
            "The network policy was violated by the given action\x00",
        IoError => "An IO error occurred\x00",
        InvalidArgument => "A given argument is invalid\x00",
        InvalidOperation => "The requested operation is invalid\x00",
        MalformedPacket => "The packet is malformed\x00",
        PacketTooLarge => "Packet size exceeds the configured limit\x00",
        UnsupportedPacketType => "Unsupported packet type\x00",
        UnsupportedHashAlgorithm => "Unsupported hash algorithm\x00",
        UnsupportedPublicKeyAlgorithm =>
            "Unsupported public key algorithm\x00",
        UnsupportedEllipticCurve => "Unsupported elliptic curve\x00",
        UnsupportedSymmetricAlgorithm =>
            "Unsupported symmetric algorithm\x00",
        UnsupportedAEADAlgorithm => "Unsupported AEAD algorithm\x00",
        UnsupportedCompressionAlgorithm =>
            "Unsupported compression algorithm\x00",
        UnsupportedSignatureType => "Unsupported signature type\x00",
        InvalidPassword => "Invalid password\x00",
        InvalidSessionKey => "Invalid session key\x00",
        MissingSessionKey => "Missing session key\x00",
        MalformedCert => "Malformed Cert\x00",
        MalformedMPI => "Malformed MPI\x00",
        BadSignature => "Bad signature\x00",
        ManipulatedMessage => "Message has been manipulated\x00",
        MalformedMessage => "Malformed message\x00",
        IndexOutOfRange => "Index out of range\x00",
        UnsupportedCert => "Cert not supported\x00",
        Expired => "Expired\x00",
        NotYetLive => "Not yet live\x00",
        NoBindingSignature => "No binding signature\x00",
        InvalidKey => "Invalid key\x00",
        PolicyViolation => "Policy violation\x00",
    }.as_bytes().as_ptr() as *const c_char
}

impl<'a> From<&'a anyhow::Error> for Status {
    fn from(e: &'a anyhow::Error) -> Self {
        if let Some(e) = e.downcast_ref::<openpgp::Error>() {
            return match e {
                &openpgp::Error::InvalidArgument(_) =>
                    Status::InvalidArgument,
                &openpgp::Error::InvalidOperation(_) =>
                    Status::InvalidOperation,
                &openpgp::Error::MalformedPacket(_) =>
                    Status::MalformedPacket,
                &openpgp::Error::PacketTooLarge(_, _, _) =>
                    Status::PacketTooLarge,
                &openpgp::Error::UnsupportedPacketType(_) =>
                    Status::UnsupportedPacketType,
                &openpgp::Error::UnsupportedHashAlgorithm(_) =>
                    Status::UnsupportedHashAlgorithm,
                &openpgp::Error::UnsupportedPublicKeyAlgorithm(_) =>
                    Status::UnsupportedPublicKeyAlgorithm,
                &openpgp::Error::UnsupportedEllipticCurve(_) =>
                    Status::UnsupportedEllipticCurve,
                &openpgp::Error::UnsupportedSymmetricAlgorithm(_) =>
                    Status::UnsupportedSymmetricAlgorithm,
                &openpgp::Error::UnsupportedAEADAlgorithm(_) =>
                    Status::UnsupportedAEADAlgorithm,
                &openpgp::Error::UnsupportedCompressionAlgorithm(_) =>