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

Use SecKeychainCopyDomainSearchList to find system keychain #3

Closed
Closed
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
44 changes: 42 additions & 2 deletions Sources/Haversack/Security/KeychainFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,27 @@
public static let systemRootCertificates = KeychainFile(at: rootCertificatesKeychainPath)

/// The path to the system keychain.
static let systemKeychainPath = "/Library/Keychains/System.keychain"
static let systemKeychainPath = system.path

/// An instance of ``KeychainFile`` that points at the system keychain
public static let system = KeychainFile(at: systemKeychainPath)
public static let system: KeychainFile = {
let legacySystemKeychainPath = "/Library/Keychains/System.keychain"
var searchList: CFArray?
let status = withUnsafeMutablePointer(to: &searchList) {
SecKeychainCopyDomainSearchList(.system, UnsafeMutablePointer($0))
}

Check warning on line 43 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
guard status == errSecSuccess else {
// attempt to use traditional path, may fail later
return KeychainFile(at: legacySystemKeychainPath)
}

Check warning on line 48 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
guard let searchList = searchList as? [SecKeychain], let systemKeychain = searchList.first else {
return KeychainFile(at: legacySystemKeychainPath)
}

Check warning on line 52 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return KeychainFile(reference: systemKeychain)
}()

/// The full path to the keychain file.
public let path: FilePath
Expand All @@ -54,6 +71,29 @@
self.path = (filePath as NSString).standardizingPath
self.passwordProvider = passwordProvider
}

Check warning on line 74 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/// Create an instance from an existing keychain reference
/// - Parameters:
/// - reference: A reference to a `SecKeychain`.
init(reference: SecKeychain) {
passwordProvider = nil
self.reference = reference

Check warning on line 81 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
var pathLength = UInt32(PATH_MAX)
let pathName = UnsafeMutablePointer<CChar>.allocate(capacity: Int(pathLength))
let status = withUnsafeMutablePointer(to: &pathLength) { pathLength in
SecKeychainGetPath(reference, pathLength, pathName)
}

Check warning on line 87 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if status == errSecSuccess {
path = FileManager().string(withFileSystemRepresentation: pathName, length: Int(pathLength))
} else {
// should never happen
path = ""
}

Check warning on line 94 in Sources/Haversack/Security/KeychainFile.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
pathName.deallocate()
}

/// Try to open and unlock the keychain file, or create the keychain if it does not yet exist.
/// - Throws: A ``HaversackError`` entity
Expand Down