您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何通過Android與MQTT進行TLS/SSL單向認證和雙向認證”,在日常操作中,相信很多人在如何通過Android與MQTT進行TLS/SSL單向認證和雙向認證問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何通過Android與MQTT進行TLS/SSL單向認證和雙向認證”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
MQTT 是一種輕量級的、靈活的物聯網消息交換和數據傳遞協議,致力于為 IoT 開發人員實現靈活性與硬件/網絡資源的平衡。為了確保通訊安全,通常使用 TLS/SSL 來進行通訊加密。
本文使用 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);
重點在于如何獲取 SSLSocketFactory
,下面對單向認證和雙向認證分別進行說明。
單向認證是指服務端認證客戶端,以下是核心代碼
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
下,然后調用
try { InputStream caCrtFileI = context.getResources().openRawResource(R.raw.ca); options.setSocketFactory(getSingleSocketFactory(caCrtFile)); } catch (Exception e) { e.printStackTrace(); }
雙向認證是指服務端和客戶端相互認證,以下是關鍵代碼
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(); }
我們需要準備好服務端證書,客戶端證書和秘鑰放到 res/raw
下,然后調用,注意密碼設為空字符串
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(); }
到此,關于“如何通過Android與MQTT進行TLS/SSL單向認證和雙向認證”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。