ios - 在 NodeJs HTTPS 中使用 certificates.cer

标签 ios node.js https apple-push-notifications ssl-certificate

我已经为 IOS 推送通知生成了一个 .cer 文件,我想将它与 NodeJS HTTPS 模块一起使用。

我为 HTTPS 模块找到的唯一示例使用 .pem 和 .sfx 文件,而不是 .cer:

var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

or 

var options = {
  pfx: fs.readFileSync('server.pfx')
}

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

有什么解决办法吗?

最佳答案

.cer 文件可以使用两种不同的格式进行编码:PEMDER

如果你的文件是使用 PEM 格式编码的,你可以像使用任何其他 .pem 文件一样使用它(更多信息可以在 Node.js documentation 中找到) :

const https = require("https");

const options = {
    key: fs.readFileSync("key.pem", "utf8"),
    cert: fs.readFileSync("cert.cer", "utf8")
};

https.createServer(options, (req, res) => {
    res.writeHead(200);
    res.end("Hello world");
}).listen(8000);

如果您的文件使用 DER 格式编码,您首先需要使用 OpenSSL 将其转换为 .pem 文件(命令取自 here ):

openssl x509 -inform der -in cert.cer -out cert.pem

然后可以使用上面的代码,cert 文件名是 cert.pem 而不是 cert.cer:

const https = require("https");

const options = {
    key: fs.readFileSync("key.pem", "utf8"),
    cert: fs.readFileSync("cert.pem", "utf8")
};

https.createServer(options, (req, res) => {
    res.writeHead(200);
    res.end("Hello world");
}).listen(8000);

如果您拥有与您的 cert.cer 文件相匹配的证书颁发机构的 key ,您可以将其包含在 https 的 options 参数中。 createServer 如下(代码示例假定文件名为 ca.pem 并且使用 PEM 格式编码):

const https = require("https");

const options = {
    ca: fs.readFileSync("ca.pem", "utf8"),
    key: fs.readFileSync("key.pem", "utf8"),
    cert: fs.readFileSync("cert.pem", "utf8")
};

https.createServer(options, (req, res) => {
    res.writeHead(200);
    res.end("Hello world");
}).listen(8000);

有关 https.createServer 及其参数的更多信息,请查看 documentation

注意:以上所有选项都假设您还有一个以 PEM 格式编码的名为 key.pem 的公钥,并且 .cer 文件名为 cert.cer。如果您没有公钥,请发表评论或将其添加到问题本身,我会相应地更新我的答案。

如果您不确定文件的编码格式,您可以尝试两种选择,看看哪一种适合您。

关于ios - 在 NodeJs HTTPS 中使用 certificates.cer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146355/

相关文章:

ios - 数组追加不能快速跨文件工作

mysql - 使用 Node.js (Express) 和 mySQL 插入 TIMESTAMP?

node.js - 如何为 typescript 安装 discord.js 的类型?

python - Python 的 pbkdf2_sha256.verify 的 NodeJS 实现

ios - 在 ios 中保存文件,即使应用程序被删除也会保留 - Swift

ios - 在 Objective-C 中以编程方式创建 UIView 并将其放入闭包中

ios - 在自定义键盘中正确实现 handleInputModeList

php - 自动使用 HTTPS

google-chrome - Chrome 不支持协议(protocol)相关 URL 引用?

java - HTTPS 登录? java