Skip to content

Commit 8c73026

Browse files
author
Maxim Moiseev
committed
[overlay] CryptoTokenKit
This is work in progress
1 parent 90a7f96 commit 8c73026

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

apinotes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(SWIFT_API_NOTES_INPUTS
1212
CoreLocation
1313
CoreSpotlight
1414
CoreText
15+
CryptoTokenKit
1516
Dispatch
1617
EventKit
1718
ExternalAccessory

apinotes/CryptoTokenKit.apinotes

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Name: CryptoTokenKit
3+
Classes:
4+
- Name: TKSmartCardSlotManager
5+
Methods:
6+
- Selector: 'getSlotWithName:reply:'
7+
MethodKind: Instance
8+
SwiftName: getSlot(withName:reply:)
9+
- Name: TKSmartCard
10+
Properties:
11+
- Name: valid
12+
SwiftName: isValid
13+
- Name: sensitive
14+
SwiftName: isSensitive
15+
Methods:
16+
- Selector: 'transmitRequest:reply:'
17+
MethodKind: Instance
18+
SwiftName: transmit(_:reply:)
19+
- Selector: 'userInteractionForSecurePINVerificationWithPINFormat:APDU:PINByteOffset:'
20+
MethodKind: Instance
21+
SwiftName: userInteractionForSecurePINVerification(_:apdu:pinByteOffset:)
22+
- Selector: 'userInteractionForSecurePINChangeWithPINFormat:APDU:currentPINByteOffset:newPINByteOffset:'
23+
MethodKind: Instance
24+
SwiftName: userInteractionForSecurePINChange(_:apdu:currentPINByteOffset:newPINByteOffset:)
25+
- Selector: 'sendIns:p1:p2:data:le:reply:'
26+
MethodKind: Instance
27+
SwiftPrivate: true
28+
- Selector: 'sendIns:p1:p2:data:le:sw:error:'
29+
MethodKind: Instance
30+
SwiftPrivate: true
31+
- Selector: 'inSessionWithError:executeBlock:'
32+
MethodKind: Instance
33+
SwiftPrivate: true
34+
- Name: TKSmartCardATRInterfaceGroup
35+
SwiftName: TKSmartCardATR.InterfaceGroup
36+
- Name: TKTokenKeyAlgorithm
37+
SwiftName: TKTokenSessionDelegate.KeyAlgorithm
38+
- Name: TKTokenKeyExchangeParameters
39+
SwiftName: TKTokenSessionDelegate.KeyExchangeParameters
40+
Protocols:
41+
- Name: TKTokenSessionDelegate
42+
Methods:
43+
- Selector: 'tokenSession:supportsOperation:usingKey:algorithm:'
44+
MethodKind: Instance
45+
SwiftName: tokenSession(_:supports:keyObjectID:algorithm:)
46+
- Selector: 'tokenSession:signData:usingKey:algorithm:error:'
47+
MethodKind: Instance
48+
SwiftName: tokenSession(_:sign:keyObjectID:algorithm:)
49+
- Selector: 'tokenSession:decryptData:usingKey:algorithm:error:'
50+
MethodKind: Instance
51+
SwiftName: tokenSession(_:decrypt:keyObjectID:algorithm:)
52+
- Selector: 'tokenSession:performKeyExchangeWithPublicKey:usingKey:algorithm:parameters:error:'
53+
MethodKind: Instance
54+
SwiftName: tokenSession(_:performKeyExchange:keyObjectID:algorithm:parameters:)
55+
- Name: TKTokenDelegate
56+
Methods:
57+
- Selector: 'token:createSessionWithError:'
58+
MethodKind: Instance
59+
SwiftName: createSession(_:)
60+
Enumerators:
61+
- Name: TKSmartCardPINEncodingASCII
62+
SwiftName: ascii
63+
- Name: TKSmartCardPINEncodingBCD
64+
SwiftName: bcd
65+
- Name: TKSmartCardProtocolT0
66+
SwiftName: t0
67+
- Name: TKSmartCardProtocolT1
68+
SwiftName: t1
69+
- Name: TKSmartCardProtocolT15
70+
SwiftName: t15
71+
Tags:
72+
- Name: TKSmartCardSlotState
73+
SwiftName: TKSmartCardSlot.State
74+
- Name: TKSmartCardPINCharset
75+
SwiftName: TKSmartCardPINFormat.Charset
76+
- Name: TKSmartCardPINEncoding
77+
SwiftName: TKSmartCardPINFormat.Encoding
78+
- Name: TKSmartCardPINJustification
79+
SwiftName: TKSmartCardPINFormat.Justification
80+
- Name: TKSmartCardPINCompletion
81+
SwiftName: TKSmartCardUserInteractionForPINOperation.Completion
82+
- Name: TKSmartCardPINConfirmation
83+
SwiftName: TKSmartCardUserInteractionForSecurePINChange.Confirmation
84+
- Name: TKErrorCode
85+
NSErrorDomain: TKErrorDomain

stdlib/public/SDK/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_subdirectory(CoreGraphics)
2121
add_subdirectory(CoreImage)
2222
add_subdirectory(CoreLocation)
2323
add_subdirectory(CoreMedia)
24+
add_subdirectory(CryptoTokenKit)
2425
add_subdirectory(Dispatch)
2526
add_subdirectory(Foundation)
2627
add_subdirectory(GameplayKit)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_swift_library(swiftCryptoTokenKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
2+
CryptoTokenKit.swift
3+
4+
TARGET_SDKS OSX
5+
SWIFT_MODULE_DEPENDS Foundation
6+
FRAMEWORK_DEPENDS CryptoTokenKit)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import CryptoTokenKit
14+
15+
import Foundation
16+
17+
@available(OSX 10.10, *)
18+
extension TKSmartCard {
19+
public func send(ins: UInt8, p1: UInt8, p2: UInt8, data: Data? = nil,
20+
le: Int? = nil, reply: (Data?, UInt16, Error?) -> Void) {
21+
22+
self.__sendIns(ins, p1: p1, p2: p2, data: data,
23+
le: le.map { NSNumber(value: $0) }, reply: reply)
24+
}
25+
26+
@available(OSX 10.12, *)
27+
public func send(ins: UInt8, p1: UInt8, p2: UInt8, data: Data? = nil,
28+
le: Int? = nil) throws -> (sw: UInt16, response: Data) {
29+
30+
var sw: UInt16 = 0
31+
let response = try self.__sendIns(ins, p1: p1, p2: p2, data: data,
32+
le: le.map { NSNumber(value: $0) }, sw: &sw)
33+
return (sw: sw, response: response)
34+
}
35+
36+
@available(OSX 10.12, *)
37+
public func withSession<T>(_ body: () throws -> T) throws -> T {
38+
var result: T? = nil
39+
try self.__inSession(executeBlock: {
40+
(errorPointer: NSErrorPointer) -> Bool in
41+
do {
42+
result = try body()
43+
return true
44+
} catch let error as NSError {
45+
errorPointer?.pointee = error
46+
return false
47+
}
48+
})
49+
50+
// it is safe to force unwrap the result here, as the self.__inSession
51+
// function rethrows the errors which happened inside the block
52+
return result!
53+
}
54+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
// REQUIRES: objc_interop
4+
// UNSUPPORTED: OS=watchos
5+
// UNSUPPORTED: OS=tvos
6+
// UNSUPPORTED: OS=ios
7+
8+
import CryptoTokenKit
9+
import Security
10+
11+
if #available(OSX 10.12, *) {
12+
struct TKSmartCardTest {
13+
func t1(manager: TKSmartCardSlotManager, name: String) {
14+
manager.getSlot(withName: name) { (slot: TKSmartCardSlot?) in
15+
let _: TKSmartCardSlot.State = slot!.state
16+
}
17+
}
18+
19+
let p1 = TKSmartCardPINFormat.Charset.numeric
20+
let p2 = TKSmartCardPINFormat.Encoding.ascii
21+
let p3 = TKSmartCardPINFormat.Encoding.bcd
22+
let p4 = TKSmartCardPINFormat.Justification.left
23+
let p5 = TKSmartCardUserInteractionForPINOperation.Completion.key
24+
let p6 = TKSmartCardUserInteractionForSecurePINChange.Confirmation.current
25+
let p7 = TKSmartCardProtocol.t0
26+
let p8 = TKSmartCardProtocol.t1
27+
let p9 = TKSmartCardProtocol.t15
28+
let p10 = TKSmartCardATR.InterfaceGroup()
29+
30+
func t2(card: TKSmartCard) throws {
31+
card.isSensitive = card.isValid
32+
card.transmit(Data()) { (response: Data?, error: Error?) in
33+
}
34+
35+
card.userInteractionForSecurePINVerification(TKSmartCardPINFormat(),
36+
apdu: Data(), pinByteOffset: 0)
37+
card.userInteractionForSecurePINChange(TKSmartCardPINFormat(),
38+
apdu: Data(), currentPINByteOffset: 0, newPINByteOffset: 0)
39+
40+
card.send(ins: 0xa4, p1: 0x04, p2: 0x00, data:Data(), le: 0) {
41+
(response: Data?, sw: UInt16, error: Error?) in
42+
}
43+
44+
card.send(ins: 0xa4, p1: 0x04, p2: 0x00, le: 0) {
45+
(response: Data?, sw: UInt16, error: Error?) in
46+
}
47+
48+
card.send(ins: 0xa4, p1: 0x04, p2: 0x00, data:Data()) {
49+
(response: Data?, sw: UInt16, error: Error?) in
50+
}
51+
52+
card.send(ins: 0xa4, p1: 0x04, p2: 0x00) {
53+
(response: Data?, sw: UInt16, error: Error?) in
54+
}
55+
56+
let _: Int = try card.withSession() {
57+
let (_, _): (UInt16, Data) = try card.send(ins: 0xa4, p1: 0x04,
58+
p2: 0x00, data: Data(), le: 0)
59+
let (_, _): (UInt16, Data) = try card.send(ins: 0xa4, p1: 0x04,
60+
p2: 0x00, le: 0)
61+
let (_, _): (UInt16, Data) = try card.send(ins: 0xa4, p1: 0x04,
62+
p2: 0x00, data: Data())
63+
let (_, _): (UInt16, Data) = try card.send(ins: 0xa4, p1: 0x04,
64+
p2: 0x00)
65+
return 1
66+
}
67+
}
68+
}
69+
70+
struct TKTokenTest {
71+
func f1(session: TKTokenSession, sessionDelegate: TKTokenSessionDelegate,
72+
algorithm: TKTokenSessionDelegate.KeyAlgorithm,
73+
parameters: TKTokenSessionDelegate.KeyExchangeParameters) throws {
74+
let _: Bool = sessionDelegate.tokenSession!(session, supports: .none,
75+
keyObjectID: "", algorithm: algorithm)
76+
let _: Data = try sessionDelegate.tokenSession!(session, sign: Data(),
77+
keyObjectID: "", algorithm: algorithm)
78+
let _: Data = try sessionDelegate.tokenSession!(session, decrypt: Data(),
79+
keyObjectID: "", algorithm: algorithm)
80+
let _: Data = try sessionDelegate.tokenSession!(session,
81+
performKeyExchange: Data(), keyObjectID: "", algorithm: algorithm,
82+
parameters: parameters)
83+
let _: Bool = algorithm.isAlgorithm(.rsaSignatureDigestPKCS1v15SHA1)
84+
let _: Bool = algorithm.supportsAlgorithm(.rsaSignatureDigestPKCS1v15SHA1)
85+
}
86+
87+
func f2(token: TKToken, delegate: TKTokenDelegate) throws {
88+
let _: TKTokenSession = try delegate.createSession(token)
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)