[go: up one dir, main page]

botan 0.12.0

Rust wrapper for Botan cryptography library
Documentation
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use botan_sys::*;
use core::fmt;

#[cfg(not(feature = "std"))]
pub(crate) use alloc::{borrow::ToOwned, string::String, string::ToString, vec::Vec};

#[cfg(not(feature = "std"))]
pub(crate) use alloc::ffi::CString;

#[cfg(not(feature = "std"))]
pub(crate) use core::ffi::CStr;

#[cfg(feature = "std")]
pub(crate) use std::ffi::{CStr, CString};

pub(crate) use botan_sys::ffi_types::{c_char, c_int, c_void};
pub(crate) use core::mem;
pub(crate) use core::ptr;

/// The result of calling an operation on the library
pub type Result<T> = ::core::result::Result<T, Error>;

pub(crate) fn make_cstr(input: &str) -> Result<CString> {
    let cstr = CString::new(input).map_err(Error::conversion_error)?;
    Ok(cstr)
}

pub(crate) fn call_botan_ffi_returning_vec_u8(
    initial_size: usize,
    cb: &dyn Fn(*mut u8, *mut usize) -> c_int,
) -> Result<Vec<u8>> {
    let mut output = vec![0; initial_size];
    let mut out_len = output.len();

    let rc = cb(output.as_mut_ptr(), &mut out_len);
    if rc == 0 {
        assert!(out_len <= output.len());
        output.resize(out_len, 0);
        return Ok(output);
    } else if rc != BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE {
        return Err(Error::from_rc(rc));
    }

    output.resize(out_len, 0);
    let rc = cb(output.as_mut_ptr(), &mut out_len);

    if rc != 0 {
        return Err(Error::from_rc(rc));
    }

    output.resize(out_len, 0);
    Ok(output)
}

#[cfg(botan_ffi_20230403)]
pub(crate) mod view {
    use super::*;

    type FfiViewBinaryFn = extern "C" fn(*mut c_void, *const u8, usize) -> c_int;

    extern "C" fn botan_ffi_view_u8_fn(ctx: *mut c_void, buf: *const u8, len: usize) -> c_int {
        if ctx.is_null() || buf.is_null() {
            return BOTAN_FFI_ERROR_NULL_POINTER;
        }

        let vec = ctx as *mut Vec<u8>;

        unsafe {
            let data = core::slice::from_raw_parts(buf, len);
            (*vec).clear();
            (*vec).extend_from_slice(data);
        }

        0
    }

    pub(crate) fn call_botan_ffi_viewing_vec_u8(
        cb: &dyn Fn(*mut c_void, FfiViewBinaryFn) -> c_int,
    ) -> Result<Vec<u8>> {
        let mut view_ctx: Vec<u8> = vec![];
        let rc = cb(
            &mut view_ctx as *mut Vec<u8> as *mut _,
            botan_ffi_view_u8_fn,
        );
        if rc != 0 {
            return Err(Error::from_rc(rc));
        }

        Ok(view_ctx)
    }

    type FfiViewStrFn = extern "C" fn(*mut c_void, *const c_char, usize) -> c_int;

    extern "C" fn botan_ffi_view_str_fn(ctx: *mut c_void, buf: *const c_char, len: usize) -> c_int {
        if ctx.is_null() || buf.is_null() {
            return BOTAN_FFI_ERROR_NULL_POINTER;
        }

        if len == 0 {
            return BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR;
        }

        let str = ctx as *mut String;

        let data = unsafe { core::slice::from_raw_parts(buf as *const u8, len - 1) };

        let mut vec = Vec::new();
        vec.extend_from_slice(data);
        match String::from_utf8(vec) {
            Ok(decoded) => {
                unsafe {
                    *str = decoded;
                }
                0
            }
            Err(_) => BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR,
        }
    }

    pub(crate) fn call_botan_ffi_viewing_str_fn(
        cb: &dyn Fn(*mut c_void, FfiViewStrFn) -> c_int,
    ) -> Result<String> {
        let mut view_ctx = String::new();
        let rc = cb(
            &mut view_ctx as *mut String as *mut _,
            botan_ffi_view_str_fn,
        );
        if rc != 0 {
            return Err(Error::from_rc(rc));
        }

        Ok(view_ctx)
    }
}

#[cfg(botan_ffi_20230403)]
pub(crate) use crate::view::*;

fn cstr_slice_to_str(raw_cstr: &[u8]) -> Result<String> {
    let cstr = CStr::from_bytes_with_nul(raw_cstr).map_err(Error::conversion_error)?;
    Ok(cstr.to_str().map_err(Error::conversion_error)?.to_owned())
}

#[cfg(botan_ffi_20230403)]
unsafe fn cstr_to_str(raw_cstr: *const c_char) -> Result<String> {
    let cstr = CStr::from_ptr(raw_cstr);
    Ok(cstr.to_str().map_err(Error::conversion_error)?.to_owned())
}

#[cfg(botan_ffi_20250506)]
pub(crate) fn interp_as_bool(result: c_int, fn_name: &'static str) -> Result<bool> {
    if result == 0 {
        Ok(false)
    } else if result == 1 {
        Ok(true)
    } else {
        Err(Error::with_message(
            ErrorType::InternalError,
            format!("Unexpected result from {}", fn_name),
        ))
    }
}

pub(crate) fn call_botan_ffi_returning_string(
    initial_size: usize,
    cb: &dyn Fn(*mut u8, *mut usize) -> c_int,
) -> Result<String> {
    let v = call_botan_ffi_returning_vec_u8(initial_size, cb)?;
    cstr_slice_to_str(&v)
}

#[allow(unused_macros)]
macro_rules! ffi_version_from_cfg {
    (botan_ffi_20230403) => {
        20230403
    };
    (botan_ffi_20240408) => {
        20240408
    };
    (botan_ffi_20250506) => {
        20250506
    };
}

pub(crate) use ffi_version_from_cfg;

macro_rules! ffi_version_guard {
    ($fn_name:expr, $cfg_val:ident, [ $($arg:ident),* ], $if_impl:block) => {{
        #[cfg($cfg_val)]
        {
            $if_impl
        }

        #[cfg(not($cfg_val))]
        {
            $(
                let _ = $arg;
            )*
            Err(Error::not_implemented(
                $fn_name,
                ffi_version_from_cfg!($cfg_val),
            ))
        }
    }};
}

pub(crate) use ffi_version_guard;

/// The library error type
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
    err_type: ErrorType,
    message: Option<String>,
}

impl Error {
    /// Return the general type of the error
    pub fn error_type(&self) -> ErrorType {
        self.err_type
    }

    /// Return an optional message specific to the error
    ///
    /// This is only available in Botan 3.x; with older versions
    /// it will always be None
    pub fn error_message(&self) -> Option<&str> {
        self.message.as_deref()
    }

    pub(crate) fn from_rc(rc: c_int) -> Self {
        let err_type = ErrorType::from(rc);

        #[cfg(botan_ffi_20230403)]
        let message = {
            let cptr = unsafe { botan_sys::botan_error_last_exception_message() };
            match unsafe { cstr_to_str(cptr) } {
                Err(_) => None,
                Ok(s) if !s.is_empty() => Some(s),
                Ok(_) => None,
            }
        };

        #[cfg(not(botan_ffi_20230403))]
        let message = None;

        Self { err_type, message }
    }

    pub(crate) fn with_message(err_type: ErrorType, message: String) -> Self {
        Self {
            err_type,
            message: Some(message),
        }
    }

    pub(crate) fn bad_parameter(message: &'static str) -> Self {
        Self::with_message(ErrorType::BadParameter, message.to_owned())
    }

    #[allow(dead_code)]
    pub(crate) fn not_implemented(fn_name: &'static str, ffi_version: u32) -> Self {
        Self::with_message(
            ErrorType::NotImplemented,
            format!("Function {fn_name} not available - requires Botan FFI {ffi_version}"),
        )
    }

    #[cfg(feature = "std")]
    pub(crate) fn conversion_error<T: std::error::Error>(e: T) -> Self {
        Self {
            err_type: ErrorType::ConversionError,
            message: Some(format!("{e}")),
        }
    }

    // Hack to deal with missing std::error::Error in no-std
    #[cfg(not(feature = "std"))]
    pub(crate) fn conversion_error<T: core::fmt::Display>(e: T) -> Self {
        Self {
            err_type: ErrorType::ConversionError,
            message: Some(format!("{}", e)),
        }
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match &self.message {
            Some(m) => write!(f, "{} ({})", self.err_type, m),
            None => write!(f, "{}", self.err_type),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Possible error categories
pub enum ErrorType {
    /// A provided authentication code was incorrect
    BadAuthCode,
    /// A bad flag was passed to the library
    BadFlag,
    /// An invalid parameter was provided to the library
    BadParameter,
    /// No value available
    NoValueAvailable,
    /// An exception was thrown while processing this request
    ExceptionThrown,
    /// There was insufficient buffer space to write the output
    InsufficientBufferSpace,
    /// Converting a string to UTF8 failed
    StringConversionError,
    /// An internal error occurred (this is a bug in the library)
    InternalError,
    /// Something about the input was invalid
    InvalidInput,
    /// An invalid object was provided to the library
    InvalidObject,
    /// An object was invoked in a way that is invalid for its current state
    InvalidObjectState,
    /// A verifier was incorrect
    InvalidVerifier,
    /// An key of invalid length was provided
    InvalidKeyLength,
    /// An object was invoked without the key being set
    KeyNotSet,
    /// Some functionality is not implemented in the current library version
    NotImplemented,
    /// A null pointer was incorrectly provided
    NullPointer,
    /// Memory exhaustion
    OutOfMemory,
    /// An error occurred while invoking a system API
    SystemError,
    /// Some unknown error occurred
    UnknownError,
    /// An error occured while converting data to C
    ConversionError,
    /// An error occurred in TLS
    TlsError,
    /// An error occurred during an HTTP transaction
    HttpError,
}

impl fmt::Display for ErrorType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let msg = match self {
            Self::BadAuthCode => "A provided authentication code was incorrect",
            Self::BadFlag => "A bad flag was passed to the library",
            Self::BadParameter => "An invalid parameter was provided to the library",
            Self::NoValueAvailable => "No value was available",
            Self::ExceptionThrown => "An exception was thrown while processing this request",
            Self::StringConversionError => "Error converting a string into UTF-8",
            Self::InsufficientBufferSpace => {
                "There was insufficient buffer space to write the output"
            }
            Self::InternalError => "An internal error occurred (this is a bug in the library)",
            Self::InvalidInput => "Something about the input was invalid",
            Self::InvalidObject => "An invalid object was provided to the library",
            Self::InvalidObjectState => {
                "An object was invoked in a way that is invalid for its current state"
            }
            Self::InvalidVerifier => "A verifier was incorrect",
            Self::InvalidKeyLength => "An key of invalid length was provided",
            Self::KeyNotSet => "An object was invoked without the key being set",
            Self::NotImplemented => {
                "Some functionality is not implemented in the current library version"
            }
            Self::NullPointer => "A null pointer was incorrectly provided",
            Self::OutOfMemory => "Memory exhaustion",
            Self::SystemError => "An error occurred while invoking a system API",
            Self::UnknownError => "Some unknown error occurred",
            Self::ConversionError => "An error occured while converting data to C",
            Self::TlsError => "An error occurred in TLS",
            Self::HttpError => "An error occurred during an HTTP transaction",
        };

        write!(f, "{msg}")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<i32> for ErrorType {
    fn from(err: i32) -> Self {
        match err {
            BOTAN_FFI_ERROR_BAD_FLAG => Self::BadFlag,
            BOTAN_FFI_ERROR_BAD_MAC => Self::BadAuthCode,
            BOTAN_FFI_ERROR_BAD_PARAMETER => Self::BadParameter,
            BOTAN_FFI_ERROR_NO_VALUE => Self::NoValueAvailable,
            BOTAN_FFI_ERROR_EXCEPTION_THROWN => Self::ExceptionThrown,
            BOTAN_FFI_ERROR_HTTP_ERROR => Self::HttpError,
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE => Self::InsufficientBufferSpace,
            BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR => Self::StringConversionError,
            BOTAN_FFI_ERROR_INTERNAL_ERROR => Self::InternalError,
            BOTAN_FFI_ERROR_INVALID_INPUT => Self::InvalidInput,
            BOTAN_FFI_ERROR_INVALID_KEY_LENGTH => Self::InvalidKeyLength,
            BOTAN_FFI_ERROR_INVALID_OBJECT => Self::InvalidObject,
            BOTAN_FFI_ERROR_INVALID_OBJECT_STATE => Self::InvalidObjectState,
            BOTAN_FFI_ERROR_KEY_NOT_SET => Self::KeyNotSet,
            BOTAN_FFI_ERROR_NOT_IMPLEMENTED => Self::NotImplemented,
            BOTAN_FFI_ERROR_NULL_POINTER => Self::NullPointer,
            BOTAN_FFI_ERROR_OUT_OF_MEMORY => Self::OutOfMemory,
            BOTAN_FFI_ERROR_SYSTEM_ERROR => Self::SystemError,
            BOTAN_FFI_ERROR_TLS_ERROR => Self::TlsError,
            BOTAN_FFI_ERROR_UNKNOWN_ERROR => Self::UnknownError,
            BOTAN_FFI_INVALID_VERIFIER => Self::InvalidVerifier,
            _ => Self::UnknownError,
        }
    }
}

/// Specifies valid keylengths for symmetric ciphers/MACs
pub struct KeySpec {
    min_keylen: usize,
    max_keylen: usize,
    mod_keylen: usize,
}

impl KeySpec {
    pub(crate) fn new(min_keylen: usize, max_keylen: usize, mod_keylen: usize) -> Result<KeySpec> {
        if min_keylen > max_keylen || mod_keylen == 0 {
            return Err(Error::with_message(
                ErrorType::ConversionError,
                "Bad key spec".to_owned(),
            ));
        }

        Ok(KeySpec {
            min_keylen,
            max_keylen,
            mod_keylen,
        })
    }

    /// Return true if the specified key length is valid for this object
    #[must_use]
    pub fn is_valid_keylength(&self, keylen: usize) -> bool {
        keylen >= self.min_keylen && keylen <= self.max_keylen && keylen % self.mod_keylen == 0
    }

    /// Return the minimum supported keylength
    #[must_use]
    pub fn minimum_keylength(&self) -> usize {
        self.min_keylen
    }

    /// Return the maximum supported keylength
    #[must_use]
    pub fn maximum_keylength(&self) -> usize {
        self.max_keylen
    }

    /// Return the required multiple of the keylength
    ///
    /// That is each key must be N*keylength_multiple() for some N
    #[must_use]
    pub fn keylength_multiple(&self) -> usize {
        self.mod_keylen
    }
}