MCPcopy Create free account
hub / github.com/RustPython/RustPython / enum_certificates

Function enum_certificates

crates/stdlib/src/ssl.rs:4971–5022  ·  view source on GitHub ↗
(
        store_name: PyUtf8StrRef,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

4969 #[cfg(windows)]
4970 #[pyfunction]
4971 fn enum_certificates(
4972 store_name: PyUtf8StrRef,
4973 vm: &VirtualMachine,
4974 ) -> PyResult<Vec<PyObjectRef>> {
4975 use schannel::{RawPointer, cert_context::ValidUses, cert_store::CertStore};
4976 use windows_sys::Win32::Security::Cryptography;
4977
4978 let store_name_str = store_name.as_str();
4979
4980 // Try both Current User and Local Machine stores
4981 let open_fns = [CertStore::open_current_user, CertStore::open_local_machine];
4982 let stores = open_fns
4983 .iter()
4984 .filter_map(|open| open(store_name_str).ok())
4985 .collect::<Vec<_>>();
4986
4987 // If no stores could be opened, raise OSError
4988 if stores.is_empty() {
4989 return Err(vm.new_os_error(format!(
4990 "failed to open certificate store {:?}",
4991 store_name_str
4992 )));
4993 }
4994
4995 let certs = stores.iter().flat_map(|s| s.certs()).map(|c| {
4996 let cert = vm.ctx.new_bytes(c.to_der().to_owned());
4997 let enc_type = unsafe {
4998 let ptr = c.as_ptr() as *const Cryptography::CERT_CONTEXT;
4999 (*ptr).dwCertEncodingType
5000 };
5001 let enc_type = match enc_type {
5002 Cryptography::X509_ASN_ENCODING => vm.new_pyobj("x509_asn"),
5003 Cryptography::PKCS_7_ASN_ENCODING => vm.new_pyobj("pkcs_7_asn"),
5004 other => vm.new_pyobj(other),
5005 };
5006 let usage: PyObjectRef = match c.valid_uses() {
5007 Ok(ValidUses::All) => vm.ctx.new_bool(true).into(),
5008 Ok(ValidUses::Oids(oids)) => {
5009 match crate::builtins::PyFrozenSet::from_iter(
5010 vm,
5011 oids.into_iter().map(|oid| vm.ctx.new_str(oid).into()),
5012 ) {
5013 Ok(set) => set.into_ref(&vm.ctx).into(),
5014 Err(_) => vm.ctx.new_bool(true).into(),
5015 }
5016 }
5017 Err(_) => vm.ctx.new_bool(true).into(),
5018 };
5019 Ok(vm.new_tuple((cert, enc_type, usage)).into())
5020 });
5021 certs.collect::<PyResult<Vec<_>>>()
5022 }
5023
5024 #[cfg(windows)]
5025 #[pyfunction]

Callers 1

Calls 15

okMethod · 0.80
new_os_errorMethod · 0.80
new_pyobjMethod · 0.80
openFunction · 0.50
ErrClass · 0.50
as_strMethod · 0.45
iterMethod · 0.45
is_emptyMethod · 0.45
mapMethod · 0.45
new_bytesMethod · 0.45
to_ownedMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected