(
store_name: PyUtf8StrRef,
vm: &VirtualMachine,
)
| 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] |
no test coverage detected