Skip to content

Commit 4372ef8

Browse files
authored
Add new TSTLSConfiguration.init the use TSTLSVersion (#94)
* Change server closed test * Add TSTLSVersion and use in init instead of tls_protocol_version_t * comment
1 parent 6a2e634 commit 4372ef8

File tree

2 files changed

+109
-30
lines changed

2 files changed

+109
-30
lines changed

Sources/MQTTNIO/TSTLSConfiguration.swift

Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,57 @@ import Network
66
import NIOSSL
77
#endif
88

9-
extension tls_protocol_version_t {
9+
/// TLS Version enumeration
10+
public enum TSTLSVersion {
11+
case tlsV10
12+
case tlsV11
13+
case tlsV12
14+
case tlsV13
15+
16+
/// return `SSLProtocol` for iOS12 api
1017
var sslProtocol: SSLProtocol {
1118
switch self {
12-
case .TLSv10:
19+
case .tlsV10:
1320
return .tlsProtocol1
14-
case .TLSv11:
21+
case .tlsV11:
1522
return .tlsProtocol11
16-
case .TLSv12:
23+
case .tlsV12:
1724
return .tlsProtocol12
18-
case .TLSv13:
25+
case .tlsV13:
1926
return .tlsProtocol13
20-
case .DTLSv10:
21-
return .dtlsProtocol1
22-
case .DTLSv12:
23-
return .dtlsProtocol12
24-
@unknown default:
25-
return .tlsProtocol1
27+
}
28+
}
29+
30+
/// return `tls_protocol_version_t` for iOS13 and later apis
31+
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
32+
var tlsProtocolVersion: tls_protocol_version_t {
33+
switch self {
34+
case .tlsV10:
35+
return .TLSv10
36+
case .tlsV11:
37+
return .TLSv11
38+
case .tlsV12:
39+
return .TLSv12
40+
case .tlsV13:
41+
return .TLSv13
42+
}
43+
}
44+
}
45+
46+
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
47+
extension tls_protocol_version_t {
48+
var tsTLSVersion: TSTLSVersion {
49+
switch self {
50+
case .TLSv10:
51+
return .tlsV10
52+
case .TLSv11:
53+
return .tlsV11
54+
case .TLSv12:
55+
return .tlsV12
56+
case .TLSv13:
57+
return .tlsV13
58+
default:
59+
preconditionFailure("Invalid TLS version")
2660
}
2761
}
2862
}
@@ -95,10 +129,10 @@ public struct TSTLSConfiguration {
95129
}
96130

97131
/// The minimum TLS version to allow in negotiation. Defaults to tlsv1.
98-
public var minimumTLSVersion: tls_protocol_version_t
132+
public var minimumTLSVersion: TSTLSVersion
99133

100134
/// The maximum TLS version to allow in negotiation. If nil, there is no upper limit. Defaults to nil.
101-
public var maximumTLSVersion: tls_protocol_version_t?
135+
public var maximumTLSVersion: TSTLSVersion?
102136

103137
/// The trust roots to use to validate certificates. This only needs to be provided if you intend to validate
104138
/// certificates.
@@ -114,9 +148,16 @@ public struct TSTLSConfiguration {
114148
public var certificateVerification: TSCertificateVerification
115149

116150
/// Initialize TSTLSConfiguration
151+
/// - Parameters:
152+
/// - minimumTLSVersion: minimum version of TLS supported
153+
/// - maximumTLSVersion: maximum version of TLS supported
154+
/// - trustRoots: The trust roots to use to validate certificates
155+
/// - clientIdentity: Client identity
156+
/// - applicationProtocols: The application protocols to use in the connection
157+
/// - certificateVerification: Should certificates be verified
117158
public init(
118-
minimumTLSVersion: tls_protocol_version_t = .TLSv10,
119-
maximumTLSVersion: tls_protocol_version_t? = nil,
159+
minimumTLSVersion: TSTLSVersion = .tlsV10,
160+
maximumTLSVersion: TSTLSVersion? = nil,
120161
trustRoots: [SecCertificate]? = nil,
121162
clientIdentity: SecIdentity? = nil,
122163
applicationProtocols: [String] = [],
@@ -134,14 +175,13 @@ public struct TSTLSConfiguration {
134175
/// - Parameters:
135176
/// - minimumTLSVersion: minimum version of TLS supported
136177
/// - maximumTLSVersion: maximum version of TLS supported
137-
/// - p12: P12 filename
138-
/// - p12Password: Password for P12
139178
/// - trustRoots: The trust roots to use to validate certificates
179+
/// - clientIdentity: Client identity
140180
/// - applicationProtocols: The application protocols to use in the connection
141181
/// - certificateVerification: Should certificates be verified
142182
public init(
143-
minimumTLSVersion: tls_protocol_version_t = .TLSv10,
144-
maximumTLSVersion: tls_protocol_version_t? = nil,
183+
minimumTLSVersion: TSTLSVersion = .tlsV10,
184+
maximumTLSVersion: TSTLSVersion? = nil,
145185
trustRoots: Certificates,
146186
clientIdentity: Identity,
147187
applicationProtocols: [String] = [],
@@ -162,15 +202,15 @@ extension TSTLSConfiguration {
162202

163203
// minimum TLS protocol
164204
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) {
165-
sec_protocol_options_set_min_tls_protocol_version(options.securityProtocolOptions, self.minimumTLSVersion)
205+
sec_protocol_options_set_min_tls_protocol_version(options.securityProtocolOptions, self.minimumTLSVersion.tlsProtocolVersion)
166206
} else {
167207
sec_protocol_options_set_tls_min_version(options.securityProtocolOptions, self.minimumTLSVersion.sslProtocol)
168208
}
169209

170210
// maximum TLS protocol
171211
if let maximumTLSVersion = self.maximumTLSVersion {
172212
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) {
173-
sec_protocol_options_set_max_tls_protocol_version(options.securityProtocolOptions, maximumTLSVersion)
213+
sec_protocol_options_set_max_tls_protocol_version(options.securityProtocolOptions, maximumTLSVersion.tlsProtocolVersion)
174214
} else {
175215
sec_protocol_options_set_tls_max_version(options.securityProtocolOptions, maximumTLSVersion.sslProtocol)
176216
}
@@ -224,4 +264,44 @@ extension TSTLSConfiguration {
224264
/// Dispatch queue used by Network framework TLS to control certificate verification
225265
static var tlsDispatchQueue = DispatchQueue(label: "TSTLSConfiguration")
226266
}
267+
268+
/// Deprecated TSTLSConfiguration
269+
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
270+
@available(*, deprecated, message: "Use the init using TSTLSVersion")
271+
extension TSTLSConfiguration {
272+
/// Initialize TSTLSConfiguration
273+
public init(
274+
minimumTLSVersion: tls_protocol_version_t,
275+
maximumTLSVersion: tls_protocol_version_t? = nil,
276+
trustRoots: Certificates,
277+
clientIdentity: Identity,
278+
applicationProtocols: [String] = [],
279+
certificateVerification: TSCertificateVerification = .fullVerification
280+
) {
281+
self.minimumTLSVersion = minimumTLSVersion.tsTLSVersion
282+
self.maximumTLSVersion = maximumTLSVersion?.tsTLSVersion
283+
self.trustRoots = trustRoots.certificates
284+
self.clientIdentity = clientIdentity.identity
285+
self.applicationProtocols = applicationProtocols
286+
self.certificateVerification = certificateVerification
287+
}
288+
289+
/// Initialize TSTLSConfiguration
290+
public init(
291+
minimumTLSVersion: tls_protocol_version_t,
292+
maximumTLSVersion: tls_protocol_version_t? = nil,
293+
trustRoots: [SecCertificate]? = nil,
294+
clientIdentity: SecIdentity? = nil,
295+
applicationProtocols: [String] = [],
296+
certificateVerification: TSCertificateVerification = .fullVerification
297+
) {
298+
self.minimumTLSVersion = minimumTLSVersion.tsTLSVersion
299+
self.maximumTLSVersion = maximumTLSVersion?.tsTLSVersion
300+
self.trustRoots = trustRoots
301+
self.clientIdentity = clientIdentity
302+
self.applicationProtocols = applicationProtocols
303+
self.certificateVerification = certificateVerification
304+
}
305+
}
306+
227307
#endif

Tests/MQTTNIOTests/MQTTNIOTests.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ final class MQTTNIOTests: XCTestCase {
173173
try client.disconnect().wait()
174174
}
175175

176-
func testMQTTServerDisconnect() throws {
177-
let expectation = XCTestExpectation(description: "testMQTTServerDisconnect")
178-
expectation.expectedFulfillmentCount = 1
179-
176+
func testMQTTServerClose() throws {
180177
struct MQTTForceDisconnectMessage: MQTTPacket {
181178
var type: MQTTPacketType { .PUBLISH }
182179
var description: String { "FORCEDISCONNECT" }
@@ -195,13 +192,15 @@ final class MQTTNIOTests: XCTestCase {
195192
let client = self.createClient(identifier: "testMQTTServerDisconnect")
196193
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
197194
_ = try client.connect().wait()
198-
try client.connection?.sendMessageNoWait(MQTTForceDisconnectMessage()).wait()
199-
client.addCloseListener(named: "Test") { _ in
200-
expectation.fulfill()
195+
XCTAssertThrowsError(_ = try client.connection?.sendMessage(MQTTForceDisconnectMessage()) { _ in true }.wait()) { error in
196+
switch error {
197+
case MQTTError.serverClosedConnection:
198+
break
199+
default:
200+
XCTFail("\(error)")
201+
}
201202
}
202203

203-
wait(for: [expectation], timeout: 5.0)
204-
205204
XCTAssertFalse(client.isActive())
206205
}
207206

0 commit comments

Comments
 (0)