• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 mod ffi;
22 
23 pub(crate) mod error;
24 pub(crate) mod ssl;
25 pub(crate) mod stack;
26 pub(crate) mod x509;
27 
28 pub mod adapter;
29 use core::ptr;
30 use std::sync::Once;
31 
32 pub use adapter::{Cert, Certificate, TlsConfig, TlsConfigBuilder, TlsFileType, TlsVersion};
33 use error::ErrorStack;
34 use libc::c_int;
35 
36 use crate::util::c_openssl::ffi::OPENSSL_init_ssl;
37 
38 /// Automatic loading of the libssl error strings. This option is a default
39 /// option.
40 pub(crate) const OPENSSL_INIT_LOAD_SSL_STRINGS: u64 = 0x00200000;
41 
42 /// Checks null-pointer.
check_ptr<T>(ptr: *mut T) -> Result<*mut T, ErrorStack>43 pub(crate) fn check_ptr<T>(ptr: *mut T) -> Result<*mut T, ErrorStack> {
44     if ptr.is_null() {
45         Err(ErrorStack::get())
46     } else {
47         Ok(ptr)
48     }
49 }
50 
51 /// Gets errors if the return value <= 0.
check_ret(r: c_int) -> Result<c_int, ErrorStack>52 pub(crate) fn check_ret(r: c_int) -> Result<c_int, ErrorStack> {
53     if r <= 0 {
54         Err(ErrorStack::get())
55     } else {
56         Ok(r)
57     }
58 }
59 
60 /// Calls this function will explicitly initialise BOTH libcrypto and libssl.
ssl_init()61 pub(crate) fn ssl_init() {
62     static SSL_INIT: Once = Once::new();
63     let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS;
64 
65     SSL_INIT.call_once(|| unsafe {
66         OPENSSL_init_ssl(init_options, ptr::null_mut());
67     })
68 }
69