国密 SM2 算法是中国国家密码管理局发布的椭圆曲线公钥密码算法,相比传统的 RSA 和国际通用 ECDSA 曲线,SM2 在国内有着特殊的应用场景和政策要求。本文详细介绍 SM2 证书的生成、验证和部署。

SM2 证书与 RSA/ECDSA 证书的区别

算法基础

特性 RSA ECDSA (secp256r1) SM2
密钥长度 2048/4096 bit 256 bit 256 bit
曲线类型 离散对数 椭圆曲线 椭圆曲线 (SM2 曲线)
签名算法 RSA + SHA-256 ECDSA + SHA-256 SM2 + SM3
国际认可 广泛认可 广泛认可 中国国密标准
性能 较慢 较快 与 secp256r1 相当

关键差异

  1. 签名算法:SM2 证书使用 SM3 哈希算法进行签名,而不是 SHA-256
  2. 曲线参数:SM2 使用特定的椭圆曲线参数(国家密码管理局指定)
  3. 兼容性:国际浏览器和设备默认不支持 SM2 证书,需要国密浏览器或支持国密的组件

生成 SM2 证书

环境要求

确保系统安装的是 OpenSSL 3.0+ 或 GmSSL:

1
2
3
4
5
# 检查 OpenSSL 版本
openssl version

# 检查是否支持 SM2 曲线
openssl ecparam -list_curves | grep SM2

注意:本文使用 OpenSSL 3.0+ 演示,GmSSL 命令类似但参数可能略有不同。

步骤一:生成 SM2 私钥

1
2
3
4
5
6
7
# 生成 SM2 私钥
openssl genpkey -algorithm EC \
    -pkeyopt ec_paramgen_curve:SM2 \
    -out sm2_private.key

# 验证私钥
openssl ec -in sm2_private.key -text -noout

输出示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Private-Key: (256 bit)
priv:
    00:8a:cb:3d:f5:67:89:ab:cd:ef:12:34:56:78:90:
    ab:cd:ef:12:34:56:78:90:ab:cd:ef:12:34:56:78
pub:
    04:4f:3f:f7:5c:4a:7a:36:c7:8a:9f:bb:7b:7b:3d:
    b2:8c:8e:c7:89:d1:74:64:9a:21:51:38:2a:0b:82:
    64:c5:c8:e3:31:d8:f8:86:cf:ad:50:38:b8:49:96:
    dd:77:6c:6e:9f:43:4d:12:49:37:28:b1:16:56:16:
    60:84:21:42:21
ASN1 OID: SM2

步骤二:生成自签名证书(测试用)

1
2
3
4
openssl req -new -x509 -days 365 \
    -key sm2_private.key \
    -out sm2_certificate.crt \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=Example Inc/CN=sm2.example.com"

步骤三:生成 CSR(生产环境推荐)

1
2
3
4
5
6
7
8
# 生成 CSR
openssl req -new \
    -key sm2_private.key \
    -out sm2.csr \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=Example Inc/CN=example.com"

# 查看 CSR 内容
openssl req -in sm2.csr -text -noout

步骤四:使用 CA 签名(生产环境)

1
2
3
4
5
6
7
# 假设已有 CA 私钥和证书
openssl x509 -req -days 365 \
    -in sm2.csr \
    -CA ca.crt \
    -CAkey ca.key \
    -CAcreateserial \
    -out sm2_certificate.crt

验证 SM2 证书

查看证书基本信息

1
2
3
4
5
# 查看证书基本信息
openssl x509 -in sm2_certificate.crt -noout -subject -issuer -dates

# 完整证书内容
openssl x509 -in sm2_certificate.crt -noout -text

关键验证点:

  1. 签名算法:应为 SM2-with-SM3
  2. 公钥算法:应为 id-ecPublicKey
  3. ASN1 OID:应为 SM2
  4. 公钥长度:256 bit

验证证书链

1
2
3
4
5
# 验证自签名证书
openssl verify -CAfile sm2_certificate.crt sm2_certificate.crt

# 验证带 CA 的证书链
openssl verify -CAfile ca.crt sm2_certificate.crt

导出公钥并验证

1
2
3
4
5
6
# 从证书提取公钥
openssl x509 -in sm2_certificate.crt -pubkey -noout > sm2_public.pem

# 验证私钥与公钥匹配
openssl ec -in sm2_private.key -pubout -out pub_from_key.pem
diff sm2_public.pem pub_from_key.pem && echo "匹配成功"

在 Nginx 中部署

注意:标准 Nginx 不支持 SM2 证书,需要使用 GmSSL 编译的 Nginx 或支持国密的 Web 服务器。

使用 GmSSL 编译 Nginx

1
2
3
4
5
6
7
8
9
# 安装 GmSSL(如果系统没有)
# 详细安装步骤请参考 GmSSL 官方文档

# 编译支持 SM2 的 Nginx
./configure --prefix=/usr/local/nginx \
    --with-openssl=/path/to/gmopenssl \
    --with-http_ssl_module

make && make install

Nginx 配置示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
server {
    listen 443 ssl;
    server_name sm2.example.com;

    # SM2 证书配置(使用 GmSSL 编译的 Nginx)
    ssl_certificate sm2_certificate.crt;
    ssl_certificate_key sm2_private.key;

    # 国密套件配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-SM2-SM4-GCM:ECDHE-SM4-SM3;
    ssl_prefer_server_ciphers on;

    # 其他配置...
    location / {
        root /var/www/html;
        index index.html;
    }
}

同时支持国际算法和国密算法(双证书方案)

很多国内站点采用双证书方案,同时支持国际浏览器和国密浏览器:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
    listen 443 ssl;
    server_name example.com;

    # 国际算法证书(RSA/ECDSA)
    ssl_certificate rsa_certificate.crt;
    ssl_certificate_key rsa_private.key;

    # 国密算法证书(SM2)
    gm_ssl_certificate sm2_certificate.crt;
    gm_ssl_certificate_key sm2_private.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    
    # 双证书套件
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-SM2-SM4-GCM-SHA384;
    ssl_prefer_server_ciphers on;

    location / {
        root /var/www/html;
        index index.html;
    }
}

适用场景与注意事项

适用场景

  1. 政府机关和国有企业:根据国家密码管理局要求
  2. 金融行业:银行、证券、保险等对安全性要求高的机构
  3. 政务系统:政府门户网站、政务服务平台
  4. 合规需求:需要通过等保测评的系统

注意事项

  1. 浏览器兼容性:普通浏览器不支持 SM2,需要使用国密浏览器(如 360 安全浏览器、奇安信浏览器等)
  2. 服务器支持:标准 Nginx/Apache 不支持,需使用 GmSSL 编译的版本
  3. 证书链验证:国密浏览器有内置根证书,私有 CA 需要配置信任
  4. 算法替代:如果不需要强制国密,可以考虑使用 ECDSA (secp256r1) 作为替代,性能更好且兼容性更广

总结

SM2 证书是国内特定场景下的重要选择,掌握其生成和部署对国内安全从业者很有价值。实际部署时需要考虑:

  • 使用 OpenSSL 3.0+ 或 GmSSL 生成证书
  • 确认服务器软件支持国密算法
  • 考虑双证书方案以兼顾兼容性
  • 妥善保管私钥,使用密码保护

如果项目没有强制国密要求,建议使用 ECDSA (secp256r1) 证书,在保证安全性的同时获得更好的兼容性。


参考来源

  • OpenSSL 官方文档:https://www.openssl.org/docs/
  • GmSSL 项目:https://gmssl.org/
  • GB/T 32918-2016 SM2 椭圆曲线公钥密码算法