Back to series
· 18 min read

How Does HTTPS Ensure Communication Security?


I. What Is HTTPS?

HTTPS (Hypertext Transfer Protocol Secure) is a form of HTTP communication that runs over a secure transport layer. It uses the TLS (Transport Layer Security) protocol to protect data transmission between the client and the server.

In ordinary HTTP communication, requests and responses are usually transmitted in plaintext. An attacker on the network path who can intercept the data may be able to read its contents, or even modify requests and responses.

HTTPS introduces TLS between HTTP and the underlying transport to provide security for communication.

1. Differences Between HTTP and HTTPS

Comparison Aspect HTTP HTTPS
Default port 80 443
Data transmission Plaintext TLS encryption
Authentication Does not provide server authentication Usually authenticates the server through digital certificates
Integrity protection Does not provide cryptographic integrity protection Provides message integrity protection
Eavesdropping resistance No Yes
Tamper resistance No Yes
Performance overhead Relatively low TLS handshake and encryption overhead
Typical uses Non-sensitive or restricted network scenarios Websites, APIs, logins, payments, etc.

Note:

  • HTTPS does not mean the website itself is trustworthy; it only indicates that a TLS-protected connection has been established between the client and the server authenticated by the certificate.
  • HTTPS cannot prevent a server from deliberately leaking data.
  • HTTPS cannot prevent browsers, servers, or endpoint devices from being compromised by malware.
  • HTTPS also cannot guarantee that business logic has no vulnerabilities.

2. Core Security Goals of HTTPS

HTTPS primarily addresses three problems.

Confidentiality

Prevents third parties from directly reading the communication content.

For example, when a user logs in to a website over HTTPS, application data such as account names and passwords is not transmitted over the network in plaintext.

Integrity

Prevents communication data from being secretly modified during transmission.

For example, an attacker cannot change the transfer amount in a user request from 100 yuan to 10,000 yuan without being detected.

Authentication

Confirms that the client is connecting to the target server, not a server forged by an attacker.

For example, when visiting https://example.com, the browser verifies whether the certificate provided by the server can prove its control over the target domain.

3. Overall Architecture of HTTPS

HTTPS involves multiple protocol layers:

Application Layer
    HTTP
      |
Security Layer
    TLS
      |
Transport Layer
    TCP
      |
Network Layer
    IP

For HTTP/1.1 and HTTP/2, the typical protocol stack is:

HTTP
  |
TLS
  |
TCP
  |
IP

For HTTP/3, the protocol stack is different:

HTTP/3
  |
QUIC
  |
UDP
  |
IP

HTTP/3 uses QUIC, and QUIC integrates TLS 1.3 handshake and encryption capabilities into the transport protocol, no longer using the traditional TCP plus TLS combination.

II. Why Does HTTPS Need TLS?

TLS is the core of secure HTTPS communication.

It establishes a secure connection between the two communicating parties and protects the data transmitted afterward.

1. What Problem Does TLS Solve?

Suppose the client sends an HTTP request directly to the server:

POST /login HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "username": "alice",
  "password": "123456"
}

If a malicious proxy, a public Wi-Fi attacker, or a compromised network device exists on the communication path, the request content may be intercepted.

An attacker may even modify the response:

HTTP/1.1 200 OK
Content-Type: text/html

<script>
alert("malicious code");
</script>

TLS protects HTTP data so that intermediate network nodes cannot easily read or modify the communication content.

2. Capabilities Provided by TLS

TLS mainly provides the following capabilities:

Capability Purpose
Encryption algorithm negotiation Determines the cipher suites and algorithm parameters used by both parties
Authentication Verifies the server certificate and its identity
Key exchange Establishes a shared session key between both parties
Data encryption Uses symmetric encryption to protect application data
Integrity protection Detects whether transmitted data has been tampered with
Forward secrecy When an appropriate ephemeral key exchange method is used, reduces the risk to historical communications posed by leakage of a long-term private key

TLS does not simply encrypt HTTP data; it establishes a secure communication environment through multiple cryptographic mechanisms working together.

III. What cryptographic technologies does HTTPS use?

To understand HTTPS, you first need to understand symmetric encryption, asymmetric encryption, hash algorithms, digital signatures, and key exchange.

1. Symmetric encryption

Symmetric encryption uses the same key to perform encryption and decryption.

For example:

Original data
    |
    | Encrypt using key K
    v
Ciphertext
    |
    | Decrypt using the same key K
    v
Original data

Common algorithms:

  • AES: A widely used symmetric encryption algorithm.
  • ChaCha20: Suitable for software implementation, with advantages on some mobile devices and low-power devices.
  • AES-GCM: Combines encryption and authentication to provide confidentiality and integrity protection.
  • ChaCha20-Poly1305: Combines stream encryption with message authentication.

Advantages of symmetric encryption:

  • Fast encryption and decryption.
  • Suitable for transmitting large amounts of data.
  • Relatively low computational overhead.

The disadvantage is that key distribution is difficult.

If the client and server do not yet share a key, how can the key be securely delivered to the other party?

This is one of the problems TLS needs to solve.

2. Asymmetric encryption

Asymmetric cryptography uses a pair of related keys:

  • Public key: Can be made public.
  • Private key: Must be properly kept by the holder.

The public key and private key are mathematically related, but under reasonable security parameters, it is infeasible to efficiently derive the private key from the public key.

Traditional RSA key encryption can provide confidentiality by encrypting with the public key and decrypting with the private key.

However, modern TLS 1.3 no longer supports RSA key transport. It mainly establishes shared keys through ephemeral Diffie-Hellman key exchange.

Asymmetric cryptography can also be used for digital signatures.

For example:

Server private key
    |
    | Sign the data digest
    v
Digital signature
    |
    | Client verifies using the server public key
    v
Verify whether the signature is valid

Digital signatures are mainly used to verify:

  • Whether the signature was generated by the corresponding private key.
  • Whether the data covered by the signature has been modified.
  • Whether the signature can be verified with the corresponding public key.

Note that digital signatures and encryption serve different purposes.

3. Hash algorithms

Hash algorithms can map data of arbitrary length to a fixed-length digest.

For example:

Original data
    |
    v
SHA-256
    |
    v
256-bit digest

Common hash algorithms:

Algorithm Digest length Description
MD5 128-bit No longer suitable for security purposes
SHA-1 160-bit No longer suitable for digital signature security purposes
SHA-256 256-bit Widely used
SHA-384 384-bit Common in high-security cipher suites
SHA-512 512-bit SHA-2 family algorithm

Hash algorithms have the following characteristics:

  • The same input produces the same digest.
  • Changes to the input usually cause the digest to change significantly.
  • It is very difficult to infer the original data from the digest.
  • It is very difficult to find two different inputs that produce the same digest.

Hash algorithms in TLS can be used for:

  • Handshake message digests.
  • Key derivation.
  • Calculations related to digital signatures.
  • Message authentication mechanisms.

It is necessary to distinguish: a plain hash by itself is not equivalent to message authentication. TLS uses AEAD or other cryptographic mechanisms to protect data integrity.

4. Digital signatures

Digital signatures are used to prove the relationship between data and a specific private key.

A server can use its own private key to sign a certificate or handshake-related data, and the client verifies it using the corresponding public key.

Common signature algorithms:

  • RSA-PSS
  • ECDSA
  • Ed25519

The basic process of a digital signature:

Signer
  |
  v
Compute data digest
  |
  v
Generate signature using private key
  |
  v
Send data and signature
  |
  v
Verifier verifies using public key
  |
  v
Determine whether the signature is valid

In TLS 1.3, the server usually uses the private key corresponding to the certificate to sign the handshake context, proving that it holds that private key.

5. Key exchange: Diffie-Hellman

Diffie-Hellman key exchange allows both parties to negotiate the same shared secret without directly transmitting the final shared key.

Common forms:

  • DH: Finite-field Diffie-Hellman.
  • ECDH: Elliptic-curve Diffie-Hellman.
  • ECDHE: Ephemeral elliptic-curve Diffie-Hellman.

Modern TLS usually uses ephemeral key exchange methods such as ECDHE.

Basic principle:

The client and server each generate their own ephemeral key pair and exchange the public parts.

Both parties use their own ephemeral private key and the other party's ephemeral public key to compute the same shared secret.

Client generates an ephemeral key pair
       |
       v
Send the client ephemeral public key
       |
       |-------------------->
                             |
                             Server generates an ephemeral key pair
                             |
                             v
                             Send the server ephemeral public key
                             |
       <--------------------|
       |
       v
Client computes the shared secret

The server also computes the same shared secret
       |
       v
Both parties use a key derivation function to generate the session key

The most important point here is:

What the client and server exchange is ephemeral public keys, not the final session key.

Even if an attacker can record the public parameters exchanged by both parties, they cannot directly compute the shared secret under reasonable security parameters.

6. Why doesn't HTTPS use asymmetric encryption to transmit all data directly?

Because asymmetric cryptographic operations are usually more expensive than symmetric encryption.

If every HTTP request and response were handled using asymmetric cryptographic operations such as RSA or ECDSA, the performance overhead would be enormous.

Therefore, HTTPS uses a hybrid encryption system:

Phase Technology used Purpose
Identity authentication Digital certificates, digital signatures Verify the server's identity
Key agreement ECDHE, etc. Establish a shared secret
Key derivation HKDF, etc. Generate session keys
Data transmission AES-GCM, ChaCha20-Poly1305 Encryption and integrity protection

The overall idea is:

Asymmetric cryptography
    |
    v
Identity authentication and key agreement
    |
    v
Derive session keys
    |
    v
Symmetric encryption
    |
    v
Protect HTTP data

This solves the key agreement problem while also ensuring efficiency when transmitting large amounts of data.

IV. What Is a Digital Certificate?

HTTPS needs to confirm the server's identity.

If a client merely receives a public key, it cannot determine who that public key actually belongs to.

An attacker can easily generate their own key pair and then pass off their public key as the public key of the target website.

Digital certificates are an important mechanism for solving the identity binding problem.

1. Definition of a Digital Certificate

A digital certificate is an electronic credential issued by a trusted Certificate Authority, or CA.

It associates information such as the domain name, organizational details, and public key with the digital signature of the certificate authority.

A typical X.509 certificate contains:

Field Purpose
Subject Subject information of the certificate
Issuer Certificate issuer
Subject Public Key Info The certificate's public key and its algorithm
Serial Number Certificate serial number
Not Before When the certificate becomes valid
Not After When the certificate expires
Subject Alternative Name Domain names or IPs the certificate is valid for
Key Usage Restrictions on how the public key may be used
Extended Key Usage Extended usages, such as server authentication
Signature Algorithm Certificate signature algorithm
Signature The signature the CA generates over the certificate contents

Modern browsers mainly use the Subject Alternative Name, or SAN, to verify whether a certificate is valid for the domain being accessed.

You cannot rely solely on the Common Name in the certificate to determine a domain match.

2. Certificate Authority (CA)

A CA is a trusted organization responsible for issuing and managing digital certificates.

Common CA services include:

  • DigiCert
  • GlobalSign
  • Let's Encrypt
  • Sectigo

A CA's core responsibilities:

  1. Confirm, according to validation rules, that the applicant has control over the domain or organization.
  2. Issue certificates based on the application information.
  3. Sign the certificate contents using the CA's private key.
  4. Provide certificate status management and revocation mechanisms.

A CA does not encrypt data on behalf of the website; it provides trusted endorsement of the server's identity.

3. Certificate Chain

Browsers generally do not trust every website certificate directly; instead, they establish trust through a certificate chain.

A typical structure:

Root CA
  |
  v
Intermediate CA
  |
  v
Server certificate

Here:

  • Root certificates are usually pre-installed in the operating system's or browser's trust store.
  • Intermediate certificates are issued by a higher-level CA.
  • Server certificates are issued by an intermediate CA or another trusted CA.

Starting from the server certificate, the browser verifies signatures and certificate constraints level by level, ultimately attempting to build a path to a trusted root certificate.

4. How Does a Browser Verify a Certificate?

After receiving the server certificate, the browser must perform a number of checks:

  1. Verify whether the certificate chain can be linked to a trusted root CA.
  2. Verify the digital signature of each certificate in the chain.
  3. Check whether the certificate has expired or is not yet valid.
  4. Check whether the accessed domain matches the SAN.
  5. Check the certificate's intended usage and extension constraints.
  6. Check the certificate's revocation status according to browser policy.
  7. Check whether the certificate complies with the relevant security policies.

Only after verification succeeds will the browser continue to complete the TLS handshake.

5. Certificate chain verification flow

flowchart TD
    A["Receive server certificate"] --> B["Read certificate issuer"]
    B --> C["Find the parent CA certificate"]
    C --> D["Verify the certificate's digital signature"]
    D --> E{"Does verification pass?"}
    E -->|No| F["Terminate verification and report an error"]
    E -->|Yes| G{"Has a trusted root certificate been reached?"}
    G -->|No| C
    G -->|Yes| H["Check validity period and certificate usage"]
    H --> I["Check domain SAN"]
    I --> J["Check revocation status and security policies"]
    J --> K{"Do all checks pass?"}
    K -->|No| F
    K -->|Yes| L["Certificate verification succeeded"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style D fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style E fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style F fill:#FCE8E6,stroke:#E5A6A0,color:#8B3A35
    style G fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style J fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style K fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style L fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

Note: certificate verification does not mean that the server's own business is secure, nor does it mean that the certificate applicant is necessarily trustworthy.

V. TLS Handshake: How Does HTTPS Establish a Secure Connection?

The TLS handshake is the core process by which HTTPS establishes secure communication.

It is responsible for negotiating protocol parameters, verifying the server's identity, establishing a shared key, and confirming that both parties are ready for secure communication.

1. TLS 1.2 and TLS 1.3

TLS has multiple versions:

Version Release Year Characteristics
SSL 2.0 1995 Deprecated; has serious security issues
SSL 3.0 1996 Deprecated
TLS 1.0 1999 Deprecated
TLS 1.1 2006 Deprecated
TLS 1.2 2008 Widely used for a long time
TLS 1.3 2018 Simplified handshake; removes many insecure mechanisms

SSL is the predecessor of TLS.

Modern HTTPS should prefer TLS 1.3, and support TLS 1.2 when compatibility requires it.

SSL 2.0, SSL 3.0, TLS 1.0, and TLS 1.1 should no longer be used.

2. The overall process of the TLS 1.3 handshake

In a normal full handshake, TLS 1.3 typically completes key negotiation in a single round trip.

The main messages include:

  • ClientHello
  • ServerHello
  • EncryptedExtensions
  • Certificate
  • CertificateVerify
  • Finished

The interaction between the client and the server is as follows:

sequenceDiagram
    participant C as Client
    participant S as Server

    C->>S: ClientHello: supported versions, cipher suites, ephemeral public key
    S->>C: ServerHello: selected parameters, server ephemeral public key
    Note over C,S: Both sides compute the shared secret and derive handshake keys
    S->>C: EncryptedExtensions
    S->>C: Certificate
    S->>C: CertificateVerify
    S->>C: Finished
    Note over C: Verify certificate, signature, and Finished
    C->>S: Finished
    Note over C,S: Both sides confirm the handshake is complete
    C->>S: Encrypted HTTP request
    S->>C: Encrypted HTTP response

It is worth noting here that not all TLS 1.3 handshake messages are transmitted in plaintext.

After ServerHello, many handshake messages are already encrypted using the negotiated handshake keys.

3. ClientHello: The Client Initiates the Handshake

The client first sends ClientHello.

The main contents include:

Field or Extension Purpose
supported_versions Supported TLS versions
cipher_suites Supported cipher suites
key_share Client ephemeral key exchange parameters
supported_groups Supported key exchange groups
signature_algorithms Supported signature algorithms
server_name SNI, indicating the target domain name
ALPN Supported application-layer protocols

For example:

The client supports TLS 1.2 and TLS 1.3 and can indicate this to the server via supported_versions.

The client also provides one or more ephemeral key exchange parameters, and the server selects appropriate parameters based on what it supports.

4. ServerHello: The Server Selects Parameters

After receiving ClientHello, the server selects the following based on its own configuration:

  • TLS protocol version.
  • Cipher suite.
  • Key exchange group.
  • The corresponding server ephemeral key parameters.

It then returns ServerHello.

Both sides can then use their own ephemeral private keys and the other party's ephemeral public key to compute a shared secret.

5. Both Sides Derive Handshake Keys

TLS 1.3 uses HKDF to derive keys for different purposes from the shared secret.

Simplified process:

ECDHE shared secret
      |
      v
HKDF key derivation
      |
      v
Handshake traffic keys
      |
      v
Encrypted handshake messages
      |
      v
Application traffic keys
      |
      v
Encrypted HTTP data

The client and server derive keys for the send and receive directions separately.

The actual TLS 1.3 key derivation process also includes the Transcript Hash, derivation stages, and key material for different directions; it does not simply use the ECDHE result directly as an AES key.

6. Certificate: The Server Sends Its Certificate

The server sends a Certificate message containing the server certificate chain.

The client needs to verify:

  • Whether the certificate chain is trusted.
  • Whether the domain name matches.
  • Whether the certificate is within its validity period.
  • Whether the certificate is suitable for server authentication.
  • Whether the certificate satisfies the relevant security policies.

If certificate verification fails, the browser usually displays a security warning or terminates the connection.

7. CertificateVerify: The Server Proves It Holds the Private Key

Sending the certificate alone is not enough.

An attacker can also copy the public certificate contents.

Therefore, the server also needs to digitally sign the handshake context using the private key corresponding to the certificate.

The client verifies the signature using the public key in the certificate.

This proves that:

The server currently participating in the TLS handshake indeed holds the private key matching the certificate.

8. Finished: Confirming Handshake Integrity

The Finished message is used to confirm the integrity of the handshake process.

Both sides compute verification data based on the messages exchanged during the handshake and authenticate it using the corresponding handshake keys.

If handshake messages are modified, verification may fail.

After both sides complete Finished message verification, they can proceed to the encrypted application data transmission phase.

VI. Why Is TLS 1.3 More Secure and Faster?

TLS 1.3 does not just add new algorithms; it also simplifies the handshake process and cryptographic design.

1. Fewer Handshake Round Trips

A traditional full TLS 1.2 handshake usually requires two round trips to complete, depending on the key exchange method.

A normal full TLS 1.3 handshake usually requires only one round trip to complete negotiation.

In scenarios with high network latency, this change can significantly improve connection setup time.

2. Removal of Insecure Algorithms and Mechanisms

TLS 1.3 removes many legacy compatibility mechanisms, such as:

  • Static RSA key transport.
  • Static DH key exchange.
  • RC4.
  • 3DES.
  • CBC-mode cipher suites.
  • Compression extension.
  • Renegotiation mechanism.

TLS 1.3 primarily uses AEAD cipher suites, such as:

  • AES-128-GCM
  • AES-256-GCM
  • ChaCha20-Poly1305

3. Forward Secrecy by Default

Forward secrecy means:

Even if the server's long-term private key is leaked in the future, an attacker cannot decrypt previously recorded sessions using only the long-term private key.

The normal full handshake in TLS 1.3 uses ephemeral key exchange, such as ECDHE.

Each connection uses independent ephemeral key material. After the handshake is complete, ephemeral secrets that are no longer needed should be promptly cleared.

Note that when session resumption or 0-RTT is used, the specific security properties differ.

4. TLS 1.3 vs. TLS 1.2

Aspect TLS 1.2 TLS 1.3
Full handshake Usually two round trips Usually one round trip
Key exchange Supports multiple legacy mechanisms Mainly uses ephemeral DH key exchange
RSA key transport Supported Not supported
Forward secrecy Depends on the key exchange method The normal full handshake uses ephemeral key exchange
Cipher suites More complex composition Simplified to AEAD cipher suites
Handshake message protection Some handshake messages in plaintext Main handshake messages after ServerHello are encrypted
0-RTT Not supported Supports early data in session resumption scenarios

VII. What Is 0-RTT in TLS 1.3?

0-RTT is an early data transmission mechanism provided by TLS 1.3.

If the client and server have previously established a connection and the client holds valid session resumption information, the client can send application data early during the new handshake.

This reduces the waiting time when resuming a connection.

1. Basic 0-RTT Flow

sequenceDiagram
    participant C as Client
    participant S as Server

    Note over C,S: The two parties have previously established a TLS session
    C->>S: ClientHello + PSK + 0-RTT Early Data
    S->>C: ServerHello
    S->>C: EncryptedExtensions
    S->>C: Finished
    C->>S: Finished
    Note over C,S: Handshake complete, subsequent data is protected with application traffic keys

PSK is part of the pre-shared key mechanism and usually comes from previously established session state.

2. Main Risk of 0-RTT: Replay Attacks

0-RTT data may be captured by an attacker and replayed under certain conditions.

For example:

A user submits a transfer request:

POST /transfer HTTP/1.1
Content-Type: application/json

{
  "amount": 1000,
  "target": "account-b"
}

If this request is sent as 0-RTT data, an attacker may attempt to replay it.

Therefore, servers need to impose strict restrictions on 0-RTT requests.

3. Which Requests Are Suitable for 0-RTT?

Usually more suitable:

  • Reading static resources.
  • Retrieving public information.
  • Requests that do not produce business side effects.

Usually unsuitable:

  • Payments.
  • Transfers.
  • Creating orders.
  • Modifying account status.
  • Deleting data.
  • Other operations that cannot be executed repeatedly.

Even for GET, you cannot determine whether it is safe based only on the HTTP method name. The server must ensure that the actual business operation has no side effects.

In engineering practice, replay risks can be reduced through idempotent design, request deduplication, and business state validation.

VIII. How Does HTTPS Prevent Man-in-the-Middle Attacks?

Man-in-the-middle attacks (MITM, Man-in-the-Middle Attack) are an important problem that HTTPS must solve.

The attacker sits between the client and server and attempts to impersonate the server, intercept data, or modify communication content.

1. Man-in-the-Middle Attacks with Plain HTTP

Suppose a user visits a website.

If the attacker controls public Wi-Fi or a network proxy, they can intercept communication between the user and the server.

Client
   |
   v
Attacker
   |
   v
Server

If plain HTTP is used, the attacker may:

  • Read login credentials.
  • Modify web page content.
  • Inject malicious scripts.
  • Modify API responses.
  • Redirect users to phishing websites.

2. How Does HTTPS Stop Attacks?

HTTPS mainly relies on three mechanisms:

Certificate Validation

The client validates the server certificate and confirms that the target domain name matches the certificate identity.

Digital Signatures

The server uses the private key corresponding to the certificate to prove that it holds the legitimate private key.

Session Encryption and Integrity Protection

Both parties use the negotiated session keys to protect subsequent communication.

Even if an attacker can forward network data, they cannot easily decrypt or modify protected data without knowing the session keys.

3. Man-in-the-Middle Attack Defense Flow

flowchart TD
    A["Client accesses the target website"] --> B["Server sends the certificate"]
    B --> C["Client verifies the certificate chain"]
    C --> D{"Is the certificate trusted?"}
    D -->|No| E["Terminate the connection and display a security warning"]
    D -->|Yes| F["Verify the domain name and certificate usage"]
    F --> G{"Does the domain name match?"}
    G -->|No| E
    G -->|Yes| H["Verify the server handshake signature"]
    H --> I{"Is the signature valid?"}
    I -->|No| E
    I -->|Yes| J["Establish a shared session key"]
    J --> K["Encrypt and transmit HTTP data"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style D fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style E fill:#FCE8E6,stroke:#E5A6A0,color:#8B3A35
    style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style G fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style H fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style I fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style J fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style K fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

4. HTTPS Cannot Completely Prevent Man-in-the-Middle Attacks

HTTPS relies on the certificate trust system.

If an attacker can:

  • Control the client device.
  • Install a malicious root certificate on the client.
  • Control a trusted CA.
  • Compromise the server and obtain the private key.
  • Trick users into visiting the wrong domain.
  • Control an endpoint proxy and gain legitimate decryption rights.

they may be able to break through some HTTPS security boundaries.

Therefore, HTTPS is not absolutely secure; rather, it provides reliable transport protection when correctly configured and in a trusted environment.

9. The Complete HTTPS Communication Process

To understand HTTPS, it is best to walk through the entire process of a browser accessing a website.

1. Entering an HTTPS Address in the Browser

The user enters:

https://www.example.com

The browser first parses the URL and identifies:

  • Protocol: HTTPS.
  • Domain name: www.example.com.
  • Default port: 443.
  • Path: /.

It then needs to resolve the domain name and establish a network connection.

2. DNS Resolution

The browser needs to obtain the IP address corresponding to the target domain name.

DNS resolution may involve:

  • Browser DNS cache.
  • Operating system DNS cache.
  • Local DNS resolver.
  • Recursive DNS server.
  • Authoritative DNS server.

Only after obtaining the target IP can the browser connect to the server.

3. Establishing a TCP Connection

For HTTP/1.1 and HTTP/2, the browser usually establishes a TCP connection with the server first.

TCP three-way handshake:

Client                         Server
   |                              |
   | -------- SYN --------------> |
   | <------ SYN + ACK ---------- |
   | -------- ACK --------------> |
   |                              |
   |   TCP connection established |

If HTTP/3 is used, the connection is established via QUIC, and a separate TCP three-way handshake is no longer performed.

4. TLS Handshake

After the TCP connection is established, the client and server begin the TLS handshake.

The TLS handshake is responsible for:

  • Negotiating the TLS version.
  • Selecting a cipher suite.
  • Exchanging ephemeral key parameters.
  • Verifying the server certificate.
  • Deriving session keys.
  • Confirming handshake integrity.

5. Sending the HTTP Request

After the TLS handshake is complete, the client sends an encrypted HTTP request.

For example:

GET /index.html HTTP/1.1
Host: www.example.com
Accept: text/html
User-Agent: ExampleBrowser/1.0

This HTTP content is protected within the TLS record layer.

6. The Server Processes the Request

After receiving and decrypting the request, the server passes it to a web service or application for processing.

This may involve:

  • Nginx.
  • Apache.
  • Spring Boot.
  • Django.
  • FastAPI.
  • Node.js.
  • Database.
  • Caching system.

7. Return an HTTP Response

The server generates an HTTP response:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=3600

<html>
  <body>
    Hello HTTPS
  </body>
</html>

The response is encrypted via TLS and then transmitted to the client.

8. Browser Decryption and Rendering

The browser decrypts the response using the corresponding session key, then parses HTML, CSS, and JavaScript, loads related resources, and completes page rendering.

9. Complete HTTPS Flowchart

flowchart TD
    A["User enters HTTPS URL"] --> B["Browser parses URL"]
    B --> C["DNS domain name resolution"]
    C --> D["Obtain server IP"]
    D --> E{"HTTP version used"}
    E -->|HTTP 1.1 or HTTP 2| F["Establish TCP connection"]
    E -->|HTTP 3| G["Establish QUIC connection"]
    F --> H["Perform TLS handshake"]
    G --> H
    H --> I["Verify server certificate"]
    I --> J{"Certificate verification passed"}
    J -->|No| K["Terminate connection and display error"]
    J -->|Yes| L["Derive session key"]
    L --> M["Send encrypted HTTP request"]
    M --> N["Server decrypts and processes request"]
    N --> O["Generate HTTP response"]
    O --> P["TLS-encrypt response data"]
    P --> Q["Browser decrypts response"]
    Q --> R["Parse resources and render page"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style D fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style E fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#EAE4F8,stroke:#B7A7D8,color:#51416F
    style J fill:#FFF2CC,stroke:#E5C978,color:#765B19
    style K fill:#FCE8E6,stroke:#E5A6A0,color:#8B3A35
    style L fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style M fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style N fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style O fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style P fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style Q fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style R fill:#EAE4F8,stroke:#B7A7D8,color:#51416F

In practice, browsers may optimize the above process through DNS caching, connection reuse, preconnection, TLS session resumption, and other means, so not every request goes through all steps in full.

10. SNI and ALPN in HTTPS

In real server deployments, one IP address may host multiple HTTPS websites.

SNI and ALPN are very important extension mechanisms in the TLS handshake.

1. SNI: Server Name Indication

SNI (Server Name Indication) allows the client to include the target domain name in the TLS ClientHello.

For example:

IP: 203.0.113.10

www.example.com
api.example.com
shop.example.com

These three domain names may share the same IP address.

The server needs to know which domain name the client is accessing in order to select the corresponding certificate and site configuration.

SNI solves this problem.

In traditional TLS, SNI usually appears in plaintext in the ClientHello, so network observers may be able to tell which domain name the client is visiting.

2. ECH: Encrypted Client Hello

ECH (Encrypted ClientHello) is used to protect some sensitive information in the ClientHello.

It can reduce the ability of network observers to directly identify the accessed domain name through SNI.

However, ECH depends on support from multiple parties such as the client, DNS, and server, so it cannot be assumed that ECH is enabled on all HTTPS connections.

3. ALPN: Application-Layer Protocol Negotiation

ALPN (Application-Layer Protocol Negotiation) allows the client and server to negotiate the application-layer protocol during the TLS handshake.

Common protocol identifiers:

Identifier Protocol
http/1.1 HTTP/1.1
h2 HTTP/2
h3 HTTP/3 uses the corresponding QUIC protocol negotiation mechanism

For example:

The client supports HTTP/1.1 and HTTP/2.

The server also supports both versions.

Both parties negotiate a suitable application-layer protocol through ALPN.

4. Differences Between SNI and ALPN

Technology Issue Addressed
SNI Which domain name the client wants to access
ALPN Which application-layer protocol the client and server will use
ECH Protects some sensitive information in ClientHello

11. Relationship Between HTTPS and HTTP/2, HTTP/3

HTTPS is not the same as HTTP/2, nor is it the same as HTTP/3.

They are at different protocol layers.

1. HTTP/1.1 + TLS

Typical structure:

HTTP/1.1
    |
   TLS
    |
   TCP
    |
    IP

Each HTTP request and response is protected by TLS.

2. HTTP/2 + TLS

HTTP/2 usually runs on top of TLS.

Main features:

  • Binary framing.
  • Multiplexing.
  • HPACK header compression.
  • Multiple concurrent streams over a single connection.
  • Reduces application-layer head-of-line blocking.

HTTP/2 still uses TCP, so packet loss at the TCP layer can affect multiple streams on the same connection.

3. HTTP/3 + QUIC

HTTP/3 uses QUIC as its transport foundation.

QUIC is based on UDP but implements the following itself:

  • Reliable data transmission.
  • Congestion control.
  • Flow control.
  • Multiplexing.
  • Connection migration.
  • Integrated TLS 1.3 handshake.

Therefore, the fact that QUIC uses UDP does not mean that HTTP/3 is unreliable.

4. Comparison of the Three

Comparison Dimension HTTP/1.1 HTTP/2 HTTP/3
Transport basis TCP TCP QUIC, based on UDP
TLS TLS is used in HTTPS scenarios TLS is usually used in browsers Integrated TLS 1.3
Data format Text-based messages Binary framing Binary frames
Multiplexing Depends on multiple connections or pipelining Multiple streams over a single connection Multiple streams over a single connection
Header compression No dedicated compression mechanism HPACK QPACK
TCP head-of-line blocking Present Can still be affected by TCP packet loss Does not use TCP
Connection migration No protocol-level support No QUIC-style migration Supports connection migration