Node.js https pem 错误:routines:PEM_read_bio:no start line
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22584268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js https pem error: routines:PEM_read_bio:no start line
提问by xbd
I am messing with login form right now with node.js, I tried creating a pem key and csr using
我现在正在使用 node.js 处理登录表单,我尝试使用创建 pem 密钥和 csr
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
However I been getting errors for running node server.js
但是我在运行 node server.js 时遇到错误
Here is my server.js
这是我的 server.js
var http = require('http'),
express = require('express'),
UserServer = require('./lib/user-server');
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('./key.pem', 'utf8'),
cert: fs.readFileSync('./csr.pem', 'utf8')
};
var app = express();
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
var httpserver = http.createServer(app).listen('3004', '127.0.0.1');
var https_server = https.createServer(options, app).listen('3005', '127.0.0.1');
UserServer.listen(https_server);
Here is the error
这是错误
crypto.js:104
if (options.cert) c.context.setCert(options.cert);
^
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Object.exports.createCredentials (crypto.js:104:31)
at Server (tls.js:1107:28)
at new Server (https.js:35:14)
at Object.exports.createServer (https.js:54:10)
I tried running
我试过跑步
openssl x509 -text -inform DER -in key.pem
It gives
它给
unable to load certificate
140735208206812:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1319:
140735208206812:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:381:Type=X509
I am not exactly sure what does the error mean as my encryption file is .pem file already, so any help would be much appreciated.
我不确定这个错误是什么意思,因为我的加密文件已经是 .pem 文件,所以任何帮助都将不胜感激。
Thanks
谢谢
回答by Hassaan
You are probably using the wrong certificate file, what you need to do is generate a self signed certificate which can be done as follows
您可能使用了错误的证书文件,您需要做的是生成一个自签名证书,可以按如下方式完成
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt
then use the server.crt
然后使用 server.crt
var options = {
key: fs.readFileSync('./key.pem', 'utf8'),
cert: fs.readFileSync('./server.crt', 'utf8')
};
回答by VIKAS KOHLI
I removed this error by write the following code
我通过编写以下代码删除了此错误
Open Terminal
打开终端
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt
Now use the server.crt and key.pem file
现在使用 server.crt 和 key.pem 文件
app.js or server.js file
app.js 或 server.js 文件
var https = require('https');
var https_options = {
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('server.crt', 'utf8')
};
var server = https.createServer(https_options, app).listen(PORT);
console.log('HTTPS Server listening on %s:%s', HOST, PORT);
It works but the certificate is not trusted. You can view the image in image file.
它可以工作,但证书不受信任。您可以在图像文件中查看图像。
回答by Mayank Kumar
Was facing the same problem In my case I changed the option parameter of cert to pfx & removed utf8 encoding.
面临同样的问题在我的情况下,我将 cert 的选项参数更改为 pfx 并删除了 utf8 编码。
before:
前:
var options = {
hostname : 'localhost',
path : '/',
method : 'POST',
cert: fs.readFileSync(testCert, 'utf8'),
passphrase:passphrase,
agent:false,
rejectUnauthorized:false
};
after:
后:
var options = {
hostname : 'localhost',
path : '/',
method : 'POST',
pfx: fs.readFileSync(testCert),
passphrase:passphrase,
agent:false,
rejectUnauthorized:false
};
回答by Kyle Coots
For me the issues was I had the key and cert swapped.
对我来说,问题是我交换了密钥和证书。
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/mysite.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/mysite.com/fullchain.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/mysite.com/chain.pem')
};
EDIT
编辑
More Complete Example (Maybe not completely functional)
更完整的示例(可能功能不完整)
Server.js
服务器.js
var fs = require('fs');
var sessionKey = 'ai_session:';
var memcachedAuth = require('memcached-auth');
var clients = {};
var users = {};
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/somesite.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/somesite.com/fullchain.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/somesite.com/chain.pem')
};
var origins = 'https://www.somesite.com:*';
var https = require('https').createServer(options,function(req,res){
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', origins);
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
});
var io = require('socket.io')(https);
https.listen(3000);
io.sockets.on('connection', function(socket){
socket.on('auth', function(data){
var session_id = sessionKey+data.token;
memcachedAuth.is_logged_in(session_id).then( (response) => {
if(response.is_logged_in){
// user is logged in
socket.emit('is_logged_in', true);
messenger.addUser(socket);
// dynamic room
socket.on('room', function(room){
socket.join(room);
console.log('joing room '+room);
});
socket.on('message', function(data){
messenger.receive(data.message_data);
});
}else{
// Not logged in
socket.emit('is_logged_in', false);
}
}).catch( (error) => {
console.log(error);
});
});
});
var messenger = {
socket: (socket)=>{
return socket;
},
subscribe: (room)=>{
},
unsubscribe: (room)=>{
},
send: (data)=>{
},
receive: (data)=>{
console.log(data);
//connected
if (clients[data.user_name]){
console.log('user');
}
},
addUser: (socket)=>{
socket.on('add-user', function(data){
clients[data] = {
"socket": socket.id
};
console.log('Adding User:' + data);
console.log(clients);
});
},
private: (socket)=>{
// Not working yet...
socket.on('message', function(data){
console.log("Sending: " + data + " to " + data.user_name);
if (clients[data.user_name]){
io.sockets.connected[clients[data.user_name].socket].emit("response", data);
} else {
console.log("User does not exist: " + data.user_name);
}
});
},
disconnect:()=>{
//Removing the socket on disconnect
socket.on('disconnect', function() {
for(var name in clients) {
if(clients[name].socket === socket.id) {
delete clients[name];
break;
}
}
});
}
}
I have created a repo on github including a more complete version of the above code if anyone is interested: https://github.com/snowballrandom/Memcached-Auth
如果有人感兴趣,我已经在 github 上创建了一个 repo,包括上述代码的更完整版本:https: //github.com/snowballrandom/Memcached-Auth
回答by Juan Pablo Mendez Nogales
For me, after trying all above solutions it ended up being a problem related to encoding. Concisely, my key was encoded using 'UTF-8 with BOM'. It should be UTF-8 instead.
对我来说,在尝试了上述所有解决方案后,它最终成为与编码相关的问题。简而言之,我的密钥是使用“带有 BOM 的 UTF-8”编码的。它应该是UTF-8。
To fix it, at least using VS Code follow this steps:
要修复它,至少使用 VS Code 遵循以下步骤:
- Open the file and click on the encoding button at the status bar (at the bottom) and select 'Save with encoding'.
- Select UTF-8.
- Then try using the certificate again.
- 打开文件并单击状态栏(底部)上的编码按钮,然后选择“使用编码保存”。
- 选择 UTF-8。
- 然后再次尝试使用该证书。
I suppose you can use other editors that support saving with the proper encoding.
我想您可以使用其他支持以正确编码进行保存的编辑器。
Source: error:0906d06c:pem routines:pem_read_bio:no start line, when importing godaddy SSL certificate
来源:error:0906d06c:pemroutines:pem_read_bio:no start line, 当导入godaddy SSL证书时
P.D I did not need to set the encodingto utf-8option when loading the file using the fs.readFileSyncfunction.
PD在使用该函数加载文件时,我不需要设置encodingtoutf-8选项fs.readFileSync。
Hope this helps somebody!
希望这对某人有帮助!
回答by Avanthika
I guess this is because your nodejs cert has expired. Type this line : npm set registry http://registry.npmjs.org/
and after that try again with npm install . This actually solved my problem.
我猜这是因为您的 nodejs 证书已过期。输入这一行:npm set registry http://registry.npmjs.org/
然后再试一次 npm install 。这实际上解决了我的问题。
回答by H. Green
I actually just had this same error message.
我实际上只是收到了同样的错误消息。
The problem was I had keyand certfiles swapped in the configuration object.
问题是我key和cert交换配置中的目标文件。
回答by neesh
If you are using windows, you should make sure that the certificate file csr.pem and key.pem don't have unix-style line endings. Openssl will generate the key files with unix style line endings. You can convert these files to dos format using a utility like unix2dos or a text editor like notepad++
如果您使用的是 Windows,您应该确保证书文件 csr.pem 和 key.pem 没有 unix 样式的行尾。Openssl 将生成带有 unix 样式行结尾的密钥文件。您可以使用像 unix2dos 这样的实用程序或像 notepad++ 这样的文本编辑器将这些文件转换为 dos 格式
回答by Sergii Ieromin
I faced with the problem like this.
我遇到了这样的问题。
The problem was that I added the public key without '-----BEGIN PUBLIC KEY-----' at the beginning and without '-----END PUBLIC KEY-----'.
问题是我在开头添加了没有'-----BEGIN PUBLIC KEY-----'并且没有'-----END PUBLIC KEY-----'的公钥。
So it causes the error.
所以它会导致错误。
Initially, my public key was like this:
最初,我的公钥是这样的:
-----BEGIN PUBLIC KEY-----
WnsbGUXbb0GbJSCwCBAhrzT0s2KMRyqqS7QBiIG7t3H2Qtmde6UoUIcTTPJgv71
......
oNLcaK2wKKyRdcROK7ZTSCSMsJpAFOY
-----END PUBLIC KEY-----
But I used just this part:
但我只使用了这部分:
WnsbGUXb+b0GbJSCwCBAhrzT0s2KMRyqqS7QBiIG7t3H2Qtmde6UoUIcTTPJgv71
......
oNLcaK2w+KKyRdcROK7ZTSCSMsJpAFOY
回答by Bugs Bunny
Corrupted cert and/or key files
损坏的证书和/或密钥文件
For me it was just corrupted files. I copied the contents from GitHub PullRequest webpage and I guess I added an extra space somewhere or whatever... once I grabbed the raw thing and replaced the file, it worked.
对我来说,这只是损坏的文件。我从 GitHub PullRequest 网页复制了内容,我想我在某处或其他地方添加了一个额外的空间......一旦我抓住了原始的东西并替换了文件,它就起作用了。


