1 //! Encryption key support
2 
3 use core_foundation::base::TCFType;
4 #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
5 use core_foundation::base::ToVoid;
6 #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
7 use core_foundation::data::CFData;
8 #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
9 use core_foundation::dictionary::CFDictionary;
10 #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
11 use core_foundation_sys::error::CFErrorRef;
12 use security_framework_sys::base::SecKeyRef;
13 use security_framework_sys::key::SecKeyGetTypeID;
14 #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
15 use security_framework_sys::key::{SecKeyCopyAttributes, SecKeyCopyExternalRepresentation};
16 use std::fmt;
17 
18 declare_TCFType! {
19     /// A type representing an encryption key.
20     SecKey, SecKeyRef
21 }
22 impl_TCFType!(SecKey, SecKeyRef, SecKeyGetTypeID);
23 
24 unsafe impl Sync for SecKey {}
25 unsafe impl Send for SecKey {}
26 
27 impl SecKey {
28     #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
29     /// Translates to SecKeyCopyAttributes
attributes(&self) -> CFDictionary30     pub fn attributes(&self) -> CFDictionary {
31         let pka = unsafe { SecKeyCopyAttributes(self.to_void() as _) };
32         unsafe { CFDictionary::wrap_under_create_rule(pka) }
33     }
34 
35     #[cfg(any(feature = "OSX_10_12", target_os = "ios"))]
36     /// Translates to SecKeyCopyExternalRepresentation
external_representation(&self) -> Option<CFData>37     pub fn external_representation(&self) -> Option<CFData> {
38         let mut error: CFErrorRef = ::std::ptr::null_mut();
39         let data = unsafe { SecKeyCopyExternalRepresentation(self.to_void() as _, &mut error) };
40         if data == ::std::ptr::null() {
41             return None;
42         }
43         Some(unsafe { CFData::wrap_under_create_rule(data) })
44     }
45 }
46 
47 // FIXME
48 impl fmt::Debug for SecKey {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result49     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
50         write!(fmt, "SecKey")
51     }
52 }
53