From cd03322304c3aa223457b1f94f5bc16cad357ec0 Mon Sep 17 00:00:00 2001 From: bordun Date: Sat, 24 Oct 2020 10:07:54 +0200 Subject: [PATCH] ssl: generate unique certificates (with random serial numbers) to avoid reuse errors. Certificates generated with older versions of o-s-c w> --- CHANGELOG.md | 3 +++ src/server/ssl.js | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f86b405..760ef1b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.5.4 +- bug fixes + - ssl: generate unique certificates (with random serial numbers) to avoid reuse errors. Certificates generated with older versions of o-s-c will be updated automatically. + - remote control - add `/STATE/OPEN` and `/STATE/SAVE` commands - ignore unsaved changes when loading a session with `/SESSION/OPEN` diff --git a/src/server/ssl.js b/src/server/ssl.js index 6623d5fb..68382b93 100644 --- a/src/server/ssl.js +++ b/src/server/ssl.js @@ -5,6 +5,20 @@ var settings = require('./settings'), forge.options.usePureJavaScript = true +// Generate a random Serial number for the certificate +// matth-c3 @ https://github.com/digitalbazaar/forge/issues/673#issuecomment-500900852 +function randomSerialNumber() { + var hexString = forge.util.bytesToHex(forge.random.getBytesSync(16)) + + var mostSiginficativeHexAsInt = parseInt(hexString[0], 16) + if (mostSiginficativeHexAsInt < 8) { + return hexString + } + + mostSiginficativeHexAsInt -= 8 + return mostSiginficativeHexAsInt.toString() + hexString.substring(1) +} + function createCertificate() { console.log('(INFO) Creating self signed ssl certificate...') @@ -40,8 +54,8 @@ if (!certificate) { var cert = pki.certificateFromPem(certificate.cert) - if (new Date() > new Date(cert.validity.notAfter)) { - console.log('(INFO) Self-signed ssl certificate has expired') + if (new Date() > new Date(cert.validity.notAfter) || cert.serialNumber === '00') { + console.log('(INFO) Self-signed ssl certificate in cache has expired or is invalid') createCertificate() } else { console.log('(INFO) Using self-signed ssl certificate in cache')