Web Security: HTTPS and SSL/TLS Certificates

Table of Contents

Introduction

In modern society, where the internet has become indispensable for daily life and business, the importance of web security is ever-increasing. In particular, HTTPS communication and SSL/TLS certificates have become fundamental technologies for achieving secure information exchange on the web.

This page provides a detailed explanation of how HTTPS communication works and the role of SSL/TLS certificates. You will be able to understand how these technologies protect web communications and ensure user privacy and data integrity.

HTTP Basics

HTTP (Hypertext Transfer Protocol) is the fundamental protocol for transferring data between web browsers and web servers. HTTP was developed by Tim Berners-Lee in 1989 and is the foundation of the World Wide Web.

Main features of HTTP:

HTTP Vulnerabilities

HTTP was not originally designed with secure data transfer in mind. Therefore, it has the following security vulnerabilities:

  • Plaintext Communication: In HTTP, all data is sent in plaintext without encryption. This allows a third party on the communication path to intercept and read the data (eavesdropping attack).
  • Lack of Integrity: There is no mechanism to verify whether the data has been tampered with, making Man-in-the-Middle attacks possible.
  • Lack of Authentication: There is no mechanism to confirm that the communication partner is indeed the intended party.

These vulnerabilities pose a significant risk, especially when handling sensitive information (passwords, credit card information, personal data, etc.). HTTPS was developed to solve these problems.

HTTPS Communication

What is HTTPS?

HTTPS (HTTP Secure) is a protocol that adds an encryption layer using TLS (Transport Layer Security) or its predecessor, SSL (Secure Sockets Layer), to HTTP. HTTPS provides data confidentiality, integrity, and authentication by encrypting the communication between a web browser and a web server.

HTTPS typically uses TCP port 443, and the URL begins with "https://". In modern web browsers, when you access a website protected by HTTPS, a lock icon is displayed in the address bar.

How HTTPS Works

HTTPS achieves secure communication by combining the following technologies:

  1. Encryption: It encrypts all data between the client and server using the TLS/SSL protocol. This prevents third parties from reading the content even if they intercept the data on the communication path.
  2. Data Integrity: It uses a Message Authentication Code (MAC) to verify that the data has not been tampered with during transit.
  3. Authentication: It uses a digital certificate to verify that the server you are connecting to is authentic. This protects users from attacks such as phishing.

HTTPS communication proceeds in the following steps:

  1. The client (browser) requests an HTTPS connection to the server.
  2. The server sends its digital certificate (SSL/TLS certificate) to the client.
  3. The client verifies the certificate and confirms the server's identity.
  4. The client and server perform a TLS handshake to establish a session key.
  5. All subsequent communication is encrypted using the established session key.

Benefits of HTTPS

Using HTTPS provides the following benefits:

  • Data Confidentiality: Since the communication content is encrypted, it prevents eavesdropping by third parties.
  • Data Integrity: You can verify that the data has not been tampered with during transit.
  • Authentication: You can verify that the server you are connecting to is authentic.
  • SEO Advantage: Search engines like Google tend to prioritize websites that use HTTPS in their search results.
  • User Trust: As a security-conscious website, you can more easily gain the trust of users.
  • Use of New Features: Many modern web technologies, such as Service Workers and HTTP/2, are only available over HTTPS.

SSL/TLS Certificates

What are SSL/TLS?

SSL (Secure Sockets Layer) and TLS (Transport Layer Security) are cryptographic protocols for achieving secure communication over the internet. TLS is the successor to SSL, and today the term SSL is often used as a general term that includes TLS.

History of SSL/TLS:

  • SSL 1.0: Developed by Netscape in 1994 but was never publicly released.
  • SSL 2.0: Released in 1995 but security flaws were discovered.
  • SSL 3.0: Released in 1996, but vulnerabilities like the POODLE attack were discovered, and it is now deprecated.
  • TLS 1.0: Standardized by the IETF in 1999 (an improvement on SSL 3.0).
  • TLS 1.1: Released in 2006.
  • TLS 1.2: Released in 2008 and is still widely used today.
  • TLS 1.3: The latest version, released in 2018, with significant improvements in security and performance.

Currently, SSL 2.0, SSL 3.0, TLS 1.0, and TLS 1.1 are considered insecure, and support for them has been discontinued in many browsers and servers. To ensure security, it is recommended to use TLS 1.2 or higher.

Types of Certificates

There are several types of SSL/TLS certificates, depending on their use and validation level:

Classification by Validation Level

  • Domain Validation (DV) Certificate:
    • The most basic certificate, which only confirms ownership of the domain.
    • The issuance process is simple and is usually completed within minutes to hours.
    • Suitable for personal blogs and small websites.
  • Organization Validation (OV) Certificate:
    • In addition to domain ownership, it also verifies the existence of the organization.
    • Issuance may take several days.
    • Suitable for business websites and online services.
  • Extended Validation (EV) Certificate:
    • A certificate issued after the most stringent validation process.
    • It verifies in detail the legal existence of the organization, its physical location, domain ownership, etc.
    • Some browsers display the organization's name in green in the address bar (the display method varies by browser).
    • Suitable for websites requiring high security, such as banks, e-commerce sites, and government agencies.

Classification by Use

  • Single Domain Certificate: Covers only one domain (e.g., example.com).
  • Wildcard Certificate: Covers a main domain and all its subdomains (e.g., *.example.com).
  • Multi-Domain Certificate (SAN Certificate): Covers multiple different domains.

Certificate Validation Process

When a browser validates a website's SSL/TLS certificate, the following process takes place:

  1. Check Certificate Expiration: Verifies that the certificate is within its validity period.
  2. Check Issuer: Verifies that the certificate was issued by a trusted Certificate Authority (CA).
  3. Check Certificate Revocation: Uses a Certificate Revocation List (CRL) or Online Certificate Status Protocol (OCSP) to check that the certificate has not been revoked.
  4. Check Domain Name: Verifies that the domain name listed in the certificate matches the domain name of the website being accessed.
  5. Validate Certificate Chain: Verifies that the certificate chain (chain of trust) from the root certificate to the target certificate is correct.

If any of these validation processes fail, the browser will display a warning to alert the user.

Role of Certificate Authorities (CAs)

A Certificate Authority (CA) is a trusted third-party organization that issues digital certificates. The main roles of a CA are as follows:

  • Certificate Issuance: Verifies the identity of the applicant and issues a digital certificate.
  • Certificate Management: Manages a database of issued certificates and revokes certificates when necessary.
  • Publication of Certificate Revocation Lists (CRLs): Publishes a list of revoked certificates so that clients like browsers can check the validity of a certificate.
  • Root Certificate Management: Securely manages the root certificate, which is the starting point of trust.

Major Certificate Authorities include DigiCert, Comodo (Sectigo), GlobalSign, and Let's Encrypt. Let's Encrypt, in particular, has greatly contributed to the spread of HTTPS by providing a free and automated certificate issuance service.

TLS Handshake

The TLS handshake is the process for establishing a secure communication channel between a client and a server. In this process, an agreement on the encryption algorithms to be used, authentication of the server, and establishment of a shared secret key are performed.

TLS 1.2 Handshake Process (simplified):

  1. ClientHello: The client initiates a connection to the server and sends its supported cipher suites (combinations of encryption algorithms), TLS version, random data, etc.
  2. ServerHello: The server responds to the client's request, selects the cipher suite and TLS version to be used, and sends its random data.
  3. Certificate: The server sends its SSL/TLS certificate to the client.
  4. ServerKeyExchange: If necessary, the server sends additional information required for the key exchange.
  5. ServerHelloDone: The server indicates that it has completed the first phase of the handshake.
  6. ClientKeyExchange: The client sends key exchange information. This includes the pre-master secret encrypted with the server's public key.
  7. ChangeCipherSpec: Both the client and server notify that they will use encryption for subsequent communication.
  8. Finished: Both the client and server send a message confirming that the handshake was successful.

In TLS 1.3, the handshake process has been simplified and can now be completed in one round-trip (1-RTT) or, in some cases, zero round-trips (0-RTT). This reduces connection establishment latency and improves website loading speed.

HTTPS in Development Environments

Using HTTPS in a development environment is important for testing applications under the same conditions as the production environment. In particular, features like secure cookies, CORS, and HTTP/2 only work correctly over HTTPS. Here, we introduce various methods for setting up HTTPS in local development or testing environments.

Self-Signed Certificates

A self-signed certificate is a certificate signed by yourself, not by a Certificate Authority (CA). They are widely used in development and testing environments because they can be created easily and at no cost. However, browsers will display a warning for certificates not signed by a trusted CA.

How to create a self-signed certificate using OpenSSL

You can create a self-signed certificate with OpenSSL using the following commands:

# Generate a private key
openssl genrsa -out server.key 2048

# Create a Certificate Signing Request (CSR)
openssl req -new -key server.key -out server.csr

# Create a self-signed certificate
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
            

Running these commands will generate the following files:

  • server.key: Private key
  • server.csr: Certificate Signing Request
  • server.crt: Self-signed certificate

The generated certificate and private key can be used in your web server configuration. However, because certificates created this way are not trusted by browsers, a warning will be displayed upon access. It is recommended to use them only in development environments and to obtain a certificate from a trusted CA for production environments.

Using mkcert

mkcert is a tool for easily creating trusted certificates in a local development environment. mkcert automatically installs a root certificate in the local trust store, allowing you to use HTTPS without browser warnings.

How to install mkcert

Installation methods for each OS are as follows:

macOS (using Homebrew)
brew install mkcert
brew install nss # if you use Firefox
            
Windows (using Chocolatey)
choco install mkcert
            
Linux (using Linuxbrew)
brew install mkcert
            

Alternatively, you can download it directly from GitHub Releases.

How to use mkcert

To use mkcert, follow these steps:

  1. Install a local CA (Certificate Authority):
    mkcert -install
  2. Create a certificate:
    mkcert localhost 127.0.0.1 ::1 example.test *.example.test
    This command creates a valid certificate for the specified domains (localhost, 127.0.0.1, ::1, example.test, *.example.test).

The created certificate and private key can be used in the configuration of your web server or application. Certificates created with mkcert are trusted by the browser on the local machine, so you can use HTTPS without warnings.

Features of Development Tools and Frameworks

Many development tools and frameworks have built-in features to easily set up HTTPS in a local development environment. Below, we introduce how to configure HTTPS in major tools and frameworks.

Node.js (Express)

const express = require('express');
const https = require('https');
const fs = require('fs');

const app = express();

// HTTPS server options
const options = {
  key: fs.readFileSync('path/to/key.pem'),
  cert: fs.readFileSync('path/to/cert.pem')
};

// Create HTTPS server
https.createServer(options, app).listen(443, () => {
  console.log('HTTPS Server running on port 443');
});
            

React (Create React App)

In Create React App, simply setting HTTPS=true in the .env file will make the development server use HTTPS:

# .env file
HTTPS=true
            

To use a custom certificate, set the environment variables as follows:

# .env file
HTTPS=true
SSL_CRT_FILE=path/to/cert.crt
SSL_KEY_FILE=path/to/cert.key
            

Vue CLI

In Vue CLI, you can configure HTTPS in the vue.config.js file:

// vue.config.js
const fs = require('fs')

module.exports = {
  devServer: {
    https: {
      key: fs.readFileSync('path/to/server.key'),
      cert: fs.readFileSync('path/to/server.crt')
    }
  }
}
            

Angular

In Angular, you can configure HTTPS in the angular.json file:

// angular.json
{
  "projects": {
    "your-app": {
      "architect": {
        "serve": {
          "options": {
            "ssl": true,
            "sslKey": "path/to/key.pem",
            "sslCert": "path/to/cert.pem"
          }
        }
      }
    }
  }
}
            

Django

In Django, you can specify the --cert-file and --key-file options with the runserver command:

python manage.py runserver --cert-file path/to/cert.pem --key-file path/to/key.pem
            

Ruby on Rails

In Ruby on Rails, you can configure HTTPS in the config/puma.rb file:

# config/puma.rb
ssl_bind '0.0.0.0', '3000', {
  key: 'path/to/server.key',
  cert: 'path/to/server.crt'
}
            

Using Let's Encrypt

Let's Encrypt is a Certificate Authority that provides a free and automated certificate issuance service. Even in development and staging environments, if you have a server with an actual domain name, you can use Let's Encrypt to obtain a valid certificate.

Let's Encrypt's Staging Environment

Let's Encrypt has a staging environment separate from the production environment. The staging environment has more lenient rate limits, making it suitable for testing and development. However, certificates issued by the staging environment are not trusted by browsers, so a warning will be displayed.

Obtaining a Let's Encrypt certificate using Certbot

Certbot is a client tool for easily obtaining and renewing Let's Encrypt certificates. Below are the basic steps for obtaining a certificate using Certbot:

Installing Certbot

Installation methods for each OS are as follows:

Ubuntu/Debian
sudo apt update
sudo apt install certbot
            
CentOS/RHEL
sudo yum install certbot
            
macOS (using Homebrew)
brew install certbot
            
Obtaining a certificate (using a web server plugin)

For Nginx:

sudo certbot --nginx -d example.com -d www.example.com
            

For Apache:

sudo certbot --apache -d example.com -d www.example.com
            
Obtaining a certificate (standalone mode)
sudo certbot certonly --standalone -d example.com -d www.example.com
            
Obtaining a certificate in the staging environment (for testing)
sudo certbot --staging --nginx -d example.com -d www.example.com
            

DNS Validation in Development Environments

For a local development environment that is not accessible from the internet, you can obtain a Let's Encrypt certificate using DNS validation. This method proves domain ownership by adding a DNS record.

sudo certbot certonly --manual --preferred-challenges dns -d example.com
            

When you run this command, it will display a TXT record that you need to add to your DNS records. Add the specified TXT record to your DNS, wait for propagation, and then continue the process.

Obtaining a Wildcard Certificate

For development environments with multiple subdomains, a wildcard certificate is convenient. Wildcard certificates can only be obtained through DNS validation.

sudo certbot certonly --manual --preferred-challenges dns -d example.com -d *.example.com
            

Let's Encrypt certificates are valid for 90 days and can be renewed automatically. It is recommended to set up periodic renewal even in a development environment.

# Test automatic certificate renewal
sudo certbot renew --dry-run

# Set up automatic renewal with a cron job
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null
            

Web Security Best Practices

Here are the best practices for strengthening the security of your website or web application:

Common Issues and Solutions

Certificate Issues

Configuration Issues

Performance Issues