# 第三章:PKI 与证书体系 > "在互联网上,没有人知道你是一条狗。但有了 PKI,至少可以证明你是哪条狗。" ```{mermaid} mindmap root((PKI 与证书体系)) PKI 架构 CA RA 证书链 X.509 证书 字段 扩展 SAN 证书生命周期 申请 签发 续期 吊销 实战 OpenSSL Let's Encrypt mTLS ``` ## 3.1 公钥基础设施(PKI) PKI(Public Key Infrastructure)是一套用于创建、管理、分发、使用、存储和撤销数字证书的系统。它解决了一个核心问题:**如何信任一个公钥确实属于它声称的拥有者?** ### PKI 的核心组件 ``` ┌─────────────────────────────────────────────────────┐ │ PKI 架构 │ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ │ │ 根 CA │───▶│ 中间 CA │───▶│ 终端实体证书 │ │ │ │(Root CA) │ │(Sub CA) │ │(End Entity) │ │ │ │ 离线保管 │ │ 在线签发 │ │ 服务器/客户端│ │ │ └──────────┘ └──────────┘ └──────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ │ │ RA │ │ CRL/OCSP │ │ 证书存储库 │ │ │ │(注册机构) │ │(吊销检查)│ │(Certificate │ │ │ │ 身份验证 │ │ │ │ Repository) │ │ │ └──────────┘ └──────────┘ └──────────────┘ │ └─────────────────────────────────────────────────────┘ ``` | 组件 | 职责 | 说明 | |------|------|------| | CA(Certificate Authority) | 签发和管理证书 | 信任的锚点 | | RA(Registration Authority) | 验证申请者身份 | CA 的代理 | | 证书存储库 | 存储和分发证书 | LDAP、HTTP | | CRL/OCSP | 证书吊销状态查询 | 实时验证证书有效性 | ### 证书链(Chain of Trust) ``` ┌─────────────────┐ │ 根 CA 证书 │ ← 自签名,预装在操作系统/浏览器中 │ (Root CA Cert) │ └────────┬────────┘ │ 签发 ▼ ┌─────────────────┐ │ 中间 CA 证书 │ ← 由根 CA 签发 │ (Intermediate) │ └────────┬────────┘ │ 签发 ▼ ┌─────────────────┐ │ 服务器证书 │ ← 由中间 CA 签发 │ (Server Cert) │ 包含服务器的公钥 └─────────────────┘ 验证过程(从下往上): 1. 客户端收到服务器证书 2. 用中间 CA 的公钥验证服务器证书的签名 3. 用根 CA 的公钥验证中间 CA 证书的签名 4. 根 CA 在本地信任库中 → 信任链建立 ✅ ``` ## 3.2 X.509 证书格式 X.509 是最广泛使用的数字证书标准(ITU-T X.509 / RFC 5280)。 ### 证书核心字段 ``` Certificate: Data: Version: 3 (0x2) Serial Number: 04:00:00:00:00:01:15:4b:5a:c3:94 Signature Algorithm: sha256WithRSAEncryption Issuer: C=US, O=Let's Encrypt, CN=R3 Validity: Not Before: Jan 1 00:00:00 2024 GMT Not After : Apr 1 00:00:00 2024 GMT Subject: CN=example.com Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) X509v3 extensions: X509v3 Subject Alternative Name: DNS:example.com, DNS:*.example.com X509v3 Key Usage: critical Digital Signature, Key Encipherment X509v3 Extended Key Usage: TLS Web Server Authentication X509v3 Basic Constraints: critical CA:FALSE Authority Information Access: OCSP - URI:http://r3.o.lencr.org CA Issuers - URI:http://r3.i.lencr.org/ Signature Algorithm: sha256WithRSAEncryption [签名数据] ``` ### Subject Alternative Name(SAN) SAN 扩展允许一个证书绑定多个身份: ``` X509v3 Subject Alternative Name: DNS:example.com # 域名 DNS:*.example.com # 通配符域名 IP Address:192.168.1.1 # IP 地址 URI:spiffe://example.org/web-server # SPIFFE ID email:admin@example.com # 邮箱 ``` ## 3.3 证书生命周期 ### 证书申请与签发 ```bash # 1. 生成私钥 openssl genpkey -algorithm RSA -out server.key -pkeyopt rsa_keygen_bits:2048 # 2. 生成证书签名请求(CSR) openssl req -new -key server.key -out server.csr \ -subj "/C=CN/ST=Anhui/L=Hefei/O=MyOrg/CN=example.com" # 3. 查看 CSR 内容 openssl req -in server.csr -text -noout # 4. CA 签发证书(使用 CA 的私钥和证书) openssl x509 -req -in server.csr \ -CA ca.crt -CAkey ca.key -CAcreateserial \ -out server.crt -days 365 -sha256 \ -extfile <(cat <