Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 74

TLS/SSL: Securing Data in Transit

Recommended resources

Hello! Welcome back to our course on Backend Engineering.

In our previous lesson, we moved from theory to practice by implementing URL-based versioning for a DRF API. You learned how to use NamespaceVersioning to create a clean, scalable structure that allows your API to evolve without breaking existing client integrations. We saw how a few lines of configuration in settings.py and urls.py, combined with version-aware logic in your views, can manage multiple API versions simultaneously.

So far in this module, we've focused on securing the logic of our API. Today, we shift our attention to securing the data itself as it travels across the network. Our learning outcome is to explain the role of TLS/SSL in providing encryption and integrity for data in transit. This is the foundational technology that powers HTTPS and is a non-negotiable component of modern web security.

Given your interest in understanding the "under the hood" mechanisms, we'll go beyond just defining TLS and explore the cryptographic processes that make it work.

The Problem: Communication in the Open

Imagine your backend API is communicating with a client over a public network like the internet. Without any protection, this communication is like sending postcards through the mail. Anyone who can intercept the mail can read the contents, alter them, or even replace them with fakes.

In technical terms, an unprotected HTTP connection is vulnerable to:

  • Eavesdropping: An attacker can intercept and read sensitive data, such as user credentials, personal information, or API keys. This violates confidentiality.
  • Tampering: An attacker can modify the data in transit. For example, they could change the amount in a financial transaction or alter a command sent to your API. This violates data integrity.
  • Impersonation (Man-in-the-Middle): An attacker can pretend to be your server, tricking the client into sending data to the wrong destination. This violates authentication.

Transport Layer Security (TLS), the modern successor to Secure Sockets Layer (SSL), is the protocol designed to solve these three problems.


The Three Pillars of TLS

TLS establishes a secure channel between two communicating parties by providing three core security services.

To begin, let's get a formal definition of these three properties. The following article from IBM provides a concise overview.

What Is Transport Layer Security (TLS)?

This article from IBM Think clearly defines TLS and the three core security properties it provides, which form the foundation of our lesson.

Please read the introduction 'What is Transport Layer Security (TLS)?' and the short section that begins 'TLS ensures secure Internet communication by enforcing three core properties...'. Focus on understanding the definitions of Authentication, Confidentiality, and Integrity.

To summarize, the three pillars are:

  1. Authentication: The server proves its identity to the client. This ensures you are talking to the legitimate server (e.g., yourbank.com) and not an imposter.
  2. Confidentiality (Encryption): Once the connection is established, all data exchanged is encrypted. An eavesdropper who intercepts the communication sees only unintelligible ciphertext, not the actual data.
  3. Integrity: The protocol ensures that the data sent is exactly the data received. Any modification or tampering with the data during transit is detected, and the connection is aborted.

How TLS Works: The Cryptographic Toolkit

TLS cleverly combines two different types of cryptography to achieve its goals efficiently:

  • Asymmetric Cryptography (Public-Key Cryptography): This involves a pair of keys: a public key that can be shared with anyone and a private key that is kept secret. Data encrypted with the public key can only be decrypted with the corresponding private key. This is fantastic for authentication and for securely sharing secrets, but it's computationally expensive and too slow for encrypting large amounts of data.
  • Symmetric Cryptography (Secret-Key Cryptography): This uses a single, shared secret key for both encryption and decryption. It is extremely fast and efficient, making it ideal for encrypting the bulk of communication data. The main challenge is: how do two parties, who have never met, securely agree on a secret key in the first place?

TLS uses a hybrid approach: it uses the slow, secure asymmetric cryptography during an initial "handshake" to authenticate the server and securely establish a shared symmetric key. Then, it switches to the fast symmetric cryptography to encrypt all subsequent data for the rest of the session.


The TLS Handshake: Building the Secure Tunnel

The TLS handshake is the multi-step process at the beginning of a TLS session where the client and server establish the secure channel. This is where the asymmetric cryptography does its work.

The following video from Computerphile provides an excellent, technically-grounded walkthrough of the TLS 1.2 handshake. The narrator explains the purpose of each message exchanged between the client and server.

TLS Handshake Explained - Computerphile

Now for the 'under the hood' details. This video gives a step-by-step walkthrough of the TLS 1.2 handshake, which is a great version to start with for understanding the core mechanics.

Watch the video from the beginning until 13:29. Pay close attention to how the 'cipher suite' dictates the process, and how the client and server exchange messages to authenticate each other and generate a shared secret key.

Here is a simplified summary of the key steps you saw in the video:

  1. ClientHello: The client initiates, sending its supported TLS versions and a list of cipher suites (the cryptographic algorithms it knows) to the server.
  2. ServerHello: The server picks the best TLS version and cipher suite they both support. It then sends back its TLS certificate.
  3. Authentication: The certificate contains the server's public key and is digitally signed by a trusted Certificate Authority (CA). The client verifies this certificate, confirming the server is who it claims to be. This achieves the authentication goal.
  4. Key Exchange: Using the server's public key and a key exchange algorithm (like Diffie-Hellman, as mentioned in the video), the client and server securely generate a shared secret key (the symmetric key) for the session. This is done in such a way that both sides can calculate the same key, but an eavesdropper cannot.
  5. Finished: Both sides send a final Finished message, encrypted with the new shared key, to confirm that the handshake was successful and was not tampered with.

From this point on, the handshake is over, and the fast, symmetric encryption takes over for all application data (i.e., the HTTP requests and responses).

The image below compares the handshake process for TLS 1.2 (which you just learned about) and the more modern TLS 1.3. Notice how TLS 1.3 is more efficient, requiring fewer round trips to establish the secure session.

Comparison of TLS 1.2 and TLS 1.3 Full Handshakes
This diagram illustrates the message flow for a full TLS 1.2 handshake compared to the more streamlined TLS 1.3 handshake. The reduced number of round-trips in TLS 1.3 significantly decreases connection latency.

Encryption and Integrity in Action

Now that the handshake is complete and a shared secret key exists, how are confidentiality and integrity maintained?

  • Confidentiality (Encryption): All data sent between the client and server—the HTTP headers, body, everything—is encrypted using the shared symmetric key and an agreed-upon algorithm like AES (Advanced Encryption Standard). If an attacker intercepts this data, they will only see random-looking bytes.

  • Integrity (Message Authentication): To prevent tampering, the sender computes a Message Authentication Code (MAC) for each message. A MAC is a cryptographic checksum created by combining the message content with the shared secret key. This MAC is sent along with the encrypted message. The receiver performs the same calculation on the received message. If the calculated MAC matches the one that was sent, the data's integrity is verified. If they don't match, it means the data was altered in transit, and the connection is immediately terminated.

This next short video clip focuses specifically on these two aspects.

How TLS Works?

The Computerphile video focused heavily on authentication and key exchange. To round out our understanding, let's focus on how TLS specifically guarantees encryption and data integrity.

Watch the section from 06:51 to 09:24. This segment explains how TLS uses fast symmetric encryption for the data itself, and how it uses Message Authentication Codes (MACs) to ensure data hasn't been tampered with.

Test your understanding!

Based on what you've learned about their different properties, why doesn't a TLS session use asymmetric (public-key) encryption for all data transmission? Conversely, why can't it just use symmetric (secret-key) encryption from the very beginning?

Show answer
  • Asymmetric encryption is not used for everything because it is too slow. The mathematical operations involved are computationally intensive, and using it to encrypt all the data in a session would add significant latency and server load. Its value is in securely establishing trust and sharing a key, not in bulk encryption.

  • Symmetric encryption cannot be used from the beginning because there is no shared secret key yet. The core problem is securely distributing this key. If you sent the secret key over an insecure channel, an attacker could intercept it, and the entire encryption scheme would be compromised. The TLS handshake, using asymmetric cryptography, is the mechanism to solve this key distribution problem.


Conclusion

You now understand the critical role TLS plays in securing modern web communications. It is not just one thing, but a sophisticated protocol that combines different cryptographic techniques to provide a trifecta of security guarantees for data in transit.

Key Takeaways:

  • TLS secures data in transit by providing Authentication, Confidentiality (Encryption), and Integrity.
  • It uses a hybrid cryptographic model: slow asymmetric cryptography for the initial handshake to authenticate the server and agree on a shared key, and fast symmetric cryptography for encrypting the actual data.
  • Data integrity is ensured using Message Authentication Codes (MACs), which act as a tamper-proof seal on every message.
  • HTTPS is simply the application of the TLS protocol to HTTP traffic.

Preview of the Next Lesson:
While TLS does an incredible job of protecting data in transit, it doesn't protect your application from malicious data once it arrives. An attacker can still send perfectly encrypted, validly-formed HTTP requests that are designed to exploit vulnerabilities in your application code. In our next lesson, we will begin exploring these threats as we explain common web vulnerabilities (SQL Injection, XSS, CSRF) and how Django's built-in features help mitigate them.