@@ -101,7 +101,6 @@ class TokenRequestViewModel: NSObject {
101
101
connection. startConnection { connection in
102
102
connection. pivSession { session, error in
103
103
guard let session = session else { Logger . ctk. error ( " No session: \( error!) " ) ; return }
104
- guard let operationType = userInfo. operationType ( ) else { Logger . ctk. error ( " No OperationType defined " ) ; return }
105
104
guard let type = userInfo. keyType ( ) ,
106
105
let objectId = userInfo. objectId ( ) ,
107
106
let algorithm = userInfo. algorithm ( ) ,
@@ -128,72 +127,35 @@ class TokenRequestViewModel: NSObject {
128
127
return
129
128
}
130
129
}
131
-
132
- switch operationType {
133
- case . signData:
134
- session. signWithKey ( in: slot, type: type, algorithm: algorithm, message: message) { signature, error in
135
- // Handle any errors
136
- if let error = error, ( error as NSError ) . code == 0x6a80 {
137
- YubiKitManager . shared. stopNFCConnection ( withErrorMessage: String ( localized: " Invalid signature " , comment: " PIV extension NFC invalid signature " ) )
138
- completion ( . communicationError( ErrorMessage ( title: String ( localized: " Invalid signature " , comment: " PIV extension NFC invalid signature " ) ,
139
- text: String ( localized: " The private key on the YubiKey does not match the certificate or there is no private key stored on the YubiKey. " , comment: " PIV extension NFC invalid signature no private key " ) ) ) )
140
- return
141
- }
142
- if let error = error {
143
- completion ( . communicationError( ErrorMessage ( title: String ( localized: " Signing failed " , comment: " PIV extension signing failed error message " ) , text: error. localizedDescription) ) )
144
- return
145
- }
146
- guard let signature = signature else { fatalError ( ) }
147
- // Verify signature
148
- let signatureError = self . verifySignature ( signature, data: message, objectId: objectId, algorithm: algorithm)
149
- if signatureError != nil {
150
- YubiKitManager . shared. stopNFCConnection ( withErrorMessage: String ( localized: " Invalid signature " , comment: " PIV extension invalid signature " ) )
151
- completion ( . communicationError( ErrorMessage ( title: String ( localized: " Invalid signature " , comment: " PIV extension invalid signature " ) ,
152
- text: String ( localized: " The private key on the YubiKey does not match the certificate. " , comment: " PIV extension invalid signature message " ) ) ) )
153
- return
154
- }
155
-
156
- YubiKitManager . shared. stopNFCConnection ( withMessage: String ( localized: " Successfully signed data " , comment: " PIV extension NFC successfully signed data " ) )
157
-
158
- if let userDefaults = UserDefaults ( suiteName: " group.com.yubico.Authenticator " ) {
159
- Logger . ctk. debug ( " Save data to userDefaults... " )
160
- userDefaults. setValue ( signature, forKey: " signedData " )
161
- completion ( nil )
162
- }
163
- } // End signWithKey Session
164
- case . decryptData:
165
- // Begin Decryption Session
166
- session. decryptWithKey ( in: slot, algorithm: algorithm, encrypted: message) { plainText, error in
167
- // Handle any errors
168
- if let error = error, ( error as NSError ) . code == 0x6a80 {
169
- YubiKitManager . shared. stopNFCConnection ( withErrorMessage: String ( localized: " Invalid decryption " , comment: " PIV extension NFC invalid decryption " ) )
170
- completion ( . communicationError( ErrorMessage ( title: String ( localized: " Invalid decryption " , comment: " PIV extension NFC invalid decryption " ) ,
171
- text: String ( localized: " The private key on the YubiKey does not match the certificate or there is no private key stored on the YubiKey. " , comment: " PIV extension NFC invalid decryption no private key " ) ) ) )
172
- return
173
- }
174
- if let error = error {
175
- completion ( . communicationError( ErrorMessage ( title: String ( localized: " Decryption failed " , comment: " PIV extension decryption failed error message " ) , text: error. localizedDescription) ) )
176
- return
177
- }
178
-
179
- guard let plainText = plainText else { fatalError ( ) }
180
-
181
- YubiKitManager . shared. stopNFCConnection ( withMessage: String ( localized: " Successfully decrypted cipher data " , comment: " PIV extension NFC successfully decrypted cipher data " ) )
182
-
183
- if let userDefaults = UserDefaults ( suiteName: " group.com.yubico.Authenticator " ) {
184
- Logger . ctk. debug ( " Save decrypted data to userDefaults... " )
185
-
186
- if let decryptedRawString = String ( data: plainText, encoding: . utf8) {
187
- // Injecting Yubico Authenticator watermark message for testing to confirm the decrypted message came via YA app
188
- //let decryptedYAString = "[Decrypted using YA] " + decryptedRawString
189
-
190
- if let decryptedYAStringAsData = decryptedRawString. data ( using: . utf8) {
191
- userDefaults. setValue ( decryptedYAStringAsData, forKey: " decryptedData " )
192
- }
193
- }
194
- completion ( nil )
195
- }
196
- } // End Decryption Session
130
+ session. signWithKey ( in: slot, type: type, algorithm: algorithm, message: message) { signature, error in
131
+ // Handle any errors
132
+ if let error = error, ( error as NSError ) . code == 0x6a80 {
133
+ YubiKitManager . shared. stopNFCConnection ( withErrorMessage: String ( localized: " Invalid signature " , comment: " PIV extension NFC invalid signature " ) )
134
+ completion ( . communicationError( ErrorMessage ( title: String ( localized: " Invalid signature " , comment: " PIV extension NFC invalid signature " ) ,
135
+ text: String ( localized: " The private key on the YubiKey does not match the certificate or there is no private key stored on the YubiKey. " , comment: " PIV extension NFC invalid signature no private key " ) ) ) )
136
+ return
137
+ }
138
+ if let error = error {
139
+ completion ( . communicationError( ErrorMessage ( title: String ( localized: " Signing failed " , comment: " PIV extension signing failed error message " ) , text: error. localizedDescription) ) )
140
+ return
141
+ }
142
+ guard let signature = signature else { fatalError ( ) }
143
+ // Verify signature
144
+ let signatureError = self . verifySignature ( signature, data: message, objectId: objectId, algorithm: algorithm)
145
+ if signatureError != nil {
146
+ YubiKitManager . shared. stopNFCConnection ( withErrorMessage: String ( localized: " Invalid signature " , comment: " PIV extension invalid signature " ) )
147
+ completion ( . communicationError( ErrorMessage ( title: String ( localized: " Invalid signature " , comment: " PIV extension invalid signature " ) ,
148
+ text: String ( localized: " The private key on the YubiKey does not match the certificate. " , comment: " PIV extension invalid signature message " ) ) ) )
149
+ return
150
+ }
151
+
152
+ YubiKitManager . shared. stopNFCConnection ( withMessage: String ( localized: " Successfully signed data " , comment: " PIV extension NFC successfully signed data " ) )
153
+
154
+ if let userDefaults = UserDefaults ( suiteName: " group.com.yubico.Authenticator " ) {
155
+ Logger . ctk. debug ( " Save data to userDefaults... " )
156
+ userDefaults. setValue ( signature, forKey: " signedData " )
157
+ completion ( nil )
158
+ }
197
159
}
198
160
}
199
161
}
@@ -224,11 +186,6 @@ class TokenRequestViewModel: NSObject {
224
186
}
225
187
}
226
188
227
- enum OperationType : String {
228
- case signData = " signData "
229
- case decryptData = " decryptData "
230
- }
231
-
232
189
233
190
extension TokenRequestViewModel {
234
191
@@ -343,11 +300,6 @@ private extension Dictionary where Key == AnyHashable, Value: Any {
343
300
guard let rawValue = self [ " algorithm " ] as? String else { return nil }
344
301
return SecKeyAlgorithm ( rawValue: rawValue as CFString )
345
302
}
346
-
347
- func operationType( ) -> OperationType ? {
348
- guard let rawValue = self [ " operationType " ] as? String else { return nil }
349
- return OperationType . init ( rawValue: rawValue)
350
- }
351
303
}
352
304
353
305
extension String : Error { }
0 commit comments