Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handshake failed on netty 4.1.21 running under Android 5.0 #7758

Closed
MarkVilkel opened this issue Mar 1, 2018 · 5 comments
Closed

Handshake failed on netty 4.1.21 running under Android 5.0 #7758

MarkVilkel opened this issue Mar 1, 2018 · 5 comments
Assignees
Labels
Milestone

Comments

@MarkVilkel
Copy link

Hi!
I've got an exception on Android 5.0 with netty 4.1.21:

javax.net.ssl.SSLHandshakeException: Handshake failed
    io.netty.handler.codec.DecoderException: javax.net.ssl.SSLHandshakeException: Handshake failed
	at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459) [na:0.0]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265) [na:0.0]
Caused by: javax.net.ssl.SSLHandshakeException: Handshake failed
	at com.android.org.conscrypt.OpenSSLEngineImpl.unwrap(OpenSSLEngineImpl.java:436) [na:0.0]
    at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:1006) [na:0.0]
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9d892e00: Failure in SSL library, usually a protocol error
                                                                             error:1408E0F4:SSL routines:SSL3_GET_MESSAGE:unexpected message (external/openssl/ssl/s3_both.c:498 0xac8b4ce0:0x00000000)
	at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake_bio(Native Method) [na:0.0]
    at com.android.org.conscrypt.OpenSSLEngineImpl.unwrap(OpenSSLEngineImpl.java:423) [na:0.0]

So Android app is unable to establish ssl connection.

I have had the same error when we were using mina library, however I've solved it by adding:

            if (!inNetBuffer.hasRemaining()) {
                res = new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);

like it was suggested by Internets in org.apache.mina.filter.support.SSLHandler.unwrap0() method:

    private SSLEngineResult unwrap0() throws SSLException {
        SSLEngineResult res;
        do {
            if (SessionLog.isDebugEnabled(session)) {
                SessionLog.debug(session, "   inNetBuffer: " + inNetBuffer);
                SessionLog.debug(session, "   appBuffer: " + appBuffer);
            }
            if (!inNetBuffer.hasRemaining()) {
                res = new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);
            } else {
                res = sslEngine.unwrap(inNetBuffer, appBuffer);
            }
            if (SessionLog.isDebugEnabled(session)) {
                SessionLog.debug(session, " Unwrap res:" + res);
            }
        } while (res.getStatus() == SSLEngineResult.Status.OK
                && (handshakeComplete && res.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING
                        || res.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP));
        
        return res;
    }

As I understand this is Android problem, described here https://issuetracker.google.com/issues/37017560
But we still have clients using Android 5.0, so we can not just ignore them.

Is not it possible to have such workaround in netty?

@normanmaurer
Copy link
Member

@MarkVilkel yes I think so... let me check

normanmaurer added a commit that referenced this issue Mar 2, 2018
Motivation:

Android 5.0 sometimes not correctly update the bytesConsumed of the SSLEngineResult when consuming data from the input ByteBuffer. This will lead to handshake failures.

Modifications:

Add a workaround for Android 5.0

Result:

Be able to use netty on Android 5.0 by fixing #7758 .
@normanmaurer
Copy link
Member

@MarkVilkel #7761 PTAL if this fixed it

normanmaurer added a commit that referenced this issue Mar 2, 2018
Motivation:

Android 5.0 sometimes not correctly update the bytesConsumed of the SSLEngineResult when consuming data from the input ByteBuffer. This will lead to handshake failures.

Modifications:

Add a workaround for Android 5.0

Result:

Be able to use netty on Android 5.0 by fixing #7758 .
@MarkVilkel
Copy link
Author

@normanmaurer thank you for a quick fix.
We've tested d0b43d1 on Android 5.0 - works fine.
Waiting this fix in the next netty version.

@johnou
Copy link
Contributor

johnou commented Mar 2, 2018

@MarkVilkel do you see this problem with a Android 5.0 client talking SSL with a Netty server eg. standalone java app, or using Netty inside an Android application on 5.0?

@MarkVilkel
Copy link
Author

@johnou we have an android application with netty client inside it connecting to netty transport server. Problem appeared only on Android 5.0

normanmaurer added a commit that referenced this issue Mar 3, 2018
Motivation:

Android 5.0 sometimes not correctly update the bytesConsumed of the SSLEngineResult when consuming data from the input ByteBuffer. This will lead to handshake failures.

Modifications:

Add a workaround for Android 5.0

Result:

Be able to use netty on Android 5.0 by fixing #7758 .
@normanmaurer normanmaurer self-assigned this Mar 3, 2018
@normanmaurer normanmaurer added this to the 4.1.23.Final milestone Mar 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants