Skip to main content

leyline_bssl/
lib.rs

1#[macro_use]
2extern crate bitflags;
3#[macro_use]
4extern crate foreign_types;
5extern crate leyline_bssl_sys as ffi;
6extern crate libc;
7
8#[cfg(test)]
9extern crate hex;
10
11use std::ffi::{c_int, c_long, c_void};
12use std::num::NonZeroUsize;
13
14#[doc(inline)]
15pub use crate::ffi::init;
16
17use crate::error::ErrorStack;
18
19#[macro_use]
20mod macros;
21
22mod bio;
23#[macro_use]
24mod util;
25pub mod asn1;
26pub mod bn;
27pub mod conf;
28pub mod derive;
29pub mod dh;
30pub mod dsa;
31pub mod ec;
32pub mod error;
33pub mod ex_data;
34mod handshake_shed;
35pub mod hash;
36pub mod hmac;
37pub mod hpke;
38pub mod nid;
39pub mod pkey;
40/// TLS 1.3 record protection that a caller runs after the handshake.
41pub mod records;
42pub mod rsa;
43pub mod srtp;
44pub mod ssl;
45pub mod stack;
46pub mod string;
47pub mod symm;
48pub mod version;
49pub mod x509;
50
51fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
52    if r.is_null() {
53        Err(ErrorStack::get())
54    } else {
55        Ok(r)
56    }
57}
58
59fn cvt_0(r: usize) -> Result<(), ErrorStack> {
60    if r == 0 {
61        Err(ErrorStack::get())
62    } else {
63        Ok(())
64    }
65}
66
67fn cvt_0i(r: c_int) -> Result<c_int, ErrorStack> {
68    if r == 0 {
69        Err(ErrorStack::get())
70    } else {
71        Ok(r)
72    }
73}
74
75fn cvt(r: c_int) -> Result<(), ErrorStack> {
76    if r <= 0 {
77        Err(ErrorStack::get())
78    } else {
79        Ok(())
80    }
81}
82
83fn cvt_nz(r: c_int) -> Result<NonZeroUsize, ErrorStack> {
84    usize::try_from(r)
85        .ok()
86        .and_then(NonZeroUsize::new)
87        .ok_or_else(ErrorStack::get)
88}
89
90fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
91    if r < 0 {
92        Err(ErrorStack::get())
93    } else {
94        Ok(r)
95    }
96}
97
98fn try_int<F, T>(from: F) -> Result<T, ErrorStack>
99where
100    F: TryInto<T> + Send + Sync + Copy + 'static,
101    T: Send + Sync + Copy + 'static,
102{
103    from.try_into()
104        .map_err(|_| ErrorStack::internal_error_str("int overflow"))
105}
106
107unsafe extern "C" fn free_data_box<T>(
108    _parent: *mut c_void,
109    ptr: *mut c_void,
110    _ad: *mut ffi::CRYPTO_EX_DATA,
111    _idx: c_int,
112    _argl: c_long,
113    _argp: *mut c_void,
114) {
115    if !ptr.is_null() {
116        unsafe {
117            drop(Box::<T>::from_raw(ptr.cast::<T>()));
118        }
119    }
120}