简体   繁体   中英

SSL connection for Grpc Java client

I am trying to connect a Java client to a non-Java server via GRPC-SSL. I am able to connect to the server when not using SSL. When I try to create the ManagedChannel using a SSL context (using the provided pem file), I get the exception below (same pem file works with a NodeJS client).

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

Exception from trustManager call:

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 relevant section

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

Any ideas what might be causing the issue or how to troubleshoot this further?

I was reading the wrong file as the certificate file for building the SslContext. Replacing the incorrect input with the right certificate file fixed the issue.

If your cert is from DNSSimple I found I needed to do openssl pkcs8 -topk8 -nocrypt -in original.key -out new_key.key.pcks8 To convert the format of the key. :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM