Grpc Java客户端的SSL连接

时间:2017-03-09 16:22:24

标签: java ssl netty grpc grpc-java

我正在尝试通过GRPC-SSL将Java客户端连接到非Java服务器。我可以在不使用SSL时连接到服务器。当我尝试使用SSL上下文(使用提供的pem文件)创建ManagedChannel时,我得到以下异常(相同的pem文件适用于NodeJS客户端)。

ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 10010)
    .sslContext(GrpcSslContexts.forClient().trustManager(new File("./test/myprivkey.pem")).build())
    .build();

来自trustManager调用的异常:

Exception in thread "main" java.lang.IllegalArgumentException: File does not contain valid certificates: /Users/continue/IdeaProjects/git/test/build/resources/main/keys/myprivkey.pem
    at io.netty.handler.ssl.SslContextBuilder.trustManager(SslContextBuilder.java:162)
    at com.test.io.grpc.test.client.Connection.getSslContext(Connection.java:65)
    at com.test.io.grpc.test.client.Connection.getSecure(Connection.java:41)
    at com.test.io.grpc.mgcs.client.TestClient.<init>(TestClient.java:36)
    at com.test.io.grpc.test.client.TestClient.main(TestClient.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.security.cert.CertificateException: found no certificates in input stream
    at io.netty.handler.ssl.PemReader.readCertificates(PemReader.java:98)
    at io.netty.handler.ssl.PemReader.readCertificates(PemReader.java:64)
    at io.netty.handler.ssl.SslContext.toX509Certificates(SslContext.java:999)
    at io.netty.handler.ssl.SslContextBuilder.trustManager(SslContextBuilder.java:160)

build.gradle相关部分

compile 'io.netty:netty-tcnative-boringssl-static:1.1.33.Fork26'

任何想法可能导致问题或如何进一步解决这个问题?

2 个答案:

答案 0 :(得分:0)

我正在读取错误的文件作为构建SslContext的证书文件。用正确的证书文件替换错误的输入解决了问题。

答案 1 :(得分:0)

如果您的证书来自DNSSimple,我发现我需要这样做 openssl pkcs8 -topk8 -nocrypt -in original.key -out new_key.key.pcks8 转换密钥的格式。 :(