Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): add case-insensitive and subject item search options #202

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions security-framework-sys/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern "C" {
pub static kSecMatchLimitAll: CFStringRef;

pub static kSecMatchTrustedOnly: CFStringRef;
pub static kSecMatchCaseInsensitive: CFStringRef;
pub static kSecMatchSubjectWholeString: CFStringRef;

pub static kSecReturnData: CFStringRef;
pub static kSecReturnAttributes: CFStringRef;
Expand Down
30 changes: 30 additions & 0 deletions security-framework/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub struct ItemSearchOptions {
keychains: Option<CFArray<SecKeychain>>,
#[cfg(not(target_os = "macos"))]
keychains: Option<CFArray<CFType>>,
case_insensitive: Option<bool>,
class: Option<ItemClass>,
key_class: Option<KeyClass>,
load_refs: bool,
Expand All @@ -140,6 +141,7 @@ pub struct ItemSearchOptions {
trusted_only: Option<bool>,
label: Option<CFString>,
service: Option<CFString>,
subject: Option<CFString>,
account: Option<CFString>,
access_group: Option<CFString>,
pub_key_hash: Option<CFData>,
Expand Down Expand Up @@ -170,6 +172,13 @@ impl ItemSearchOptions {
self
}

/// Whether search for an item should be case insensitive or not.
#[inline(always)]
pub fn case_insensitive(&mut self, case_insensitive: Option<bool>) -> &mut Self {
self.case_insensitive = case_insensitive;
self
}

/// Search only for keys of the specified class. Also sets self.class to
/// `ItemClass::key()`.
#[inline(always)]
Expand Down Expand Up @@ -232,6 +241,13 @@ impl ItemSearchOptions {
self.service = Some(CFString::new(service));
self
}

/// Search for an item with exactly the given subject.
#[inline(always)]
pub fn subject(&mut self, subject: &str) -> &mut Self {
self.subject = Some(CFString::new(subject));
self
}

/// Search for an item with the given account.
#[inline(always)]
Expand Down Expand Up @@ -291,6 +307,13 @@ impl ItemSearchOptions {
params.push((CFString::wrap_under_get_rule(kSecClass), class.to_value()));
}

if let Some(case_insensitive) = self.case_insensitive {
params.push((
CFString::wrap_under_get_rule(kSecMatchCaseInsensitive),
CFBoolean::from(case_insensitive).as_CFType()
));
}

if let Some(key_class) = self.key_class {
params.push((CFString::wrap_under_get_rule(kSecAttrKeyClass), key_class.to_value()));
}
Expand Down Expand Up @@ -343,6 +366,13 @@ impl ItemSearchOptions {
service.as_CFType(),
));
}

if let Some(ref subject) = self.subject {
params.push((
CFString::wrap_under_get_rule(kSecMatchSubjectWholeString),
subject.as_CFType(),
));
}

if let Some(ref account) = self.account {
params.push((
Expand Down
Loading