MQTT Paho Android 支持SSL/TLS(亲测有效)

登录时支持ssl的交互

这是调测登录界面设计

图片

代码中对ssl/tls的支持

使用MqttAndroidClient配置mqtt客户端请求时,不加密及加密方式连接存在以下几点差异:

url及端口差异

 val uri: String = if (tlsConnection) {
                "ssl://$host:$port"
            } else {
                "tcp://$host:$port"
            }

支持tls时,url前缀是ssl: 普通mqtt连接时候,前缀是tcp

端口差异:tcp请求时,默认端口1883 ssl请求时,默认端口是8883

socketFactory配置项

笔者项目中只支持单向验证,即客户端验证服务端,所以需要在客户端加载服务端证书用于ssl连接

 if(connection.isSSL == 1){
            //单项验证,客户端验证服务端,onenet提供的.pem证书,需要用keytool转成java支持的bks、或者jks等
            connOpts.socketFactory = connection.client.getSSLSocketFactory(context.assets.open("MQTTS-certificate.bks"), "12345678")
            connOpts.isHttpsHostnameVerificationEnabled = false
            connOpts.setSSLHostnameVerifier { _, _ -> true  }
        }

说明:

  1. 1. java中不支持pem证书加载,所以需要使用keytool工具将pem格式证书转成java/android支持的bks或者jks等

 keytool -importcert -v -trustcacerts -file ./MQTTS-certificate.pem -alias ca -keystore ./mqtt.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath ./bcprov-ext-jdk18on-176.jar

bcprov-ext-jdk18on-176.jar需要从以下地址下载 https://www.bouncycastle.org/latest_releases.html

  1. 1. 证书中会涉及域名验证,如果证书中缺少这个字段,那么运行时候会报下面错误 MqttException (0) - javax.net.ssl.SSLHandshakeException: No subjectAltNames on the certificate match 解决办法是跳过域名及host验证的流程

connOpts.isHttpsHostnameVerificationEnabled = false
connOpts.setSSLHostnameVerifier { _, _ -> true  }

client.getSSLSocketFactory实现

下面我们看看MqttAndroidClient创建sslSocketFactory的具体实现代码。

//info.mqtt.android.service.MqttAndroidClient
 /**
     * Get the SSLSocketFactory using SSL key store and password
     * A convenience method, which will help user to create a SSLSocketFactory
     * object
     *
     * @param keyStore the SSL key store which is generated by some SSL key tool,
     * such as keytool in Java JDK
     * @param password the password of the key store which is set when the key store
     * is generated
     * @return SSLSocketFactory used to connect to the server with SSL
     * authentication
     * @throws MqttSecurityException if there was any error when getting the SSLSocketFactory
     */
    @Throws(MqttSecurityException::class)
    fun getSSLSocketFactory(keyStore: InputStream?, password: String): SSLSocketFactory {
        return try {
            val sslSockFactory: SSLSocketFactory
            val ts: KeyStore = KeyStore.getInstance("BKS")
            ts.load(keyStore, password.toCharArray())
            val tmf = TrustManagerFactory.getInstance("X509")
            tmf.init(ts)
            val tm = tmf.trustManagers
            val ctx: SSLContext = SSLContext.getInstance("TLSv1")
            ctx.init(null, tm, null)
            sslSockFactory = ctx.socketFactory
            sslSockFactory
        } catch (e: KeyStoreException) {
            throw MqttSecurityException(e)
        } catch (e: CertificateException) {
            throw MqttSecurityException(e)
        } catch (e: IOException) {
            throw MqttSecurityException(e)
        } catch (e: NoSuchAlgorithmException) {
            throw MqttSecurityException(e)
        } catch (e: KeyManagementException) {
            throw MqttSecurityException(e)
        }
    }

Github

https://github.com/hannesa2/paho.mqtt.android https://github.com/eclipse/paho.mqtt.android

项目中涉及的sample示例代码很值得一探究竟,对你掌握MQTT相关支持很有帮助喔!

转自:MQTT Paho Android 支持SSL/TLS(亲测有效)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我无法直接为您提供完整的协议栈代码。协议栈的实现可能因硬件和操作系统不同而有所不同,需要根据具体情况进行适当的修改。 以下是一些常用协议栈的简介及其相关资源,供您参考: 1. WIFI协议栈:WIFI协议栈主要包括PHY层、MAC层和网络层。常用的WIFI协议包括IEEE 802.11a/b/g/n/ac/ax等。如果您需要实现WIFI协议栈,您可以参考以下资源: - ESP8266 WIFI模块源代码:https://github.com/espressif/esp8266-rtos-sdk/tree/master/components/esp8266/include/driver/include/driver - ESP32 WIFI模块源代码:https://github.com/espressif/esp-idf/tree/master/components/esp_wifi - Linux下的WIFI实现:https://wireless.wiki.kernel.org/en/developers/documentation/wireless-drivers 2. 蓝牙协议栈:蓝牙协议栈主要包括PHY层、MAC层、L2CAP层、RFCOMM层、SDP层等。常用的蓝牙协议包括BLE、Classic Bluetooth等。如果您需要实现蓝牙协议栈,您可以参考以下资源: - BlueZ蓝牙协议栈:https://git.kernel.org/pub/scm/bluetooth/bluez.git/ - Android蓝牙协议栈:https://android.googlesource.com/platform/system/bt/ - Nordic Semiconductor的nRF5 SDK:https://www.nordicsemi.com/Software-and-Tools/Software/nRF5-SDK 3. TCP/IP协议栈:TCP/IP协议栈主要包括物理层、数据链路层、网络层、传输层和应用层。常用的TCP/IP协议包括TCP、UDP、IP等。如果您需要实现TCP/IP协议栈,您可以参考以下资源: - lwIP协议栈:http://www.nongnu.org/lwip/ - Linux内核中的TCP/IP协议栈实现:https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt - Contiki OS中的TCP/IP协议栈实现:https://github.com/contiki-os/contiki/tree/master/os/net 4. CoAP协议栈:CoAP协议栈是一种轻量级的RESTful协议,适用于物联网设备之间的通信。如果您需要实现CoAP协议栈,您可以参考以下资源: - Erbium CoAP协议栈:https://github.com/contiki-os/er-coap - LibCoAP协议栈:https://github.com/obgm/libcoap - Californium CoAP协议栈:https://github.com/eclipse/californium 5. MQTT协议栈:MQTT协议栈是一种轻量级的消息协议,适用于物联网设备之间的通信。如果您需要实现MQTT协议栈,您可以参考以下资源: - Paho MQTT协议栈:https://github.com/eclipse/paho.mqtt.embedded-c - Mosquitto MQTT协议栈:https://github.com/eclipse/mosquitto - Eclipse IoT MQTT协议栈:https://www.eclipse.org/paho/clients/c/embedded/ 6. HTTP(S)协议栈:HTTP(S)协议栈是一种广泛应用于互联网上的应用层协议。如果您需要实现HTTP(S)协议栈,您可以参考以下资源: - Mongoose HTTP(S)协议栈:https://github.com/cesanta/mongoose - libcurl HTTP(S)协议栈:https://curl.se/libcurl/ - Microchip HTTP(S)协议栈:https://www.microchip.com/design-centers/wireless-connectivity/wifi/products/wi-fi-software/mrf24w-software 7. SSL/TLS协议栈:SSL/TLS协议栈是一种安全传输协议,适用于互联网上的安全通信。如果您需要实现SSL/TLS协议栈,您可以参考以下资源: - OpenSSL SSL/TLS协议栈:https://www.openssl.org/ - wolfSSL SSL/TLS协议栈:https://www.wolfssl.com/ - mbed TLS SSL/TLS协议栈:https://tls.mbed.org/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值