2017-05-04 97 views
0

我的應用通過ssl連接使用證書文件連接到mqtt經紀商。Android,MQTT:經紀商證書到期

我在代理中設置證書文件(.crt)和密鑰文件(.key)。

我使用Paho Mqtt客戶端API,並且此API需要SSLSocketFactory來建立連接。

我使用Google示例創建了一個帶TrustManager的SSLSocketFactory實例,該TrustManager在密鑰庫中信任我們的CA.

// Load CAs from an InputStream 
// (could be from a resource or ByteArrayInputStream or ...) 
CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
InputStream caInput = new BufferedInputStream(new 
FileInputStream("load-der.crt")); 
Certificate ca; 

try { 
    ca = cf.generateCertificate(caInput); 
    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); 
} finally { 
    caInput.close(); 
} 

// Create a KeyStore containing our trusted CAs 
String keyStoreType = KeyStore.getDefaultType(); 
KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
keyStore.load(null, null); 
keyStore.setCertificateEntry("ca", ca); 

// Create a TrustManager that trusts the CAs in our KeyStore 
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
tmf.init(keyStore); 

// Create an SSLContext that uses our TrustManager 
SSLContext context = SSLContext.getInstance("TLS"); 
context.init(null, tmf.getTrustManagers(), null); 

現在一切工作正常。但是,證書將在2017年5月30日到期。

我該如何處理客戶端(Android)中的證書?

我已經在測試環境中使用新證書進行了測試。該應用程序無法連接到經紀人。例外是:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 

如何處理Android上的證書續訂?

+0

你的意思是撤銷或更新,而不是「翻新」?同時更新證書並將其分發到設備將完全取決於您使用的CA. – hardillb

+0

@hardillb我的意思是更新。謝謝。 – Kingslayerpy

回答

0

我已經使用相同的.key文件申請了一個新的證書,並且所有的客戶端都沒有用新的.crt連接到代理的任何問題。

但是,使用相同的密鑰進行證書更新安全嗎?