Demystifying TLS/SSL

Exploring the Depths of TLS/SSL for a Secure Online Experience

Chirag Jain
Engineering at Bajaj Health
12 min readApr 23, 2024

--

Ever clicked “ignore” on a security warning just to get your internet working?

Chrome browser warning about an unsecure webpage

We’ve all been there!! Wether navigating through secure connections for everyday browsing or battling certificate verification issues in local development, the intricacies of TLS often remain hidden beneath the surface.

But what exactly is going on behind the scenes when websites say they’re “secure”?

For a lot of people (even developers!!), the difference between HTTP & HTTPS is just of an ‘S’. But believe me this S’ can literally cost you millions of dollars, if taken for-granted !!

In this article we’ll dive deep into the world of TLS & SSL, understanding how exactly this invisible security guard keeps our online information safe. So, Let’s just embark on a journey to unveil the magic behind TLS and gain a deeper understanding of the invisible shield protecting our digital world.

Understanding HTTP: The Foundation of Web Communication

Before diving into the complexities of TLS/SSL, it’s crucial to understand the underlying protocol it secures: HTTP (Hypertext Transfer Protocol). HTTP acts as the cornerstone of web communication, facilitating the exchange of information between web browsers and servers. It operates like a client-server model:

  • Clients (e.g., web browsers) initiate requests for specific resources, like web pages or images, identified by Uniform Resource Locators (URLs).
  • Servers (e.g., web servers) receive these requests, process them, and send back responses containing the requested resources or error messages.

This fundamental understanding of HTTP lays the groundwork for exploring how TLS/SSL encrypts communication within the HTTP protocol, safeguarding sensitive data and ensuring a secure online experience.

HTTP vs HTTPS: HTTP is a protocol or set of communication rules for client-server communication over any network. HTTPS is the practice of establishing a secure SSL/TLS protocol on an insecure HTTP connection.

HTTP Communication over the underlying TCP Connection

Unmasking Malicious Intermediaries: The Man-in-the-Middle Attack

While HTTP provides the foundation for web communication, it lacks inherent security measures. This vulnerability opens the door for a nefarious practice called a Man-in-the-Middle (MitM) attack.

In a MitM attack, a malicious actor secretly positions themselves between a client and a server, intercepting and potentially altering the communication flowing between them. Imagine an eavesdropper on a phone line, but in the digital world.

Every request sent from your device, travels through a lots of devices like, routers, hubs etc, before reaching to the actual server.
Imagine: You are connected to a public Wifi, every data packet you send travels through that Wifi Router, and If an attacker hacks that Wifi Router, he can access all of your data packets, thus stealing all the sensitive information.

This attack can be used to steal sensitive information like login credentials, credit card details, or even personal messages. Understanding MitM attacks highlights the critical need for additional security protocols like TLS/SSL, which are designed to prevent such malicious activities and ensure the integrity and confidentiality of data transmitted over the internet.

Man-in-the-Middle Attack (MitM)

Fortifying Web Connections: Understanding SSL/TLS

In the digital world, any communication over the web carries the risk of being intercepted, as discussed above. To combat this vulnerability, we need a way to somehow communicate in a “code language” that only the Client (your device) and the Server (the website) can understand. This code must be mutually agreed upon, fast, and reliable to ensure both privacy and a seamless user experience.

This is where SSL or TLS comes into the Picture!!

What is SSL?

Secure Sockets Layer (SSL) is a communication protocol, or set of rules, that creates a secure connection between two devices or applications on a network. It’s important to establish trust and authenticate the other party before you share credentials or data over the internet.

It was the first widely adopted cryptographic protocol used to secure data transmission over the internet, developed by Netscape in the 1990s. SSL created a secure and encrypted communication channel between a client and a server, ensuring that the data transmitted between the two parties remains confidential and integral.

However, SSL is an older technology that contains some serious security flaws.

What is TLS?

Transport Layer Security (TLS) is the upgraded version of SSL that fixes existing SSL vulnerabilities. TLS authenticates more efficiently and continues to support encrypted communication channels.

Both SSL and TLS are communication protocols that encrypt data between servers, applications, users, and systems. They authenticate two parties connected over a network so they can exchange data securely.

TLS is the direct successor to SSL, and all versions of SSL, TLS 1.0 & TLS 1.1 are now deprecated. However, it’s common to find the term SSL describing a TLS connection. In most cases, the terms SSL and SSL/TLS both refer to the TLS protocol and TLS certificates.

Working of TLS: Encryption & Authentication

As we’ve established, TLS serves two crucial purposes:

  1. Confidentiality: Encrypting the entire client-server communication to render it unreadable even if intercepted by malicious actors.
  2. Authentication: Verifying the server’s identity, ensuring the user interacts with the intended server and not an imposter.

To delve into these processes, let’s explore the fundamental concepts of symmetric and asymmetric keys, which are the building blocks of TLS’s cryptographic magic.

The Cornerstones of Encryption: Symmetric and Asymmetric Keys

The foundation of any encryption algorithm lies in keys, which act as the digital locks and unlocks for securing data. In the context of TLS, the client and server use these keys to encrypt and decrypt communication.

There are two primary types of encryption algorithms based on the key usage:

1. Symmetric Key Encryption:

  • Shared Secret: This approach utilizes a single symmetric key shared by both the client and server.
  • Simple and Efficient: Encryption and decryption are performed using the same key, making it a relatively fast and efficient process. However, securely sharing the key beforehand is crucial for its effectiveness.
  • Limited Applicability: While efficient, symmetric key encryption becomes impractical when multiple parties need to communicate securely, as securely distributing the same key to everyone becomes challenging.
Symmetric Key Encryption

2. Asymmetric Key Encryption:

  • Key Pair: Unlike symmetric keys, this method employs a key pair, consisting of a public key and a private key. The public key is widely distributed, while the private key is kept confidential by the server.
  • Enhanced Security: Data encrypted with the public key can only be decrypted using the corresponding private key, offering a more secure approach compared to sharing a single key.
  • Slower Processing: However, asymmetric key encryption generally involves complex mathematical operations, making it computationally slower than symmetric encryption.
Asymmetric Key Encryption

Understanding these key types is essential for grasping the inner workings of TLS, which leverages both symmetric and asymmetric keys to establish a secure communication channel.

The TLS Dance: Striking a Balance between Security and Performance

Having explored symmetric and asymmetric key encryption, one might be tempted to simply choose symmetric encryption due to its speed and efficiency:

  • Symmetric Encryption, The Alluring Choice: It seems straightforward — generate a shared key, encrypt the communication with it, and send both the key and encrypted data to the server. This approach offers undeniable advantages in terms of speed and computational efficiency.

However, this seemingly simple solution introduces a critical vulnerability:

  • The Achilles’ Heel of Shared Keys: Sharing the key over an insecure channel like the internet exposes it to potential interception. If an attacker acquires the key, they can decrypt all subsequent communication, rendering the entire encryption process futile.

On the other hand, relying solely on asymmetric encryption for the entire communication isn’t ideal either:

  • Asymmetric Encryption, The Secure, Yet Sluggish Option: While it offers enhanced security by using separate public and private keys, encrypting and decrypting large amounts of data with asymmetric algorithms is computationally expensive, leading to significant performance drawbacks.

TLS takes a clever approach, leveraging the strengths of both key types in a two-phase process:

1. TLS Handshake (Asymmetric Encryption with RSA Algorithm):

  • Server Key Generation: The server generates a key pair, consisting of a private key (kept secret) and a public key (widely distributed). This key pair acts like a digital lock and key, where data encrypted with the public key can only be decrypted by the corresponding private key.

Beyond ensuring secure communication, to verify the server’s identity, a trusted authority (CA) issues a signed certificate containing the server’s public key. This digitally signed passport is then shared by server instead of the plain public key, which assures the client they’re connecting to the legitimate server, not an imposter, safeguarding against man-in-the-middle attacks.

Flow of Issuance of CA signed TLS/SSL Certificate
  • Client Generates Symmetric Key: The client generates a temporary symmetric key to be used for encrypting the actual communication data.
  • Secure Key Delivery: The client encrypts the symmetric key with the server’s public key. This ensures only the server, with its corresponding private key, can decrypt the message and obtain the symmetric key.

Key aspects to remember:

  • This process is analogous to securely sending a physical key through an untrusted mail system. Even if someone intercepts the package (encrypted message), they cannot open it (decrypt the message) without the server’s private key.
  • Change Cipher Spec (CCS): After sending the encrypted symmetric key, the client sends a Change Cipher Spec message, indicating the transition to using the symmetric key for subsequent communication.

2. Secure Communication (Symmetric Encryption):

Once the symmetric key is securely exchanged, both parties use it to encrypt and decrypt the actual communication data. This provides the desired balance between security and performance:

  • Security: The data remains confidential even if intercepted, as only the parties possessing the shared symmetric key can decrypt it.
  • Performance: Symmetric encryption is computationally efficient, making data transfer faster compared to using asymmetric encryption for the entire communication.

In summary, the TLS handshake orchestrates a secure exchange of a symmetric key using asymmetric encryption. This key then unlocks the door for efficient and confidential communication using symmetric encryption.

HTTPS Communication over TLS 1.2 with RSA Algorithm

Forward Secrecy Attacks

While TLS 1.2 with the RSA algorithm offers a robust security solution, it has a critical vulnerability known as a Forward Secrecy Attack. This attack exploits the fact that the same private key is used for multiple TLS sessions.

The Attack Scenario:

  1. Passive Recording: An attacker intercepts and records encrypted communication over an extended period. This communication remains undecipherable due to the encryption.
  2. Compromised Private Key: If the attacker subsequently gains access to the server’s private key, through methods like software vulnerabilities or social engineering, they can decrypt all the previously recorded encrypted data.

This once actually happened, when due to some bug in OpenSSL (the library which provides implementation of RSA algorithm), people were able to leak the part of memory where private key was loaded into the server!!

Impact and Mitigations:

This scenario highlights the significance of Forward Secrecy, a property that ensures past communication remains secure even if the server’s private key is compromised. Unfortunately, RSA alone doesn’t offer forward secrecy.

Addressing the Issue:

  • Key Rotation: While not a foolproof solution, regularly rotating the server’s private key, as suggested, can mitigate the potential damage in case of a key compromise by limiting the amount of vulnerable data. However, frequent key rotation introduces its own set of challenges, such as increased certificate management overhead.
  • More Robust Key Exchange Algorithm: Fortunately, there are more robust mechanisms of asymmetric key exchange algorithm like Diffie-Hellman key exchange to generate unique session keys for each connection, rendering any captured communication useless even if the private key is compromised later.

Wonders of Mathematics: Meet Diffie-Hellman Algorithm

The Diffie-Hellman key exchange algorithm is a fascinating example of how mathematics can be used to achieve seemingly impossible feats in the realm of cryptography. Often described as “magic math,” it allows two parties to establish a shared secret key for secure communication without ever revealing their private information to each other, even over an insecure public network.

Here’s a breakdown of this mathematical marvel:

1. Preparation:

Both the client and server possess private keys (large numbers denoted by x and y, respectively) that they keep secret. Additionally, they agree on publicly known values: g (a recommended prime number) and n (a large prime number).

All the Different Keys Required to form a Symmetric Key

2. Client Initiates:

The client sends two publicly visible pieces of information to the server:

  • The public values g and n.
  • The result of a mathematical operation involving their private key x and the public values: g raised to the power of x and then modulo n (denoted as g^x % n). This calculation cleverly conceals the value of x, even if someone knows g and n.

3. Server Responds:

The server, upon receiving the client’s message, performs a similar calculation using its own private key y:

  • The server then sends its own encrypted message back to the client: g raised to the power of its private key y and modulo n, i.e, (g^y % n).

4. Shared Secret Key Revealed:

Now, the magic happens! Both the client and the server perform the final step independently:

  • They each raise the received message from the other party to the power of their respective private key.
  • The client raises the server’s message (g^y % n) to the power of its private key x, resulting in (g^y % n)^x.
  • The server raises the client’s message (g^x % n) to the power of its private key y, resulting in (g^x % n)^y.

Remarkably, due to the mathematical properties of modular exponentiation, both calculations by the client and server will yield the same secret key: g^(xy) % n. This key, derived from a combination of their private keys but never directly revealing them, is used to securely encrypt and decrypt their communication, ensuring confidentiality even if an eavesdropper intercepts the messages.

HTTPS Communication over TLS 1.2 with Diffie-Hellman Algorithm

TLS 1.3 & 0-RTT Resumption: Optimizing Secure Connections

While TLS (Transport Layer Security) is the cornerstone of online security, its initial handshake process can introduce a slight delay in establishing secure communication. TLS 1.3, the latest version of the protocol, introduced a feature called 0-RTT (Zero Round-Trip Time) Resumption to address this issue and optimize performance for returning users.

Understanding the Challenge:

During a typical TLS handshake, the client and server engage in an initial exchange of messages to establish the encryption parameters and verify each other’s identities. This process, although crucial for security, can add some latency to the connection setup.

0-RTT Resumption to the Rescue:

TLS 1.3 with 0-RTT Resumption leverages the benefits of previous connections to streamline the handshake for returning users:

  1. Session Establishment: During the initial connection, the client and server establish a shared session and generate a secret key (called a resumption secret). This secret key is securely stored by both parties.
  2. Subsequent Visits: When a user revisits the same website, the client can include the resumption secret and other relevant information in its initial “Client Hello” message during the handshake.
  3. Server Verification: The server receives the client’s message and, if it recognizes the valid resumption secret and associated session details, it can skip most of the handshake steps.
  4. Faster Encryption: With the key already established and the handshake streamlined, the server can immediately respond with the encrypted data, significantly reducing the initial latency.
TLS 1.3 with 0-RTT

Benefits of 0-RTT Resumption:

  • Improved User Experience: Faster connection establishment leads to a smoother and more responsive user experience for returning visitors.
  • Reduced Server Load: By streamlining the handshake, 0-RTT reduces the processing burden on the server, especially for websites with high traffic.
  • Enhanced Efficiency: It optimizes resource utilization and improves overall network performance.

Security Considerations:

While 0-RTT offers performance benefits, it’s crucial to remember that security remains paramount:

  • Limited Data: Only a limited amount of initial data can be sent by the client using 0-RTT to mitigate potential security risks associated with potential replay attacks.
  • Server Discretion: The server ultimately decides whether to accept or reject the 0-RTT request based on its security policies and risk assessment.

Conclusion

The journey through the world of TLS has hopefully shed light on the crucial role it plays in securing our online communication. From understanding the vulnerabilities it addresses to appreciating the intricate dance of the handshake and the innovative optimizations like 0-RTT Resumption, we’ve explored how TLS safeguards our data and ensures trust in the digital world.

Remember, as technology evolves, so do the threats it faces. Staying informed about these advancements and potential risks empowers us to make informed choices and navigate the online landscape with confidence. By embracing secure protocols like TLS and understanding their functionalities, we can contribute to a safer and more trustworthy internet experience for everyone.

Let’s keep building a Safe and Secure Web, One Encrypted Connection at a Time.

--

--