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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use security_framework_sys::import_export::*;
use core_foundation::string::CFString;
use core_foundation::base::{TCFType, CFType};
use core_foundation::data::CFData;
use core_foundation::dictionary::CFDictionary;
use core_foundation::array::CFArray;
use std::ptr;
#[cfg(target_os = "macos")]
use access::SecAccess;
#[cfg(target_os = "macos")]
use keychain::SecKeychain;
use trust::SecTrust;
use certificate::SecCertificate;
use identity::SecIdentity;
use base::Result;
use cvt;
pub struct ImportedIdentity {
pub label: String,
pub key_id: Vec<u8>,
pub trust: SecTrust,
pub cert_chain: Vec<SecCertificate>,
pub identity: SecIdentity,
_p: (),
}
#[derive(Default)]
pub struct Pkcs12ImportOptions {
passphrase: Option<CFString>,
#[cfg(target_os = "macos")]
keychain: Option<SecKeychain>,
#[cfg(target_os = "macos")]
access: Option<SecAccess>,
}
#[cfg(target_os = "macos")]
impl ::Pkcs12ImportOptionsInternals for Pkcs12ImportOptions {
fn keychain(&mut self, keychain: SecKeychain) -> &mut Self {
self.keychain = Some(keychain);
self
}
fn access(&mut self, access: SecAccess) -> &mut Self {
self.access = Some(access);
self
}
}
impl Pkcs12ImportOptions {
pub fn new() -> Pkcs12ImportOptions {
Self::default()
}
pub fn passphrase(&mut self, passphrase: &str) -> &mut Self {
self.passphrase = Some(CFString::new(passphrase));
self
}
#[cfg(target_os = "macos")]
pub fn keychain(&mut self, keychain: SecKeychain) -> &mut Self {
self.keychain = Some(keychain);
self
}
#[cfg(target_os = "macos")]
pub fn access(&mut self, access: SecAccess) -> &mut Self {
self.access = Some(access);
self
}
pub fn import(&self, pkcs12_data: &[u8]) -> Result<Vec<ImportedIdentity>> {
unsafe {
let pkcs12_data = CFData::from_buffer(pkcs12_data);
let mut options = vec![];
if let Some(ref passphrase) = self.passphrase {
options.push((CFString::wrap_under_get_rule(kSecImportExportPassphrase),
passphrase.as_CFType()));
}
self.import_setup(&mut options);
let options = CFDictionary::from_CFType_pairs(&options);
let mut raw_items = ptr::null();
try!(cvt(SecPKCS12Import(pkcs12_data.as_concrete_TypeRef(),
options.as_concrete_TypeRef(),
&mut raw_items)));
let raw_items = CFArray::wrap_under_create_rule(raw_items);
let mut items = vec![];
for raw_item in &raw_items {
let raw_item = CFDictionary::wrap_under_get_rule(raw_item as *mut _);
let label = raw_item.get(kSecImportItemLabel as *const _);
let label = CFString::wrap_under_get_rule(label as *const _).to_string();
let key_id = raw_item.get(kSecImportItemKeyID as *const _);
let key_id = CFData::wrap_under_get_rule(key_id as *const _).to_owned();
let trust = raw_item.get(kSecImportItemTrust as *const _);
let trust = SecTrust::wrap_under_get_rule(trust as usize as *mut _);
let cert_chain = raw_item.get(kSecImportItemCertChain as *const _);
let cert_chain = CFArray::wrap_under_get_rule(cert_chain as *const _);
let cert_chain = cert_chain.iter()
.map(|c| {
SecCertificate::wrap_under_get_rule(c as *mut _)
})
.collect();
let identity = raw_item.get(kSecImportItemIdentity as *const _);
let identity = SecIdentity::wrap_under_get_rule(identity as usize as *mut _);
items.push(ImportedIdentity {
label: label,
key_id: key_id,
trust: trust,
cert_chain: cert_chain,
identity: identity,
_p: (),
});
}
Ok(items)
}
}
#[cfg(target_os = "macos")]
fn import_setup(&self, options: &mut Vec<(CFString, CFType)>) {
unsafe {
if let Some(ref keychain) = self.keychain {
options.push((CFString::wrap_under_get_rule(kSecImportExportKeychain),
keychain.as_CFType()));
}
if let Some(ref access) = self.access {
options.push((CFString::wrap_under_get_rule(kSecImportExportAccess),
access.as_CFType()));
}
}
}
#[cfg(not(target_os = "macos"))]
fn import_setup(&self, _: &mut Vec<(CFString, CFType)>) {}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn missing_passphrase() {
let data = include_bytes!("../test/server.p12");
assert!(Pkcs12ImportOptions::new().import(data).is_err());
}
}