X.509 证书的核心功能由扩展(Extensions)机制实现。扩展允许在证书中添加额外信息,定义证书的用途、约束、身份验证方式等关键属性。本文详细介绍常见扩展的作用和使用方法。
什么是证书扩展#
X.509 v3 引入的扩展机制是现代 PKI 的基础。每个扩展由 OID(对象标识符)、关键性标志和扩展值组成。
扩展分为两类:
- 关键扩展(Critical):证书使用者必须理解和处理的扩展,否则证书验证失败
- 非关键扩展(Non-critical):可选的扩展,可被忽略但不应影响验证
常见扩展详解#
1. Basic Constraints(基本约束)#
定义证书是否为 CA(证书颁发机构)以及证书链深度。
1
2
3
4
5
6
7
8
|
# CA 证书
basicConstraints=CA:TRUE
# 终端实体证书
basicConstraints=CA:FALSE
# 无限制的 CA 路径长度
basicConstraints=CA:TRUE,pathlen:10
|
实际输出:
1
2
|
X509v3 Basic Constraints:
CA:TRUE
|
2. Key Usage(密钥用法)#
指定证书密钥的允许用途。
1
2
3
4
5
6
7
8
|
# 服务器认证证书
keyUsage=digitalSignature,keyEncipherment
# CA 签名证书
keyUsage=digitalSignature,keyCertSign,cRLSign
# 代码签名证书
keyUsage=digitalSignature,nonRepudiation
|
常见值:
digitalSignature:数字签名
keyEncipherment:密钥加密
dataEncipherment:数据加密
keyCertSign:证书签名
cRLSign:CRL 签名
3. Extended Key Usage(扩展密钥用法)#
进一步细化证书用途,主要用于 TLS/SSL。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 服务器认证
extendedKeyUsage=serverAuth
# 客户端认证
extendedKeyUsage=clientAuth
# 代码签名
extendedKeyUsage=codeSigning
# 电子邮件保护
extendedKeyUsage=emailProtection
# 时间戳服务
extendedKeyUsage=timeStamping
# 同时支持服务器和客户端认证
extendedKeyUsage=serverAuth,clientAuth
|
4. Subject Alternative Name(主题备用名称)#
允许证书支持多个域名或 IP 地址,是现代 HTTPS 的必备扩展。
1
2
3
4
5
6
7
8
|
# 多个域名
subjectAltName=DNS:example.com,DNS:www.example.com,DNS:api.example.com
# 域名 + IP
subjectAltName=DNS:example.com,IP:192.168.1.1
# 邮箱
subjectAltName=email:admin@example.com
|
实际输出:
1
2
|
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com, IP Address:192.168.1.1
|
5. Subject Key Identifier(主题密钥标识符)#
唯一标识证书持有者的公钥,用于证书链构建。
1
2
3
4
5
|
# 自动生成
subjectKeyIdentifier=hash
# 指定标识符
subjectKeyIdentifier=80:84:1B:F2:00:C4:3A:A6:76:AC:AA:71:60:BA:C1:03:DB:9B:DB:10
|
6. Authority Key Identifier(颁发机构密钥标识符)#
标识颁发证书的 CA 公钥,帮助构建证书链。
1
2
3
4
5
|
# 从颁发者证书自动提取
authorityKeyIdentifier=keyid:always
# 同时包含颁发者名称
authorityKeyIdentifier=keyid,issuer
|
7. CRL Distribution Points(CRL 分发点)#
指定证书吊销列表的获取位置。
1
|
crlDistributionPoints=URI:http://crl.example.com/ca.crl
|
提供 OCSP 响应器地址和颁发者证书下载位置。
1
2
3
4
5
6
7
8
|
# OCSP 位置
authorityInfoAccess=OCSP;URI:http://ocsp.example.com
# 颁发者证书位置
authorityInfoAccess=caIssuers;URI:http://crt.example.com/ca.crt
# 两者结合
authorityInfoAccess=OCSP;URI:http://ocsp.example.com,caIssuers;URI:http://crt.example.com/ca.crt
|
完整示例:创建带扩展的证书#
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 1. 创建私钥
openssl genrsa -out server.key 2048
# 2. 创建 CSR(可选,使用已有配置)
openssl req -new -key server.key -out server.csr \
-subj "/CN=example.com/O=Example Inc/C=US"
# 3. 使用扩展配置文件签发证书
openssl x509 -req -in server.csr \
-CA ca.crt -CAkey ca.key \
-CAcreateserial \
-out server.crt -days 365 \
-extfile openssl.cnf -extensions server_ext
|
openssl.cnf 配置示例:
1
2
3
4
5
6
7
|
[server_ext]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = DNS:example.com,DNS:www.example.com
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
|
查看证书扩展#
1
2
3
4
5
6
7
8
|
# 查看所有扩展
openssl x509 -in server.crt -noout -text
# 查看特定扩展
openssl x509 -in server.crt -noout -ext subjectAltName
# 查看扩展 OID
openssl x509 -in server.crt -noout -exts
|
常见问题#
SAN 和 Common Name 的区别#
自 2017 年起,主流浏览器已废弃 Common Name 验证,仅支持 SAN。确保为所有域名添加 SAN 条目。
关键扩展未识别会怎样#
如果证书包含关键扩展但验证方不理解,验证将失败。非关键扩展可被安全忽略。
自签名证书的扩展#
自签名证书的 authorityKeyIdentifier 应与 subjectKeyIdentifier 相同:
1
2
|
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
|
| 扩展 |
作用 |
建议 |
| Basic Constraints |
标识 CA 或终端实体 |
终端实体设为 CA:FALSE |
| Key Usage |
定义密钥用途 |
根据实际用途选择 |
| Extended Key Usage |
细化 TLS/SSL 用途 |
HTTPS 必须包含 serverAuth |
| Subject Alternative Name |
多域名/多 IP 支持 |
HTTPS 必须包含 |
| Subject Key Identifier |
公钥唯一标识 |
建议添加 |
| Authority Key Identifier |
证书链构建 |
建议添加 |
正确使用扩展是构建安全可靠 PKI 系统的基础。生产环境中务必根据实际需求配置每个扩展,避免过度宽松的安全设置。
参考来源: