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

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

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


当前回答

生成10年无密码和证书的密钥,短方法:

openssl req  -x509 -nodes -new  -keyout server.key -out server.crt -days 3650 -subj "/C=/ST=/L=/O=/OU=web/CN=www.server.com"

对于标志-subj |,允许使用subject空值-subj“/C=/ST=/L=/O=/OU=web/CN=www.server.com”,但可以根据需要设置更多详细信息:

C-国家名称(2字母代码)ST-状态L-地点名称(如城市)O-组织名称OU-组织单位名称CN-通用名称-必填!

其他回答

这对我有用

openssl req -x509 -nodes -subj '/CN=localhost'  -newkey rsa:4096 -keyout ./sslcert/key.pem -out ./sslcert/cert.pem -days 365

服务器.js

var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var compression = require('compression');
var express = require('express');
var app = express();

app.use(compression());
app.use(express.static(__dirname + '/www'));    

app.get('/*', function(req,res) {
  res.sendFile(path.join(__dirname+'/www/index.html'));
});

// your express configuration here

var httpServer = http.createServer(app);
var credentials = {
    key: fs.readFileSync('./sslcert/key.pem', 'utf8'),
    cert: fs.readFileSync('./sslcert/cert.pem', 'utf8')
};
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

console.log(`RUNNING ON  http://127.0.0.1:8080`);
console.log(`RUNNING ON  http://127.0.0.1:8443`);

正如已经详细讨论过的,自签名证书不受Internet信任。您可以将自签名证书添加到许多浏览器,但不是所有浏览器。或者,您可以成为自己的证书颁发机构。

人们不想从证书颁发机构获得签名证书的主要原因是成本--赛门铁克每年收取995美元至1999美元的证书费用--仅针对用于内部网络的证书,赛门铁克每年收取399美元。如果您正在处理信用卡付款或为高利润公司的利润中心工作,那么该成本很容易被证明是合理的。对于一个在互联网上创建的个人项目,或者一个以最低预算运行的非营利组织,或者如果一个人在一个组织的成本中心工作,成本中心总是试图用更少的钱做更多的事情,这是很多人都无法承受的。

另一种方法是使用certbot(参见certbot)。Certbot是一个易于使用的自动客户端,可为web服务器获取和部署SSL/TLS证书。

如果您设置了certbot,则可以使其为您创建和维护Let's Encrypt证书颁发机构颁发的证书。

我在周末为我的组织做了这件事。我在服务器(Ubuntu 16.04)上安装了certbot所需的软件包,然后运行了设置和启用certbot所必需的命令。一个可能需要certbot的DNS插件-我们目前正在使用DigitalOcean,但可能很快会迁移到另一个服务。

请注意,有些指示不太正确,需要谷歌花点时间才能弄清楚。这第一次花了我相当多的时间,但现在我想我可以在几分钟内完成。

对于DigitalOcean,我遇到的一个问题是,系统提示我输入DigitalOcean凭据INI文件的路径。脚本所指的是应用程序和API页面以及该页面上的令牌/密钥选项卡。您需要为DigitalOcean的API创建或生成个人访问令牌(读写)——这是一个65个字符的十六进制字符串。然后需要将该字符串放入运行certbot的Web服务器上的文件中。该文件的第一行可以是注释(注释以#开头)。第二行是:

dns_digitalocean_token = 0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff

一旦我知道了如何为DigitalOcean的API设置读写令牌,就可以很容易地使用certbot来设置通配符证书。请注意,您不必设置通配符证书,而是可以指定要应用证书的每个域和子域。正是通配符证书需要包含来自DigitalOcean的个人访问令牌的凭据INI文件。

请注意,公钥证书(也称为身份证书或SSL证书)过期并需要续订。因此,您需要定期(重复)更新证书。certbot文档包括续订证书。

我的计划是编写一个脚本,使用openssl命令获取证书的到期日期,并在到期前30天或更短的时间内触发续订。然后我将把这个脚本添加到cron中,并每天运行一次。

以下是读取证书过期日期的命令:

root@prod-host:~# /usr/bin/openssl x509 -enddate -noout -in path-to-certificate-pem-file
notAfter=May 25 19:24:12 2019 GMT

我不能评论,所以我将把这作为一个单独的答案。我发现了一些公认的单线回答的问题:

一行代码在密钥中包含一个口令。一行程序使用SHA-1,在许多浏览器中,SHA-1会在控制台中抛出警告。

下面是一个简化版本,它删除了密码短语,提高了安全性以抑制警告,并在注释中包含了一个建议,即传递-subj以删除完整的问题列表:

openssl genrsa -out server.key 2048
openssl rsa -in server.key -out server.key
openssl req -sha256 -new -key server.key -out server.csr -subj '/CN=localhost'
openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt

将“localhost”替换为所需的任何域。您需要逐个运行前两个命令,因为OpenSSL将提示输入密码短语。

要将两者合并为.pem文件,请执行以下操作:

cat server.crt server.key > cert.pem

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。

我不能评论,所以我补充了一个单独的答案。我试图为NGINX创建一个自签名证书,这很简单,但当我想将其添加到Chrome白名单时,我遇到了一个问题。我的解决方案是创建一个根证书并用它签署一个子证书。

所以一步一步。创建文件config_ssl_c.cnf注意,配置文件有一个选项basicConstraints=CA:true,这意味着该证书应该是根证书。

这是一个很好的做法,因为您只需创建一次即可重复使用。

[ req ]
default_bits = 2048

prompt = no
distinguished_name=req_distinguished_name
req_extensions = v3_req

[ req_distinguished_name ]
countryName=UA
stateOrProvinceName=root region
localityName=root city
organizationName=Market(localhost)
organizationalUnitName=roote department
commonName=market.localhost
emailAddress=root_email@root.localhost

[ alternate_names ]
DNS.1        = market.localhost
DNS.2        = www.market.localhost
DNS.3        = mail.market.localhost
DNS.4        = ftp.market.localhost
DNS.5        = *.market.localhost

[ v3_req ]
keyUsage=digitalSignature
basicConstraints=CA:true
subjectKeyIdentifier = hash
subjectAltName = @alternate_names

子证书的下一个配置文件将调用config_ssl.cnf。

[ req ]
default_bits = 2048

prompt = no
distinguished_name=req_distinguished_name
req_extensions = v3_req

[ req_distinguished_name ]
countryName=UA
stateOrProvinceName=Kyiv region
localityName=Kyiv
organizationName=market place
organizationalUnitName=market place department
commonName=market.localhost
emailAddress=email@market.localhost

[ alternate_names ]
DNS.1        = market.localhost
DNS.2        = www.market.localhost
DNS.3        = mail.market.localhost
DNS.4        = ftp.market.localhost
DNS.5        = *.market.localhost

[ v3_req ]
keyUsage=digitalSignature
basicConstraints=CA:false
subjectAltName = @alternate_names
subjectKeyIdentifier = hash

第一步-创建根密钥和证书

openssl genrsa -out ca.key 2048
openssl req -new -x509 -key ca.key -out ca.crt -days 365 -config config_ssl_ca.cnf

第二步创建子密钥和文件CSR-证书签名请求。因为这样做的目的是通过root签名子证书并获得正确的证书

openssl genrsa -out market.key 2048
openssl req -new -sha256 -key market.key -config config_ssl.cnf -out market.csr

打开Linux终端并执行以下命令

echo 00 > ca.srl
touch index.txt

ca.srl文本文件包含下一个要使用的十六进制序列号。强制性的此文件必须存在并包含有效的序列号。

最后一步,再创建一个配置文件,并将其命名为config_ca.cnf

# we use 'ca' as the default section because we're usign the ca command
[ ca ]
default_ca = my_ca

[ my_ca ]
#  a text file containing the next serial number to use in hex. Mandatory.
#  This file must be present and contain a valid serial number.
serial = ./ca.srl

# the text database file to use. Mandatory. This file must be present though
# initially it will be empty.
database = ./index.txt

# specifies the directory where new certificates will be placed. Mandatory.
new_certs_dir = ./

# the file containing the CA certificate. Mandatory
certificate = ./ca.crt

# the file contaning the CA private key. Mandatory
private_key = ./ca.key

# the message digest algorithm. Remember to not use MD5
default_md = sha256

# for how many days will the signed certificate be valid
default_days = 365

# a section with a set of variables corresponding to DN fields
policy = my_policy

# MOST IMPORTANT PART OF THIS CONFIG
copy_extensions = copy

[ my_policy ]
# if the value is "match" then the field value must match the same field in the
# CA certificate. If the value is "supplied" then it must be present.
# Optional means it may be present. Any fields not mentioned are silently
# deleted.
countryName = match
stateOrProvinceName = supplied
organizationName = supplied
commonName = market.localhost
organizationalUnitName = optional
commonName = supplied

您可能会问,为什么这么难,为什么我们必须再创建一个配置来按根签名子证书。答案很简单,因为子证书必须具有SAN块-主题替代名称。如果我们使用“openssl x509”utils对子证书进行签名,则根证书将删除子证书中的SAN字段。因此,我们使用“opensslca”而不是“opensslx509”来避免删除SAN字段。我们创建一个新的配置文件,并告诉它复制所有扩展字段copy_extensions=copy。

openssl ca -config config_ca.cnf -out market.crt -in market.csr

该程序会问您两个问题:

签署证书?说“Y”1个证书请求中有1个已认证,是否提交?说“Y”

在终端中,您可以看到一个带有单词“Database”的句子,它表示您通过命令“touch”创建的文件index.txt。它将包含由“openssl-ca”util创建的所有证书的所有信息。要检查证书的有效性,请使用:

openssl rsa -in market.key -check

如果您想查看CRT中的内容:

openssl x509 -in market.crt -text -noout

如果您想了解CSR的内部内容:

openssl req -in market.csr -noout -text