Che*_*rif 8 https ssl-certificate apple-push-notifications node.js ios
我为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);
Run Code Online (Sandbox Code Playgroud)
有解决方案吗
文件.cer可以使用两种不同的格式进行编码:PEM和DER.
如果您的文件是使用该PEM格式编码的,您可以像任何其他文件一样使用它(更多信息可以在Node.js 文档.pem中找到):
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);
Run Code Online (Sandbox Code Playgroud)
如果您的文件使用该DER格式进行编码,您首先需要.pem使用 OpenSSL 将其转换为文件(该命令取自此处):
openssl x509 -inform der -in cert.cer -out cert.pem
Run Code Online (Sandbox Code Playgroud)
然后可以使用上面的代码,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);
Run Code Online (Sandbox Code Playgroud)
如果您拥有与您的文件匹配的证书颁发机构的密钥cert.cer,您可以将其包含在以下options参数中(代码示例假定文件是名称,并且使用以下格式进行编码):https.createServerca.pemPEM
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);
Run Code Online (Sandbox Code Playgroud)
有关https.createServer及其参数的更多信息,请查看文档。
注意:上面的所有选项都假设您还有一个PEM以名为 的格式编码的公钥key.pem,并且该.cer文件名为cert.cer. 如果您没有公钥,请发表评论或将其添加到问题本身,我将相应地更新我的答案。
如果您不确定文件的编码格式,您可以尝试这两个选项,看看哪一个最适合您。
这是一个使用的示例crt,您可以将 cer 转换为 crt,以防它不起作用:
var express = require('express');
var app = express();
var fs = require('fs');
var https = require('https');
var credentials = {
ca: fs.readFileSync(__dirname+"/ssl/certificate.ca-crt", 'utf8'), //certificate concatenation or intermediate certificates
key: fs.readFileSync(__dirname+"/ssl/mydomain.com.key", 'utf8'), //SSL key
cert: fs.readFileSync(__dirname+"/ssl/certificate.crt", 'utf8') //the certificate
};
app.configure(function() {
// set up your express application
});
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(443);
Run Code Online (Sandbox Code Playgroud)
取自此处(西班牙语):salvatorelab.es
您还可以查看这些文件(crt、ca-crt...)包含或外观的示例。