我正在为嵌入式Linux设备添加HTTPS支持。我已尝试使用以下步骤生成自签名证书:

openssl req -new > cert.csr
openssl rsa -in privkey.pem -out key.pem
openssl x509 -in cert.csr -out cert.pem -req -signkey key.pem -days 1001
cat key.pem>>cert.pem

这是可行的,但我在Google Chrome上遇到了一些错误:

这可能不是你要找的网站!站点的安全证书不受信任!

我错过了什么吗?这是构建自签名证书的正确方法吗?


当前回答

快速命令行:最低版本

“我想要一个pfx格式的自签名证书,用于www.example.com,且不会引起太大的麻烦”:

openssl req -x509 -sha256 -days 365 -nodes -out cert.crt -keyout cert.key -subj "/CN=www.example.com"
openssl pkcs12 -export -out cert.pfx -inkey cert.key -in cert.crt

其他回答

我建议添加-sha256参数,以使用SHA-2哈希算法,因为主要浏览器正在考虑将“SHA-1证书”显示为不安全。

来自已接受答案的同一命令行-@diegows,添加了-sha256

openssl req-x509-sha256-newkey rsa:2048-keyout key.pem-out cert.pem-天XXX

更多信息请访问谷歌安全博客。

2018年5月更新。正如许多人在评论中指出的那样,使用SHA-2不会为自签名证书添加任何安全性。但我仍然建议使用它作为一个好习惯,不要使用过时/不安全的加密哈希函数。在“为什么最终实体证书之上的证书可以基于SHA-1?”中提供了完整的解释?。

openssl允许通过单个命令(-newkey)生成自签名证书指示生成私钥,-x509指示发出自签名证书而不是签名请求)::

openssl req -x509 -newkey rsa:4096 \
-keyout my.key -passout pass:123456 -out my.crt \
-days 365 \
-subj /CN=localhost/O=home/C=US/emailAddress=me@mail.internal \
-addext "subjectAltName = DNS:localhost,DNS:web.internal,email:me@mail.internal" \
-addext keyUsage=digitalSignature -addext extendedKeyUsage=serverAuth

您可以在单独的步骤中生成私钥和构造自签名证书:

openssl genrsa -out my.key -passout pass:123456 2048

openssl req -x509 \
-key my.key -passin pass:123456 -out my.csr \
-days 3650 \
-subj /CN=localhost/O=home/C=US/emailAddress=me@mail.internal \
-addext "subjectAltName = DNS:localhost,DNS:web.internal,email:me@mail.internal" \
-addext keyUsage=digitalSignature -addext extendedKeyUsage=serverAuth

查看生成的证书:

openssl x509 -text -noout -in my.crt

Java keytool创建PKCS#12存储::

keytool -genkeypair -keystore my.p12 -alias master \
-storetype pkcs12 -keyalg RSA -keysize 2048 -validity 3650 \
-storepass 123456 \
-dname "CN=localhost,O=home,C=US" \
-ext 'san=dns:localhost,dns:web.internal,email:me@mail.internal'

要导出自签名证书,请执行以下操作:

keytool -exportcert -keystore my.p12 -file my.crt \
-alias master -rfc -storepass 123456

查看生成的证书:

keytool -printcert -file my.crt

GnuTLS的certtool不允许从CLI传递不同的属性。我不喜欢乱搞配置文件((

以下是@diegows的答案中描述的选项,在文档中有更详细的描述:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX

请求PKCS#10证书请求和证书生成实用程序。-2009年5月此选项输出自签名证书,而不是证书请求。这通常用于生成测试证书或自签名根CA。-新密钥参数此选项创建新的证书请求和新的私钥。争论采取多种形式之一。rsa:nbits,其中nbits是位数,生成大小为n位的RSA密钥。-键入文件名这提供了用于写入新创建的私钥的文件名。-输出文件名默认情况下,指定要写入的输出文件名或标准输出。-第n天当使用-x509选项时,指定要验证的天数的证书。默认值为30天。-节点如果指定了此选项,则如果创建了私钥,则不会对其进行加密。

文件实际上比上述更详细;我只是在这里总结了一下。

一个内衬FTW。我喜欢保持简单。为什么不使用一个包含所有所需参数的命令?这就是我喜欢的方式-这将创建一个x509证书及其PEM密钥:

openssl req -x509 \
 -nodes -days 365 -newkey rsa:4096 \
 -keyout self.key.pem \
 -out self-x509.crt \
 -subj "/C=US/ST=WA/L=Seattle/CN=example.com/emailAddress=someEmail@gmail.com"

该命令包含通常为证书详细信息提供的所有答案。通过这种方式,您可以设置参数并运行命令,获取输出,然后喝咖啡。

>>更多信息请点击此处<<

快速命令行:最低版本

“我想要一个pfx格式的自签名证书,用于www.example.com,且不会引起太大的麻烦”:

openssl req -x509 -sha256 -days 365 -nodes -out cert.crt -keyout cert.key -subj "/CN=www.example.com"
openssl pkcs12 -export -out cert.pfx -inkey cert.key -in cert.crt