Android HTTPS(4)直接用SSLSocket,黑名单,客户端证书
1.Warnings About Using SSLSocket Directly
So far, the examples have focused on HTTPS using HttpsURLConnection
. Sometimes apps need to use SSL separate from HTTP. For example, an email app might use SSL variants of SMTP, POP3, or IMAP. In those cases, the app would want to use SSLSocket
directly, much the same way that HttpsURLConnection
does internally.
The techniques described so far to deal with certificate verification issues also apply to SSLSocket
. In fact, when using a custom TrustManager
, what is passed to HttpsURLConnection
is an SSLSocketFactory
. So if you need to use a custom TrustManager
with an SSLSocket
, follow the same steps and use thatSSLSocketFactory
to create your SSLSocket
.
Caution: SSLSocket
does not perform hostname verification. It is up the your app to do its own hostname verification, preferably by calling getDefaultHostnameVerifier()
with the expected hostname. Further beware that HostnameVerifier.verify()
doesn't throw an exception on error but instead returns a boolean result that you must explicitly check.
Here is an example showing how you can do this. It shows that when connecting to gmail.com port 443 without SNI support, you'll receive a certificate for mail.google.com. This is expected in this case, so check to make sure that the certificate is indeed for mail.google.com:
// Open SSLSocket directly to gmail.com
SocketFactory sf = SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("gmail.com", );
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession s = socket.getSession(); // Verify that the certicate hostname is for mail.google.com
// This is due to lack of SNI support in the current SSLSocket.
if (!hv.verify("mail.google.com", s)) {
throw new SSLHandshakeException("Expected mail.google.com, "
"found " + s.getPeerPrincipal());
} // At this point SSLSocket performed certificate verificaiton and
// we have performed hostname verification, so it is safe to proceed. // ... use socket ...
socket.close();
2.Blacklisting
SSL relies heavily on CAs to issue certificates to only the properly verified owners of servers and domains. In rare cases, CAs are either tricked or, in the case of Comodo or DigiNotar, breached, resulting in the certificates for a hostname to be issued to someone other than the owner of the server or domain.
In order to mitigate this risk, Android has the ability to blacklist certain certificates or even whole CAs. While this list was historically built into the operating system, starting in Android 4.2 this list can be remotely updated to deal with future compromises.
3.Pinning
An app can further protect itself from fraudulently issued certificates by a technique known as pinning. This is basically using the example provided in the unknown CA case above to restrict an app's trusted CAs to a small set known to be used by the app's servers. This prevents the compromise of one of the other 100+ CAs in the system from resulting in a breach of the apps secure channel.
4.Client Certificates
This article has focused on the user of SSL to secure communications with servers. SSL also supports the notion of client certificates that allow the server to validate the identity of a client. While beyond the scope of this article, the techniques involved are similar to specifying a custom TrustManager
. See the discussion about creating a custom KeyManager
in the documentation for HttpsURLConnection
.
Android HTTPS(4)直接用SSLSocket,黑名单,客户端证书的更多相关文章
- Android : 关于HTTPS、TLS/SSL认证以及客户端证书导入方法
一.HTTPS 简介 HTTPS 全称 HTTP over TLS/SSL(TLS就是SSL的新版本3.1).TLS/SSL是在传输层上层的协议,应用层的下层,作为一个安全层而存在,翻译过来一般叫做传 ...
- SVN 错误:Error validating server certificate for 'https://xxxxxxx':443... Mac os svn客户端证书验证缓存 解决
mac上的SVN今天突然间 不好使了 在进行SVN操作是报出警告信息 Error validating server certificate for 'https://xxxxxxx':443 - T ...
- 转 Android HTTPS详解
目录(?)[-] 前言 HTTPS原理 SSLTLS协议作用 基本的运行过程 握手阶段的详细过程 客户端发出请求ClientHello 服务器回应ServerHello 客户端回应 服务器的最后回应 ...
- 只用@property定义一个属性speed,子类不能直接用_speed,需要在interface的成员变量列表里写上_speed
//写法一: @interface Person : NSObject { } @property (nonatomic, strong) NSString *name; @end @implemen ...
- Android Https双向认证 + GRPC
keywords:android https 双向认证android GRPC https 双向认证 ManagedChannel channel = OkHttpChannelBuilder.for ...
- CommandoVM-虚拟机映像文件 | VM打开直接用
呵呵!自从火眼发布了这个CommandoVM,想必大家应该都挺激动,然而实际操作一下,基本炸裂-- 因为并没有给类似于kali这种直接安装的现成镜像,而是要通过github的脚本去完全网络安装 实际操 ...
- 如何使用charles对Android Https进行抓包
Charles.png charles是一款在Mac下常用的截取网络封包工具,对Android Http进行抓包,只要对手机设置代理即可,但对Android Https进行抓包还是破费一些功夫,网 ...
- Netty为什么不直接用AtomicXXX,而要用AtomicXXXFieldUpdater去更新变量呢?
更多技术分享可关注我 前言 如果仔细阅读过Netty的线程调度模型的源码,或者NIO线程对象及其线程池的创建源码,那么肯定会遇到类似“AtomicIntegerFieldUpdater”的身影,不禁想 ...
- 服务之间的调用为啥不直接用 HTTP 而用 RPC?
什么是 RPC?RPC原理是什么? 什么是 RPC? RPC(Remote Procedure Call)-远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.比 ...
随机推荐
- 【python】正则表达式
参考资料:http://deerchao.net/tutorials/regex/regex.htm 1.正则表达式基础 2.python 正则表达式 1.正则表达式基础 元字符: 其他语法: (1) ...
- 【Dancing Link专题】解题报告
DLX用于优化精确覆盖问题,由于普通的DFS暴力搜索会超时,DLX是一个很强有力的优化手段,其实DLX的原理很简单,就是利用十字链表的快速删除和恢复特点,在DFS时删除一些行和列以减小查找规模,使得搜 ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- 【BZOJ】【1901】【Zju2112】 Dynamic Rankings
再填个坑. 动态维护区间第K大(带单点修改) 首先裸的区间第K大我们是用的[前缀和]思想,实现O(n)预处理,O(1)找树查询,那么如果是动态的呢?我们可以利用树状数组(BIT)的思想,进行O(log ...
- poi实现Excel比较
http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java http://stac ...
- 代码优先-Code First
非常有用的两篇文章 MSDN:Code First 迁移 博客园:CodeFirst数据迁移(不丢失数据库原有数据) EF有三种开发模式:Model First,Database First 和 Co ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- SQL注入测试用例
//看看是什么权限的and 1=(Select IS_MEMBER('db_owner'))And char(124)%2BCast(IS_MEMBER('db_owner') as varchar( ...
- .NET4.0下网站应用法度用UrlRewriter.dll重写无后缀路径 (在IIS7.5中的设备办法)
.NET4.0下网站应用法度用UrlRewriter.dll重写无后缀路径 在IIS中新建网站(端标语8111) 直接运行http://localhost:8111/ 设备办法: 二.添加通配符脚本映 ...
- CSS预处理器实践之Sass、Less大比拼[转]
什么是CSS预处理器? CSS可以让你做很多事情,但它毕竟是给浏览器认的东西,对开发者来说,Css缺乏很多特性,例如变量.常量以及一些编程语法,代码难易组织和维护.这时Css预处理器就应运而生了.Cs ...