TLS 1.2 和 1.3 协议的摘录

411 阅读3分钟

TLS

以下这段话摘自 RFC 8446 TLS 1.3

The primary goal of TLS is to provide a secure channel between two communicating peers; the only requirement from the underlying transport is a reliable, in-order data stream. Specifically, the secure channel should provide the following properties:

  • Authentication: The server side of the channel is always authenticated; the client side is optionally authenticated. Authentication can happen via asymmetric cryptography (e.g., RSA, the Elliptic Curve Digital Signature Algorithm (ECDSA),or the Edwards-Curve Digital Signature Algorithm (EdDSA) or a symmetric pre-shared key (PSK).
  • Confidentiality: Data sent over the channel after establishment is only visible to the endpoints. TLS does not hide the length of the data it transmits, though endpoints are able to pad TLS records in order to obscure lengths and improve protection against traffic analysis techniques.
  • Integrity: Data sent over the channel after establishment cannot be modified by attackers without detection.

TLS 1.2 handsake

RFC 5246 标准中 TLS handshake 图示:

  Client                                               Server

  ClientHello              -------->
                                                  ServerHello
                                                 Certificate*
                                           ServerKeyExchange*
                                          CertificateRequest*
                           <--------          ServerHelloDone
  Certificate*
  ClientKeyExchange
  CertificateVerify*
  [ChangeCipherSpec]
  Finished                 -------->
                                           [ChangeCipherSpec]
                           <--------                 Finished
  Application Data         <------->         Application Data

         Figure 1.  Message flow for a full handshake
         
 * Indicates optional or situation-dependent messages that are not always sent.
 
 Note: To help avoid pipeline stalls, ChangeCipherSpec is an independent TLS
 protocol content type, and is not actually a TLS handshake message.

恢复已连接过的会话则简单点,只需要一个来回:

  Client                                                Server

  ClientHello               -------->
                                                   ServerHello
                                            [ChangeCipherSpec]
                            <--------                 Finished
  [ChangeCipherSpec]
  Finished                  -------->
  Application Data          <------->         Application Data

      Figure 2.  Message flow for an abbreviated handshake

可以看出 TLS 1.2 初次建立连接需要 4 次握手,相当于 2RTT 之后才能传输应用数据。

密钥交换在握手过程是很关键的部分。TLS 的安全性取决于 master secret,交换的目的是为了计算 premaster secret,这个值是组成 master secret 的来源。TLS 支持多种密钥交换算法:

RSA 是一种事实上的标准密钥交换算法,得到广泛支持。但它有一个严重安全问题:它的设计使得被动攻击只要获得服务器密钥,就能解码所有数据,所以 RSA 正逐渐被其他支持 forward secrecy 交换算法替代。

When RSA is used for server authentication and key exchange, a 48-byte pre_master_secret is generated by the client, encrypted under the server's public key, and sent to the server. The server uses its private key to decrypt the pre_master_secret. Both parties then convert the pre_master_secret into the master_secret, as specified above.

TLS 1.3 handshake

TLS 1.3 相比 TLS 1.2 更安全,也更快速。以下是二者之间的部分区别:

  1. 移除了一些对称加密算法
  2. 支持 0-RTT 模式
  3. 静态 RSA 和 Diffie-Hellman cipher suites 被移除了。所有基于公钥的密钥交换机制现在都提供 forward secrecy
  4. ServerHello 之后的所有消息都被加密

TLS 1.3 的握手图示:

   Client                                           Server

Key  ^ ClientHello
Exch | + key_share*
     | + signature_algorithms*
     | + psk_key_exchange_modes*
     v + pre_shared_key*       -------->
                                                  ServerHello  ^ Key
                                                 + key_share*  | Exch
                                            + pre_shared_key*  v
                                        {EncryptedExtensions}  ^  Server
                                        {CertificateRequest*}  v  Params
                                               {Certificate*}  ^
                                         {CertificateVerify*}  | Auth
                                                   {Finished}  v
                               <--------  [Application Data*]
     ^ {Certificate*}
Auth | {CertificateVerify*}
     v {Finished}              -------->
       [Application Data]      <------->   [Application Data]

+  Indicates noteworthy extensions sent in the previously noted message.

*  Indicates optional or situation-dependent messages/extensions that are not always sent.

{} Indicates messages protected using keys derived from a [sender]_handshake_traffic_secret.

[] Indicates messages protected using keys derived from [sender]_application_traffic_secret_N.

           Figure 1: Message Flow for Full TLS Handshake

可以看到在一个来回就完成了握手的过程。

0-RTT Data

When clients and servers share a PSK (either obtained externally or via a previous handshake), TLS 1.3 allows clients to send data on the first flight ("early data"). The client uses the PSK to authenticate the server and to encrypt the early data.

As shown in Figure 4, the 0-RTT data is just added to the 1-RTT handshake in the first flight. The rest of the handshake uses the same messages as for a 1-RTT handshake with PSK resumption.

     Client                                               Server

     ClientHello
     + early_data
     + key_share*
     + psk_key_exchange_modes
     + pre_shared_key
     (Application Data*)     -------->
                                                     ServerHello
                                                + pre_shared_key
                                                    + key_share*
                                           {EncryptedExtensions}
                                                   + early_data*
                                                      {Finished}
                             <--------       [Application Data*]
     (EndOfEarlyData)
     {Finished}              -------->
     [Application Data]      <------->        [Application Data]

           +  Indicates noteworthy extensions sent in the
              previously noted message.

           *  Indicates optional or situation-dependent
              messages/extensions that are not always sent.

           () Indicates messages protected using keys
              derived from a client_early_traffic_secret.

           {} Indicates messages protected using keys
              derived from a [sender]_handshake_traffic_secret.

           [] Indicates messages protected using keys
              derived from [sender]_application_traffic_secret_N.

           Figure 4: Message Flow for a 0-RTT Handshake

IMPORTANT NOTE: The security properties for 0-RTT data are weaker than those for other kinds of TLS data. Specifically:

  1. This data is not forward secret, as it is encrypted solely under keys derived using the offered PSK.

  2. There are no guarantees of non-replay between connections. Protection against replay for ordinary TLS 1.3 1-RTT data is provided via the server's Random value, but 0-RTT data does not depend on the ServerHello and therefore has weaker guarantees. This is especially relevant if the data is authenticated either with TLS client authentication or inside the application protocol. The same warnings apply to any use of the early_exporter_master_secret.