Skip to content

Commit

Permalink
feat: add self signed delegate and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
leeway1208 committed Mar 4, 2023
1 parent adeeef7 commit 2df529b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
34 changes: 20 additions & 14 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -458,25 +458,31 @@ extension ViewController: CocoaMQTT5Delegate {
}
}


let myCert = "myCert"

extension ViewController: CocoaMQTTDelegate {

// Optional ssl CocoaMQTTDelegate
func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
TRACE("trust: \(trust)")
/// Validate the server certificate
///
/// Some custom validation...
///
/// if validatePassed {
/// completionHandler(true)
/// } else {
/// completionHandler(false)
/// }
completionHandler(true)
// self signed delegate
func mqttUrlSession(_ mqtt: CocoaMQTT, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void){
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {

let certData = Data(base64Encoded: myCert as String)!

if let trust = challenge.protectionSpace.serverTrust,
let cert = SecCertificateCreateWithData(nil, certData as CFData) {
let certs = [cert]
SecTrustSetAnchorCertificates(trust, certs as CFArray)

completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: trust))
return
}
}

completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)

}


func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
TRACE("ack: \(ack)")

Expand Down
10 changes: 9 additions & 1 deletion Source/CocoaMQTT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ import MqttCocoaAsyncSocket
///
/// This method will be called if enable `allowUntrustCACertificate`
@objc optional func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void)


@objc optional func mqttUrlSession(_ mqtt: CocoaMQTT, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

///
@objc optional func mqtt(_ mqtt: CocoaMQTT, didPublishComplete id: UInt16)

Expand Down Expand Up @@ -571,6 +573,12 @@ extension CocoaMQTT: CocoaMQTTSocketDelegate {
didReceiveTrust(self, trust, completionHandler)
}

public func socketUrlSession(_ socket: CocoaMQTTSocketProtocol, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
printDebug("Call the SSL/TLS manually validating function - socketUrlSession")

delegate?.mqttUrlSession?(self, didReceiveTrust: trust, didReceiveChallenge: challenge, completionHandler: completionHandler)
}

// ?
public func socketDidSecure(_ sock: MGCDAsyncSocket) {
printDebug("Socket has successfully completed SSL/TLS negotiation")
Expand Down
8 changes: 8 additions & 0 deletions Source/CocoaMQTT5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ import MqttCocoaAsyncSocket
/// This method will be called if enable `allowUntrustCACertificate`
@objc optional func mqtt5(_ mqtt5: CocoaMQTT5, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void)

@objc optional func mqtt5UrlSession(_ mqtt: CocoaMQTT5, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

///
@objc optional func mqtt5(_ mqtt5: CocoaMQTT5, didPublishComplete id: UInt16, pubCompData: MqttDecodePubComp?)

Expand Down Expand Up @@ -613,6 +615,12 @@ extension CocoaMQTT5: CocoaMQTTSocketDelegate {
didReceiveTrust(self, trust, completionHandler)
}

public func socketUrlSession(_ socket: CocoaMQTTSocketProtocol, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
printDebug("Call the SSL/TLS manually validating function - socketUrlSession")

delegate?.mqtt5UrlSession?(self, didReceiveTrust: trust, didReceiveChallenge: challenge, completionHandler: completionHandler)
}

// ?
public func socketDidSecure(_ sock: MGCDAsyncSocket) {
printDebug("Socket has successfully completed SSL/TLS negotiation")
Expand Down
1 change: 1 addition & 0 deletions Source/CocoaMQTTSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MqttCocoaAsyncSocket
public protocol CocoaMQTTSocketDelegate: AnyObject {
func socketConnected(_ socket: CocoaMQTTSocketProtocol)
func socket(_ socket: CocoaMQTTSocketProtocol, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Swift.Void)
func socketUrlSession(_ socket: CocoaMQTTSocketProtocol, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
func socket(_ socket: CocoaMQTTSocketProtocol, didWriteDataWithTag tag: Int)
func socket(_ socket: CocoaMQTTSocketProtocol, didRead data: Data, withTag tag: Int)
func socketDidDisconnect(_ socket: CocoaMQTTSocketProtocol, withError err: Error?)
Expand Down
21 changes: 17 additions & 4 deletions Source/CocoaMQTTWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import CocoaMQTT
// MARK: - Interfaces

public protocol CocoaMQTTWebSocketConnectionDelegate: AnyObject {

func connection(_ conn: CocoaMQTTWebSocketConnection, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Swift.Void)

func urlSessionConnection(_ conn: CocoaMQTTWebSocketConnection, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

func connectionOpened(_ conn: CocoaMQTTWebSocketConnection)

Expand Down Expand Up @@ -149,7 +151,7 @@ public class CocoaMQTTWebSocket: CocoaMQTTSocketProtocol {
internal var delegate: CocoaMQTTSocketDelegate?
internal var delegateQueue: DispatchQueue?
internal var internalQueue = DispatchQueue(label: "CocoaMQTTWebSocket")

private var connection: CocoaMQTTWebSocketConnection?

private func reset() {
Expand Down Expand Up @@ -256,8 +258,17 @@ public class CocoaMQTTWebSocket: CocoaMQTTSocketProtocol {
}

extension CocoaMQTTWebSocket: CocoaMQTTWebSocketConnectionDelegate {
public func connection(_ conn: CocoaMQTTWebSocketConnection, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Swift.Void) {
guard conn.isEqual(connection) else { return }
public func urlSessionConnection(_ conn: CocoaMQTTWebSocketConnection, didReceiveTrust trust: SecTrust, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let del = delegate {
__delegate_queue {
del.socketUrlSession(self, didReceiveTrust: trust, didReceiveChallenge: challenge, completionHandler: completionHandler)
}
} else {
completionHandler(.performDefaultHandling, nil)
}
}

public func connection(_ conn: CocoaMQTTWebSocketConnection, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
if let del = delegate {
__delegate_queue {
del.socket(self, didReceive: trust, completionHandler: completionHandler)
Expand Down Expand Up @@ -366,6 +377,8 @@ extension CocoaMQTTWebSocket.FoundationConnection: URLSessionWebSocketDelegate {
delegate.connection(self, didReceive: trust) { shouldTrust in
completionHandler(shouldTrust ? .performDefaultHandling : .rejectProtectionSpace, nil)
}
delegate.urlSessionConnection(self, didReceiveTrust: trust, didReceiveChallenge: challenge, completionHandler: completionHandler)

} else {
completionHandler(.performDefaultHandling, nil)
}
Expand Down

0 comments on commit 2df529b

Please sign in to comment.