Bài này gộp 2 chủ đề: TLS / HTTPS, Encryption at Rest & in Transit.
Mục tiêu học tập
- Hiểu vì sao HTTPS là bắt buộc cho mọi web app (eavesdropping, MITM, integrity)
- Nắm TLS handshake — so sánh TLS 1.2 và TLS 1.3
- Phân biệt cipher suite, key exchange (ECDHE), forward secrecy
- Hiểu certificate chain: root CA → intermediate → leaf, cách trình duyệt verify
- Biết Let's Encrypt và ACME protocol — auto-renew với certbot/Caddy
- Hiểu HSTS preload và lý do HPKP bị deprecate
- Nắm cơ bản mTLS (mutual TLS) cho service-to-service auth
- Phân biệt symmetric (AES-256-GCM) và asymmetric encryption (RSA, ECC) — khi nào dùng cái nào
- Hiểu envelope encryption pattern: DEK + KEK, cách KMS hoạt động
- So sánh AWS KMS, GCP KMS, Azure Key Vault — IAM-controlled key access
- Biết database encryption at rest: TDE (Transparent Data Encryption) vs application-level
- Hiểu E2EE (end-to-end encryption) ở mức cơ bản — Signal protocol intro
- Tránh các sai lầm phổ biến: ECB mode, hardcoded keys, IV yếu, dùng RSA encrypt data lớn
Phần 1: TLS / HTTPS
1. Tại sao HTTPS bắt buộc
Ba mối đe doạ HTTPS phòng chống
Cụ thể từng mối đe doạ
| Mối đe doạ | Ví dụ thực tế | HTTPS giải quyết bằng |
|---|---|---|
| Eavesdropping | Wi-Fi công cộng nghe HTTP traffic, lấy session cookie | Encryption (symmetric cipher) |
| MITM | ISP/coffee shop chen vào, fake server response | Server authentication (certificate) |
| Tampering | ISP inject quảng cáo vào HTML, malware vào JS | Integrity (MAC/AEAD) |
Bonus: Tin cậy của user
- Browser hiển thị "Not Secure" trên HTTP → user mất niềm tin
- Một số API browser chỉ hoạt động trên HTTPS: Service Worker, WebRTC, Geolocation, Notifications, Web Crypto, HTTP/2, HTTP/3
- SEO: Google rank HTTPS site cao hơn
2. TLS handshake — TLS 1.2 vs TLS 1.3
TLS 1.2 (2008)
TLS 1.3 (2018)
Bảng so sánh
| Tính chất | TLS 1.2 | TLS 1.3 |
|---|---|---|
| Năm ra mắt | 2008 | 2018 (RFC 8446) |
| Handshake RTT | 2 RTT | 1 RTT (0-RTT khi resume) |
| Cipher suites | ~37 suite, nhiều legacy | 5 suite, chỉ AEAD |
| Key exchange | RSA, DH, ECDHE | Chỉ (EC)DHE — forward secrecy bắt buộc |
| Forward secrecy | Optional | Mandatory |
| Legacy crypto | Cho phép RC4, 3DES, MD5, SHA-1 | Loại bỏ hoàn toàn |
| Encrypted handshake | Không (Certificate gửi plaintext) | Có (từ EncryptedExtensions trở đi) |
3. Cipher suite
Cipher suite định nghĩa các thuật toán dùng trong TLS connection.
TLS 1.2 cipher suite name
TLS 1.3 cipher suite (gọn hơn nhiều)
TLS_AES_256_GCM_SHA384 ← bulk + HKDF
TLS_CHACHA20_POLY1305_SHA256 ← cho ARM/mobile (no AES-NI)
TLS_AES_128_GCM_SHA256
Key exchange và signature không nằm trong cipher suite ở TLS 1.3 — được negotiate riêng (signature algorithm extension).
Khuyến nghị cipher (2024-2025)
GOOD (TLS 1.3):
TLS_AES_256_GCM_SHA384
TLS_AES_128_GCM_SHA256
TLS_CHACHA20_POLY1305_SHA256
GOOD (TLS 1.2):
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES256-GCM-SHA384
ECDHE-ECDSA-CHACHA20-POLY1305
BAD — disable:
- Anything với RC4, 3DES, DES, MD5, SHA-1, NULL
- Static RSA key exchange (no forward secrecy)
- TLS 1.0, TLS 1.1, SSL 3.0, SSL 2.0
4. Forward Secrecy
Ý tưởng
Nếu server private key bị compromise trong tương lai, traffic đã ghi lại trước đó không giải mã được.
Không có forward secrecy (RSA key exchange):
Client encrypt session key bằng server public key → gửi qua mạng
Attacker ghi lại traffic
1 năm sau: server private key bị leak
Attacker decrypt session key cũ → decrypt mọi traffic cũ
Có forward secrecy (ECDHE):
Client + Server tạo session key qua Diffie-Hellman ephemeral
Mỗi session có key pair mới, vứt đi sau handshake
Server long-term key chỉ dùng để SIGN, không encrypt session key
Server key leak → KHÔNG giúp giải mã traffic cũ
ECDHE flow đơn giản
1. Server có long-term key pair (RSA hoặc ECDSA) cho IDENTITY (sign)
2. Mỗi handshake:
- Server tạo ephemeral EC key pair (eS_priv, eS_pub)
- Client tạo ephemeral EC key pair (eC_priv, eC_pub)
- Trao đổi public key
- Cả hai compute: shared_secret = ECDH(eS_priv, eC_pub) = ECDH(eC_priv, eS_pub)
- Server SIGN ephemeral pub key bằng long-term key (chứng minh identity)
3. Sau handshake: vứt eS_priv, eC_priv → không ai có thể tái tạo shared_secret
TLS 1.3 bắt buộc forward secrecy — đây là lý do RSA key exchange bị loại bỏ hoàn toàn.
5. Certificate chain
Cấu trúc tin cậy
Browser verify như thế nào?
1. Server gửi: [leaf cert, intermediate cert] trong ServerHello
2. Browser:
a. Đọc leaf cert → kiểm tra Subject Alt Name có match hostname không
b. Kiểm tra leaf cert còn hạn (notBefore < now < notAfter)
c. Verify chữ ký của intermediate trên leaf
d. Lấy intermediate cert → verify chữ ký của root trên intermediate
e. Root cert phải có trong trust store của OS/browser
f. Kiểm tra revocation:
- CRL (Certificate Revocation List) — list bị thu hồi
- OCSP (Online Certificate Status Protocol)
- OCSP stapling — server đính kèm OCSP response
3. Nếu mọi bước OK → "Secure" lock icon
Kiểm tra chain với openssl
# Lấy certificate chain từ server
openssl s_client -connect example.com:443 -showcerts
# Verify chain
openssl s_client -connect example.com:443 -CApath /etc/ssl/certs
# In ra detail của leaf cert
echo | openssl s_client -connect example.com:443 2>/dev/null | \
openssl x509 -text -noout
# Check expiration
echo | openssl s_client -connect example.com:443 2>/dev/null | \
openssl x509 -enddate -noout
# notAfter=Mar 15 23:59:59 2026 GMT
Lỗi chain phổ biến
| Lỗi | Triệu chứng | Nguyên nhân |
|---|---|---|
unable to get local issuer certificate | Browser/curl không tin cert | Server không gửi intermediate cert |
NET::ERR_CERT_DATE_INVALID | Cert hết hạn | Quên renew |
NET::ERR_CERT_COMMON_NAME_INVALID | Hostname mismatch | CN/SAN không có domain đang truy cập |
NET::ERR_CERT_AUTHORITY_INVALID | Browser không tin root | Self-signed cert hoặc root không có trong store |
6. Let's Encrypt và ACME protocol
Let's Encrypt là gì
CA miễn phí, automated, được hỗ trợ bởi Internet Security Research Group (ISRG). Cert valid 90 ngày, khuyến khích renew sau 60 ngày.
ACME protocol (RFC 8555)
certbot — cách phổ biến nhất
# Cài certbot
sudo apt install certbot python3-certbot-nginx
# Standalone (cert + nginx config auto)
sudo certbot --nginx -d example.com -d www.example.com
# DNS-01 (cho wildcard cert)
sudo certbot certonly --manual --preferred-challenges dns -d "*.example.com"
# Auto-renew (cron tự setup)
sudo certbot renew --dry-run
Cron job mặc định:
0 */12 * * * root certbot renew --quiet
Caddy — đơn giản nhất
# Caddyfile
example.com {
reverse_proxy localhost:3000
}
Đó là tất cả. Caddy tự xin Let's Encrypt cert, tự renew, tự redirect HTTP→HTTPS, tự config HTTPS đúng chuẩn. Không cần nginx + certbot dance.
nginx + certbot snippet
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
location / {
proxy_pass http://localhost:3000;
}
}
# Redirect HTTP → HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
7. HSTS và HPKP
HSTS (HTTP Strict Transport Security)
Response header:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Ý nghĩa: "Browser, từ giờ trong 1 năm, chỉ kết nối với tôi qua HTTPS — kể cả khi user gõ http://".
HSTS preload list
Vấn đề first-visit: lần đầu vào site, browser chưa thấy HSTS header → có thể bị MITM trên HTTP. Giải pháp: preload list.
1. Site đăng ký tại https://hstspreload.org
2. Chrome, Firefox, Safari ship sẵn danh sách HSTS preload trong browser
3. Kể cả lần đầu vào site, browser auto upgrade HTTP→HTTPS
Yêu cầu preload:
- Cert valid
- HTTPS hoạt động trên toàn domain và subdomain
max-age >= 31536000(1 năm)includeSubDomains; preloaddirective
Cảnh báo: preload khó undo. Nếu vài tháng sau bạn muốn tắt HTTPS trên subdomain nào đó, phải đợi vài tháng để remove khỏi list.
HPKP (HTTP Public Key Pinning) — deprecated
Ý tưởng: server gửi header pin một public key. Browser nhớ pin, từ chối cert nào không match pin.
Public-Key-Pins: pin-sha256="base64=="; max-age=2592000;
Tại sao bị deprecate (2017)?
- Foot-gun lớn: pin sai key → site không truy cập được cho user đã visit, kể cả khi đã reissue cert. Một số site đã tự khoá mình hàng tháng.
- Ransomware vector: attacker compromise server tạm thời → pin key giả → server không thể recover.
- Certificate Transparency (CT) ra đời, giải quyết phần lớn vấn đề "rogue CA" mà HPKP nhắm tới.
Chrome bỏ HPKP support năm 2018. Thay thế: Expect-CT header (nay cũng deprecated) và CAA DNS record.
CAA record — phòng tuyến hiện tại
example.com. IN CAA 0 issue "letsencrypt.org"
example.com. IN CAA 0 issuewild ";"
example.com. IN CAA 0 iodef "mailto:security@example.com"
Ý nghĩa: chỉ Let's Encrypt được phép issue cert cho example.com. Mọi CA tuân thủ CA/Browser Forum baseline phải check CAA trước khi issue.
8. mTLS (mutual TLS)
TLS thường vs mTLS
TLS thường:
Client verify Server identity (qua cert)
Server không verify Client identity
→ App-level auth (cookie, JWT, OAuth) để biết client là ai
mTLS:
Client verify Server identity
Server cũng verify Client identity (qua client cert)
→ Auth ngay tại transport layer, không cần app-level token
Khi nào dùng mTLS
- Service-to-service trong microservices (vd: service mesh — Istio, Linkerd)
- B2B API giữa các công ty (cert thay vì shared secret)
- IoT — thiết bị có cert cá biệt, không phù hợp với username/password
- Zero Trust network — mọi connection phải auth, kể cả internal
Setup mTLS đơn giản với nginx
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
# Yêu cầu client cert
ssl_client_certificate /etc/ssl/ca.crt; # CA tin cậy cho client cert
ssl_verify_client on; # bắt buộc client cert
location / {
# Forward client cert info xuống upstream
proxy_set_header X-Client-CN $ssl_client_s_dn_cn;
proxy_set_header X-Client-Cert $ssl_client_escaped_cert;
proxy_pass http://backend;
}
}
Service mesh: auto mTLS
Trong Kubernetes với Istio/Linkerd:
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
spec:
mtls:
mode: STRICT
Istio inject sidecar (Envoy) vào mỗi pod. Sidecar handle TLS handshake với cert được cấp bởi Istio CA. App code không cần biết về TLS — Envoy lo hết.
Pattern client cert authentication trong code (Node.js)
import https from "node:https";
const options = {
hostname: "api.partner.com",
port: 443,
path: "/data",
method: "GET",
cert: fs.readFileSync("./client-cert.pem"),
key: fs.readFileSync("./client-key.pem"),
ca: fs.readFileSync("./partner-ca.pem"), // verify server
};
const req = https.request(options, (res) => {
console.log(`Status: ${res.statusCode}`);
});
req.end();
Phần 2: Encryption at Rest & in Transit
1. Symmetric vs Asymmetric
Trade-off và use case
| Tiêu chí | Symmetric (AES) | Asymmetric (RSA/ECC) |
|---|---|---|
| Tốc độ | Rất nhanh | Chậm |
| Key size | 128/256 bit | 2048-4096 bit (RSA), 256 bit (ECC) |
| Encrypt data lớn | Có | KHÔNG — chỉ encrypt key ngắn |
| Key distribution | Khó (cần kênh an toàn) | Dễ (public key share công khai) |
| Use case chính | Bulk encryption (file, network) | Key exchange, digital signature |
Tại sao thực tế dùng cả hai (hybrid)?
TLS chính là ví dụ:
- ECDHE (asymmetric, slow) chỉ để negotiate session key
- AES-256-GCM (symmetric, fast) để encrypt dữ liệu chính
Asymmetric giải quyết vấn đề "làm sao share key an toàn", symmetric đảm nhận heavy lifting.
2. AES-GCM — AEAD chuẩn hiện đại
Block cipher mode
Vì sao ECB là tai họa kinh điển
Image gốc (Tux): Sau khi encrypt với AES-ECB:
████████████████ █▒█▓█▒▓█▒█▓█▒
█ ████ ▒▓▒ ▓███
█ ████ ▒▓▒ ▓███
█ ████ ▒▓▒ ▓███
████████████████ █▒█▓█▒▓█▒█▓█▒
→ Pattern lộ rõ! Cùng plaintext block → cùng ciphertext block.
ECB là sai lầm cơ bản nhất trong cryptography. Mọi mode hiện đại (CBC, GCM, XTS) đều có IV (Initialization Vector) ngẫu nhiên để cùng plaintext cho ra ciphertext khác nhau.
AES-GCM trong Node.js
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
function encrypt(plaintext, key) {
// KEY: 32 bytes (256 bit), bí mật
// IV: 12 bytes (96 bit) ngẫu nhiên, KHÔNG được reuse với cùng key
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", key, iv);
const encrypted = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
const authTag = cipher.getAuthTag(); // 16 bytes
// Lưu: iv + authTag + ciphertext (iv và tag KHÔNG cần bí mật)
return Buffer.concat([iv, authTag, encrypted]);
}
function decrypt(data, key) {
const iv = data.subarray(0, 12);
const authTag = data.subarray(12, 28);
const ciphertext = data.subarray(28);
const decipher = createDecipheriv("aes-256-gcm", key, iv);
decipher.setAuthTag(authTag);
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString("utf8");
// Nếu authTag mismatch → throw → biết data bị tamper
}
IV/Nonce rules
RULE: Với cùng một key, IV/nonce KHÔNG được reuse.
Reuse IV trong GCM = catastrophic, attacker có thể recover key stream.
Cách sinh IV đúng:
1. Random 96-bit (cho GCM) — đủ entropy nếu < 2^32 message với cùng key
2. Counter (incremented mỗi message) — cần synchronization
Cách sinh IV SAI:
- Hardcoded "000000000000"
- Lấy từ timestamp với độ phân giải thấp
- Dùng IV cũ
3. Asymmetric: RSA và ECC
RSA
Key generation:
- Chọn 2 prime lớn p, q
- n = p * q (modulus)
- chọn e (public exponent, thường = 65537)
- tính d (private exponent)
- public_key = (n, e), private_key = (n, d)
Encrypt: c = m^e mod n
Decrypt: m = c^d mod n
- Key size khuyến nghị: 3072 bit (~128-bit security). RSA-2048 vẫn được, sẽ deprecate dần.
- Padding: BẮT BUỘC dùng OAEP (Optimal Asymmetric Encryption Padding). PKCS#1 v1.5 padding có lỗ hổng (Bleichenbacher attack).
- Giới hạn: RSA-3072 chỉ encrypt được block ≤ 384 bytes (bằng modulus) — với OAEP-SHA256 tối đa ~318 bytes plaintext sau khi trừ padding. Không bao giờ dùng RSA encrypt data thật, chỉ encrypt symmetric key.
ECC (Elliptic Curve Cryptography)
Curves phổ biến:
- secp256r1 / P-256 / NIST P-256 (TLS, web standard)
- secp384r1 / P-384
- Curve25519 (Signal, modern, an toàn hơn NIST curves)
- Curve448 (high security)
- ECC-256 ≈ RSA-3072 về security level
- Key nhỏ hơn rất nhiều → tốt cho mobile/IoT
- Operation nhanh hơn RSA
- Ed25519 cho signature, X25519 cho key exchange — đang là chuẩn mới
So sánh
| RSA-3072 | ECDSA P-256 | Ed25519 | |
|---|---|---|---|
| Public key size | 384 bytes | 64 bytes | 32 bytes |
| Sign speed | Chậm | Trung bình | Rất nhanh |
| Verify speed | Nhanh | Trung bình | Rất nhanh |
| Security level | ~128-bit | ~128-bit | ~128-bit |
| Recommended | OK | OK | Best (cho new project) |
4. Envelope Encryption — cách KMS hoạt động
Vấn đề trực tiếp dùng KMS
Nếu mỗi byte data đều gọi KMS encrypt:
- KMS có rate limit (~10,000 req/giây)
- Latency cao (mỗi gọi 10-50ms)
- Chi phí cao ($0.03/10,000 request)
- KMS không thiết kế để encrypt data lớn (giới hạn 4 KB)
Envelope encryption pattern
Flow encrypt
1. App gọi KMS GenerateDataKey(KeyId="my-kek", KeySpec=AES_256)
2. KMS trả về:
- plaintext_DEK (32 bytes — chỉ tồn tại trong memory app)
- ciphertext_DEK (DEK đã được encrypt bằng KEK trong KMS)
3. App encrypt data:
encrypted_data = AES-256-GCM(plaintext_DEK, data)
4. App lưu xuống storage:
{ ciphertext_DEK, iv, authTag, encrypted_data }
5. App XÓA plaintext_DEK khỏi memory (zeroize)
Flow decrypt
1. App đọc { ciphertext_DEK, iv, authTag, encrypted_data } từ storage
2. App gọi KMS Decrypt(CiphertextBlob=ciphertext_DEK)
→ KMS verify IAM, decrypt bằng KEK, trả về plaintext_DEK
3. App decrypt data:
data = AES-256-GCM-decrypt(plaintext_DEK, iv, authTag, encrypted_data)
4. App XÓA plaintext_DEK khỏi memory
Lợi ích
- Performance: KMS chỉ gọi 1 lần / object (encrypt key 4 KB), không phải mỗi byte
- Caching: có thể cache DEK in memory cho nhiều object trong session
- Key rotation: rotate KEK trong KMS không cần re-encrypt data — chỉ cần re-encrypt DEK (nhanh)
- Audit: KMS log mỗi Decrypt call → biết ai access data, khi nào
Code ví dụ (AWS KMS + Node.js)
import { KMSClient, GenerateDataKeyCommand, DecryptCommand } from "@aws-sdk/client-kms";
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
const kms = new KMSClient({ region: "ap-southeast-1" });
const KEY_ID = "alias/my-app-kek";
async function encryptObject(plaintext) {
// Bước 1: Generate DEK
const { Plaintext: dek, CiphertextBlob: encryptedDek } = await kms.send(
new GenerateDataKeyCommand({ KeyId: KEY_ID, KeySpec: "AES_256" })
);
// Bước 2: Encrypt data bằng DEK
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", dek, iv);
const ciphertext = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
const authTag = cipher.getAuthTag();
// Bước 3: Trả về envelope (DEK đã encrypt + ciphertext)
return {
encryptedDek: Buffer.from(encryptedDek).toString("base64"),
iv: iv.toString("base64"),
authTag: authTag.toString("base64"),
ciphertext: ciphertext.toString("base64"),
};
}
async function decryptObject(envelope) {
// Bước 1: Decrypt DEK bằng KMS
const { Plaintext: dek } = await kms.send(
new DecryptCommand({
CiphertextBlob: Buffer.from(envelope.encryptedDek, "base64"),
})
);
// Bước 2: Decrypt data bằng DEK
const decipher = createDecipheriv("aes-256-gcm", dek, Buffer.from(envelope.iv, "base64"));
decipher.setAuthTag(Buffer.from(envelope.authTag, "base64"));
return Buffer.concat([
decipher.update(Buffer.from(envelope.ciphertext, "base64")),
decipher.final(),
]).toString("utf8");
}
5. KMS so sánh: AWS, GCP, Azure
| Tính chất | AWS KMS | GCP Cloud KMS | Azure Key Vault |
|---|---|---|---|
| Auth | IAM | GCP IAM | Azure AD / Managed Identity |
| HSM | FIPS 140-3 Level 3 (trước 2023: 140-2 Level 2) | FIPS 140-2 Level 3 (HSM keys) | FIPS 140-2 Level 2 (Level 3 với Managed HSM) |
| BYOK (Bring Your Own Key) | Có (import) | Có | Có |
| Hold Your Own Key (HYOK) | XKS (External Key Store) | EKM (External Key Manager) | Managed HSM + customer key |
| Key rotation | Auto (1 năm) cho symmetric | Auto (configurable) | Auto (configurable) |
| Cross-region | Multi-region keys | Multi-region keyring | Geo-replicated vault |
| Pricing | $1/key/month + API calls | $0.06/key version/month + API calls | $0.03/10K transactions, premium tier có HSM |
IAM-controlled key access
// AWS KMS key policy — ai được phép dùng key này
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAppRoleToUseKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::123456789012:role/app-role" },
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey",
"kms:DescribeKey"
],
"Resource": "*"
},
{
"Sid": "DenyAuditorFromDecrypt",
"Effect": "Deny",
"Principal": { "AWS": "arn:aws:iam::123456789012:role/auditor" },
"Action": "kms:Decrypt",
"Resource": "*"
}
]
}
Key policy + IAM policy đồng thời được evaluate. Mặc định KMS key chỉ trao quyền cho root account → phải explicit grant.
6. Database encryption at rest
Ba lớp encryption
Bảng so sánh
| Lớp | Threat protected | Threat KHÔNG protected |
|---|---|---|
| Full-disk (EBS encryption, LUKS) | Disk bị steal | DB connection compromise, SQL injection, admin xem data |
| TDE (DB-level) | Backup steal, disk steal | App có access → đọc plaintext bình thường |
| Application-level | Mọi threat trên + DBA xem | Application bug làm leak |
TDE — Transparent Data Encryption
PostgreSQL: pgcrypto extension (column-level), hoặc dùng RDS encryption (full DB)
MySQL: native TDE từ 5.7+
SQL Server: TDE built-in (Enterprise edition)
Oracle: TDE built-in (Advanced Security option)
AWS RDS: tick "Enable encryption" khi tạo DB → toàn bộ storage, snapshot, replica đều encrypt
Dùng AWS KMS key (default hoặc CMK của bạn)
TDE protection model:
Disk steal scenario:
Attacker steal raw disk → đọc EBS snapshot → encrypted nonsense ✓ Safe
App compromise scenario:
Attacker SQL injection → SELECT * FROM users → DB decrypt on the fly → plaintext ✗ Not safe
Application-level encryption (column-level)
-- Lưu thẳng encrypted blob vào column
CREATE TABLE users (
id UUID PRIMARY KEY,
email TEXT NOT NULL,
ssn_encrypted BYTEA, -- AES-256-GCM ciphertext
ssn_encrypted_dek BYTEA -- DEK encrypted by KMS
);
// App encrypt trước khi INSERT
const { encryptedDek, iv, authTag, ciphertext } = await encryptObject(user.ssn);
await db.query(
"INSERT INTO users (id, email, ssn_encrypted, ssn_encrypted_dek) VALUES ($1, $2, $3, $4)",
[user.id, user.email, Buffer.concat([iv, authTag, ciphertext]), encryptedDek]
);
// Decrypt khi cần dùng — DBA query thẳng DB chỉ thấy ciphertext
Trade-off:
- Pros: DBA và backup compromise không lộ data
- Cons: query/index trên encrypted field rất khó, latency cao hơn, complexity tăng
Đặc biệt: searchable encryption / deterministic encryption
Nếu cần WHERE email = ? trên encrypted column, có thể dùng deterministic encryption (cùng plaintext → cùng ciphertext). Nhưng deterministic mode lộ pattern (như ECB ở mức row) — chỉ dùng cho lookup, không cho data nhạy cảm chính.
7. End-to-End Encryption (E2EE)
Khái niệm
Signal Protocol — chuẩn de facto
Concepts:
- Double Ratchet: mỗi message dùng key khác, key forward-secret
- X3DH (Extended Triple Diffie-Hellman): key agreement async
- Pre-keys: cho phép initiate session khi recipient offline
Properties đạt được:
- Forward secrecy: leak key hiện tại không decrypt được message cũ
- Future secrecy (post-compromise security): leak key tạm → tự healing sau vài turn
- Deniability: không thể prove ai gửi message cụ thể nào
E2EE trong app phổ biến
| App | E2EE default | Group chat | Backup E2EE |
|---|---|---|---|
| Signal | Có | Có | Có (PIN-encrypted) |
| Có | Có | Có (optional, từ 2021) | |
| iMessage | Có | Có | Có (Advanced Data Protection, opt-in) |
| Telegram | KHÔNG (chỉ Secret Chat) | KHÔNG | KHÔNG |
| Slack | KHÔNG | - | - |
| Zoom | Có (opt-in từ 2020) | Limited | - |
Khi nào dev cần biết E2EE?
- Build app messaging / collaboration nhạy cảm
- Build storage app cho user (Standard Notes, Bitwarden, ProtonMail)
- Healthcare, legal — compliance yêu cầu provider không đọc được content
Library: libsignal, Matrix/Olm, Tink.
8. Sai lầm crypto phổ biến
1. Dùng ECB mode
# ❌ NGUY HIỂM
from Crypto.Cipher import AES
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(pad(plaintext, 16))
# ✅ ĐÚNG
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
nonce = get_random_bytes(12)
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
2. Hardcode key trong source
// ❌ NGUY HIỂM
const KEY = Buffer.from("0123456789abcdef0123456789abcdef", "hex");
// ✅ ĐÚNG — load từ KMS/secret manager
const key = await loadKeyFromKMS();
3. IV/Nonce reuse
// ❌ NGUY HIỂM — cùng IV cho mọi message
const IV = Buffer.alloc(12, 0);
const cipher = createCipheriv("aes-256-gcm", key, IV);
// ✅ ĐÚNG
const iv = randomBytes(12); // random mỗi lần
4. Dùng RSA encrypt data thật
# ❌ NGUY HIỂM (chỉ encrypt được vài trăm byte)
from cryptography.hazmat.primitives.asymmetric import padding
ciphertext = public_key.encrypt(huge_data, padding.OAEP(...))
# ✅ ĐÚNG — hybrid encryption
# 1. Generate random AES key
# 2. Encrypt data bằng AES-GCM
# 3. Encrypt AES key bằng RSA-OAEP
5. Roll your own crypto
Quy tắc: KHÔNG TỰ VIẾT crypto primitive.
Dùng library đã được audit: libsodium, Tink, OpenSSL, BoringSSL, ring (Rust).
6. MD5/SHA-1 cho password hashing
// ❌ NGUY HIỂM — MD5/SHA-1 broken, không slow, không salted
const hash = createHash("md5").update(password).digest("hex");
// ✅ ĐÚNG — password hash chuyên dụng
import { hash, verify } from "@node-rs/argon2";
const hashed = await hash(password); // argon2id, salt tự động
const ok = await verify(hashed, password);
// Hoặc bcrypt, scrypt, PBKDF2
7. Weak random (Math.random, time)
// ❌ NGUY HIỂM — Math.random predictable
const token = Math.random().toString(36);
// ✅ ĐÚNG
import { randomBytes } from "node:crypto";
const token = randomBytes(32).toString("hex");
8. So sánh hash/MAC bằng ==
// ❌ NGUY HIỂM — timing attack
if (computedHmac === providedHmac) { ... }
// ✅ ĐÚNG — constant-time compare
import { timingSafeEqual } from "node:crypto";
if (timingSafeEqual(Buffer.from(computedHmac), Buffer.from(providedHmac))) { ... }
Câu hỏi ôn tập
-
Tại sao TLS 1.3 nhanh hơn TLS 1.2 trong handshake?
Xem đáp án
TLS 1.3 giảm từ 2 RTT xuống 1 RTT trước khi client gửi được application data, và hỗ trợ 0-RTT khi resume session. Lý do: TLS 1.3 loại bỏ negotiation 2 chiều phức tạp — client gửi
key_sharengay trong ClientHello (đoán cipher), server có đủ info để compute key và trả luôn certificate + Finished trong ServerHello. Ngoài ra, TLS 1.3 mã hoá phần lớn handshake (từ EncryptedExtensions), nâng cao privacy. -
Forward secrecy nghĩa là gì, và tại sao TLS 1.3 bắt buộc?
Xem đáp án
Forward secrecy: nếu server long-term private key bị compromise trong tương lai, traffic đã được ghi lại trước đó vẫn không giải mã được. Đạt được bằng cách dùng ephemeral Diffie-Hellman (ECDHE) — mỗi session tạo key pair tạm, vứt đi sau handshake; long-term key chỉ dùng để sign, không encrypt session key.
TLS 1.3 loại bỏ static RSA key exchange (không có forward secrecy) và chỉ cho phép (EC)DHE — vì vậy forward secrecy mặc định bật cho mọi connection.
-
Khi browser nhận được leaf certificate, nó verify như thế nào để quyết định "trusted"?
Xem đáp án
(1) Hostname đang truy cập phải match
Subject Alt Name(SAN) của cert. (2) Thời gian hiện tại nằm giữanotBeforevànotAfter. (3) Chữ ký của intermediate CA trên leaf cert valid. (4) Đi ngược chain: chữ ký của root CA trên intermediate valid. (5) Root CA phải có trong trust store của OS/browser. (6) Kiểm tra revocation qua CRL, OCSP, hoặc OCSP stapling.Nếu một trong các bước fail → browser hiển thị cảnh báo "Not Secure".
-
Vì sao HPKP bị deprecated, và cơ chế nào thay thế nó?
Xem đáp án
HPKP cho phép server pin public key vào browser — browser nhớ pin và từ chối cert nào không match. Vấn đề: (1) pin sai key = tự khoá site khỏi tất cả user đã visit, không revert được trong thời gian
max-age. (2) Attacker chiếm server tạm thời có thể pin key giả → ransomware. (3) Hiệu ứng yếu so với rủi ro vận hành.Thay thế: Certificate Transparency (CT logs — mọi cert public phải log, dễ phát hiện rogue CA) và CAA DNS record (chỉ định CA nào được phép issue cert cho domain). Cả hai đều an toàn hơn HPKP về mặt vận hành.
-
mTLS khác TLS thường ở điểm gì, và khi nào nên dùng?
Xem đáp án
TLS thường chỉ verify identity của server (qua cert). mTLS verify cả client (client cũng có cert, server check chain).
Nên dùng mTLS khi: (1) service-to-service trong microservices/service mesh — auth ở transport layer thay vì share token, (2) B2B API giữa các tổ chức — cert cấp cho từng partner, (3) IoT — thiết bị có cert cá biệt thay vì username/password, (4) Zero Trust — mọi connection nội bộ vẫn phải auth.
Trade-off: phức tạp hơn (cert lifecycle, distribution), không phù hợp cho public-facing user app (user không có client cert).
-
Vì sao thực tế dùng hybrid encryption (asymmetric + symmetric) thay vì chỉ một loại?
Xem đáp án
Symmetric (AES) nhanh, encrypt được data lớn (GB/s), nhưng có vấn đề key distribution — làm sao share key bí mật qua kênh không an toàn? Asymmetric (RSA, ECC) giải quyết key distribution (public key có thể public), nhưng cực chậm và chỉ encrypt được data nhỏ (vài trăm byte).
Hybrid pattern: dùng asymmetric chỉ để trao đổi/encrypt một symmetric key ngắn, sau đó dùng symmetric key đó encrypt data lớn. TLS, PGP, S/MIME, age tool đều dùng pattern này. Envelope encryption (KMS) cũng là một biến thể: KEK trong HSM "asymmetric-like" còn DEK là symmetric.
-
Trong envelope encryption với AWS KMS, vì sao thiết kế này hiệu quả hơn việc gọi
kms:Encrypttrực tiếp lên data?Xem đáp án
(1) Performance: KMS có giới hạn 4 KB/request và rate limit ~10K req/s. Encrypt object 100 MB trực tiếp không khả thi. Với envelope, KMS chỉ gọi 1 lần để encrypt/decrypt DEK (32 bytes), bulk encryption làm bằng AES local.
(2) Cost: KMS request có phí ($0.03/10K), envelope giảm số request KMS đáng kể.
(3) Key rotation: rotate KEK trong KMS chỉ cần re-encrypt các DEK (nhanh), không cần đụng data. Rotate trực tiếp = phải đọc, decrypt, re-encrypt mọi byte data.
(4) Audit: KMS log mỗi
Decrypt→ trace được ai access data, khi nào. -
Sự khác biệt giữa TDE và application-level encryption — và threat nào bảo vệ được/không?
Xem đáp án
TDE (Transparent Data Encryption): DB tự encrypt khi ghi disk, decrypt khi đọc — app không biết. Protect được: disk/snapshot/backup bị steal. KHÔNG protect: app/DBA query thẳng DB sẽ thấy plaintext (DB decrypt on-the-fly).
Application-level: app encrypt data trước khi
INSERT, decrypt sau khiSELECT. Protect: cả DBA và compromised backup đều chỉ thấy ciphertext. Trade-off: query/index trên encrypted field rất khó, latency cao hơn.Quy tắc thường dùng: TDE cho mọi DB (default), application-level thêm cho các column siêu nhạy cảm (SSN, credit card, health record).
-
Tại sao dùng AES-ECB là sai lầm cơ bản, và chế độ nào nên dùng?
Xem đáp án
ECB encrypt mỗi block độc lập, cùng plaintext block → cùng ciphertext block. Hệ quả: pattern trong plaintext (ví dụ vùng pixel màu giống nhau trong ảnh, header lặp lại) lộ ra trong ciphertext. Hình "ECB-encrypted Tux" là minh hoạ kinh điển — vẫn nhận ra hình con chim cánh cụt sau khi "encrypt".
Nên dùng: AES-GCM (AEAD — authenticated encryption với associated data) hoặc ChaCha20-Poly1305 cho mobile. Cả hai có IV/nonce ngẫu nhiên (cùng plaintext → ciphertext khác nhau) và có MAC tag chống tampering. Tránh CBC mode (legacy, dễ bị padding oracle attack nếu implement sai).
-
Vì sao E2EE khác với "encryption at rest + TLS in transit" mà các SaaS thường quảng cáo là "encrypted"?
Xem đáp án
Với encryption at rest + TLS:
- TLS bảo vệ data trên đường truyền giữa client và server
- At-rest encryption bảo vệ data trên disk của server
- Nhưng: server vẫn decrypt data trong memory để xử lý → server có khả năng đọc plaintext bất cứ lúc nào. Nhân viên service provider, government subpoena, hoặc attacker chiếm server đều có thể đọc.
E2EE: data được encrypt tại client (ví dụ Signal app trên điện thoại A), chỉ decrypt tại client nhận (điện thoại B). Server chỉ relay ciphertext, không có khả năng decrypt — kể cả Signal/WhatsApp staff cũng không đọc được.
Trade-off của E2EE: server-side search/preview/spam filtering khó hơn nhiều, password recovery thường yêu cầu device cũ (vì server không có key).
Bài tập thực hành
Phần 1: TLS / HTTPS
# 1. Kiểm tra cert của một site
openssl s_client -connect github.com:443 -servername github.com < /dev/null 2>/dev/null | \
openssl x509 -text -noout | head -30
# 2. Xem cipher suite được dùng
openssl s_client -connect github.com:443 < /dev/null 2>&1 | grep -E "Protocol|Cipher"
# 3. Test với TLS 1.3 only
openssl s_client -connect github.com:443 -tls1_3 < /dev/null
# 4. Generate self-signed cert cho local dev
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem \
-sha256 -days 365 -nodes \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,DNS:127.0.0.1"
# 5. Generate CA + client cert cho mTLS demo
# Tạo CA
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -key ca.key -days 365 -out ca.crt -subj "/CN=My CA"
# Tạo client key + CSR
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr -subj "/CN=client-1"
# CA sign client CSR
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
-out client.crt -days 365
# 6. Setup Caddy với auto-HTTPS (cần domain thật + public IP)
cat > Caddyfile <<EOF
example.com {
reverse_proxy localhost:3000
}
EOF
caddy run
# 7. SSL Labs test (online, comprehensive)
# Mở: https://www.ssllabs.com/ssltest/analyze.html?d=your-site.com
# Mục tiêu: A+ rating
Phần 2: Encryption at Rest & in Transit
# 1. AES-GCM encrypt/decrypt với openssl
echo "Hello secret" | openssl enc -aes-256-gcm -e \
-K $(openssl rand -hex 32) \
-iv $(openssl rand -hex 12) \
-base64
# 2. Generate RSA và ECDSA key pair
openssl genrsa -out rsa-private.pem 3072
openssl rsa -in rsa-private.pem -pubout -out rsa-public.pem
openssl ecparam -name secp256r1 -genkey -noout -out ec-private.pem
openssl ec -in ec-private.pem -pubout -out ec-public.pem
# 3. Encrypt file bằng hybrid (RSA + AES)
openssl rand -out aes.key 32
openssl rand -out aes.iv 16
# Encrypt data
openssl enc -aes-256-cbc -in document.txt -out document.enc \
-K $(xxd -p aes.key | tr -d '\n') \
-iv $(xxd -p aes.iv | tr -d '\n')
# Encrypt AES key bằng RSA
openssl pkeyutl -encrypt -inkey rsa-public.pem -pubin \
-in aes.key -out aes.key.enc
# 4. Demo AWS KMS envelope encryption với CLI
aws kms create-key --description "demo-kek"
KEY_ID=<from output>
# Generate DEK
aws kms generate-data-key --key-id $KEY_ID --key-spec AES_256
# → Plaintext (base64 32 bytes), CiphertextBlob (encrypted DEK)
# 5. Demo password hashing đúng cách
npm install argon2
node -e "
const argon2 = require('argon2');
(async () => {
const hash = await argon2.hash('mypassword', { type: argon2.argon2id });
console.log('Hash:', hash);
console.log('Verify:', await argon2.verify(hash, 'mypassword'));
})();
"
# 6. Check RDS encryption status
aws rds describe-db-instances --query \
'DBInstances[*].[DBInstanceIdentifier,StorageEncrypted,KmsKeyId]'
Tài liệu tham khảo
- TLS 1.3 RFC 8446
- Let's Encrypt — How It Works
- Mozilla SSL Configuration Generator
- SSL Labs SSL Test
- HSTS preload list
- Caddy server
- Istio mTLS
- AWS KMS Concepts — Envelope Encryption
- GCP Cloud KMS
- NIST SP 800-38D — AES GCM
- Signal Protocol specifications
- Cryptography Right Answers (Latacora)
- PostgreSQL pgcrypto