Node.js https pem error: routines:PEM_read_bio:no start line

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

I am messing with login form right now with node.js, I tried creating a pem key and csr using

openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem

However I been getting errors for running node server.js

Here is my 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.

Thanks

回答1:

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

   var options = {       key: fs.readFileSync('./key.pem', 'utf8'),       cert: fs.readFileSync('./server.crt', 'utf8')    };


回答2:

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.



回答3:

Was facing the same problem In my case I changed the option parameter of cert to pfx & removed utf8 encoding.

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 };


回答4:

I removed this error by write the following code

Open Terminal

  1. openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem

  2. openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt

Now use the server.crt and key.pem file

app.js or server.js file

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.



回答5:

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++



回答6:

Generate the private key and server certificate with specific expiry date or with infinite(XXX) expiry time and self sign it.

$ openssl req -x509 -sha256 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX

$ Enter a private key passphrase...`

Then it will work!



回答7:

If you log the

var options = {   key: fs.readFileSync('./key.pem', 'utf8'),   cert: fs.readFileSync('./csr.pem', 'utf8') };

You might notice there are invalid characters due to improper encoding.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!