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

Fix macOS Access Groups #538

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion Lib/KeychainAccess/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,18 @@ public final class Keychain {

// MARK:

public class func allKeys(_ itemClass: ItemClass) -> [(String, String)] {
public class func allKeys(_ itemClass: ItemClass, accessGroupsCompatible: Bool = true) -> [(String, String)] {
Copy link
Author

@rivera-ernesto rivera-ernesto Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...so I added an optional accessGroupsCompatible that defaults to true (per Apple documentation Tip)

var query = [String: Any]()
query[Class] = itemClass.rawValue
query[AttributeSynchronizable] = SynchronizableAny
query[MatchLimit] = MatchLimitAll
query[ReturnAttributes] = kCFBooleanTrue

if #available(iOS 13.0, OSX 10.15, watchOS 6.0, tvOS 13.0, *) {
if accessGroupsCompatible {
query[UseDataProtectionKeychain] = true
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is from a class function, so we can't check if options has access groups set or not...

}

var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
Expand Down Expand Up @@ -1299,6 +1305,9 @@ private let UseAuthenticationUIFail = String(kSecUseAuthenticationUIFail)
@available(iOS 9.0, OSX 10.11, watchOS 2.0, tvOS 9.0, *)
private let UseAuthenticationUISkip = String(kSecUseAuthenticationUISkip)

@available(iOS 13.0, OSX 10.15, watchOS 6.0, tvOS 13.0, *)
private let UseDataProtectionKeychain = String(kSecUseDataProtectionKeychain)

#if os(iOS) && !targetEnvironment(macCatalyst)
/** Credential Key Constants */
private let SharedPassword = String(kSecSharedPassword)
Expand Down Expand Up @@ -1361,6 +1370,12 @@ extension Options {
}
}
#endif

if #available(iOS 13.0, OSX 10.15, watchOS 6.0, tvOS 13.0, *) {
if accessGroup != nil {
query[UseDataProtectionKeychain] = true
}
Copy link
Author

@rivera-ernesto rivera-ernesto Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatically set UseDataProtectionKeychain to true when an access group was set. This is required for macOS and doesn't affect other platforms

}

return query
}
Expand Down Expand Up @@ -1403,6 +1418,12 @@ extension Options {
}

attributes[AttributeSynchronizable] = synchronizable ? kCFBooleanTrue : kCFBooleanFalse

if #available(iOS 13.0, OSX 10.15, watchOS 6.0, tvOS 13.0, *) {
if accessGroup != nil {
attributes[UseDataProtectionKeychain] = true
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here

}

return (attributes, nil)
}
Expand Down