使用证书中的公钥将引发错误

时间:2019-07-10 07:19:07

标签: node.js ssl x509certificate auth0 x509

我计划将auth0用于一些非常具体的要求。为此,我需要基于证书和公钥的https服务器。我已从auth0下载证书,并使用以下命令提取了公钥:

openssl x509 -pubkey -noout -in yourfilename.cer > pubkey.pem

然后我从中删除--- Begin ---和--- End ---。但是,当对https服务器使用证书和密钥时,它不起作用,并且我不断收到此错误:

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

我的代码:

const privateKey = fs.readFileSync('./certs/pubkey.pem', 'utf-8');
const certificate = fs.readFileSync('./certs/certificate.cer', 'utf-8');
const credentials = {key: privateKey, cert: certificate};

1 个答案:

答案 0 :(得分:2)

这里的问题是,您试图从公共密钥.pem文件创建私有密钥。我相信这就是为什么您从服务器创建代码中得到错误的原因。

例如,此代码有效:

const fs = require('fs');
const https = require('https');
const privateKey  = fs.readFileSync('./privateKey.key', 'utf8');
const certificate = fs.readFileSync('./certificate.crt', 'utf8');

const credentials = { key: privateKey, cert: certificate };
const express = require('express');
const app = express();

// Add test route here. 
app.get("/test", (req, res) => {
    res.status(200).send("All good");
})

const server = https.createServer(credentials, app);
server.listen(8443);

如果我们尝试:

const fs = require('fs');
const https = require('https');
const privateKey  = fs.readFileSync('./pubkey.pem', 'utf8');
const certificate = fs.readFileSync('./certificate.crt', 'utf8');

const credentials = {key: privateKey, cert: certificate};
const express = require('express');
const app = express();

// Add test route here. 
app.get("/test", (req, res) => {
    res.status(200).send("All good");
})

const server = https.createServer(credentials, app);
server.listen(8443);

我们会收到错误消息:

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

您需要一个私钥来创建https服务器,并且证书中没有该私钥。