Red Hat Training

A Red Hat training course is available for RHEL 8

3.4. 使用 OpenSSL 为 TLS 客户端证书创建私钥和 CSR

只有在来自证书颁发机构(CA)的有效 TLS 证书时才可以使用 TLS 加密通信频道。要获取证书,您必须首先为您的客户端创建私钥和证书签名请求(CSR)。

流程

  1. 在客户端系统中生成私钥,例如:

    $ openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:P-256 -out <client-private.key>
  2. 可选:使用您选择的文本编辑器准备一个简化创建 CSR 的配置文件,例如:

    $ vim <example_client.cnf>
    [client-cert]
    keyUsage = critical, digitalSignature, keyEncipherment
    extendedKeyUsage = clientAuth
    subjectAltName = @alt_name
    
    [req]
    distinguished_name = dn
    prompt = no
    
    [dn]
    CN = <client.example.com>
    
    [clnt_alt_name]
    email= <client@example.com>

    extendedKeyUsage = clientAuth 选项限制证书的使用。

  3. 使用之前创建的私钥创建 CSR:

    $ openssl req -key <client-private.key> -config <example_client.cnf> -new -out <client-cert.csr>

    如果省略 -config 选项,req 工具会提示您输入更多信息,例如:

    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    …
    Common Name (eg, your name or your server's hostname) []: <client.example.com>
    Email Address []: <client@example.com>

后续步骤

验证

  1. 检查证书人类可读的部分是否与您的要求匹配,例如:

    $ openssl x509 -text -noout -in <client-cert.crt>
    Certificate:
    …
                X509v3 Extended Key Usage:
                    TLS Web Client Authentication
                X509v3 Subject Alternative Name:
                    email:client@example.com
    …

其它资源

  • openssl (1), x509 (1), genpkey (1), req (1), 和 config (5) man page