证书

概述

商密证书指的是满足《SM2椭圆曲线公钥密码算法》以及《基于SM2密码算法的数字证书格式规范》等标准的数字证书。openEuler发布的openSSL软件提供了对商密证书的支持。

前置条件

openSSL大于或等于1.1.1m-6版本:

bash
$ rpm -qa openssl
openssl-1.1.1m-6.oe2209.x86_64

如何使用

场景1:生成商密证书

  1. 生成SM2签名私钥:

    bash
    $ openssl ecparam -genkey -name SM2 -out sm2.key
  2. 生成签名请求:

    bash
    $ openssl req -new -sm3 -key sm2.key -out sm2.csr
  3. 生成商密证书(可使用-extfile指定证书配置文件):

    bash
    $ openssl x509 -req -days 3650 -signkey sm2.key -in sm2.csr -out sm2.crt
  4. 查看证书信息:

    bash
    $ openssl x509 -text -in sm2.crt

场景2:构建证书链

使用x509命令(一般用于功能测试)

  1. 生成CA私钥和证书:

    bash
    $ openssl ecparam -genkey -name SM2 -out ca.key
    $ openssl req -new -sm3 -key ca.key -out ca.csr
    $ openssl x509 -req -days 3650 -signkey ca.key -in ca.csr -out ca.crt
  2. 生成二级签名私钥和签名请求:

    bash
    $ openssl ecparam -genkey -name SM2 -out sm2.key
    $ openssl req -new -sm3 -key sm2.key -out sm2.csr
  3. 基于一级证书生成二级证书(可使用-extfile指定证书配置文件):

    bash
    $ openssl x509 -req -sm3 -CAcreateserial -CA ca.crt -CAkey ca.key -in sm2.csr -out sm2.crt

使用ca配置文件(一般用于正式场景)

  1. 准备用于生成证书的配置文件(可使用openssl源码目录下的apps/openssl.cnf);

  2. 生成自签名CA证书(下列命令为使用openSSL 1.1.1版本的场景,如使用openSSL 3.0.0以上的版本,openssl req命令中的*-newkey参数需要替换为sm2:SM2.pem*):

    bash
    $ openssl ecparam -name SM2 -out SM2.pem
    $ openssl req -config ./openssl.cnf -nodes -keyout CA.key -newkey ec:SM2.pem -new -out CA.csr
    $ openssl x509 -sm3 -req -days 30 -in CA.csr -extfile ./openssl.cnf -extensions v3_ca -signkey CA.key -out CA.crt
  3. 生成二级证书(下列命令为使用openSSL 1.1.1版本的场景,如使用openSSL 3.0.9及以上的版本,openssl req命令中的*-newkey参数需要替换为sm2:SM2.pem*):

    bash
    $ openssl req -config ./openssl.cnf -nodes -keyout SS.key -newkey ec:SM2.pem -new -out SS.csr
    $ openssl x509 -sm3 -req -days 30 -in SS.csr -CA CA.crt -CAkey CA.key -extfile ./openssl.cnf -extensions v3_req -out SS.crt -CAcreateserial

场景3:生成TLCP通信证书

详见《TLCP协议栈》章节。