From c0562ba50a949019ed15e9b8feb167f944e2d4e4 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Tue, 17 Nov 2026 20:28:30 -0800 Subject: [PATCH] Allow after first unlock accessibility (#871) * Allow after first unlock * Fix tests * Fix tests --- .../Packages/Resources/Localizable.xcstrings | 22 +++++++++++++++++++ .../CertificateKit/CertificateStore.swift | 3 ++- .../SecretKit/Types/CreationOptions.swift | 5 +++++ .../CryptoKitMigrator.swift | 2 +- .../SecureEnclaveStore.swift | 6 ++--- .../Sources/SettingsKit/SettingsStore.swift | 2 +- .../SmartCardSecretKit/SmartCardStore.swift | 2 +- .../OpenSSHPublicKeyWriterTests.swift | 4 ++-- .../OpenSSHSignatureWriterTests.swift | 2 +- .../AuthenticationHandlerTests.swift | 2 +- .../Tests/SecretAgentKitTests/StubStore.swift | 2 +- .../Tests/SecretKitTests/AnySecretTests.swift | 2 +- .../Preview Content/PreviewStore.swift | 1 + .../Views/Secrets/CreateSecretView.swift | 8 +++++++ 14 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Sources/Packages/Resources/Localizable.xcstrings b/Sources/Packages/Resources/Localizable.xcstrings index b68952a..e266347 100644 --- a/Sources/Packages/Resources/Localizable.xcstrings +++ b/Sources/Packages/Resources/Localizable.xcstrings @@ -6720,6 +6720,28 @@ } } }, + "create_secret_allow_while_locked_description" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Consider enabling this if you run scripts while your Mac is locked." + } + } + } + }, + "create_secret_allow_while_locked_label" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Allow While Locked" + } + } + } + }, "create_secret_biometry_current_warning" : { "extractionState" : "manual", "localizations" : { diff --git a/Sources/Packages/Sources/CertificateKit/CertificateStore.swift b/Sources/Packages/Sources/CertificateKit/CertificateStore.swift index 45d37ec..b694ee4 100644 --- a/Sources/Packages/Sources/CertificateKit/CertificateStore.swift +++ b/Sources/Packages/Sources/CertificateKit/CertificateStore.swift @@ -39,7 +39,8 @@ import SecretKit kSecAttrService: Constants.keyTag, kSecAttrAccount: certificate.id, kSecUseDataProtectionKeychain: true, - kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly, + // Note: Previous versions used kSecAttrAccessibleWhenUnlockedThisDeviceOnly + kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, kSecValueData: certificate.rawData, kSecAttrGeneric: attributes ]) diff --git a/Sources/Packages/Sources/SecretKit/Types/CreationOptions.swift b/Sources/Packages/Sources/SecretKit/Types/CreationOptions.swift index 99ab8f3..b74231c 100644 --- a/Sources/Packages/Sources/SecretKit/Types/CreationOptions.swift +++ b/Sources/Packages/Sources/SecretKit/Types/CreationOptions.swift @@ -8,6 +8,9 @@ public struct Attributes: Sendable, Codable, Hashable { /// The authentication requirements for the key. This is simply a description of the option recorded at creation – modifying it doers not modify the key's authentication requirements. public let authentication: AuthenticationRequirement + /// Whether the key can be used while unlocked. This is only applicable to keys with `notRequired` authentication requirements. + public let allowWhileLocked: Bool + /// The string appended to the end of the SSH Public Key. /// If nil, a default value will be used. public var publicKeyAttribution: String? @@ -15,10 +18,12 @@ public struct Attributes: Sendable, Codable, Hashable { public init( keyType: KeyType, authentication: AuthenticationRequirement, + allowWhileLocked: Bool, publicKeyAttribution: String? = nil ) { self.keyType = keyType self.authentication = authentication + self.allowWhileLocked = allowWhileLocked self.publicKeyAttribution = publicKeyAttribution } diff --git a/Sources/Packages/Sources/SecureEnclaveSecretKit/CryptoKitMigrator.swift b/Sources/Packages/Sources/SecureEnclaveSecretKit/CryptoKitMigrator.swift index 3828a27..3f2470f 100644 --- a/Sources/Packages/Sources/SecureEnclaveSecretKit/CryptoKitMigrator.swift +++ b/Sources/Packages/Sources/SecureEnclaveSecretKit/CryptoKitMigrator.swift @@ -47,7 +47,7 @@ extension SecureEnclave { .contains("DeviceOwnerAuthentication") ? .presenceRequired : .unknown do { let parsed = try CryptoKit.SecureEnclave.P256.Signing.PrivateKey(dataRepresentation: tokenObjectID) - let secret = Secret(id: UUID().uuidString, name: name, publicKey: parsed.publicKey.x963Representation, attributes: Attributes(keyType: .init(algorithm: .ecdsa, size: 256), authentication: auth)) + let secret = Secret(id: UUID().uuidString, name: name, publicKey: parsed.publicKey.x963Representation, attributes: Attributes(keyType: .init(algorithm: .ecdsa, size: 256), authentication: auth, allowWhileLocked: false)) guard !migratedPublicKeys.contains(parsed.publicKey.x963Representation) else { logger.log("Skipping \(name), public key already present. Marking as migrated.") markMigrated(secret: secret, oldID: id) diff --git a/Sources/Packages/Sources/SecureEnclaveSecretKit/SecureEnclaveStore.swift b/Sources/Packages/Sources/SecureEnclaveSecretKit/SecureEnclaveStore.swift index c09f9b5..5735da2 100644 --- a/Sources/Packages/Sources/SecureEnclaveSecretKit/SecureEnclaveStore.swift +++ b/Sources/Packages/Sources/SecureEnclaveSecretKit/SecureEnclaveStore.swift @@ -244,17 +244,17 @@ extension SecureEnclave.Store { /// - Note: Despite the name, the "Data" of the key is _not_ actual key material. This is an opaque data representation that the SEP can manipulate. @discardableResult func saveKey(_ key: Data, name: String, attributes: Attributes) throws -> String { - let attributes = try JSONEncoder().encode(attributes) + let encodedAttributes = try JSONEncoder().encode(attributes) let id = UUID().uuidString let keychainAttributes = KeychainDictionary([ kSecClass: Constants.keyClass, kSecAttrService: Constants.keyTag, kSecUseDataProtectionKeychain: true, - kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly, + kSecAttrAccessible: attributes.allowWhileLocked ? kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly : kSecAttrAccessibleWhenUnlockedThisDeviceOnly, kSecAttrAccount: id, kSecValueData: key, kSecAttrLabel: name, - kSecAttrGeneric: attributes + kSecAttrGeneric: encodedAttributes ]) let status = SecItemAdd(keychainAttributes, nil) if status != errSecSuccess { diff --git a/Sources/Packages/Sources/SettingsKit/SettingsStore.swift b/Sources/Packages/Sources/SettingsKit/SettingsStore.swift index 2f222dc..4c8e9b0 100644 --- a/Sources/Packages/Sources/SettingsKit/SettingsStore.swift +++ b/Sources/Packages/Sources/SettingsKit/SettingsStore.swift @@ -40,7 +40,7 @@ import OSLog kSecAttrService: Constants.keyTag, kSecAttrAccount: String(describing: SettingsKeyType.self), kSecUseDataProtectionKeychain: true, - kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly, + kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, kSecValueData: data, ]) let status = SecItemAdd(keychainAttributes, nil) diff --git a/Sources/Packages/Sources/SmartCardSecretKit/SmartCardStore.swift b/Sources/Packages/Sources/SmartCardSecretKit/SmartCardStore.swift index fd8d63d..66d6cd2 100644 --- a/Sources/Packages/Sources/SmartCardSecretKit/SmartCardStore.swift +++ b/Sources/Packages/Sources/SmartCardSecretKit/SmartCardStore.swift @@ -156,7 +156,7 @@ extension SmartCard.Store { let publicKeySecRef = SecKeyCopyPublicKey(publicKeyRef)! let publicKeyAttributes = SecKeyCopyAttributes(publicKeySecRef) as! [CFString: Any] let publicKey = publicKeyAttributes[kSecValueData] as! Data - let attributes = Attributes(keyType: KeyType(secAttr: algorithmSecAttr, size: keySize)!, authentication: .presenceRequired) + let attributes = Attributes(keyType: KeyType(secAttr: algorithmSecAttr, size: keySize)!, authentication: .presenceRequired, allowWhileLocked: false) let secret = SmartCard.Secret(id: tokenID, name: name, publicKey: publicKey, attributes: attributes) guard signatureAlgorithm(for: secret) != nil else { return nil } return secret diff --git a/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHPublicKeyWriterTests.swift b/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHPublicKeyWriterTests.swift index 806d399..b1ff535 100644 --- a/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHPublicKeyWriterTests.swift +++ b/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHPublicKeyWriterTests.swift @@ -46,8 +46,8 @@ import SSHProtocolKit extension OpenSSHPublicKeyWriterTests { enum Constants { - static let ecdsa256Secret = TestSecret(id: Data(), name: "Test Key (ECDSA 256)", publicKey: Data(base64Encoded: "BOVEjgAA5PHqRgwykjN5qM21uWCHFSY/Sqo5gkHAkn+e1MMQKHOLga7ucB9b3mif33MBid59GRK9GEPVlMiSQwo=")!, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 256), authentication: .notRequired, publicKeyAttribution: "test@example.com")) - static let ecdsa384Secret = TestSecret(id: Data(), name: "Test Key (ECDSA 384)", publicKey: Data(base64Encoded: "BG2MNc/C5OTHFE2tBvbZCVcpOGa8vBMquiTLkH4lwkeqOPxhi+PyYUfQZMTRJNPiTyWPoMBqNiCIFRVv60yPN/AHufHaOgbdTP42EgMlMMImkAjYUEv9DESHTVIs2PW1yQ==")!, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 384), authentication: .notRequired, publicKeyAttribution: "test@example.com")) + static let ecdsa256Secret = TestSecret(id: Data(), name: "Test Key (ECDSA 256)", publicKey: Data(base64Encoded: "BOVEjgAA5PHqRgwykjN5qM21uWCHFSY/Sqo5gkHAkn+e1MMQKHOLga7ucB9b3mif33MBid59GRK9GEPVlMiSQwo=")!, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 256), authentication: .notRequired, allowWhileLocked: true, publicKeyAttribution: "test@example.com")) + static let ecdsa384Secret = TestSecret(id: Data(), name: "Test Key (ECDSA 384)", publicKey: Data(base64Encoded: "BG2MNc/C5OTHFE2tBvbZCVcpOGa8vBMquiTLkH4lwkeqOPxhi+PyYUfQZMTRJNPiTyWPoMBqNiCIFRVv60yPN/AHufHaOgbdTP42EgMlMMImkAjYUEv9DESHTVIs2PW1yQ==")!, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 384), authentication: .notRequired, allowWhileLocked: true, publicKeyAttribution: "test@example.com")) } diff --git a/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHSignatureWriterTests.swift b/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHSignatureWriterTests.swift index 008fc77..0fd0404 100644 --- a/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHSignatureWriterTests.swift +++ b/Sources/Packages/Tests/SSHProtocolKitTests/OpenSSHSignatureWriterTests.swift @@ -51,7 +51,7 @@ private extension OpenSSHSignatureWriterTests { publicKey: Data(repeating: 0x01, count: 65), attributes: Attributes( keyType: KeyType(algorithm: .ecdsa, size: 256), - authentication: .notRequired, + authentication: .notRequired, allowWhileLocked: true, publicKeyAttribution: "test@example.com" ) ) diff --git a/Sources/Packages/Tests/SecretAgentKitTests/AuthenticationHandlerTests.swift b/Sources/Packages/Tests/SecretAgentKitTests/AuthenticationHandlerTests.swift index f79a082..e5ea5fa 100644 --- a/Sources/Packages/Tests/SecretAgentKitTests/AuthenticationHandlerTests.swift +++ b/Sources/Packages/Tests/SecretAgentKitTests/AuthenticationHandlerTests.swift @@ -34,7 +34,7 @@ public struct TestSecret: Secret { init(authenticationRequirement: AuthenticationRequirement, testEvaluationResult: Bool, approval: ApprovalMode = .manual) { self.canceled = false self.testEvaluationResult = testEvaluationResult - self.secret = AnySecret(TestSecret(attributes: .init(keyType: .ecdsa256, authentication: authenticationRequirement))) + self.secret = AnySecret(TestSecret(attributes: .init(keyType: .ecdsa256, authentication: authenticationRequirement, allowWhileLocked: false))) self.approval = approval } diff --git a/Sources/Packages/Tests/SecretAgentKitTests/StubStore.swift b/Sources/Packages/Tests/SecretAgentKitTests/StubStore.swift index 3b3dc17..66f8c0b 100644 --- a/Sources/Packages/Tests/SecretAgentKitTests/StubStore.swift +++ b/Sources/Packages/Tests/SecretAgentKitTests/StubStore.swift @@ -77,7 +77,7 @@ extension Stub { let privateKey: Data init(keySize: Int, publicKey: Data, privateKey: Data) { - self.attributes = Attributes(keyType: .init(algorithm: .ecdsa, size: keySize), authentication: .notRequired, publicKeyAttribution: "ecdsa-\(keySize)@example.com") + self.attributes = Attributes(keyType: .init(algorithm: .ecdsa, size: keySize), authentication: .notRequired, allowWhileLocked: false, publicKeyAttribution: "ecdsa-\(keySize)@example.com") self.publicKey = publicKey self.privateKey = privateKey } diff --git a/Sources/Packages/Tests/SecretKitTests/AnySecretTests.swift b/Sources/Packages/Tests/SecretKitTests/AnySecretTests.swift index b8e4b2b..ac13975 100644 --- a/Sources/Packages/Tests/SecretKitTests/AnySecretTests.swift +++ b/Sources/Packages/Tests/SecretKitTests/AnySecretTests.swift @@ -9,7 +9,7 @@ import Testing @Test func eraser() { let data = Data(UUID().uuidString.utf8) - let secret = SmartCard.Secret(id: data, name: "Name", publicKey: data, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 256), authentication: .notRequired)) + let secret = SmartCard.Secret(id: data, name: "Name", publicKey: data, attributes: Attributes(keyType: KeyType(algorithm: .ecdsa, size: 256), authentication: .notRequired, allowWhileLocked: true)) let erased = AnySecret(secret) #expect(erased.id == secret.id as AnyHashable) #expect(erased.name == secret.name) diff --git a/Sources/Secretive/Preview Content/PreviewStore.swift b/Sources/Secretive/Preview Content/PreviewStore.swift index 5a14b7c..efdcdea 100644 --- a/Sources/Secretive/Preview Content/PreviewStore.swift +++ b/Sources/Secretive/Preview Content/PreviewStore.swift @@ -15,6 +15,7 @@ extension Preview { Attributes( keyType: .init(algorithm: .ecdsa, size: 256), authentication: .presenceRequired, + allowWhileLocked: true, ) } } diff --git a/Sources/Secretive/Views/Secrets/CreateSecretView.swift b/Sources/Secretive/Views/Secrets/CreateSecretView.swift index ca77124..d99bbac 100644 --- a/Sources/Secretive/Views/Secrets/CreateSecretView.swift +++ b/Sources/Secretive/Views/Secrets/CreateSecretView.swift @@ -10,6 +10,7 @@ struct CreateSecretView: View { @State private var name = "" @State private var keyAttribution = "" @State private var authenticationRequirement: AuthenticationRequirement = .presenceRequired + @State private var allowWhileLocked = false @State private var keyType: KeyType? @State var advanced = false @State var errorText: String? @@ -73,6 +74,12 @@ struct CreateSecretView: View { } if advanced { Section { + Toggle(isOn: $allowWhileLocked) { + Text(.createSecretAllowWhileLockedLabel) + Text(.createSecretAllowWhileLockedDescription) + .font(.subheadline) + .foregroundStyle(.secondary) + } VStack { Picker(.createSecretKeyTypeLabel, selection: $keyType) { ForEach(store.supportedKeyTypes.available, id: \.self) { option in @@ -146,6 +153,7 @@ struct CreateSecretView: View { attributes: .init( keyType: keyType!, authentication: authenticationRequirement, + allowWhileLocked: allowWhileLocked, publicKeyAttribution: attribution ) )