Smack 4.1.8ライブラリを使用してXMPP経由でAndroid(23)でチャットアプリケーションを構築しようとしています。私はこれを携帯電話で直接テストしています(Galaxy S6とGalaxy E7)。私はxmpp.jpに接続しようとしています。接続しようとするとこの例外が発生します。Android(23) - Smack(4.1.8) - XMPP - "SSL23_GET_SERVER_HELLO:不明なプロトコル"エラー
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.google.android.gms.org.conscrypt.OpenSSLSocketImpl.startHandshake(:com.google.android.gms:405)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.google.android.gms.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(:com.google.android.gms:638)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.google.android.gms.org.conscrypt.OpenSSLSocketImpl.getInputStream(:com.google.android.gms:600)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection.initReaderAndWriter(XMPPTCPConnection.java:659)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.java:629)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:855)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:364)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.example.android.proximitychat.Utility.MyXMPP$2.doInBackground(MyXMPP.java:247)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.example.android.proximitychat.Utility.MyXMPP$2.doInBackground(MyXMPP.java:227)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at java.lang.Thread.run(Thread.java:818)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb88c5e50: Failure in SSL library, usually a protocol error
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (third_party/java/android_libs/gcore/migration_libs/packages/external/openssl/ssl/s23_clnt.c:795 0x9c5f2dcc:0x00000000)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
08-13 00:49:49.017 15912-16034/com.example.android.proximitychat W/System.err: at com.google.android.gms.org.conscrypt.OpenSSLSocketImpl.startHandshake(:com.google.android.gms:333)
これはtutorialを使用しています。私はDummySSLSocketFactory、[NoSSLFactory]と[TLSFactory]ソリューションを試しました(2つ以上のリンクを投稿できません:|)。彼らの誰も私のために働いていない。
私の接続コードは次のとおりです。
private void initialiseConnection() {
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
.builder();
//config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
config.setServiceName(serverAddress);
config.setHost(serverAddress);
//config.setPort(5222);
config.setPort(5222);
config.setDebuggerEnabled(true);
SSLContext sslContext = null;
try {
sslContext = createSSLContext(context);
//sslContext.getSupportedSSLParameters();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
//} catch(NoSuchProviderException e){
// e.printStackTrace();
}
//config.setSocketFactory(new DummySSLSocketFactory());
//config.setSocketFactory(SSLSocketFactory.getDefault());
config.setCustomSSLContext(sslContext);
//SSLSocketFactory socketFactory = sslContext.getSocketFactory();
//NoSSLv3SocketFactory socketFactory = new NoSSLv3SocketFactory(sslContext.getSocketFactory());
TLSSocketFactory socketFactory = new TLSSocketFactory(sslContext);
//SSLSocketFactory noSSLv3Factory = new TlsOnlySocketFactory(sslContext.getSocketFactory());
config.setSocketFactory(socketFactory);
config.setEnabledSSLProtocols(new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"});
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
connection = new XMPPTCPConnection(config.build());
XMPPConnectionListener connectionListener = new XMPPConnectionListener();
connection.addConnectionListener(connectionListener);
}
private SSLContext createSSLContext(Context context) throws KeyStoreException,
NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {
KeyStore trustStore;
InputStream in = null;
trustStore = KeyStore.getInstance("BKS");
in = context.getResources().openRawResource(R.raw.keystore1);
trustStore.load(in, "MY_STORE_PASSWORD".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
//SSLContext sslContext = SSLContext.getInstance("TLS");
SSLContext sslContext = null;
try {
//sslContext = SSLContext.getInstance("TLSv1", "AndroidOpenSSL");
sslContext = SSLContext.getInstance("TLS");
}
catch(Exception e){
e.printStackTrace();
}
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
Log.d("SSL Protocol: ", sslContext.getProtocol());
Log.d("SSL Provider: ", sslContext.getProvider().toString());
String[] protocols = sslContext.getDefaultSSLParameters().getProtocols();
sslContext.getDefaultSSLParameters().setProtocols(protocols);
return sslContext;
}
TLSFactoryコードは次のとおりです。
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}
public TLSSocketFactory(SSLContext sslContext){
internalSSLSocketFactory = sslContext.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket());
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
//((SSLSocket)socket).setEnabledProtocols(new String[] {"SSLv3","TLSv1", "TLSv1.1", "TLSv1.2"});
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"});
Log.d("TLSSocketFactory: ", "setting TLS list");
}
if(socket!= null) {
Log.d("TLSSocketFactory: ", "socket class: " +socket.getClass());
}
Log.d("TLSSocketFactory: ","Returning TLS Enabled Socket");
return socket;
}
}
私はこのために見つけることができるすべてのソリューションを試してみましたが、まだXMPPサーバーに接続することができませんしています。私は同様の結果を得て、wtfismyip.comでjabberサーバーを試しました。この時点で2週間以上立ち往生してください。このロードブロッキングを乗り越えるための助けに感謝します。