Skip to content

Commit f57cb4b

Browse files
committed
Added SCP support to all YKFConnections and YKFSessions types.
1 parent c1a24b0 commit f57cb4b

18 files changed

+397
-10
lines changed

YubiKit/YubiKit/Connections/AccessoryConnection/YKFAccessoryConnection.m

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ - (void)u2fSession:(YKFU2FSessionCompletionBlock _Nonnull)callback {
158158
}];
159159
}
160160

161+
- (void)u2fSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFU2FSessionCompletionBlock _Nonnull)completion {
162+
[self.currentSession clearSessionState];
163+
[YKFU2FSession sessionWithConnectionController:self.connectionController
164+
scpKeyParams:scpKeyParams
165+
completion:^(YKFU2FSession *_Nullable session, NSError * _Nullable error) {
166+
self.currentSession = session;
167+
completion(session, error);
168+
}];
169+
}
170+
161171
- (void)fido2Session:(YKFFIDO2SessionCompletionBlock _Nonnull)callback {
162172
[self.currentSession clearSessionState];
163173
[YKFFIDO2Session sessionWithConnectionController:self.connectionController
@@ -167,6 +177,17 @@ - (void)fido2Session:(YKFFIDO2SessionCompletionBlock _Nonnull)callback {
167177
}];
168178
}
169179

180+
- (void)fido2Session:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFFIDO2SessionCompletionBlock _Nonnull)completion {
181+
[self.currentSession clearSessionState];
182+
[YKFFIDO2Session sessionWithConnectionController:self.connectionController
183+
scpKeyParams:scpKeyParams
184+
completion:^(YKFFIDO2Session *_Nullable session, NSError * _Nullable error) {
185+
self.currentSession = session;
186+
completion(session, error);
187+
}];
188+
}
189+
190+
170191
- (void)pivSession:(YKFPIVSessionCompletionBlock _Nonnull)callback {
171192
[self.currentSession clearSessionState];
172193
[YKFPIVSession sessionWithConnectionController:self.connectionController
@@ -176,6 +197,16 @@ - (void)pivSession:(YKFPIVSessionCompletionBlock _Nonnull)callback {
176197
}];
177198
}
178199

200+
- (void)pivSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFPIVSessionCompletionBlock _Nonnull)completion {
201+
[self.currentSession clearSessionState];
202+
[YKFPIVSession sessionWithConnectionController:self.connectionController
203+
scpKeyParams:scpKeyParams
204+
completion:^(YKFPIVSession *_Nullable session, NSError * _Nullable error) {
205+
self.currentSession = session;
206+
completion(session, error);
207+
}];
208+
}
209+
179210
- (void)challengeResponseSession:(YKFChallengeResponseSessionCompletionBlock _Nonnull)callback {
180211
[self.currentSession clearSessionState];
181212
[YKFChallengeResponseSession sessionWithConnectionController:self.connectionController
@@ -185,6 +216,16 @@ - (void)challengeResponseSession:(YKFChallengeResponseSessionCompletionBlock _No
185216
}];
186217
}
187218

219+
- (void)challengeResponseSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFChallengeResponseSessionCompletionBlock _Nonnull)completion {
220+
[self.currentSession clearSessionState];
221+
[YKFChallengeResponseSession sessionWithConnectionController:self.connectionController
222+
scpKeyParams:scpKeyParams
223+
completion:^(YKFChallengeResponseSession *_Nullable session, NSError * _Nullable error) {
224+
self.currentSession = session;
225+
completion(session, error);
226+
}];
227+
}
228+
188229
- (void)managementSession:(YKFManagementSessionCompletion _Nonnull)callback {
189230
[self.currentSession clearSessionState];
190231
[YKFManagementSession sessionWithConnectionController:self.connectionController
@@ -194,6 +235,16 @@ - (void)managementSession:(YKFManagementSessionCompletion _Nonnull)callback {
194235
}];
195236
}
196237

238+
- (void)managementSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFManagementSessionCompletion _Nonnull)completion {
239+
[self.currentSession clearSessionState];
240+
[YKFManagementSession sessionWithConnectionController:self.connectionController
241+
scpKeyParams:scpKeyParams
242+
completion:^(YKFManagementSession *_Nullable session, NSError * _Nullable error) {
243+
self.currentSession = session;
244+
completion(session, error);
245+
}];
246+
}
247+
197248
- (void)securityDomainSession:(YKFSecurityDomainSessionCompletion _Nonnull)completion {
198249
[self.currentSession clearSessionState];
199250
[YKFSecurityDomainSession sessionWithConnectionController:self.connectionController
@@ -203,6 +254,16 @@ - (void)securityDomainSession:(YKFSecurityDomainSessionCompletion _Nonnull)compl
203254
}];
204255
}
205256

257+
- (void)securityDomainSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFSecurityDomainSessionCompletion _Nonnull)completion {
258+
[self.currentSession clearSessionState];
259+
[YKFSecurityDomainSession sessionWithConnectionController:self.connectionController
260+
scpKeyParams:scpKeyParams
261+
completion:^(YKFSecurityDomainSession *_Nullable session, NSError * _Nullable error) {
262+
self.currentSession = session;
263+
completion(session, error);
264+
}];
265+
}
266+
206267
- (void)executeRawCommand:(NSData *)data completion:(YKFRawComandCompletion)completion {
207268
YKFAPDU *apdu = [[YKFAPDU alloc] initWithData:data];
208269
[self.connectionController execute:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error, NSTimeInterval executionTime) {

YubiKit/YubiKit/Connections/NFCConnection/YKFNFCConnection.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ - (void)u2fSession:(YKFU2FSessionCompletionBlock _Nonnull)callback {
113113
}
114114
}
115115

116+
- (void)u2fSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFU2FSessionCompletionBlock _Nonnull)completion {
117+
[self.currentSession clearSessionState];
118+
[YKFU2FSession sessionWithConnectionController:self.connectionController
119+
scpKeyParams:scpKeyParams
120+
completion:^(YKFU2FSession *_Nullable session, NSError * _Nullable error) {
121+
self.currentSession = session;
122+
completion(session, error);
123+
}];
124+
}
125+
116126
- (void)fido2Session:(YKFFIDO2SessionCompletionBlock _Nonnull)callback {
117127
[self.currentSession clearSessionState];
118128
[YKFFIDO2Session sessionWithConnectionController:self.connectionController
@@ -122,6 +132,16 @@ - (void)fido2Session:(YKFFIDO2SessionCompletionBlock _Nonnull)callback {
122132
}];
123133
}
124134

135+
- (void)fido2Session:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFFIDO2SessionCompletionBlock _Nonnull)completion {
136+
[self.currentSession clearSessionState];
137+
[YKFFIDO2Session sessionWithConnectionController:self.connectionController
138+
scpKeyParams:scpKeyParams
139+
completion:^(YKFFIDO2Session *_Nullable session, NSError * _Nullable error) {
140+
self.currentSession = session;
141+
completion(session, error);
142+
}];
143+
}
144+
125145
- (void)pivSession:(YKFPIVSessionCompletionBlock _Nonnull)callback {
126146
[self.currentSession clearSessionState];
127147
[YKFPIVSession sessionWithConnectionController:self.connectionController
@@ -131,6 +151,16 @@ - (void)pivSession:(YKFPIVSessionCompletionBlock _Nonnull)callback {
131151
}];
132152
}
133153

154+
- (void)pivSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFPIVSessionCompletionBlock _Nonnull)completion {
155+
[self.currentSession clearSessionState];
156+
[YKFPIVSession sessionWithConnectionController:self.connectionController
157+
scpKeyParams:scpKeyParams
158+
completion:^(YKFPIVSession *_Nullable session, NSError * _Nullable error) {
159+
self.currentSession = session;
160+
completion(session, error);
161+
}];
162+
}
163+
134164
- (void)securityDomainSession:(YKFSecurityDomainSessionCompletion _Nonnull)completion {
135165
[self.currentSession clearSessionState];
136166
[YKFSecurityDomainSession sessionWithConnectionController:self.connectionController
@@ -140,6 +170,16 @@ - (void)securityDomainSession:(YKFSecurityDomainSessionCompletion _Nonnull)compl
140170
}];
141171
}
142172

173+
- (void)securityDomainSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFSecurityDomainSessionCompletion _Nonnull)completion {
174+
[self.currentSession clearSessionState];
175+
[YKFSecurityDomainSession sessionWithConnectionController:self.connectionController
176+
scpKeyParams:scpKeyParams
177+
completion:^(YKFSecurityDomainSession *_Nullable session, NSError * _Nullable error) {
178+
self.currentSession = session;
179+
completion(session, error);
180+
}];
181+
}
182+
143183
- (void)challengeResponseSession:(YKFChallengeResponseSessionCompletionBlock _Nonnull)callback {
144184
[self.currentSession clearSessionState];
145185
[YKFChallengeResponseSession sessionWithConnectionController:self.connectionController
@@ -149,6 +189,16 @@ - (void)challengeResponseSession:(YKFChallengeResponseSessionCompletionBlock _No
149189
}];
150190
}
151191

192+
- (void)challengeResponseSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFChallengeResponseSessionCompletionBlock _Nonnull)completion {
193+
[self.currentSession clearSessionState];
194+
[YKFChallengeResponseSession sessionWithConnectionController:self.connectionController
195+
scpKeyParams:scpKeyParams
196+
completion:^(YKFChallengeResponseSession *_Nullable session, NSError * _Nullable error) {
197+
self.currentSession = session;
198+
completion(session, error);
199+
}];
200+
}
201+
152202
- (void)managementSession:(YKFManagementSessionCompletion _Nonnull)callback {
153203
[self.currentSession clearSessionState];
154204
[YKFManagementSession sessionWithConnectionController:self.connectionController
@@ -158,6 +208,16 @@ - (void)managementSession:(YKFManagementSessionCompletion _Nonnull)callback {
158208
}];
159209
}
160210

211+
- (void)managementSession:(id<YKFSCPKeyParamsProtocol> _Nonnull)scpKeyParams completion:(YKFManagementSessionCompletion _Nonnull)completion {
212+
[self.currentSession clearSessionState];
213+
[YKFManagementSession sessionWithConnectionController:self.connectionController
214+
scpKeyParams:scpKeyParams
215+
completion:^(YKFManagementSession *_Nullable session, NSError * _Nullable error) {
216+
self.currentSession = session;
217+
completion(session, error);
218+
}];
219+
}
220+
161221
- (void)executeRawCommand:(NSData *)data completion:(YKFRawComandCompletion)completion {
162222
YKFAPDU *apdu = [[YKFAPDU alloc] initWithData:data];
163223
[self.connectionController execute:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error, NSTimeInterval executionTime) {

YubiKit/YubiKit/Connections/SCP/YKFSCPSecurityDomainSession+Private.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ typedef void (^YKFSecurityDomainSessionCompletion)(YKFSecurityDomainSession *_Nu
3030
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
3131
completion:(YKFSecurityDomainSessionCompletion _Nonnull)completion;
3232

33-
//+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
34-
// scpKeyParams:(nonnull id<YKFSCPKeyParamsProtocol>)scpKeyParams
35-
// completion:(YKFOATHSessionCompletion _Nonnull)completion;
36-
//
37-
33+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
34+
scpKeyParams:(nonnull id<YKFSCPKeyParamsProtocol>)scpKeyParams
35+
completion:(YKFSecurityDomainSessionCompletion _Nonnull)completion;
3836

3937
@end
4038

YubiKit/YubiKit/Connections/SCP/YKFSCPSecurityDomainSession.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#import "YKFSCPKeyRef.h"
2222
#import "YKFNSDataAdditions.h"
2323
#import "YKFNSDataAdditions+Private.h"
24+
#import "YKFSCPProcessor.h"
25+
#import "YKFSCPKeyParamsProtocol.h"
26+
2427
@implementation YKFSecurityDomainSession
2528

2629
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
@@ -38,6 +41,33 @@ + (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProto
3841
}];
3942
}
4043

44+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
45+
scpKeyParams:(id<YKFSCPKeyParamsProtocol>)scpKeyParams
46+
completion:(YKFSecurityDomainSessionCompletion _Nonnull)completion {
47+
YKFSecurityDomainSession *session = [YKFSecurityDomainSession new];
48+
session.smartCardInterface = [[YKFSmartCardInterface alloc] initWithConnectionController:connectionController];
49+
50+
YKFSelectApplicationAPDU *apdu = [[YKFSelectApplicationAPDU alloc] initWithApplicationName:YKFSelectApplicationAPDUNameSecurityDomain];
51+
[session.smartCardInterface selectApplication:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error) {
52+
if (error) {
53+
completion(nil, error);
54+
} else {
55+
if (scpKeyParams) {
56+
[YKFSCPProcessor processorWithSCPKeyParams:scpKeyParams sendRemainingIns:YKFSmartCardInterfaceSendRemainingInsNormal usingSmartCardInterface:session.smartCardInterface completion:^(YKFSCPProcessor * _Nullable processor, NSError * _Nullable error) {
57+
if (error) {
58+
completion(nil, error);
59+
} else {
60+
session.smartCardInterface.scpProcessor = processor;
61+
completion(session, nil);
62+
}
63+
}];
64+
} else {
65+
completion(session, nil);
66+
}
67+
}
68+
}];
69+
}
70+
4171
- (void)getDataWithTag:(UInt16)tag data:(NSData * _Nullable)data completion:(YKFSecurityDomainSessionDataCompletionBlock)completion {
4272
YKFAPDU *apdu = [[YKFAPDU alloc] initWithCla:0 ins:0xca p1:(uint8_t)(tag >> 8) p2:(uint8_t)(tag & 0xff) data:data type:YKFAPDUTypeExtended];
4373
[self.smartCardInterface executeCommand:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error) {

YubiKit/YubiKit/Connections/Shared/APDU/YKFAPDU.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "YKFAccessoryConnectionController.h"
1717
#import "YKFNSMutableDataAdditions.h"
1818
#import "YKFAssert.h"
19+
#import "YKFNSDataAdditions+Private.h"
1920

2021
@interface YKFAPDU()
2122

YubiKit/YubiKit/Connections/Shared/Sessions/ChalResp/YKFChallengeResponseSession+Private.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#import "YKFSessionProtocol+Private.h"
1717
#import "YKFChallengeResponseSession.h"
1818

19-
@protocol YKFConnectionControllerProtocol;
19+
@protocol YKFConnectionControllerProtocol, YKFSCPKeyParamsProtocol;
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

@@ -26,6 +26,10 @@ typedef void (^YKFChallengeResponseSessionCompletion)(YKFChallengeResponseSessio
2626
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
2727
completion:(YKFChallengeResponseSessionCompletion _Nonnull)completion;
2828

29+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
30+
scpKeyParams:(nonnull id<YKFSCPKeyParamsProtocol>)scpKeyParams
31+
completion:(YKFChallengeResponseSessionCompletion _Nonnull)completion;
32+
2933
@end
3034

3135
NS_ASSUME_NONNULL_END

YubiKit/YubiKit/Connections/Shared/Sessions/ChalResp/YKFChallengeResponseSession.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import "YKFChallengeResponseError.h"
1818
#import "YKFSessionError+Private.h"
1919
#import "YKFSelectApplicationAPDU.h"
20+
#import "YKFSCPProcessor.h"
21+
#import "YKFSCPKeyParamsProtocol.h"
2022

2123
@implementation YKFChallengeResponseSession
2224

@@ -36,6 +38,33 @@ + (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProto
3638
}];
3739
}
3840

41+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
42+
scpKeyParams:(id<YKFSCPKeyParamsProtocol>)scpKeyParams
43+
completion:(YKFChallengeResponseSessionCompletion _Nonnull)completion {
44+
YKFChallengeResponseSession *session = [YKFChallengeResponseSession new];
45+
session.smartCardInterface = [[YKFSmartCardInterface alloc] initWithConnectionController:connectionController];
46+
47+
YKFSelectApplicationAPDU *apdu = [[YKFSelectApplicationAPDU alloc] initWithApplicationName:YKFSelectApplicationAPDUNameChalResp];
48+
[session.smartCardInterface selectApplication:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error) {
49+
if (error) {
50+
completion(nil, error);
51+
} else {
52+
if (scpKeyParams) {
53+
[YKFSCPProcessor processorWithSCPKeyParams:scpKeyParams sendRemainingIns:YKFSmartCardInterfaceSendRemainingInsNormal usingSmartCardInterface:session.smartCardInterface completion:^(YKFSCPProcessor * _Nullable processor, NSError * _Nullable error) {
54+
if (error) {
55+
completion(nil, error);
56+
} else {
57+
session.smartCardInterface.scpProcessor = processor;
58+
completion(session, nil);
59+
}
60+
}];
61+
} else {
62+
completion(session, nil);
63+
}
64+
}
65+
}];
66+
}
67+
3968
- (void)sendChallenge:(nonnull NSData *)challenge slot:(YKFSlot)slot completion:(nonnull YKFChallengeResponseSessionResponseBlock)completion {
4069
YKFChalRespSendRequest *request = [[YKFChalRespSendRequest alloc] initWithChallenge:challenge slot: slot];
4170
[self.smartCardInterface executeCommand:request.apdu completion:^(NSData * _Nullable data, NSError * _Nullable error) {

YubiKit/YubiKit/Connections/Shared/Sessions/FIDO2/YKFFIDO2Session+Private.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#import "YKFSessionProtocol+Private.h"
1717
#import "YKFFIDO2Session.h"
1818

19-
@protocol YKFConnectionControllerProtocol;
19+
@protocol YKFConnectionControllerProtocol, YKFSCPKeyParamsProtocol;
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

@@ -26,6 +26,10 @@ typedef void (^YKFFIDO2SessionCompletion)(YKFFIDO2Session *_Nullable, NSError* _
2626
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
2727
completion:(YKFFIDO2SessionCompletion _Nonnull)completion;
2828

29+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
30+
scpKeyParams:(nonnull id<YKFSCPKeyParamsProtocol>)scpKeyParams
31+
completion:(YKFFIDO2SessionCompletion _Nonnull)completion;
32+
2933
@end
3034

3135
NS_ASSUME_NONNULL_END

YubiKit/YubiKit/Connections/Shared/Sessions/FIDO2/YKFFIDO2Session.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#import "YKFSmartCardInterface.h"
5050
#import "YKFSelectApplicationAPDU.h"
5151

52+
#import "YKFSCPProcessor.h"
53+
#import "YKFSCPKeyParamsProtocol.h"
54+
5255
static const int YKFFIDO2RequestMaxRetries = 30; // times
5356
static const NSTimeInterval YKFFIDO2RequestRetryTimeInterval = 0.5; // seconds
5457
NSString* const YKFFIDO2OptionRK = @"rk";
@@ -100,6 +103,34 @@ + (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProto
100103
}];
101104
}
102105

106+
+ (void)sessionWithConnectionController:(nonnull id<YKFConnectionControllerProtocol>)connectionController
107+
scpKeyParams:(id<YKFSCPKeyParamsProtocol>)scpKeyParams
108+
completion:(YKFFIDO2SessionCompletion _Nonnull)completion {
109+
YKFFIDO2Session *session = [YKFFIDO2Session new];
110+
session.smartCardInterface = [[YKFSmartCardInterface alloc] initWithConnectionController:connectionController];
111+
112+
YKFSelectApplicationAPDU *apdu = [[YKFSelectApplicationAPDU alloc] initWithApplicationName:YKFSelectApplicationAPDUNameFIDO2];
113+
[session.smartCardInterface selectApplication:apdu completion:^(NSData * _Nullable data, NSError * _Nullable error) {
114+
if (error) {
115+
completion(nil, error);
116+
} else {
117+
if (scpKeyParams) {
118+
[YKFSCPProcessor processorWithSCPKeyParams:scpKeyParams sendRemainingIns:YKFSmartCardInterfaceSendRemainingInsNormal usingSmartCardInterface:session.smartCardInterface completion:^(YKFSCPProcessor * _Nullable processor, NSError * _Nullable error) {
119+
if (error) {
120+
completion(nil, error);
121+
} else {
122+
session.smartCardInterface.scpProcessor = processor;
123+
completion(session, nil);
124+
}
125+
}];
126+
} else {
127+
completion(session, nil);
128+
}
129+
[session updateKeyState:YKFFIDO2SessionKeyStateIdle];
130+
}
131+
}];
132+
}
133+
103134
- (void)clearSessionState {
104135
[self clearUserVerification];
105136
}

0 commit comments

Comments
 (0)