Contents (9)
What Is HTTPS?
In my previous post, I wrote about HTTP. But for security reasons, HTTPS is a must in most cases. That way, even if packets get intercepted somewhere along the network route, the HTTP messages are encrypted, so nobody can tell what's in them.
1. Encryption Methods
Encryption methods fall into two broad categories: symmetric-key encryption and asymmetric-key encryption. You can think of the way HTTPS encrypts things as using both symmetric-key and asymmetric-key encryption.
1.1 Symmetric-Key Encryption
Symmetric-key encryption uses the same key for encryption and decryption. That makes it simple to implement and fast, but if the key gets stolen, whoever has it can both encrypt and decrypt, so distributing the key is tricky. The most widely used algorithm for symmetric-key encryption is AES.

1.2 Asymmetric-Key Encryption
Asymmetric-key encryption encrypts with a public key and decrypts with a private key. The reverse works too: if you encrypt with the private key, you can decrypt with the public key, and the two keys form a pair. Usually only the server decrypts and the client encrypts, so it's fine to hand the public key out to anyone. That's because data encrypted with it can only be decrypted by the server.
Asymmetric-key encryption is also put to use in another way. If a public key has been verified, you can use it to verify where encrypted information came from. If you successfully decrypt some data with the public key provided by Company A, you know the data came from Company A. And if decryption fails, you also know it isn't Company A's data. An organization that verifies which company a public key belongs to is called a CA (Certificate Authority).
Asymmetric keys make key distribution easy, but they're more complex to implement and slower to compute. The most widely used algorithm for asymmetric-key encryption is RSA.

2. How HTTPS Encryption Works
With HTTPS, the server receives the symmetric key generated by the client through asymmetric-key encryption, and then data is sent using that client-generated symmetric key. In short, it goes through the following process.
The client receives the server's SSL certificate. (It includes the public key.)
The client generates a session key (a one-time, symmetric key), "encrypts the generated session key with the server's public key," and sends it to the server.
The server and the client exchange data using the session key (symmetric key) that only the two of them know.
3. CAs and Certificates
Exchanging session keys with a public key (asymmetric key) is risky in itself. For example, if a hacker in the middle forges the public key and sends it to the client, the client has no way to tell. So the client may think it's the real one and send its very own session key straight to the hacker. The solution to this is the SSL/TLS certificate. It's less a technical solution than one based on social agreement and trust. Just as you trust that someone is from South Korea once it's proven that their passport was issued in Korea, a public key certified by one of a small number of rigorously vetted CAs proves that it came from that server.
3.1 Creating a Certificate
The server gets a certificate issued through a CA. Here's how that works.
The server sends the [Server-Public-Key] that only the server knows, along with its [Site-Info], to the CA and requests an SSL certificate.
The CA hashes the server's [Certificate-Info] (the combination of the [Server-Public-Key], [Site-Info], and [Issuance-Info]) all together to create the [Hashed-Certificate-Info].
Using the [CA-Private-Key] that only the CA knows, it encrypts the [Hashed-Certificate-Info] to create a Signature, then combines the two into a certificate and sends it to the server.
When communicating, the server gives the client the certificate issued by the CA. When the certificate's Signature is decrypted with the [CA-Public-Key], it becomes the [Hashed-Certificate-Info].

3.2 Verifying a Certificate
The client's browser has a list of [CA-Public-Key]s for trusted CAs. So by checking whether the certificate's Signature checks out when decrypted with a [CA-Public-Key] from this list, it can prove that the data really came from the correct domain and not from a hacker. (Trust-based)
If the certificate is from an organization that isn't on the browser's built-in [CA-Public-Key] list, the browser pops up a warning. In that case, you can check that organization's public key and so on yourself, decide whether it's an organization you can trust, and then use it. And here's the verification process.
The client sends a request to the server and gets the certificate.
It hashes the certificate's [Certificate-Info] (everything except the signature) with the specified algorithm. This gives you the [Hashed-Certificate-Info].
The client decrypts the certificate's Signature with the [CA-Public-Key] built into the browser. This value is the [Hashed-Certificate-Info].
It checks whether the [Hashed-Certificate-Info] values from steps 2 and 3 above are the same, proving that the certificate was delivered correctly and didn't come from a hacker.

In the actual certificate verification process, there's also a step that checks with the CA whether the certificate has been revoked. The client checks this through CRL or OCSP.
Method | Checked by | Description |
|---|---|---|
CRL | Client | The client downloads the revocation list that the CA publishes periodically and checks it |
OCSP | Client | The client asks the CA about the certificate's status in real time |
OCSP Stapling | Client (checks), Server (delivers) | The server also passes along the OCSP response it got from the CA to the client, which improves performance |
3.3 After Verifying the Certificate
After verifying the certificate, the client generates a [Client-Session-Key], decrypts this key with the [Server-Public-Key] and sends it to the server, and then data is sent back and forth encrypted with the [Client-Session-Key].
