在paho-Eclipse mqtt Android中的SSL/TLS证书固定

GOK*_*KUL 5 ssl android openssl mqtt paho

我可以使用paho {mqttv3:1.1.0}服务在没有TLS/SSL证书的情况下执行与Broker的MQTT连接.但是,当我尝试在Android中固定SSL证书时,它无法正常工作.请帮我解决这个问题.附加代码以获得更多洞察力.

第1步:证书转换

将certificate.pem文件转换为弹力城堡格式并存储在Android项目的原始文件夹中.

D:\>"C:\Program Files\Java\jdk1.8.0_121\bin\keytool" -import -alias mqtt-broker -file C:\Users\abcd\Downloads\certificate.pem  -keypass ***** -keystore raw_key_file  -storetype BKS -storepass ***** -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider  -providerpath C:\Users\abcd\Downloads\bcprov-jdk16-1.45.jar
Run Code Online (Sandbox Code Playgroud)

第2步:MQTT客户端设置

mclientPublisher = new MqttAndroidClient(context, mqttBrokerURL,PublisherClientID, new MemoryPersistence());
    if (null != mclientPublisher) 
       {
        MqttConnectOptions options = null;
        options = new MqttConnectOptions();
        options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
        options.setConnectionTimeout(60);
        options.setKeepAliveInterval(120);
        options.setAutomaticReconnect(true);
        SslUtility.newInstance(context);         
        options.setSocketFactory(SslUtility.getInstance().getSocketFactory
        (R.raw.raw_key_file,"****"));
        options.setCleanSession(true);
       }
Run Code Online (Sandbox Code Playgroud)

第3步:SSL实用程序类

 public class SslUtility {

   private static SslUtility     mInstance = null;
   private Context                mContext = null;
   private HashMap<Integer, SSLSocketFactory> mSocketFactoryMap = new 
   HashMap<Integer, SSLSocketFactory>();




   public SslUtility(Context context) {
    mContext = context;
   }

   public static SslUtility getInstance( ) {
    if ( null == mInstance ) {
            throw new RuntimeException("the first call must be to 
                                        SslUtility.newInstance(Context) ");
    }
    return mInstance;
  }


  public static SslUtility newInstance( Context context ) {
    if ( null == mInstance ) {
        mInstance = new SslUtility( context );
    }
    return mInstance;
  }


 public SSLSocketFactory getSocketFactory(int certificateId, String 
                                                      certificatePassword )
    {
     SSLSocketFactory result = mSocketFactoryMap.get(certificateId);
       if ( ( null == result) && ( null != mContext ) ) { 

        try {
          KeyStore keystoreTrust = KeyStore.getInstance("BKS");   
          keystoreTrust.load(mContext.getResources().
          openRawResource(certificateId),certificatePassword.toCharArray());
          TrustManagerFactory trustManagerFactory = 
          TrustManagerFactory.getInstance(TrustManagerFactory
                                              .getDefaultAlgorithm());
          trustManagerFactory.init(keystoreTrust);
          SSLContext sslContext = SSLContext.getInstance("TLS");
          sslContext.init(null, trustManagerFactory.getTrustManagers(), 
                                               new SecureRandom());
          result = sslContext.getSocketFactory();
          mSocketFactoryMap.put( certificateId, result);  
        }
        catch ( Exception ex ) {
        }
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

ERROR/logcat的:

 05-18 10:18:55.638 20810-20810/: disconnect method -inside catch block- Error is : java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.eclipse.paho.android.service.MqttAndroidClient.isConnected()' on a null object reference
Run Code Online (Sandbox Code Playgroud)

似乎没有创建MQTT客户端对象.

参考:

Android-MQTT库中的SSL证书固定[Paho]

泛美卫生组织,日食论坛

开发者Android-Google

HiveMQ-MQTT