How exactly HTTPS (ssl) works

SslHttps

Ssl Problem Overview


I have been reading on HTTPS, trying to figure out how exactly it works. To me it doesn't seem to make sense, for example, I was reading this

https://ssl.trustwave.com/support/support-how-ssl-works.php

And notice it says this in the page

> Step 4: xyz.com will next create a > unique hash and encrypt it using both > the customer's public key and > xyz.com's private key, and send this > back to the client. > > Step 5: Customer's browser will decrypt the hash. This process shows > that the xyz.com sent the hash and > only the customer is able to read it.

What I don't understand is, couldn't a hacker just intercept the public key it sends back to the "customer's browser", and be able to decrypt anything the customer can?

Thanks for any response

Ssl Solutions


Solution 1 - Ssl

Why is HTTPS required?

To verify whether the website is authenticated/certified or not (uncertified websites can do evil things). An authenticated website has a unique personal certificate purchased from one of the CA’s.

Who are CA’s (Certificate Authorities)?

CA’s are globally trusted companies like GoDaddy, GeoTrust, VeriSign etc who provide digital certificates to the websites.

What are public keys and private keys?

Keys are nothing but long random numbers used to encrypt/decrypt data.
Public keys are keys which can be shared with others. Private keys are meant to be kept private.

Suppose Jerry generates a private key and public key. He makes many copies of that public key and shares with others.
Now, others can only encrypt the data using the public key and that data can only be decrypted by the private key of Jerry.
Another approach is to use public keys to only decrypt the data and private keys to only encrypt the data.

How does a company get a certificate?

Website owner first generates a public key and private key, keeping the private key secret. He gives a Certificate Signing Request file (CSR) and his public key to the CA.
CA then creates a personal certificate based on CSR including domain name, owner name, expiry date, serial no. etc and also adds an encrypted text (= digital signature) to the certificate and finally encrypts the whole certificate with the public key of the server and sends it back to the website owner.
This certificate is then decrypted with the private key of the website owner and finally, he installs it on the website.

> Note: That encrypted text is the digital signature of the CA. That > text is encrypted by the private key of the CA and can only be > decrypted by a public key of CA.
> When you install your operating > system or Browser, root-certificates from many trusted CA's like > GeoTrust, VeriSign, GoDaddy etc. come with it. These root-certificates > contain the public key of that CA provider which helps decrypt the > signature.


HTTPS security can be split into 2 parts (Handshakes):

  1. To validate the certificate of a website:

enter image description here

  1. When you enter the URL www.Google.com, Google’s server gives its public key and certificate (which was signed by GeoTrust) to the Browser.

  2. Now browser has to verify the authenticity of the certificate i.e. it’s actually signed from GeoTrust or not. As browsers come with a pre-installed list of public keys from all the major CA’s, it picks the public key of the GeoTrust and tries to decrypt the digital signature of the certificate which was encrypted by the private key of GeoTrust.

  3. If it’s able to decrypt the signature (which means it’s a trustworthy website) then it proceeds to the next step else it stops and shows a red cross before the URL.

  1. To create a secure connection (encrypts outgoing and incoming data) so that no one else can read it:

enter image description here

  1. As I mentioned, Google sends its public key when you enter www.Google.com . Any data encrypted with this public key can only be decrypted by Google’s private key which Google doesn’t share with anyone.

  2. After validating the certificate, browser creates a new key let’s call it Session Key and make 2 copies of it. These keys can encrypt as well as decrypt the data.

  3. The browser then encrypts (1 copy of session key + other request data) with the Google's public key . Then it sends it back to the Google server.

  4. Google’s server decrypts the encrypted data using its private key and gets the session key , and other request data.

> Now, see, server and browser both have got the same copies of session > key of the browser. No one else has this key, therefore, only server > and browser can encrypt and decrypt the data. This key will now be > used for both to decrypt and to encrypt the data.

  1. When Google sends the data like requested HTML document and other HTTP data to the browser it first encrypts the data with this session key and browser decrypts the data with the other copy of the session key.

  2. Similarly, when browser sends the data to the Google server it encrypts it with the session key which server decrypts on the other side.

> Note: This session key is only used for that session only. If the user > closes the website and opens again, a new session key would be > created.


can't get enough of web? behind the scenes when u type www.google.com in browser

Solution 2 - Ssl

> What I don't understand is, couldn't a hacker just intercept the public key it sends back to the "customer's browser", and be able to decrypt anything the customer can.

First, understand that there are generally two steps of HTTPs communication.

  1. Generate a shared symmetric key which can only be known between client and server, no one else knows it

  2. With this shared symmetric key, client and server is able to safely communicate with each other without worrying about information being intercepted and decrypted by others.

So the question becomes, how can the client and server generate a secret shared key without being known by others in this open internet? This is the asymmetric algorithm coming to play, a demo flow is like below:

-- Client receives public key from server.

-- Client generates a key string "DummySharedKey" which will be later used as shared key, and encrypt it into "7$&^^%####LDD!!@" with server's public key, Man-in-the-middle may have the public key and might be able to intercept the encrypted data, but the data is useless to him as the data can only be decrypted by sever's private key.

-- Server receives the encrypted key string "7$&^^%####LDD!!@", and decrypt it into "DummySharedKey" with its private key

Above key exchange steps makes sure that only Client and Server can know the shared key is "DummySharedKey", no one else knows it.

So it's critical to understand that it is Client's responsibility to generate the shared key, NOT SERVER! (i think this is what confused you)

I also recommend you to take a look at this video which explains HTTPs very well. https://www.youtube.com/watch?v=JCvPnwpWVUQ&list=FLt15cp4Q09BpKVUryBOg2hQ&index=3

Solution 3 - Ssl

> What I don't understand is, couldn't a hacker just intercept the public key it sends back to the "customer's browser", and be able to decrypt anything the customer can.

Public/private key encryption is based on modulo arithmetics using prime numbers.

Such asymmetric encryption was only discovered in the mid-1970s. It is credited to Diffie and Hellman, and to Rivest, Shamir and Adleman. (Though, both actually rediscovered things already known by the British secret services.)

The wikipedia page on Diffie-Hellman has a detailed example of a secret key exchange through a public channel. While it does not describe SSL itself, it should be handy to make sense of why knowing a public key doesn't reveal the contents of a message.

You might also find this simple RSA example interesting.

Solution 4 - Ssl

I'm studying related topics and read several blogs like how-https-works and how-does-https-work-rsa-encryption-explained/.

I have summarized a sequence diagram based on my study and hope it can be helpful to someone who comes to this thread.

enter image description here

For more details, you can check the blog mentioned.

Solution 5 - Ssl

I have written a small blog post around SSL Handshake between the server/client. Please feel free to take a look.

SSL Handshake

A small snippet from the same is as follows:

"Client makes a request to the server over HTTPS. Server sends a copy of its SSL certificate + public key. After verifying the identity of the server with its local trusted CA store, client generates a secret session key, encrypts it using the server's public key and sends it. Server decrypts the secret session key using its private key and sends an acknowledgment to the client. Secure channel established."

It also describes the symmetric/asymmetric encryption which is used for SSL certificates and data transfer once secure transport is established.

Solution 6 - Ssl

For detailed explanation of SSL, see https://www.ietf.org/rfc/rfc2246.txt

Here are the brief ideas of SSL to answer your question:

  1. Using certificates to authenticate. Server certificate is a must and client certificate is optional (only when the server requests it). A certificate is like something to prove who you are and it also contains a public key for asymmetric encryption.

  2. Using asymmetric encryption (with public key in the server certificate) to establish a shared symmetric key which is used to transfer data between client and server securely by symmetric encryption (for performance reason because symmetric encryption is faster than asymmetric encryption).

The shared symmetric key is established by exchanging a premaster secret from client side (encrypted with server public key) and is derived from the pre-master secret together with client random and server random (thanks @EJP for pointing this out in the comment):

master_secret = PRF(pre_master_secret, "master secret",
                           ClientHello.random + ServerHello.random)

We need the server random and client random to prevent replay attacks that an attacker can capture the previous session and replay it for the new session.

> What I don't understand is, couldn't a hacker just intercept the > public key it sends back to the "customer's browser", and be able to > decrypt anything the customer can.

The hacker cannot decrypt the message since he does not know the server private key. Be aware that public key cannot be used to decrypt the message.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAustinView Question on Stackoverflow
Solution 1 - SslGorvGoylView Answer on Stackoverflow
Solution 2 - SslFrank ZhangView Answer on Stackoverflow
Solution 3 - SslDenis de BernardyView Answer on Stackoverflow
Solution 4 - SslEugeneView Answer on Stackoverflow
Solution 5 - SslAbhishek JainView Answer on Stackoverflow
Solution 6 - SslKhanh TOView Answer on Stackoverflow