Skip to content

Commit 7bb3585

Browse files
committed
openssl: use host_env for CA paths and file checks
Replace openssl-probe and local std::fs checks with host_env native_certs and fs. Vendored OpenSSL loads system CA paths from host_env; OpenSSL's own defaults stay for a system lib. Assisted-by: Grok:grok-4.6
1 parent caad2ae commit 7bb3585

6 files changed

Lines changed: 56 additions & 81 deletions

File tree

‎Cargo.lock‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ num_cpus = "1.17.0"
258258
num_enum = { version = "0.7", default-features = false }
259259
openssl = "0.10.80"
260260
openssl-sys = "0.9.110"
261-
openssl-probe = "0.2.1"
262261
optional = "0.5"
263262
parking_lot = "0.12.3"
264263
paste = "1.0.15"

‎crates/stdlib/Cargo.toml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ sqlite = ["dep:libsqlite3-sys"]
1919
# SSL backends
2020
ssl = ["host_env"]
2121
ssl-rustls = ["__ssl-rustls", "rustls/custom-provider"]
22-
ssl-openssl = ["ssl", "openssl", "openssl-sys", "foreign-types-shared", "openssl-probe"]
22+
ssl-openssl = ["ssl", "openssl", "openssl-sys", "foreign-types-shared", "rustpython-host_env/native-certs"]
2323
ssl-openssl-vendor = ["ssl-openssl", "openssl/vendored"]
2424
tkinter = ["dep:tk-sys", "dep:tcl-sys", "dep:widestring"]
2525
flame-it = ["flame"]
@@ -85,7 +85,6 @@ uuid = { workspace = true, features = ["v1"] }
8585
# OpenSSL dependencies (optional, for ssl-openssl feature)
8686
openssl = { workspace = true, optional = true }
8787
openssl-sys = { workspace = true, optional = true }
88-
openssl-probe = { workspace = true, optional = true }
8988
foreign-types-shared = { workspace = true, optional = true }
9089

9190
# Rustls dependencies (optional, for ssl-rustls feature)

‎crates/stdlib/build.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ fn main() {
2828

2929
#[cfg(feature = "ssl-openssl")]
3030
{
31+
// Cargo `DEP_*` metadata from openssl-sys, not process host environment.
3132
#[allow(
33+
clippy::disallowed_methods,
3234
clippy::unusual_byte_groupings,
33-
reason = "OpenSSL version number is parsed with grouped hex fields"
35+
reason = "build scripts read Cargo DEP_* vars; OpenSSL version hex is grouped by field"
3436
)]
3537
if let Ok(v) = std::env::var("DEP_OPENSSL_VERSION_NUMBER") {
3638
println!("cargo:rustc-env=OPENSSL_API_VERSION={v}");
@@ -42,13 +44,21 @@ fn main() {
4244
}
4345
}
4446
}
47+
#[allow(
48+
clippy::disallowed_methods,
49+
reason = "build scripts read Cargo DEP_* vars"
50+
)]
4551
if let Ok(v) = std::env::var("DEP_OPENSSL_CONF") {
4652
for conf in v.split(',') {
4753
println!("cargo:rustc-cfg=osslconf=\"{conf}\"");
4854
}
4955
}
5056
// it's possible for openssl-sys to link against the system openssl under certain conditions,
5157
// so let the ssl module know to only perform a probe if we're actually vendored
58+
#[allow(
59+
clippy::disallowed_methods,
60+
reason = "build scripts read Cargo DEP_* vars"
61+
)]
5262
if std::env::var("DEP_OPENSSL_VENDORED").is_ok_and(|s| s == "1") {
5363
println!("cargo::rustc-cfg=openssl_vendored")
5464
}

‎crates/stdlib/src/openssl.rs‎

Lines changed: 43 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ cfg_select! {
3131

3232
pub(crate) use _ssl::module_def;
3333

34-
use openssl_probe::ProbeResult;
35-
use rustpython_common::lock::LazyLock;
36-
37-
// define our own copy of ProbeResult so we can handle the vendor case
38-
// easily, without having to have a bunch of cfgs
39-
static PROBE: LazyLock<ProbeResult> = cfg_select! {
40-
openssl_vendored => LazyLock::new(openssl_probe::probe),
41-
_ => LazyLock::new(|| ProbeResult { cert_file: None, cert_dir: vec![] })
42-
};
43-
44-
fn probe() -> &'static ProbeResult {
45-
&PROBE
46-
}
47-
4834
#[allow(non_upper_case_globals)]
4935
#[pymodule(with(
5036
cert::ssl_cert,
@@ -55,7 +41,7 @@ fn probe() -> &'static ProbeResult {
5541
mod _ssl {
5642
use core::hint::cold_path;
5743

58-
use super::{bio, probe};
44+
use super::bio;
5945

6046
// Import error types and helpers used in this module (others are exposed via pymodule(with(...)))
6147
use super::ssl_error::{
@@ -102,19 +88,14 @@ mod _ssl {
10288
use core::{ffi::CStr, fmt};
10389
use std::{
10490
io::{Read, Write},
105-
path::{Path, PathBuf},
91+
path::PathBuf,
10692
time::Instant,
10793
};
10894

10995
// Import certificate types from parent module
11096
use super::cert::{self, cert_to_certificate, cert_to_py};
11197

11298
pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py<PyModule>) -> PyResult<()> {
113-
// if openssl is vendored, it doesn't know the locations
114-
// of system certificates - cache the probe result now.
115-
#[cfg(openssl_vendored)]
116-
rustpython_common::lock::LazyLock::force(&super::PROBE);
117-
11899
__module_exec(vm, module);
119100
Ok(())
120101
}
@@ -440,42 +421,34 @@ mod _ssl {
440421
.and_then(|obj| obj2py(obj, vm))
441422
}
442423

443-
// Lazily compute and cache cert file/dir paths
444-
static CERT_PATHS: LazyLock<(PathBuf, PathBuf)> = LazyLock::new(|| {
445-
fn path_from_cstr(c: &CStr) -> PathBuf {
446-
#[cfg(unix)]
447-
{
448-
use std::os::unix::ffi::OsStrExt;
449-
std::ffi::OsStr::from_bytes(c.to_bytes()).into()
450-
}
451-
#[cfg(windows)]
452-
{
453-
// Use lossy conversion for potential non-UTF8
454-
PathBuf::from(c.to_string_lossy().as_ref())
455-
}
424+
fn path_from_cstr(c: &CStr) -> PathBuf {
425+
#[cfg(unix)]
426+
{
427+
use std::os::unix::ffi::OsStrExt;
428+
std::ffi::OsStr::from_bytes(c.to_bytes()).into()
456429
}
430+
#[cfg(windows)]
431+
{
432+
PathBuf::from(c.to_string_lossy().as_ref())
433+
}
434+
}
457435

458-
let probe = probe();
459-
let cert_file = probe
460-
.cert_file
461-
.as_ref()
462-
.map(PathBuf::from)
463-
.unwrap_or_else(|| {
464-
path_from_cstr(unsafe { CStr::from_ptr(sys::X509_get_default_cert_file()) })
465-
});
466-
let cert_dir = probe
467-
.cert_dir
468-
.first()
469-
.map(PathBuf::from)
470-
.unwrap_or_else(|| {
471-
path_from_cstr(unsafe { CStr::from_ptr(sys::X509_get_default_cert_dir()) })
472-
});
473-
(cert_file, cert_dir)
474-
});
436+
fn openssl_default_cert_paths() -> (PathBuf, PathBuf) {
437+
(
438+
path_from_cstr(unsafe { CStr::from_ptr(sys::X509_get_default_cert_file()) }),
439+
path_from_cstr(unsafe { CStr::from_ptr(sys::X509_get_default_cert_dir()) }),
440+
)
441+
}
475442

476-
fn get_cert_file_dir() -> (&'static Path, &'static Path) {
477-
let (cert_file, cert_dir) = &*CERT_PATHS;
478-
(cert_file.as_path(), cert_dir.as_path())
443+
fn get_cert_file_dir() -> (PathBuf, PathBuf) {
444+
cfg_select! {
445+
all(openssl_vendored, not(target_arch = "wasm32")) => {
446+
let (cert_file, cert_dir) =
447+
rustpython_host_env::native_certs::default_verify_paths();
448+
(PathBuf::from(cert_file), PathBuf::from(cert_dir))
449+
}
450+
_ => openssl_default_cert_paths(),
451+
}
479452
}
480453

481454
// Lazily compute and cache cert environment variable names
@@ -495,8 +468,8 @@ mod _ssl {
495468
) -> PyResult<(&'static str, PyObjectRef, &'static str, PyObjectRef)> {
496469
let (cert_file_env, cert_dir_env) = &*CERT_ENV_NAMES;
497470
let (cert_file, cert_dir) = get_cert_file_dir();
498-
let cert_file = OsPath::new_str(cert_file).filename(vm);
499-
let cert_dir = OsPath::new_str(cert_dir).filename(vm);
471+
let cert_file = OsPath::new_str(cert_file.as_path()).filename(vm);
472+
let cert_dir = OsPath::new_str(cert_dir.as_path()).filename(vm);
500473
Ok((
501474
cert_file_env.as_str(),
502475
cert_file,
@@ -1419,7 +1392,7 @@ mod _ssl {
14191392
openssl_vendored => {
14201393
let (cert_file, cert_dir) = get_cert_file_dir();
14211394
self.builder()
1422-
.load_verify_locations(Some(cert_file), Some(cert_dir))
1395+
.load_verify_locations(Some(cert_file.as_path()), Some(cert_dir.as_path()))
14231396
.map_err(|e| convert_openssl_error(vm, e))
14241397
}
14251398
_ => {
@@ -1595,7 +1568,7 @@ mod _ssl {
15951568
let capath_path = args.capath.map(|p| p.to_path_buf(vm)).transpose()?;
15961569
// Check file/directory existence before calling OpenSSL to get proper errno
15971570
if let Some(ref path) = cafile_path
1598-
&& !path.exists()
1571+
&& !rustpython_host_env::fs::exists(path)
15991572
{
16001573
return Err(vm
16011574
.new_os_subtype_error(
@@ -1606,7 +1579,7 @@ mod _ssl {
16061579
.upcast());
16071580
}
16081581
if let Some(ref path) = capath_path
1609-
&& !path.exists()
1582+
&& !rustpython_host_env::fs::exists(path)
16101583
{
16111584
return Err(vm
16121585
.new_os_subtype_error(
@@ -1915,7 +1888,7 @@ mod _ssl {
19151888
let cert_path = certfile.to_path_buf(vm)?;
19161889

19171890
// Check file existence before calling OpenSSL to get proper errno
1918-
if !cert_path.exists() {
1891+
if !rustpython_host_env::fs::exists(&cert_path) {
19191892
return Err(vm
19201893
.new_os_subtype_error(
19211894
vm.ctx.exceptions.file_not_found_error.to_owned(),
@@ -1925,7 +1898,7 @@ mod _ssl {
19251898
.upcast());
19261899
}
19271900
if let Some(ref kp) = key_path
1928-
&& !kp.exists()
1901+
&& !rustpython_host_env::fs::exists(kp)
19291902
{
19301903
return Err(vm
19311904
.new_os_subtype_error(
@@ -4110,11 +4083,8 @@ mod _ssl {
41104083
ssl::SslContextBuilder,
41114084
x509::{X509, store::X509StoreBuilder},
41124085
};
4113-
use std::{
4114-
fs::{File, read_dir},
4115-
io::Read,
4116-
path::Path,
4117-
};
4086+
use rustpython_host_env::fs;
4087+
use std::path::Path;
41184088

41194089
static CERT_DIR: &'static str = "/system/etc/security/cacerts";
41204090

@@ -4123,7 +4093,7 @@ mod _ssl {
41234093
b: &mut SslContextBuilder,
41244094
) -> Result<(), PyBaseExceptionRef> {
41254095
let root = Path::new(CERT_DIR);
4126-
if !root.is_dir() {
4096+
if !fs::is_dir(root) {
41274097
return Err(vm
41284098
.new_os_subtype_error(
41294099
vm.ctx.exceptions.file_not_found_error.to_owned(),
@@ -4134,23 +4104,21 @@ mod _ssl {
41344104
}
41354105

41364106
let mut combined_pem = String::new();
4137-
let entries = read_dir(root)
4107+
let entries = fs::read_dir(root)
41384108
.map_err(|err| vm.new_os_error(format!("read cert root: {}", err)))?;
41394109
for entry in entries {
41404110
let entry =
41414111
entry.map_err(|err| vm.new_os_error(format!("iter cert root: {}", err)))?;
41424112

41434113
let path = entry.path();
4144-
if !path.is_file() {
4114+
if !fs::is_file(&path) {
41454115
continue;
41464116
}
41474117

4148-
File::open(&path)
4149-
.and_then(|mut file| file.read_to_string(&mut combined_pem))
4150-
.map_err(|err| {
4151-
vm.new_os_error(format!("open cert file {}: {}", path.display(), err))
4152-
})?;
4153-
4118+
let pem = fs::read_to_string(&path).map_err(|err| {
4119+
vm.new_os_error(format!("open cert file {}: {}", path.display(), err))
4120+
})?;
4121+
combined_pem.push_str(&pem);
41544122
combined_pem.push('\n');
41554123
}
41564124

‎crates/stdlib/src/ssl.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ mod _ssl {
11201120
/// Helper: Load system certificates using rustls-native-certs
11211121
///
11221122
/// This uses platform-specific methods:
1123-
/// - Linux: openssl-probe to find certificate files
1123+
/// - Linux: host CA file/directory paths
11241124
/// - macOS: Keychain API
11251125
/// - Windows: System certificate store (ROOT + CA stores)
11261126
fn load_system_certificates(

0 commit comments

Comments
 (0)