DEV Community

Anusha Renangi
Anusha Renangi

Posted on Edited on

Networking Essentials for Software Engineers – Part 8: What's Really Behind the 'S' in HTTPS?

You're sitting in a coffee shop, enjoying your coffee and using the free Wi-Fi.

You open a website, enter your username and password, and log in.

Everything looks normal.

You're enjoying your coffee, without paying much attention to the people sitting around you.

But what if one of them is a hacker?

If the website uses HTTP, your username and password travel without encryption.

Someone who manages to intercept your network traffic could read those details without you even knowing.

This is one of the reasons websites use HTTPS instead of HTTP.

That extra S stands for Secure. It helps protect the information travelling between your browser and the server.

But how does it actually protect our information? How can the browser and server communicate securely when they've never connected before?

In this article, we'll understand what happens behind that S in HTTPS and how it works internally.

How Does HTTPS Actually Work?

HTTPS uses a protocol called TLS (Transport Layer Security) to protect the communication between your browser and the server.

TLS mainly helps with three things:

  1. Encryption: Keeps the information private so others can't read it.
  2. Authentication: Helps your browser confirm that it's communicating with the correct server.
  3. Integrity: Helps detect if someone changes the information while it's travelling.

So, when you open an HTTPS website, TLS helps protect your information while it travels between your browser and the server.

But how does TLS actually do all this?

By the end of this article, you'll understand how it all works internally.

How Does Encryption Work?

When you log in to a website, your browser sends your username and password to the server.

For example:

username=anusha
password=hello123
Enter fullscreen mode Exit fullscreen mode

This is readable information, which is called plaintext.

We don't want someone who intercepts the request to read these details.

So, before sending the information, the browser converts it into an unreadable form using encryption.

For example:

Original data:
username=anusha&password=hello123

Encrypted data:
8fA92xK7pQ31mZ...
Enter fullscreen mode Exit fullscreen mode

The unreadable information is called ciphertext.

When the encrypted data reaches the server, it is converted back into its original readable form. This process is called decryption.

How Do Encryption Keys Work?

Encryption doesn't simply replace letters with random characters.

It uses a mathematical algorithm along with an encryption key.

Think of the key as a secret value that controls how the information is encrypted.

The server needs the correct key to decrypt the information.

One common approach is called symmetric encryption, where both sides use the same secret key.

For example:

  • The browser uses a secret key to encrypt your login details.
  • The server uses that same key to decrypt them.

HTTPS uses symmetric encryption to protect HTTP requests and responses.

But there's a problem.

Your browser and the server are two different computers communicating over the internet.

Both need the same secret key, but they can't simply send it over the internet because someone could intercept it.

How Can the Browser and Server Establish the Same Secret?

We know that the browser and server need a shared secret to communicate securely.

But sending that secret directly over the internet isn't safe.

Instead, they can calculate the same secret independently without sharing the secret itself.

This is possible using a method called Diffie–Hellman key exchange.

Let's understand it with a simple example.

Step 1: Choose Public and Private Numbers

The browser and server agree on two public numbers:

  • g = 5
  • m = 23

These numbers can be known to anyone.

Now, each side chooses its own private number:

  • Browser: a = 6
  • Server: b = 15

These private numbers are never shared.

Step 2: Calculate and Exchange Public Values

The browser calculates:

A=ga mod m A = g^a \bmod m

Substituting our values:

A=56 mod 23=8 A = 5^6 \bmod 23 = \boxed{8}

The server calculates:

B=gb mod m B = g^b \bmod m
B=515 mod 23=19 B = 5^{15} \bmod 23 = \boxed{19}

Here, mod means the remainder after division.

Now the browser sends 8 to the server, and the server sends 19 to the browser.

Step 3: Calculate the Shared Secret

The browser uses the value received from the server along with its own private number:

S=Ba mod m S = B^a \bmod m
S=196 mod 23=2 S = 19^6 \bmod 23 = \boxed{2}

The server does the same using the browser's public value and its own private number:

S=Ab mod m S = A^b \bmod m
S=815 mod 23=2 S = 8^{15} \bmod 23 = \boxed{2}

Both calculated the same secret: 2.

Why Do Both Get the Same Secret?

The browser calculates:

S=(gb)a mod m=gba mod m S = (g^b)^a \bmod m = g^{ba} \bmod m

The server calculates:

S=(ga)b mod m=gab mod m S = (g^a)^b \bmod m = g^{ab} \bmod m

Since a × b = b × a, both calculations produce the same result.

The important part is that neither the browser nor the server sent its private number or the final shared secret over the network.

Someone watching the communication can see the exchanged public values, but with the much stronger mathematics used in real TLS, calculating the private numbers from those values is practically impossible.

Our example uses small numbers to make the calculation easy. Modern TLS commonly uses a stronger version called ECDHE (Elliptic Curve Diffie–Hellman Ephemeral).

But There's Still One Problem

Now the browser and server can calculate the same shared secret without sending it directly over the internet.

When you open https://example.com, your browser exchanges public values with the server.

What if a hacker intercepts this communication and sends their own public value to your browser instead of the server's value?

Your browser could end up establishing a shared secret with the hacker, thinking it's communicating with the actual website.

The hacker could also establish a separate shared secret with the real server.

This is called a Man-in-the-Middle (MITM) attack.

So, establishing a shared secret alone isn't enough. The browser also needs to verify that it's communicating with the correct server.

How Does the Browser Know It's Connecting to the Correct Server?

In the previous section, we saw how a hacker could interfere with the key exchange and pretend to be the actual server.

So, before trusting the connection, our browser needs to verify the server's identity.

For this, HTTPS uses TLS certificates.

Let's understand how they work using https://example.com.

Step 1: The Website Gets a TLS Certificate

The owner of example.com first generates two mathematically related keys:

  • Public key: Can be shared with anyone.
  • Private key: Must be kept secret by the website owner.

The website owner then requests a certificate from an organization called a Certificate Authority (CA).

For example, Let's Encrypt is a Certificate Authority.

The CA checks whether the person requesting the certificate actually controls example.com.

Once verified, the CA creates a certificate containing information such as:

Domain:      example.com
Public Key:  WEBSITE_PUBLIC_KEY
Issued By:   Certificate Authority
Valid Until: Expiration date
Enter fullscreen mode Exit fullscreen mode

Now, the CA uses its own private key to create a digital signature for the certificate.

A digital signature is a value calculated using mathematical algorithms. It helps others verify who signed the certificate and whether its information has been changed.

The CA adds this signature to the certificate and issues it to the website owner.

Step 2: How Does the Browser Verify the Certificate?

When you install an operating system like Windows, Ubuntu, or macOS, it already includes certificates for trusted Certificate Authorities.

These certificates contain the CAs' public keys. Some browsers also maintain their own trusted CA lists.

Now, when you open https://example.com, the website sends its TLS certificate to your browser.

For example:

Website Certificate
---------------------------
Domain:       example.com
Public Key:   WEBSITE_PUBLIC_KEY
Issued By:    Example CA
CA Signature: SIGNATURE_123
Enter fullscreen mode Exit fullscreen mode

The server normally also sends the intermediate certificates needed for verification.

Your browser performs a few checks.

1. Check the domain name

You opened https://example.com.

The browser checks whether the certificate is valid for example.com.

If the certificate belongs to another website, the check fails.

2. Check the expiration date

Every certificate has a start date and an expiration date.

The browser compares these dates with the current date.

If the certificate has expired or isn't valid yet, the check fails.

3. Verify the CA's signature

The certificate says it was issued by Example CA.

But anyone could create a certificate and write "Example CA" on it.

So the browser needs to verify the signature instead of simply trusting the name.

Remember, when the CA issued the certificate, it created a signature using its private key.

Now the browser has three things:

  • The certificate information, including example.com and WEBSITE_PUBLIC_KEY.
  • The CA's digital signature included in the certificate.
  • The issuing CA's public key, obtained from its certificate.

The browser uses these three values in a mathematical signature-verification algorithm.

Certificate Information
          +
CA Digital Signature
          +
CA Public Key
          |
          v
Mathematical Verification
          |
          v
     VALID / INVALID
Enter fullscreen mode Exit fullscreen mode

The public and private keys are mathematically related.

The CA's private key was used to create the signature, and its corresponding public key is used to verify it.

If someone changes the domain, public key, or other signed information inside the certificate, the signature verification fails.

In actual HTTPS, certificates are often issued by intermediate CAs.

So the browser checks the signatures in the certificate chain until it reaches a root CA that the browser or operating system already trusts.

After these checks, the browser can trust that the website's public key is certified for example.com.

Step 3: How Does the Server Prove It Has the Private Key?

We have verified the certificate, but there's still one problem.

Anyone can copy a website's certificate because certificates contain public information.

A hacker could copy the genuine certificate from example.com and send the same certificate to your browser.

The certificate would still pass its validity checks.

So the browser also needs to verify whether the connected server actually possesses the private key matching the website's public key.

Remember, the website has:

Website Public Key:  WEBSITE_PUBLIC_KEY
Website Private Key: WEBSITE_PRIVATE_KEY
Enter fullscreen mode Exit fullscreen mode

During the TLS handshake, the browser and server exchange information to establish their shared secret.

The server takes information from this handshake and uses its private key to calculate another digital signature.

TLS Handshake Information
          +
Website Private Key
          |
          v
Digital Signature Algorithm
          |
          v
       Signature
Enter fullscreen mode Exit fullscreen mode

The server sends this calculated signature to the browser.

It never sends its private key.

Now the browser has:

  • The website's public key from the verified certificate.
  • The handshake information exchanged earlier.
  • The digital signature sent by the server.

It uses these values in another mathematical verification algorithm.

Website Public Key
          +
Handshake Information
          +
Server's Signature
          |
          v
Mathematical Verification
          |
          v
     VALID / INVALID
Enter fullscreen mode Exit fullscreen mode

If the signature is valid, it proves that the server possesses the private key corresponding to the public key in the certificate.

A hacker who only copied the certificate cannot create a valid signature for the current handshake without the matching private key.

Why Do We Need Both Verifications?

The browser performs two different checks:

First: It verifies the CA's signature using the CA's public key. This confirms that a trusted CA certified the website's public key for example.com.

Second: It verifies the server's handshake signature using the website's public key. This proves that the server possesses the corresponding private key.

So, the browser doesn't need to know either private key. It uses mathematical verification to check the signatures.

Together, these checks help the browser confirm that it's communicating with the correct website rather than someone pretending to be it.

How Does TLS Encrypt HTTP Requests and Responses?

We now know two things:

  • The browser and server can establish the same shared secret using Diffie–Hellman.
  • The browser can verify the server's identity using TLS certificates.

Now, how is that shared secret used to encrypt our actual HTTP requests?

Step 1: Generate Encryption Keys

Remember, both the browser and server have calculated the same shared secret.

But TLS doesn't use that shared secret directly to encrypt HTTP data.

Instead, it uses a mathematical function called HKDF (HMAC-based Key Derivation Function) to generate encryption keys.

Both the browser and server use the shared secret and the same TLS handshake information to calculate these keys independently.

TLS creates separate keys for sending data in each direction.

For example:

  • Key A: Used for communication from the browser to the server.
  • Key B: Used for communication from the server to the browser.

Both computers have the keys needed for both directions.

These keys are never sent directly over the internet.

Step 2: Browser Encrypts the HTTP Request

Now you log in to https://example.com.

Your browser prepares an HTTP request containing your login information.

Before sending it, TLS encrypts the request using Key A.

For encryption, TLS uses an algorithm such as AES-GCM.

The algorithm takes the original information and the encryption key and converts the data into an unreadable form.

So, someone intercepting the request cannot read your username or password.

Step 3: Server Decrypts the Request

When the encrypted data reaches the server, TLS uses the corresponding Key A to decrypt it.

The server gets back the original HTTP request and processes it normally.

Step 4: Server Encrypts the Response

After processing the request, the server prepares its HTTP response.

This time, the server uses Key B to encrypt the response.

The encrypted response travels back to your browser.

Your browser uses the corresponding Key B to decrypt it and read the response.

So, the browser and server can exchange information securely without sending their encryption keys over the internet.

What If Someone Changes the Encrypted Data?

We know that encryption prevents others from reading our information.

But what if a hacker intercepts an encrypted request and changes some of its data before it reaches the server?

For example, you're transferring ₹1,000 to Rahul through a banking website.

What if someone tries to change the request so the server processes ₹10,000 instead?

Apart from encrypting the request, TLS also needs a way to detect such changes.

For this, AES-GCM generates something called an authentication tag.

Think of it like a secret code calculated using the encryption key and the data being protected.

The browser sends both the encrypted request and its authentication tag.

When the server receives them, it uses the same secret key to verify the tag.

If someone has changed the encrypted data, the verification fails, and TLS rejects it.

A hacker who doesn't know the secret key cannot normally create a valid tag for the modified data.

This protection is called integrity.

So, TLS protects our information from being read and also helps detect if someone tries to change it while it's travelling.

Putting Everything Together: How Does the TLS Handshake Work?

We've learned how encryption keys, Diffie–Hellman, certificates, and digital signatures work.

Now let's see how all these things are used when you open https://example.com.

We'll use TLS 1.3, which is a modern version of TLS.

Step 1: Browser Sends ClientHello

Your browser starts by sending a message called ClientHello.

This message contains:

  • The TLS versions your browser supports.
  • The encryption algorithms it supports, such as AES-GCM.
  • The browser's public value generated for the Diffie–Hellman key exchange.
  • The domain name it wants to connect to, such as example.com.

Remember our Diffie–Hellman example?

The browser generates a private number and calculates a public value.

It shares only the public value, not the private number.

Step 2: Server Sends ServerHello

The server responds with ServerHello.

It selects a TLS version and encryption algorithm that both sides support.

It also sends its own Diffie–Hellman public value.

Now both the browser and server have the information needed to calculate the same shared secret independently.

They use this shared secret to derive keys that protect the remaining handshake messages.

Step 3: Server Sends Its TLS Certificate

Next, the server sends its TLS certificate.

The browser checks whether:

  • The certificate is valid for example.com.
  • The certificate is within its validity period.
  • The certificate's signatures can be verified through a trusted Certificate Authority.

The server also sends a digital signature created using its certificate's private key.

The browser verifies that signature using the website's public key from the certificate.

This helps confirm that the server possesses the correct private key and isn't simply presenting a copied certificate.

In TLS 1.3, these certificate and verification messages are already encrypted using the handshake keys established earlier.

Step 4: Both Complete the Handshake

The server and browser exchange messages called Finished.

These messages help verify that the handshake information hasn't been changed and that both sides have established the expected keys.

If the checks succeed, the handshake is complete.

Both sides also derive the encryption keys needed for HTTP communication.

Step 5: Encrypted HTTP Communication Begins

Now your browser can send its HTTP requests.

For example, when you log in:

  1. Your browser encrypts the login request using the client traffic key.
  2. The server decrypts it using the corresponding key.
  3. The server processes the request and encrypts its response using the server traffic key.
  4. Your browser decrypts the response.

TLS also checks the authentication tags to detect changes to the protected data.

So, the TLS handshake happens before normal encrypted HTTP communication begins, allowing the browser and server to establish the keys and verify the connection.

All these steps normally happen automatically, without us noticing them while browsing a website.

Conclusion

We use HTTPS almost every day, but there's a lot happening behind that small S.

From establishing shared secrets using Diffie–Hellman to verifying certificates and encrypting HTTP requests, TLS handles all these steps before we can securely exchange information with a website.

And the best part? All of this happens automatically, usually within a fraction of a second.

The next time you see https:// in your browser, you'll have a better idea of what's happening behind it! 🔐

Top comments (0)