加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

理解 HTTPS 的工作原理

发布时间:2019-08-21 09:13:05 所属栏目:教程 来源:IT生涯
导读:副标题#e# 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: SS

在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状态切换。

  1. empty state -------------------> pending state ------------------> current state 
  2.  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算法的握手过程是这样的(已在总览中提到)

理解 HTTPS 的工作原理

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算法的原理是这样的:

理解 HTTPS 的工作原理
  1. By Original schema: A.J. Han Vinck, University of Duisburg-Essen SVG version: Flugaal [Public domain], via Wikimedia Commons 

使用Diffie–Hellman算法交换premaster secret的流程

理解 HTTPS 的工作原理

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 thepremaster 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是如何计算的

  1. master_secret = PRF(pre_master_secret, "master secret", 
  2.  ClientHello.random + ServerHello.random) 
  3.  [0..47]; 

加密,压缩和MAC算法参数是如何计算的

Handshaking Protocols使得客户端和服务端交换了三个参数:client_random,server_random和master_secret,通过以下算法生成算法所需要的参数

  1. To generate the key material, compute 
  2.  key_block = PRF(SecurityParameters.master_secret, 
  3.  "key expansion", 
  4.  SecurityParameters.`server_random ` + 
  5.  SecurityParameters.`client_random`); 
  6. until enough output has been generated. Then, the key_block is 
  7. partitioned as follows: 
  8.  client_write_MAC_key[SecurityParameters.mac_key_length] 
  9.  server_write_MAC_key[SecurityParameters.mac_key_length] 
  10.  client_write_key[SecurityParameters.enc_key_length] 
  11.  server_write_key[SecurityParameters.enc_key_length] 
  12.  client_write_IV[SecurityParameters.fixed_iv_length] 
  13.  server_write_IV[SecurityParameters.fixed_iv_length] 

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读