it was suggested to look at
<bsm/libbsm.h>
. However this seems to be Objective-C and deprecated ?
The libbsm API is neither in Objective-C nor deprecated. You can access it from Swift like you’d access any other C API:
If the API is modularised, import that.
If not, use a bridging header.
It turns out that libbsm is modularised, so you can just go
import Darwin.bsm
.
I have gathered that I should use
SecCodeCopyGuestWithAttributes
with the flag
kSecGuestAttributeAudit
.
Correct. However, you seem to be off in the weeds with regards calling that. Pasted in below is some code looks up the bundle ID based on the audit token.
IMPORTANT If you’re building a security product, basing security decisions on the bundle ID is unwise. While the bundle ID is sealed by the code signature, there’s nothing stopping folks from spoofing it. For example, anyone can build an app with a bundle ID of
com.apple.finder
. A better approach is to use the code’s
designated identifier
designated requirement. This typically embeds both the code signing identifier (more or less equivalent to the bundle ID) and information about who signed the code (for third-party code, this is the Team ID).
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
func bundleIDForAuditToken(_ tokenData: Data) -> String? {
// Get a code reference.
var codeQ: SecCode? = nil
var err = SecCodeCopyGuestWithAttributes(nil, [
kSecGuestAttributeAudit: tokenData
] as NSDictionary, [], &codeQ)
guard err == errSecSuccess else {
return nil
}
let code = codeQ!
// Convert that to a static code.
var staticCodeQ: SecStaticCode? = nil
err = SecCodeCopyStaticCode(code, [], &staticCodeQ)
guard err == errSecSuccess else {
return nil
}
let staticCode = staticCodeQ!
// Get code signing information about that.
var infoQ: CFDictionary? = nil
err = SecCodeCopySigningInformation(staticCode, [], &infoQ)
guard err == errSecSuccess else {
return nil
}
let info = infoQ! as! [String:Any]
// Extract the bundle ID from that.
guard
let plist = info[kSecCodeInfoPList as String] as? [String:Any],
let bundleID = plist[kCFBundleIdentifierKey as String] as? String
else {
return nil
}
return bundleID
}