1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //! TLS implementation based on [`Openssl`]
15 //!
16 //! [`Openssl`]: https://www.openssl.org/
17
18 #[macro_use]
19 mod foreign;
20 mod bio;
21 pub mod ffi;
22
23 pub(crate) mod error;
24 pub(crate) mod ssl;
25
26 // todo
27 #[allow(dead_code)]
28 pub(crate) mod stack;
29 pub(crate) mod x509;
30
31 pub mod adapter;
32 pub(crate) mod verify;
33
34 use core::ptr;
35 use std::sync::Once;
36
37 pub use adapter::{Cert, Certificate, TlsConfig, TlsConfigBuilder, TlsFileType, TlsVersion};
38 use error::ErrorStack;
39 use libc::c_int;
40 pub use verify::{PubKeyPins, PubKeyPinsBuilder};
41
42 pub(crate) use crate::util::c_openssl::ffi::callback::*;
43 use crate::util::c_openssl::ffi::OPENSSL_init_ssl;
44
45 /// Automatic loading of the libssl error strings. This option is a default
46 /// option.
47 pub(crate) const OPENSSL_INIT_LOAD_SSL_STRINGS: u64 = 0x00200000;
48
49 /// Checks null-pointer.
check_ptr<T>(ptr: *mut T) -> Result<*mut T, ErrorStack>50 pub(crate) fn check_ptr<T>(ptr: *mut T) -> Result<*mut T, ErrorStack> {
51 if ptr.is_null() {
52 Err(ErrorStack::get())
53 } else {
54 Ok(ptr)
55 }
56 }
57
58 /// Gets errors if the return value <= 0.
check_ret(r: c_int) -> Result<c_int, ErrorStack>59 pub(crate) fn check_ret(r: c_int) -> Result<c_int, ErrorStack> {
60 if r <= 0 {
61 Err(ErrorStack::get())
62 } else {
63 Ok(r)
64 }
65 }
66
67 /// Calls this function will explicitly initialise BOTH libcrypto and libssl.
ssl_init()68 pub(crate) fn ssl_init() {
69 static SSL_INIT: Once = Once::new();
70 let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS;
71
72 SSL_INIT.call_once(|| unsafe {
73 OPENSSL_init_ssl(init_options, ptr::null_mut());
74 })
75 }
76