按照 Node api 文档中的指定,我尝试使用 openssl 使用自行创建和签名的证书尝试第一个。一切都很顺利,除了无法从 Android 测试客户端,因为它需要 ca 证书。当我尝试第二种方法(使用 pfx 而不是使用 key,cert)时 https.createserver 会抛出错误
crypto.js:145
c.context.loadPKCS12(pfx);
^
Error: mac verify failure
at Object.exports.createCredentials (crypto.js:145:17)
at Server (tls.js:1130:28)
at new Server (https.js:35:14)
at Object.exports.createServer (https.js:54:10)
at Object.<anonymous> (C:\iTollonServer\iTollonServer\iTollonServer\app.js:105:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
来自 Node Api 的代码:
// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
Or
var https = require('https');
var fs = require('fs');
var options = {
pfx: fs.readFileSync('server.pfx')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
在 pfx 情况下,您应该添加选项
passphrase: 'password'
对于未来使用 Node.js 的人,如果您使用的是 OpenSSL 生成的证书 < v3, you may encounter compatibility issues with Node.js. The solution is to decrypt this old certificate and write it in the new version.