Skip to content

Commit 01faaee

Browse files
authored
Merge pull request #10 from MuniekMg/main
Certificate chain for client authentication
2 parents 2203ca3 + 976b5ba commit 01faaee

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

Sources/MQTTNIO/TSTSLConfiguration.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public enum TSTrustRoots {
5959
case certificates([SecCertificate])
6060
}
6161

62+
public enum TSSecIdentity {
63+
/// Client authentication disabled
64+
case none
65+
66+
/// Client authentication with single certificate
67+
case identity(SecIdentity)
68+
69+
/// Client authentication with certificate chain
70+
case chain((identity: SecIdentity, chain: [SecCertificate]))
71+
}
72+
6273
#if swift(>=5.5) && canImport(_Concurrency)
6374
extension TSTrustRoots: @unchecked MQTTSendable {}
6475
#endif
@@ -81,8 +92,8 @@ public struct TSTLSConfiguration {
8192
/// trust is used (as if `trustRoots` had been explicitly set to `.default`).
8293
public var trustRoots: TSTrustRoots?
8394

84-
/// The local identity to present in the TLS handshake. Defaults to nil.
85-
public var clientIdentity: SecIdentity?
95+
/// The local identity to present in the TLS handshake. Defaults to none.
96+
public var clientIdentity: TSSecIdentity
8697

8798
/// The application protocols to use in the connection. Should be an ordered list of ASCII
8899
/// strings representing the ALPN identifiers of the protocols to negotiate. For clients,
@@ -104,7 +115,7 @@ public struct TSTLSConfiguration {
104115
maximumTLSVersion: TSTLSVersion? = nil,
105116
certificateVerification: TSCertificateVerification = .fullVerification,
106117
trustRoots: TSTrustRoots? = nil,
107-
clientIdentity: SecIdentity? = nil,
118+
clientIdentity: TSSecIdentity = .none,
108119
applicationProtocols: [String] = []
109120
) {
110121
self.minimumTLSVersion = minimumTLSVersion
@@ -137,8 +148,19 @@ public struct TSTLSConfiguration {
137148
}
138149
}
139150

140-
if let clientIdentity = clientIdentity, let secIdentity = sec_identity_create(clientIdentity) {
141-
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secIdentity)
151+
switch clientIdentity {
152+
case .identity(let clientIdentity):
153+
if let secIdentity = sec_identity_create(clientIdentity) {
154+
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secIdentity)
155+
}
156+
157+
case .chain(let clientIdentity):
158+
if let secIdentity = sec_identity_create_with_certificates(clientIdentity.identity, clientIdentity.chain as CFArray) {
159+
sec_protocol_options_set_local_identity(options.securityProtocolOptions, secIdentity)
160+
}
161+
162+
case .none:
163+
break
142164
}
143165

144166
for applicationProtocol in applicationProtocols {

0 commit comments

Comments
 (0)