blob: 397620f37b77d6eb7ab1e4332d2f2500d7acd40e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
//
// KeychainAttributes.swift
// MullvadVPN
//
// Created by pronebird on 22/04/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
import Security
extension Keychain {
enum Accessible: RawRepresentable, CaseIterable, KeychainAttributeDecodable, KeychainAttributeEncodable {
case whenPasscodeSetThisDeviceOnly
case whenUnlocked
case whenUnlockedThisDeviceOnly
case afterFirstUnlock
case afterFirstUnlockThisDeviceOnly
var rawValue: CFString {
switch self {
case .whenPasscodeSetThisDeviceOnly:
return kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
case .whenUnlocked:
return kSecAttrAccessibleWhenUnlocked
case .whenUnlockedThisDeviceOnly:
return kSecAttrAccessibleWhenUnlockedThisDeviceOnly
case .afterFirstUnlock:
return kSecAttrAccessibleAfterFirstUnlock
case .afterFirstUnlockThisDeviceOnly:
return kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
}
}
init?(rawValue: CFString) {
let maybeCase = Self.allCases.first { $0.rawValue == rawValue }
if let maybeCase = maybeCase {
self = maybeCase
} else {
return nil
}
}
init?(attributes: [CFString: Any]) {
if let rawValue = attributes[kSecAttrAccessible] as? String {
self.init(rawValue: rawValue as CFString)
} else {
return nil
}
}
func updateKeychainAttributes(in attributes: inout [CFString : Any]) {
attributes[kSecAttrAccessible] = rawValue
}
}
struct Attributes: KeychainAttributeEncodable, KeychainAttributeDecodable {
var `class`: KeychainClass?
var service: String?
var account: String?
var accessGroup: String?
var accessible: Accessible?
var creationDate: Date?
var modificationDate: Date?
var generic: Data?
var valueData: Data?
var valuePersistentReference: Data?
var `return`: Set<Keychain.Return>?
var matchLimit: Keychain.MatchLimit?
init() {}
init(attributes: [CFString: Any]) {
`class` = KeychainClass(attributes: attributes)
service = attributes[kSecAttrService] as? String
account = attributes[kSecAttrAccount] as? String
accessGroup = attributes[kSecAttrAccessGroup] as? String
accessible = Accessible(attributes: attributes)
creationDate = attributes[kSecAttrCreationDate] as? Date
modificationDate = attributes[kSecAttrModificationDate] as? Date
generic = attributes[kSecAttrGeneric] as? Data
valueData = attributes[kSecValueData] as? Data
valuePersistentReference = attributes[kSecValuePersistentRef] as? Data
`return` = Set(attributes: attributes)
matchLimit = Keychain.MatchLimit(attributes: attributes)
}
func updateKeychainAttributes(in attributes: inout [CFString: Any]) {
`class`?.updateKeychainAttributes(in: &attributes)
if let service = service {
attributes[kSecAttrService] = service
}
if let account = account {
attributes[kSecAttrAccount] = account
}
if let accessGroup = accessGroup {
attributes[kSecAttrAccessGroup] = accessGroup
}
accessible?.updateKeychainAttributes(in: &attributes)
if let creationDate = creationDate {
attributes[kSecAttrCreationDate] = creationDate
}
if let modificationDate = modificationDate {
attributes[kSecAttrModificationDate] = modificationDate
}
if let generic = generic {
attributes[kSecAttrGeneric] = generic
}
if let valueData = valueData {
attributes[kSecValueData] = valueData
}
if let valuePersistentReference = valuePersistentReference {
attributes[kSecValuePersistentRef] = valuePersistentReference
}
`return`?.updateKeychainAttributes(in: &attributes)
matchLimit?.updateKeychainAttributes(in: &attributes)
}
}
}
|