Skip to content

Commit

Permalink
update crypt mech logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wesw-stripe committed Jan 30, 2025
1 parent e2279b5 commit 2c8e3bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 70 deletions.
80 changes: 10 additions & 70 deletions Crypt/Mechanisms/CryptMechanism.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,94 +24,53 @@ class CryptMechanism: NSObject {
// This NSString will be used as the domain for the inter-mechanism context data
let contextCryptDomain: NSString = "com.grahamgilbert.crypt"

// // Key for hint data
// private let needsEncryptionHintKey = "com.grahamgilbert.crypt.needsEncryption"

// Log Crypt Mechanism
private static let log = OSLog(subsystem: "com.grahamgilbert.crypt", category: "CryptMechanism")
// Define a pointer to the MechanismRecord. This will be used to get and set
// all the inter-mechanism data. It is also used to allow or deny the login.
var mechanism: UnsafePointer<MechanismRecord>

// init the class with a MechanismRecord
@objc init(mechanism: UnsafePointer<MechanismRecord>) {
os_log("initWithMechanismRecord", log: CryptMechanism.log, type: .default)
os_log("initWithMechanismRecord", log: mechLog, type: .debug)
self.mechanism = mechanism
}

// Allow the login. End of the mechanism
func allowLogin() {
os_log("called allowLogin", log: CryptMechanism.log, type: .default)
os_log("called allowLogin", log: mechLog, type: .default)
_ = self.mechanism.pointee.fPlugin.pointee.fCallbacks.pointee.SetResult(
mechanism.pointee.fEngine, AuthorizationResult.allow)
}

private func getContextData(key: AuthorizationString) -> NSData? {
os_log("getContextData called", log: CryptMechanism.log, type: .debug)
os_log("getContextData called", log: mechLog, type: .debug)
var value: UnsafePointer<AuthorizationValue>?
let data = withUnsafeMutablePointer(to: &value) { (ptr: UnsafeMutablePointer) -> NSData? in
var flags = AuthorizationContextFlags()
if self.mechanism.pointee.fPlugin.pointee.fCallbacks.pointee.GetContextValue(
self.mechanism.pointee.fEngine, key, &flags, ptr) != errAuthorizationSuccess {
os_log("GetContextValue failed", log: CryptMechanism.log, type: .error)
os_log("GetContextValue failed", log: mechLog, type: .error)
return nil
}
guard let length = ptr.pointee?.pointee.length else {
os_log("length failed to unwrap", log: CryptMechanism.log, type: .error)
os_log("length failed to unwrap", log: mechLog, type: .error)
return nil
}
guard let buffer = ptr.pointee?.pointee.data else {
os_log("data failed to unwrap", log: CryptMechanism.log, type: .error)
os_log("data failed to unwrap", log: mechLog, type: .error)
return nil
}
if length == 0 {
os_log("length is 0", log: CryptMechanism.log, type: .error)
os_log("length is 0", log: mechLog, type: .error)
return nil
}
return NSData.init(bytes: buffer, length: length)
}
os_log("getContextData success", log: CryptMechanism.log, type: .debug)
return data
}

// private func getHintData(key: AuthorizationString) -> NSData? {
// os_log("getHintData called", log: CryptMechanism.log, type: .default)
// var value: UnsafePointer<AuthorizationValue>?
// let data = withUnsafeMutablePointer(to: &value) { (ptr: UnsafeMutablePointer) -> NSData? in
// if self.mechanism.pointee.fPlugin.pointee.fCallbacks.pointee.GetHintValue(
// self.mechanism.pointee.fEngine, key, ptr) != errAuthorizationSuccess {
// os_log("GetHintValue failed", log: CryptMechanism.log, type: .error)
// return nil
// }
// guard let length = ptr.pointee?.pointee.length else {
// os_log("length failed to unwrap", log: CryptMechanism.log, type: .error)
// return nil
// }
// guard let buffer = ptr.pointee?.pointee.data else {
// os_log("data failed to unwrap", log: CryptMechanism.log, type: .error)
// return nil
// }
// if length == 0 {
// os_log("length is 0", log: CryptMechanism.log, type: .error)
// return nil
// }
// return NSData.init(bytes: buffer, length: length)
// }
// os_log("getHintData success", log: CryptMechanism.log, type: .default)
// return data
// }
//
// private func setHintData(key: AuthorizationString, data: NSData) -> Bool {
// os_log("setHintData called", log: CryptMechanism.log, type: .default)
// var value = AuthorizationValue(length: data.length,
// data: UnsafeMutableRawPointer(mutating: data.bytes))
// return (self.mechanism.pointee.fPlugin.pointee.fCallbacks.pointee.SetHintValue(
// self.mechanism.pointee.fEngine, key, &value) != errAuthorizationSuccess)
// }

var username: NSString? {
get {
os_log("Requesting username...", log: CryptMechanism.log, type: .debug)
os_log("Requesting username...", log: mechLog, type: .debug)
guard let data = getContextData(key: kAuthorizationEnvironmentUsername) else {
return nil
}
Expand All @@ -125,7 +84,7 @@ class CryptMechanism: NSObject {

var password: NSString? {
get {
os_log("Requesting password...", log: CryptMechanism.log, type: .debug)
os_log("Requesting password...", log: mechLog, type: .debug)
guard let data = getContextData(key: kAuthorizationEnvironmentPassword) else {
return nil
}
Expand All @@ -139,7 +98,7 @@ class CryptMechanism: NSObject {

var uid: uid_t {
get {
os_log("Requesting uid...", log: CryptMechanism.log, type: .debug)
os_log("Requesting uid...", log: mechLog, type: .debug)
var uid: UInt32 = UInt32.max - 1 // nobody
guard let data = getContextData(key: kAuthorizationEnvironmentUID) else {
return uid
Expand All @@ -148,23 +107,4 @@ class CryptMechanism: NSObject {
return uid
}
}

// var needsEncryption: Bool {
// set {
// os_log("needsEncryption set to %@", log: CryptMechanism.log, type: .default, newValue as CVarArg)
// guard let data = try? NSKeyedArchiver.archivedData(withRootObject: NSNumber.init(value: newValue), requiringSecureCoding: false) else { return }
// _ = setHintData(key: needsEncryptionHintKey, data: data as NSData)
// }
//
// get {
// os_log("Requesting needsEncryption...", log: CryptMechanism.log, type: .default)
// guard let data = getHintData(key: needsEncryptionHintKey) else {
// return false
// }
// guard let value = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSNumber.self, from: data as Data) else {
// return false
// }
// return (value).boolValue
// }
// }
}
1 change: 1 addition & 0 deletions Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ let keychainLog = OSLog(subsystem: cryptBundleID, category: "Keychain")
let filevaultLog = OSLog(subsystem: cryptBundleID, category: "Filevault")
let prefLog = OSLog(subsystem: cryptBundleID, category: "Preferences")
let enablementLog = OSLog(subsystem: cryptBundleID, category: "Enablement")
let mechLog = OSLog(subsystem: cryptBundleID, category: "CryptMechanism")

0 comments on commit 2c8e3bb

Please sign in to comment.