gpt4 book ai didi

node.js - 在 Node.js 应用程序中使用 SSL 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:47 25 4
gpt4 key购买 nike

已编辑

我正在尝试让 SSL 在应用程序上运行,但我就是无法让它运行。我在解决问题时遇到问题。

我已经创建了一个基本的应用程序来测试它:

var https = require('https');
var fs = require('fs');

var options = {
key: fs.readFileSync('./ssl/server_key.pem'),
cert: fs.readFileSync('./ssl/server_crt.pem'),
passphrase: 'mypassphrase',
ca: fs.readFileSync('./ssl/cacert.pem'),
requestCert: true,
rejectUnauthorized: false
};

https.createServer(options, function (req, res) {
console.log('req.client.authorized: %s', req.client.authorized);
if (req.client.authorized) {
res.writeHead(200);
res.end("hello world\n");
}
else {
res.writeHead(404);
res.end("Not authorised\n");
}

}).listen(3000);
console.log('Server started...');

我已经使用 Ubuntu Documentation on OpenSSL 创建了证书.我使用的过程是:

  1. 创建证书颁发机构根证书和 key

    export OPENSSL_CONF=~/myCA/caconfig.cnf

    openssl req -x509 -newkey rsa:2048 -out cacert.pem -outform PEM -days 1825

    生成:CA 公共(public)证书“cacert.pem”;和 CA 私钥“cakey.pem”

  2. 创建自签名服务器证书

    export OPENSSL_CONF=~/myCA/exampleserver.cnf

    openssl req -newkey rsa:1024 -keyout tempkey.pem -keyform PEM -out tempreq.pem -outform PEM

    mv tempkey.pem server_key.pem

    export OPENSSL_CONF=~/myCA/caconfig.cnf

    openssl ca -in tempreq.pem -out server_crt.pem

    生成:服务器应用程序证书文件“server_crt.pem”;和服务器应用程序 key 文件“server_key.pem”。

  3. 从服务器的根 CA X.509 证书创建 PKCS#12 证书以供客户端使用

    openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

    openssl pkcs12 -export -out mycert.pfx -in mycert.pem -name "Certificate for MySite"

    这会生成文件“mycert.pfx”。

  4. 复制文件到app的ssl目录

    我将文件“cacert.pem”、“server_key.pem”和“server_crt.pem”复制到应用的 ssl 目录。

  5. 通过 Firefox 的内置证书管理器将文件“mycert.pfx”导入 Firefox(我将其导入标记为“您的证书”的选项卡)。

  6. 在 Firefox 中输入 URL (https://ssl:3000)

    此时,我收到来自 Firefox 的“用户识别请求”,我从中选择我的证书。然后我收到来自 Firefox 的错误消息:

    “...安全连接失败 连接到 ssl:3000 时出错。 对等证书的签名无效。 (错误代码:sec_error_bad_signature)……”

我尝试使用 curl -v -s -k -E mycert.pem:somepassword https://ssl:3000 进行故障排除,并收到以下输出:

  • About to connect() to ssl port 3000 (#0)
  • Trying XXX.XXX.XXX.XXX... connected
  • successfully set certificate verify locations:
  • CAfile: none CApath: /etc/ssl/certs
  • SSLv3, TLS handshake, Client hello (1):
  • SSLv3, TLS handshake, Server hello (2):
  • SSLv3, TLS handshake, CERT (11):
  • SSLv3, TLS handshake, Request CERT (13):
  • SSLv3, TLS handshake, Server finished (14):
  • SSLv3, TLS handshake, CERT (11):
  • SSLv3, TLS handshake, Client key exchange (16):
  • SSLv3, TLS handshake, CERT verify (15):
  • SSLv3, TLS change cipher, Client hello (1):
  • SSLv3, TLS handshake, Finished (20):
  • SSLv3, TLS change cipher, Client hello (1):
  • SSLv3, TLS handshake, Finished (20):
  • SSL connection using AES256-SHA
  • Server certificate:
  • subject: CN=ssl; ST=Beijing; C=CN; emailAddress=root@localhost; O=Bengul Company; OU=Bedroom
  • start date: 2012-07-17 05:11:49 GMT
  • expire date: 2017-07-16 05:11:49 GMT
  • subjectAltName: ssl matched
  • issuer: CN=ssl; ST=Beijing; C=CN; emailAddress=root@localhost; O=Bengul Company; OU=Bedroom
  • SSL certificate verify result: self signed certificate (18), continuing anyway. GET / HTTP/1.1 User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 Host: ssl:3000 Accept: /

HTTP/1.1 404 Not Found

Date: Tue, 17 Jul 2012 05:22:04 GMT

Connection: keep-alive

Transfer-Encoding: chunked

Not authorised

  • Connection #0 to host ssl left intact
  • Closing connection #0
  • SSLv3, TLS alert, Client hello (1):

如果我然后运行 ​​netstat 我会得到这个输出:

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 ssl:55719 ssl:3000 TIME_WAIT

我不知道我做错了什么。如果有人能指出正确的方向,我将不胜感激。

最佳答案

我最近在支持 SSL 的 node.js 中实现了一个简单的代理服务器。也许这可以帮助您调试解决方案。您可以在我的 Github 项目中找到用于生成自签名证书的代码和简短说明:https://github.com/alienhard/malinger .

关于node.js - 在 Node.js 应用程序中使用 SSL 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11489796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com