我正在为嵌入式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上遇到了一些错误:

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

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


当前回答

我建议添加-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 req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365

如果您不想用密码保护您的私钥,也可以添加-node(“no DES”的缩写)。否则,它将提示您输入“至少4个字符”的密码。

天数参数(365)可以替换为任何数字以影响到期日期。然后它会提示您输入“国家名称”之类的信息,但您只需按Enter键并接受默认值即可。

添加-sbj'/CN=localhost'以抑制有关证书内容的问题(用所需域替换localhost)。

自签名证书不会通过任何第三方进行验证,除非您以前将其导入浏览器。如果需要更多的安全性,则应使用由证书颁发机构(CA)签名的证书。

快速命令行:最低版本

“我想要一个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

现代浏览器现在如果缺少SAN(Subject Alternate Name),则会为格式良好的自签名证书抛出安全错误。OpenSSL没有提供命令行方式来指定这一点,因此许多开发人员的教程和书签突然过时了。

重新运行的最快方法是一个简短的独立conf文件:

创建OpenSSL配置文件(示例:req.cnf)[要求]distinguished_name=请求分类名称x509_extensions=v3_reqprompt=否[请求分配名称]C=我们ST=VAL=SomeCityO=我的公司OU=我的部门CN=www.company.com[v3_req]keyUsage=关键,数字签名,密钥协议extendedKeyUsage=服务器身份验证subjectAltName=@alt_names[备选名称]DNS.1=www.company.comDNS.2=公司网站DNS.3=公司网创建引用此配置文件的证书openssl req-x509-节点-天730-新密钥rsa:2048\-keyout cert.key-out cert.pem-config req.cnf-sha256

来自的配置示例https://support.citrix.com/article/CTX135602

一个内衬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"

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

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

如果您想使用openssl生成自签名证书,这里有一个我们生成的脚本,可以按原样使用。

#!/bin/bash

subj='//SKIP=skip/C=IN/ST=Country/L=City/O=MyCompany/OU=Technology'
red='\033[31m'        # red
yellow='\033[33m'        # yellow
green='\033[32m'        # green
blue='\033[34m'        # Blue
purple='\033[35m'      # Purple
cyan='\033[36m'        # Cyan
white='\033[37m'       # White


gencerts(){
certname=$1
pkname=$2
alias=$3
$(openssl genrsa -out $pkname'pem.pem' 4096)
$(openssl req -new -sha256 -key $pkname'pem.pem' -out $certname'csr.csr' -subj $subj)
$(openssl x509 -req -sha256 -days 3650 -in $certname'csr.csr' -signkey $pkname'pem.pem' -out $certname'.crt')
$(openssl pkcs12 -export -out $pkname'.p12' -name $alias -inkey $pkname'pem.pem' -in $certname'.crt')
}

verify(){
pkname=$1
keytool -v -list -storetype pkcs12 -keystore $pkname'.p12'
}

echo -e "${purple}WELCOME TO KEY PAIR GENERATOR"
echo -e "${yellow} Please enter the name of the certificate required: "
read certname
echo -e "${green}Please enter the name of the Private Key p12 file required: "
read pkname
echo -e "${cyan}Please enter the ALIAS of the Private Key p12 file : "
read pkalias
echo -e "${white}Please wait while we generate your Key Pair"

gencerts $certname $pkname $pkalias
echo -e "${white}Now lets verify the private key :)"

tput bel     # Play a bell

verify $pkname

请告诉我是否可以对脚本进行任何改进。