The title is my question. Android (I'm using API level 23) gives me an error whenever I try to just replace all instances of Socket with SSLSocket when using SSLSocketFactory. I believe the handshake fails because Android needs a certain security level implemented into the SSLSocket to allow it to work. However, I'm lost on how to do all this. It's my first time dealing with SSLSockets and encryption, but I know that I want an RSA-based SSLSocket with client authorization. I'm stuck on the part involving creating my own public and private keys and there's something about CA certificates and keystores - I don't understand how I would go at implementing this, and do I even need CA certificates if it's between devices in the same WiFi? I'm not connecting anything to the web.
I've attached a re-creation of my previous attempt on server and client side for making an SSLSocket with the Android error below that. Clarification and sample code would be greatly appreciated. Thank you.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
try {
//server side
int port = 8000;
SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
serverSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port);
serverSocket.setEnabledProtocols(new String[]{"TLSv1.2"});
while (serverSocket != null && !serverSocket.isClosed()) {
SSLSocket socket = (SSLSocket) serverSocket.accept();
//socket.setKeepAlive(true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
return START_STICKY;
}
//client side - would be on a different device in a real use case
public void clientConnect() {
int port = 8000;
SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = f.createSocket("192.168.1.175", port);
}
The error I'm getting when trying to send data between the established connection on Android is:
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: javax.net.ssl.SSLHandshakeException: Read error: ssl=0x75b6f94808: Failure in SSL library, usually a protocol error
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: error:100000b8:SSL routines:OPENSSL_internal:NO_SHARED_CIPHER (external/boringssl/src/ssl/handshake_server.cc:705 0x760bfc205a:0x00000000)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.SSLUtils.toSSLHandshakeException(SSLUtils.java:362)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.convertException(ConscryptEngine.java:1134)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:919)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:747)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:712)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:849)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.access$100(ConscryptEngineSocket.java:722)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:238)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:217)
2021-01-25 21:24:07.423 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.waitForHandshake(ConscryptEngineSocket.java:563)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngineSocket.getOutputStream(ConscryptEngineSocket.java:298)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.harout.smssync.SharedResources.send(SharedResources.java:46)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.harout.smssync.MainActivity$2$1.run(MainActivity.java:81)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: Caused by: javax.net.ssl.SSLProtocolException: Read error: ssl=0x75b6f94808: Failure in SSL library, usually a protocol error
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: error:100000b8:SSL routines:OPENSSL_internal:NO_SHARED_CIPHER (external/boringssl/src/ssl/handshake_server.cc:705 0x760bfc205a:0x00000000)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.NativeCrypto.ENGINE_SSL_read_direct(Native Method)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.NativeSsl.readDirectByteBuffer(NativeSsl.java:568)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.readPlaintextDataDirect(ConscryptEngine.java:1095)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:1079)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:876)
2021-01-25 21:24:07.424 9003-9388/com.harout.smssync W/System.err: ... 10 more
question from:
https://stackoverflow.com/questions/65896299/how-can-i-create-an-sslsocket-on-android-for-sending-data-between-devices-on-the 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…