security_framework/os/macos/
mod.rs

1//! OSX specific extensions.
2
3pub mod access;
4pub mod certificate;
5pub mod certificate_oids;
6pub mod code_signing;
7pub mod digest_transform;
8pub mod encrypt_transform;
9pub mod identity;
10pub mod import_export;
11pub mod item;
12pub mod key;
13pub mod keychain;
14pub mod keychain_item;
15pub mod passwords;
16pub mod secure_transport;
17pub mod transform;
18
19#[cfg(test)]
20pub(crate) mod test {
21    use crate::identity::SecIdentity;
22    use crate::item::{ItemClass, ItemSearchOptions, Reference, SearchResult};
23    use crate::os::macos::item::ItemSearchOptionsExt;
24    use crate::os::macos::keychain::SecKeychain;
25    use std::fs::File;
26    use std::io::prelude::*;
27    use std::path::Path;
28
29    #[must_use]
30    pub(crate) fn identity(dir: &Path) -> SecIdentity {
31        // FIXME https://github.com/rust-lang/rust/issues/30018
32        let keychain = keychain(dir);
33        let mut items = p!(ItemSearchOptions::new()
34            .class(ItemClass::identity())
35            .keychains(&[keychain])
36            .search());
37        match items.pop().unwrap() {
38            SearchResult::Ref(Reference::Identity(identity)) => identity,
39            _ => panic!("expected identity"),
40        }
41    }
42
43    #[must_use]
44    pub(crate) fn keychain(dir: &Path) -> SecKeychain {
45        let path = dir.join("server.keychain");
46        let mut file = p!(File::create(&path));
47        p!(file.write_all(include_bytes!("../../../test/server.keychain")));
48        drop(file);
49
50        let mut keychain = p!(SecKeychain::open(&path));
51        p!(keychain.unlock(Some("password123")));
52        keychain
53    }
54}