@@ -31,20 +31,6 @@ cfg_select! {
3131
3232pub ( 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 {
5541mod _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
0 commit comments