這篇文章主要介紹“如何通過(guò)Android與MQTT進(jìn)行TLS/SSL單向認(rèn)證和雙向認(rèn)證”,在日常操作中,相信很多人在如何通過(guò)Android與MQTT進(jìn)行TLS/SSL單向認(rèn)證和雙向認(rèn)證問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何通過(guò)Android與MQTT進(jìn)行TLS/SSL單向認(rèn)證和雙向認(rèn)證”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)在網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、成都APP應(yīng)用開(kāi)發(fā)、網(wǎng)站運(yùn)營(yíng)等方面均有出色的表現(xiàn),憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,我們更提供一系列營(yíng)銷型網(wǎng)站建設(shè),網(wǎng)站制作和網(wǎng)站推廣的服務(wù),以推動(dòng)各中小企業(yè)全面信息化,并利用創(chuàng)新技術(shù)幫助各行業(yè)提升企業(yè)形象和運(yùn)營(yíng)效率。
MQTT 是一種輕量級(jí)的、靈活的物聯(lián)網(wǎng)消息交換和數(shù)據(jù)傳遞協(xié)議,致力于為 IoT 開(kāi)發(fā)人員實(shí)現(xiàn)靈活性與硬件/網(wǎng)絡(luò)資源的平衡。為了確保通訊安全,通常使用 TLS/SSL 來(lái)進(jìn)行通訊加密。
本文使用 Eclipse Paho Android Service 和 BouncyCastle
,添加依賴
dependencies { implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' implementation 'org.bouncycastle:bcpkix-jdk15on:1.59' }
以下是 Android 連接 TLS/SSL 的核心代碼部分
MqttConnectOptions options = new MqttConnectOptions(); SSLSocketFactory sslSocketFactory = ... options.setSocketFactory(sslSocketFactory);
重點(diǎn)在于如何獲取 SSLSocketFactory
,下面對(duì)單向認(rèn)證和雙向認(rèn)證分別進(jìn)行說(shuō)明。
單向認(rèn)證是指服務(wù)端認(rèn)證客戶端,以下是核心代碼
public static SSLSocketFactory getSingleSocketFactory(InputStream caCrtFileInputStream) throws Exception { Security.addProvider(new BouncyCastleProvider()); X509Certificate caCert = null; BufferedInputStream bis = new BufferedInputStream(caCrtFileInputStream); CertificateFactory cf = CertificateFactory.getInstance("X.509"); while (bis.available() > 0) { caCert = (X509Certificate) cf.generateCertificate(bis); } KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType()); caKs.load(null, null); caKs.setCertificateEntry("cert-certificate", caCert); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(caKs); SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, tmf.getTrustManagers(), null); return sslContext.getSocketFactory(); }
我們把 ca.crt
放到 res/raw
下,然后調(diào)用
try { InputStream caCrtFileI = context.getResources().openRawResource(R.raw.ca); options.setSocketFactory(getSingleSocketFactory(caCrtFile)); } catch (Exception e) { e.printStackTrace(); }
雙向認(rèn)證是指服務(wù)端和客戶端相互認(rèn)證,以下是關(guān)鍵代碼
public static SSLSocketFactory getSocketFactory(InputStream caCrtFile, InputStream crtFile, InputStream keyFile, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); // load CA certificate X509Certificate caCert = null; BufferedInputStream bis = new BufferedInputStream(caCrtFile); CertificateFactory cf = CertificateFactory.getInstance("X.509"); while (bis.available() > 0) { caCert = (X509Certificate) cf.generateCertificate(bis); } // load client certificate bis = new BufferedInputStream(crtFile); X509Certificate cert = null; while (bis.available() > 0) { cert = (X509Certificate) cf.generateCertificate(bis); } // load client private cert PEMParser pemParser = new PEMParser(new InputStreamReader(keyFile)); Object object = pemParser.readObject(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); KeyPair key = converter.getKeyPair((PEMKeyPair) object); KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType()); caKs.load(null, null); caKs.setCertificateEntry("cert-certificate", caCert); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(caKs); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(null, null); ks.setCertificateEntry("certificate", cert); ks.setKeyEntry("private-cert", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[]{cert}); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, password.toCharArray()); SSLContext context = SSLContext.getInstance("TLSv1.2"); context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); return context.getSocketFactory(); }
我們需要準(zhǔn)備好服務(wù)端證書(shū),客戶端證書(shū)和秘鑰放到 res/raw
下,然后調(diào)用,注意密碼設(shè)為空字符串
try { InputStream caCrtFile = context.getResources().openRawResource(R.raw.ca); InputStream crtFile = context.getResources().openRawResource(R.raw.cert); InputStream keyFile = context.getResources().openRawResource(R.raw.key); options.setSocketFactory(getSocketFactory(caCrtFile, crtFile, keyFile, "")); } catch (Exception e) { e.printStackTrace(); }
到此,關(guān)于“如何通過(guò)Android與MQTT進(jìn)行TLS/SSL單向認(rèn)證和雙向認(rèn)證”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!