ssl - 如何使用 OpenSSL 生成带有 SubjectAltName 的自签名证书?

标签 ssl openssl ssl-certificate x509certificate x509

<分区>

我正在尝试使用 OpenSSL 生成一个自签名证书,其中包含 SubjectAltName。虽然我正在为证书生成 csr,但我猜我必须使用 OpenSSL x509 的 v3 扩展。 我正在使用:

openssl req -new -x509 -v3 -key private.key -out certificate.pem -days 730

谁能帮我弄清楚确切的语法?

最佳答案

Can someone help me with the exact syntax?

这是一个三步过程,涉及修改 openssl.cnf 文件。您也许可以仅使用命令行选项来完成此操作,但我不会那样做。

找到您的 openssl.cnf 文件。它可能位于 /usr/lib/ssl/openssl.cnf:

$ find /usr/lib -name openssl.cnf
/usr/lib/openssl.cnf
/usr/lib/openssh/openssl.cnf
/usr/lib/ssl/openssl.cnf

在我的 Debian系统,/usr/lib/ssl/openssl.cnf 被内置的openssl 程序使用。在最近的 Debian 系统上,它位于 /etc/ssl/openssl.cnf

您可以通过向文件添加伪造的 XXX 来确定正在使用哪个 openssl.cnf 并查看 openssl 是否阻塞。


首先修改req参数。将 alternate_names 部分添加到 openssl.cnf 中,其中包含您要使用的名称。不存在现有的 alternate_names 部分,因此将它添加到何处并不重要。

[ alternate_names ]

DNS.1        = example.com
DNS.2        = www.example.com
DNS.3        = mail.example.com
DNS.4        = ftp.example.com

接下来,将以下内容添加到existing [ v3_ca ] 部分。搜索确切的字符串 [ v3_ca ]:

subjectAltName      = @alternate_names

您可以在 [ v3_ca ] 下将 keyUsage 更改为以下内容:

keyUsage = digitalSignature, keyEncipherment

digitalSignaturekeyEncipherment 是服务器证书的标准费用。不要担心 nonRepudiation。那些想成为律师的计算机科学专业的人/女孩想出了一个毫无用处的东西。这在法律界毫无意义。

最后,IETF ( RFC 5280 ),浏览器和 CA 运行快速且松散,因此您提供的 key 用法可能无关紧要。


其次,修改签名参数。在 CA_default 部分下找到这一行:

# Extension copying option: use with caution.
# copy_extensions = copy

并将其更改为:

# Extension copying option: use with caution.
copy_extensions = copy

这可确保将 SAN 复制到证书中。复制 DNS 名称的其他方法已损坏。


第三,生成您的自签名证书:

$ openssl genrsa -out private.key 3072
$ openssl req -new -x509 -key private.key -sha256 -out certificate.pem -days 730
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
...

最后,检查证书:

$ openssl x509 -in certificate.pem -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 9647297427330319047 (0x85e215e5869042c7)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=US, ST=MD, L=Baltimore, O=Test CA, Limited, CN=Test CA/emailAddress=test@example.com
        Validity
            Not Before: Feb  1 05:23:05 2014 GMT
            Not After : Feb  1 05:23:05 2016 GMT
        Subject: C=US, ST=MD, L=Baltimore, O=Test CA, Limited, CN=Test CA/emailAddress=test@example.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (3072 bit)
                Modulus:
                    00:e2:e9:0e:9a:b8:52:d4:91:cf:ed:33:53:8e:35:
                    ...
                    d6:7d:ed:67:44:c3:65:38:5d:6c:94:e5:98:ab:8c:
                    72:1c:45:92:2c:88:a9:be:0b:f9
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Key Identifier:
                34:66:39:7C:EC:8B:70:80:9E:6F:95:89:DB:B5:B9:B8:D8:F8:AF:A4
            X509v3 Authority Key Identifier:
                keyid:34:66:39:7C:EC:8B:70:80:9E:6F:95:89:DB:B5:B9:B8:D8:F8:AF:A4

            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Key Usage:
                Digital Signature, Non Repudiation, Key Encipherment, Certificate Sign
            X509v3 Subject Alternative Name:
                DNS:example.com, DNS:www.example.com, DNS:mail.example.com, DNS:ftp.example.com
    Signature Algorithm: sha256WithRSAEncryption
         3b:28:fc:e3:b5:43:5a:d2:a0:b8:01:9b:fa:26:47:8e:5c:b7:
         ...
         71:21:b9:1f:fa:30:19:8b:be:d2:19:5a:84:6c:81:82:95:ef:
         8b:0a:bd:65:03:d1

关于ssl - 如何使用 OpenSSL 生成带有 SubjectAltName 的自签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43745846/

相关文章:

java - Spark 应用程序不发送中间/链证书

azure - 为服务结构集群生成客户端证书

java - GCM 服务器推送失败并出现 SSLException (Java)

ssl - 实现 SSL 的正确方法是什么?

c - 如何从 X509 证书中获取 key 使用值?

ssl - WebSphere 无法找到请求目标的有效证书路径

ssl - 将 TLS 与 SIM7080 (SIM7000) AT 命令一起使用。如何处理证书

openssl - 如何使用 TLSv1.1 连接到 mariadb 服务器?

java - 禁用主机名验证

java - 当根证书已存在于 cacert 文件中时,是否需要导入子 CA 证书?