javascriptnode.jssslhttpscertbot

node https module giving "error:0480006C:PEM routines::no start line" error, despite the certificates being correct


Outline

Problem

The https module is giving me the error error:0480006C:PEM routines::no start line when I try to start the server with https.

The Code

app.js

const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const { config } = require('dotenv');

config();

const app = express();

const httpPort = process.env.HTTP_PORT;
const httpsPort = process.env.HTTPS_PORT;

try {
  const privateKeyPath = process.env.SSL_KEY;
  const publicKeyPath = process.env.SSL_CERT;
  const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
  const certificate = fs.readFileSync(publicKeyPath, 'utf8');
  const credentials = { key: privateKey, cert: certificate };
  const httpsServer = https.createServer(credentials, app);
  httpsServer.listen(httpsPort, () => {
    console.log(`HTTPS Server listening on port ${httpsPort}`);
  });
} catch (ex) {
  console.error('Certificates not found. Not using HTTPS');
  console.error(ex);
}

const httpServer = http.createServer(app);

httpServer.listen(httpPort, () => {
  console.log(`HTTP Server listening on port ${httpPort}`);
});

.env

HTTP_PORT=80
HTTPS_PORT=443
SSL_KEY=/etc/letsencrypt/live/example.com/fullchain.pem
SSL_CERT=/etc/letsencrypt/live/example.com/privkey.pem

fullchain.pem

-----BEGIN CERTIFICATE-----
 ...base64 encoded text here
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
 ...base64 encoded text here
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
 ...base64 encoded text here
-----END CERTIFICATE-----

privkey.pem

-----BEGIN PRIVATE KEY-----
 ...base64 encoded text here
-----END PRIVATE KEY-----

Output

Certificates not found. Not using HTTPS
Error: error:0480006C:PEM routines::no start line
    at node:internal/tls/secure-context:69:13
    at Array.forEach (<anonymous>)
    at setCerts (node:internal/tls/secure-context:67:3)
    at configSecureContext (node:internal/tls/secure-context:156:5)
    at Object.createSecureContext (node:_tls_common:117:3)
    at Server.setSecureContext (node:_tls_wrap:1348:27)
    at Server (node:_tls_wrap:1207:8)
    at new Server (node:https:74:3)
    at Object.createServer (node:https:112:10)
    at Object.<anonymous> (/root/reponame/app.js:78:29) {
  library: 'PEM routines',
  reason: 'no start line',
  code: 'ERR_OSSL_PEM_NO_START_LINE'
}

Solution

  • As stated by @DivineSoul, the issue was that I had the private key path & public key path switched around the wrong way.