DEV Community 👩‍💻👨‍💻

Simone Aronica
Simone Aronica

Posted on • Updated on

[SOLVED] Node.js ERR_SSL_VERSION_OR_CIPHER_MISMATCH

Hello everyone,
I am using Node.js to host a webserver with both http and https, and I have generated my self signed certificate via the openssl commands:

$ openssl genrsa -des3 -out rootCA.key 2048
$ openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt

However when I try to connect to https://127.0.0.1:8080 or https://localhost:8080 or the machine's IP from another device I always get ERR_SSL_VERSION_OR_CIPHER_MISMATCH

What could it be? I have been trying to figure out for a while now and I can't understand what is the issue.

Server code

.env

PASSPHRASE=dummyPass
HTTPS=true
HTTPPORT=8079
HTTPSPORT=8080
KEY=rootCA.key
CERT=rootCA.crt
server.js

const httpsEnabled = process.env.HTTPS == 'true' || false;
if(httpsEnabled)
{
  const httpsPort = process.env.HTTPSPORT || 8081;
  const httpsServer = https.createServer(credentials, app);

  const keyStream = path.resolve(`./${process.env.KEY}`);
  const certificateStream = path.resolve(`./${process.env.CERT}`);
  const privateKey  = fs.readFileSync(keyStream, 'utf8');
  const certificate = fs.readFileSync(certificateStream, 'utf8');

  const credentials = {key: privateKey, cert: certificate, passphrase: process.env.PASSPHRASE};

  httpsServer.on("error", () => {
    console.log(`Could not start the app on port ${httpsPort}`);
    process.exit();
  });
  httpsServer.listen(httpsPort, () => {
    console.log(`App listening for HTTPS requests to https://127.0.0.1:${httpsPort}/`);
  });
}

Solved:

I'm stupid, I started the server before reading the credential. This is the fixed code:

server.js

const httpsEnabled = process.env.HTTPS == "true" || false;
if (httpsEnabled) {
  const keyStream = path.resolve(`./${process.env.KEY}`);
  const certificateStream = path.resolve(`./${process.env.CERT}`);
  const privateKey = fs.readFileSync(keyStream, "utf8");
  const certificate = fs.readFileSync(certificateStream, "utf8");
  const credentials = {
    key: privateKey,
    cert: certificate,
    passphrase: process.env.PASSPHRASE
  };

  const httpsPort = process.env.HTTPSPORT || 8081;
  const httpsServer = https.createServer(credentials, app);

  httpsServer.on("error", () => {
    console.log(`Could not start the app on port ${httpsPort}`);
    process.exit();
  });
  httpsServer.listen(httpsPort, () => {
    console.log(
      `App listening for HTTPS requests to https://127.0.0.1:${httpsPort}/`
    );
  });
}

Top comments (0)

Create an Account!

👀 Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.