NodeJS & SSL - “密码读取错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30957793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:53:46  来源:igfitidea点击:

NodeJS & SSL - "bad password read"

node.jssslhttpscertificate

提问by MrQuiw

Node is failing to create a secure context for SSL communications.

节点无法为 SSL 通信创建安全上下文。

Specifically, I'm trying to get remote notifications to work on iOS. I use a module, called node-apn which throws this error:

具体来说,我正在尝试让远程通知在 iOS 上工作。我使用了一个名为 node-apn 的模块,它会引发此错误:

Error: error:0906A068:PEM routines:PEM_do_header:bad password read
at Error (native)
at Object.createSecureContext (_tls_common.js:108:19)
at Object.exports.connect (_tls_wrap.js:852:21)
at apnSocket (/home/Slurp/node_modules/apn/lib/socket.js:56:19)
at Connection.<anonymous> (/home/Slurp/node_modules/apn/lib/connection.js:188:17)
at _fulfilled (/home/Slurp/node_modules/apn/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/home/Slurp/node_modules/apn/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/home/Slurp/node_modules/apn/node_modules/q/q.js:796:13)

This seems to be a generic error though, and isn't really related to APN specifically.

不过,这似乎是一个通用错误,与 APN 并没有真正相关。

回答by Gang Su

This is because you've specified a passphrase when generating the cert. This is a password that must be supplied by anyone wanting to use it.

这是因为您在生成证书时指定了密码。这是任何想要使用它的人都必须提供的密码。

Adding a passphrase field to the credentials solves the problem.

向凭证添加密码字段可以解决问题。

var credentials = {
    key: fs.readFileSync('XXX.key', 'utf8'),
    cert: fs.readFileSync('XXX.crt', 'utf8'),
    passphrase: 'XXXX'
}

var httpsServer = https.createServer(credentials, app);

回答by Daniel

The following command will generate an unencrypted key, so you won't need to provide a passphrase:

以下命令将生成未加密的密钥,因此您无需提供密码:

openssl rsa -in yourKey.key -out newKey.key

This command will prompt you for the passphrase.

此命令将提示您输入密码。

回答by Syed Shahzaib Hussain

Use these to generate pem.

使用这些来生成 pem。

openssl genrsa -out server-key.pem 1024 openssl req -new -key server-key.pem -out server-csr.pem openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem

openssl genrsa -out server-key.pem 1024 openssl req -new -key server-key.pem -out server-csr.pem openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem