1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! OSX specific extensions to certificate functionality.

use core_foundation::base::TCFType;
use core_foundation::string::CFString;
use security_framework_sys::certificate::*;
use std::ptr;

use cvt;
use base::Result;
use certificate::SecCertificate;
use key::SecKey;

/// An extension trait adding OSX specific functionality to `SecCertificate`.
pub trait SecCertificateExt {
    /// Returns the common name associated with the certificate.
    fn common_name(&self) -> Result<String>;

    /// Returns the public key associated with the certificate.
    fn public_key(&self) -> Result<SecKey>;
}

impl SecCertificateExt for SecCertificate {
    fn common_name(&self) -> Result<String> {
        unsafe {
            let mut string = ptr::null();
            try!(cvt(SecCertificateCopyCommonName(self.as_concrete_TypeRef(), &mut string)));
            Ok(CFString::wrap_under_create_rule(string).to_string())
        }
    }

    fn public_key(&self) -> Result<SecKey> {
        unsafe {
            let mut key = ptr::null_mut();
            try!(cvt(SecCertificateCopyPublicKey(self.as_concrete_TypeRef(), &mut key)));
            Ok(SecKey::wrap_under_create_rule(key))
        }
    }
}

#[cfg(test)]
mod test {
    use test::certificate;
    use super::SecCertificateExt;

    #[test]
    fn common_name() {
        let certificate = certificate();
        assert_eq!("foobar.com", p!(certificate.common_name()).to_string());
    }

    #[test]
    fn public_key() {
        let certificate = certificate();
        p!(certificate.public_key());
    }
}