微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 使用带有NodeJs HTTPS的certificates.cer

我为 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文件可以使用两种不同的格式进行编码:PEM和DER.

如果您的文件使用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",cert: fs.readFileSync("cert.pem",res) => {
    res.writeHead(200);
    res.end("Hello world");
}).listen(8000);

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

const https = require("https");

const options = {
    ca: fs.readFileSync("ca.pem",key: fs.readFileSync("key.pem",res) => {
    res.writeHead(200);
    res.end("Hello world");
}).listen(8000);

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

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

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

原文地址:https://www.jb51.cc/iOS/332204.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐