Commit a121170b authored by Henrik Böving's avatar Henrik Böving
Browse files

Merge remote-tracking branch 'upstream/master'

parents 9fc64b43 6b5a4365
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ jobs:
      - uses: actions/checkout@v2
      - uses: sfackler/actions/rustup@master
        with:
          version: 1.36.0
          version: 1.46.0
      - run: echo "::set-output name=version::$(rustc --version)"
        id: rust-version
      - uses: actions/cache@v1
@@ -148,7 +148,10 @@ jobs:
            - name: openssl
              version: vendored
            - name: openssl
              version: 1.1.1k
              version: 3.0.0
              dl-path: /
            - name: openssl
              version: 1.1.1l
              dl-path: /
            - name: openssl
              version: 1.1.0l
@@ -200,7 +203,7 @@ jobs:
        - uses: actions/cache@v2
          with:
            path: /opt/openssl
            key: openssl-${{ matrix.target }}-${{ matrix.library.name }}-${{ matrix.library.version }}
            key: openssl-${{ matrix.target }}-${{ matrix.library.name }}-${{ matrix.library.version }}-2
          if: matrix.library.version != 'vendored'
          id: openssl-cache
        - name: Build OpenSSL
@@ -238,7 +241,7 @@ jobs:

            case "${{ matrix.library.name }}" in
            "openssl")
              ./Configure --prefix=$OPENSSL_DIR $OS_COMPILER -fPIC -g $OS_FLAGS no-shared
              ./Configure --prefix=$OPENSSL_DIR --libdir=lib $OS_COMPILER -fPIC -g $OS_FLAGS no-shared
              ;;
            "libressl")
              ./configure --prefix=$OPENSSL_DIR --disable-shared --with-pic
+12 −1
Original line number Diff line number Diff line
@@ -2,8 +2,19 @@

## [Unreleased]

## [v0.2.0] - 2021-06-18

### Changed

* Constructors and accessors on the `Function` and `Reason` types have been made private APIs.

### Added

* Added support for OpenSSL 3.x.x.

## v0.1.0 - 2019-03-14

Initial release

[Unreleased]: https://github.com/sfackler/rust-openssl/compare/openssl-errors-v0.1.0...master
[Unreleased]: https://github.com/sfackler/rust-openssl/compare/openssl-errors-v0.2.0...master
[v0.2.0]: https://github.com/sfackler/rust-openssl/compare/openssl-errors-v0.1.0...openssl-errors-v0.2.0
+3 −2
Original line number Diff line number Diff line
[package]
name = "openssl-errors"
version = "0.1.0"
version = "0.2.0"
authors = ["Steven Fackler <sfackler@gmail.com>"]
edition = "2018"
license = "MIT/Apache-2.0"
@@ -10,9 +10,10 @@ readme = "README.md"
categories = ["api-bindings"]

[dependencies]
cfg-if = "1.0"
libc = "0.2"

openssl-sys = { version = "0.9.42", path = "../openssl-sys" }
openssl-sys = { version = "0.9.64", path = "../openssl-sys" }

[dev-dependencies]
openssl = { version = "0.10.19", path = "../openssl" }
+13 −0
Original line number Diff line number Diff line
#![allow(clippy::inconsistent_digit_grouping, clippy::unusual_byte_groupings)]

use std::env;

fn main() {
    if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
        let version = u64::from_str_radix(&version, 16).unwrap();

        if version >= 0x3_00_00_00_0 {
            println!("cargo:rustc-cfg=ossl300");
        }
    }
}
+149 −47
Original line number Diff line number Diff line
@@ -45,8 +45,9 @@
//! println!("{}", Error::get().unwrap());
//! ```
#![warn(missing_docs)]
#![doc(html_root_url = "https://docs.rs/openssl-errors/0.1")]
#![doc(html_root_url = "https://docs.rs/openssl-errors/0.2")]

use cfg_if::cfg_if;
use libc::{c_char, c_int};
use std::borrow::Cow;
use std::marker::PhantomData;
@@ -70,19 +71,37 @@ pub trait Library {
    fn id() -> c_int;
}

cfg_if! {
    if #[cfg(ossl300)] {
        type FunctionInner = *const c_char;
    } else {
        type FunctionInner = c_int;
    }
}

/// A function declaration, parameterized by its error library.
pub struct Function<T>(c_int, PhantomData<T>);
pub struct Function<T>(FunctionInner, PhantomData<T>);

// manual impls necessary for the 3.0.0 case
unsafe impl<T> Sync for Function<T> where T: Sync {}
unsafe impl<T> Send for Function<T> where T: Send {}

impl<T> Function<T> {
    /// Creates a function from its raw identifier.
    /// This is not considered a part of the crate's public API, and is subject to change at any time.
    ///
    /// # Safety
    ///
    /// The inner value must be valid for the lifetime of the process.
    #[doc(hidden)]
    #[inline]
    pub const fn from_raw(raw: c_int) -> Function<T> {
    pub const unsafe fn __from_raw(raw: FunctionInner) -> Function<T> {
        Function(raw, PhantomData)
    }

    /// Returns the function's raw identifier.
    /// This is not considered a part of the crate's public API, and is subject to change at any time.
    #[doc(hidden)]
    #[inline]
    pub const fn as_raw(&self) -> c_int {
    pub const fn __as_raw(&self) -> FunctionInner {
        self.0
    }
}
@@ -91,15 +110,17 @@ impl<T> Function<T> {
pub struct Reason<T>(c_int, PhantomData<T>);

impl<T> Reason<T> {
    /// Creates a reason from its raw identifier.
    /// This is not considered a part of the crate's public API, and is subject to change at any time.
    #[doc(hidden)]
    #[inline]
    pub const fn from_raw(raw: c_int) -> Reason<T> {
    pub const fn __from_raw(raw: c_int) -> Reason<T> {
        Reason(raw, PhantomData)
    }

    /// Returns the reason's raw identifier.
    /// This is not considered a part of the crate's public API, and is subject to change at any time.
    #[doc(hidden)]
    #[inline]
    pub const fn as_raw(&self) -> c_int {
    pub const fn __as_raw(&self) -> c_int {
        self.0
    }
}
@@ -119,13 +140,37 @@ pub unsafe fn __put_error<T>(
) where
    T: Library,
{
    put_error_inner(T::id(), func.0, reason.0, file, line, message)
}

unsafe fn put_error_inner(
    library: c_int,
    func: FunctionInner,
    reason: c_int,
    file: &'static str,
    line: u32,
    message: Option<Cow<'static, str>>,
) {
    cfg_if! {
        if #[cfg(ossl300)] {
            openssl_sys::ERR_new();
            openssl_sys::ERR_set_debug(
                file.as_ptr() as *const c_char,
                line as c_int,
                func,
            );
            openssl_sys::ERR_set_error(library, reason, ptr::null());
        } else {
            openssl_sys::ERR_put_error(
        T::id(),
        func.as_raw(),
        reason.as_raw(),
                library,
                func,
                reason,
                file.as_ptr() as *const c_char,
                line as c_int,
            );
        }
    }

    let data = match message {
        Some(Cow::Borrowed(s)) => Some((s.as_ptr() as *const c_char as *mut c_char, 0)),
        Some(Cow::Owned(s)) => {
@@ -172,8 +217,9 @@ macro_rules! put_error {
                $reason,
                concat!(file!(), "\0"),
                line!(),
                // go through format_args to ensure the message string is handled in the same way as the args case
                $crate::export::Option::Some($crate::export::Cow::Borrowed(
                    concat!($message, "\0"),
                    format_args!(concat!($message, "\0")).as_str().unwrap(),
                )),
            );
        }
@@ -223,31 +269,11 @@ macro_rules! openssl_errors {
            fn id() -> $crate::export::c_int {
                static INIT: $crate::export::Once = $crate::export::Once::new();
                static mut LIB_NUM: $crate::export::c_int = 0;
                static mut STRINGS: [
                    $crate::export::ERR_STRING_DATA;
                    2 + $crate::openssl_errors!(@count $($func_name;)* $($reason_name;)*)
                ] = [
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
                    },
                    $(
                        $crate::export::ERR_STRING_DATA {
                            error: $crate::export::ERR_PACK(0, $lib_name::$func_name.as_raw(), 0),
                            string: concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char,
                        },
                    )*
                    $(
                        $crate::export::ERR_STRING_DATA {
                            error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.as_raw()),
                            string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
                        },
                    )*
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: $crate::export::null(),
                $crate::__openssl_errors_helper! {
                    @strings $lib_name($lib_str)
                    functions { $($func_name($func_str);)* }
                    reasons { $($reason_name($reason_str);)* }
                }
                ];

                unsafe {
                    INIT.call_once(|| {
@@ -263,19 +289,21 @@ macro_rules! openssl_errors {
        }

        impl $lib_name {
            $crate::openssl_errors!(@func_consts $lib_name; 1; $($(#[$func_attr])* $func_name;)*);
            $crate::openssl_errors!(@func_consts $lib_name; 1; $($(#[$func_attr])* $func_name($func_str);)*);
            $crate::openssl_errors!(@reason_consts $lib_name; 1; $($(#[$reason_attr])* $reason_name;)*);
        }
    )*};
    (@func_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident; $($tt:tt)*) => {
    (@func_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident($str:expr); $($tt:tt)*) => {
        $(#[$attr])*
        pub const $name: $crate::Function<$lib_name> = $crate::Function::from_raw($n);
        pub const $name: $crate::Function<$lib_name> = unsafe {
            $crate::Function::__from_raw($crate::__openssl_errors_helper!(@func_value $n, $str))
        };
        $crate::openssl_errors!(@func_consts $lib_name; $n + 1; $($tt)*);
    };
    (@func_consts $lib_name:ident; $n:expr;) => {};
    (@reason_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident; $($tt:tt)*) => {
        $(#[$attr])*
        pub const $name: $crate::Reason<$lib_name> = $crate::Reason::from_raw($n);
        pub const $name: $crate::Reason<$lib_name> = $crate::Reason::__from_raw($n);
        $crate::openssl_errors!(@reason_consts $lib_name; $n + 1; $($tt)*);
    };
    (@reason_consts $lib_name:ident; $n:expr;) => {};
@@ -284,3 +312,77 @@ macro_rules! openssl_errors {
    };
    (@count) => { 0 };
}

cfg_if! {
    if #[cfg(ossl300)] {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __openssl_errors_helper {
            (
                @strings $lib_name:ident($lib_str:expr)
                functions { $($func_name:ident($func_str:expr);)* }
                reasons { $($reason_name:ident($reason_str:expr);)* }
            ) => {
                static mut STRINGS: [
                    $crate::export::ERR_STRING_DATA;
                    2 + $crate::openssl_errors!(@count $($reason_name;)*)
                ] = [
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
                    },
                    $(
                        $crate::export::ERR_STRING_DATA {
                            error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.__as_raw()),
                            string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
                        },
                    )*
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: $crate::export::null(),
                    }
                ];
            };
            (@func_value $n:expr, $func_str:expr) => {
                concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char
            };
        }
    } else {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __openssl_errors_helper {
            (
                @strings $lib_name:ident($lib_str:expr)
                functions { $($func_name:ident($func_str:expr);)* }
                reasons { $($reason_name:ident($reason_str:expr);)* }
            ) => {
                static mut STRINGS: [
                    $crate::export::ERR_STRING_DATA;
                    2 + $crate::openssl_errors!(@count $($func_name;)* $($reason_name;)*)
                ] = [
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
                    },
                    $(
                        $crate::export::ERR_STRING_DATA {
                            error: $crate::export::ERR_PACK(0, $lib_name::$func_name.__as_raw(), 0),
                            string: concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char,
                        },
                    )*
                    $(
                        $crate::export::ERR_STRING_DATA {
                            error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.__as_raw()),
                            string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
                        },
                    )*
                    $crate::export::ERR_STRING_DATA {
                        error: 0,
                        string: $crate::export::null(),
                    }
                ];
            };
            (@func_value $n:expr, $func_str:expr) => {$n};
        }
    }
}
Loading