[英]Secured connection in grpc
I have issues in connecting to my server using gRPC. 使用gRPC连接到服务器时出现问题。 The server uses certificate files(rpc.cert and rpc.key) to authenticate but i do not know how to include those files. 服务器使用证书文件(rpc.cert和rpc.key)进行身份验证,但我不知道如何包括这些文件。 Currently this is the code i use to connect 目前这是我用来连接的代码
ManagedChannel channel = OkHttpChannelBuilder.forAddress("127.0.0.1", 9111)
.usePlaintext(true)
.build();
Using the above code throws this error 使用上面的代码会引发此错误
io.grpc.StatusRuntimeException: UNAVAILABLE: End of stream or IOExceptio
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.jav
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:202)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:131)
at com.dcrwallet.grpc.WalletLoaderServiceGrpc$WalletLoaderServiceBlo
at com.decrediton.MainActivity$2.onClick(MainActivity.java:86)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22641)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6285)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygote
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: javax.net.ssl.SSLHandshakeException: Handshake failed
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:444)
at io.grpc.okhttp.OkHttpProtocolNegotiator.negotiate(OkHttpProtocolNegotiator.java:93)
at io.grpc.okhttp.OkHttpProtocolNegotiator$AndroidNegotiator.negotiate(OkHttpProtocolNegotiator.java:159)
at io.grpc.okhttp.OkHttpTlsUpgrader.upgrade(OkHttpTlsUpgrader.java:63)
at io.grpc.okhttp.OkHttpClientTransport$1.run(OkHttpClientTransport.java:429)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb036ce80: Failure in SSL library, usually a protocol error error:1000006b:SSL routines:OPENSSL_internal:BAD_ECC_CERT (external/boringssl/src/ssl/s3_clnt.c:957 0xa74a5d15:0x00000000)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:362)
I cannot find any documentation on using grpc okhttp in android. 我找不到在android中使用grpc okhttp的任何文档。 The gRPC documentation by google does not include that so i pretty much don't know what to do about the error.Thanks 谷歌的gRPC文档不包含该内容,所以我几乎不知道该怎么办。
Since the server expects TLS, you can't use plaintext. 由于服务器需要TLS,因此不能使用纯文本。 Normally, you don't need to do anything; 通常,您不需要执行任何操作。 grpc-java Channels default to using TLS: grpc-java频道默认使用TLS:
ManagedChannel channel = OkHttpChannelBuilder.forAddress("127.0.0.1", 9111)
.sslSocketFactory(yourSslSocketFactory)
.build();
The client doesn't need any files to identify the server because the server's certificate should be signed by a trusted Certificate Authority (CA). 客户端不需要任何文件来标识服务器,因为服务器的证书应由受信任的证书颁发机构(CA)签名。 It's unclear by your question if this is the case though. 您的问题尚不清楚,是否确实如此。 If you are using a self-signed certificate or a custom CA to sign the certificate then SSLSocketFactory.getDefault()
, which grpc-okhttp uses by default, likely will not accept the server's certificate. 如果您使用自签名证书或自定义CA对证书进行签名,则grpc-okhttp默认使用SSLSocketFactory.getDefault()
可能不会接受服务器的证书。
In that rarer case, you will need to specify an SSLSocketFactory
for gRPC to use: 在这种罕见的情况下,您将需要指定SSLSocketFactory
以便gRPC使用:
ManagedChannel channel = OkHttpChannelBuilder.forAddress("127.0.0.1", 9111)
.sslSocketFactory(yourSslSocketFactory)
.build();
You would need to include a certificate in the client binary and the yourSslSocketFactory
would need to reference that certificate for it's TrustManager
. 您需要在客户端二进制文件中包括一个证书,而yourSslSocketFactory
将需要为其TrustManager
引用该证书。 As an example (taken from some grpc tests ): 例如(摘自一些grpc测试 ):
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(theRawCert);
ks.setCertificateEntry("customca", cert);
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
SSLContext context = SSLContext.getInstance("TLS", provider);
context.init(null, trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.