Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to socket with socket.io through SSL. I have read the 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 ...> }

This errors with the error in the title throw new Error('Missing PFX or certificate + private key.');. Does anyone know what might be happening? None of the other solutions to this answer solved my case.

like image 942
Fane Avatar asked Oct 25 '15 19:10

Fane


1 Answers

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

From the docs:

  • key: A string or Buffer containing the private key of the server in PEM format
  • cert : A string holding the PEM encoded certificate
  • passphrase: A string of passphrase for the private key or pfx [optional default: null]

or

  • pfx : A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates

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

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

-- ADDITION --

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

like image 72
windm Avatar answered Sep 22 '22 15:09

windm