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

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

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


当前回答

2017年一艘班轮:

openssl req \
-newkey rsa:2048 \
-x509 \
-nodes \
-keyout server.pem \
-new \
-out server.pem \
-subj /CN=localhost \
-reqexts SAN \
-extensions SAN \
-config <(cat /System/Library/OpenSSL/openssl.cnf \
    <(printf '[SAN]\nsubjectAltName=DNS:localhost')) \
-sha256 \
-days 3650

这也适用于Chrome 57,因为它提供了SAN,而无需另一个配置文件。它取自这里的一个答案。

这将创建一个包含私钥和证书的.pem文件。如果需要,可以将它们移动到单独的.pem。

其他回答

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

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

从2023年起,OpenSSL≥1.1.1,以下命令可满足您的所有需求,包括主题备用名称(SAN):

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \
  -keyout example.key -out example.crt -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1"

在OpenSSL≤1.1.0的旧系统上,如Debian≤9或CentOS≤7,需要使用此命令的更长版本:

openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \
  -keyout example.key -out example.crt -extensions san -config \
  <(echo "[req]"; 
    echo distinguished_name=req; 
    echo "[san]"; 
    echo subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1
    ) \
  -subj "/CN=example.com"

任一命令都创建一个证书

对于(子)域example.com和www.example.net(SAN)有效,对于IP地址10.0.0.1(SAN)也是有效的,相对强劲(截至2023年)有效期为3650天(约10年)。

将生成以下文件:

私钥:example.key证书:example.crt

所有信息都在命令行中提供。没有让你烦恼的互动输入。没有你必须处理的配置文件。所有必要的步骤都由单个OpenSSL调用执行:从私钥生成到自签名证书。


备注#1:加密参数

由于证书是自签名的,需要用户手动接受,因此使用短到期或弱加密是没有意义的。

未来,您可能希望使用超过4096位的RSA密钥和比sha256更强的哈希算法,但截至2023年,这些都是正常的值。它们足够强大,同时受到所有现代浏览器的支持。

备注#2:参数“-nodes”

理论上,您可以省略-nodes参数(这意味着“没有DES加密”),在这种情况下,example.key将使用密码进行加密。然而,这对于服务器安装几乎从来都没有用处,因为您要么必须将密码也存储在服务器上,要么必须在每次重新启动时手动输入密码。

备注#3:另请参见

直接在命令行上向openssl提供subjectAltName如何通过命令行向SSL证书添加多个电子邮件地址?有关MSYS_NO_PATHCONV的更多信息

2017年一艘班轮:

openssl req \
-newkey rsa:2048 \
-x509 \
-nodes \
-keyout server.pem \
-new \
-out server.pem \
-subj /CN=localhost \
-reqexts SAN \
-extensions SAN \
-config <(cat /System/Library/OpenSSL/openssl.cnf \
    <(printf '[SAN]\nsubjectAltName=DNS:localhost')) \
-sha256 \
-days 3650

这也适用于Chrome 57,因为它提供了SAN,而无需另一个配置文件。它取自这里的一个答案。

这将创建一个包含私钥和证书的.pem文件。如果需要,可以将它们移动到单独的.pem。

生成密钥我将/etc/mysql用于证书存储,因为/etc/appamor.d/usr.sbin.mysqld包含/etc/mysql/*.pem r。苏多苏-cd/etc/mysqlopenssl genrsa-out ca-key.pem 2048;openssl req-new-x509-nodes-days 1000-key ca-key.pem-out ca-cert.pem;openssl req-newkey rsa:2048-天1000-节点-keyout server-key.pem-out server-req.pem;openssl x509-req-in server-req.pem-天1000-CA CA-cert.pem-CA密钥CA-key.pem-set_serial 01-out server-cert.pum;openssl req-newkey rsa:2048-天1000-nodes-keyout client-key.pem-out client-req.pem;openssl x509-req-in client-req.pem-days 1000-CA CA-cert.pem-CA密钥CA-key.pem-set_serial 01-out client-cert.pum;添加配置/etc/mysql/my.cnf[客户]ssl-ca=/etc/mysql/ca-cert.pemssl证书=/etc/mysql/client-cert.pemssl密钥=/etc/mysql/client-key.pem[mysqld]ssl-ca=/etc/mysql/ca-cert.pemssl证书=/etc/mysql/server-cert.pemssl密钥=/etc/mysql/server-key.pem

在我的设置中,Ubuntu服务器登录到:/var/log/mysql/error.log

后续注意事项:

SSL错误:无法从“…”获取证书如果MySQL不在appmors配置中,则可能会拒绝对证书文件的读取访问。如前面步骤^中所述,将所有证书保存为/etc/mysql/目录中的.pem文件,appamor默认批准该目录(或修改appamor/SELinux以允许访问存储它们的任何位置)SSL错误:无法获取私钥您的MySQL服务器版本可能不支持默认的rsa:2048格式将生成的rsa:2048转换为纯rsa:openssl rsa-in server-key.pem-out server-key.phpopenssl rsa-in client-key.pem-out client-key.php检查本地服务器是否支持SSL:mysql-u根-pmysql>显示变量,如“%ssl%”;+---------------+----------------------------+|变量名称|值|+---------------+----------------------------+|have_openssl|是||have_ssl|是||ssl-ca|/etc/mysql/ca-cert.pem||ssl_容量|||ssl_cert |/etc/mysql/server-cert.pem||ssl_密码|||ssl-key |/etc/mysql/server-key.pem|+---------------+----------------------------+验证与数据库的连接是否经过SSL加密:正在验证连接登录到MySQL实例时,可以发出查询:显示状态,如“Ssl_cypher”;如果您的连接未加密,结果将为空:mysql>显示状态,如“Ssl_cypher”;+---------------+-------+|变量名称|值|+---------------+-------+|Ssl_密码||+---------------+-------+一组1行(0.00秒)否则,它将显示正在使用的密码的非零长度字符串:mysql>显示状态,如“Ssl_cypher”;+---------------+--------------------+|变量名称|值|+---------------+--------------------+|Ssl_密码|DHE-RSA-AES256-SHA|+---------------+--------------------+一组1行(0.00秒)特定用户的连接需要ssl(“需要ssl”):安全套接字层告诉服务器只允许帐户使用SSL加密连接。授予测试的所有特权。*至“root”@“本地主机”需要SSL;要进行连接,客户端必须指定--ssl ca选项来验证服务器证书,还可以指定--ssl密钥和--ssl证书选项。如果未指定--ssl ca选项或--ssl capath选项,则客户端不会对服务器证书进行身份验证。


备用链接:《用SSL保护PHP与MySQL的连接》中的冗长教程。