Okhttp hostnameVerifier详解

news2024/10/7 17:23:19

hostnameVerifier

  • 方法简介
  • 核心原理
  • 参考资料

方法简介

本篇博文以Okhttp 4.6.0来解析hostnameVerfier的作用,顾名思义,该方法的主要作用就是鉴定hostnname的合法性。Okhttp在初始化的时候我们可以自己配置hostnameVerfier

new OkHttpClient.Builder()
    .connectTimeout(20, TimeUnit.SECONDS)
    .readTimeout(20, TimeUnit.SECONDS)
    .writeTimeout(35, TimeUnit.SECONDS)  
    .hostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            //注意这里在生产环境中千万不要直接写死true          
            return true;
        }
    })
    .build();

但是网上好多资料将verfiy直接返回true是十分危险的。当然如果vertify返回fasle意味着hostname验证不通过,http请求无法成功,比如我以自己的博客地址发起http请求,错误信息如下:
在这里插入图片描述

{http errorCode=-500, mErrorMsg=Hostname yanchen.blog.csdn.net not verified:
    certificate: sha256/tlnf6pbfeu257hnJ9e6j4A1ZWH3vVMzn3Zn3F9kLHdg=
    DN: CN=*.blog.csdn.net
    subjectAltNames: [*.blog.csdn.net]}

执行vertify的地方是在RealConnection里面,执行之后。
在这里插入图片描述

除了自定义hostnameVerfier之外,Okhttp提供了默认实现,现在就分析下起内部原理。

核心原理

在Okhttp内置了OkHostnameVerifier,该方法通过session.peerCertificates[0] as X509Certificate获取证书的对象

override fun verify(host: String, session: SSLSession): Boolean {
    return try {
      verify(host, session.peerCertificates[0] as X509Certificate)
    } catch (_: SSLException) {
      false
    }
  }

fun verify(host: String, certificate: X509Certificate): Boolean {
    return when {
      host.canParseAsIpAddress() -> verifyIpAddress(host, certificate)
      else -> verifyHostname(host, certificate)
    }
  }

通过X509Certificate对象提供了一系列get方法可以获取到证书的公钥,序列号等一系列信息。见下图:
在这里插入图片描述
最终会调用verifyHostname方法,通过certificate获取getSubjectAltNames拿到SubjectAltName之后,将hostname与SubjectAltName进行比对,如果符合就返回true,否则就返回fasle.

private fun verifyHostname(hostname: String, certificate: X509Certificate): Boolean {
    val hostname = hostname.toLowerCase(Locale.US)
    return getSubjectAltNames(certificate, ALT_DNS_NAME).any {
      verifyHostname(hostname, it)
    }
  }

//hostname和SubjectAltName比对
private fun verifyHostname(hostname: String?, pattern: String?): Boolean {
    var hostname = hostname
    var pattern = pattern
    //检验客户端域名的有效性
    if (hostname.isNullOrEmpty() ||
        hostname.startsWith(".") ||
        hostname.endsWith("..")) {
      // Invalid domain name
      return false
    }
    //检验证书中SubjectAltName的有效性
    if (pattern.isNullOrEmpty() ||
        pattern.startsWith(".") ||
        pattern.endsWith("..")) {
      // Invalid pattern/domain name
      return false
    }

    // Normalize hostname and pattern by turning them into absolute domain names if they are not
    // yet absolute. This is needed because server certificates do not normally contain absolute
    // names or patterns, but they should be treated as absolute. At the same time, any hostname
    // presented to this method should also be treated as absolute for the purposes of matching
    // to the server certificate.
    //   www.android.com  matches www.android.com
    //   www.android.com  matches www.android.com.
    //   www.android.com. matches www.android.com.
    //   www.android.com. matches www.android.com
    if (!hostname.endsWith(".")) {
      hostname += "."
    }
    if (!pattern.endsWith(".")) {
      pattern += "."
    }
    // Hostname and pattern are now absolute domain names.

    pattern = pattern.toLowerCase(Locale.US)
    // Hostname and pattern are now in lower case -- domain names are case-insensitive.

    if ("*" !in pattern) {
      // Not a wildcard pattern -- hostname and pattern must match exactly.
      return hostname == pattern
    }

    // Wildcard pattern

    // WILDCARD PATTERN RULES:
    // 1. Asterisk (*) is only permitted in the left-most domain name label and must be the
    //    only character in that label (i.e., must match the whole left-most label).
    //    For example, *.example.com is permitted, while *a.example.com, a*.example.com,
    //    a*b.example.com, a.*.example.com are not permitted.
    // 2. Asterisk (*) cannot match across domain name labels.
    //    For example, *.example.com matches test.example.com but does not match
    //    sub.test.example.com.
    // 3. Wildcard patterns for single-label domain names are not permitted.

    if (!pattern.startsWith("*.") || pattern.indexOf('*', 1) != -1) {
      // Asterisk (*) is only permitted in the left-most domain name label and must be the only
      // character in that label
      return false
    }

    // Optimization: check whether hostname is too short to match the pattern. hostName must be at
    // least as long as the pattern because asterisk must match the whole left-most label and
    // hostname starts with a non-empty label. Thus, asterisk has to match one or more characters.
    if (hostname.length < pattern.length) {
      return false // Hostname too short to match the pattern.
    }

    if ("*." == pattern) {
      return false // Wildcard pattern for single-label domain name -- not permitted.
    }

    // Hostname must end with the region of pattern following the asterisk.
    val suffix = pattern.substring(1)
    if (!hostname.endsWith(suffix)) {
      return false // Hostname does not end with the suffix.
    }

    // Check that asterisk did not match across domain name labels.
    val suffixStartIndexInHostname = hostname.length - suffix.length
    if (suffixStartIndexInHostname > 0 &&
        hostname.lastIndexOf('.', suffixStartIndexInHostname - 1) != -1) {
      return false // Asterisk is matching across domain name labels -- not permitted.
    }

    // Hostname matches pattern.
    return true
  }

那么SubjectAltName是什么?我们可以通过如下方法获取:

new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
              try {
                X509Certificate x509Certificate= (X509Certificate) session.getPeerCertificates()[0];
                Collection<List<?>> subjectAltNames = x509Certificate.getSubjectAlternativeNames();
                for (List<?> subjectAltName : subjectAltNames) {
                    if (subjectAltName == null || subjectAltName.size() < 2) continue;
                    int type = (int)subjectAltName.get(0);
                    if (type!= 2) continue;
                    String altName = (String)subjectAltName.get(1);
                    LogUtil.logD("hostnameVerifier","x509Certificate altName=="+altName);
                }
            } catch (Exception e) {

            }     
            return true;
        }
    }

在这里插入图片描述
在这里插入图片描述
Okhttp 内置的hostname校验逻辑很简单,大家可以自行查看起源码即可。

参考资料

  1. Android CertificateSource系统根证书的检索和获取
  2. Android https TrustManager checkServerTrusted 详解
  3. Android RootTrustManager 证书校验简单分析
  4. Android CertificateSource系统根证书的检索和获取
  5. Android AndroidNSSP的简单说明
  6. Okhttp之RealConnection建立链接简单分析

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1893215.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ExtendSim在商业和服务行业中的仿真

仿真使企业能够做出明智的、数据驱动的预测&#xff0c;从而指导决策、产生积极成果并建立竞争优势。 精益分析 使用 ExtendSim 中的精益分析方法对欧洲的供应链网络进行建模&#xff0c;一家制造商实现了对最终客户的服务水平提高了 98%&#xff0c;而且现在可以在库存减少约 …

新手必学:TikTok视频标签的使用方法

想让你的TikTok视频火起来&#xff0c;就得用对标签。标签能帮你的作品被更多人看到&#xff0c;也更有利于推广&#xff0c;可以为品牌增加曝光度、吸引更多观众、提高转化率和借势热门话题。那么应该如何选择标签并使用标签呢&#xff0c;看完这篇分享你或许会有所启发&#…

dc-3靶机渗透

环境准备 dc-3靶机下载链接&#xff1a; https://download.vulnhub.com/dc/DC-3-2.zip 启动靶机遇到的问题解决文章在下面 http://t.csdnimg.cn/zLQAI kali最新版 dc-3靶机 两台机器都在vmware上运行 网络设置NAT模式 渗透过程 信息收集 首先使用ifconfig获取kali的IP地址 可…

【RflySim学习笔记】1.RflySim的绪论

目录 文章目录 目录1.RflySim 平台背景2.RflySim 平台特点3.RflySim 平台核心组件介绍3.1 CopterSim3.2 RflySim3D/RflySimUE5UE引擎&#xff1a;RflySim3D/RflySimUE5 3.3 QGroundControl地面站3.4 Python38Env3.5 MATLAB自动代码生成工具箱3.6 SITL/HITL批处理脚本3.7 PX4 Fi…

【已解决】: fatal error: cuda_runtime_api.h: No such file or directory

既然他找不到&#xff0c;我们就把路径给他写清楚&#xff01; 检查自己是不是有这个文件&#xff1a; 去路径/usr/local下&#xff0c;使用命令查询是否拥有该文件&#xff1a; find . -name cuda_runtime_api.h结果&#xff1a; 因为我要使用的是cuda-11.3&#xff0c;因…

Linux 摄像头编号固化

一、前言 在工业领域&#xff0c;一台设备会有很多个摄像头&#xff0c;可以使用命令&#xff1a;ll /dev/video* 进行查看&#xff1b; 在代码中&#xff0c;如果需要使用摄像头&#xff0c;那么都是需要具体到哪个摄像头编号的&#xff0c;例如 open("/dev/video4"…

Redis 五大数据类型底层原理

0、前言 本文涉及的主题&#xff1a; redis 对象存储 底层数据结构&#xff1a;int、embstr、raw、ziplist、listpack、quicklist、skiplist、intset、hashtable redis 数据类型&#xff1a;string、list、set、zset、hash 1、对象存储、底层编码、数据类型 1.1 对象存储…

SpringBoot实现多数据源切换快速入门

1. 概述 随着项目规模的扩大和业务需求的复杂化&#xff0c;单一数据源已经不能满足实际开发中的需求。在许多情况下&#xff0c;我们需要同时操作多个数据库&#xff0c;或者需要将不同类型的数据存储在不同的数据库中。这时&#xff0c;多数据源场景成为必不可少的解决方案。…

C#的多线程UI窗体控件显示方案 - 开源研究系列文章

上次编写了《LUAgent服务器端工具》这个应用&#xff0c;然后里面需要新启动一个线程去对文件进行上传到FTP服务器&#xff0c;但是新线程里无法对应用主线程UI的内容进行更改&#xff0c;所以就需要在线程里设置主UI线程里控件信息的方法&#xff0c;于是就有了此博文。此文记…

录屏怎么打开?这3招请收好

在数字化飞速发展的今天&#xff0c;录屏功能已经不仅仅是一个简单的工具&#xff0c;而是成为了我们工作、学习和娱乐中的得力助手。但是&#xff0c;不同设备或系统打开录屏功能的方式可能有所不同&#xff0c;打开录屏功能的方式也在不断更新和演变&#xff0c;今天我们就来…

【前端项目笔记】8 订单管理

订单管理 效果展示&#xff1a; 在开发功能之前先创建分支order cls 清屏 git branch 查看所有分支&#xff08;*代表当前分支&#xff09; git checkout -b order 新建分支order git push -u origin order 将本地的当前分支提交到云端仓库origin中命名为order 通过路由方式…

“工控机”是什么?和普通电脑有区别嘛!

在现代工业生产中,有一种特殊的计算机,它不像普通电脑那样被放置于明亮的办公室内,而是常常藏身于机器轰鸣、环境恶劣的工厂车间里,这就是工控机——工业控制计算机的简称。作为工业自动化领域不可或缺的核心设备,工控机不仅承载着监控与数据采集(SCADA)、过程控制、数据…

LLM大模型安全概述

引言 2022年底以来&#xff0c;以ChatGPT为代表的大模型飞速发展&#xff0c;正在成为#驱动新质生产力发展#​的新动能、人类探索未知的新工具. 在显著提升人工智能(artificial intelligence, AI)模型通用理解和生成能力的同时&#xff0c;也带来了前所未有的安全风险. 大模型…

plugin:vite:import-analysis]No known conditions for“./lib/locale/lang/zh-cn“

将原有引入&#xff1a; import zhCn from element-plus/lib/locale/lang/zh-cn 改成&#xff1a; import zhCn from element-plus/es/locale/lang/zh-cn; 原因版本升级&#xff0c;引入路径改变&#xff08;原先的包在node_modules\element-plus\lib找不到&#xff09; 新…

数据预处理:统计关联性分析/数据清洗/数据增强/特征工程实例

专栏介绍 1.专栏面向零基础或基础较差的机器学习入门的读者朋友,旨在利用实际代码案例和通俗化文字说明,使读者朋友快速上手机器学习及其相关知识体系。 2.专栏内容上包括数据采集、数据读写、数据预处理、分类\回归\聚类算法、可视化等技术。 3.需要强调的是,专栏仅介绍主…

科技赋能智慧应急:“数字孪生+无人机”在防汛救灾中的应用

近期&#xff0c;全国多地暴雨持续&#xff0c;“麻辣王子工厂停工”“水上派出所成水上的派出所了”等相关词条冲上热搜&#xff0c;让人们看到了全国各地城市内涝、洪涝带来的严重灾情。暴雨带来的影响可见一斑&#xff0c;潜在的洪水、泥石流、山体滑坡等地质灾害更应提高警…

UartAssist 串口助手,存储文件 打开是乱码

问题描述 使用UartAssist 对采集的串口数据进行存储&#xff0c;存储为日志格式的文件&#xff0c;但打开的文件有些可正常显示&#xff0c;大部分打开是乱码&#xff1b; (串口输出无中文) 若打开的文本为 UTF-16&#xff0c;则为乱码&#xff0c;若是ANSI格式&#xff0c;则可…

【MySQL】库的操作【创建和操纵】

文章目录 1.创建数据库1.1字符集和校验规则1.查看系统默认字符集以及校验规则2.查看数据库支持的字符集以及校验规则 1.2校验规则对数据库的影响1.创建一个数据库&#xff0c;校验规则使用utf8_ general_ ci[不区分大小写]2.创建一个数据库&#xff0c;校验规则使用utf8_ bin[区…

HTML内容爬取:使用Objective-C进行网页数据提取

网页爬取简介 网页爬取&#xff0c;通常被称为网络爬虫或爬虫&#xff0c;是一种自动浏览网页并提取所需数据的技术。这些数据可以是文本、图片、链接或任何网页上的元素。爬虫通常遵循一定的规则&#xff0c;访问网页&#xff0c;解析页面内容&#xff0c;并存储所需信息。 …

继电器测试的价格和性价比如何?

继电器是广泛应用于各种电气控制系统中的开关元件&#xff0c;其主要功能是在输入信号的控制下&#xff0c;实现电路的断开和闭合。继电器的性能和质量直接影响到整个电气系统的稳定性和可靠性&#xff0c;因此对继电器进行严格的测试是非常必要的。那么&#xff0c;继电器测试…