I have m2mqtt_srv.crt
SSL certificate file.
I am using CocoaMQTT
library.
I want to connect my MQTT server on port 8883.
Below is my tried code.
import UIKit
import CocoaMQTT
class ViewController: UIViewController, CocoaMQTTDelegate {
@IBOutlet var statusLabel: UILabel!
var mqtt: CocoaMQTT?
override func viewDidLoad() {
super.viewDidLoad()
selfSignedSSLSetting()
let a = mqtt?.connect()
print(a as Any)
print(mqtt?.connState as Any)
}
func selfSignedSSLSetting() {
let clientID = "AWS"
let defaultHost = "18.159.54.124"
mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 8883)
mqtt!.username = ""
mqtt!.password = ""
mqtt!.keepAlive = 60
mqtt!.delegate = self
mqtt!.enableSSL = true
mqtt!.allowUntrustCACertificate = true
let clientCertArray = getClientCertFromP12File(certName: "m2mqtt_srv", certPassword: "MySecretPassword")
var sslSettings: [String: NSObject] = [:]
sslSettings[kCFStreamSSLCertificates as String] = clientCertArray
mqtt!.sslSettings = sslSettings
}
func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState) {
statusLabel.text = state.description
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
print(ack)
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
print(message)
}
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
print(id)
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
print(message)
}
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topics: [String]) {
print(topics)
}
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
print(topic)
}
func mqttDidPing(_ mqtt: CocoaMQTT) {
print(mqtt)
print("Status: (mqtt.connState)")
}
func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
print(mqtt)
}
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
print(err as Any)
statusLabel.text = err?.localizedDescription
}
func getClientCertFromP12File(certName: String, certPassword: String) -> CFArray? {
// get p12 file path
let resourcePath = Bundle.main.path(forResource: certName, ofType: "crt")
guard let filePath = resourcePath, let p12Data = NSData(contentsOfFile: filePath) else {
print("Failed to open the certificate file: (certName).crt")
return nil
}
// create key dictionary for reading p12 file
let key = kSecImportExportPassphrase as String
let options : NSDictionary = [key: certPassword]
var items : CFArray?
let securityError = SecPKCS12Import(p12Data, options, &items)
guard securityError == errSecSuccess else {
if securityError == errSecAuthFailed {
print("ERROR: SecPKCS12Import returned errSecAuthFailed. Incorrect password?")
} else {
print("Failed to open the certificate file: (certName).crt")
}
return nil
}
guard let theArray = items, CFArrayGetCount(theArray) > 0 else {
return nil
}
let dictionary = (theArray as NSArray).object(at: 0)
guard let identity = (dictionary as AnyObject).value(forKey: kSecImportItemIdentity as String) else {
return nil
}
let certArray = [identity] as CFArray
return certArray
}
}
Can you please let me know How I have to connect to MQTT server with using m2mqtt_srv.crt
this certificate?
Or
Is m2mqtt_srv.crt SSL certificate I have to use or something with extension .p12 file I have to use?
I have no idea how to do it?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…