Commit 40de0f52 authored by Steven Fackler's avatar Steven Fackler
Browse files

Merge remote-tracking branch 'origin/master' into openssl-300

parents c4797466 d7c36c63
Loading
Loading
Loading
Loading
+24 −35
Original line number Diff line number Diff line
@@ -25,16 +25,11 @@ fn resolve_with_wellknown_homebrew_location(dir: &str) -> Option<PathBuf> {
    // Check up default aarch 64 Homebrew installation location first
    // for quick resolution if possible.
    //  `pkg-config` on brew doesn't necessarily contain settings for openssl apparently.
    let mut version_dir = dir.to_owned();
    version_dir.push_str("@1.1");
    let homebrew = Path::new(&version_dir);
    let homebrew = Path::new(dir).join("opt/openssl@1.1");
    if homebrew.exists() {
        return Some(homebrew.to_path_buf());
    }
    let homebrew = Path::new(dir);
    if homebrew.exists() {
        return Some(homebrew.to_path_buf());
        return Some(homebrew);
    }

    // Calling `brew --prefix <package>` command usually slow and
    // takes seconds, and will be used only as a last resort.
    let output = execute_command_and_get_output("brew", &["--prefix", "openssl@1.1"]);
@@ -45,44 +40,38 @@ fn resolve_with_wellknown_homebrew_location(dir: &str) -> Option<PathBuf> {
        }
    }

    let output = execute_command_and_get_output("brew", &["--prefix", "openssl"]);
    if let Some(ref output) = output {
        let homebrew = Path::new(&output);
        if homebrew.exists() {
            return Some(homebrew.to_path_buf());
        }
    }

    None
}

fn resolve_with_wellknown_pkgsrc_location() -> Option<PathBuf> {
    let pkgsrc = Path::new("/opt/pkg");
    let pkgsrc_include_openssl = pkgsrc.join("include/openssl");
    if pkgsrc_include_openssl.exists() {
        return Some(pkgsrc.to_path_buf());
    }

fn resolve_with_wellknown_location(dir: &str) -> Option<PathBuf> {
    let root_dir = Path::new(dir);
    let include_openssl = root_dir.join("include/openssl");
    if include_openssl.exists() {
        Some(root_dir.to_path_buf())
    } else {
        None
    }
}

fn find_openssl_dir(target: &str) -> OsString {
    let host = env::var("HOST").unwrap();

    if host == target {
        if target == "aarch64-apple-darwin" {
            if let Some(dir) = resolve_with_wellknown_homebrew_location("/opt/homebrew/opt/openssl")
            {
    if host == target && target.ends_with("-apple-darwin") {
        let homebrew_dir = match target {
            "aarch64-apple-darwin" => "/opt/homebrew",
            _ => "/usr/local",
        };

        if let Some(dir) = resolve_with_wellknown_homebrew_location(homebrew_dir) {
            return dir.into();
            }
        } else if target.contains("apple-darwin") {
            if let Some(dir) = resolve_with_wellknown_homebrew_location("/usr/local/opt/openssl") {
        } else if let Some(dir) = resolve_with_wellknown_location("/opt/pkg") {
            // pkgsrc
            return dir.into();
            } else if let Some(dir) = resolve_with_wellknown_pkgsrc_location() {
        } else if let Some(dir) = resolve_with_wellknown_location("/opt/local") {
            // MacPorts
            return dir.into();
        }
    }
    }

    try_pkg_config();
    try_vcpkg();
+8 −0
Original line number Diff line number Diff line
@@ -81,6 +81,14 @@ extern "C" {

    pub fn EC_GROUP_new_by_curve_name(nid: c_int) -> *mut EC_GROUP;

    pub fn EC_POINT_is_at_infinity(group: *const EC_GROUP, point: *const EC_POINT) -> c_int;

    pub fn EC_POINT_is_on_curve(
        group: *const EC_GROUP,
        point: *const EC_POINT,
        ctx: *mut BN_CTX,
    ) -> c_int;

    pub fn EC_POINT_new(group: *const EC_GROUP) -> *mut EC_POINT;

    pub fn EC_POINT_free(point: *mut EC_POINT);
+57 −0
Original line number Diff line number Diff line
@@ -527,6 +527,38 @@ impl EcPointRef {
            .map(|_| ())
        }
    }

    /// Checks if point is infinity
    ///
    /// OpenSSL documentation at [`EC_POINT_is_at_infinity`]
    ///
    /// [`EC_POINT_is_at_infinity`]: https://www.openssl.org/docs/man1.1.0/man3/EC_POINT_is_at_infinity.html
    pub fn is_infinity(&self, group: &EcGroupRef) -> bool {
        unsafe {
            let res = ffi::EC_POINT_is_at_infinity(group.as_ptr(), self.as_ptr());
            res == 1
        }
    }

    /// Checks if point is on a given curve
    ///
    /// OpenSSL documentation at [`EC_POINT_is_on_curve`]
    ///
    /// [`EC_POINT_is_on_curve`]: https://www.openssl.org/docs/man1.1.0/man3/EC_POINT_is_on_curve.html
    pub fn is_on_curve(
        &self,
        group: &EcGroupRef,
        ctx: &mut BigNumContextRef,
    ) -> Result<bool, ErrorStack> {
        unsafe {
            let res = cvt_n(ffi::EC_POINT_is_on_curve(
                group.as_ptr(),
                self.as_ptr(),
                ctx.as_ptr(),
            ))?;
            Ok(res == 1)
        }
    }
}

impl EcPoint {
@@ -1074,4 +1106,29 @@ mod test {
        assert_eq!(xbn2, xbn);
        assert_eq!(ybn2, ybn);
    }

    #[test]
    fn is_infinity() {
        let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
        let mut ctx = BigNumContext::new().unwrap();
        let g = group.generator();
        assert_eq!(g.is_infinity(&group), false);

        let mut order = BigNum::new().unwrap();
        group.order(&mut order, &mut ctx).unwrap();
        let mut inf = EcPoint::new(&group).unwrap();
        inf.mul_generator(&group, &order, &ctx).unwrap();
        assert_eq!(inf.is_infinity(&group), true);
    }

    #[test]
    fn is_on_curve() {
        let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
        let mut ctx = BigNumContext::new().unwrap();
        let g = group.generator();
        assert_eq!(g.is_on_curve(&group, &mut ctx).unwrap(), true);

        let group2 = EcGroup::from_curve_name(Nid::X9_62_PRIME239V3).unwrap();
        assert_eq!(g.is_on_curve(&group2, &mut ctx).unwrap(), false);
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -28,9 +28,15 @@
//! Additionally, it will use `pkg-config` on Unix-like systems to find the system installation.
//!
//! ```not_rust
//! # macOS
//! # macOS (Homebrew)
//! $ brew install openssl@1.1
//!
//! # macOS (MacPorts)
//! $ sudo port install openssl
//!
//! # macOS (pkgsrc)
//! $ sudo pkgin install openssl
//!
//! # Arch Linux
//! $ sudo pacman -S pkg-config openssl
//!
+1 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ impl DerefMut for SslAcceptorBuilder {

cfg_if! {
    if #[cfg(ossl110)] {
        #[allow(clippy::unnecessary_wraps)]
        fn setup_curves(_: &mut SslContextBuilder) -> Result<(), ErrorStack> {
            Ok(())
        }