Skip to main content

security/
identity.rs

1use serde_json::Value;
2
3use crate::bridge;
4use crate::certificate::Certificate;
5use crate::error::Result;
6
7#[derive(Debug)]
8pub struct Identity {
9    handle: bridge::Handle,
10}
11
12impl Identity {
13    pub fn import_pkcs12_first(data: &[u8], password: &str) -> Result<Self> {
14        let password = bridge::cstring(password)?;
15        let mut status = 0;
16        let mut error = std::ptr::null_mut();
17        let raw = unsafe {
18            bridge::security_identity_import_pkcs12_first(
19                data.as_ptr().cast(),
20                bridge::len_to_isize(data.len())?,
21                password.as_ptr(),
22                &mut status,
23                &mut error,
24            )
25        };
26        bridge::required_handle("security_identity_import_pkcs12_first", raw, status, error)
27            .map(|handle| Self { handle })
28    }
29
30    pub fn label(&self) -> Result<Option<String>> {
31        let raw = unsafe { bridge::security_identity_copy_label(self.handle.as_ptr()) };
32        bridge::optional_string(raw)
33    }
34
35    pub fn chain_count(&self) -> usize {
36        usize::try_from(unsafe { bridge::security_identity_get_chain_count(self.handle.as_ptr()) })
37            .unwrap_or_default()
38    }
39
40    pub fn certificate(&self) -> Result<Certificate> {
41        let mut status = 0;
42        let mut error = std::ptr::null_mut();
43        let raw = unsafe {
44            bridge::security_identity_copy_certificate(self.handle.as_ptr(), &mut status, &mut error)
45        };
46        bridge::required_handle("security_identity_copy_certificate", raw, status, error)
47            .map(Certificate::from_handle)
48    }
49
50    pub fn private_key_attributes(&self) -> Result<Value> {
51        let mut status = 0;
52        let mut error = std::ptr::null_mut();
53        let raw = unsafe {
54            bridge::security_identity_copy_private_key_attributes(
55                self.handle.as_ptr(),
56                &mut status,
57                &mut error,
58            )
59        };
60        bridge::required_json("security_identity_copy_private_key_attributes", raw, status, error)
61    }
62}