Detailed Explanation of TLS Handshake Process and Key Exchange
Problem Description
The TLS handshake is the core process for establishing a secure communication connection in the HTTPS protocol. Interviews often require a detailed explanation of its steps, the principles of key exchange, and the role of key algorithms. It is necessary to explain how the handshake achieves identity authentication, key negotiation, and data encryption.
Solution Process
-
Handshake Objectives and Basic Concepts
- Objectives: The client and server negotiate a symmetric encryption key (session key) for subsequent encrypted communication, while also verifying the server's identity (and optionally the client's identity).
- Core Problem: How to securely transmit the symmetric key? — Through asymmetric encryption (e.g., RSA, ECDHE) to protect the key exchange process.
- Key Document: Server digital certificate (issued by a CA), containing information such as the public key, domain name, and validity period.
-
Detailed TLS Handshake Steps
Step 1: ClientHello- The client sends the highest supported TLS version, a list of supported cipher suites (e.g.,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256), and a random number (Client Random). - Cipher suite meaning: Key exchange algorithm (ECDHE) + Signature algorithm (RSA) + Symmetric encryption algorithm (AES-128-GCM) + Hash algorithm (SHA256).
Step 2: ServerHello
- The server selects a TLS version and cipher suite supported by both parties, and sends its own random number (Server Random) and the server's digital certificate.
- If using the ECDHE algorithm, the server additionally sends a Server Key Exchange message containing elliptic curve parameters and a temporary public key (server ECDHE public key).
Step 3: Certificate Verification and Key Calculation
- The client verifies the certificate's validity (expiration, domain name match, trusted CA chain, etc.).
- The client generates a temporary public-private key pair (client ECDHE public key) and sends it to the server via the Client Key Exchange message.
- Key Exchange Principle:
- Both parties use the ECDHE algorithm, along with the other party's public key and their own private key, to calculate the same pre-master secret (Pre-Master Secret).
- Combined with the previous Client Random and Server Random, the master secret (Master Secret) is generated via a PRF (Pseudo-Random Function), ultimately deriving the session keys (symmetric encryption key, MAC key, etc.).
Step 4: Handshake Completion and Encrypted Communication
- Both parties send a Change Cipher Spec message to confirm that subsequent communication will use the negotiated session key for encryption.
- Exchange Finished messages (encrypted handshake hash) to verify that the handshake process has not been tampered with.
- Thereafter, the session enters the application data transmission phase, using symmetric encryption to protect the data.
- The client sends the highest supported TLS version, a list of supported cipher suites (e.g.,
-
Key Technologies and Interview Points
- Forward Secrecy: If using ephemeral key exchange algorithms like ECDHE, even if the server's private key is leaked, historical sessions cannot be decrypted (because temporary keys are generated for each handshake).
- Difference between RSA and ECDHE:
- RSA key exchange: The client directly encrypts the pre-master secret with the certificate's public key and sends it, offering no forward secrecy.
- ECDHE: The pre-master secret is dynamically calculated using temporary elliptic curve parameters, providing forward secrecy.
- Session Resumption: If the client has previously connected to the same server, the full handshake can be skipped using a Session ID or Session Ticket, reducing latency.
-
Summary
The TLS handshake uses asymmetric encryption to ensure secure key exchange, a certificate mechanism for identity authentication, and finally switches to efficient symmetric encryption. Understanding the cryptographic principles behind each step (such as elliptic curve operations in ECDHE) and the design objectives (like forward secrecy) is key to answering in-depth questions.