AndroidAsync icon indicating copy to clipboard operation
AndroidAsync copied to clipboard

Fatal alert received bad_record_mac

Open Savic opened this issue 12 years ago • 14 comments

Hello!

I am using AndroidAsync 1.3.8 connecting to a websocket (wss) server supporting TLSv1, TLSv1.1, TLSv1.2, SSLv3 using Android 4.4.4 on a Nexus 7 and I get the following Exception (also tried with android 4.3 and had the same issue):

08-22 11:51:32.730 5230-5361/ W/System.err﹕ javax.net.ssl.SSLException: Fatal alert received bad_record_mac 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.android.org.conscrypt.SSLEngineImpl.unwrap(SSLEngineImpl.java:484) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncSSLSocketWrapper$3.onDataAvailable(AsyncSSLSocketWrapper.java:261) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:33) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:61) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.Util.emitAllData(Util.java:20) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:175) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:758) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:603) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37) 08-22 11:51:32.740 5230-5361/ W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:552)

The biggest problem of mine is that i cannot reproduce the error. The exception is thrown "randomly" (subjectivly spoken) while receiving data from the wesocket server. For example 1 out of 10 "fresh" connections with the server receives the exception. I would be thankful for any help!

Savic avatar Sep 04 '14 19:09 Savic

Is the websocket server the same one every time?

koush avatar Sep 04 '14 21:09 koush

Thx for the fast replay! Yes the websocket server is always the same!

Savic avatar Sep 05 '14 04:09 Savic

what version of ion? Can you link me to the server so I can see the certificate?

koush avatar Sep 05 '14 17:09 koush

I am not using ION at all, I am only using AndroidAsync 1.3.8 as I only need webSockets, should i try using ION with webSockets? I also should mention that the exception occures when data is rapidly sent and received. I am sorry but for now I do not have the permission to link the server, I will try getting the permission on the coming monday!

Savic avatar Sep 05 '14 17:09 Savic

ok will take a look, it may be a buffering issue.

koush avatar Sep 05 '14 17:09 koush

thank you, I will try to get the cert. info as soon as possible!

Savic avatar Sep 05 '14 17:09 Savic

Are you sure this is 1.3.8? The stack trace doesn't line up at all with the code. I think you're running 137 or lower.

138 actually includes a bunch of SSL fixes, specifically for conscrypt (4.4.4) and the L preview.

koush avatar Sep 05 '14 17:09 koush

Yes you are right, these loggings are from 1.3.7, it was my fault, copy/past failure! But I got the same exception on 1.3.8 and that is why I wrote 1.3.8. I am sorry for this mistake, I will try to put the 1.3.8 loggings with the exception tommorrow!

Savic avatar Sep 05 '14 18:09 Savic

Yes, a new log would be helpful. Also isolating the issue with only reads or only writes would help too.

koush avatar Sep 05 '14 20:09 koush

I isolated the websocket only with sending and receiving and could not reproduce the exception (I do not think that its a buffering problem).

Here we go the new log with 1.3.8 (took longer due to weekend):

09-08 11:24:30.629 1418-1546/ W/System.err﹕ javax.net.ssl.SSLException: Fatal alert received bad_record_mac 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.android.org.conscrypt.SSLEngineImpl.unwrap(SSLEngineImpl.java:484) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncSSLSocketWrapper$4.onDataAvailable(AsyncSSLSocketWrapper.java:172) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:33) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:61) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.Util.emitAllData(Util.java:20) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:175) 09-08 11:24:30.629 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:766) 09-08 11:24:30.639 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:608) 09-08 11:24:30.639 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37) 09-08 11:24:30.639 1418-1546/ W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:557)

I also got a "similar" exception like above with 1.3.8:

09-08 10:36:15.505 16209-16339/ W/System.err﹕ javax.net.ssl.SSLProtocolException: Bad record MAC 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.android.org.conscrypt.ConnectionStateTLS.decrypt(ConnectionStateTLS.java:342) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.android.org.conscrypt.ConnectionState.decrypt(ConnectionState.java:132) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.android.org.conscrypt.SSLRecordProtocol.unwrap(SSLRecordProtocol.java:382) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.android.org.conscrypt.SSLEngineImpl.unwrap(SSLEngineImpl.java:463) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncSSLSocketWrapper$4.onDataAvailable(AsyncSSLSocketWrapper.java:172) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:33) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.BufferedDataEmitter.onDataAvailable(BufferedDataEmitter.java:61) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.Util.emitAllData(Util.java:20) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:175) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:766) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:608) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37) 09-08 10:36:15.505 16209-16339/ W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:557)

Savic avatar Sep 08 '14 10:09 Savic

And for the cert. information, the link: https://staging.myfly.cc/login.html

Savic avatar Sep 08 '14 10:09 Savic

And here is also the websocket-server: wss://staging.myfly.cc/signalingnode ; after 5 sec you get auto. disconnected from the websocket-server if you dont auth. yourself

Savic avatar Sep 09 '14 13:09 Savic

will check thanks

koush avatar Sep 09 '14 15:09 koush

+1 for me. I can reproduce this pretty reliably in a test case. It happened occasionally in 1.3.8, but much more frequently in my test case with 2.1.3. I'm using a socket.io 0.9 server that supports only TLSv1 TLSv1.1 TLSv1.2

In my test case I connect the socket, and then send a series of calls to the same event through the socket (I'm testing our rate-limiting code).

If I do essentially this:

public void testRateLimit() {
    client = connectsocket(); // method to set up the socket, etc.
    sendEvent(1);
    waitForRateLimitToGetHit();
}

public void sendEvent(Integer attempt) {
    if (attempt < 30) {
        client.emit(event, data, new Acknowledge() {
                @Override
                public void acknowledge(JSONArray arguments) {
                    sendEvent(attempt + 1); // Once finished, emit again
                });
    }
}

With the above code, everything works fine. If I change the test to this:

public void testRateLimit() {
    client = connectsocket(); // method to set up the socket, etc.
    sendEvent(1);
    sendEvent(1); // start a second simultaneous loop of emits
    sendEvent(1); // and a third!
    waitForRateLimitToGetHit();
}

I will frequently see crashes that look like either this:

04-24 10:16:19.928  16468-16559/ W/System.err﹕ javax.net.ssl.SSLException: Fatal alert received bad_record_mac
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.SSLEngineImpl.unwrap(SSLEngineImpl.java:485)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncSSLSocketWrapper$5.onDataAvailable(AsyncSSLSocketWrapper.java:191)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.Util.emitAllData(Util.java:22)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:146)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:788)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:626)
04-24 10:16:19.938  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:41)
04-24 10:16:19.948  16468-16559/ W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:568)

or this:

04-24 11:10:26.716    6192-6303/ W/System.err﹕ javax.net.ssl.SSLProtocolException: Bad record MAC
04-24 11:10:26.716    6192-6303/ W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.ConnectionStateTLS.decrypt(ConnectionStateTLS.java:342)
04-24 11:10:26.726    6192-6303/ W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.ConnectionState.decrypt(ConnectionState.java:132)
04-24 11:10:26.726    6192-6303/ W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.SSLRecordProtocol.unwrap(SSLRecordProtocol.java:382)
04-24 11:10:26.726    6192-6303/ W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.SSLEngineImpl.unwrap(SSLEngineImpl.java:464)
04-24 11:10:26.736    6192-6303/ W/System.err﹕ at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383)
04-24 11:10:26.736    6192-6303/ W/System.err﹕ at com.koushikdutta.async.AsyncSSLSocketWrapper$5.onDataAvailable(AsyncSSLSocketWrapper.java:191)
04-24 11:10:26.736    6192-6303/ W/System.err﹕ at com.koushikdutta.async.Util.emitAllData(Util.java:22)
04-24 11:10:26.736    6192-6303/ W/System.err﹕ at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:146)
04-24 11:10:26.736    6192-6303/ W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:788)
04-24 11:10:26.736    6192-6303/com.digium.respokesdktest W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:626)
04-24 11:10:26.746    6192-6303/com.digium.respokesdktest W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:41)
04-24 11:10:26.746    6192-6303/com.digium.respokesdktest W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:568)

If I change the test to this:

public void testRateLimit() {
    client = connectsocket(); // method to set up the socket, etc.
    sendEvent(1);
    sendEvent(1); // start a second simultaneous loop of emits
    sendEvent(1); // and a third!
    sendEvent(1);
    sendEvent(1); 
    sendEvent(1); // six simultaneous loops
    waitForRateLimitToGetHit();
}

The socket will just hang after 3 or so events are actually sent. Any ideas? I'm building an SDK other developers would use, so I don't have a whole lot of control on the sequence of calls to client.emit() without building my own queueing system on top of yours. Any ideas?

jznadams avatar Apr 24 '15 15:04 jznadams