Skip to content

Commit 10aa120

Browse files
committed
ssl: discover default certificate paths in host_env
Keep host certificate path probing with the native certificate provider, select the first available OpenSSL-style file and directory, and leave environment overrides to ssl.py. This also gives Android string defaults instead of the previous None values. Co-authored-by: Ivan Mironov <[email protected]> Assisted-by: OpenAI Codex: GPT-5
1 parent 0b8fcb4 commit 10aa120

3 files changed

Lines changed: 57 additions & 25 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,6 @@ def test_dealloc_warn(self):
762762
support.gc_collect()
763763
self.assertIn(r, str(cm.warning.args[0]))
764764

765-
@unittest.expectedFailureIf(sys.platform == "android", "TODO: RUSTPYTHON; TypeError: path should be string, bytes, os.PathLike or integer, not NoneType")
766765
def test_get_default_verify_paths(self):
767766
paths = ssl.get_default_verify_paths()
768767
self.assertEqual(len(paths), 6)

‎crates/host_env/src/native_certs.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ pub struct LoadResult {
55
pub errors: Vec<String>,
66
}
77

8+
const DEFAULT_CERT_FILES: &[&str] = &[
9+
"/etc/ssl/certs/ca-certificates.crt",
10+
"/etc/pki/tls/certs/ca-bundle.crt",
11+
"/etc/ssl/ca-bundle.pem",
12+
"/etc/pki/tls/cacert.pem",
13+
"/etc/ssl/cert.pem",
14+
"/usr/local/share/certs/ca-root-nss.crt",
15+
"/usr/local/etc/openssl/cert.pem",
16+
];
17+
18+
const DEFAULT_CERT_DIRS: &[&str] = &[
19+
"/etc/ssl/certs",
20+
"/etc/pki/tls/certs",
21+
"/system/etc/security/cacerts",
22+
"/usr/local/share/certs",
23+
];
24+
25+
fn first_existing<'a>(candidates: &'a [&'a str], exists: impl Fn(&str) -> bool) -> Option<&'a str> {
26+
candidates.iter().copied().find(|path| exists(path))
27+
}
28+
29+
/// Return the host's first available OpenSSL-style certificate file and directory.
30+
/// Environment overrides are intentionally left to Python's `ssl` module.
31+
pub fn default_verify_paths() -> (String, String) {
32+
if cfg!(windows) {
33+
return (String::new(), String::new());
34+
}
35+
36+
let cafile = first_existing(DEFAULT_CERT_FILES, |path| crate::fs::is_file(path))
37+
.unwrap_or("/etc/ssl/cert.pem")
38+
.to_owned();
39+
let capath = first_existing(DEFAULT_CERT_DIRS, |path| crate::fs::is_dir(path))
40+
.unwrap_or("/etc/ssl/certs")
41+
.to_owned();
42+
(cafile, capath)
43+
}
44+
845
pub fn load() -> LoadResult {
946
let result = rustls_native_certs::load_native_certs();
1047
LoadResult {
@@ -20,3 +57,18 @@ pub fn load() -> LoadResult {
2057
.collect(),
2158
}
2259
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::first_existing;
64+
65+
#[test]
66+
fn first_existing_path_preserves_candidate_priority() {
67+
let candidates = ["missing", "first", "second"];
68+
assert_eq!(
69+
first_existing(&candidates, |path| path == "first" || path == "second"),
70+
Some("first")
71+
);
72+
assert_eq!(first_existing(&candidates, |_| false), None);
73+
}
74+
}

‎crates/stdlib/src/ssl.rs‎

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4867,34 +4867,15 @@ mod _ssl {
48674867

48684868
#[pyfunction]
48694869
fn get_default_verify_paths(vm: &VirtualMachine) -> PyObjectRef {
4870-
// Return default certificate paths as a tuple
48714870
// Lib/ssl.py expects: (openssl_cafile_env, openssl_cafile, openssl_capath_env, openssl_capath)
4872-
// parts[0] = environment variable name for cafile
4873-
// parts[1] = default cafile path
4874-
// parts[2] = environment variable name for capath
4875-
// parts[3] = default capath path
4876-
4877-
// Common default paths for different platforms
4878-
// These match the first candidates that rustls-native-certs/openssl-probe checks
4879-
let (default_cafile, default_capath): (Option<&str>, Option<&str>) = cfg_select! {
4880-
// macOS primarily uses Keychain API, but provides fallback paths
4881-
// for compatibility and when Keychain access fails
4882-
target_os = "macos" => (Some("/etc/ssl/cert.pem"), Some("/etc/ssl/certs")),
4883-
// Linux: matches openssl-probe's first candidate (/etc/ssl/cert.pem)
4884-
// openssl-probe checks multiple locations at runtime, but we return
4885-
// OpenSSL's compile-time default
4886-
target_os = "linux" => (Some("/etc/ssl/cert.pem"), Some("/etc/ssl/certs")),
4887-
// Windows uses certificate store, not file paths
4888-
// Return empty strings to avoid None being passed to os.path.isfile()
4889-
windows => (Some(""), Some("")),
4890-
_ => (None, None),
4891-
};
4871+
let (default_cafile, default_capath) =
4872+
rustpython_host_env::native_certs::default_verify_paths();
48924873

48934874
let tuple = vm.ctx.new_tuple(vec![
48944875
vm.ctx.new_str("SSL_CERT_FILE").into(), // openssl_cafile_env
4895-
default_cafile.map_or_else(|| vm.ctx.none(), |s| vm.ctx.new_str(s).into()), // openssl_cafile
4896-
vm.ctx.new_str("SSL_CERT_DIR").into(), // openssl_capath_env
4897-
default_capath.map_or_else(|| vm.ctx.none(), |s| vm.ctx.new_str(s).into()), // openssl_capath
4876+
vm.ctx.new_str(default_cafile).into(), // openssl_cafile
4877+
vm.ctx.new_str("SSL_CERT_DIR").into(), // openssl_capath_env
4878+
vm.ctx.new_str(default_capath).into(), // openssl_capath
48984879
]);
48994880

49004881
tuple.into()

0 commit comments

Comments
 (0)