SSL in socket.io with express: Missing PFX or certificate + private key.

I want a socket with socket.io over SSL. I read other answers but nothing worked

Here is my code:

var ssl_options = { key : fs.readFileSync(my_key_path), cert : fs.readFileSync(my_cert_path) }; var protocol = "https"; preparedApp = require(protocol).createServer(ssl_options,app); var io = require('socket.io')(preparedApp); preparedApp.listen(8080, function(){}); io.on('connection', function(socket){}); 

And here is the log of my ssl_options ...

 { key: <Buffer 41 ...>, cert: <Buffer 4a ...> } 

These are errors with an error in the header throw new Error('Missing PFX or certificate + private key.'); . Does anyone know what could happen? None of the other solutions to this answer resolved my case.

+6
source share
1 answer

Use the PEM (RSA) format for your private key. Check if the private key is base64 encoded between "----- BEGIN RSA PRIVATE KEY -----" and "----- END RSA PRIVATE KEY -----"

From the docs:

  • : A string or buffer containing the serverโ€™s private key in PEM format.
  • cert: string containing the certificate encoded by PEM.
  • passphrase: passphrase string for private key or pfx [optional default: null]

or

  • pfx: String or buffer containing private keys, certificates, and PFX or PKCS12 certificates

To convert the private key to RSA PEM: openssl rsa -in <PATH TO KEY> -out key.pem -outform PEM

To create the PKCS # 12 package, use openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile ca.pem -out host.pfx

- ADD -

To ensure that the certificate is a PEM encoded run openssl x509 -in <PATH TO CERT> -out cert.pem -outform PEM

+2
source

All Articles