TLS握手的过程

翻译自 https://www.ibm.com/docs/en/ibm-mq/9.0?topic=tls-overview-ssltls-handshake

  1. The TLS client sends a client hello message that lists cryptographic information such as the TLS version and, in the client’s order of preference, the CipherSuites supported by the client. The message also contains a random byte string that is used in subsequent computations. The protocol allows for the client hello to include the data compression methods supported by the client.

TLS客户端发送client hello,里面包含了加密的信息,如TLS version, client支持的加密方式,客户端随机字符串,如下图所示

client hello

  1. The TLS server responds with a server hello message that contains the CipherSuite chosen by the server from the list provided by the client, the session ID, and another random byte string. The server also sends its digital certificate. If the server requires a digital certificate for client authentication, the server sends a client certificate request that includes a list of the types of certificates supported and the Distinguished Names of acceptable Certification Authorities (CAs).

TLS服务端响应server hello消息,包含了服务端选择的加密方式, session ID, 以及服务器端随机字符串。 服务器同时发送certificate给Client。 如果服务端要求客户端做证书验证,服务端会发起一个client certificate request(客户端证书验证请求),包含支持的加密方式和可接收的CA。
客户端验证不是必须的,它从另一层保证了client的真实有效性,避免了一些非法用户的攻击行为,但是同时也增加了服务器的资源开销。

server hello

certificate消息和Server Hello Done消息

  1. The TLS client verifies the server’s digital certificate. For more information, see How TLS provides identification, authentication, confidentiality, and integrity.

client验证server的certificate

  1. The TLS client sends the random byte string that enables both the client and the server to compute the secret key to be used for encrypting subsequent message data. The random byte string itself is encrypted with the server’s public key.

TLS client根据客户端和服务端的信息生成secret key,使用服务器证书的public key加密,然后发送给Server。后续的通信将采用该secret key做对称加密。

Client Key Exchange

  1. If the TLS server sent a client certificate request, the client sends a random byte string encrypted with the client’s private key, together with the client’s digital certificate, or a no digital certificate alert. This alert is only a warning, but with some implementations the handshake fails if client authentication is mandatory.

如果Serve要求client certificate request,那么client会发送client的certificate,同时使用client的certifcate私钥机密的随机字符串;如果客户端没有certificate,那么会给一个无证书的通告。这个通告仅仅是个告警,但是有些强制要求client验证的实现会导致tls握手失败。

  1. The TLS server verifies the client’s certificate. For more information, see How TLS provides identification, authentication, confidentiality, and integrity.

如果收到client certificate,Server会验证client‘s certificate

  1. The TLS client sends the server a finished message, which is encrypted with the secret key, indicating that the client part of the handshake is complete.

TLS client发送包含secret key的finished消息,表示client端TLS握手完成

  1. The TLS server sends the client a finished message, which is encrypted with the secret key, indicating that the server part of the handshake is complete.

TLS server发送finished消息,表示server端TLS握手完成

  1. For the duration of the TLS session, the server and client can now exchange messages that are symmetrically encrypted with the shared secret key.

握手完成后,整个session对话过程Server和Client可以使用协商好的secret key用对称加密方式通信了。

TLS handshake

### SSL/TLS 握手过程详解 #### 一、握手的目标 TLS/SSL 握手的主要目标是在客户端和服务器之间建立一个安全的通信通道。这个通道通过保密性(Confidentiality)、完整性(Integrity)以及身份验证(Authentication)来保护数据传输[^1]。 #### 二、基本流程概述 整个握手过程可以分为以下几个阶段: 1. **客户端问候 (Client Hello)** 客户端发起连接请求,发送 `Client Hello` 消息给服务器。此消息包含支持的协议版本、加密套件列表以及其他参数(如随机数)。这些信息用于后续的安全配置[^2]。 2. **服务器响应 (Server Hello 和证书)** 服务器接收到 `Client Hello` 后返回自己的 `Server Hello` 响应,其中包括选定的协议版本、加密算法和其他必要参数。随后,服务器还会提供其数字证书以证明自己身份的真实性。如果使用 RSA 密钥交换,则无需额外的消息;如果是其他类型的密钥交换方式可能还需要进一步交互[^1][^2]。 3. **公钥验证与预主密钥生成** 客户端接收到来自服务端的信息之后,会对所提供的 X.509 数字证书进行校验,确认无误后再利用其中所含有的公共密钥加密一段预先产生的秘密值——即所谓的 “pre-master secret”。然后把这个经过加密处理后的 pre-master secret 发送给服务器[^1]。 4. **计算共享主密钥** 双方各自基于之前获得的数据独立推导出相同的 session key 或 master secret 。这一操作依赖于 Diffie-Hellman 算法或其他类似的机制完成最终对称加密所需的材料准备[^2]。 5. **变更密码规格通知 Change Cipher Spec** 当双方都准备好切换至新的加密模式时,分别发出一条特殊的控制命令叫做 change cipher spec message ,表示接下来所有的通讯都将采用新协商出来的设置来进行加解密工作[^1]。 6. **Finished Message 验证成功与否** 最后一步就是互相传递 finished messages 来检验前面所有步骤是否正确执行完毕。只有当彼此都能够解读对方发来的这条特殊消息的时候才意味着整个 handshake 过程圆满结束,并正式开启受保护状态下的正常业务往来活动[^2]。 #### 三、代码示例模拟简单场景 下面是一个非常简化的 Python 脚本来展示如何构建 Client Hello 请求并解析 Server Hello 返回的内容: ```python import socket from ssl import SSLContext, PROTOCOL_TLSv1_2 def create_tls_handshake(host='www.example.com', port=443): context = SSLContext(PROTOCOL_TLSv1_2) with socket.create_connection((host, port)) as sock: with context.wrap_socket(sock, server_hostname=host) as ssock: print(f"Connected to {ssock.server_hostname}") print("Cipher used:", ssock.cipher()) create_tls_handshake() ``` 以上脚本仅作为演示用途,在实际应用环境中应当考虑更多细节因素比如错误捕获等等。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值