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::keychain::SecKeychain;
24    use std::fs::File;
25    use std::io::prelude::*;
26    use std::path::Path;
27
28    #[must_use]
29    pub(crate) fn identity(dir: &Path) -> SecIdentity {
30        // FIXME https://github.com/rust-lang/rust/issues/30018
31        let keychain = keychain(dir);
32        let mut items = p!(ItemSearchOptions::new()
33            .class(ItemClass::identity())
34            .keychains(&[keychain])
35            .search());
36        match items.pop().unwrap() {
37            SearchResult::Ref(Reference::Identity(identity)) => identity,
38            _ => panic!("expected identity"),
39        }
40    }
41
42    #[must_use]
43    pub(crate) fn keychain(dir: &Path) -> SecKeychain {
44        let path = dir.join("server.keychain");
45        let mut file = p!(File::create(&path));
46        p!(file.write_all(include_bytes!("../../../test/server.keychain")));
47        drop(file);
48
49        let mut keychain = p!(SecKeychain::open(&path));
50        p!(keychain.unlock(Some("password123")));
51        keychain
52    }
53}