Android Https双向认证 + GRPC

keywords:
android https 双向认证
android GRPC https 双向认证
ManagedChannel channel = OkHttpChannelBuilder.forAddress("xxx",yyy)
                        .overrideAuthority("zzz")
                        .sslSocketFactory(sslFactory)
                        .build();

1、千万不要像官网案例那样设置setPlaintext(true),这个是设置明文,我们用的是密文

2、xxx是服务器的ip,yyy是端口号,zzz是domain(这些参数服务端都会提供给你)
特别注意这个overrideAuthority一定要调用,覆盖hostname来匹配服务器的证书

3、sslSocketFactory这个方法就是设置密钥的方法。一般服务端会提供个.pem的密钥文件,放在raw中。我们把这个密钥设置到OkHttpChannelBuilder中即可以访问服务器了,关于sslSocketFactory 网上可以搜索到很多有关的内容。

cert files gen:
# Generates client.crt which is the clientCertChainFile for the client (need for mutual TLS only)
openssl x509 -passin pass:1111 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
echo Remove passphrase from client key:
openssl rsa -passin pass:1111 -in client.key -out client.key
echo Converting the private keys to X.509:
# Generates client.pem which is the clientPrivateKeyFile for the Client (needed for mutual TLS only)
openssl pkcs8 -topk8 -nocrypt -in client.key -out client.pem
# Generates server.pem which is the privateKeyFile for the Server
openssl pkcs8 -topk8 -nocrypt -in server.key -out server.pem

refs:

gRPC Android SSL/TLS Demo(Android上带有SSL/TLS加密的gRPC使用详解)
https://www.jianshu.com/p/2873a8349ca0

src demo:
https://github.com/grpc/grpc-java/blob/015b2fffa31995ff7923b4076f9324d464b7d827/android-interop-testing/app/src/main/java/io/grpc/android/integrationtest/TesterOkHttpChannelBuilder.java

grpc android mutual tls, how to send client certificate and key to server #3887
https://github.com/grpc/grpc-java/issues/3887

OkHttpChannelBuilder API Doc
https://grpc.io/grpc-java/javadoc/io/grpc/okhttp/OkHttpChannelBuilder.html#sslSocketFactory-javax.net.ssl.SSLSocketFactory-


NettyServerBuilder API Doc
https://grpc.io/grpc-java/javadoc/io/grpc/netty/NettyServerBuilder.html#sslContext-io.netty.handler.ssl.SslContext-

https://github.com/grpc/grpc-java/tree/master/examples/example-tls

 

 

Aandroid中https请求的单向认证和双向认证
https://blog.csdn.net/u011394071/article/details/52880062

 

基于Retrofit实现HTTPS思路
由于Retrofit是基于OkHttp实现的,因此想通过Retrofit实现HTTPS需要给Retrofit设置一个OkHttp代理对象用于处理HTTPS的握手过程。代理代码如下:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .sslSocketFactory(SSLHelper.getSSLCertifcation(context))//为OkHttp对象设置SocketFactory用于双向认证
    .hostnameVerifier(new UnSafeHostnameVerifier())
    .build();
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://10.2.8.56:8443")
    .addConverterFactory(GsonConverterFactory.create())//添加 json 转换器
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//添加 RxJava 适配器
    .client(okHttpClient)//添加OkHttp代理对象
    .build();

Android HTTPS 自制证书实现双向认证(OkHttp + Retrofit + Rxjava)
https://www.jianshu.com/p/64172ccfb73b


# gen client.p12

openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12 -name tomcat -CAfile ca.crt -caname root -chain

上面代码的目的是将 client.crt 和 client.key转换成 client.p12。.p12格式就是pkcs12的后缀。
然后结合第二个链接的结尾那块所讲,把.p12格式转换成.bks

PS: 生成client.p12时,会输入密码,我们要记住这个密码,后面用得到

# gen client.pem
openssl pkcs8 -topk8 -nocrypt -in client.key -out client.pem

 

已有ca.crt,ca.csr 和ca.key ,怎么才能创建客户端的cer 和pem?

服务器端已被同样的ca证书签署过。


我自己经过如下步骤:

1.生成客户端key
openssl genrsa -out client-key.key 1024


2.生成客户端请求文件
openssl req -new -out client-req.csr -key client-key.key


3.生成客户端证书(root证书,rootkey,客户端key,客户端请求文件这4个生成客户端证书)
openssl x509 -req -in client-req.csr -out client-cert.cer -signkey client-key.key -CA ca.cer -CAkey ca.key -CAcreateserial -days 3650


4.生成客户端p12格式根证书
openssl pkcs12 -export -clcerts -in client-cert.cer -inkey client-key.key -out client.p12


5. 生成pem证书
openssl pkcs12 -in client.p12 -nocerts -nodes -out client.pem

 

posted @ 2019-01-28 23:48  petercao  阅读(1840)  评论(1编辑  收藏  举报