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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use core_foundation::array::CFArray;
use core_foundation::base::{CFType, TCFType};
use core_foundation::boolean::CFBoolean;
use core_foundation::dictionary::CFDictionary;
use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use core_foundation_sys::base::{CFTypeRef, CFGetTypeID, CFRelease};
use security_framework_sys::item::*;
use std::fmt;
use std::ptr;
use base::Result;
use certificate::SecCertificate;
use cvt;
use identity::SecIdentity;
use key::SecKey;
#[cfg(target_os = "macos")]
use keychain::SecKeychain;
#[derive(Debug, Copy, Clone)]
pub enum ItemClass {
GenericPassword,
InternetPassword,
Certificate,
Key,
Identity,
}
impl ItemClass {
fn to_value(&self) -> CFType {
unsafe {
let raw = match *self {
ItemClass::GenericPassword => kSecClassGenericPassword,
ItemClass::InternetPassword => kSecClassInternetPassword,
ItemClass::Certificate => kSecClassCertificate,
ItemClass::Key => kSecClassKey,
ItemClass::Identity => kSecClassIdentity,
};
CFType::wrap_under_get_rule(raw as *const _)
}
}
}
#[derive(Default)]
pub struct ItemSearchOptions {
keychains: Option<CFArray>,
class: Option<ItemClass>,
load_refs: bool,
limit: Option<i64>,
label: Option<CFString>,
}
#[cfg(target_os = "macos")]
impl ::ItemSearchOptionsInternals for ItemSearchOptions {
fn keychains(&mut self, keychains: &[SecKeychain]) -> &mut ItemSearchOptions {
self.keychains = Some(CFArray::from_CFTypes(keychains));
self
}
}
impl ItemSearchOptions {
pub fn new() -> ItemSearchOptions {
ItemSearchOptions::default()
}
pub fn class(&mut self, class: ItemClass) -> &mut ItemSearchOptions {
self.class = Some(class);
self
}
#[cfg(target_os = "macos")]
pub fn keychains(&mut self, keychains: &[SecKeychain]) -> &mut ItemSearchOptions {
self.keychains = Some(CFArray::from_CFTypes(keychains));
self
}
pub fn load_refs(&mut self, load_refs: bool) -> &mut ItemSearchOptions {
self.load_refs = load_refs;
self
}
pub fn limit(&mut self, limit: i64) -> &mut ItemSearchOptions {
self.limit = Some(limit);
self
}
pub fn label(&mut self, label: &str) -> &mut ItemSearchOptions {
self.label = Some(CFString::new(label));
self
}
pub fn search(&self) -> Result<Vec<SearchResult>> {
unsafe {
let mut params = vec![];
if let Some(ref keychains) = self.keychains {
params.push((CFString::wrap_under_get_rule(kSecMatchSearchList),
keychains.as_CFType()));
}
if let Some(class) = self.class {
params.push((CFString::wrap_under_get_rule(kSecClass), class.to_value()));
}
if self.load_refs {
params.push((CFString::wrap_under_get_rule(kSecReturnRef),
CFBoolean::true_value().as_CFType()));
}
if let Some(limit) = self.limit {
params.push((CFString::wrap_under_get_rule(kSecMatchLimit),
CFNumber::from_i64(limit).as_CFType()));
}
if let Some(ref label) = self.label {
params.push((CFString::wrap_under_get_rule(kSecAttrLabel),
label.as_CFType()));
}
let params = CFDictionary::from_CFType_pairs(¶ms);
let mut ret = ptr::null();
try!(cvt(SecItemCopyMatching(params.as_concrete_TypeRef(), &mut ret)));
let type_id = CFGetTypeID(ret);
let mut items = vec![];
if type_id == CFArray::type_id() {
let array = CFArray::wrap_under_create_rule(ret as *mut _);
for item in &array {
items.push(get_item(item as *const _));
}
} else {
items.push(get_item(ret));
CFRelease(ret);
}
Ok(items)
}
}
}
#[cfg(target_os = "macos")]
unsafe fn get_item(item: CFTypeRef) -> SearchResult {
use os::macos::keychain_item::SecKeychainItem;
let type_id = CFGetTypeID(item);
let reference = if type_id == SecCertificate::type_id() {
Reference::Certificate(SecCertificate::wrap_under_get_rule(item as *mut _))
} else if type_id == SecKey::type_id() {
Reference::Key(SecKey::wrap_under_get_rule(item as *mut _))
} else if type_id == SecIdentity::type_id() {
Reference::Identity(SecIdentity::wrap_under_get_rule(item as *mut _))
} else if type_id == SecKeychainItem::type_id() {
Reference::KeychainItem(SecKeychainItem::wrap_under_get_rule(item as *mut _))
} else {
panic!("Got bad type from SecItemCopyMatching: {}", type_id);
};
SearchResult {
reference: Some(reference),
_p: (),
}
}
#[cfg(not(target_os = "macos"))]
unsafe fn get_item(item: CFTypeRef) -> SearchResult {
let type_id = CFGetTypeID(item);
let reference = if type_id == SecCertificate::type_id() {
Reference::Certificate(SecCertificate::wrap_under_get_rule(item as *mut _))
} else if type_id == SecKey::type_id() {
Reference::Key(SecKey::wrap_under_get_rule(item as *mut _))
} else if type_id == SecIdentity::type_id() {
Reference::Identity(SecIdentity::wrap_under_get_rule(item as *mut _))
} else {
panic!("Got bad type from SecItemCopyMatching: {}", type_id);
};
SearchResult {
reference: Some(reference),
_p: (),
}
}
#[derive(Debug)]
pub enum Reference {
Identity(SecIdentity),
Certificate(SecCertificate),
Key(SecKey),
#[cfg(target_os = "macos")]
KeychainItem(::os::macos::keychain_item::SecKeychainItem),
}
pub struct SearchResult {
pub reference: Option<Reference>,
_p: (),
}
impl fmt::Debug for SearchResult {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("SearchResult")
.field("reference", &self.reference)
.finish()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn find_nothing() {
assert!(ItemSearchOptions::new().search().is_err());
}
#[test]
fn limit_two() {
let results = ItemSearchOptions::new()
.class(ItemClass::Certificate)
.limit(2)
.search()
.unwrap();
assert_eq!(results.len(), 2);
}
}