爱折腾的孩纸

使用 OpenSSL 制作自签名 ECC 证书

一般步骤

  • 制作私钥
    openssl ecparam -out {KeyFile.key} -name prime256v1 -genkey

  • 制作证书请求
    openssl req -key {KeyFile.key} -new -out {ReqFile.req}
    如果事先做了一个配置,可以在这里使用 -config {ConfigFile.cnf} 参数载入配置

  • 制作自签名根证书
    openssl x509 -req -in {ReqFile.req} -signkey {KeyFile.key} -out {PEMFile.pem} -days 365
    可通过 -days 参数设置证书过期时间

  • 使用根证书签名
    openssl x509 -req -in {ReqFile.req} -CA {CA_PEMFile.pem} -CAkey {CA_KeyFile.key} -out {PEMFile.pem} -CAcreateserial -days 365

制作证书请求时会询问一些配置,其中最重要的是 CommonName,主要使用这个字段判断证书所有者身份

实例

  1. 制作自签名根证书,假设根证书我们都命名为 CA.*

    a. 制作私钥
    openssl ecparam -out CA.key -name prime256v1 -genkey
    b. 制作请求
    openssl req -key CA.key -new -out CA.req
    为了方便记忆,CommonName 我们设置为 ca.system.zhujinliang.com
    c. 制作自签名证书
    openssl x509 -req -in CA.req -signkey CA.key -out CA.pem -days 365

    CA.pem 就是我们的根证书,CA.key 是私钥,私钥一定要安全保存,一方面签名证书时需要用到私钥,另一方面,私钥丢失就意味着可以被恶意签发证书

  2. 制作服务器证书,假设服务器证书命名为 sv1.*

    a. 制作私钥
    openssl ecparam -out sv1.key -name prime256v1 -genkey
    b. 制作请求
    openssl req -key sv1.key -new -out sv1.req
    这里 CommonName 我们设置为 sv1.zhujinliang.com
    c. 签名证书
    openssl x509 -req -in sv1.req -CA CA.pem -CAkey CA.key -out sv1.pem -CAcreateserial -days 365

    sv1.pem 就是服务器证书,sv1.key 是证书的私钥

    签发时会生成 CA.srl 文件,这个是最后一次颁发的证书的序列号,之后生成证书会需要这个文件

  3. 制作客户端证书,制作过程与制作服务器证书相同,这里假设客户端证书命名为 liang.*

    a. 制作私钥
    openssl ecparam -out liang.key -name prime256v1 -genkey
    b. 制作请求
    openssl req -key liang.key -new -out liang.req
    这里 CommonName 我们设置为 liang
    c. 签名证书
    openssl x509 -req -in liang.req -CA CA.pem -CAkey CA.key -out liang.pem -CAcreateserial -days 365

    liang.pem 就是服务器证书,liang.key 是证书的私钥

查看证书信息

可以通过命令 openssl x509 -in CA.pem -text 查看证书信息。

评论