HTTPS工作原理
ztj100 2025-01-17 14:38 9 浏览 0 评论
文章来源:猫尾博客
https://cattail.me/tech/2015/11/30/how-https-works.html
目标读者:理解HTTP协议,对称和非对称加密,想要了解HTTPS协议的工作原理
读完本文,你能明白
- 什么是HTTPS,TLS(SSL),TLS和HTTPS是什么关系
- 什么是证书和数字签名,它们是如何传递信任的
- HTTPS有什么样的功能,它是如何实现这样的功能的
简介
HTTPS,也称作HTTP over TLS。TLS的前身是SSL,TLS 1.0通常被标示为SSL 3.1,TLS 1.1为SSL 3.2,TLS 1.2为SSL 3.3。本文着重描述TLS协议的1.2版本
下图描述了在TCP/IP协议栈中TLS(各子协议)和HTTP的关系
Credit: Kaushal Kumar Panday From: SSL Handshake and HTTPS Bindings on IIS
其中Handshake protocol,Change Ciper Spec protocol和Alert protocol组成了SSL Handshaking Protocols。
HTTPS和HTTP协议相比提供了
- 数据完整性:内容传输经过完整性校验
- 数据隐私性:内容经过对称加密,每个连接生成一个唯一的加密密钥
- 身份认证:第三方无法伪造服务端(客户端)身份
其中,数据完整性和隐私性由TLS Record Protocol保证,身份认证由TLS Handshaking Protocols实现。
总览
使用RSA算法的SSL握手过程是这样的
Source: Keyless SSL: The Nitty Gritty Technical Details
- [明文] 客户端发送随机数client_random和支持的加密方式列表
- [明文] 服务器返回随机数server_random ,选择的加密方式和服务器证书链
- [RSA] 客户端验证服务器证书,使用证书中的公钥加密premaster secret 发送给服务端
- 服务端使用私钥解密premaster secret
- 两端分别通过client_random,server_random 和premaster secret 生成master secret,用于对称加密后续通信内容
证书(Digital certificate)
那么什么是证书呢?
证书中包含什么信息
- 证书信息:过期时间和序列号
- 所有者信息:姓名等
- 所有者公钥
为什么服务端要发送证书给客户端
互联网有太多的服务需要使用证书来验证身份,以至于客户端(操作系统或浏览器等)无法内置所有证书,需要通过服务端将证书发送给客户端。
客户端为什么要验证接收到的证书
中间人攻击
客户端<------------攻击者<------------服务端
伪造证书 拦截请求
客户端如何验证接收到的证书
为了回答这个问题,需要引入数字签名(Digital Signature)。
+---------------------+
| A digital signature |
|(not to be confused |
|with a digital |
|certificate) | +---------+ +--------+
| is a mathematical |----哈希--->| 消息摘要 |---私钥加密--->| 数字签名 |
|technique used | +---------+ +--------+
|to validate the |
|authenticity and |
|integrity of a |
|message, software |
|or digital document. |
+---------------------+
将一段文本通过哈希(hash)和私钥加密处理后生成数字签名。
假设消息传递在Bob,Susan和Pat三人之间发生。Susan将消息连同数字签名一起发送给Bob,Bob接收到消息后,可以这样验证接收到的消息就是Susan发送的
+---------------------+
| A digital signature |
|(not to be confused |
|with a digital |
|certificate) | +---------+
| is a mathematical |----哈希--->| 消息摘要 |
|technique used | +---------+
|to validate the | |
|authenticity and | |
|integrity of a | |
|message, software | 对
|or digital document. | 比
+---------------------+ |
|
|
+--------+ +---------+
| 数字签名 |---公钥解密--->| 消息摘要 |
+--------+ +---------+
当然,这个前提是Bob知道Susan的公钥。更重要的是,和消息本身一样,公钥不能在不安全的网络中直接发送给Bob。
此时就引入了证书颁发机构(Certificate Authority,简称CA),CA数量并不多,Bob客户端内置了所有受信任CA的证书。CA对Susan的公钥(和其他信息)数字签名后生成证书。
Susan将证书发送给Bob后,Bob通过CA证书的公钥验证证书签名。
Bob信任CA,CA信任Susan 使得 Bob信任Susan,信任链(Chain Of Trust)就是这样形成的。
事实上,Bob客户端内置的是CA的根证书(Root Certificate),HTTPS协议中服务器会发送证书链(Certificate Chain)给客户端。
TLS协议
TLS协议包括TLS Record Protocol和TLS Handshake Protocol。总览中的流程图仅涉及到TLS Handshake Protocol。
TLS Record Protocol
在TLS协议中,有四种子协议运行于Record protocol之上
- Handshake protocol
- Alert protocol
- Change cipher spec protocol
- Application data protocol
Record protocol起到了这样的作用
- 在发送端:将数据(Record)分段,压缩,增加MAC(Message Authentication Code)和加密
- 在接收端:将数据(Record)解密,验证MAC,解压并重组
值得一提的是,Record protocol提供了数据完整性和隐私性保证,但Record类型(type)和长度(length)是公开传输的
Record Protocol有三个连接状态(Connection State),连接状态定义了压缩,加密和MAC算法。所有的Record都是被当前状态(Current State)确定的算法处理的。
TLS Handshake Protocol和Change Ciper Spec Protocol会导致Record Protocol状态切换。
empty state -------------------> pending state ------------------> current state
Handshake Protocol Change Cipher Spec
初始当前状态(Current State)没有指定加密,压缩和MAC算法,因而在完成TLS Handshaking Protocols一系列动作之前,客户端和服务端的数据都是明文传输的;当TLS完成握手过程后,客户端和服务端确定了加密,压缩和MAC算法及其参数,数据(Record)会通过指定算法处理。
其中,Record首先被加密,然后添加MAC(message authentication code)以保证数据完整性。
TLS Handshaking Protocols
Handshakeing protocols包括Alert Protocol,Change Ciper Spec Protocol和Handshake protocol。本文不会详细介绍Alert Protocol和Change Ciper Spec Protocol。
使用RSA算法的握手过程是这样的(已在总览中提到)
Source: Keyless SSL: The Nitty Gritty Technical Details
客户端和服务端在握手hello消息中明文交换了client_random和server_random ,使用RSA公钥加密传输premaster secret ,最后通过算法,客户端和服务端分别计算master secret。其中,不直接使用premaster secret 的原因是:保证secret的随机性不受任意一方的影响。
除了使用RSA算法在公共信道交换密钥,还可以通过Diffie–Hellman算法。Diffie–Hellman算法的原理是这样的
By Original schema: A.J. Han Vinck, University of Duisburg-Essen SVG version: Flugaal [Public domain], via Wikimedia Commons
使用Diffie–Hellman算法交换premaster secret 的流程
Source: Keyless SSL: The Nitty Gritty Technical Details
小结
TLS Handshaking Protocols协商了TLS Record Protocol使用的算法和所需参数,并验证了服务端身份;TLS Record Protocol在协商后保证应用层数据的完整性和隐私性。
TLS Handshaking Protocol的核心是在公开信道上传递premaster secret。
Q&A
为什么传输内容不直接使用非对称加密?
性能
HTTPS能保证正常连接?
no
There are a number of ways in which a man-in-the-middle attacker can attempt to make two entities drop down to the least secure method they support.
攻击者甚至可以直接丢弃双方的数据包
服务端如何验证客户端身份?
通过Client Certificate
This message conveys the client’s certificate chain to the server; the server will use it when verifying the CertificateVerify message (when the client authentication is based on signing) or calculating the premaster secret (for non-ephemeral Diffie- Hellman). The certificate MUST be appropriate for the negotiated cipher suite’s key exchange algorithm, and any negotiated extensions.
Alert protocol有什么作用?
Closure Alerts:防止Truncation Attack
In a truncation attack, an attacker inserts into a message a TCP code indicating the message has finished, thus preventing the recipient picking up the rest of the message. To prevent this, SSL from version v3 onward has a closing handshake, so the recipient knows the message has not ended until this has been performed.
Error Alerts:错误处理
master secret是如何计算的
master_secret = PRF(pre_master_secret, "master secret",
ClientHello.random + ServerHello.random)
[0..47];
加密,压缩和MAC算法参数是如何计算的
Handshaking Protocols使得客户端和服务端交换了三个参数:client_random,server_random 和master_secret,通过以下算法生成算法所需要的参数
To generate the key material, compute
key_block = PRF(SecurityParameters.master_secret,
"key expansion",
SecurityParameters.`server_random ` +
SecurityParameters.`client_random`);
until enough output has been generated. Then, the key_block is
partitioned as follows:
client_write_MAC_key[SecurityParameters.mac_key_length]
server_write_MAC_key[SecurityParameters.mac_key_length]
client_write_key[SecurityParameters.enc_key_length]
server_write_key[SecurityParameters.enc_key_length]
client_write_IV[SecurityParameters.fixed_iv_length]
server_write_IV[SecurityParameters.fixed_iv_length]
The master secret is expanded into a sequence of secure bytes, which is then split to a client write MAC key, a server write MAC key, a client write encryption key, and a server write encryption key
使用Diffie-Hellman算法的TLS握手细节
Source: https://cipherstuff.wordpress.com/
拓展阅读
- Keyless
- Let’s Encrypt
- Session resume
- 证书Revoke
参考链接
- TLS1.2规范:The Transport Layer Security (TLS) Protocol Version 1.2
- PKI规范:Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile
- 证书和数字签名:What is a Digital Signature?
- TLS Handshake:Keyless SSL: The Nitty Gritty Technical Details
相关推荐
- Whoosh,纯python编写轻量级搜索工具
-
引言在许多应用程序中,搜索功能是至关重要的。Whoosh是一个纯Python编写的轻量级搜索引擎库,可以帮助我们快速构建搜索功能。无论是在网站、博客还是本地应用程序中,Whoosh都能提供高效的全文搜...
- 如何用Python实现二分搜索算法(python二分法查找代码)
-
如何用Python实现二分搜索算法二分搜索(BinarySearch)是一种高效的查找算法,适用于在有序数组中快速定位目标值。其核心思想是通过不断缩小搜索范围,每次将问题规模减半,时间复杂度为(O...
- 路径扫描 -- dirsearch(路径查找器怎么使用)
-
外表干净是尊重别人,内心干净是尊重自己,干净,在今天这个时代,应该是一种极高的赞美和珍贵。。。----网易云热评一、软件介绍Dirsearch是一种命令行工具,可以强制获取web服务器中的目录和文件...
- 78行Python代码帮你复现微信撤回消息!
-
来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。...
- 从零开始学习 Python!2《进阶知识》 Python进阶之路
-
欢迎来到Python学习的进阶篇章!如果你说已经掌握了基础语法,那么这篇就是你开启高手之路的大门。我们将一起探讨面向对象编程...
- 白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件
-
一、背景介绍...
- Python之txt数据预定替换word预定义定位标记生成word报告(四)
-
续接Python之txt数据预定替换word预定义定位标记生成word报告(一)https://mp.toutiao.com/profile_v4/graphic/preview?pgc_id=748...
- Python——字符串和正则表达式中的反斜杠('\')问题详解
-
在本篇文章里小编给大家整理的是关于Python字符串和正则表达式中的反斜杠('\')问题以及相关知识点,有需要的朋友们可以学习下。在Python普通字符串中在Python中,我们用'\'来转义某些普通...
- Python re模块:正则表达式综合指南
-
Python...
- python之re模块(python re模块sub)
-
re模块一.re模块的介绍1.什么是正则表达式"定义:正则表达式是一种对字符和特殊字符操作的一种逻辑公式,从特定的字符中,用正则表达字符来过滤的逻辑。(也是一种文本模式;)2、正则表达式可以帮助我们...
- MySQL、PostgreSQL、SQL Server 数据库导入导出实操全解
-
在数字化时代,数据是关键资产,数据库的导入导出操作则是连接数据与应用场景的桥梁。以下是常见数据库导入导出的实用方法及代码,包含更多细节和特殊情况处理,助你应对各种实际场景。一、MySQL数据库...
- Zabbix监控系统系列之六:监控 mysql
-
zabbix监控mysql1、监控规划在创建监控项之前要尽量考虑清楚要监控什么,怎么监控,监控数据如何存储,监控数据如何展现,如何处理报警等。要进行监控的系统规划需要对Zabbix很了解,这里只是...
- mysql系列之一文详解Navicat工具的使用(二)
-
本章内容是系列内容的第二部分,主要介绍Navicat工具的使用。若查看第一部分请见:...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Whoosh,纯python编写轻量级搜索工具
- 如何用Python实现二分搜索算法(python二分法查找代码)
- 路径扫描 -- dirsearch(路径查找器怎么使用)
- 78行Python代码帮你复现微信撤回消息!
- 从零开始学习 Python!2《进阶知识》 Python进阶之路
- 白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件
- Python之txt数据预定替换word预定义定位标记生成word报告(四)
- 假期苦短,我用Python!这有个自动回复拜年信息的小程序
- Python——字符串和正则表达式中的反斜杠('\')问题详解
- Python re模块:正则表达式综合指南
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)