grpc-java
grpc-java copied to clipboard
Using SSLCertificateSocketFactory for TLS handshake doesn't work externally for Android older than 5.0
Currently, internal Android projects are using SSLCertificateSocketFactory for TLS handshake, but since ALPN only works well with Android 5.0 or newer, they use NPN for Android 4.4 and older.
It works just because internal OKHttp has been hacked to support NPN.
With the external OKHttp, it will failed as:
com.google.common.util.concurrent.UncheckedExecutionException: io.grpc.StatusRuntimeException: INTERNAL: Failed starting transport
at io.grpc.stub.Calls.getUnchecked(Calls.java:117)
at io.grpc.stub.Calls.blockingUnaryCall(Calls.java:129)
at io.grpc.android.integrationtest.TestServiceGrpc$TestServiceBlockingStub.emptyCall(TestServiceGrpc.java:331)
at io.grpc.android.integrationtest.IntegrationTester.emptyUnary(IntegrationTester.java:93)
at io.grpc.android.integrationtest.IntegrationTester.runTest(IntegrationTester.java:72)
at io.grpc.android.integrationtest.GrpcTestTask.doInBackground(GrpcTestTask.java:53)
at io.grpc.android.integrationtest.GrpcTestTask.doInBackground(GrpcTestTask.java:12)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: io.grpc.StatusRuntimeException: INTERNAL: Failed starting transport
at io.grpc.Status.asRuntimeException(Status.java:428)
at io.grpc.stub.Calls$UnaryStreamToFuture.onClose(Calls.java:324)
at io.grpc.ChannelImpl$CallImpl$ClientStreamListenerImpl$3.run(ChannelImpl.java:402)
at io.grpc.SerializingExecutor$TaskRunner.run(SerializingExecutor.java:152)
... 3 more
Caused by: java.lang.RuntimeException: protocol negotiation failed
at com.squareup.okhttp.OkHttpTlsUpgrader.upgrade(OkHttpTlsUpgrader.java:95)
at io.grpc.transport.okhttp.OkHttpClientTransport.start(OkHttpClientTransport.java:293)
at io.grpc.ChannelImpl.obtainActiveTransport(ChannelImpl.java:199)
at io.grpc.ChannelImpl.access$600(ChannelImpl.java:55)
at io.grpc.ChannelImpl$CallImpl.start(ChannelImpl.java:266)
at io.grpc.stub.Calls.asyncServerStreamingCall(Calls.java:174)
at io.grpc.stub.Calls.unaryFutureCall(Calls.java:86)
at io.grpc.stub.Calls.blockingUnaryCall(Calls.java:129)
at io.grpc.android.integrationtest.TestServiceGrpc$TestServiceBlockingStub.emptyCall(TestServiceGrpc.java:331)
at io.grpc.android.integrationtest.IntegrationTester.emptyUnary(IntegrationTester.java:93)
at io.grpc.android.integrationtest.IntegrationTester.runTest(IntegrationTester.java:72)
at io.grpc.android.integrationtest.GrpcTestTask.doInBackground(GrpcTestTask.java:53)
at io.grpc.android.integrationtest.GrpcTestTask.doInBackground(GrpcTestTask.java:12)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
... 3 more
]>
at io.grpc.android.integrationtest.TesterActivityTest.testGrpc(TesterActivityTest.java:65)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
The reason is that even the socket has done handshake successfully with NPN, Platform.getSelectedProtocol only checks for ALPN, and returns null if ALPN is not supported.
Depending on the outcome of @zsurocking's tests, I would hope the solution here is "always use ALPN."