Fixed re-generting the TLS certificate if the desired cert name does not match.

Ylian Saint-Hilaire committed Aug 24, 2022 at 15:03 UTC d6a1f04d4a82773a0bdac22bb9b442907166a37b
1 file changed +12 -10
certoperations.js
+12 -10
@@ -918,12 +918,8 @@ module.exports.CertificateOperations = function (parent) {
918 r.AmtMpsName = obj.pki.certificateFromPem(r.mps.cert).subject.getField('CN').value;
919 var webCertificate = obj.pki.certificateFromPem(r.web.cert);
920 r.WebIssuer = webCertificate.issuer.getField('CN').value;
921 - if (commonName == 'un-configured') { // If the "cert" name is not set, try to use the certificate CN instead (ok if the certificate is not wildcard).
922 - commonName = webCertificate.subject.getField('CN').value;
923 - if (commonName.startsWith('*.')) { console.log("ERROR: Must specify a server full domain name in Config.json->Settings->Cert when using a wildcard certificate."); process.exit(0); return; }
924 - }
925 - r.CommonName = commonName;
926 - r.CommonNames = [commonName.toLowerCase()];
921 + r.CommonName = webCertificate.subject.getField('CN').value;
922 + r.CommonNames = [ r.CommonName ];
923 var altNames = webCertificate.getExtension('subjectAltName');
924 if (altNames) {
925 for (i = 0; i < altNames.altNames.length; i++) {
@@ -935,12 +931,18 @@ module.exports.CertificateOperations = function (parent) {
931 }
932 var rootCertificate = obj.pki.certificateFromPem(r.root.cert);
933 r.RootName = rootCertificate.subject.getField('CN').value;
934 +
935 + // If the "cert" name is not set, try to use the certificate CN instead (ok if the certificate is not wildcard).
936 + if (commonName == 'un-configured') {
937 + if (r.CommonName.startsWith('*.')) { console.log("ERROR: Must specify a server full domain name in Config.json->Settings->Cert when using a wildcard certificate."); process.exit(0); return; }
938 + commonName = r.CommonName;
939 + }
940 }
941
942 // Look for domains that have DNS names and load their certificates
943 r.dns = {};
944 for (i in config.domains) {
943 - if ((i != "") && (config.domains[i] != null) && (config.domains[i].dns != null)) {
945 + if ((i != '') && (config.domains[i] != null) && (config.domains[i].dns != null)) {
946 dnsname = config.domains[i].dns;
947 // Check if this domain matches a parent wildcard cert, if so, use the parent cert.
948 if (obj.compareCertificateNames(r.CommonNames, dnsname) == true) {
@@ -987,8 +989,8 @@ module.exports.CertificateOperations = function (parent) {
989 if (xorganizationField != null) { xorganization = xorganizationField.value; }
990 if (certargs == null) { commonName = r.CommonName; country = xcountry; organization = xorganization; }
991
990 - // Check if we have correct certificates
991 - if (obj.compareCertificateNames(r.CommonNames, commonName) == false) { forceWebCertGen = 1; }
992 + // Check if we have correct certificates.
993 + if (obj.compareCertificateNames(r.CommonNames, commonName) == false) { forceWebCertGen = 1; } else { r.CommonName = commonName; }
994 if (r.AmtMpsName != mpsCommonName) { forceMpsCertGen = 1; }
995
996 // If the certificates matches what we want, use them.
@@ -1140,7 +1142,7 @@ module.exports.CertificateOperations = function (parent) {
1142
1143 // Look for domains with DNS names that have no certificates and generated them.
1144 for (i in config.domains) {
1143 - if ((i != "") && (config.domains[i] != null) && (config.domains[i].dns != null)) {
1145 + if ((i != '') && (config.domains[i] != null) && (config.domains[i].dns != null)) {
1146 dnsname = config.domains[i].dns;
1147 // Check if this domain matches a parent wildcard cert, if so, use the parent cert.
1148 if (obj.compareCertificateNames(r.CommonNames, dnsname) == true) {