跳到主要内容

7. TLS 握手协议 (The TLS Handshaking Protocols)

TLS Handshake Protocol 负责以下事项:

  • 协商 TLS version 和 cipher suite.
  • server 和 client 认证.
  • 协商加密参数和 key.
  • 检测传输错误.

handshake protocol 包含三个子协议.

7.1. Change Cipher Spec Protocol

Change Cipher Spec Protocol 用于通知 peer, 后续 record 将在新协商的 CipherSpec 和 key 下受到保护. 该协议只有一条消息, 使用当前而非 pending 的 CipherSpec 进行加密和压缩. 消息内容是值为 1 的单个 byte.

struct {
enum { change_cipher_spec(1), (255) } type;
} ChangeCipherSpec;

ChangeCipherSpec message 在协商出 security parameter 后的 handshake 期间发送. 接收方收到 ChangeCipherSpec message 后必须将 read pending state 更新为 read current state. Finished message 会紧随其后发送, 并使用新的 algorithm, key 和 secret. 实现不得在 handshake 完成前发送 ChangeCipherSpec message. 该消息之后收到的第一条 message 必须是 Finished message.

7.2. Alert Protocol

TLS 提供 Alert message 向 peer entity 传达告警. 与其他 message 一样, alert message 使用当前 connection state 加密和压缩.

enum { warning(1), fatal(2), (255) } AlertLevel;

enum {
close_notify(0),
unexpected_message(10),
bad_record_mac(20),
decryption_failed_RESERVED(21),
record_overflow(22),
decompression_failure(30),
handshake_failure(40),
no_certificate_RESERVED(41),
bad_certificate(42),
unsupported_certificate(43),
certificate_revoked(44),
certificate_expired(45),
certificate_unknown(46),
illegal_parameter(47),
unknown_ca(48),
access_denied(49),
decode_error(50),
decrypt_error(51),
export_restriction_RESERVED(60),
protocol_version(70),
insufficient_security(71),
internal_error(80),
user_canceled(90),
no_renegotiation(100),
unsupported_extension(110),
(255)
} AlertDescription;

struct {
AlertLevel level;
AlertDescription description;
} Alert;

7.2.1. Closure Alerts

client 和 server 必须在关闭 connection 写侧之前共享关闭信息. 任一方都可以通过发送 close_notify alert 发起关闭. 收到 closure alert 的任何一方必须立即停止在 connection 上发送新数据. 发送 close_notify 后, 实现不得再在该 connection 上发送任何数据.

7.2.2. Error Alerts

TLS protocol 的错误处理很简单. 检测到错误时, 检测方向 peer 发送消息. 发送或收到 fatal alert 后, 双方必须立即关闭 connection. server 和 client 必须忘记失败 connection 中建立的 secret value 和 key.

error alert 包括 unexpected_message, bad_record_mac, record_overflow, handshake_failure, bad_certificate, unsupported_certificate, certificate_revoked, certificate_expired, certificate_unknown, illegal_parameter, unknown_ca, access_denied, decode_error, decrypt_error, protocol_version, insufficient_security, internal_error, user_canceled, no_renegotiation 和 unsupported_extension.

7.3. 握手协议概述 (Handshake Protocol Overview)

TLS Handshake Protocol 包含以下步骤:

  • 交换 hello message, 以协商 algorithm, 交换 random value, 并检查 session resumption.
  • 交换必要的 cryptographic parameter, 使 client 和 server 能协商 premaster secret.
  • 交换 certificate 和密码学信息, 使 client 和 server 能认证自身.
  • 从 premaster secret 和交换的 random value 生成 master secret.
  • 向 record layer 提供 security parameter.
  • 允许 client 和 server 验证 peer 已计算出相同 security parameter, 且 handshake 未被攻击者篡改.

7.4. Handshake Protocol

TLS Handshake Protocol 是 TLS Record Layer 的高层 client 之一. 该协议用于协商 connection 的安全属性. handshake message 被交给 TLS record layer, 在其中封装到一个或多个 TLSPlaintext 结构中, 并按当前活动 connection state 处理和传输.

enum {
hello_request(0), client_hello(1), server_hello(2),
certificate(11), server_key_exchange (12),
certificate_request(13), server_hello_done(14),
certificate_verify(15), client_key_exchange(16),
finished(20), (255)
} HandshakeType;

struct {
HandshakeType msg_type;
uint24 length;
select (HandshakeType) {
case hello_request: HelloRequest;
case client_hello: ClientHello;
case server_hello: ServerHello;
case certificate: Certificate;
case server_key_exchange: ServerKeyExchange;
case certificate_request: CertificateRequest;
case server_hello_done: ServerHelloDone;
case certificate_verify: CertificateVerify;
case client_key_exchange: ClientKeyExchange;
case finished: Finished;
} body;
} Handshake;

handshake protocol message 带有显式 length, 并且不能跨 handshake record boundary 以非完整消息方式分片. receiver 必须检查同一 handshake record 中的 handshake message 后没有其他数据, 除非这些数据本身也构成有效 handshake message.