How can I generate a self-signed certificate with SubjectAltName using OpenSSL?
我正在尝试使用带有SubjectAltName的OpenSSL生成自签名证书。虽然我正在为证书生成csr,但我猜想我必须使用OpenSSL x509的v3扩展名。
我在用 :
1 | openssl req -new -x509 -v3 -key private.key -out certificate.pem -days 730 |
有人可以为我提供确切的语法吗?
Can someone help me with the exact syntax?
这是一个三步过程,涉及修改
查找您的
1 2 3 4 | $ find /usr/lib -name openssl.cnf /usr/lib/openssl.cnf /usr/lib/openssh/openssl.cnf /usr/lib/ssl/openssl.cnf |
在我的Debian系统上,内置的
您可以通过在文件中添加一个虚假的
首先,修改
1 2 3 4 5 6 | [ alternate_names ] DNS.1 = example.com DNS.2 = www.example.com DNS.3 = mail.example.com DNS.4 = ftp.example.com |
接下来,将以下内容添加到现有的
1 | subjectAltName = @alternate_names |
您可以在
1 | keyUsage = digitalSignature, keyEncipherment |
最后,IETF(RFC 5280),浏览器和CA可以快速而轻松地运行,因此您提供何种密钥用法可能并不重要。
其次,修改签名参数。在
1 2 | # Extension copying option: use with caution. # copy_extensions = copy |
并将其更改为:
1 2 | # Extension copying option: use with caution. copy_extensions = copy |
这样可以确保将SAN复制到证书中。复制dns名称的其他方法已损坏。
第三,生成您的自签名:
1 2 3 4 5 6 | $ 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. ... |
最后,检查证书:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | $ 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/[email protected] 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/[email protected] 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 |